static tcpclient_t * _new_client(tcpsvr_t *svr,wod_socket_t fd) { if(svr->num_cli == TCPSVR_MAX_CONNECT){ return NULL; } int hash = -1; int i=0; for(i=0;i<TCPSVR_MAX_CONNECT;i++){ hash = hash_func(svr->cut_index++); if(!svr->clients[hash].binuse){ break; } } assert(hash != -1); tcpclient_t * cli = &svr->clients[hash]; cli->svr = svr; if(0 != tcpclient_init(cli,fd,hash)){ return NULL; } cli->binuse = true; wod_net_noblock(fd,1); wod_net_keep_alive(fd,1); ++svr->num_cli; return cli; }
//----------------------------------------------------------------------------- NetworkType* new_tcp_client_state( char* ip, int port, uint32_t *ticks) { NetworkType *tcp = malloc(sizeof(NetworkType)); tcp->tick = tick; tcp->logic_tick = logic_tick; tcp->add_command = add_command; tcp->get_command = get_command; tcp->get_id = get_id; tcp->cleanup = cleanup; TcpClientState *state = (TcpClientState *) malloc(sizeof(TcpClientState)); memset(state, 0, sizeof(TcpClientState)); tcp->state = (void *)state; state->msg_size = 0; state->in = new_queue(1); state->out = new_queue(1); state->last_post_time = 0; state->post_delay = 0.12; state->waiting = 0; state->client_id = -1; state->ready = 0; state->read_buf = malloc(1); state->ticks = ticks; state->socket = new_tcpclient(); tcpclient_init(state->socket, port, ip); tcpclient_set_user_data(state->socket, tcp->state); printf("Connecting...\n"); tcpclient_set_handlers(state->socket, &client_read, &client_disconnect); tcpclient_connect(state->socket); return tcp; }