diff options
author | Aki <please@ignore.pl> | 2021-08-15 17:57:18 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-08-15 17:57:18 +0200 |
commit | 742b94fbd5756551ab5cd6f2ac1799dfbc7f5152 (patch) | |
tree | e6837d41659cf34b7e3dc8f7184e0d5572cc1985 | |
parent | 52526c295ccdbe48b4965b269482c0a9fba6e658 (diff) | |
download | plop-742b94fbd5756551ab5cd6f2ac1799dfbc7f5152.zip plop-742b94fbd5756551ab5cd6f2ac1799dfbc7f5152.tar.gz plop-742b94fbd5756551ab5cd6f2ac1799dfbc7f5152.tar.bz2 |
Moved client lua thread init to where connection is initialized
-rw-r--r-- | connection.c | 1 | ||||
-rw-r--r-- | connection.h | 1 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | plop.c | 21 | ||||
-rw-r--r-- | 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); @@ -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; } @@ -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)) @@ -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); |