SLOW Network Performance
I''m having a problem with my network code severely slowing down my game. When trying to poll the network by setting the timeval sec and u_sec to 0, and calling select(). My framerate instantly fell from 85 to 20fps. Upon further analysis, I was able to determine that the networking polling routine was taking up 85% of the game loop. Is This Normal?!? Do I need to put my code into a seperate thread, or am I doing something wrong....?
Here''s the routine i''m using...
int DH_NetServer::receive(char *data, int data_size)
{
int new_fd = 0;
int bytes_received = 0;
fd_set tmp_set;
sockaddr_in sender;
timeval timeout;
socklen_t sin_size = sizeof(sockaddr_in);
if(!port_open){
DH_Error.push("DH_NetServer::receive - you must open a port before you receive data from it\n");
return -1;
}
for(int i=0; i <= fdmax ;i++)
{
// copy the master set of file descriptors so it''s not altered
tmp_set = master_set;
// enable polling
timeout.tv_sec = 0;
timeout.tv_usec = 0;
if (select(fdmax+1, &tmp_set, NULL, NULL,&timeout) == -1) {
DH_Error.push("DH_NetServer::receive - Error selecting socket\n");
return(-1);
}
// check for incoming data
if(FD_ISSET(i, &tmp_set)) {
// add any new connections
if(i == listen_sockfd)
{
// make sure that we are in the valid range of connections
// -1 max_connection''s means infinite
if(num_connections > max_connections && max_connections != -1){
DH_Error.push("DH_NetServer::receive - maximum connection already met, cannot ad connection\n");
return(-1);
}
// accept and add the new descriptor to the file descriptor set
new_fd = accept(listen_sockfd, (sockaddr *)&sender,&sin_size);
FD_SET(new_fd, &master_set);
// update the max file descriptor
if(new_fd>fdmax)
fdmax = new_fd;
// increment the number of connections
num_connections++;
// update the latest connection information
latest_connector = sender;
latest_fd = new_fd;
// return -2 when a new connection is detected
return(-2);
} // (i == listen_sockfd)
// receive the message from the file descriptor
if((bytes_received = recv(i, data, data_size, 0)) == -1){
DH_Error.push("DH_NetServer::receive - Error receiving data\n");
return -1;
}
// check for connection close
// remove the socket from the set if it closes
if(bytes_received == 0) {
closesocket(i);
num_connections++;
latest_disconnect_fd = i;
FD_CLR((unsigned int)i, &master_set);
}
// exit the loop once we got the data
return (bytes_received);
}// if(FD_ISSET(i, tmp_set)
}// for(...;i<fdmax; ...)
return(-3);
}
March 28, 2002 01:02 PM
quote:
Original post by Hairu
I''m having a problem with my network code severely slowing down my game. When trying to poll the network by setting the timeval sec and u_sec to 0, and calling select(). My framerate instantly fell from 85 to 20fps. Upon further analysis, I was able to determine that the networking polling routine was taking up 85% of the game loop. Is This Normal?!? Do I need to put my code into a seperate thread, or am I doing something wrong....?
Unfortantely, the key word here is _poll_. Polling is usually very slow. Try using non blocking sockets and checking for input every frame, or at some other set time.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement