int main (int argc, char **argv) { int i, j; char *ubuf = 0; int afd, sfd, tmp; thread_t cli_thread = {}; BUG_ON ( (afd = xlisten ("tcp+ipc+inproc://127.0.0.1:18897") ) < 0); thread_start (&cli_thread, xclient_thread, 0); usleep (100000); BUG_ON (xselect (XPOLLIN, 1, &afd, 1, &tmp) <= 0); for (j = 0; j < 3; j++) { BUG_ON ( (sfd = xaccept (afd) ) < 0); for (i = 0; i < cnt; i++) { while (xselect (XPOLLIN, 1, &sfd, 1, &tmp) <= 0) usleep (10000); BUG_ON (tmp != sfd); BUG_ON (0 != xrecv (sfd, &ubuf) ); BUG_ON (0 != xsend (sfd, ubuf) ); } xclose (sfd); } thread_stop (&cli_thread); xclose (afd); return 0; }
t_client *add_client(t_info *info, int server, int x, int y) { static int last_x = -1; static int last_y = -1; static int last_direction = -1; static int id = 0; t_client *client; client = xmalloc(sizeof(*client)); client->status = ST_NOT_LOGGED; client->fct_read = client_read; client->fct_write = client_write; init_buffer(client); client->level = START_LEVEL; client->x = (x ? x : get_random(info->x, last_x)); client->y = (y ? y : get_random(info->y, last_y)); last_x = client->x; last_y = client->y; push_list(&(info->zone[client->x][client->y].clients), client); client->direction = get_random(WEST, last_direction); client->team = NULL; init_ressources(client); push_list(&info->clients, (void *)client); if (server) client->socket = xaccept(server, NULL, NULL); client->id = id++; return (client); }
int main(int ac, char **av) { t_ftp f; f.port = (ac < 2 ? PORT_DEFAULT : av[1]); if (create_server(&f) == TRUE) while ((f.cs = xaccept(f.s, NULL, NULL)) > 0) get_client(&f); close(f.s); return (TRUE); }
static int thread_accept(struct accept_args *a, int force_nonblock) { int rv; /* always use non-blocking accept() under 1.8 for green threads */ set_nonblocking(a->fd); TRAP_BEG; rv = (int)xaccept(a); TRAP_END; return rv; }
void file_read(t_server *serv, int s) { t_file *file; struct sockaddr_in csin; socklen_t len; int cs; len = sizeof(csin); cs = xaccept(s, (struct sockaddr *)&csin, &len); if ((file = get_file_by_fd(serv->files, s)) != NULL) file->fd = cs; serv->fd_type[cs] = FD_WFILE_READ; serv->fct_read[cs] = read_file; serv->fct_write[cs] = NULL; close(s); serv->fd_type[s] = FD_FREE; }