From: Egor Date: Thu, 5 Jun 2025 16:33:33 +0000 (+0300) Subject: poll: change the net_pollfd public interface X-Git-Url: https://git.patchysicp.xyz/?a=commitdiff_plain;h=7dee3f0ffe71ec99262d54d55e641411f0125165;p=patchyircd.git poll: change the net_pollfd public interface Since we're copying the structure out in ::get we might as well keep it separate from the internal structure (which really should be combined with polldata). Also rename it to just pollfd since we no longer have name conflict with poll.h. --- diff --git a/src/main.cpp b/src/main.cpp index c316fe5..d2311a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -392,18 +392,16 @@ int main(int argc, char *argv[]) netpoll::begin(); job_process(); - int ev; - netpoll::net_pollfd pfd; - while ((ev = netpoll::get(&pfd))) { - switch (pfd.type) { + for (netpoll::event ev; netpoll::get(ev);) { + int isr = ev.revents & UV_READABLE; + int isw = ev.revents & UV_WRITABLE; + switch (ev.type) { case SOCKTYPE_LISTENER: - if (ev & UV_READABLE) - accept_from_sock((struct listener *)pfd.assoc); + if (isr) + accept_from_sock((struct listener *)ev.assoc); break; case SOCKTYPE_CLIENT: { - struct client_data *cl = (struct client_data *)pfd.assoc; - int isr = ev & UV_READABLE; - int isw = ev & UV_WRITABLE; + struct client_data *cl = (struct client_data *)ev.assoc; /* NOTE: recvProc can kill the client, so writing should be done first. */ if ((cl->want_read_for_write && isr) || (cl->want_write_for_write && isw)) sendClient(cl, 0, 0); @@ -412,9 +410,7 @@ int main(int argc, char *argv[]) break; } case SOCKTYPE_DYING: { - struct client_data *cl = (struct client_data *)pfd.assoc; - int isr = ev & UV_READABLE; - int isw = ev & UV_WRITABLE; + struct client_data *cl = (struct client_data *)ev.assoc; if (cl->wbufLen) if ((cl->want_read_for_write && isr) || (cl->want_write_for_write && isw)) sendClient(cl, 0, 0); diff --git a/src/netpoll.cpp b/src/netpoll.cpp index b7e00f7..c94b511 100644 --- a/src/netpoll.cpp +++ b/src/netpoll.cpp @@ -6,8 +6,13 @@ #include namespace netpoll { +struct pollfd { + void *assoc; + SOCKET fd; + int type; +}; #define NETPOLL_MAXFDS 1024 -static struct net_pollfd fds[NETPOLL_MAXFDS] = {}; +static struct pollfd fds[NETPOLL_MAXFDS] = {}; static int nfds = 0; #define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member))) @@ -76,17 +81,20 @@ void begin(void) } uv_run(uvloop, UV_RUN_ONCE); } -int get(struct net_pollfd *out) +bool get(event &out) { - int ev = 0; - while (!ev) { - if (lastptr == nfds) - return 0; - ev = uvpoll[lastptr]->revents & (UV_READABLE | UV_WRITABLE); + while (lastptr != nfds && !uvpoll[lastptr]->revents) { lastptr++; } - *out = fds[lastptr-1]; - return ev; + if (lastptr == nfds) + return false; + pollfd &pfd = fds[lastptr]; + out.assoc = pfd.assoc; + out.fd = pfd.fd; + out.type = pfd.type; + out.revents = uvpoll[lastptr]->revents; + lastptr++; + return true; } static int search(SOCKET fd, bool add) { @@ -101,7 +109,7 @@ static int search(SOCKET fd, bool add) { abortf("netpoll::add: NETPOLL_MAXFDS limit\n"); memmove(it+1, it, sizeof(fds[0])*(fds+nfds-it)); nfds++; - *it = net_pollfd{}; + *it = pollfd{}; it->fd = fd; } return it - fds; diff --git a/src/netpoll.h b/src/netpoll.h index 6f72823..cc38c14 100644 --- a/src/netpoll.h +++ b/src/netpoll.h @@ -5,10 +5,10 @@ #include "netsock.h" #include namespace netpoll { -struct net_pollfd { +struct event { void *assoc; SOCKET fd; - int type; + int type, revents; }; void init(void); void fini(void); @@ -17,13 +17,12 @@ void del_and_close(SOCKET fd); void reg(SOCKET fd, int ev); void change_type(SOCKET fd, int type); void begin(void); -int get(struct net_pollfd *out); +bool get(event &out); } #define SOCKTYPE_LISTENER 0 -#define SOCKTYPE_SELFPIPE 1 -#define SOCKTYPE_CLIENT 2 -#define SOCKTYPE_DYING 3 -#define SOCKTYPE_MAX 4 +#define SOCKTYPE_CLIENT 1 +#define SOCKTYPE_DYING 2 +#define SOCKTYPE_MAX 3 #endif