short sv_net_init(short port) { struct sockaddr_in addr; if( server.with_ggz ) { sock = xtuxggz_giveme_sock(); } else { sock = net_init_socket(); memset((char*)&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; /* Edited 23 Jan 2003 now we only bind to desired port */ addr.sin_port = htons(port); if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { printf("Couldn't bind to port %d.\n", port); perror("bind"); return -1; } /* listen for new connections, allow BACKLOG pending */ if (listen(sock, BACKLOG) < 0) { perror("listen"); return -1; } } /* Set server socket to nonblocking */ fcntl(sock, F_SETFL, O_NONBLOCK); return port; /* Port we are bound to */ }
int cl_network_connect(char *sv_name, int port) { struct hostent *hp; struct sockaddr_in addr; netmsg msg; demo_header header; int sock = -1; if( client.demo != DEMO_PLAY ) { /* A real connection, not just a demo */ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); if(!client.with_ggz) { sock = net_init_socket(); if( (addr.sin_addr.s_addr = inet_addr(sv_name)) == INADDR_NONE ) { if((hp = gethostbyname(sv_name)) == NULL) { close(sock); sock = -1; return 0; } else { addr.sin_addr.s_addr = *(long *) hp->h_addr; } } if( connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0 ) { perror("connect"); close(sock); sock = -1; return 0; } } else { /* ggz mode */ signal(SIGPIPE, SIG_IGN); /* Ignore Pipe errors */ if( (sock = ggz_client_get_sock()) < 0 ) { sock = -1; return 0; } /* avoid perdig effect */ sleep(3); } } switch( client.demo ) { case DEMO_RECORD: printf("RECORDING Connection.......\n"); header.x_tiles = client.x_tiles; header.y_tiles = client.y_tiles; header.fps = client.fps; server = net_rec_new_connection(sock, client.demoname, header); break; case DEMO_PLAY: server = net_fake_connection(client.demoname, &header); client.x_tiles = header.x_tiles; client.y_tiles = header.y_tiles; client.view_w = client.x_tiles * TILE_W; client.view_h = client.y_tiles * TILE_H; break; case DEMO_NONE: default: server = net_new_connection(sock); } if( server == NULL ) { printf("Couldn't create new connection\n"); close(sock); sock = -1; return 0; } client.connected = 1; client.sv_status = JOINING; printf("Joining Game....\n"); cl_netmsg_send_join(); printf("Querying Server info......\n"); cl_netmsg_send_query_sv_info(); net_output_flush(server); /* FIXME: This alarm() does not work correctly */ alarm(10); /* Timeout after 10 secs */ signal(SIGALRM, sigcatcher); printf("Reading data from server...... "); while( net_input_flush(server, NM_ALL) > 0 ) { printf("Got something!\n"); while( (msg = net_next_message(server, NM_ALL)).type != NETMSG_NONE ) { if( msg.type == NETMSG_SV_INFO ) { /* Got server info ok, we can join the game */ alarm(0); cl_net_handle_message(msg); return 1; } else if(msg.type == NETMSG_REJECTION || msg.type == NETMSG_QUIT){ /* We were rejected or another error occured trying to join */ alarm(0); cl_net_handle_message(msg); return 0; } else if( client.debug ) { printf("IGNORING message type %d!\n", msg.type); } } } alarm(0); printf("TIMED OUT waiting for server info!\n"); cl_network_disconnect(); return 0; }