diff options
-rw-r--r-- | main.c | 57 | ||||
-rw-r--r-- | plop.c | 38 | ||||
-rw-r--r-- | plop.h | 6 |
3 files changed, 46 insertions, 55 deletions
@@ -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; } } } @@ -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; } @@ -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; |