summaryrefslogtreecommitdiffhomepage
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c54
1 files changed, 36 insertions, 18 deletions
diff --git a/main.c b/main.c
index 06f44d4..ebe6bfe 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
-#include <poll.h>
#include <string.h>
+#include <sys/epoll.h>
#include <lauxlib.h>
#include <lua.h>
@@ -26,33 +26,51 @@ int main(int argc, char ** argv)
return 2; // TODO: Document error codes/print usage information
}
- static const int max_clients = 200;
+ const int efd = epoll_create1(0);
- int fdc = 0;
- struct request * requests[max_clients];
- struct pollfd fdv[max_clients];
-
- memset(requests, 0, sizeof(requests));
- memset(fdv, 0, sizeof(fdv));
+ if (-1 == efd)
+ {
+ return 3; // TODO: Handle errors properly
+ }
- fdv[0].fd = make_server(NULL, argv[1]);
- fdv[0].events = POLLIN;
- fdc++;
+ struct epoll_event e;
+ e.events = EPOLLIN;
+ e.data.ptr = NULL; // TODO: Consider putting server's Lua state in here?
+ const int server = make_server(NULL, argv[1]); // TODO: Check server's fd before ctl?
- if (-1 == fdv[0].fd)
+ if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, server, &e))
{
- return 1; // TODO: Document error codes/print usage information
+ return 4; // TODO: Handle errors properly
}
- while (0 < fdc && -1 != poll(fdv, fdc, -1))
+ static const int MAX_EVENTS = 20;
+ struct epoll_event events[MAX_EVENTS];
+
+ while (1)
{
- int shift_by = 0;
+ int evc = epoll_wait(efd, events, MAX_EVENTS, -1);
- for (int i = 1; i < fdc; ++i)
+ if (-1 == evc)
{
- shift_by = handle_client(L, &fdv[i], &requests[i], shift_by);
+ return 5; // TODO: Handle errors properly
}
- fdc = handle_server(fdv, fdc - shift_by, max_clients);
+ for (int i = 0; i < evc; ++i)
+ {
+ if (NULL == events[i].data.ptr)
+ {
+ if (-1 == handle_server(L, efd, server))
+ {
+ return 6; // TODO: Handle errors properly
+ }
+ }
+ else
+ {
+ if (-1 == handle_client(L, &events[i]))
+ {
+ return 7; // TODO: Handle errors properly
+ }
+ }
+ }
}
}