diff options
author | Aki <please@ignore.pl> | 2021-08-15 16:15:04 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-08-15 16:15:04 +0200 |
commit | 3ac2cb114a38ea683c1f0646ddba3ac49987b169 (patch) | |
tree | f0435c6dd7a0b6ab98e76a1b73ceb236a65bddf6 | |
parent | a0473749524261367d91cee981d76e099bac6f83 (diff) | |
download | plop-3ac2cb114a38ea683c1f0646ddba3ac49987b169.zip plop-3ac2cb114a38ea683c1f0646ddba3ac49987b169.tar.gz plop-3ac2cb114a38ea683c1f0646ddba3ac49987b169.tar.bz2 |
Exported plop state to a structure
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | main.c | 22 | ||||
-rw-r--r-- | plop.c | 6 | ||||
-rw-r--r-- | plop.h | 9 |
4 files changed, 26 insertions, 13 deletions
@@ -11,7 +11,7 @@ all: plop plop: connection.o main.o plop.o stream.o -main.o: CFLAGS+=-DPLOP_DEFAULT_HANDLER=\"$(PLOP_DEFAULT_HANDLER)\" +main.o plop.o: CFLAGS+=-DPLOP_DEFAULT_HANDLER=\"$(PLOP_DEFAULT_HANDLER)\" main.o: plop.h plop.o: connection.h plop.h stream.h connection.o: connection.h @@ -50,28 +50,26 @@ int main(int argc, char ** argv) } } - const char * handler_path = PLOP_DEFAULT_HANDLER; - if (optind < argc) { - handler_path = argv[optind]; + plop.handler = argv[optind]; } - lua_State * L = plop_initialize_lua(); + plop.L = plop_initialize_lua(); - if (NULL == L) + if (NULL == plop.L) { return 9; } - if (LUA_OK != plop_load_handler(L, handler_path)) + if (LUA_OK != plop_load_handler(plop.L, plop.handler)) { return 2; } - const int efd = epoll_create1(0); + plop.efd = epoll_create1(0); - if (-1 == efd) + if (-1 == plop.efd) { return 3; } @@ -81,7 +79,7 @@ int main(int argc, char ** argv) e.data.ptr = NULL; // TODO: Consider putting server's Lua state in here? const int server = open_server(NULL, service); // TODO: Check server's fd before ctl? - if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, server, &e)) + if (-1 == epoll_ctl(plop.efd, EPOLL_CTL_ADD, server, &e)) { return 4; } @@ -91,7 +89,7 @@ int main(int argc, char ** argv) while (1) { - int evc = epoll_wait(efd, events, MAX_EVENTS, -1); + int evc = epoll_wait(plop.efd, events, MAX_EVENTS, -1); if (-1 == evc) { @@ -102,14 +100,14 @@ int main(int argc, char ** argv) { if (NULL == events[i].data.ptr) { - if (-1 == plop_handle_server(L, efd, server)) + if (-1 == plop_handle_server(plop.L, plop.efd, server)) { return 6; } } else { - if (-1 == plop_handle_client(L, &events[i])) + if (-1 == plop_handle_client(plop.L, &events[i])) { return 7; } @@ -18,6 +18,12 @@ #include "connection.h" #include "stream.h" +struct plop plop = { + .handler = PLOP_DEFAULT_HANDLER, + .L = NULL, + .efd = -1, +}; + /// Initializes new Lua state for the server. /// \return Lua state lua_State * plop_initialize_lua(void) @@ -4,6 +4,15 @@ #include <lua.h> +struct plop +{ + const char * handler; + lua_State * L; + int efd; +}; + +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 *); |