diff options
-rw-r--r-- | include/luaevent.h | 1 | ||||
-rw-r--r-- | src/event_buffer.c | 43 |
2 files changed, 44 insertions, 0 deletions
diff --git a/include/luaevent.h b/include/luaevent.h index 2424756..ef3386b 100644 --- a/include/luaevent.h +++ b/include/luaevent.h @@ -15,6 +15,7 @@ typedef struct { le_base* event_base_get(lua_State* L, int idx); void load_timeval(double time, struct timeval *tv); +int getSocketFd(lua_State* L, int idx); int luaopen_luaevent(lua_State* L); diff --git a/src/event_buffer.c b/src/event_buffer.c index ff3bad3..4d65273 100644 --- a/src/event_buffer.c +++ b/src/event_buffer.c @@ -2,6 +2,7 @@ * Licensed as LGPL - See doc/COPYING for details */ #include "event_buffer.h" +#include "luaevent.h" #include <lauxlib.h> #include <malloc.h> @@ -169,6 +170,46 @@ static int event_buffer_drain(lua_State* L) { return 0; } +/* LUA: buffer:write + (integer/lightuserdata fd) - Attempts to write all the data out to the FD + (socket) - Attempts to write all the data out to the socket object +*/ +static int event_buffer_write(lua_State* L) { + le_buffer* buf = event_buffer_check(L, 1); + int ret; + if(lua_isnumber(L, 2)) { + ret = evbuffer_write(buf->buffer, lua_tointeger(L, 2)); + } else if(lua_islightuserdata(L, 2)) { + ret = evbuffer_write(buf->buffer, (int)(long)lua_touserdata(L, 2)); + } else if(lua_isuserdata(L, 2)) { + ret = evbuffer_write(buf->buffer, getSocketFd(L, 2)); + } else { + luaL_argerror(L, 2, "Unexpected data type. Expects: integer/lightuserdata/socket"); + } + lua_pushinteger(L, ret); + return 1; +} + +/* LUA: buffer:read + (integer/lightuserdata fd, len) - Attempts to read up to 'len' out of the FD + (socket, len) - Attempts to read up to 'len' out of the socket object +*/ +static int event_buffer_read(lua_State* L) { + le_buffer* buf = event_buffer_check(L, 1); + int len = luaL_checkinteger(L, 3); + int ret; + if(lua_isnumber(L, 2)) { + ret = evbuffer_read(buf->buffer, lua_tointeger(L, 2), len); + } else if(lua_islightuserdata(L, 2)) { + ret = evbuffer_read(buf->buffer, (int)(long)lua_touserdata(L, 2), len); + } else if(lua_isuserdata(L, 2)) { + ret = evbuffer_read(buf->buffer, getSocketFd(L, 2), len); + } else { + luaL_argerror(L, 2, "Unexpected data type. Expects: integer/lightuserdata/socket"); + } + lua_pushinteger(L, ret); + return 1; +} static luaL_Reg buffer_funcs[] = { {"add", event_buffer_add}, {"length", event_buffer_get_length}, @@ -176,6 +217,8 @@ static luaL_Reg buffer_funcs[] = { {"readline", event_buffer_readline}, {"drain", event_buffer_drain}, {"close", event_buffer_gc}, + {"read", event_buffer_read}, + {"write", event_buffer_write}, {NULL, NULL} }; static luaL_Reg funcs[] = { |