From 742b94fbd5756551ab5cd6f2ac1799dfbc7f5152 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 15 Aug 2021 17:57:18 +0200 Subject: Moved client lua thread init to where connection is initialized --- connection.c | 1 + connection.h | 1 + main.c | 2 +- plop.c | 21 +++++++++------------ plop.h | 2 +- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/connection.c b/connection.c index d321ae0..b0d9fcc 100644 --- a/connection.c +++ b/connection.c @@ -24,6 +24,7 @@ struct connection * connection_new(const int client) c->fd = client; c->ref = LUA_NOREF; c->L = NULL; + c->push = 0; return c; } diff --git a/connection.h b/connection.h index 14ec62b..c7e9676 100644 --- a/connection.h +++ b/connection.h @@ -7,6 +7,7 @@ struct connection int fd; int ref; lua_State * L; + int push; }; struct connection * connection_new(const int); diff --git a/main.c b/main.c index 568ea83..7f87ee4 100644 --- a/main.c +++ b/main.c @@ -108,7 +108,7 @@ int main(int argc, char ** argv) else { struct connection * c = (struct connection *) events[i].data.ptr; - if (-1 == plop_handle_client(plop.L, c)) + if (-1 == plop_handle_client(c)) { return 7; } diff --git a/plop.c b/plop.c index 1a204da..057208a 100644 --- a/plop.c +++ b/plop.c @@ -146,27 +146,18 @@ void plop_drop_thread(const int ref) } /// Handles client events. -/// \param L Server's Lua state /// \param c Connection associated with the client /// \return -1 if an error occured -int plop_handle_client(lua_State * L, struct connection * c) +int plop_handle_client(struct connection * c) { int nargs = 0; - if (NULL == c->L) + if (c->push) { - 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; + c->push = 0; } int nresults = 0; @@ -176,6 +167,7 @@ int plop_handle_client(lua_State * L, struct connection * c) { case LUA_OK: { + // TODO: If keep-alive, then reset c->push? plop_drop_thread(c->ref); connection_free(c); return 0; @@ -255,7 +247,12 @@ int plop_handle_server(const int efd, const int server) return -1; } + connection->L = lua_newthread(plop.L); + if (NULL == connection->L) + return -1; // TODO: Revisit error handling. + connection->ref = luaL_ref(plop.L, LUA_REGISTRYINDEX); connection->fd = client; + connection->push = 1; e.data.ptr = connection; if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, client, &e)) diff --git a/plop.h b/plop.h index b4dbed5..b1ed235 100644 --- a/plop.h +++ b/plop.h @@ -17,5 +17,5 @@ lua_State * plop_initialize_lua(void); int open_server(const char *, const char *); int plop_load_handler(lua_State *, const char *); void plop_drop_thread(const int); -int plop_handle_client(lua_State *, struct connection *); +int plop_handle_client(struct connection *); int plop_handle_server(const int, const int); -- cgit v1.1