Game Development Reference
In-Depth Information
n
Initialize the server socket manager and attach a listen socket.
n
Call
DoSelect()
on the server socket manager.
n
If there
s input on the listen socket, create a new socket and attach it to the
socket manager.
n
Handle input/output exceptions on all other sockets.
'
What we need is a class that extends
NetListenSocket
by overloading
VHandle-
Input()
to create new clients. The clients are encapsulated by the
RemoteEvent-
Socket
, which is the final piece to this puzzle. Its job is to send game events
generated on the server to a remote client and fool the client into thinking that the
events were actually generated locally.
class GameServerListenSocket: public NetListenSocket
{
public:
GameServerListenSocket(int portnum) { Init(portnum); }
void VHandleInput();
};
void GameServerListenSocket::VHandleInput()
{
unsigned int theipaddr;
SOCKET new_sock = AcceptConnection(&theipaddr);
int value = 1;
setsockopt(new_sock, SOL_SOCKET, SO_DONTLINGER,
(char *)&value, sizeof(value));
if (new_sock != INVALID_SOCKET)
{
RemoteEventSocket * sock =
GCC_NEW RemoteEventSocket(new_sock, theipaddr);
int sockId = g_pSocketManager->AddSocket(sock);
int ipAddress = g_pSocketManager->GetIpAddress(sockdId);
shared_ptr<EvtData_Remote_Client> pEvent (
GCC_NEW EvtData_Remote_Client( sockId, ipAddress ) );
IEventManager::Get()->VQueueEvent(pEvent);
}
}
Notice another cameo from Chapter 11? Here, the method calls the Event Manager
s
VQueueEvent()
with a new event:
EvtData_Remote_Client
. The event takes
the socket ID and the IP address and passes them onto any game subsystem that is
'