void server_task(void* arg) { struct cornet_config_t* config; struct client_config_t cconfig; int cfd, fd; char remote[16]; int rport; config = (struct cornet_config_t*)arg; if (!config->name) { config->name = "server_task"; } taskname(config->name); if ((fd = netannounce(config->istcp, config->server, config->port)) < 0) { fprintf(stderr, "cannot announce on tcp port %d: %s\n", config->port, strerror(errno)); taskexitall(1); } cconfig.h = config->h; while((cfd = netaccept(fd, remote, &rport)) >= 0){ fprintf(stderr, "connection from %s:%d\n", remote, rport); cconfig.fd = cfd; taskcreate(client_task, &cconfig, config->stack_size); } close(fd); printf("Exiting server task: %d\n", taskid()); }
static inline int Server_accept(int listen_fd) { int cfd = -1; int rport = -1; char remote[IPADDR_SIZE]; int rc = 0; Connection *conn = NULL; cfd = netaccept(listen_fd, remote, &rport); check(cfd >= 0, "Failed to accept on listening socket."); Server *srv = Server_queue_latest(); check(srv != NULL, "Failed to get a server from the configured queue."); conn = Connection_create(srv, cfd, rport, remote); check(conn != NULL, "Failed to create connection after accept."); rc = Connection_accept(conn); check(rc == 0, "Failed to register connection, overloaded."); return 0; error: if(conn != NULL) Connection_destroy(conn); return -1; }
void taskmain(int argc, char **argv) { int cfd, fd; int rport; char remote[16]; if(argc != 2){ fprintf(stderr, "usage: tcplongconnection localport \n"); taskexitall(1); } if((fd = netannounce(TCP, 0, atoi(argv[1]))) < 0){ fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno)); taskexitall(1); } fdnoblock(fd); while((cfd = netaccept(&fd, remote, &rport)) >= 0){ fprintf(stderr, "connection from %s:%d\n", remote, rport); taskcreate(proxytask, (void*)cfd, STACK); } }