static void _poll(struct connection_server * server) { int timeout = 100; void * buffer = NULL; for (;;) { struct connection * c = connection_poll(server->pool, timeout); if (c==NULL) { skynet_command(server->ctx,"TIMEOUT","1"); return; } timeout = 0; if (buffer == NULL) { buffer = malloc(DEFAULT_BUFFER_SIZE); } int size = read(c->fd, buffer, DEFAULT_BUFFER_SIZE); if (size < 0) { continue; } if (size == 0) { free(buffer); buffer = NULL; if (c->close == 0) { c->close = 1; skynet_send(server->ctx, 0, c->address, PTYPE_CLIENT | PTYPE_TAG_DONTCOPY, 0, NULL, 0); connection_del(server->pool, c->fd); } } else { skynet_send(server->ctx, 0, c->address, PTYPE_CLIENT | PTYPE_TAG_DONTCOPY, 0, buffer, size); buffer = NULL; } } }
static void test(struct connection_pool *p, int id) { int i=0; while (i<5) { int handle = id; const char * line = connection_readline(p, handle, "\n", NULL); if (line == NULL) { line = connection_poll(p,1000,&handle,NULL); } if (line) { printf("%d %d: %s\n",i,handle, line); connection_write(p,handle,"readline\n",9); ++i; } else { if (handle) { printf("Close %d\n",handle); return; } } } i=0; while (i<5) { int handle = id; uint8_t * buffer = connection_read(p, handle, 8); if (buffer == NULL) { buffer = connection_poll(p,1000,&handle,NULL); } if (buffer) { int j; printf("%d %d: ",i,handle); for (j=0;j<8;j++) { printf("%02x ",buffer[j]); } printf("\n"); connection_write(p,handle,"readblock\n",10); ++i; } else { if (handle) { printf("Close %d\n", handle); return; } } } }
static void connection_list_poll(fd_set *fd_read) { struct connection *i, *next; i = connection_list_head; while (i) { next = i->next; if (connection_poll(i, fd_read)) connection_delete(i); i = next; } }