From 52526c295ccdbe48b4965b269482c0a9fba6e658 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 15 Aug 2021 17:38:16 +0200 Subject: Separated connection and server Lua state --- connection.c | 13 ++----------- connection.h | 4 ++-- main.c | 2 +- plop.c | 18 +++++++++++++----- plop.h | 3 ++- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/connection.c b/connection.c index 8fad3c5..d321ae0 100644 --- a/connection.c +++ b/connection.c @@ -8,13 +8,10 @@ #include /// Creates new connection. -/// \param L Server's Lua state /// \param client File descriptor of client's socket /// \return Pointer to connection or NULL if an error occured -struct connection * connection_new(lua_State * L, const int client) +struct connection * connection_new(const int client) { - (void) L; // TODO: Review if lua_State is still needed for connections and server handler. - struct connection * c = malloc(sizeof(struct connection)); if (NULL == c) @@ -32,15 +29,9 @@ struct connection * connection_new(lua_State * L, const int client) } /// Frees all resources associated with the connection. -/// \param L Server's Lua state /// \param c Connection to free -void connection_free(lua_State * L, struct connection * c) +void connection_free(struct connection * c) { - if (LUA_NOREF != c->ref) - { - luaL_unref(L, LUA_REGISTRYINDEX, c->ref); - } - close(c->fd); // TODO: Check for errors in close()? free(c); } diff --git a/connection.h b/connection.h index 88cd6b8..14ec62b 100644 --- a/connection.h +++ b/connection.h @@ -9,5 +9,5 @@ struct connection lua_State * L; }; -struct connection * connection_new(lua_State *, const int); -void connection_free(lua_State *, struct connection *); +struct connection * connection_new(const int); +void connection_free(struct connection *); diff --git a/main.c b/main.c index 2e0320c..568ea83 100644 --- a/main.c +++ b/main.c @@ -100,7 +100,7 @@ int main(int argc, char ** argv) { if (NULL == events[i].data.ptr) { - if (-1 == plop_handle_server(plop.L, plop.efd, server)) + if (-1 == plop_handle_server(plop.efd, server)) { return 6; } diff --git a/plop.c b/plop.c index f9d4de2..1a204da 100644 --- a/plop.c +++ b/plop.c @@ -138,6 +138,13 @@ int plop_load_handler(lua_State * L, const char * path) return LUA_OK; } +/// Drops Lua thread referenced by *ref* in the registry of main Lua state. +void plop_drop_thread(const int ref) +{ + if (LUA_NOREF != ref) + luaL_unref(plop.L, LUA_REGISTRYINDEX, ref); +} + /// Handles client events. /// \param L Server's Lua state /// \param c Connection associated with the client @@ -169,7 +176,8 @@ int plop_handle_client(lua_State * L, struct connection * c) { case LUA_OK: { - connection_free(L, c); + plop_drop_thread(c->ref); + connection_free(c); return 0; } case LUA_YIELD: @@ -187,18 +195,18 @@ int plop_handle_client(lua_State * L, struct connection * c) dprintf(2, "%s\n", err); lua_pop(c->L, 1); - connection_free(L, c); + plop_drop_thread(c->ref); + connection_free(c); return -1; } } } /// Accepts awaiting connections if any. -/// \param L Server's Lua state /// \param efd File descriptor for epoll's context /// \param server File descriptor for server socket /// \return -1 if an error occured -int plop_handle_server(lua_State * L, const int efd, const int server) +int plop_handle_server(const int efd, const int server) { struct epoll_event e; e.events = EPOLLIN | EPOLLOUT; // TODO: Add EPOLLHUP? @@ -241,7 +249,7 @@ int plop_handle_server(lua_State * L, const int efd, const int server) return 0; // TODO: Retriage this error at some point. } - struct connection * connection = connection_new(L, client); + struct connection * connection = connection_new(client); if (NULL == connection) { return -1; diff --git a/plop.h b/plop.h index ede7888..b4dbed5 100644 --- a/plop.h +++ b/plop.h @@ -16,5 +16,6 @@ extern struct plop plop; 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_server(lua_State *, const int, const int); +int plop_handle_server(const int, const int); -- cgit v1.1