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
00123 ReplicaManager();
00124
00125
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
00343
00344 enum
00345 {
00346
00347 REPLICA_EXPLICIT_CONSTRUCTION=1<<0,
00348
00349 REPLICA_IMPLICIT_CONSTRUCTION=1<<1,
00350 REPLICA_SCOPE_TRUE=1<<2,
00351 REPLICA_SCOPE_FALSE=1<<3,
00352 REPLICA_SERIALIZE=1<<4,
00353 };
00354
00357 struct CommandStruct
00358 {
00359 Replica *replica;
00360 unsigned char command;
00361 unsigned int userFlags;
00362 };
00363
00364 struct RegisteredReplica
00365 {
00366 Replica *replica;
00367 RakNetTime lastDeserializeTrue;
00368 unsigned char allowedInterfaces;
00369 unsigned int referenceOrder;
00370 };
00371
00372 struct RemoteObject
00373 {
00374 Replica *replica;
00375 bool inScope;
00376 RakNetTime lastSendTime;
00377 unsigned int userFlags;
00378 };
00379
00380 struct ReceivedCommand
00381 {
00382 SystemAddress systemAddress;
00383 NetworkID networkID;
00384 unsigned command;
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
00402 SystemAddress systemAddress;
00403
00404
00405
00406 bool callDownloadCompleteCB;
00407
00408
00409
00410
00411
00412
00413 DataStructures::List<CommandStruct> commandList;
00414
00415
00416
00417 DataStructures::OrderedList<Replica *, RemoteObject, ReplicaManager::RemoteObjectComp> remoteObjectList;
00418
00419
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
00429 ReplicaReturnResult ProcessReceivedCommand(ParticipantStruct *participantStruct, ReceivedCommand *receivedCommand);
00430 unsigned GetCommandListReplicaIndex(const DataStructures::List<ReplicaManager::CommandStruct> &commandList, Replica *replica, bool *objectExists) const;
00431
00432
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
00450 ParticipantStruct* GetParticipantBySystemAddress(const SystemAddress systemAddress) const;
00451
00452
00453
00454
00455 ReceiveConstructionInterface *_constructionCB;
00456
00457
00458 SendDownloadCompleteInterface *_sendDownloadCompleteCB;
00459 ReceiveDownloadCompleteInterface *_receiveDownloadCompleteCB;
00460
00461
00462 unsigned char sendChannel;
00463
00464
00465 bool autoParticipateNewConnections;
00466 bool autoSerializeInScope;
00467
00468 bool defaultScope;
00469 bool autoConstructToNewParticipants;
00470 unsigned int nextReferenceIndex;
00471
00472 #ifdef _DEBUG
00473
00474 bool inUpdate;
00475 #endif
00476 };
00477
00478
00479 #endif
00480
00481 #endif // _RAKNET_SUPPORT_*