diff options
Diffstat (limited to 'plop.c')
-rw-r--r-- | plop.c | 51 |
1 files changed, 33 insertions, 18 deletions
@@ -16,8 +16,7 @@ #include <lualib.h> #include "connection.h" -#include "request.h" -#include "response.h" +#include "stream.h" /// Initializes new Lua state for the server. /// \return Lua state @@ -139,27 +138,43 @@ int plop_load_handler(lua_State * L, const char * path) /// \return -1 if an error occured int plop_handle_client(lua_State * L, struct epoll_event * event) { - struct connection * connection = (struct connection *) event->data.ptr; + struct connection * c = (struct connection *) event->data.ptr; + int nargs = 0; - if (-1 == parse_request(connection)) + if (NULL == c->L) { - lua_newtable(connection->L); - lua_pushstring(connection->L, "status"); - lua_pushinteger(connection->L, 400); // TODO: How about a function that generates error responses? - lua_rawset(connection->L, -3); - } - else // TODO: 0 may mean EAGAIN, stuff will be bad very soon from here. - { - // TODO: Push the handler to stack earlier to avoid shifting it. - lua_getglobal(connection->L, "handler"); - lua_insert(connection->L, 1); - lua_call(connection->L, 5, 1); + c->L = lua_newthread(L); + + if (NULL == c->L) + { + return -1; // TODO: Fail only this connection? + } + + c->ref = luaL_ref(L, LUA_REGISTRYINDEX); + + lua_getglobal(c->L, "handler"); + stream_push_new(c->L, c->fd); + nargs = 1; } - int result = response_send(connection->L, connection->fd); - connection_free(L, connection); + int result = lua_resume(c->L, NULL, nargs); + connection_free(L, c); // TODO: Allow consistent connections? - return result; + switch (result) + { + case LUA_OK: + { + return 0; + } + case LUA_YIELD: + { + return 0; + } + default: + { + return -1; + } + } } /// Accepts awaiting connections if any. |