summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-08-15 16:15:04 +0200
committerAki <please@ignore.pl>2021-08-15 16:15:04 +0200
commit3ac2cb114a38ea683c1f0646ddba3ac49987b169 (patch)
treef0435c6dd7a0b6ab98e76a1b73ceb236a65bddf6
parenta0473749524261367d91cee981d76e099bac6f83 (diff)
downloadplop-3ac2cb114a38ea683c1f0646ddba3ac49987b169.zip
plop-3ac2cb114a38ea683c1f0646ddba3ac49987b169.tar.gz
plop-3ac2cb114a38ea683c1f0646ddba3ac49987b169.tar.bz2
Exported plop state to a structure
-rw-r--r--Makefile2
-rw-r--r--main.c22
-rw-r--r--plop.c6
-rw-r--r--plop.h9
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 <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 *);