SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FullyConnectedMesh2.h
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
20
21
22 #include "NativeFeatureIncludes.h"
23 #if _RAKNET_SUPPORT_FullyConnectedMesh2==1
24
25 #ifndef __FULLY_CONNECTED_MESH_2_H
26 #define __FULLY_CONNECTED_MESH_2_H
27
28 #include "PluginInterface2.h"
29 #include "memoryoverride.h"
30 #include "NativeTypes.h"
31 #include "DS_List.h"
32 #include "string.h"
33 #include "BitStream.h"
34
35 typedef int64_t FCM2Guid;
36
37 namespace SLNet
38 {
40 class RakPeerInterface;
41
47 class RAK_DLL_EXPORT FullyConnectedMesh2 : public PluginInterface2
48 {
49 public:
50 // GetInstance() and DestroyInstance(instance*)
51 STATIC_FACTORY_DECLARATIONS(FullyConnectedMesh2)
52
53 FullyConnectedMesh2();
54 virtual ~FullyConnectedMesh2();
55
61 void SetConnectOnNewRemoteConnection(bool attemptConnection, SLNet::RakString pw);
62
69 RakNetGUID GetConnectedHost(void) const;
70 SystemAddress GetConnectedHostAddr(void) const;
71
73 RakNetGUID GetHostSystem(void) const;
74
76 bool IsHostSystem(void) const;
77
80 void GetHostOrder(DataStructures::List<RakNetGUID> &hostList);
81
84 bool IsConnectedHost(void) const;
85
90 void SetAutoparticipateConnections(bool b);
91
94 void ResetHostCalculation(void);
95
102 void AddParticipant(RakNetGUID rakNetGuid);
103
106 void GetParticipantList(DataStructures::List<RakNetGUID> &participantList);
107
111 bool HasParticipant(RakNetGUID participantGuid);
112
117 // bool GetParticipantContext(RakNetGUID participantGuid, BitStream *userContext);
118
121 // void SetMyContext(BitStream *userContext);
122
128 void ConnectToRemoteNewIncomingConnections(Packet *packet);
129
131 void Clear(void);
132
133 unsigned int GetParticipantCount(void) const;
134 void GetParticipantCount(unsigned int *participantListSize) const;
135
160 virtual void StartVerifiedJoin(RakNetGUID client);
161
170 virtual void RespondOnVerifiedJoinCapable(Packet *packet, bool accept, BitStream *additionalData);
171
184 virtual void GetVerifiedJoinRequiredProcessingList(RakNetGUID host,
185 DataStructures::List<SystemAddress> &addresses,
186 DataStructures::List<RakNetGUID> &guids,
187 DataStructures::List<BitStream*> &userData);
188
200 virtual void GetVerifiedJoinAcceptedAdditionalData(Packet *packet, bool *thisSystemAccepted, DataStructures::List<RakNetGUID> &systemsAccepted, BitStream *additionalData);
201
209 virtual void GetVerifiedJoinRejectedAdditionalData(Packet *packet, BitStream *additionalData);
210
212 virtual void WriteVJCUserData(SLNet::BitStream *bsOut) {(void) bsOut;}
213
219 static void SkipToVJCUserData(SLNet::BitStream *bsIn);
220
225 //virtual void WriteVJSUserData(SLNet::BitStream *bsOut, RakNetGUID userGuid, BitStream *userContext) {(void) bsOut; (void) userGuid; (void) userContext;}
226 virtual void WriteVJSUserData(SLNet::BitStream *bsOut, RakNetGUID userGuid) {(void) bsOut; (void) userGuid;}
227
229 SLNet::TimeUS GetElapsedRuntime(void);
230
232 virtual PluginReceiveResult OnReceive(Packet *packet);
234 virtual void OnRakPeerStartup(void);
236 virtual void OnAttach(void);
238 virtual void OnRakPeerShutdown(void);
240 virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
242 virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
244 virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason);
245
247 struct FCM2Participant
248 {
249 FCM2Participant() {}
250 FCM2Participant(const FCM2Guid &_fcm2Guid, const RakNetGUID &_rakNetGuid) : fcm2Guid(_fcm2Guid), rakNetGuid(_rakNetGuid) {}
251
252 // Low half is a random number.
253 // High half is the order we connected in (totalConnectionCount)
254 FCM2Guid fcm2Guid;
255 RakNetGUID rakNetGuid;
256 // BitStream userContext;
257 };
258
259 enum JoinInProgressState
260 {
261 JIPS_PROCESSING,
262 JIPS_FAILED,
263 JIPS_CONNECTED,
264 JIPS_UNNECESSARY,
265 };
266
267 struct VerifiedJoinInProgressMember
268 {
269 SystemAddress systemAddress;
270 RakNetGUID guid;
271 JoinInProgressState joinInProgressState;
272 BitStream *userData;
273
274 bool workingFlag;
275 };
276
278 struct VerifiedJoinInProgress
279 {
280 RakNetGUID requester;
281 DataStructures::List<VerifiedJoinInProgressMember> vjipMembers;
282 //bool sentResults;
283 };
284
286 unsigned int GetTotalConnectionCount(void) const;
287
288 protected:
289 void PushNewHost(const RakNetGUID &guid, RakNetGUID oldHost);
290 void SendOurFCMGuid(SystemAddress addr);
291 void SendFCMGuidRequest(RakNetGUID rakNetGuid);
292 void SendConnectionCountResponse(SystemAddress addr, unsigned int responseTotalConnectionCount);
293 void OnRequestFCMGuid(Packet *packet);
294 //void OnUpdateUserContext(Packet *packet);
295 void OnRespondConnectionCount(Packet *packet);
296 void OnInformFCMGuid(Packet *packet);
297 void OnUpdateMinTotalConnectionCount(Packet *packet);
298 void AssignOurFCMGuid(void);
299 void CalculateHost(RakNetGUID *rakNetGuid, FCM2Guid *fcm2Guid);
300 // bool AddParticipantInternal( RakNetGUID rakNetGuid, FCM2Guid theirFCMGuid, BitStream *userContext );
301 bool AddParticipantInternal( RakNetGUID rakNetGuid, FCM2Guid theirFCMGuid );
302 void CalculateAndPushHost(void);
303 bool ParticipantListComplete(void);
304 void IncrementTotalConnectionCount(unsigned int i);
305 PluginReceiveResult OnVerifiedJoinStart(Packet *packet);
306 PluginReceiveResult OnVerifiedJoinCapable(Packet *packet);
307 virtual void OnVerifiedJoinFailed(RakNetGUID hostGuid, bool callCloseConnection);
308 virtual void OnVerifiedJoinAccepted(Packet *packet);
309 virtual void OnVerifiedJoinRejected(Packet *packet);
310 unsigned int GetJoinsInProgressIndex(RakNetGUID requester) const;
311 void UpdateVerifiedJoinInProgressMember(const AddressOrGUID systemIdentifier, RakNetGUID guidToAssign, JoinInProgressState newState);
312 bool ProcessVerifiedJoinInProgressIfCompleted(VerifiedJoinInProgress *vjip);
313 void ReadVerifiedJoinInProgressMember(SLNet::BitStream *bsIn, VerifiedJoinInProgressMember *vjipm);
314 unsigned int GetVerifiedJoinInProgressMemberIndex(const AddressOrGUID systemIdentifier, VerifiedJoinInProgress *vjip);
315 void DecomposeJoinCapable(Packet *packet, VerifiedJoinInProgress *vjip);
316 void WriteVerifiedJoinCapable(SLNet::BitStream *bsOut, VerifiedJoinInProgress *vjip);
317 void CategorizeVJIP(VerifiedJoinInProgress *vjip,
318 DataStructures::List<RakNetGUID> &participatingMembersOnClientSucceeded,
319 DataStructures::List<RakNetGUID> &participatingMembersOnClientFailed,
320 DataStructures::List<RakNetGUID> &participatingMembersNotOnClient,
321 DataStructures::List<RakNetGUID> &clientMembersNotParticipatingSucceeded,
322 DataStructures::List<RakNetGUID> &clientMembersNotParticipatingFailed);
323
324 // Used to track how long RakNet has been running. This is so we know who has been running longest
325 SLNet::TimeUS startupTime;
326
327 // Option for SetAutoparticipateConnections
328 bool autoParticipateConnections;
329
330 // totalConnectionCount is roughly maintained across all systems, and increments by 1 each time a new system connects to the mesh
331 // It is always kept at the highest known value
332 // 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.
333 unsigned int totalConnectionCount;
334
335 // Our own ourFCMGuid. Starts at unassigned (0). Assigned once we send ID_FCM2_REQUEST_FCMGUID and get back ID_FCM2_RESPOND_CONNECTION_COUNT
336 FCM2Guid ourFCMGuid;
337
339 DataStructures::List<FCM2Participant*> fcm2ParticipantList;
340
341 RakNetGUID lastPushedHost;
342
343 // Optimization: Store last calculated host in these variables.
344 RakNetGUID hostRakNetGuid;
345 FCM2Guid hostFCM2Guid;
346
347 SLNet::RakString connectionPassword;
348 bool connectOnNewRemoteConnections;
349
350 DataStructures::List<VerifiedJoinInProgress*> joinsInProgress;
351 BitStream myContext;
352 };
353
354 } // namespace SLNet
355
356 /*
357 Startup()
358 ourFCMGuid=unknown
359 totalConnectionCount=0
360 Set startupTime
361
362 AddParticipant()
363 if (sender by guid is a participant)
364 return;
365 AddParticipantInternal(guid);
366 if (ourFCMGuid==unknown)
367 Send to that system a request for their fcmGuid, totalConnectionCount. Inform startupTime.
368 else
369 Send to that system a request for their fcmGuid. Inform total connection count, our fcmGuid
370
371 OnRequestGuid()
372 if (sender by guid is not a participant)
373 {
374 // 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.
375 // It doesn't affect the outcome as long as we still process the data
376 AddParticipantInternal(guid);
377 }
378 if (ourFCMGuid==unknown)
379 {
380 if (includedStartupTime)
381 {
382 // Nobody has a fcmGuid
383
384 if (their startup time is greater than our startup time)
385 ReplyConnectionCount(1);
386 else
387 ReplyConnectionCount(2);
388 }
389 else
390 {
391 // They have a fcmGuid, we do not
392
393 SetMaxTotalConnectionCount(remoteCount);
394 AssignTheirGuid()
395 GenerateOurGuid();
396 SendOurGuid(all);
397 }
398 }
399 else
400 {
401 if (includedStartupTime)
402 {
403 // We have a fcmGuid they do not
404
405 ReplyConnectionCount(totalConnectionCount+1);
406 SendOurGuid(sender);
407 }
408 else
409 {
410 // We both have fcmGuids
411
412 SetMaxTotalConnectionCount(remoteCount);
413 AssignTheirGuid();
414 SendOurGuid(sender);
415 }
416 }
417
418 OnReplyConnectionCount()
419 SetMaxTotalConnectionCount(remoteCount);
420 GenerateOurGuid();
421 SendOurGuid(allParticipants);
422
423 OnReceiveTheirGuid()
424 AssignTheirGuid()
425 */
426
427 #endif
428
429 #endif // _RAKNET_SUPPORT_*