TCPObject

consoledoc.h

Allows communications between the game and a server using TCP/IP protocols.

More...

Callbacks

void
onConnectionRequest(string address, string ID)

Called whenever a connection request is made.

void
onLine(string line)

Called whenever a line of data is sent to this TCPObject.

bool
onPacket(string data)

Called when we get a packet with no newlines or nulls (probably websocket).

void

Called when we are done reading all lines.

void

Called whenever the DNS has been resolved.

void

Called whenever the DNS has failed to resolve.

void

Called whenever a connection is established with a server.

void

Called whenever a connection has failed to be established with a server.

void

Called whenever the TCPObject disconnects from whatever it is currently connected to.

Public Functions

void
connect(string address)

Connect to the given address.

void

Disconnect from whatever this TCPObject is currently connected to, if anything.

void

Eat the rest of the lines.

void
listen(uint port)

Start listening on the specified port for connections.

void
send(string data)

Transmits the data string to the connected computer.

bool
sendFile(string fileName)

Transmits the file in binary to the connected computer.

Detailed Description

Allows communications between the game and a server using TCP/IP protocols.

To use TCPObject you set up a connection to a server, send data to the server, and handle each line of the server's response using a callback. Once you are done communicating with the server, you disconnect.

TCPObject is intended to be used with text based protocols which means you'll need to delineate the server's response with an end-of-line character. i.e. the newline character \n. You may optionally include the carriage return character \r prior to the newline and TCPObject will strip it out before sending the line to the callback. If a newline character is not included in the server's output, the received data will not be processed until you disconnect from the server (which flushes the internal buffer).

TCPObject may also be set up to listen to a specific port, making Torque into a TCP server. When used in this manner, a callback is received when a client connection is made. Following the outside connection, text may be sent and lines are processed in the usual manner.

If you want to work with HTTP you may wish to use HTTPObject instead as it handles all of the HTTP header setup and parsing.

// In this example we'll retrieve the new forum threads RSS
// feed from garagegames.com.  As we're using TCPObject, the
// raw text response will be received from the server, including
// the HTTP header.

// Define callbacks for our specific TCPObject using our instance's
// name (RSSFeed) as the namespace.

// Handle an issue with resolving the server's name
function RSSFeed::onDNSFailed(%this)
{
   // Store this state
   %this.lastState = "DNSFailed";

   // Handle DNS failure
}

function RSSFeed::onConnectFailed(%this)
{
   // Store this state
   %this.lastState = "ConnectFailed";

   // Handle connection failure
}

function RSSFeed::onDNSResolved(%this)
{
   // Store this state
   %this.lastState = "DNSResolved";

}

function RSSFeed::onConnected(%this)
{
   // Store this state
   %this.lastState = "Connected";

}

function RSSFeed::onDisconnect(%this)
{
   // Store this state
   %this.lastState = "Disconnected";

}

// Handle a line from the server
function RSSFeed::onLine(%this, %line)
{
   // Print the line to the console
   echo( %line );
}

// Create the TCPObject
%rss = new TCPObject(RSSFeed);

// Define a dynamic field to store the last connection state
%rss.lastState = "None";

// Connect to the server
%rss.connect("www.garagegames.com:80");

// Send the RSS feed request to the server.  Response will be
// handled in onLine() callback above
%rss.send("GET /feeds/rss/threads HTTP/1.1\r\nHost: www.garagegames.com\r\n\r\n");

Callbacks

onConnectionRequest(string address, string ID)

Called whenever a connection request is made.

This callback is used when the TCPObject is listening to a port and a client is attempting to connect. Parameters:

address

Server address connecting from.

ID

Connection ID

onLine(string line)

Called whenever a line of data is sent to this TCPObject.

This callback is called when the received data contains a newline \n character, or the connection has been disconnected and the TCPObject's buffer is flushed. Parameters:

line

Data sent from the server.

onPacket(string data)

Called when we get a packet with no newlines or nulls (probably websocket).

Parameters:

data

Data sent from the server.

return:

true if script handled the packet.

onEndReceive()

Called when we are done reading all lines.

onDNSResolved()

Called whenever the DNS has been resolved.

onDNSFailed()

Called whenever the DNS has failed to resolve.

onConnected()

Called whenever a connection is established with a server.

onConnectFailed()

Called whenever a connection has failed to be established with a server.

onDisconnect()

Called whenever the TCPObject disconnects from whatever it is currently connected to.

Public Functions

connect(string address)

Connect to the given address.

Parameters:

address

Server address (including port) to connect to.

// Set the address.
%address = "www.garagegames.com:80";

// Inform this TCPObject to connect to the specified address.
%thisTCPObj.connect(%address);

disconnect()

Disconnect from whatever this TCPObject is currently connected to, if anything.

// Inform this TCPObject to disconnect from anything it is currently connected to.
%thisTCPObj.disconnect();

finishLastLine()

Eat the rest of the lines.

listen(uint port)

Start listening on the specified port for connections.

This method starts a listener which looks for incoming TCP connections to a port. You must overload the onConnectionRequest callback to create a new TCPObject to read, write, or reject the new connection.

Parameters:

port

Port for this TCPObject to start listening for connections on.

// Create a listener on port 8080.
new TCPObject( TCPListener );
TCPListener.listen( 8080 );

function TCPListener::onConnectionRequest( %this, %address, %id )
{
   // Create a new object to manage the connection.
   new TCPObject( TCPClient, %id );
}

function TCPClient::onLine( %this, %line )
{
   // Print the line of text from client.
   echo( %line );
}

send(string data)

Transmits the data string to the connected computer.

This method is used to send text data to the connected computer regardless if we initiated the connection using connect(), or listening to a port using listen(). Parameters:

data

The data string to send.

// Set the command data
%data = "GET " @ $RSSFeed::serverURL @ " HTTP/1.0\r\n";
%data = %data @ "Host: " @ $RSSFeed::serverName @ "\r\n";
%data = %data @ "User-Agent: " @ $RSSFeed::userAgent @ "\r\n\r\n"

// Send the command to the connected server.
%thisTCPObj.send(%data);

sendFile(string fileName)

Transmits the file in binary to the connected computer.

Parameters:

fileName

The filename of the file to transfer.