From 3ac2cb114a38ea683c1f0646ddba3ac49987b169 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 15 Aug 2021 16:15:04 +0200 Subject: Exported plop state to a structure --- Makefile | 2 +- main.c | 22 ++++++++++------------ plop.c | 6 ++++++ plop.h | 9 +++++++++ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index d0bf8a2..7facfe1 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/main.c b/main.c index 950aa03..94b8bde 100644 --- a/main.c +++ b/main.c @@ -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; } diff --git a/plop.c b/plop.c index ee24634..8a21919 100644 --- a/plop.c +++ b/plop.c @@ -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) diff --git a/plop.h b/plop.h index f3a1fe0..903fb1f 100644 --- a/plop.h +++ b/plop.h @@ -4,6 +4,15 @@ #include +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 *); -- cgit v1.1