summaryrefslogtreecommitdiffhomepage
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
parent906eb598f150931e821446e9dcf022ea9052ab36 (diff)
downloadplop-d4e5c2f12a284b64f6b68a994d63fe7babf7f46d.zip
plop-d4e5c2f12a284b64f6b68a994d63fe7babf7f46d.tar.gz
plop-d4e5c2f12a284b64f6b68a994d63fe7babf7f46d.tar.bz2
Switched back to poll from epoll
-rw-r--r--main.c57
-rw-r--r--plop.c38
-rw-r--r--plop.h6
3 files changed, 46 insertions, 55 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;
}
}
}
diff --git a/plop.c b/plop.c
index 3a438e6..a1de2d0 100644
--- a/plop.c
+++ b/plop.c
@@ -6,7 +6,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
@@ -21,7 +20,9 @@
struct plop plop = {
.handler = PLOP_DEFAULT_HANDLER,
.L = NULL,
- .efd = -1,
+ .fds = NULL,
+ .data = NULL,
+ .nfds = 0,
};
/// Initializes new Lua state for the server.
@@ -147,7 +148,7 @@ void plop_drop_thread(const int ref)
/// Handles client events.
/// \param c Connection associated with the client
-/// \return -1 if an error occured
+/// \return -1 if an error occured, 1 if expecting to continue
int plop_handle_client(struct connection * c)
{
int nargs = 0;
@@ -174,7 +175,7 @@ int plop_handle_client(struct connection * c)
}
case LUA_YIELD:
{
- return 0;
+ return 1;
}
default:
{
@@ -199,8 +200,14 @@ int plop_handle_client(struct connection * c)
/// \return -1 if an error occured
int plop_handle_server(const int server)
{
- struct epoll_event e;
- e.events = EPOLLIN | EPOLLOUT; // TODO: Add EPOLLHUP?
+ int i = 1; // Always skip server descriptor.
+ for (; i <= plop.nfds; ++i)
+ {
+ if (-1 == plop.fds[i].fd)
+ break;
+ if (plop.nfds == i)
+ return 0;
+ }
const int client = accept(server, NULL, NULL);
if (-1 == client)
@@ -234,30 +241,17 @@ int plop_handle_server(const int server)
}
}
- if (-1 == fcntl(client, F_SETFL, (fcntl(client, F_GETFL, 0) | O_NONBLOCK)))
- {
- close(client);
- return 0; // TODO: Retriage this error at some point.
- }
-
struct connection * connection = connection_new(client);
if (NULL == connection)
- {
return -1;
- }
-
connection->L = lua_newthread(plop.L);
if (NULL == connection->L)
- return -1; // TODO: Revisit error handling.
+ return -1; // TODO: Revisit error handling.
connection->ref = luaL_ref(plop.L, LUA_REGISTRYINDEX);
connection->fd = client;
connection->push = 1;
- e.data.ptr = connection;
-
- if (-1 == epoll_ctl(plop.efd, EPOLL_CTL_ADD, client, &e))
- {
- return -1;
- }
+ plop.fds[i].fd = client;
+ plop.data[i] = connection;
return 0;
}
diff --git a/plop.h b/plop.h
index 0cfe428..d2b68f5 100644
--- a/plop.h
+++ b/plop.h
@@ -1,5 +1,7 @@
#pragma once
+#include <poll.h>
+
#include <lua.h>
#include "connection.h"
@@ -8,7 +10,9 @@ struct plop
{
const char * handler;
lua_State * L;
- int efd;
+ struct pollfd * fds;
+ struct connection ** data;
+ int nfds;
};
extern struct plop plop;