#include <types/protocols.h>
/* different possible states for the fd */
-#define FD_STCLOSE 0
#define FD_STLISTEN 1
#define FD_STCONN 2
#define FD_STREADY 3
struct {
int (*f)(int fd); /* read/write function */
} cb[DIR_SIZE];
- void *owner; /* the session (or proxy) associated with this fd */
+ void *owner; /* the session (or proxy) associated with this fd, NULL if closed */
struct { /* used by pollers which support speculative polling */
unsigned char e; /* read and write events status. 4 bits, may be merged into flags' lower bits */
unsigned int s1; /* Position in spec list+1. 0=not in list. */
((e & EPOLLHUP) ? FD_POLL_HUP : 0);
if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_RD)) {
- if (fdtab[fd].state == FD_STCLOSE)
+ if (!fdtab[fd].owner)
continue;
if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))
fdtab[fd].cb[DIR_RD].f(fd);
}
if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_WR)) {
- if (fdtab[fd].state == FD_STCLOSE)
+ if (!fdtab[fd].owner)
continue;
if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP))
fdtab[fd].cb[DIR_WR].f(fd);
fd = kev[count].ident;
if (kev[count].filter == EVFILT_READ) {
if (FD_ISSET(fd, fd_evts[DIR_RD])) {
- if (fdtab[fd].state == FD_STCLOSE)
+ if (!fdtab[fd].owner)
continue;
fdtab[fd].ev |= FD_POLL_IN;
fdtab[fd].cb[DIR_RD].f(fd);
}
} else if (kev[count].filter == EVFILT_WRITE) {
if (FD_ISSET(fd, fd_evts[DIR_WR])) {
- if (fdtab[fd].state == FD_STCLOSE)
+ if (!fdtab[fd].owner)
continue;
fdtab[fd].ev |= FD_POLL_OUT;
fdtab[fd].cb[DIR_WR].f(fd);
status--;
if (FD_ISSET(fd, fd_evts[DIR_RD])) {
- if (fdtab[fd].state == FD_STCLOSE)
+ if (!fdtab[fd].owner)
continue;
if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))
fdtab[fd].cb[DIR_RD].f(fd);
}
if (FD_ISSET(fd, fd_evts[DIR_WR])) {
- if (fdtab[fd].state == FD_STCLOSE)
+ if (!fdtab[fd].owner)
continue;
if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP))
fdtab[fd].cb[DIR_WR].f(fd);
* seen first. Moreover, system buffers will be flushed faster.
*/
if (FD_ISSET(fd, tmp_evts[DIR_RD])) {
- if (fdtab[fd].state == FD_STCLOSE)
+ if (!fdtab[fd].owner)
continue;
fdtab[fd].ev |= FD_POLL_IN;
fdtab[fd].cb[DIR_RD].f(fd);
}
if (FD_ISSET(fd, tmp_evts[DIR_WR])) {
- if (fdtab[fd].state == FD_STCLOSE)
+ if (!fdtab[fd].owner)
continue;
fdtab[fd].ev |= FD_POLL_OUT;
fdtab[fd].cb[DIR_WR].f(fd);
int ret;
#if DEBUG_DEV
- if (fdtab[fd].state == FD_STCLOSE) {
+ if (!fdtab[fd].owner) {
fprintf(stderr, "sepoll.fd_isset called on closed fd #%d.\n", fd);
ABORT_NOW();
}
unsigned int i;
#if DEBUG_DEV
- if (fdtab[fd].state == FD_STCLOSE) {
+ if (!fdtab[fd].owner) {
fprintf(stderr, "sepoll.fd_set called on closed fd #%d.\n", fd);
ABORT_NOW();
}
unsigned int i;
#if DEBUG_DEV
- if (fdtab[fd].state == FD_STCLOSE) {
+ if (!fdtab[fd].owner) {
fprintf(stderr, "sepoll.fd_clr called on closed fd #%d.\n", fd);
ABORT_NOW();
}
((e & EPOLLHUP) ? FD_POLL_HUP : 0);
if ((fdtab[fd].spec.e & FD_EV_MASK_R) == FD_EV_WAIT_R) {
- if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
+ if (!fdtab[fd].owner || fdtab[fd].state == FD_STERROR)
continue;
if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))
fdtab[fd].cb[DIR_RD].f(fd);
}
if ((fdtab[fd].spec.e & FD_EV_MASK_W) == FD_EV_WAIT_W) {
- if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
+ if (!fdtab[fd].owner || fdtab[fd].state == FD_STERROR)
continue;
if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR))
fdtab[fd].cb[DIR_WR].f(fd);
}
/* one callback might already have closed the fd by itself */
- if (fdtab[fd].state == FD_STCLOSE)
+ if (!fdtab[fd].owner)
continue;
if (!(fdtab[fd].spec.e & (FD_EV_RW_SL|FD_EV_RW_PL))) {
port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
fdinfo[fd].port_range = NULL;
close(fd);
- fdtab[fd].state = FD_STCLOSE;
+ fdtab[fd].owner = NULL;
- while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE))
+ while ((maxfd-1 >= 0) && !fdtab[maxfd-1].owner)
maxfd--;
}
*/
void init(int argc, char **argv)
{
- int i;
int arg_mode = 0; /* MODE_DEBUG, ... */
char *tmp;
char *cfg_pidfile = NULL;
sizeof(struct fdinfo) * (global.maxsock));
fdtab = (struct fdtab *)calloc(1,
sizeof(struct fdtab) * (global.maxsock));
- for (i = 0; i < global.maxsock; i++) {
- fdtab[i].state = FD_STCLOSE;
- }
-
/*
* Note: we could register external pollers here.
* Built-in pollers have been registered before main().
send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
}
- if (fdtab[cfd].state != FD_STCLOSE)
+ if (fdtab[cfd].owner)
fd_delete(cfd);
else
close(cfd);