Added event_buffer object + 'add' functionality

Thu, 06 Sep 2007 23:22:55 -0400

author
Thomas Harning Jr <harningt@gmail.com>
date
Thu, 06 Sep 2007 23:22:55 -0400
changeset 26
a117895e867c
parent 25
5778073d2903
child 27
0d82e4f49a56

Added event_buffer object + 'add' functionality

include/event_buffer.h file | annotate | diff | comparison | revisions
src/event_buffer.c file | annotate | diff | comparison | revisions
src/luaevent.c file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/event_buffer.h	Thu Sep 06 23:22:55 2007 -0400
@@ -0,0 +1,18 @@
+/* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
+ * Licensed as LGPL - See doc/COPYING for details */
+#ifndef EVENT_BUFFER_H
+#define EVENT_BUFFER_H
+
+#include "luaevent.h"
+#include <lua.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <event.h>
+
+typedef struct {
+	struct evbuffer* buffer;
+} le_buffer;
+
+int event_buffer_register(lua_State* L);
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/event_buffer.c	Thu Sep 06 23:22:55 2007 -0400
@@ -0,0 +1,76 @@
+/* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
+ * Licensed as LGPL - See doc/COPYING for details */
+
+#include "event_buffer.h"
+#include <lauxlib.h>
+
+#define EVENT_BUFFER_MT "EVENT_BUFFER_MT"
+
+#define BUFFER_ADD_CHECK_INPUT_FIRST 1
+
+static le_buffer* event_buffer_get(lua_State* L, int idx) {
+	return (le_buffer*)luaL_checkudata(L, idx, EVENT_BUFFER_MT);
+}
+static int is_event_buffer(lua_State* L, int idx) {
+	int ret;
+	lua_getmetatable(L, idx);
+	luaL_getmetatable(L, EVENT_BUFFER_MT);
+	ret = lua_rawequal(L, -2, -1);
+	lua_pop(L, 2);
+	return ret;
+}
+
+/* LUA: buffer:add(...)
+	progressively adds items to the buffer
+		if arg[*] is string, treat as a string:format call
+		if arg[*] is a buffer, perform event_add_buffer
+	returns number of bytes added
+*/
+int event_buffer_add(lua_State* L) {
+	le_buffer* buf = event_buffer_get(L, 1);
+	struct evbuffer* buffer = buf->buffer;
+	int oldLength = EVBUFFER_LENGTH(buffer);
+	int last = lua_top(L);
+	int i;
+	for(i = 2; i <= last; i++) {
+		if(!lua_isstring(L, i) && !is_event_buffer(L, i))
+			luaL_argerror(L, i, "Argument is not a string or buffer object");
+/* Optionally perform checks and data loading separately to avoid overfilling the buffer */
+#if BUFFER_ADD_CHECK_INPUT_FIRST
+	}
+	for(i = 2; i <= last; i++) {
+#endif
+		if(lua_isstring(L, i)) {
+			size_t len;
+			const char* data = lua_tolstring(L, i, &len);
+			if(0 != evbuffer_add(buffer, data, len))
+				luaL_error(L, "Failed to add data to the buffer");
+		} else {
+			le_buffer* buf2 = event_buffer_get(L, i);
+			if(0 != evbuffer_add_buffer(buffer, buf2->buffer))
+				luaL_error(L, "Failed to move buffer-data to the buffer");
+		}
+	}
+	lua_pushinteger(L, EVBUFFER_LENGTH(buffer) - oldLength);
+	return 1;
+}
+
+static luaL_Reg buffer_funcs[] = {
+	{"add",event_buffer_add},
+	{NULL, NULL}
+};
+static luaL_Ref funcs[] = {
+	{NULL, NULL}
+};
+ 
+int event_buffer_register(lua_State* L) {
+	luaL_newmetatable(L, EVENT_BUFFER_MT);
+	lua_newtable(L);
+	luaL_register(L, NULL, buffer_funcs);
+	lua_setfield(L, -2, "__index");
+	lua_pop(L, 1);
+	
+	luaL_register(L, "luaevent.core.buffer", funcs);
+	return 0;
+}
+ 
\ No newline at end of file
--- a/src/luaevent.c	Thu Sep 06 00:05:55 2007 -0400
+++ b/src/luaevent.c	Thu Sep 06 23:22:55 2007 -0400
@@ -3,6 +3,7 @@
 
 #include "luaevent.h"
 #include "event_callback.h"
+#include "event_buffer.h"
 
 #include <lua.h>
 #include <lauxlib.h>
@@ -119,6 +120,7 @@
 int luaopen_luaevent_core(lua_State* L) {
 	/* Register external items */
 	event_callback_register(L);
+	event_buffer_register(L);
 	/* Setup metatable */
 	luaL_newmetatable(L, EVENT_BASE_MT);
 	lua_newtable(L);

mercurial