![]() |
Shadowrun: Awakened 29 September 2011 - Build 871
|
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 #include "PluginInterface2.h" 00016 #include "RakMemoryOverride.h" 00017 #include "DS_Multilist.h" 00018 #include "NativeTypes.h" 00019 #include "DS_List.h" 00020 #include "RakString.h" 00021 00022 typedef int64_t FCM2Guid; 00023 00024 namespace RakNet 00025 { 00027 class RakPeerInterface; 00028 00034 class RAK_DLL_EXPORT FullyConnectedMesh2 : public PluginInterface2 00035 { 00036 public: 00037 // GetInstance() and DestroyInstance(instance*) 00038 STATIC_FACTORY_DECLARATIONS(FullyConnectedMesh2) 00039 00040 FullyConnectedMesh2(); 00041 virtual ~FullyConnectedMesh2(); 00042 00048 void SetConnectOnNewRemoteConnection(bool attemptConnection, RakNet::RakString pw); 00049 00056 RakNetGUID GetConnectedHost(void) const; 00057 SystemAddress GetConnectedHostAddr(void) const; 00058 00060 RakNetGUID GetHostSystem(void) const; 00061 00063 bool IsHostSystem(void) const; 00064 00067 void GetHostOrder(DataStructures::List<RakNetGUID> &hostList); 00068 00071 bool IsConnectedHost(void) const; 00072 00077 void SetAutoparticipateConnections(bool b); 00078 00081 void ResetHostCalculation(void); 00082 00087 void AddParticipant(RakNetGUID rakNetGuid); 00088 00091 void GetParticipantList(DataStructures::List<RakNetGUID> &participantList); 00092 00098 void ConnectToRemoteNewIncomingConnections(Packet *packet); 00099 00101 void Clear(void); 00102 00103 unsigned int GetParticipantCount(void) const; 00104 void GetParticipantCount(DataStructures::DefaultIndexType *participantListSize) const; 00106 RakNet::TimeUS GetElapsedRuntime(void); 00107 00109 virtual PluginReceiveResult OnReceive(Packet *packet); 00111 virtual void OnRakPeerStartup(void); 00113 virtual void OnAttach(void); 00115 virtual void OnRakPeerShutdown(void); 00117 virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); 00119 virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); 00120 00122 struct FCM2Participant 00123 { 00124 FCM2Participant() {} 00125 FCM2Participant(const FCM2Guid &_fcm2Guid, const RakNetGUID &_rakNetGuid) : fcm2Guid(_fcm2Guid), rakNetGuid(_rakNetGuid) {} 00126 00127 // Low half is a random number. 00128 // High half is the order we connected in (totalConnectionCount) 00129 FCM2Guid fcm2Guid; 00130 RakNetGUID rakNetGuid; 00131 }; 00132 00134 unsigned int GetTotalConnectionCount(void) const; 00135 00136 protected: 00137 void PushNewHost(const RakNetGUID &guid, RakNetGUID oldHost); 00138 void SendOurFCMGuid(SystemAddress addr); 00139 void SendFCMGuidRequest(RakNetGUID rakNetGuid); 00140 void SendConnectionCountResponse(SystemAddress addr, unsigned int responseTotalConnectionCount); 00141 void OnRequestFCMGuid(Packet *packet); 00142 void OnRespondConnectionCount(Packet *packet); 00143 void OnInformFCMGuid(Packet *packet); 00144 void OnUpdateMinTotalConnectionCount(Packet *packet); 00145 void AssignOurFCMGuid(void); 00146 void CalculateHost(RakNetGUID *rakNetGuid, FCM2Guid *fcm2Guid); 00147 bool AddParticipantInternal( RakNetGUID rakNetGuid, FCM2Guid theirFCMGuid ); 00148 void CalculateAndPushHost(void); 00149 bool ParticipantListComplete(void); 00150 void IncrementTotalConnectionCount(unsigned int i); 00151 00152 // Used to track how long RakNet has been running. This is so we know who has been running longest 00153 RakNet::TimeUS startupTime; 00154 00155 // Option for SetAutoparticipateConnections 00156 bool autoParticipateConnections; 00157 00158 // totalConnectionCount is roughly maintained across all systems, and increments by 1 each time a new system connects to the mesh 00159 // It is always kept at the highest known value 00160 // 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. 00161 unsigned int totalConnectionCount; 00162 00163 // Our own ourFCMGuid. Starts at unassigned (0). Assigned once we send ID_FCM2_REQUEST_FCMGUID and get back ID_FCM2_RESPOND_CONNECTION_COUNT 00164 FCM2Guid ourFCMGuid; 00165 00167 DataStructures::List<FCM2Participant> fcm2ParticipantList; 00168 00169 RakNetGUID lastPushedHost; 00170 00171 // Optimization: Store last calculated host in these variables. 00172 RakNetGUID hostRakNetGuid; 00173 FCM2Guid hostFCM2Guid; 00174 00175 RakNet::RakString connectionPassword; 00176 bool connectOnNewRemoteConnections; 00177 }; 00178 00179 } // namespace RakNet 00180 00181 /* 00182 Startup() 00183 ourFCMGuid=unknown 00184 totalConnectionCount=0 00185 Set startupTime 00186 00187 AddParticipant() 00188 if (sender by guid is a participant) 00189 return; 00190 AddParticipantInternal(guid); 00191 if (ourFCMGuid==unknown) 00192 Send to that system a request for their fcmGuid, totalConnectionCount. Inform startupTime. 00193 else 00194 Send to that system a request for their fcmGuid. Inform total connection count, our fcmGuid 00195 00196 OnRequestGuid() 00197 if (sender by guid is not a participant) 00198 { 00199 // 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. 00200 // It doesn't affect the outcome as long as we still process the data 00201 AddParticipantInternal(guid); 00202 } 00203 if (ourFCMGuid==unknown) 00204 { 00205 if (includedStartupTime) 00206 { 00207 // Nobody has a fcmGuid 00208 00209 if (their startup time is greater than our startup time) 00210 ReplyConnectionCount(1); 00211 else 00212 ReplyConnectionCount(2); 00213 } 00214 else 00215 { 00216 // They have a fcmGuid, we do not 00217 00218 SetMaxTotalConnectionCount(remoteCount); 00219 AssignTheirGuid() 00220 GenerateOurGuid(); 00221 SendOurGuid(all); 00222 } 00223 } 00224 else 00225 { 00226 if (includedStartupTime) 00227 { 00228 // We have a fcmGuid they do not 00229 00230 ReplyConnectionCount(totalConnectionCount+1); 00231 SendOurGuid(sender); 00232 } 00233 else 00234 { 00235 // We both have fcmGuids 00236 00237 SetMaxTotalConnectionCount(remoteCount); 00238 AssignTheirGuid(); 00239 SendOurGuid(sender); 00240 } 00241 } 00242 00243 OnReplyConnectionCount() 00244 SetMaxTotalConnectionCount(remoteCount); 00245 GenerateOurGuid(); 00246 SendOurGuid(allParticipants); 00247 00248 OnReceiveTheirGuid() 00249 AssignTheirGuid() 00250 */ 00251 00252 #endif 00253 00254 #endif // _RAKNET_SUPPORT_*
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.