A management system for your game objects and players to make serialization, scoping, and object creation and destruction easier. More...
#include <ReplicaManager.h>
Inherits PluginInterface2.
A management system for your game objects and players to make serialization, scoping, and object creation and destruction easier.
ReplicaManager is a system manager for your game objects that performs the following tasks: 1. Tracks all locally registered game objects and players and only performs operations to and for those objects and players 2. Allows you to automatically turn off unneeded local and remote functions for your game objects, thus providing convenience and security against unauthorized sends. 3. Sends notifications of existing game objects to new connections, including a download complete message. 4. Sends notifications of new game objects to existing players. A. Serialize and scoping calls are not sent to objects that were not notified of that object. B. Notification calls can be canceled on a per-object basis. Object notification sends are tracked on a per-system per-object basis. 5. Configurable per-system per-object scoping. A. Scoping provides a mechanism to hide and unhide remote objects without destroying the whole object, used when when entities should not be destroyed but are currently not visible to systems. B. Serialize calls are not sent to hidden objects. C. Scoping calls can be canceled on a per-object basis. Scope is tracked on a per-system per-object basis. 6. Replicate, SetScope, SignalSerializeNeeded, and the corresponding Replica interfaces are processed in RakPeer::Receive, rather than immediately. A. This allows the ReplicaManager to reorganize function calls in order by dependency. This allows out of order calls, per-object call cancellation (which also cancels dependencies), and per-object call delays (which also delays dependencies) B. For example, although SetScope and SignalSerializeNeeded have a dependency on SetNetworkID(), you can still call them in the constructor and call SetNetworkID() later, as long as it happens before calling RakPeer::Receive() 7. The system is fast, uses little memory, and is intentionally hands off such that it can work with any game architecture and network topology
What the ReplicaManager system does NOT do for you 1. Actually create or destroy your game objects 2. Associate object destruction events with remote system disconnects. 3. Set networkIDs via SetNetworkID() on newly created objects. 4. Object sub-serialization. Serialize only granular on the level of entire objects. If you want to serialize part of the object, you need to set your own flags and indicate in the BitStream which parts were sent and which not.
Definition at line 119 of file ReplicaManager.h.
anonymous enum |
REPLICA_EXPLICIT_CONSTRUCTION | |
REPLICA_IMPLICIT_CONSTRUCTION | |
REPLICA_SCOPE_TRUE | |
REPLICA_SCOPE_FALSE | |
REPLICA_SERIALIZE |
Definition at line 344 of file ReplicaManager.h.
ReplicaManager::ReplicaManager | ( | ) |
Definition at line 71 of file ReplicaManager.cpp.
ReplicaManager::~ReplicaManager | ( | ) | [virtual] |
unsigned int * ReplicaManager::AccessSerializationFlags | ( | Replica * | replica, | |
SystemAddress | systemAddress | |||
) |
Each participant has a per-remote object bitfield passed to the Replica::Serialize call. This function is used to read and change these flags directly for a single participant. It gives more control than SignalSerializationFlags but only works for a single participant at a time.
[in] | replica | An object previously registered with Replicate |
[in] | systemAddress | The participant to read/write the flags for |
Definition at line 599 of file ReplicaManager.cpp.
bool ReplicaManager::AddParticipant | ( | SystemAddress | systemAddress | ) |
Adds a participant to the ReplicaManager system. Only these participants get packets and we only accept ReplicaManager packets from these participants. This way you can have connections that have nothing to do with your game - for example remote console logins
[in] | systemAddress | Which player you are referring to |
Definition at line 94 of file ReplicaManager.cpp.
void ReplicaManager::AssertParticipantsClear | ( | void | ) |
If you think all participants should have been removed, call this to assert on any that were not. Useful for debugging shutdown or restarts
Definition at line 661 of file ReplicaManager.cpp.
void ReplicaManager::AssertReplicatedObjectsClear | ( | void | ) |
If you think all objects should have been removed, call this to assert on any that were not. Useful for debugging shutdown or restarts
Definition at line 657 of file ReplicaManager.cpp.
void ReplicaManager::Clear | ( | void | ) | [protected] |
Frees all memory.
Definition at line 647 of file ReplicaManager.cpp.
int ReplicaManager::CommandStructComp | ( | Replica *const & | key, | |
const ReplicaManager::CommandStruct & | data | |||
) | [static] |
void ReplicaManager::Construct | ( | Replica * | replica, | |
bool | isCopy, | |||
SystemAddress | systemAddress, | |||
bool | broadcast | |||
) |
Construct the specified object on the specified system Replica::SendConstruction will be called immediately, this is a change from before, because otherwise if you later send other packets that refer to this object, this object won't exist yet. The other system will get Replica::ReceiveConstruction If your system assigns NetworkIDs, do so before calling Replicate as the NetworkID is automatically included in the packet. Replicate packets that are sent to systems that already have this NetworkID are ignored.
[in] | replica | A pointer to your object |
[in] | isCopy | True means that this is a copy of an object that already exists on the systems specified by systemAddress and broadcast. If true, we will consider these systems as having the object without sending a datagram to them. SendConstruction will NOT be called for objects which isCopy is true. |
[in] | systemAddress | The participant to send the command to, or the one to exclude if broadcast is true. |
[in] | broadcast | True to send to all. If systemAddress!=UNASSIGNED_SYSTEM_ADDRESS then this means send to all but that participant |
Definition at line 177 of file ReplicaManager.cpp.
void ReplicaManager::DereferencePointer | ( | Replica * | replica | ) |
Call this before you delete replica. This locally removes all references to this pointer. No messages are sent. Best place to put this is in the destructor of replica
[in] | replica | A pointer to your object |
Definition at line 326 of file ReplicaManager.cpp.
void ReplicaManager::Destruct | ( | Replica * | replica, | |
SystemAddress | systemAddress, | |||
bool | broadcast | |||
) |
Call this with your game objects to have them send Replica::SendDestruction. This will be sent immediately to all participants that have this object. Those participants will get Replica::ReceiveDestruction All pending calls for this object, for this player, are canceled. Nothing is actually deleted - this just signals that the other system called this function. It is up to you to actually delete your object.
[in] | replica | A pointer to your object |
[in] | systemAddress | The participant to send the command to, or the one to exclude if broadcast is true. |
[in] | broadcast | True to send to all systems that have the object. If systemAddress!=UNASSIGNED_SYSTEM_ADDRESS then this means send to all but that participant |
Definition at line 232 of file ReplicaManager.cpp.
void ReplicaManager::DisableReplicaInterfaces | ( | Replica * | replica, | |
unsigned char | interfaceFlags | |||
) |
Lets you disable calling any or all of the interface functions in an instance of Replica This setting is the same for all participants for this object, so if you want per-participant permissions you will need to handle that inside your implementation All functions enabled by default.
[in] | replica | The object you are referring to |
[in] | interfaceFlags | A bitwise-OR of REPLICA_SEND_CONSTRUCTION ... REPLICA_SET_ALL corresponding to the function of the same name |
Definition at line 489 of file ReplicaManager.cpp.
void ReplicaManager::EnableReplicaInterfaces | ( | Replica * | replica, | |
unsigned char | interfaceFlags | |||
) |
Lets you enable calling any or all of the interface functions in an instance of Replica This setting is the same for all participants for this object, so if you want per-participant permissions you will need to handle that inside your implementation All functions enabled by default.
[in] | replica | The object you are referring to |
[in] | interfaceFlags | A bitwise-OR of REPLICA_SEND_CONSTRUCTION ... REPLICA_SET_ALL corresponding to the function of the same name |
Definition at line 475 of file ReplicaManager.cpp.
unsigned ReplicaManager::GetCommandListReplicaIndex | ( | const DataStructures::List< ReplicaManager::CommandStruct > & | commandList, | |
Replica * | replica, | |||
bool * | objectExists | |||
) | const [protected] |
Definition at line 1266 of file ReplicaManager.cpp.
SystemAddress ReplicaManager::GetParticipantAtIndex | ( | unsigned | index | ) |
Returns a SystemAddress previously added with AddParticipant
[in] | index | An index, from 0 to GetParticipantCount()-1. |
Definition at line 538 of file ReplicaManager.cpp.
ReplicaManager::ParticipantStruct * ReplicaManager::GetParticipantBySystemAddress | ( | const SystemAddress | systemAddress | ) | const [protected] |
Definition at line 1161 of file ReplicaManager.cpp.
unsigned ReplicaManager::GetParticipantCount | ( | void | ) | const |
Returns the number of unique participants added with AddParticipant As these systems disconnect, they are no longer participants, so this accurately returns how many participants are using the system
Definition at line 534 of file ReplicaManager.cpp.
Replica * ReplicaManager::GetReplicaAtIndex | ( | unsigned | index | ) |
Returns a previously registered Replica *, from index 0 to GetReplicaCount()-1. The order that Replica * objects are returned in is arbitrary (it currently happens to be ordered by pointer address). Calling Dereplicate immediately deletes the Replica * passed to it, so if you call Dereplicate while using this function the array will be shifted over and the current index will now reference the next object in the array, if there was one.
[in] | index | An index, from 0 to GetReplicaCount()-1. |
Definition at line 530 of file ReplicaManager.cpp.
unsigned ReplicaManager::GetReplicaCount | ( | void | ) | const |
Returns how many Replica instances are registered. This number goes up with each non-duplicate call to Replicate and down with each non-duplicate call to Dereplicate Used for GetReplicaAtIndex if you want to perform some object on all registered Replica objects.
Definition at line 526 of file ReplicaManager.cpp.
bool ReplicaManager::HasParticipant | ( | SystemAddress | systemAddress | ) |
Returns if a participant has been added
Definition at line 542 of file ReplicaManager.cpp.
bool ReplicaManager::IsConstructed | ( | Replica * | replica, | |
SystemAddress | systemAddress | |||
) |
Tells us if a particular system got a SendConstruction() message from this object. e.g. does this object exist on this remote system? This is set by the user when calling Replicate and sending (any) data to outBitStream in Replica::SendConstruction
[in] | replica | The object we are checking |
[in] | systemAddress | The system we are checking |
Definition at line 503 of file ReplicaManager.cpp.
bool ReplicaManager::IsInScope | ( | Replica * | replica, | |
SystemAddress | systemAddress | |||
) |
Tells us if a particular object is in scope for a particular system This is set by the user when calling SetScope and sending (any) data to outBitstream in Replica::SendScopeChange
[in] | replica | The object we are checking |
[in] | systemAddress | The system we are checking |
Definition at line 514 of file ReplicaManager.cpp.
void ReplicaManager::OnClosedConnection | ( | SystemAddress | systemAddress, | |
RakNetGUID | rakNetGUID, | |||
PI2_LostConnectionReason | lostConnectionReason | |||
) | [protected, virtual] |
Called when a connection is dropped because the user called RakPeer::CloseConnection() for a particular system
[in] | systemAddress | The system whose connection was closed |
[in] | rakNetGuid | The guid of the specified system |
[in] | lostConnectionReason | How the connection was closed: manually, connection lost, or notification of disconnection |
Reimplemented from PluginInterface2.
Definition at line 1047 of file ReplicaManager.cpp.
void ReplicaManager::OnNewConnection | ( | SystemAddress | systemAddress, | |
RakNetGUID | rakNetGUID, | |||
bool | isIncoming | |||
) | [protected, virtual] |
Called when we got a new connection
[in] | systemAddress | Address of the new connection |
[in] | rakNetGuid | The guid of the specified system |
[in] | isIncoming | If true, this is ID_NEW_INCOMING_CONNECTION, or the equivalent |
Reimplemented from PluginInterface2.
Definition at line 1059 of file ReplicaManager.cpp.
void ReplicaManager::OnRakPeerShutdown | ( | void | ) | [protected, virtual] |
Called when RakPeer is shutdown.
Reimplemented from PluginInterface2.
Definition at line 1055 of file ReplicaManager.cpp.
PluginReceiveResult ReplicaManager::OnReceive | ( | Packet * | packet | ) | [protected, virtual] |
OnReceive is called for every packet.
[in] | packet | the packet that is being returned to the user |
Reimplemented from PluginInterface2.
Definition at line 1068 of file ReplicaManager.cpp.
int ReplicaManager::ParticipantStructComp | ( | const SystemAddress & | key, | |
ReplicaManager::ParticipantStruct *const & | data | |||
) | [static] |
Definition at line 62 of file ReplicaManager.cpp.
ReplicaReturnResult ReplicaManager::ProcessReceivedCommand | ( | ParticipantStruct * | participantStruct, | |
ReceivedCommand * | receivedCommand | |||
) | [protected] |
Definition at line 1173 of file ReplicaManager.cpp.
void ReplicaManager::ReferencePointer | ( | Replica * | replica | ) |
This makes sure the object is tracked, so you can get calls on it. This will automatically happen if you call Construct, SetScope, or SignalSerializeNeeded with replica Otherwise you need to call this, or for security the system will ignore calls that reference this object, even if given a valid NetworkID Duplicate calls are safe and are simply ignored. Best place to put this is in the SetReceiveConstructionCB callback so that all your objects are registered.
[in] | replica | A pointer to your object |
Try setting the network ID manager if the user forgot
Definition at line 310 of file ReplicaManager.cpp.
int ReplicaManager::RegisteredReplicaComp | ( | Replica *const & | key, | |
const ReplicaManager::RegisteredReplica & | data | |||
) | [static] |
int ReplicaManager::RegisteredReplicaRefOrderComp | ( | const unsigned int & | key, | |
const ReplicaManager::RegisteredReplica & | data | |||
) | [static] |
Definition at line 44 of file ReplicaManager.cpp.
int ReplicaManager::RemoteObjectComp | ( | Replica *const & | key, | |
const ReplicaManager::RemoteObject & | data | |||
) | [static] |
bool ReplicaManager::RemoveParticipant | ( | SystemAddress | systemAddress | ) |
Removes a participant from the data replicator system This is called automatically on ID_DISCONNECTION_NOTIFICATION and ID_CONNECTION_LOST messages, as well as CloseConnection() calls.
[in] | systemAddress | Which player you are referring to |
Definition at line 158 of file ReplicaManager.cpp.
void ReplicaManager::SetAutoConstructToNewParticipants | ( | bool | autoConstruct | ) |
This means automatically construct all known objects to all new participants Has no effect on existing participants Useful if your architecture always has all objects constructed on all systems all the time anyway, or if you want them to normally start constructed Defaults to false.
[in] | autoConstruct | true or false, as desired. |
Definition at line 463 of file ReplicaManager.cpp.
void ReplicaManager::SetAutoParticipateNewConnections | ( | bool | autoAdd | ) |
Do or don't automatically call AddParticipant when new systems connect to us. Won't add automatically add connections that already exist before this was called Defaults to false
[in] | autoAdd | True or false, to add or not |
Definition at line 90 of file ReplicaManager.cpp.
void ReplicaManager::SetAutoSerializeInScope | ( | bool | autoSerialize | ) |
When an object goes in scope for a system, you normally want to serialize that object to that system. Setting this flag to true will call Serialize for you automatically when SendScopeChange returns REPLICA_PROCESSING_DONE and the scopeTrue parameter is true Defaults to false
[in] | autoSerialize | True or false as needed. |
Definition at line 471 of file ReplicaManager.cpp.
void ReplicaManager::SetDefaultScope | ( | bool | scope | ) |
Set the default scope for new objects to all players. Defaults to false, which means Serialize will not be called for new objects automatically. If you set this to true, then new players will get existing objects, and new objects will be sent to existing players This only applies to players that connect and objects that are replicated after this call. Existing object scopes are not affected. Useful to set to true if you don't use scope, or if all objects normally start in scope
[in] | scope | The default scope to use. |
Definition at line 467 of file ReplicaManager.cpp.
void ReplicaManager::SetDownloadCompleteCB | ( | SendDownloadCompleteInterface * | sendDownloadComplete, | |
ReceiveDownloadCompleteInterface * | receiveDownloadComplete | |||
) |
Set your callbacks to be called when, after connecting to another system, you get all objects that system is going to send to you when it is done with the first iteration through the object list. Optional if you want to send and receive the download complete notification
[in] | sendDownloadComplete | A class that implements the SendDownloadCompleteInterface interface. |
[in] | receiveDownloadComplete | A class that implements the ReceiveDownloadCompleteInterface interface. |
Definition at line 452 of file ReplicaManager.cpp.
void ReplicaManager::SetReceiveConstructionCB | ( | ReceiveConstructionInterface * | receiveConstructionInterface | ) |
Required callback Set your callback to parse requests to create new objects. Specifically, when Replica::SendConstruction is called and the networkID of the object is either unset or can't be found, this callback will get that call. How do you know what object to create? It's up to you, but I suggest in Replica::SendConstruction you encode the class name. The best way to do this is with the StringTable class.
[in] | ReceiveConstructionInterface | An instance of a class that implements ReceiveConstructionInterface |
Definition at line 447 of file ReplicaManager.cpp.
void ReplicaManager::SetScope | ( | Replica * | replica, | |
bool | inScope, | |||
SystemAddress | systemAddress, | |||
bool | broadcast | |||
) |
Sets the scope of your object in relation to another participant. Objects that are in-scope for that participant will send out Replica::Serialize calls. Otherwise Serialize calls are not sent. Scoping is useful when you want to disable sends to an object temporarily, without deleting that object. Calling this results in Replica::SendScopeChange being called on the local object and Replica::ReceiveScopeChange on the remote object if that object has been created on that remote system. Your game should ensure that objects not in scope are hidden, but not deallocated, on the remote system. Replica::SendScopeChange with inScope as true will automatically perform Replica::Serialize
[in] | replica | An object previously registered with Replicate |
[in] | inScope | in scope or not. |
[in] | systemAddress | The participant to send the command to, or the one to exclude if broadcast is true. |
[in] | broadcast | True to send to all. If systemAddress!=UNASSIGNED_SYSTEM_ADDRESS then this means send to all but that participant |
Definition at line 354 of file ReplicaManager.cpp.
void ReplicaManager::SetSendChannel | ( | unsigned char | channel | ) |
This channel will be used for all RakPeer::Send calls
[in] | channel | The channel to use for internal RakPeer::Send calls from this system. Defaults to 0. |
Definition at line 458 of file ReplicaManager.cpp.
void ReplicaManager::SignalSerializationFlags | ( | Replica * | replica, | |
SystemAddress | systemAddress, | |||
bool | broadcast, | |||
bool | set, | |||
unsigned int | flags | |||
) |
Each participant has a per-remote object bitfield passed to the Replica::Serialize call. This function can set or unset these flags for one or more participants at the same time. Flags are NOT automatically cleared on serialize. You must clear them when you want to do so.
[in] | replica | An object previously registered with Replicate |
[in] | systemAddress | The participant to set the flags for |
[in] | broadcast | True to apply to all participants. If systemAddress!=UNASSIGNED_SYSTEM_ADDRESS then this means send to all but that participant |
[in] | set | True set the bits set in flags with the per-object per-system serialization flags. False to unset these bits. |
[in] | flags | Modifier to the Per-object per-system flags sent to Replica::Serialize. See the parameter /a set |
Definition at line 546 of file ReplicaManager.cpp.
void ReplicaManager::SignalSerializeNeeded | ( | Replica * | replica, | |
SystemAddress | systemAddress, | |||
bool | broadcast | |||
) |
Signal that data has changed and we need to call Serialize() on the replica object. This will happen if the object has been registered, Replica::SendConstruction wrote to outBitStream and returned true, and the object is in scope for this player.
[in] | replica | An object previously registered with Replicate |
[in] | systemAddress | The participant to send the command to, or the one to exclude if broadcast is true. |
[in] | broadcast | True to send to all. If systemAddress!=UNASSIGNED_SYSTEM_ADDRESS then this means send to all but that participant |
Definition at line 406 of file ReplicaManager.cpp.
void ReplicaManager::Update | ( | void | ) | [virtual] |
Processes all pending commands and does sends as needed. This is called automatically when RakPeerInterface::Receive is called. Depending on where you call RakPeerInterface::Receive you may also wish to call this manually for better responsiveness. For example, if you call RakPeerInterface::Receive at the start of each game tick, this means you would have to wait a render cycle, causing
[in] | peer | Pointer to a valid instance of RakPeerInterface used to perform sends |
Reimplemented from PluginInterface2.
Definition at line 665 of file ReplicaManager.cpp.
Definition at line 455 of file ReplicaManager.h.
Definition at line 459 of file ReplicaManager.h.
Definition at line 458 of file ReplicaManager.h.
bool ReplicaManager::autoConstructToNewParticipants [protected] |
Definition at line 469 of file ReplicaManager.h.
bool ReplicaManager::autoParticipateNewConnections [protected] |
Definition at line 465 of file ReplicaManager.h.
bool ReplicaManager::autoSerializeInScope [protected] |
Definition at line 466 of file ReplicaManager.h.
bool ReplicaManager::defaultScope [protected] |
Definition at line 468 of file ReplicaManager.h.
unsigned int ReplicaManager::nextReferenceIndex [protected] |
Definition at line 470 of file ReplicaManager.h.
DataStructures::OrderedList<SystemAddress, ParticipantStruct *, ReplicaManager::ParticipantStructComp> ReplicaManager::participantList [protected] |
List of participants Each participant has several queues of pending commands Sorted by systemAddress The only complexity is that each participant also needs a list of objects that mirror the variable replicatedObjects so we know per-player if that object is in scope
Definition at line 447 of file ReplicaManager.h.
DataStructures::OrderedList<Replica *, RegisteredReplica, ReplicaManager::RegisteredReplicaComp> ReplicaManager::replicatedObjects [protected] |
List of objects replicated in the Replicate function. Used to make sure queued actions happen on valid pointers, since objects are removed from the list in Dereplicate Sorted by raw pointer address using the default sort
Definition at line 441 of file ReplicaManager.h.
unsigned char ReplicaManager::sendChannel [protected] |
Definition at line 462 of file ReplicaManager.h.