Game Development Community

Spawning sockets from a server TCPObject

by David Spuhler · in Torque Game Engine · 08/11/2005 (8:15 am) · 8 replies

I want to run a server using the torque engine:

if there are incoming connection attempts i am notified by

TCPObject::onConnectionRequest(const NetAddress* addr, U32 connectId)

(if i got that right...)

now i want to use this callback to spawn a new TCPObject handling this specific client-server connection.
Can anybody help me? How do i have to create this new TCPObject?
Thanks in advance!

#1
08/15/2005 (10:45 am)
Hmm... Good question. I'm curious about this, too, now. :)
#3
08/15/2005 (6:43 pm)
Ah... Cool. I'd forgotten about Mr. Vanderbeck's excellent resource.
#4
08/15/2005 (7:00 pm)
Quick and dirty...

$listenPort = 1234;

// This is the server listener
new TCPObject(ServerListener);
ServerListener.listen($listenPort);

// Initialize an "array" to hold our clients
$clients = new ScriptGroup(TCPClients);

// A request to connect will trip this method
function ServerListener::onConnectRequest(%this, %addr, %id)
{
   // Use the id we receive to spawn a new socket for this client
   // The first argument is the object name (see below...)
   %client = new TCPObject(tcpSocket, %id);
   
   // Add the client to some sort of list so we can track them (just handy to do...)
   TCPClients.add(%client);
}

// Notice that we're using the object name we
// set in the listeners onConnectRequest method
function tcpSocket::onConnected(%this) {
   // A client connected (%this is %client [the socket] from onConnectRequest)
   echo("** [" @ %this @ "] Connected");
}

function tcpSocket::onDisconnect(%this) {
   // A client disconnected (%this is %client from onConnectRequest)
   echo("** [" @ %this @ "] Disconnect");
}

function tcpSocket::onLine(%this, %cmd)
{
   // A line has arrived from the client, parse it
   // again, %this is %client from onConnectRequest
}

- Brett
#5
08/16/2005 (11:22 am)
I see you use the Scriptgroup method to keep track of client connections Brett. That was one of the modifications I made to JV's resource for my personal use.
#6
09/16/2005 (1:57 pm)
I also am using this resource and made a simgroup modification.

I'm having some weird behavior when testing a server instance with a client instance on the same machine. It took me a while to track it down, but it looks like I'm not getting the onDisconnect() call to fire in script or C++, on either server or client when the connection is broken. This was causing me some problems because those functions do some cleanup. I've stepped through both the client and the server with a script and VS.Net debugger, and they're not being called.

Has anyone else had this problem? It's possible our netcode got miswired somewhere with our own mistakes or something, but I haven't seen any changes so far.

Anyways, just curious. I'll follow up when I get it working.

Thanks!
- Drew
#7
09/16/2005 (7:36 pm)
Ok, well I don't know how I managed to break my scripts, but for some reason everything is working ok now. The one thing I did discover is that onDisconnect() is not called on the socket that calls disconnect(). So if you need to run some clean up code, like to delete the connection, it needs to be placed in the onDisconnect() call *and* in the spot where you call .disconnect().
#8
09/17/2005 (6:19 pm)
Yes, onDisconnect is for when the server forces a disconnect, I bet.

Thanks for the awesome code snippets and tips, guys! This is a good thread!