diff options
author | Aki <please@ignore.pl> | 2021-08-15 17:38:16 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-08-15 17:38:16 +0200 |
commit | 52526c295ccdbe48b4965b269482c0a9fba6e658 (patch) | |
tree | d0cb9a1432e41525f8d724ccc68d9f3dc43184bd | |
parent | 3070ac071925261691d785b78450483f996d2e51 (diff) | |
download | plop-52526c295ccdbe48b4965b269482c0a9fba6e658.zip plop-52526c295ccdbe48b4965b269482c0a9fba6e658.tar.gz plop-52526c295ccdbe48b4965b269482c0a9fba6e658.tar.bz2 |
Separated connection and server Lua state
-rw-r--r-- | connection.c | 13 | ||||
-rw-r--r-- | connection.h | 4 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | plop.c | 18 | ||||
-rw-r--r-- | 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 <lua.h> /// 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 *); @@ -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; } @@ -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; @@ -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); |