summaryrefslogtreecommitdiffhomepage
path: root/plop.c
diff options
context:
space:
mode:
Diffstat (limited to 'plop.c')
-rw-r--r--plop.c38
1 files changed, 16 insertions, 22 deletions
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;
}