I'm setting up a multiplayer game with CFSockets and CFStreams. Everything is working smoothly and I'm sending data back and forth between server and clients. The problem is I get no notification when a client disconnects.
I got the code from the book
Beginning iPhone Games Development and here is a part when a new client connects:
Code:
static void serverAcceptCallback(CFSocketRef socket, CFSocketCallBackType type,
CFDataRef address, const void *data, void *info) {
SocketServer *server = (SocketServer*)info;
// We can only process "connection accepted" calls here
if ( type != kCFSocketAcceptCallBack ) {
return;
}
// for AcceptCallBack, the data parameter is a pointer to a CFSocketNativeHandle
CFSocketNativeHandle handle = *(CFSocketNativeHandle*)data;
Connection *connection = [[[Connection alloc]
initWithNativeSocketHandle:handle] autorelease];
if ( [connection connect] ) {
[server.delegate newClientConnected:connection];
}
}
My question is, do I need to ping the user once in a while, and if he does not respond in like 10 seconds, assume he's disconnected?
There are several reasons why he might be disconnected:
- Quit hosted game (from a button in the game)
- Quit app
- Got phonecall
- Network error maybe
So how is a good way to catch a disconnect? It would be neat if there was a function like the one above.
Thanks!