• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ReplicaManager.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #include "NativeFeatureIncludes.h"
00009 #if _RAKNET_SUPPORT_ReplicaManager==1
00010 
00011 #ifndef __REPLICA_MANAGER_H
00012 #define __REPLICA_MANAGER_H
00013 
00014 #include "Export.h"
00015 #include "RakNetTypes.h"
00016 #include "DS_OrderedList.h"
00017 #include "PluginInterface2.h"
00018 #include "NetworkIDObject.h"
00019 #include "DS_Queue.h"
00020 #include "ReplicaEnums.h"
00021 
00023 namespace RakNet
00024 {
00025         class BitStream;
00026 };
00027 class Replica;
00028 class ReplicaManager;
00029 
00034 
00039 
00043 class ReceiveConstructionInterface
00044 {
00045 public:
00046         ReceiveConstructionInterface() {}
00047         virtual ~ReceiveConstructionInterface() {}
00048 
00057         virtual ReplicaReturnResult ReceiveConstruction(RakNet::BitStream *inBitStream, RakNetTime timestamp, NetworkID networkID, NetworkIDObject *existingObject, SystemAddress senderId, ReplicaManager *caller)=0;
00058 };
00059 
00063 class SendDownloadCompleteInterface
00064 {
00065 public:
00066         SendDownloadCompleteInterface() {}
00067         virtual ~SendDownloadCompleteInterface() {}
00068 
00074         virtual ReplicaReturnResult SendDownloadComplete(RakNet::BitStream *outBitStream, RakNetTime currentTime, SystemAddress senderId, ReplicaManager *caller)=0;
00075 };
00076 
00080 class ReceiveDownloadCompleteInterface
00081 {
00082 public:
00083         ReceiveDownloadCompleteInterface() {}
00084         virtual ~ReceiveDownloadCompleteInterface() {}
00085 
00090         virtual ReplicaReturnResult ReceiveDownloadComplete(RakNet::BitStream *inBitStream, SystemAddress senderId, ReplicaManager *caller)=0;
00091 };
00092 
00094 
00119 class RAK_DLL_EXPORT ReplicaManager : public PluginInterface2
00120 {
00121 public:
00122         // Constructor
00123         ReplicaManager();
00124 
00125         // Destructor
00126         virtual ~ReplicaManager();
00127 
00130         void AssertReplicatedObjectsClear(void);
00131 
00134         void AssertParticipantsClear(void);
00135 
00140         void SetAutoParticipateNewConnections(bool autoAdd);
00141 
00146         bool AddParticipant(SystemAddress systemAddress);
00147 
00152         bool RemoveParticipant(SystemAddress systemAddress);
00153 
00168         void Construct(Replica *replica, bool isCopy, SystemAddress systemAddress, bool broadcast);
00169 
00182         void Destruct(Replica *replica, SystemAddress systemAddress, bool broadcast);
00183 
00190         void ReferencePointer(Replica *replica);
00191 
00196         void DereferencePointer(Replica *replica);
00197 
00212         void SetScope(Replica *replica, bool inScope, SystemAddress systemAddress, bool broadcast);
00213 
00221         void SignalSerializeNeeded(Replica *replica, SystemAddress systemAddress, bool broadcast);
00222 
00228         void SetReceiveConstructionCB(ReceiveConstructionInterface *receiveConstructionInterface);
00229 
00235         void SetDownloadCompleteCB( SendDownloadCompleteInterface *sendDownloadComplete, ReceiveDownloadCompleteInterface *receiveDownloadComplete );
00236 
00239         void SetSendChannel(unsigned char channel);
00240 
00246         void SetAutoConstructToNewParticipants(bool autoConstruct);
00247 
00253         void SetDefaultScope(bool scope);
00254 
00259         void SetAutoSerializeInScope(bool autoSerialize);
00260 
00266         void Update(void);
00267 
00273         void EnableReplicaInterfaces(Replica *replica, unsigned char interfaceFlags);
00274 
00282         void DisableReplicaInterfaces(Replica *replica, unsigned char interfaceFlags);
00283 
00288         bool IsConstructed(Replica *replica, SystemAddress systemAddress);
00289 
00294         bool IsInScope(Replica *replica, SystemAddress systemAddress);
00295 
00300         unsigned GetReplicaCount(void) const;
00301 
00308     Replica *GetReplicaAtIndex(unsigned index);
00309 
00313         unsigned GetParticipantCount(void) const;
00314 
00318         SystemAddress GetParticipantAtIndex(unsigned index);
00319 
00322         bool HasParticipant(SystemAddress systemAddress);
00323 
00332         void SignalSerializationFlags(Replica *replica, SystemAddress systemAddress, bool broadcast, bool set, unsigned int flags);
00333 
00340         unsigned int* AccessSerializationFlags(Replica *replica, SystemAddress systemAddress);
00341 
00342         // ---------------------------- ALL INTERNAL AFTER HERE ----------------------------
00343 
00344         enum
00345         {
00346                 // Treat the object as on the remote system, and send a packet
00347                 REPLICA_EXPLICIT_CONSTRUCTION=1<<0,
00348                 // Treat the object as on the remote system, but do not send a packet. Overridden by REPLICA_EXPLICIT_CONSTRUCTION.
00349                 REPLICA_IMPLICIT_CONSTRUCTION=1<<1,
00350                 REPLICA_SCOPE_TRUE=1<<2, // Mutually exclusive REPLICA_SCOPE_FALSE
00351                 REPLICA_SCOPE_FALSE=1<<3, // Mutually exclusive REPLICA_SCOPE_TRUE
00352                 REPLICA_SERIALIZE=1<<4,
00353         };
00354 
00357         struct CommandStruct
00358         {
00359                 Replica *replica; // Pointer to an external object - not allocated here.
00360                 unsigned char command; // This is one of the enums immediately above.
00361                 unsigned int userFlags;
00362         };
00363 
00364         struct RegisteredReplica
00365         {
00366                 Replica *replica; // Pointer to an external object - not allocated here.
00367                 RakNetTime lastDeserializeTrue; //   For replicatedObjects it's the last time deserialize returned true.
00368                 unsigned char allowedInterfaces; // Replica interface flags
00369                 unsigned int referenceOrder; // The order in which we started tracking this object.  Used so autoconstruction can send objects in-order
00370         };
00371 
00372         struct RemoteObject
00373         {
00374                 Replica *replica; // Pointer to an external object - not allocated here.
00375         bool inScope; // Is replica in scope or not?
00376                 RakNetTime lastSendTime;
00377                 unsigned int userFlags;
00378         };
00379 
00380         struct ReceivedCommand
00381         {
00382                 SystemAddress systemAddress;
00383                 NetworkID networkID;
00384                 unsigned command; // A packetID
00385                 unsigned u1;
00386                 RakNet::BitStream *userData;
00387         };
00388 
00389 
00390         static int RegisteredReplicaComp( Replica* const &key, const ReplicaManager::RegisteredReplica &data );
00391         static int RegisteredReplicaRefOrderComp( const unsigned int &key, const ReplicaManager::RegisteredReplica &data );
00392         static int RemoteObjectComp( Replica* const &key, const ReplicaManager::RemoteObject &data );
00393         static int CommandStructComp( Replica* const &key, const ReplicaManager::CommandStruct &data );
00394 
00397         struct ParticipantStruct
00398         {
00399                 ~ParticipantStruct();
00400 
00401                 // The player this participant struct represents.
00402                 SystemAddress systemAddress;
00403 
00404                 // Call sendDownloadCompleteCB when REPLICA_SEND_CONSTRUCTION is done for all objects in commandList
00405                 // This variable tracks if we did it yet or not.
00406                 bool callDownloadCompleteCB;
00407 
00408                 // Sorted list of Replica*, sorted by pointer, along with a command to perform on that pointer.
00409                 // Ordering is just for fast lookup.
00410                 // Nothing is allocated inside this list
00411                 // DataStructures::OrderedList<Replica *, CommandStruct, ReplicaManager::CommandStructComp> commandList;
00412                 // June 4, 2007 - Don't sort commands in the command list.  The game replies on processing the commands in order
00413                 DataStructures::List<CommandStruct> commandList;
00414 
00415                 // Sorted list of Replica*, sorted by pointer, along with if that object is inScope or not for this system
00416                 // Only objects that exist on the remote system are in this list, so not all objects are necessarily in this list
00417                 DataStructures::OrderedList<Replica *, RemoteObject, ReplicaManager::RemoteObjectComp> remoteObjectList;
00418 
00419                 // List of pending ReceivedCommand to process
00420                 DataStructures::Queue<ReceivedCommand*> pendingCommands;
00421         };
00422 
00423         static int ParticipantStructComp( const SystemAddress &key, ReplicaManager::ParticipantStruct * const &data );
00424 
00425 protected:
00427         void Clear(void);
00428         // Processes a struct representing a received command
00429         ReplicaReturnResult ProcessReceivedCommand(ParticipantStruct *participantStruct, ReceivedCommand *receivedCommand);
00430         unsigned GetCommandListReplicaIndex(const DataStructures::List<ReplicaManager::CommandStruct> &commandList, Replica *replica, bool *objectExists) const;
00431 
00432         // Plugin interface functions
00433         PluginReceiveResult OnReceive(Packet *packet);
00434         void OnClosedConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
00435         void OnRakPeerShutdown(void);
00436         void OnNewConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
00437 
00441         DataStructures::OrderedList<Replica *, RegisteredReplica, ReplicaManager::RegisteredReplicaComp> replicatedObjects;
00442 
00447         DataStructures::OrderedList<SystemAddress, ParticipantStruct *, ReplicaManager::ParticipantStructComp> participantList;
00448 
00449         // Internal functions
00450         ParticipantStruct* GetParticipantBySystemAddress(const SystemAddress systemAddress) const;
00451 
00452         // Callback pointers.
00453         
00454         // Required callback to handle construction calls
00455         ReceiveConstructionInterface *_constructionCB;
00456 
00457         // Optional callbacks to send and receive download complete.
00458         SendDownloadCompleteInterface *_sendDownloadCompleteCB;
00459         ReceiveDownloadCompleteInterface *_receiveDownloadCompleteCB;
00460 
00461         // Channel to do send calls on.  All calls are reliable ordered except for Replica::Serialize
00462         unsigned char sendChannel;
00463 
00464         // Stores what you pass to SetAutoParticipateNewConnections
00465         bool autoParticipateNewConnections;
00466         bool autoSerializeInScope;
00467 
00468         bool defaultScope;
00469         bool autoConstructToNewParticipants;
00470         unsigned int nextReferenceIndex;
00471 
00472 #ifdef _DEBUG
00473         // Check for and assert on recursive calls to update
00474         bool inUpdate;
00475 #endif
00476 };
00477 
00478 
00479 #endif
00480 
00481 #endif // _RAKNET_SUPPORT_*

Generated on Thu Sep 30 2010 01:27:27 for RakNet by  doxygen 1.7.1