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);
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);
#include <algorithm>
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)))
}
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) {
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;
#include "netsock.h"
#include <uv.h>
namespace netpoll {
-struct net_pollfd {
+struct event {
void *assoc;
SOCKET fd;
- int type;
+ int type, revents;
};
void init(void);
void fini(void);
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