Game Development Reference
In-Depth Information
Right after the socket is connected, you probably want to set it to nonblocking.
Here ' s a method that does exactly that, and it is exactly like you saw in the primer:
void NetSocket::SetBlocking(bool blocking)
{
unsigned long val = blocking ?0:1;
ioctlsocket(m_sock, FIONBIO, &val);
}
It
s now time to learn how this class sends packets to the remote computer. When-
ever you have a packet you want to send, the Send() method simply adds it to the
end of the list of packets to send. It doesn ' t send the packets right away. This is done
once per update loop by the Send() method:
'
void NetSocket::Send(shared_ptr<IPacket> pkt, bool clearTimeOut)
{
if (clearTimeOut)
m_timeOut = 0;
m_OutList.push_back(pkt);
}
The VHandleOutput() method
'
s job is to iterate the list of packets in the output list
and call the socket
'
s send() API until all the data is gone or there is some kind of
error.
void NetSocket::VHandleOutput()
{
int fSent = 0;
do
{
GCC_ASSERT(!m_OutList.empty());
PacketList::iterator i = m_OutList.begin();
shared_ptr<IPacket> pkt = *i;
const char *buf = pkt->VGetData();
int len = static_cast<int>(pkt->VGetSize());
int rc = send(m_sock, buf+m_sendOfs, len-m_sendOfs, 0);
if (rc > 0)
{
g_pSocketManager->AddToOutbound(rc);
m_sendOfs += rc;
fSent = 1;
}
else if (WSAGetLastError() != WSAEWOULDBLOCK)
 
Search WWH ::




Custom Search