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

FullyConnectedMesh2.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #include "NativeFeatureIncludes.h"
00010 #if _RAKNET_SUPPORT_FullyConnectedMesh2==1
00011 
00012 #ifndef __FULLY_CONNECTED_MESH_2_H
00013 #define __FULLY_CONNECTED_MESH_2_H
00014 
00015 class RakPeerInterface;
00016 #include "PluginInterface2.h"
00017 #include "RakMemoryOverride.h"
00018 #include "DS_Multilist.h"
00019 #include "NativeTypes.h"
00020 #include "DS_List.h"
00021 #include "RakString.h"
00022 
00023 typedef int64_t FCM2Guid;
00024 
00030 class RAK_DLL_EXPORT FullyConnectedMesh2 : public PluginInterface2
00031 {
00032 public:
00033         FullyConnectedMesh2();
00034         virtual ~FullyConnectedMesh2();
00035 
00039         void SetConnectOnNewRemoteConnection(bool attemptConnection, RakNet::RakString pw);
00040 
00046         RakNetGUID GetConnectedHost(void) const;
00047         SystemAddress GetConnectedHostAddr(void) const;
00048 
00050         RakNetGUID GetHostSystem(void) const;
00051 
00053         bool IsHostSystem(void) const;
00054 
00057         bool IsConnectedHost(void) const;
00058 
00062         void SetAutoparticipateConnections(bool b);
00063 
00065         void ResetHostCalculation(void);
00066 
00069         void AddParticipant(RakNetGUID rakNetGuid);
00070 
00071         unsigned int GetParticipantCount(void) const;
00072         void GetParticipantCount(DataStructures::DefaultIndexType *participantListSize) const;
00074         RakNetTimeUS GetElapsedRuntime(void);
00075 
00077         virtual PluginReceiveResult OnReceive(Packet *packet);
00079         virtual void OnRakPeerStartup(void);
00081         virtual void OnAttach(void);
00083         virtual void OnRakPeerShutdown(void);
00085         virtual void OnClosedConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
00087         virtual void OnNewConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
00088 
00090         struct FCM2Participant
00091         {
00092                 FCM2Participant() {}
00093                 FCM2Participant(const FCM2Guid &_fcm2Guid, const RakNetGUID &_rakNetGuid) : fcm2Guid(_fcm2Guid), rakNetGuid(_rakNetGuid) {}
00094 
00095                 // Low half is a random number.
00096                 // High half is the order we connected in (totalConnectionCount)
00097                 FCM2Guid fcm2Guid;
00098                 RakNetGUID rakNetGuid;
00099         };
00100 
00101 protected:
00102         void Clear(void);
00103         void PushNewHost(const RakNetGUID &guid);
00104         void SendOurFCMGuid(SystemAddress addr);
00105         void SendFCMGuidRequest(RakNetGUID rakNetGuid);
00106         void SendConnectionCountResponse(SystemAddress addr, unsigned int responseTotalConnectionCount);
00107         void OnRequestFCMGuid(Packet *packet);
00108         void OnRespondConnectionCount(Packet *packet);
00109         void OnInformFCMGuid(Packet *packet);
00110         void AssignOurFCMGuid(void);
00111         void CalculateHost(RakNetGUID *rakNetGuid, FCM2Guid *fcm2Guid);
00112         bool AddParticipantInternal( RakNetGUID rakNetGuid, FCM2Guid theirFCMGuid );
00113         void CalculateAndPushHost(void);
00114         bool ParticipantListComplete(void);
00115         void IncrementTotalConnectionCount(unsigned int i);
00116 
00117         // Used to track how long RakNet has been running. This is so we know who has been running longest
00118         RakNetTimeUS startupTime;
00119 
00120         // Option for SetAutoparticipateConnections
00121         bool autoParticipateConnections;
00122 
00123         // totalConnectionCount is roughly maintained across all systems, and increments by 1 each time a new system connects to the mesh
00124         // It is always kept at the highest known value
00125         // It is used as the high 4 bytes for new FCMGuids. This causes newer values of FCM2Guid to be higher than lower values. The lowest value is the host.
00126         unsigned int totalConnectionCount;
00127 
00128         // Our own ourFCMGuid. Starts at unassigned (0). Assigned once we send ID_FCM2_REQUEST_FCMGUID and get back ID_FCM2_RESPOND_CONNECTION_COUNT
00129         FCM2Guid ourFCMGuid;
00130 
00132         DataStructures::List<FCM2Participant> participantList;
00133 
00134         RakNetGUID lastPushedHost;
00135 
00136         // Optimization: Store last calculated host in these variables.
00137         RakNetGUID hostRakNetGuid;
00138         FCM2Guid hostFCM2Guid;
00139 
00140         RakNet::RakString connectionPassword;
00141         bool connectOnNewRemoteConnections;
00142 };
00143 
00144 /*
00145 Startup()
00146 ourFCMGuid=unknown
00147 totalConnectionCount=0
00148 Set startupTime
00149 
00150 AddParticipant()
00151 if (sender by guid is a participant)
00152 return;
00153 AddParticipantInternal(guid);
00154 if (ourFCMGuid==unknown)
00155 Send to that system a request for their fcmGuid, totalConnectionCount. Inform startupTime.
00156 else
00157 Send to that system a request for their fcmGuid. Inform total connection count, our fcmGuid
00158 
00159 OnRequestGuid()
00160 if (sender by guid is not a participant)
00161 {
00162         // They added us as a participant, but we didn't add them. This can be caused by lag where both participants are not added at the same time.
00163         // It doesn't affect the outcome as long as we still process the data
00164         AddParticipantInternal(guid);
00165 }
00166 if (ourFCMGuid==unknown)
00167 {
00168         if (includedStartupTime)
00169         {
00170                 // Nobody has a fcmGuid
00171 
00172                 if (their startup time is greater than our startup time)
00173                         ReplyConnectionCount(1);
00174                 else
00175                         ReplyConnectionCount(2);
00176         }
00177         else
00178         {
00179                 // They have a fcmGuid, we do not
00180 
00181                 SetMaxTotalConnectionCount(remoteCount);
00182                 AssignTheirGuid()
00183                 GenerateOurGuid();
00184                 SendOurGuid(all);
00185         }
00186 }
00187 else
00188 {
00189         if (includedStartupTime)
00190         {
00191                 // We have a fcmGuid they do not
00192 
00193                 ReplyConnectionCount(totalConnectionCount+1);
00194                 SendOurGuid(sender);
00195         }
00196         else
00197         {
00198                 // We both have fcmGuids
00199 
00200                 SetMaxTotalConnectionCount(remoteCount);
00201                 AssignTheirGuid();
00202                 SendOurGuid(sender);
00203         }
00204 }
00205 
00206 OnReplyConnectionCount()
00207 SetMaxTotalConnectionCount(remoteCount);
00208 GenerateOurGuid();
00209 SendOurGuid(allParticipants);
00210 
00211 OnReceiveTheirGuid()
00212 AssignTheirGuid()
00213 */
00214 
00215 #endif
00216 
00217 #endif // _RAKNET_SUPPORT_*

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