static int check_errlevel(struct log_test_data *ltd, nsock_loglevel_t level) { nsock_event_id id; nsock_set_loglevel(level); ltd->current_level = level; ltd->got_dbgfull = 0; ltd->got_dbg = 0; ltd->got_info = 0; ltd->got_error = 0; ltd->total = 0; ltd->errcode = 0; id = nsock_timer_create(ltd->nsp, nop_handler, 200, NULL); nsock_event_cancel(ltd->nsp, id, 0); if (ltd->errcode) return ltd->errcode; if (ltd->total > 0) return -EINVAL; return 0; }
int try_cancel_timer(nsock_pool * nsp, int idx, int notify) { int res; printf("%ld:Attempting to cancel id %li (idx %d) %s notify.\n", time(NULL), ev_ids[idx], idx, ((notify) ? "WITH" : "WITHOUT")); res = nsock_event_cancel(nsp, ev_ids[idx], notify); printf("Kill of %li %s\n", ev_ids[idx], (res == 0) ? "FAILED" : "SUCCEEDED"); return res; }
static void refresh_idle_timer(nsock_pool nsp) { if (o.idletimeout <= 0) return; nsock_event_cancel(nsp, cs.idle_timer_event_id, 0); cs.idle_timer_event_id = nsock_timer_create(nsp, idle_timer_handler, o.idletimeout, NULL); }
static int cancel_udp_run(void *tdata) { struct basic_test_data *btd = (struct basic_test_data *)tdata; struct sockaddr_in peer; nsock_iod iod; nsock_event_id id; int done = 0; iod = nsock_iod_new(btd->nsp, NULL); AssertNonNull(iod); memset(&peer, 0, sizeof(peer)); peer.sin_family = AF_INET; inet_aton("127.0.0.1", &peer.sin_addr); id = nsock_connect_udp(btd->nsp, iod, cancel_handler, (void *)&done, (struct sockaddr *)&peer, sizeof(peer), PORT_UDP); nsock_event_cancel(btd->nsp, id, 1); nsock_iod_delete(iod, NSOCK_PENDING_SILENT); return (done == 1) ? 0 : -ENOEXEC; }
static void timer_handler(nsock_pool nsp, nsock_event nse, void *tdata) { struct timer_test_data *ttd = (struct timer_test_data *)tdata; int rnd, rnd2; if (nse_status(nse) != NSE_STATUS_SUCCESS) { ttd->stop = -nsock_pool_get_error(nsp); return; } if (ttd->timer_count > TIMERS_BUFFLEN - 3) return; rnd = rand() % ttd->timer_count; rnd2 = rand() % 3; switch (rnd2) { case 0: /* Do nothing */ /* Actually I think I'll create two timers :) */ add_timer(ttd, rand() % 3000); add_timer(ttd, rand() % 3000); break; case 1: /* Try to kill another id (which may or may not be active */ nsock_event_cancel(nsp, ttd->timer_list[rnd], rand() % 2); break; case 2: /* Create a new timer */ add_timer(ttd, rand() % 3000); break; default: assert(0); } }
void telnet_event_handler(nsock_pool nsp, nsock_event nse, void *mydata) { nsock_iod nsi = nse_iod(nse); enum nse_status status = nse_status(nse); enum nse_type type = nse_type(nse); struct sockaddr_in peer; struct telnet_state *ts; int nbytes; char *str; int read_timeout = -1; int write_timeout = 2000; ts = (struct telnet_state *)mydata; printf("telnet_event_handler: Received callback of type %s with status %s\n", nse_type2str(type), nse_status2str(status)); if (status == NSE_STATUS_SUCCESS) { switch (type) { case NSE_TYPE_CONNECT: case NSE_TYPE_CONNECT_SSL: nsi_getlastcommunicationinfo(nsi, NULL, NULL, NULL, (struct sockaddr *)&peer, sizeof peer); printf("Successfully connected %sto %s:%hu -- start typing lines\n", (type == NSE_TYPE_CONNECT_SSL) ? "(SSL!) " : "", inet_ntoa(peer.sin_addr), peer.sin_port); /* First of all, lets add STDIN to our list of watched filehandles */ if ((ts->stdin_nsi = nsi_new2(nsp, STDIN_FILENO, NULL)) == NULL) { fprintf(stderr, "Failed to create stdin msi\n"); exit(1); } /* Now lets read from stdin and the network, line buffered (by nsock) */ ts->latest_readtcpev = nsock_readlines(nsp, ts->tcp_nsi, telnet_event_handler, read_timeout, ts, 1); ts->latest_readstdinev = nsock_readlines(nsp, ts->stdin_nsi, telnet_event_handler, read_timeout, ts, 1); break; case NSE_TYPE_READ: str = nse_readbuf(nse, &nbytes); if (nsi == ts->tcp_nsi) { printf("%s", str); /* printf("Read from tcp socket (%d bytes):\n%s", nbytes, str); */ ts->latest_readtcpev = nsock_readlines(nsp, ts->tcp_nsi, telnet_event_handler, read_timeout, ts, 1); } else { /* printf("Read from stdin (%d bytes):\n%s", nbytes, str); */ nsock_write(nsp, ts->tcp_nsi, telnet_event_handler, write_timeout, ts, str, nbytes); ts->latest_readstdinev = nsock_readlines(nsp, ts->stdin_nsi, telnet_event_handler, read_timeout, ts, 1); } break; case NSE_TYPE_WRITE: /* Nothing to do, really */ break; case NSE_TYPE_TIMER: break; default: fprintf(stderr, "telnet_event_handler: Got bogus type -- quitting\n"); exit(1); break; } } else if (status == NSE_STATUS_EOF) { printf("Got EOF from %s\nCancelling outstanding readevents.\n", (nsi == ts->tcp_nsi) ? "tcp socket" : "stdin"); /* One of these is the event I am currently handling! But I wanted to be evil when testing this out... */ if (nsock_event_cancel(nsp, ts->latest_readtcpev, 1) != 0) { printf("Cancelled tcp event: %li\n", ts->latest_readtcpev); } if (nsock_event_cancel(nsp, ts->latest_readstdinev, 1) != 0) { printf("Cancelled stdin event: %li\n", ts->latest_readstdinev); } } else if (status == NSE_STATUS_ERROR) { if (nsi_checkssl(nsi)) { printf("SSL %s failed: %s\n", nse_type2str(type), ERR_error_string(ERR_get_error(), NULL)); } else { int err; err = nse_errorcode(nse); printf("%s failed: (%d) %s\n", nse_type2str(type), err, strerror(err)); } } return; }