Game Development Reference
In-Depth Information
{
HandleException();
fSent = 0;
}
else
{
fSent = 0;
}
if (m_sendOfs == pkt->VGetSize())
{
m_OutList.pop_front();
m_sendOfs = 0;
}
} while ( fSent && !m_OutList.empty() );
}
The idea behind reading the socket for input is similar, but there
'
s some buffer man-
agement to worry about. For efficiency
s a single monolithic buffer for
each NetSocket object. Depending on how the remote sends data, you might get
your packet in chunks. TCP is guaranteed to send things in the right order, and it
won
'
s sake, there
'
t split them up, but you might attempt to send something large, like a movie
file. In any case, you want to collect bytes in the read buffer until you have a valid
packet and then copy those bytes into a dynamic data structure like BinaryPacket
so your game can process it.
Since you might receive multiple packets in a single read, the read buffer operates in
a round-robin fashion. The read/write heads continually advance until they get too
close to the end of the buffer, and then they copy any partial packets to the beginning
of the buffer and start the whole process over.
'
void NetSocket::VHandleInput()
{
bool bPktReceived = false;
u_long packetSize = 0;
int rc = recv(m_sock,
m_recvBuf+m_recvBegin+m_recvOfs,
RECV_BUFFER_SIZE-(m_recvBegin+m_recvOfs), 0);
if (rc==0)
return;
if (rc == SOCKET_ERROR)
{
m_deleteFlag = 1;
Search WWH ::




Custom Search