summaryrefslogtreecommitdiffhomepage
path: root/main.c
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-08-15 20:04:35 +0200
committerAki <please@ignore.pl>2021-08-15 20:04:35 +0200
commitd4e5c2f12a284b64f6b68a994d63fe7babf7f46d (patch)
tree465b7f22baa92ada276b4f5be1a15a618225f100 /main.c
parent906eb598f150931e821446e9dcf022ea9052ab36 (diff)
downloadplop-d4e5c2f12a284b64f6b68a994d63fe7babf7f46d.zip
plop-d4e5c2f12a284b64f6b68a994d63fe7babf7f46d.tar.gz
plop-d4e5c2f12a284b64f6b68a994d63fe7babf7f46d.tar.bz2
Switched back to poll from epoll
Diffstat (limited to 'main.c')
-rw-r--r--main.c57
1 files changed, 25 insertions, 32 deletions
diff --git a/main.c b/main.c
index ee7c796..72ddc49 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
+#include <poll.h>
#include <stdio.h>
-#include <sys/epoll.h>
#include <unistd.h>
#include <lua.h>
@@ -67,51 +67,44 @@ int main(int argc, char ** argv)
return 2;
}
- plop.efd = epoll_create1(0);
-
- if (-1 == plop.efd)
+ static const int nfds = 50; // TODO-maybe: Expand, allow setting default starting value?
+ struct pollfd fds[nfds];
+ struct connection * data[nfds];
+ for (int i = 0; i < nfds; ++i)
{
- return 3;
+ fds[i].fd = -1;
+ fds[i].events = POLLIN | POLLOUT;
+ data[i] = NULL;
}
-
- struct epoll_event e;
- e.events = EPOLLIN;
- 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(plop.efd, EPOLL_CTL_ADD, server, &e))
- {
+ const int server = open_server(NULL, service);
+ if (-1 == server)
return 4;
- }
-
- static const int MAX_EVENTS = 20;
- struct epoll_event events[MAX_EVENTS];
-
+ fds[0].fd = server;
+ fds[0].events = POLLIN;
+ plop.fds = fds;
+ plop.data = data;
+ plop.nfds = nfds;
while (1)
{
- int evc = epoll_wait(plop.efd, events, MAX_EVENTS, -1);
-
- if (-1 == evc)
- {
+ int ret = poll(fds, nfds, -1); // TODO-maybe: Specify timeout and allow timers?
+ if (-1 == ret)
return 5;
- }
-
- for (int i = 0; i < evc; ++i)
+ for (int i = 0; i < nfds; ++i)
{
- if (NULL == events[i].data.ptr)
+ if (-1 == fds[i].fd)
+ continue;
+ if (NULL == data[i])
{
if (-1 == plop_handle_server(server))
- {
return 6;
- }
}
else
{
- struct connection * c = (struct connection *) events[i].data.ptr;
- if (-1 == plop_handle_client(c))
- {
+ ret = plop_handle_client(data[i]);
+ if (-1 == ret)
return 7;
- }
+ else if (0 == ret)
+ fds[i].fd = -1;
}
}
}