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
NetworkIDManager.cpp
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
18
19
20 #include "slikenet/NetworkIDManager.h"
21 #include "slikenet/NetworkIDObject.h"
22 #include "slikenet/assert.h"
23 #include "slikenet/GetTime.h"
24 #include "slikenet/sleep.h"
25 #include "slikenet/SuperFastHash.h"
26 #include "slikenet/peerinterface.h"
27
28 using namespace SLNet;
29
30 STATIC_FACTORY_DEFINITIONS(NetworkIDManager,NetworkIDManager)
31
32 NetworkIDManager::NetworkIDManager()
33 {
34 startingOffset = RakPeerInterface::Get64BitUniqueRandomNumber();
35 Clear();
36 }
37 NetworkIDManager::~NetworkIDManager(void)
38 {
39
40 }
41 void NetworkIDManager::Clear(void)
42 {
43 memset(networkIdHash,0,sizeof(networkIdHash));
44 }
45 NetworkIDObject *NetworkIDManager::GET_BASE_OBJECT_FROM_ID(NetworkID x)
46 {
47 unsigned int hashIndex=NetworkIDToHashIndex(x);
48 NetworkIDObject *nio=networkIdHash[hashIndex];
49 while (nio)
50 {
51 if (nio->GetNetworkID()==x)
52 return nio;
53 nio=nio->nextInstanceForNetworkIDManager;
54 }
55 return 0;
56 }
57 NetworkID NetworkIDManager::GetNewNetworkID(void)
58 {
59 while (GET_BASE_OBJECT_FROM_ID(++startingOffset))
60 ;
61 if (startingOffset==UNASSIGNED_NETWORK_ID)
62 {
63 while (GET_BASE_OBJECT_FROM_ID(++startingOffset))
64 ;
65 }
66 return startingOffset;
67 }
68 unsigned int NetworkIDManager::NetworkIDToHashIndex(NetworkID networkId)
69 {
70 // return SuperFastHash((const char*) &networkId.guid.g,sizeof(networkId.guid.g)) % NETWORK_ID_MANAGER_HASH_LENGTH;
71 return (unsigned int) (networkId % NETWORK_ID_MANAGER_HASH_LENGTH);
72 }
73 void NetworkIDManager::TrackNetworkIDObject(NetworkIDObject *networkIdObject)
74 {
75 RakAssert(networkIdObject->GetNetworkIDManager()==this);
76 NetworkID rawId = networkIdObject->GetNetworkID();
77 RakAssert(rawId!=UNASSIGNED_NETWORK_ID);
78
79 networkIdObject->nextInstanceForNetworkIDManager=0;
80
81 unsigned int hashIndex=NetworkIDToHashIndex(rawId);
82 // printf("TrackNetworkIDObject hashIndex=%i guid=%s\n",hashIndex, networkIdObject->GetNetworkID().guid.ToString()); // removeme
83 if (networkIdHash[hashIndex]==0)
84 {
85 networkIdHash[hashIndex]=networkIdObject;
86 return;
87 }
88 NetworkIDObject *nio=networkIdHash[hashIndex];
89 // Duplicate insertion?
90 RakAssert(nio!=networkIdObject);
91 // Random GUID conflict?
92 RakAssert(nio->GetNetworkID()!=rawId);
93
94 while (nio->nextInstanceForNetworkIDManager!=0)
95 {
96 nio=nio->nextInstanceForNetworkIDManager;
97
98 // Duplicate insertion?
99 RakAssert(nio!=networkIdObject);
100 // Random GUID conflict?
101 RakAssert(nio->GetNetworkID()!=rawId);
102 }
103
104 nio->nextInstanceForNetworkIDManager=networkIdObject;
105 }
106 void NetworkIDManager::StopTrackingNetworkIDObject(NetworkIDObject *networkIdObject)
107 {
108 RakAssert(networkIdObject->GetNetworkIDManager()==this);
109 NetworkID rawId = networkIdObject->GetNetworkID();
110 RakAssert(rawId!=UNASSIGNED_NETWORK_ID);
111
112 // RakAssert(networkIdObject->GetNetworkID()!=UNASSIGNED_NETWORK_ID);
113 unsigned int hashIndex=NetworkIDToHashIndex(rawId);
114 // printf("hashIndex=%i\n",hashIndex); // removeme
115 NetworkIDObject *nio=networkIdHash[hashIndex];
116 if (nio==0)
117 {
118 RakAssert("NetworkIDManager::StopTrackingNetworkIDObject didn't find object" && 0);
119 return;
120 }
121 if (nio==networkIdObject)
122 {
123 networkIdHash[hashIndex]=nio->nextInstanceForNetworkIDManager;
124 return;
125 }
126
127 while (nio)
128 {
129 if (nio->nextInstanceForNetworkIDManager==networkIdObject)
130 {
131 nio->nextInstanceForNetworkIDManager=networkIdObject->nextInstanceForNetworkIDManager;
132 return;
133 }
134 nio=nio->nextInstanceForNetworkIDManager;
135 }
136
137 RakAssert("NetworkIDManager::StopTrackingNetworkIDObject didn't find object" && 0);
138 }