NetObject

Engine/source/sim/netObject.h

Superclass for ghostable networked objects.

More...

Dirty List

Whenever a NetObject becomes "dirty", we add it to the dirty list.

We also remove ourselves on the destructor.

This is done so that when we want to send updates (in NetConnection), it's very fast to find the objects that need to be updated.

Next item in the dirty list...

Previous item in the dirty list...

Static pointer to the head of the dirty NetObject list.

Miscellaneous

bool

Called when the object is added to the sim.

Called when the object is removed from the sim.

Short-Circuited Networking

When we are running with client and server on the same system (which can happen be either when we are doing a single player game, or if we're hosting a multiplayer game and having someone playing on the same instance), we can do some short circuited code to enhance performance.

These variables are used to make it simpler; if we are running in short-circuited mode, the ghosted client gets the server object while the server gets the client object.

note:

"Premature optimization is the root of all evil" - Donald Knuth. The current codebase uses this feature in three small places, mostly for non-speed-related purposes.

Returns a pointer to the server object when on a local connection.

Returns a pointer to the client object when on a local connection.

T *
getServerObject(T * netObj)

Template form for the callers convenience.

T *
getClientObject(T * netObj)

Template form for the callers convenience.

Protected Types

enum
NetFlags {
  IsGhost =  BIT(1)
  ScopeAlways =  BIT(6)
  ScopeLocal =  BIT(7)
  Ghostable =  BIT(8)
  MaxNetFlagBit =  15
}

Public Types

enum
_Anonymous_ {  SCOPE_ID_BITS = 14
}

Private Types

Parent 

Public Friends

Private Attributes

Mask indicating which states are dirty and need to be retransmitted on this object.

Protected Attributes

Pointer to the client object on a local connection.

Head of a linked list storing GhostInfos referencing this NetObject.

Flag values from NetFlags.

The index of this ghost in the GhostManager on the server.

Pointer to the server object on a local connection.

Public Functions

Clear the specified bits from the dirty mask.

Stop scoping the object to all connections.

Return a textual description of the object.

filterMaskBits(U32 mask, NetConnection * connection)

Get the ghost index of this object.

getUpdatePriority(CameraScopeQuery * focusObject, U32 updateMask, S32 updateSkips)

This returns a value which is used to prioritize which objects need to be updated.

bool

Is this a client object?

bool

Is this is a ghost?

bool

Is this object ghostable?

bool

Should this object always be ghosted?

bool

Is this object subject to scoping?

bool

Should this object only be visible to the client which created it?

bool

Is this a server object?

Queries the object about information used to determine scope.

packUpdate(NetConnection * conn, U32 mask, BitStream * stream)

Instructs this object to pack its state for transfer over the network.

setMaskBits(U32 orMask)

Used to mark a bit as dirty; ie, that its corresponding set of fields need to be transmitted next update.

Scope the object to all connections.

Instructs this object to read state data previously packed with packUpdate.

Public Static Functions

Protected Functions

Protected Static Functions

addNetworkedField(const char * in_pFieldname, const U32 in_fieldType, const dsize_t in_fieldOffset, AbstractClassRep::WriteDataNotify in_writeDataFn, const char * in_pFieldDocs, U32 flags, U32 networkMask)
addNetworkedField(const char * in_pFieldname, const U32 in_fieldType, const dsize_t in_fieldOffset, AbstractClassRep::WriteDataNotify in_writeDataFn, const U32 in_elementCount, const char * in_pFieldDocs, U32 flags, U32 networkMask)
addNetworkedField(const char * in_pFieldname, const U32 in_fieldType, const dsize_t in_fieldOffset, const char * in_pFieldDocs, U32 flags, U32 networkMask)

Register a simple field.

addNetworkedField(const char * in_pFieldname, const U32 in_fieldType, const dsize_t in_fieldOffset, const U32 in_elementCount, const char * in_pFieldDocs, U32 flags, U32 networkMask)

Add a networked field.

Detailed Description

Superclass for ghostable networked objects.

Introduction To NetObject And Ghosting

One of the most powerful aspects of Torque's networking code is its support for ghosting and prioritized, most-recent-state network updates. The way this works is a bit complex, but it is immensely efficient. Let's run through the steps that the server goes through for each client in this part of Torque's networking:

  • First, the server determines what objects are in-scope for the client. This is done by calling onCameraScopeQuery() on the object which is considered the "scope" object. This is usually the player object, but it can be something else. (For instance, the current vehicle, or a object we're remote controlling.)

  • Second, it ghosts them to the client; this is implemented in netGhost.cc.

  • Finally, it sends updates as needed, by checking the dirty list and packing updates.

There several significant advantages to using this networking system:

  • Efficient network usage, since we only send data that has changed. In addition, since we only care about most-recent data, if a packet is dropped, we don't waste effort trying to deliver stale data.

  • Cheating protection; since we don't deliver information about game objects which aren't in scope, we dramatically reduce the ability of clients to hack the game and gain a meaningful advantage. (For instance, they can't find out about things behind them, since objects behind them don't fall in scope.) In addition, since ghost IDs are assigned per-client, it's difficult for any sort of co-ordination between cheaters to occur.

NetConnection contains the Ghost Manager implementation, which deals with transferring data to the appropriate clients and keeping state in synch.

An Example Implementation

The basis of the ghost implementation in Torque is NetObject. It tracks the dirty flags for the various states that the object trackers, and does some other book-keeping to allow more efficient operation of the networking layer.

Using a NetObject is very simple; let's go through a simple example implementation:

class SimpleNetObject : public NetObject
{
public:
  typedef NetObject Parent;
  DECLARE_CONOBJECT(SimpleNetObject);

Above is the standard boilerplate code for a Torque class. You can find out more about this in SimObject.

char message1[256];
char message2[256];
enum States {
   Message1Mask = BIT(0),
   Message2Mask = BIT(1),
};

For our example, we're having two "states" that we keep track of, message1 and message2. In a real object, we might map our states to health and position, or some other set of fields. You have 32 bits to work with, so it's possible to be very specific when defining states. In general, you should try to use as few states as possible (you never know when you'll need to expand your object's functionality!), and in fact, most of your fields will end up changing all at once, so it's not worth it to be too fine-grained. (As an example, position and velocity on Player are controlled by the same bit, as one rarely changes without the other changing, too.)

SimpleNetObject()
{
   // in order for an object to be considered by the network system,
   // the Ghostable net flag must be set.
   // the ScopeAlways flag indicates that the object is always scoped
   // on all active connections.
   mNetFlags.set(ScopeAlways | Ghostable);
   dStrcpy(message1, "Hello World 1!", bufLen);
   dStrcpy(message2, "Hello World 2!", bufLen);
}

Here is the constructor. Here, you see that we initialize our net flags to show that we should always be scoped, and that we're to be taken into consideration for ghosting. We also provide some initial values for the message fields.

U32 packUpdate(NetConnection *, U32 mask, BitStream *stream)
{
   // check which states need to be updated, and update them
   if(stream->writeFlag(mask & Message1Mask))
      stream->writeString(message1);
   if(stream->writeFlag(mask & Message2Mask))
      stream->writeString(message2);

   // the return value from packUpdate can set which states still
   // need to be updated for this object.
   return 0;
}

Here's half of the meat of the networking code, the packUpdate() function. (The other half, unpackUpdate(), we'll get to in a second.) The comments in the code pretty much explain everything, however, notice that the code follows a pattern of if(writeFlag(mask & StateMask)) { ... write data ... }. The packUpdate()/unpackUpdate() functions are responsible for reading and writing the dirty bits to the bitstream by themselves.

void unpackUpdate(NetConnection *, BitStream *stream)
{
   // the unpackUpdate function must be symmetrical to packUpdate
   if(stream->readFlag())
   {
      stream->readString(message1);
      Con::printf("Got message1: %s", message1);
   }
   if(stream->readFlag())
   {
      stream->readString(message2);
      Con::printf("Got message2: %s", message2);
   }
}

The other half of the networking code in any NetObject, unpackUpdate(). In our simple example, all that the code does is print the new messages to the console; however, in a more advanced object, you might trigger animations, update complex object properties, or even spawn new objects, based on what packet data you unpack.

void setMessage1(const char *msg)
{
   setMaskBits(Message1Mask);
   dStrcpy(message1, msg, bufLen);
}
void setMessage2(const char *msg)
{
   setMaskBits(Message2Mask);
   dStrcpy(message2, msg, bufLen);
}

Here are the accessors for the two properties. It is good to encapsulate your state variables, so that you don't have to remember to make a call to setMaskBits every time you change anything; the accessors can do it for you. In a more complex object, you might need to set multiple mask bits when you change something; this can be done using the | operator, for instance, setMaskBits( Message1Mask | Message2Mask ); if you changed both messages.

IMPLEMENT_CO_NETOBJECT_V1(SimpleNetObject);

ConsoleMethod(SimpleNetObject, setMessage1, void, 3, 3, "(string msg) Set message 1.")
{
   object->setMessage1(argv[2]);
}

ConsoleMethod(SimpleNetObject, setMessage2, void, 3, 3, "(string msg) Set message 2.")
{
   object->setMessage2(argv[2]);
}

Finally, we use the NetObject implementation macro, IMPLEMENT_CO_NETOBJECT_V1(), to implement our NetObject. It is important that we use this, as it makes Torque perform certain initialization tasks that allow us to send the object over the network. IMPLEMENT_CONOBJECT() doesn't perform these tasks, see the documentation on AbstractClassRep for more details.

Dirty List

Whenever a NetObject becomes "dirty", we add it to the dirty list.

We also remove ourselves on the destructor.

This is done so that when we want to send updates (in NetConnection), it's very fast to find the objects that need to be updated.

NetObject * mPrevDirtyList 

Next item in the dirty list...

NetObject * mNextDirtyList 

Previous item in the dirty list...

NetObject * mDirtyList 

Static pointer to the head of the dirty NetObject list.

Miscellaneous

DECLARE_CONOBJECT(NetObject )

onAdd()

Reimplemented from: SimObject

Reimplemented by: ScenePolyhedralObject, ScenePolyhedralObject, AccumulationVolume, AIPlayer, Camera, ConvexShape, Debris, RenderMeshExample, RenderObjectExample, RenderShapeExample, Explosion, fxFoliageReplicator, fxShapeReplicator, GroundCover, Lightning, ParticleEmitter, ParticleEmitterNode, Precipitation, Ribbon, RibbonNode, Splash, GameBase, GroundPlane, Item, LevelInfo, LightBase, BoxEnvironmentProbe, ReflectionProbe, Skylight, SphereEnvironmentProbe, MissionArea, MissionMarker, WayPoint, SpawnSphere, CameraBookmark, NotesObject, OcclusionVolume, PathCamera, PathShape, PhysicalZone, PhysicsDebris, PhysicsForce, PhysicsShape, Player, Prefab, Projectile, ProximityMine, RigidShape, Scene, SFXEmitter, ShapeBase, StaticShape, Trigger, TSStatic, AITurretShape, TurretShape, FlyingVehicle, HoverVehicle, Vehicle, VehicleBlocker, WheeledVehicle, TerrainBlock, VActor, VPath

onRemove()

Reimplemented from: SimGroup

Reimplemented by: AccumulationVolume, AIPlayer, Camera, ConvexShape, Debris, RenderMeshExample, RenderObjectExample, RenderShapeExample, Explosion, fxFoliageReplicator, fxShapeReplicator, GroundCover, Lightning, ParticleEmitter, ParticleEmitterNode, Precipitation, Ribbon, RibbonNode, Splash, GameBase, GroundPlane, Item, LevelInfo, LightBase, BoxEnvironmentProbe, ReflectionProbe, Skylight, SphereEnvironmentProbe, MissionArea, MissionMarker, CameraBookmark, NotesObject, PathCamera, PathShape, PhysicalZone, PhysicsDebris, PhysicsForce, PhysicsShape, Player, Prefab, Projectile, ProximityMine, RigidShape, Scene, SFXEmitter, ShapeBase, StaticShape, Trigger, TSStatic, AITurretShape, TurretShape, FlyingVehicle, HoverVehicle, Vehicle, VehicleBlocker, WheeledVehicle, TerrainBlock, VActor, VPath

initPersistFields()

Short-Circuited Networking

When we are running with client and server on the same system (which can happen be either when we are doing a single player game, or if we're hosting a multiplayer game and having someone playing on the same instance), we can do some short circuited code to enhance performance.

These variables are used to make it simpler; if we are running in short-circuited mode, the ghosted client gets the server object while the server gets the client object.

note:

"Premature optimization is the root of all evil" - Donald Knuth. The current codebase uses this feature in three small places, mostly for non-speed-related purposes.

getServerObject()

Returns a pointer to the server object when on a local connection.

getClientObject()

Returns a pointer to the client object when on a local connection.

getServerObject(T * netObj)

Template form for the callers convenience.

getClientObject(T * netObj)

Template form for the callers convenience.

Protected Types

NetFlags

Enumerator

IsGhost = BIT(1)

This is a ghost.

ScopeAlways = BIT(6)

Object always ghosts to clients.

ScopeLocal = BIT(7)

Ghost only to local client.

Ghostable = BIT(8)

Set if this object CAN ghost.

MaxNetFlagBit = 15

Public Types

@177

Enumerator

SCOPE_ID_BITS = 14

Private Types

typedef SimGroup Parent 

Public Friends

Private Attributes

U32 mDirtyMaskBits 

Mask indicating which states are dirty and need to be retransmitted on this object.

Protected Attributes

SimObjectPtr< NetObject > mClientObject 

Pointer to the client object on a local connection.

GhostInfo * mFirstObjectRef 

Head of a linked list storing GhostInfos referencing this NetObject.

BitSet32 mNetFlags 

Flag values from NetFlags.

U32 mNetIndex 

The index of this ghost in the GhostManager on the server.

U16 mScope_id 
U16 mScope_refs 
bool mScope_registered 
SimObjectPtr< NetObject > mServerObject 

Pointer to the server object on a local connection.

Public Functions

NetObject()

~NetObject()

addScopeRef()

clearMaskBits(U32 orMask)

Clear the specified bits from the dirty mask.

Parameters:

orMask

Bits to clear

clearScopeAlways()

Stop scoping the object to all connections.

The object's ScopeAlways flag is cleared and the object is removed from all current active connections.

describeSelf()

Reimplemented from: SimObject

Reimplemented by: SceneRootZone, SceneSimpleZone, Portal

filterMaskBits(U32 mask, NetConnection * connection)

getNetIndex()

Get the ghost index of this object.

getScopeId()

getScopeRegistered()

getUpdatePriority(CameraScopeQuery * focusObject, U32 updateMask, S32 updateSkips)

This returns a value which is used to prioritize which objects need to be updated.

In NetObject, our returned priority is 0.1 * updateSkips, so that less recently updated objects are more likely to be updated.

In subclasses, this can be adjusted. For instance, ShapeBase provides priority based on proximity to the camera.

Parameters:

focusObject

Information from a previous call to onCameraScopeQuery.

updateMask

Current update mask.

updateSkips

Number of ticks we haven't been updated for.

return:

A floating point value indicating priority. These are typically < 5.0.

Reimplemented by: GameBase, Projectile, ShapeBase, VPath

isClientObject()

Is this a client object?

isGhost()

Is this is a ghost?

isGhostable()

Is this object ghostable?

isGhostAlways()

Should this object always be ghosted?

isScopeable()

Is this object subject to scoping?

isScopeLocal()

Should this object only be visible to the client which created it?

isServerObject()

Is this a server object?

onCameraScopeQuery(NetConnection * cr, CameraScopeQuery * camInfo)

Queries the object about information used to determine scope.

Something that is 'in scope' is somehow interesting to the client.

If we are a NetConnection's scope object, it calls this method to determine how things should be scoped; basically, we tell it our field of view with camInfo, and have the opportunity to manually mark items as "in scope" as we see fit.

By default, we just mark all ghostable objects as in scope.

Parameters:

cr

Net connection requesting scope information.

camInfo

Information about what this object can see.

Reimplemented by: SceneObject, afxCamera, Player, ShapeBase

packUpdate(NetConnection * conn, U32 mask, BitStream * stream)

Instructs this object to pack its state for transfer over the network.

Parameters:

conn

Net connection being used

mask

Mask indicating fields to transmit.

stream

Bitstream to pack data to

return:

Any bits which were not dealt with. The value is stored by the networking system. Don't set bits you weren't passed.

Reimplemented by: AccumulationVolume, GroundCover, Precipitation, PathCamera, PathShape, afxChoreographer, afxEffectron, afxMagicSpell, afxSelectron, afxSpellBook, afxMooring, afxProjectile, afxStaticShape, DecalRoad, MeshRoad, River, afxCamera, afxMagicMissile, SimpleNetObject, BasicClouds, CloudLayer, ScatterSky, SkyBox, Sun, TimeOfDay, VolumetricFog, WaterBlock, WaterObject, WaterPlane, Forest, ForestWindEmitter, CoverPoint, NavMesh, NavPath, OpenVRTrackedObject, SceneObject, Marker, SceneSimpleZone, Camera, ConvexShape, RenderMeshExample, RenderObjectExample, RenderShapeExample, fxFoliageReplicator, fxShapeReplicator, Lightning, ParticleEmitterNode, RibbonNode, Splash, GameBase, Item, LevelInfo, LightBase, BoxEnvironmentProbe, ReflectionProbe, Skylight, SphereEnvironmentProbe, MissionArea, MissionMarker, WayPoint, SpawnSphere, CameraBookmark, NotesObject, PhysicalZone, PhysicsShape, Player, PointLight, Portal, Prefab, Projectile, ProximityMine, RigidShape, Scene, SFXEmitter, ShapeBase, SpotLight, StaticShape, Trigger, TSStatic, AITurretShape, TurretShape, FlyingVehicle, HoverVehicle, Vehicle, VehicleBlocker, WheeledVehicle, TerrainBlock, SceneAmbientSoundObject, SceneAmbientSoundObject, ScenePolyhedralObject, ScenePolyhedralObject, SceneSpace, SceneZoneSpace, GroundPlane, VHumanoidActor, VActor, VPath

removeScopeRef()

setMaskBits(U32 orMask)

Used to mark a bit as dirty; ie, that its corresponding set of fields need to be transmitted next update.

Parameters:

orMask

Bit(s) to set

setScopeAlways()

Scope the object to all connections.

The object is marked as ScopeAlways and is immediately ghosted to all active connections. This function has no effect if the object is not marked as Ghostable.

setScopeRegistered(bool flag)

unpackUpdate(NetConnection * conn, BitStream * stream)

Instructs this object to read state data previously packed with packUpdate.

Parameters:

conn

Net connection being used

stream

stream to read from

Reimplemented by: afxChoreographer, afxEffectron, afxMagicSpell, afxSelectron, afxSpellBook, afxMooring, afxProjectile, afxStaticShape, DecalRoad, MeshRoad, River, AccumulationVolume, GroundCover, Precipitation, PathCamera, PathShape, afxCamera, afxMagicMissile, SimpleNetObject, BasicClouds, CloudLayer, ScatterSky, SkyBox, Sun, TimeOfDay, VolumetricFog, WaterBlock, WaterObject, WaterPlane, Forest, ForestWindEmitter, CoverPoint, NavMesh, NavPath, OpenVRTrackedObject, SceneObject, Marker, SceneSimpleZone, Camera, ConvexShape, RenderMeshExample, RenderObjectExample, RenderShapeExample, fxFoliageReplicator, fxShapeReplicator, Lightning, ParticleEmitterNode, RibbonNode, Splash, GameBase, Item, LevelInfo, LightBase, BoxEnvironmentProbe, ReflectionProbe, Skylight, SphereEnvironmentProbe, MissionArea, MissionMarker, WayPoint, SpawnSphere, CameraBookmark, NotesObject, PhysicalZone, PhysicsShape, Player, PointLight, Portal, Prefab, Projectile, ProximityMine, RigidShape, Scene, SFXEmitter, ShapeBase, SpotLight, StaticShape, Trigger, TSStatic, AITurretShape, TurretShape, FlyingVehicle, HoverVehicle, Vehicle, VehicleBlocker, WheeledVehicle, TerrainBlock, SceneAmbientSoundObject, SceneAmbientSoundObject, ScenePolyhedralObject, ScenePolyhedralObject, SceneSpace, SceneZoneSpace, GroundPlane, VHumanoidActor, VActor, VPath

Public Static Functions

collapseDirtyList()

Protected Functions

onScopeIdChange()

Reimplemented by: GameBase

Protected Static Functions

addNetworkedField(const char * in_pFieldname, const U32 in_fieldType, const dsize_t in_fieldOffset, AbstractClassRep::WriteDataNotify in_writeDataFn, const char * in_pFieldDocs, U32 flags, U32 networkMask)

addNetworkedField(const char * in_pFieldname, const U32 in_fieldType, const dsize_t in_fieldOffset, AbstractClassRep::WriteDataNotify in_writeDataFn, const U32 in_elementCount, const char * in_pFieldDocs, U32 flags, U32 networkMask)

addNetworkedField(const char * in_pFieldname, const U32 in_fieldType, const dsize_t in_fieldOffset, const char * in_pFieldDocs, U32 flags, U32 networkMask)

Register a simple field.

Parameters:

in_pFieldname

Name of the field.

in_fieldType

Type of the field.

see:

ConsoleDynamicTypes

Parameters:
in_fieldOffset

Offset to the field from the start of the class; calculated using the Offset() macro.

in_pFieldDocs

Usage string for this field.

see:

console_autodoc

addNetworkedField(const char * in_pFieldname, const U32 in_fieldType, const dsize_t in_fieldOffset, const U32 in_elementCount, const char * in_pFieldDocs, U32 flags, U32 networkMask)

Add a networked field.

A networked field is a regular field but with a bitmask flag associated to it. When the field is set, it automatically triggers a call to setMaskBits with the mask associated to the field in order to streamline simple networking code Register a complex field.

Parameters:

in_pFieldname

Name of the field.

in_fieldType

Type of the field.

see:

ConsoleDynamicTypes

Parameters:
in_fieldOffset

Offset to the field from the start of the class; calculated using the Offset() macro.

in_elementCount

Number of elements in this field. Arrays of elements are assumed to be contiguous in memory.

in_pFieldDocs

Usage string for this field.

see:

console_autodoc