Game Development Reference
In-Depth Information
handle, so they use the socket ID to figure out which socket is going to get the
packet. In the case of a server system with hundreds of attached clients, this function
makes short work of finding a socket handle that corresponds to a generic socket ID.
NetSocket *BaseSocketManager::FindSocket(int sockId)
{
SocketIdMap::iterator i = m_SockMap.find(sockId);
if (i==m_SockMap.end())
return NULL;
return (*i).second;
}
bool BaseSocketManager::Send(int sockId, shared_ptr<IPacket> packet)
{
NetSocket *sock = FindSocket(sockId);
if (!sock)
return false;
sock->Send(packet);
return true;
}
The real meat of the socket manager class is DoSelect() . There are four stages of
this method:
n Set up which sockets are going to be polled for activity.
n Call the select() API.
n Handle processing of any socket with input, output, or exceptions.
n Close any sockets that need closing.
void BaseSocketManager::DoSelect(int pauseMicroSecs, int handleInput)
{
timeval tv;
tv.tv_sec = 0;
// 100 microseconds is 0.1 milliseconds or .0001 seconds
tv.tv_usec = pauseMicroSecs;
fd_set inp_set, out_set, exc_set;
int maxdesc;
FD_ZERO(&inp_set);
FD_ZERO(&out_set);
FD_ZERO(&exc_set);
maxdesc = 0;
Search WWH ::




Custom Search