1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) { int clientfd; struct sockaddr_in clientaddr; socklen_t len = sizeof(clientaddr); clientfd = Accept(watcher->fd, (struct sockaddr *)&clientaddr, &len); print_accept_info(clientfd, &clientaddr);
struct ev_io *w_client = (struct ev_io*) malloc (sizeof(struct ev_io)); ev_io_init(w_client, client_cb, clientfd, EV_READ); ev_io_start(loop, w_client);
clients[clientfd] = clientfd; }
void client_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) { int n = handle_client(watcher->fd); if (n == 0) { int fd = watcher->fd; ev_io_stop(loop, watcher); Close(fd); free(watcher); clients[fd] = -1; printf("client=%d, client exit...\n", fd); } }
|