void process_iod_events(struct npool *nsp, struct niod *nsi, int ev) { int i = 0; /* store addresses of the pointers to the first elements of each kind instead * of storing the values, as a connect can add a read for instance */ gh_lnode_t **start_elems[] = { &nsi->first_connect, &nsi->first_read, &nsi->first_write, #if HAVE_PCAP &nsi->first_pcap_read, #endif NULL }; gh_list_t *evlists[] = { &nsp->connect_events, &nsp->read_events, &nsp->write_events, #if HAVE_PCAP &nsp->pcap_read_events, #endif NULL }; assert(nsp == nsi->nsp); nsock_log_debug_all("Processing events on IOD %lu (ev=%d)", nsi->id, ev); /* We keep the events separate because we want to handle them in the * order: connect => read => write => timer for several reasons: * * 1) Makes sure we have gone through all the net i/o events before * a timer expires (would be a shame to timeout after the data was * available but before we delivered the events * * 2) The connect() results often lead to a read or write that can be * processed in the same cycle. In the same way, read() often * leads to write(). */ for (i = 0; evlists[i] != NULL; i++) { gh_lnode_t *current, *next, *last; /* for each list, get the last event and don't look past it as an event * could add another event in the same list and so on... */ last = gh_list_last_elem(evlists[i]); for (current = *start_elems[i]; current != NULL && gh_lnode_prev(current) != last; current = next) { struct nevent *nse; #if HAVE_PCAP if (evlists[i] == &nsi->nsp->pcap_read_events) nse = lnode_nevent2(current); else #endif nse = lnode_nevent(current); /* events are grouped by IOD. Break if we're done with the events for the * current IOD */ if (nse->iod != nsi) break; process_event(nsp, evlists[i], nse, ev); next = gh_lnode_next(current); if (nse->event_done) { /* event is done, remove it from the event list and update IOD pointers * to the first events of each kind */ update_first_events(nse); gh_list_remove(evlists[i], current); gh_list_append(&nsp->free_events, &nse->nodeq_io); if (nse->timeout.tv_sec) gh_heap_remove(&nsp->expirables, &nse->expire); } } } }
/* An internal function for cancelling an event when you already have a pointer * to the msevent (use nsock_event_cancel if you just have an ID). The * event_list passed in should correspond to the type of the event. For example, * with NSE_TYPE_READ, you would pass in &nsp->read_events;. elem is the list * element in event_list which holds the event. Pass a nonzero for notify if * you want the program owning the event to be notified that it has been * cancelled */ int msevent_cancel(mspool *nsp, msevent *nse, gh_list_t *event_list, gh_lnode_t *elem, int notify) { if (nse->event_done) { /* This event has already been marked for death somewhere else -- it will be * gone soon (and if we try to kill it now all hell will break loose due to * reentrancy. */ return 0; } nsock_log_info(nsp, "msevent_cancel on event #%li (type %s)", nse->id, nse_type2str(nse->type)); /* Now that we found the event... we go through the motions of cleanly * cancelling it */ switch (nse->type) { case NSE_TYPE_CONNECT: case NSE_TYPE_CONNECT_SSL: handle_connect_result(nsp, nse, NSE_STATUS_CANCELLED); break; case NSE_TYPE_READ: handle_read_result(nsp, nse, NSE_STATUS_CANCELLED); break; case NSE_TYPE_WRITE: handle_write_result(nsp, nse, NSE_STATUS_CANCELLED); break; case NSE_TYPE_TIMER: handle_timer_result(nsp, nse, NSE_STATUS_CANCELLED); break; #if HAVE_PCAP case NSE_TYPE_PCAP_READ: handle_pcap_read_result(nsp, nse, NSE_STATUS_CANCELLED); break; #endif default: fatal("Invalid nsock event type (%d)", nse->type); } assert(nse->event_done); if (nse->timeout.tv_sec) gh_heap_remove(&nsp->expirables, &nse->expire); if (event_list) { update_first_events(nse); gh_list_remove(event_list, elem); } gh_list_append(&nsp->free_events, &nse->nodeq_io); nsock_log_debug_all(nsp, "NSE #%lu: Removing event from list", nse->id); #if HAVE_PCAP #if PCAP_BSD_SELECT_HACK if (nse->type == NSE_TYPE_PCAP_READ) { nsock_log_debug_all(nsp, "PCAP NSE #%lu: CANCEL TEST pcap=%p read=%p curr=%p sd=%i", nse->id, &nsp->pcap_read_events, &nsp->read_events, event_list,((mspcap *)nse->iod->pcap)->pcap_desc); /* If event occurred, and we're in BSD_HACK mode, then this event was added to * two queues. read_event and pcap_read_event Of course we should * destroy it only once. I assume we're now in read_event, so just unlink * this event from pcap_read_event */ if (((mspcap *)nse->iod->pcap)->pcap_desc >= 0 && event_list == &nsp->read_events) { /* event is done, list is read_events and we're in BSD_HACK mode. So unlink * event from pcap_read_events */ gh_list_remove(&nsp->pcap_read_events, &nse->nodeq_pcap); nsock_log_debug_all(nsp, "PCAP NSE #%lu: Removing event from PCAP_READ_EVENTS", nse->id); } if (((mspcap *)nse->iod->pcap)->pcap_desc >= 0 && event_list == &nsp->pcap_read_events) { /* event is done, list is read_events and we're in BSD_HACK mode. * So unlink event from read_events */ gh_list_remove(&nsp->read_events, &nse->nodeq_io); nsock_log_debug_all(nsp, "PCAP NSE #%lu: Removing event from READ_EVENTS", nse->id); } } #endif #endif msevent_dispatch_and_delete(nsp, nse, notify); return 1; }