static void event_enable_read(ACL_EVENT *eventp, ACL_VSTREAM *stream, int timeout, ACL_EVENT_NOTIFY_RDWR callback, void *context) { const char *myname = "event_enable_read"; EVENT_KERNEL *ev = (EVENT_KERNEL *) eventp; ACL_EVENT_FDTABLE *fdp = (ACL_EVENT_FDTABLE *) stream->fdp; ACL_SOCKET sockfd = ACL_VSTREAM_SOCK(stream); if (fdp == NULL) { fdp = event_fdtable_alloc(); fdp->flag = EVENT_FDTABLE_FLAG_ADD_READ | EVENT_FDTABLE_FLAG_EXPT; fdp->stream = stream; acl_ring_append(&ev->fdp_delay_list, &fdp->delay_entry); fdp->flag |= EVENT_FDTABLE_FLAG_DELAY_OPER; stream->fdp = (void *) fdp; /* 添加流关闭时的回调函数 */ acl_vstream_add_close_handle(stream, stream_on_close, eventp); } else if ((fdp->flag & EVENT_FDTABLE_FLAG_ADD_READ)) { goto END; } else if ((fdp->flag & EVENT_FDTABLE_FLAG_DEL_READ)) { /* 停止禁止读监听过程 */ acl_assert((fdp->flag & EVENT_FDTABLE_FLAG_READ)); /* 重新启用读监听过程, 因为之前的过程是正在拆除读监听过程但 * 还没有正式拆除,所以只需要清除拆除标志位即可 */ fdp->flag &= ~EVENT_FDTABLE_FLAG_DEL_READ; } else if (!(fdp->flag & EVENT_FDTABLE_FLAG_READ)) { fdp->flag |= EVENT_FDTABLE_FLAG_ADD_READ; if (!(fdp->flag & EVENT_FDTABLE_FLAG_DELAY_OPER)) { acl_ring_append(&ev->fdp_delay_list, &fdp->delay_entry); fdp->flag |= EVENT_FDTABLE_FLAG_DELAY_OPER; } } END: if (fdp->fdidx == -1) { fdp->fdidx = eventp->fdcnt; eventp->fdtabs[eventp->fdcnt++] = fdp; } if (eventp->maxfd != ACL_SOCKET_INVALID && eventp->maxfd < sockfd) eventp->maxfd = sockfd; if (fdp->r_callback != callback || fdp->r_context != context) { fdp->r_callback = callback; fdp->r_context = context; } if (timeout > 0) { fdp->r_timeout = timeout * 1000000; fdp->r_ttl = eventp->present + fdp->r_timeout; } else { fdp->r_ttl = 0; fdp->r_timeout = 0; } }
static void event_enable_write(ACL_EVENT *eventp, ACL_VSTREAM *stream, int timeout, ACL_EVENT_NOTIFY_RDWR callback, void *context) { EVENT_KERNEL *ev = (EVENT_KERNEL *) eventp; ACL_EVENT_FDTABLE *fdp = (ACL_EVENT_FDTABLE *) stream->fdp; ACL_SOCKET sockfd = ACL_VSTREAM_SOCK(stream); if (fdp == NULL) { fdp = event_fdtable_alloc(); fdp->flag = EVENT_FDTABLE_FLAG_ADD_WRITE | EVENT_FDTABLE_FLAG_EXPT; fdp->stream = stream; acl_ring_append(&ev->fdp_delay_list, &fdp->delay_entry); fdp->flag |= EVENT_FDTABLE_FLAG_DELAY_OPER; stream->fdp = (void *) fdp; /* 添加流关闭时的回调函数 */ acl_vstream_add_close_handle(stream, stream_on_close, eventp); #ifdef USE_FDMAP acl_fdmap_add(ev->fdmap, sockfd, fdp); #endif } else if ((fdp->flag & EVENT_FDTABLE_FLAG_ADD_WRITE)) { goto END; } else if ((fdp->flag & EVENT_FDTABLE_FLAG_DEL_WRITE)) { acl_assert((fdp->flag & EVENT_FDTABLE_FLAG_WRITE)); fdp->flag &= ~EVENT_FDTABLE_FLAG_DEL_WRITE; } else if (!(fdp->flag & EVENT_FDTABLE_FLAG_WRITE)) { fdp->flag |= EVENT_FDTABLE_FLAG_ADD_WRITE; if (!(fdp->flag & EVENT_FDTABLE_FLAG_DELAY_OPER)) { acl_ring_append(&ev->fdp_delay_list, &fdp->delay_entry); fdp->flag |= EVENT_FDTABLE_FLAG_DELAY_OPER; } } END: if (fdp->fdidx == -1) { fdp->fdidx = eventp->fdcnt; eventp->fdtabs[eventp->fdcnt++] = fdp; } if (eventp->maxfd != ACL_SOCKET_INVALID && eventp->maxfd < sockfd) eventp->maxfd = sockfd; if (fdp->w_callback != callback || fdp->w_context != context) { fdp->w_callback = callback; fdp->w_context = context; } if (timeout > 0) { fdp->w_timeout = timeout * 1000000; fdp->w_ttl = eventp->present + fdp->w_timeout; } else { fdp->w_ttl = 0; fdp->w_timeout = 0; } }
static ACL_EVENT_FDTABLE *read_enable(ACL_EVENT *eventp, ACL_VSTREAM *stream, int timeout, ACL_EVENT_NOTIFY_RDWR callback, void *context) { EVENT_POLL *ev = (EVENT_POLL *) eventp; ACL_SOCKET sockfd = ACL_VSTREAM_SOCK(stream); ACL_EVENT_FDTABLE *fdp = (ACL_EVENT_FDTABLE *) stream->fdp; if (fdp == NULL) { fdp = event_fdtable_alloc(); fdp->stream = stream; stream->fdp = (void *) fdp; acl_vstream_add_close_handle(stream, stream_on_close, eventp); acl_fdmap_add(ev->fdmap, sockfd, fdp); } if (fdp->fdidx == -1) { fdp->fdidx = eventp->fdcnt; eventp->fdtabs[eventp->fdcnt++] = fdp; } if ((fdp->flag & EVENT_FDTABLE_FLAG_WRITE)) { fdp->flag |= EVENT_FDTABLE_FLAG_READ; ev->fds[fdp->fdidx].events |= POLLIN | POLLHUP | POLLERR; } else { fdp->flag = EVENT_FDTABLE_FLAG_READ | EVENT_FDTABLE_FLAG_EXPT; ev->fds[fdp->fdidx].events = POLLIN | POLLHUP | POLLERR; } ev->fds[fdp->fdidx].fd = sockfd; if (eventp->maxfd != ACL_SOCKET_INVALID && eventp->maxfd < sockfd) eventp->maxfd = sockfd; if (fdp->r_callback != callback || fdp->r_context != context) { fdp->r_callback = callback; fdp->r_context = context; } if (timeout > 0) { fdp->r_timeout = ((acl_int64) timeout) * 1000000; fdp->r_ttl = eventp->present + fdp->r_timeout; } else { fdp->r_ttl = 0; fdp->r_timeout = 0; } return fdp; }
static void event_enable_write(ACL_EVENT *eventp, ACL_VSTREAM *stream, int timeout, ACL_EVENT_NOTIFY_RDWR callback, void *context) { EVENT_SELECT *ev = (EVENT_SELECT *) eventp; ACL_SOCKET sockfd = ACL_VSTREAM_SOCK(stream); ACL_EVENT_FDTABLE *fdp = (ACL_EVENT_FDTABLE *) stream->fdp; if (fdp == NULL) { fdp = event_fdtable_alloc(); fdp->stream = stream; stream->fdp = (void *) fdp; acl_vstream_add_close_handle(stream, stream_on_close, eventp); } if (fdp->fdidx == -1) { fdp->fdidx = eventp->fdcnt; eventp->fdtabs[eventp->fdcnt++] = fdp; } if ((fdp->flag & EVENT_FDTABLE_FLAG_READ)) fdp->flag |= EVENT_FDTABLE_FLAG_WRITE; else fdp->flag = EVENT_FDTABLE_FLAG_WRITE | EVENT_FDTABLE_FLAG_EXPT; FD_SET(sockfd, &ev->wmask); FD_SET(sockfd, &ev->xmask); if (eventp->maxfd != ACL_SOCKET_INVALID && eventp->maxfd < sockfd) eventp->maxfd = sockfd; if (fdp->w_callback != callback || fdp->w_context != context) { fdp->w_callback = callback; fdp->w_context = context; } if (timeout > 0) { fdp->w_timeout = ((acl_int64) timeout) * 1000000; fdp->w_ttl = eventp->present + fdp->w_timeout; } else { fdp->w_ttl = 0; fdp->w_timeout = 0; } }
void master_udp::service_main(ACL_VSTREAM *stream, char*, char**) { acl_assert(__mu != NULL); socket_stream* ss = (socket_stream*) stream->context; if (ss == NULL) { // 当本函数第一次被调用时,需要打开 socket_stream 流 ss = NEW socket_stream(); if (ss->open(stream) == false) logger_fatal("open stream error!"); stream->context = ss; acl_vstream_add_close_handle(stream, on_close, ss); } #ifndef ACL_WINDOWS if (__mu->daemon_mode_) acl_watchdog_pat(); // 必须通知 acl_master 框架一下 #endif __mu->on_read(ss); }