Game Development Reference
In-Depth Information
}
else if (m_recvBegin + m_recvOfs + MAX_PACKET_SIZE > RECV_BUFFER_SIZE)
{
// we don
t want to overrun the buffer - so we copy the leftover bits
// to the beginning of the receive buffer and start over
int leftover = m_recvOfs;
memcpy(m_recvBuf, &m_recvBuf[m_recvBegin], m_recvOfs);
m_recvBegin = 0;
'
}
}
}
Easy to Read or Super Efficient? Do Both!
When you define your packet definitions and protocols, make sure you can
easily switch between a tight, efficient packet definition and an easy-to-read
definition such as clear text. You
'
ll use one for production, but the other is
invaluable for debugging.
A Socket Class for Listening
A listen socket is an easy extension of the NetSocket class. It adds the capability to
listen for client connections and accept them, adding new sockets to the global socket
manager:
class NetListenSocket: public NetSocket
{
public:
NetListenSocket() { };
NetListenSocket(int portnum) { port = 0; Init(portnum); }
void Init(int portnum);
SOCKET AcceptConnection(unsigned int *pAddr);
unsigned short port;
};
There are five steps to create a listen socket: You create a socket handle, set the
socket options, bind the socket to a listen port, set it to nonblocking, and finally call
listen() .
void NetListenSocket::Init(int portnum)
{
struct sockaddr_in sa;
int value = 1;
 
 
Search WWH ::




Custom Search