00001 00002 00003 00004 00005 00006 00007 00008 #include "NetworkIDManager.h" 00009 #include "NetworkIDObject.h" 00010 #include "RakAssert.h" 00011 #include <string.h> // For memset 00012 00013 bool NetworkIDNode::operator==( const NetworkIDNode& right ) const 00014 { 00015 if ( networkID == right.networkID ) 00016 return !0; 00017 00018 return 0; 00019 } 00020 00021 bool NetworkIDNode::operator > ( const NetworkIDNode& right ) const 00022 { 00023 if ( networkID > right.networkID ) 00024 return !0; 00025 00026 return 0; 00027 } 00028 00029 bool NetworkIDNode::operator < ( const NetworkIDNode& right ) const 00030 { 00031 if ( networkID < right.networkID ) 00032 return !0; 00033 00034 return 0; 00035 } 00036 00037 NetworkIDNode::NetworkIDNode() 00038 { 00039 object = 0; 00040 } 00041 00042 NetworkIDNode::NetworkIDNode( NetworkID _networkID, NetworkIDObject *_object ) 00043 { 00044 networkID = _networkID; 00045 object = _object; 00046 } 00047 00048 00049 //------------------------------------------------------------------------------------- 00050 NetworkIDObject* NetworkIDManager::GET_BASE_OBJECT_FROM_ID( NetworkID x ) 00051 { 00052 if ( x == UNASSIGNED_NETWORK_ID ) 00053 return 0; 00054 00055 #if defined(NETWORK_ID_USE_PTR_TABLE) || defined (NETWORK_ID_USE_HASH) 00056 // You can't use this technique in peer to peer mode. Undefine NETWORK_ID_USE_PTR_TABLE in NetworkIDManager.h 00057 RakAssert(NetworkID::IsPeerToPeerMode()==false); 00058 return IDArray[x.localSystemAddress]; 00059 #else 00060 00061 NetworkIDNode *n = IDTree.GetPointerToNode( NetworkIDNode( ( x ), 0 ) ); 00062 00063 if ( n ) 00064 { 00065 return n->object; 00066 } 00067 00068 return 0; 00069 00070 #endif 00071 00072 } 00073 //------------------------------------------------------------------------------------- 00074 void* NetworkIDManager::GET_OBJECT_FROM_ID( NetworkID x ) 00075 { 00076 #if defined(NETWORK_ID_USE_PTR_TABLE) || defined (NETWORK_ID_USE_HASH) 00077 if (x.localSystemAddress==65535) 00078 return 0; 00079 00080 // You can't use this technique in peer to peer mode. Undefine NETWORK_ID_USE_PTR_TABLE in NetworkIDManager.h 00081 RakAssert(NetworkID::IsPeerToPeerMode()==false); 00082 if (IDArray[x.localSystemAddress]) 00083 { 00084 if (IDArray[x.localSystemAddress]->GetParent()) 00085 { 00086 return IDArray[x.localSystemAddress]->GetParent(); 00087 } 00088 else 00089 { 00090 #ifdef _DEBUG 00091 // If this assert hit then this object requires a call to SetParent and it never got one. 00092 RakAssert(IDArray[x.localSystemAddress]->RequiresSetParent()==false); 00093 #endif 00094 return IDArray[x.localSystemAddress]; 00095 } 00096 } 00097 #else 00098 NetworkIDObject *object = (NetworkIDObject *) GET_BASE_OBJECT_FROM_ID( x ); 00099 if (object) 00100 { 00101 if (object->GetParent()) 00102 { 00103 return object->GetParent(); 00104 } 00105 else 00106 { 00107 #ifdef _DEBUG 00108 // If this assert hit then this object requires a call to SetParent and it never got one. 00109 RakAssert(object->RequiresSetParent()==false); 00110 #endif 00111 return object; 00112 } 00113 } 00114 #endif 00115 00116 return 0; 00117 } 00118 //------------------------------------------------------------------------------------- 00119 NetworkIDManager::NetworkIDManager(void) 00120 { 00121 calledSetIsNetworkIDAuthority=false; 00122 sharedNetworkID=0; 00123 externalSystemAddress=UNASSIGNED_SYSTEM_ADDRESS; 00124 guid=UNASSIGNED_RAKNET_GUID; 00125 00126 #if defined(NETWORK_ID_USE_PTR_TABLE) || defined (NETWORK_ID_USE_HASH) 00127 // Last element is reserved for UNASSIGNED_NETWORK_ID 00128 IDArray = (NetworkIDObject**) rakMalloc_Ex(sizeof(NetworkIDObject*) * 65534, __FILE__, __LINE__); 00129 memset(IDArray,0,sizeof(NetworkIDObject*)*65534); 00130 // You can't use this technique in peer to peer mode. Undefine NETWORK_ID_USE_PTR_TABLE in NetworkIDManager.h 00131 RakAssert(NetworkID::IsPeerToPeerMode()==false); 00132 #endif 00133 } 00134 //------------------------------------------------------------------------------------- 00135 NetworkIDManager::~NetworkIDManager(void) 00136 { 00137 #if defined(NETWORK_ID_USE_PTR_TABLE) || defined (NETWORK_ID_USE_HASH) 00138 rakFree_Ex(IDArray, __FILE__, __LINE__ ); 00139 #endif 00140 } 00141 //------------------------------------------------------------------------------------- 00142 00143 void NetworkIDManager::SetIsNetworkIDAuthority(bool isAuthority) 00144 { 00145 isNetworkIDAuthority=isAuthority; 00146 calledSetIsNetworkIDAuthority=true; 00147 } 00148 00149 //------------------------------------------------------------------------------------- 00150 00151 bool NetworkIDManager::IsNetworkIDAuthority(void) const 00152 { 00153 RakAssert(calledSetIsNetworkIDAuthority); 00154 return isNetworkIDAuthority; 00155 } 00156 00157 //------------------------------------------------------------------------------------- 00158 00159 unsigned short NetworkIDManager::GetSharedNetworkID( void ) 00160 { 00161 RakAssert(calledSetIsNetworkIDAuthority); 00162 return sharedNetworkID; 00163 } 00164 00165 //------------------------------------------------------------------------------------- 00166 00167 void NetworkIDManager::SetSharedNetworkID( unsigned short i ) 00168 { 00169 RakAssert(calledSetIsNetworkIDAuthority); 00170 sharedNetworkID = i; 00171 } 00172 00173 //------------------------------------------------------------------------------------- 00174 void NetworkIDManager::SetExternalSystemAddress(SystemAddress systemAddress) 00175 { 00176 RakAssert(calledSetIsNetworkIDAuthority); 00177 RakAssert(systemAddress!=UNASSIGNED_SYSTEM_ADDRESS); 00178 externalSystemAddress=systemAddress; 00179 } 00180 //------------------------------------------------------------------------------------- 00181 SystemAddress NetworkIDManager::GetExternalSystemAddress(void) 00182 { 00183 RakAssert(calledSetIsNetworkIDAuthority); 00184 return externalSystemAddress; 00185 } 00186 //------------------------------------------------------------------------------------- 00187 void NetworkIDManager::SetGuid(RakNetGUID g) 00188 { 00189 guid=g; 00190 } 00191 RakNetGUID NetworkIDManager::GetGuid(void) 00192 { 00193 RakAssert(guid!=UNASSIGNED_RAKNET_GUID); 00194 return guid; 00195 }