|
Provides the basis for implementing a multiplayer game protocol. More...
Public Member Functions | |
void | checkMaxRate () |
Ensures that all configured packet rates and sizes meet minimum requirements. | |
void | clearPaths () |
On the server, resets the connection to indicate that mod paths have not been transmitted. | |
void | connect (string remoteAddress) |
Connects to the remote address. | |
string | connectLocal () |
Connects with the server that is running within the same process as the client. | |
string | getAddress () |
Returns the far end network address for the connection. | |
int | getGhostID (int realID) |
On server or client, convert a real id to the ghost id for this connection. | |
int | getGhostsActive () |
Returns the number of active ghosts on the connection. | |
int | getPacketLoss () |
Returns the percentage of packets lost per tick. | |
int | getPing () |
Returns the average round trip time (in ms) for the connection. | |
int | resolveGhostID (int ghostID) |
On the client, convert a ghost ID from this connection to a real SimObject ID. | |
int | resolveObjectFromGhostIndex (int ghostID) |
On the server, convert a ghost ID from this connection to a real SimObject ID. | |
void | setSimulatedNetParams (float packetLoss, int delay) |
Simulate network issues on the connection for testing. | |
void | transmitPaths () |
Sent by the server during phase 2 of the mission download to update mod paths. |
Provides the basis for implementing a multiplayer game protocol.
NetConnection combines a low-level notify protocol implemented in ConnectionProtocol with a SimGroup, and implements several distinct subsystems:
The NetConnection is a SimGroup. On the client side, it contains all the objects which have been ghosted to that client. On the server side, it is empty; it can be used (typically in script) to hold objects related to the connection. For instance, you might place an observation camera in the NetConnnection. In both cases, when the connection is destroyed, so are the contained objects.
The NetConnection also has the concept of local connections. These are used when the client and server reside in the same process. A local connection is typically required to use the standard Torque world building tools. A local connection is also required when building a single player game.
void NetConnection::checkMaxRate | ( | ) |
Ensures that all configured packet rates and sizes meet minimum requirements.
This method is normally only called when a NetConnection class is first constructed. It need only be manually called if the global variables that set the packet rate or size have changed.
pref::Net::PacketRateToClient or $pref::Net::PacketSize have been changed since a NetConnection has been created, this method must be called on all connections for them to follow the new rates or size.
void NetConnection::clearPaths | ( | ) |
On the server, resets the connection to indicate that mod paths have not been transmitted.
Typically when a mission has ended on the server, all connected clients are informed of this change and their connections are reset back to a starting state. This method resets a connection on the server to indicate that mod paths have not been transmitted.
// Inform the clients for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++) { // clear ghosts and paths from all clients %cl = ClientGroup.getObject(%clientIndex); %cl.endMission(); %cl.resetGhosting(); %cl.clearPaths(); }
void NetConnection::connect | ( | string | remoteAddress | ) |
Connects to the remote address.
Attempts to connect with another NetConnection on the given address. Typically once connected, a game's information is passed along from the server to the client, followed by the player entering the game world. The actual procedure is dependent on the NetConnection subclass that is used. i.e. GameConnection.
remoteAddress | The address to connect to in the form of IP:<address>:<port although the IP: portion is optional. The address portion may be in the form of w.x.y.z or as a host name, in which case a DNS lookup will be performed. You may also substitue the word broadcast for the address to broadcast the connect request over the local subnet. |
string NetConnection::connectLocal | ( | ) |
Connects with the server that is running within the same process as the client.
string NetConnection::getAddress | ( | ) |
Returns the far end network address for the connection.
The address will be in one of the following forms:
int NetConnection::getGhostID | ( | int | realID | ) |
On server or client, convert a real id to the ghost id for this connection.
Torque's network ghosting system only exchanges ghost ID's between the server and client. Use this method on the server or client to discover an object's ghost ID based on its real SimObject ID.
realID | The real SimObject ID of the object. |
int NetConnection::getGhostsActive | ( | ) |
Returns the number of active ghosts on the connection.
int NetConnection::getPacketLoss | ( | ) |
Returns the percentage of packets lost per tick.
int NetConnection::getPing | ( | ) |
Returns the average round trip time (in ms) for the connection.
The round trip time is recalculated every time a notify packet is received. Notify packets are used to information the connection that the far end successfully received the sent packet.
int NetConnection::resolveGhostID | ( | int | ghostID | ) |
On the client, convert a ghost ID from this connection to a real SimObject ID.
Torque's network ghosting system only exchanges ghost ID's between the server and client. Use this method on the client to discover an object's local SimObject ID when you only have a ghost ID.
ghostID | The ghost ID of the object as sent by the server. |
%object = ServerConnection.resolveGhostID( %ghostId );
int NetConnection::resolveObjectFromGhostIndex | ( | int | ghostID | ) |
On the server, convert a ghost ID from this connection to a real SimObject ID.
Torque's network ghosting system only exchanges ghost ID's between the server and client. Use this method on the server to discover an object's local SimObject ID when you only have a ghost ID.
ghostID | The ghost ID of the object as sent by the server. |
%object = %client.resolveObjectFromGhostIndex( %ghostId );
void NetConnection::setSimulatedNetParams | ( | float | packetLoss, | |
int | delay | |||
) |
Simulate network issues on the connection for testing.
packetLoss | The fraction of packets that will be lost. Ranges from 0.0 (no loss) to 1.0 (complete loss) | |
delay | Delays packets being transmitted by simulating a particular ping. This is an absolute integer, measured in ms. |
void NetConnection::transmitPaths | ( | ) |
Sent by the server during phase 2 of the mission download to update mod paths.
Mod paths on the client need to be in sync with the server prior to objects being ghosted. This is typically done during the standard mission start phase 2 when following Torque's example mission startup sequence.
function serverCmdMissionStartPhase2Ack(%client, %seq, %playerDB) { // Make sure to ignore calls from a previous mission load if (%seq != $missionSequence || !$MissionRunning) return; if (%client.currentPhase != 1.5) return; %client.currentPhase = 2; // Set the player datablock choice %client.playerDB = %playerDB; // Update mod paths, this needs to get there before the objects. %client.transmitPaths(); // Start ghosting objects to the client %client.activateGhosting(); }