00001
00002
00003
00004
00005
00006
00007
00008 #include "RakNetDefines.h"
00009 #include "RakPeer.h"
00010 #include "RakNetTypes.h"
00011
00012 #ifdef _WIN32
00013 #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00014
00015 #else
00016 #define closesocket close
00017 #include <unistd.h>
00018 #endif
00019
00020 #if defined(new)
00021 #pragma push_macro("new")
00022 #undef new
00023 #define RMO_NEW_UNDEF_ALLOCATING_QUEUE
00024 #endif
00025
00026
00027 #include <time.h>
00028
00029 #include <ctype.h>
00030 #include <string.h>
00031 #include "GetTime.h"
00032 #include "MessageIdentifiers.h"
00033 #include "DS_HuffmanEncodingTree.h"
00034 #include "Rand.h"
00035 #include "PluginInterface2.h"
00036 #include "StringCompressor.h"
00037 #include "StringTable.h"
00038 #include "NetworkIDObject.h"
00039 #include "RakNetTypes.h"
00040 #include "SHA1.h"
00041 #include "RakSleep.h"
00042 #include "RouterInterface.h"
00043 #include "RakAssert.h"
00044 #include "RakNetVersion.h"
00045 #include "NetworkIDManager.h"
00046 #include "DataBlockEncryptor.h"
00047 #include "gettimeofday.h"
00048 #include "SignaledEvent.h"
00049 #include "SuperFastHash.h"
00050
00051 RAK_THREAD_DECLARATION(UpdateNetworkLoop);
00052 RAK_THREAD_DECLARATION(RecvFromLoop);
00053 RAK_THREAD_DECLARATION(UDTConnect);
00054
00055 #define REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE 8
00056
00057 #if !defined ( __APPLE__ ) && !defined ( __APPLE_CC__ )
00058 #include <stdlib.h>
00059 #endif
00060
00061 #if defined(_XBOX) || defined(X360)
00062
00063 #elif defined(_WIN32)
00064
00065 #else
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 #endif
00080
00081
00082 static const int NUM_MTU_SIZES=3;
00083 #if defined(_XBOX) || defined(X360)
00084
00085 #elif __PC__
00086 static const int mtuSizes[NUM_MTU_SIZES]={1200, 1200, 576};
00087 #else
00088 static const int mtuSizes[NUM_MTU_SIZES]={MAXIMUM_MTU_SIZE, 1200, 576};
00089 #endif
00090
00091
00092 #include "RakAlloca.h"
00093
00094
00095
00096
00097
00098 #ifdef _MSC_VER
00099 #pragma warning( push )
00100 #endif
00101
00102 using namespace RakNet;
00103
00104 static RakNetRandom rnr;
00105
00106 struct RakPeerAndIndex
00107 {
00108 SOCKET s;
00109 unsigned short remotePortRakNetWasStartedOn_PS3;
00110 RakPeer *rakPeer;
00111 };
00112
00113
00114
00115
00116
00117 #ifdef HOST_ENDIAN_IS_BIG
00118 void __inline BSWAPCPY(unsigned char *dest, unsigned char *source, int bytesize)
00119 {
00120 #ifdef _DEBUG
00121 RakAssert( (bytesize % 4 == 0)&&(bytesize)&& "Something is wrong with your exponent or modulus size.");
00122 #endif
00123 int i;
00124 for (i=0; i<bytesize; i+=4)
00125 {
00126 dest[i] = source[i+3];
00127 dest[i+1] = source[i+2];
00128 dest[i+2] = source[i+1];
00129 dest[i+3] = source[i];
00130 }
00131 }
00132 void __inline BSWAPSELF(unsigned char *source, int bytesize)
00133 {
00134 #ifdef _DEBUG
00135 RakAssert( (bytesize % 4 == 0)&&(bytesize)&& "Something is wrong with your exponent or modulus size.");
00136 #endif
00137 int i;
00138 unsigned char a, b;
00139 for (i=0; i<bytesize; i+=4)
00140 {
00141 a = source[i];
00142 b = source[i+1];
00143 source[i] = source[i+3];
00144 source[i+1] = source[i+2];
00145 source[i+2] = b;
00146 source[i+3] = a;
00147 }
00148 }
00149 #endif
00150
00151 static const unsigned int SYN_COOKIE_OLD_RANDOM_NUMBER_DURATION = 10000;
00152 static const unsigned int MAX_OFFLINE_DATA_LENGTH=400;
00153
00154
00155
00156 #pragma warning(disable:4309) // 'initializing' : truncation of constant value
00157
00158 static const char OFFLINE_MESSAGE_DATA_ID[16]={0x00,0xFF,0xFF,0x00,0xFE,0xFE,0xFE,0xFE,0xFD,0xFD,0xFD,0xFD,0x12,0x34,0x56,0x78};
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 struct PacketFollowedByData
00171 {
00172 Packet p;
00173 unsigned char data[1];
00174 };
00175
00176 Packet *RakPeer::AllocPacket(unsigned dataSize, const char *file, unsigned int line)
00177 {
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 Packet *p;
00189 packetAllocationPoolMutex.Lock();
00190 p = packetAllocationPool.Allocate(file,line);
00191 packetAllocationPoolMutex.Unlock();
00192 p = new ((void*)p) Packet;
00193 p->data=(unsigned char*) rakMalloc_Ex(dataSize,file,line);
00194 p->length=dataSize;
00195 p->bitSize=BYTES_TO_BITS(dataSize);
00196 p->deleteData=true;
00197 p->guid=UNASSIGNED_RAKNET_GUID;
00198 return p;
00199 }
00200
00201 Packet *RakPeer::AllocPacket(unsigned dataSize, unsigned char *data, const char *file, unsigned int line)
00202 {
00203
00204 Packet *p;
00205 packetAllocationPoolMutex.Lock();
00206 p = packetAllocationPool.Allocate(file,line);
00207 packetAllocationPoolMutex.Unlock();
00208 p = new ((void*)p) Packet;
00209 p->data=data;
00210 p->length=dataSize;
00211 p->bitSize=BYTES_TO_BITS(dataSize);
00212 p->deleteData=true;
00213 p->guid=UNASSIGNED_RAKNET_GUID;
00214 return p;
00215 }
00216
00217
00218
00219
00220 RakPeer::RakPeer()
00221 {
00222 StringCompressor::AddReference();
00223 RakNet::StringTable::AddReference();
00224
00225 #if !defined(_XBOX) && !defined(_WIN32_WCE) && !defined(X360)
00226 usingSecurity = false;
00227 #endif
00228 memset( frequencyTable, 0, sizeof( unsigned int ) * 256 );
00229 rawBytesSent = rawBytesReceived = compressedBytesSent = compressedBytesReceived = 0;
00230 outputTree = inputTree = 0;
00231 defaultMTUSize = mtuSizes[NUM_MTU_SIZES-1];
00232 trackFrequencyTable = false;
00233 maximumIncomingConnections = 0;
00234 maximumNumberOfPeers = 0;
00235
00236 remoteSystemList = 0;
00237 remoteSystemLookup=0;
00238 bytesSentPerSecond = bytesReceivedPerSecond = 0;
00239 endThreads = true;
00240 isMainLoopThreadActive = false;
00241 isRecvFromLoopThreadActive = false;
00242
00243 #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0
00244 occasionalPing = true;
00245 #else
00246 occasionalPing = false;
00247 #endif
00248 allowInternalRouting=false;
00249 for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++)
00250 mySystemAddress[i]=UNASSIGNED_SYSTEM_ADDRESS;
00251 allowConnectionResponseIPMigration = false;
00252 blockOnRPCReply=false;
00253
00254 incomingPasswordLength=0;
00255 router=0;
00256 splitMessageProgressInterval=0;
00257
00258 unreliableTimeout=1000;
00259 networkIDManager=0;
00260 maxOutgoingBPS=0;
00261 firstExternalID=UNASSIGNED_SYSTEM_ADDRESS;
00262 myGuid=UNASSIGNED_RAKNET_GUID;
00263 networkIDManager=0;
00264 userUpdateThreadPtr=0;
00265 userUpdateThreadData=0;
00266
00267 #ifdef _DEBUG
00268
00269 defaultTimeoutTime=30000;
00270 #else
00271 defaultTimeoutTime=10000;
00272 #endif
00273
00274 #ifdef _DEBUG
00275 _packetloss=0.0;
00276 _minExtraPing=0;
00277 _extraPingVariance=0;
00278 #endif
00279
00280 #ifdef _RAKNET_THREADSAFE
00281 bufferedCommands.SetPageSize(sizeof(BufferedCommandStruct)*32);
00282 socketQueryOutput.SetPageSize(sizeof(SocketQueryOutput)*8);
00283 bufferedPackets.SetPageSize(sizeof(RecvFromStruct)*32);
00284 #endif
00285
00286 packetAllocationPoolMutex.Lock();
00287 packetAllocationPool.SetPageSize(sizeof(DataStructures::MemoryPool<Packet>::MemoryWithPage)*32);
00288 packetAllocationPoolMutex.Unlock();
00289
00290 remoteSystemIndexPool.SetPageSize(sizeof(DataStructures::MemoryPool<RemoteSystemIndex>::MemoryWithPage)*32);
00291
00292 GenerateGUID();
00293
00294 quitAndDataEvents.InitEvent();
00295 limitConnectionFrequencyFromTheSameIP=false;
00296 ResetSendReceipt();
00297 }
00298
00299
00300
00301
00302 RakPeer::~RakPeer()
00303 {
00304
00305
00306
00307 Shutdown( 0, 0 );
00308
00309
00310 ClearBanList();
00311
00312 StringCompressor::RemoveReference();
00313 RakNet::StringTable::RemoveReference();
00314
00315 quitAndDataEvents.CloseEvent();
00316 }
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 bool RakPeer::Startup( unsigned short maxConnections, int _threadSleepTimer, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount, int threadPriority )
00332 {
00333 if (IsActive())
00334 return false;
00335
00336 if (threadPriority==-99999)
00337 {
00338 #if defined(_XBOX) || defined(X360)
00339
00340 #elif defined(_WIN32)
00341 threadPriority=0;
00342 #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00343
00344 #else
00345 threadPriority=1000;
00346 #endif
00347 }
00348
00349
00350 #if !defined(_XBOX) && !defined(X360)
00351 memset( ipList, 0, sizeof( char ) * 16 * MAXIMUM_NUMBER_OF_INTERNAL_IDS );
00352 SocketLayer::Instance()->GetMyIP( ipList,binaryAddresses );
00353 #endif
00354
00355 unsigned int i;
00356 if (myGuid==UNASSIGNED_RAKNET_GUID)
00357 {
00358 seedMT( GenerateSeedFromGuid() );
00359 }
00360
00361 rnr.SeedMT( GenerateSeedFromGuid() );
00362
00363 RakPeerAndIndex rpai[32];
00364 RakAssert(socketDescriptorCount<32);
00365
00366 RakAssert(socketDescriptors && socketDescriptorCount>=1);
00367
00368 if (socketDescriptors==0 || socketDescriptorCount<1)
00369 return false;
00370
00371
00372
00373
00374 RakAssert( maxConnections > 0 );
00375
00376 if ( maxConnections <= 0 )
00377 return false;
00378
00379 DerefAllSockets();
00380
00381
00382
00383 for (i=0; i<socketDescriptorCount; i++)
00384 {
00385 if (socketDescriptors[i].port!=0 && SocketLayer::Instance()->IsPortInUse(socketDescriptors[i].port, socketDescriptors[i].hostAddress)==true)
00386 {
00387 DerefAllSockets();
00388 return false;
00389 }
00390
00391 RakNetSmartPtr<RakNetSocket> rns(RakNet::OP_NEW<RakNetSocket>(__FILE__,__LINE__));
00392 if (socketDescriptors[i].remotePortRakNetWasStartedOn_PS3==0)
00393 rns->s = (unsigned int) SocketLayer::Instance()->CreateBoundSocket( socketDescriptors[i].port, true, socketDescriptors[i].hostAddress, 100 );
00394 else
00395 rns->s = (unsigned int) SocketLayer::Instance()->CreateBoundSocket_PS3Lobby( socketDescriptors[i].port, true, socketDescriptors[i].hostAddress );
00396
00397 if ((SOCKET)rns->s==(SOCKET)-1)
00398 {
00399 DerefAllSockets();
00400 return false;
00401 }
00402
00403
00404 rns->boundAddress=SocketLayer::GetSystemAddress( rns->s );
00405 rns->remotePortRakNetWasStartedOn_PS3=socketDescriptors[i].remotePortRakNetWasStartedOn_PS3;
00406 rns->userConnectionSocketIndex=i;
00407
00408
00409 int zero=0;
00410 if (SocketLayer::Instance()->SendTo((SOCKET)rns->s, (const char*) &zero,4,"127.0.0.1", rns->boundAddress.port, rns->remotePortRakNetWasStartedOn_PS3)!=0)
00411 {
00412 DerefAllSockets();
00413 return false;
00414 }
00415
00416 socketList.Push(rns, __FILE__, __LINE__ );
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 }
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488 if ( maximumNumberOfPeers == 0 )
00489 {
00490
00491 if ( maximumIncomingConnections > maxConnections )
00492 maximumIncomingConnections = maxConnections;
00493
00494 maximumNumberOfPeers = maxConnections;
00495
00496
00497
00498
00499
00500
00501
00502 remoteSystemList = RakNet::OP_NEW_ARRAY<RemoteSystemStruct>(maximumNumberOfPeers, __FILE__, __LINE__ );
00503
00504 remoteSystemLookup = RakNet::OP_NEW_ARRAY<RemoteSystemIndex*>((unsigned int) maximumNumberOfPeers * REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE, __FILE__, __LINE__ );
00505
00506 for ( i = 0; i < maximumNumberOfPeers; i++ )
00507
00508 {
00509
00510 remoteSystemList[ i ].isActive = false;
00511 remoteSystemList[ i ].systemAddress = UNASSIGNED_SYSTEM_ADDRESS;
00512 remoteSystemList[ i ].guid = UNASSIGNED_RAKNET_GUID;
00513 remoteSystemList[ i ].myExternalSystemAddress = UNASSIGNED_SYSTEM_ADDRESS;
00514 remoteSystemList[ i ].connectMode=RemoteSystemStruct::NO_ACTION;
00515 remoteSystemList[ i ].MTUSize = defaultMTUSize;
00516 #ifdef _DEBUG
00517 remoteSystemList[ i ].reliabilityLayer.ApplyNetworkSimulator(_packetloss, _minExtraPing, _extraPingVariance);
00518 #endif
00519 }
00520
00521 for (unsigned int i=0; i < (unsigned int) maximumNumberOfPeers*REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE; i++)
00522 {
00523 remoteSystemLookup[i]=0;
00524 }
00525 }
00526
00527
00528
00529
00530
00531 if ( endThreads )
00532 {
00533
00534
00535
00536 memset( frequencyTable, 0, sizeof( unsigned int ) * 256 );
00537
00538
00539 rawBytesSent = rawBytesReceived = compressedBytesSent = compressedBytesReceived = 0;
00540
00541 updateCycleIsRunning = false;
00542 endThreads = false;
00543
00544 threadSleepTimer = _threadSleepTimer;
00545
00546 ClearBufferedCommands();
00547 ClearBufferedPackets();
00548 ClearSocketQueryOutput();
00549
00550
00551 for (int ipIndex=0; ipIndex < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ipIndex++)
00552 {
00553 #if !defined(_XBOX) && !defined(X360)
00554 if (ipList[ipIndex][0])
00555 {
00556 mySystemAddress[ipIndex].port = SocketLayer::Instance()->GetLocalPort(socketList[0]->s);
00557
00558 mySystemAddress[ipIndex].binaryAddress = inet_addr( ipList[ ipIndex ] );
00559
00560
00561 }
00562 else
00563 mySystemAddress[ipIndex]=UNASSIGNED_SYSTEM_ADDRESS;
00564 #else
00565 mySystemAddress[ipIndex]=UNASSIGNED_SYSTEM_ADDRESS;
00566 #endif
00567 }
00568
00569 if ( isMainLoopThreadActive == false )
00570 {
00571
00572 int errorCode = RakNet::RakThread::Create(UpdateNetworkLoop, this, threadPriority);
00573
00574 if ( errorCode != 0 )
00575 {
00576 Shutdown( 0, 0 );
00577 return false;
00578 }
00579
00580 for (i=0; i<socketDescriptorCount; i++)
00581 {
00582 rpai[i].remotePortRakNetWasStartedOn_PS3=socketDescriptors[i].remotePortRakNetWasStartedOn_PS3;
00583 rpai[i].s=socketList[i]->s;
00584 rpai[i].rakPeer=this;
00585 isRecvFromLoopThreadActive=false;
00586 errorCode = RakNet::RakThread::Create(RecvFromLoop, &rpai[i], threadPriority);
00587
00588 if ( errorCode != 0 )
00589 {
00590 Shutdown( 0, 0 );
00591 return false;
00592 }
00593
00594 while ( isRecvFromLoopThreadActive == false )
00595 RakSleep(10);
00596 }
00597
00598 }
00599
00600
00601
00602 while ( isMainLoopThreadActive == false )
00603 RakSleep(10);
00604
00605 }
00606
00607 for (i=0; i < messageHandlerList.Size(); i++)
00608 {
00609 messageHandlerList[i]->OnRakPeerStartup();
00610 }
00611
00612 #ifdef USE_THREADED_SEND
00613 SendToThread::AddRef();
00614 #endif
00615
00616 return true;
00617 }
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637 void RakPeer::InitializeSecurity(const char *pubKeyE, const char *pubKeyN, const char *privKeyP, const char *privKeyQ )
00638 {
00639 #if !defined(_XBOX) && !defined(_WIN32_WCE) && !defined(X360)
00640 if ( endThreads == false )
00641 return ;
00642
00643
00644
00645 if (
00646
00647 ( privKeyP && privKeyQ == 0 ) ||
00648 ( privKeyQ && privKeyP == 0 ) ||
00649 ( pubKeyE && pubKeyN == 0 ) ||
00650 ( pubKeyN && pubKeyE == 0 ) )
00651 {
00652
00653 RakAssert( 0 );
00654 }
00655
00656 GenerateSYNCookieRandomNumber();
00657
00658 usingSecurity = true;
00659
00660 if ( privKeyP == 0 && privKeyQ == 0 && pubKeyE == 0 && pubKeyN == 0 )
00661 {
00662 keysLocallyGenerated = true;
00663
00664 rsacrypt.generatePrivateKey(RAKNET_RSA_FACTOR_LIMBS);
00665 }
00666
00667 else
00668 {
00669 if ( pubKeyE && pubKeyN )
00670 {
00671
00672 memcpy( ( char* ) & publicKeyE, pubKeyE, sizeof( publicKeyE ) );
00673 memcpy( publicKeyN, pubKeyN, sizeof( publicKeyN ) );
00674 }
00675
00676 if ( privKeyP && privKeyQ )
00677 {
00678 bool b = rsacrypt.setPrivateKey( (const uint32_t*) privKeyP, (const uint32_t*) privKeyQ, RAKNET_RSA_FACTOR_LIMBS/2);
00679 (void) b;
00680 RakAssert(b);
00681
00682
00683
00684
00685
00686
00687
00688 }
00689
00690 keysLocallyGenerated = false;
00691 }
00692 #endif
00693 }
00694
00695
00696
00697
00698
00699
00700 void RakPeer::DisableSecurity( void )
00701 {
00702 #if !defined(_XBOX) && !defined(_WIN32_WCE) && !defined(X360)
00703 if ( endThreads == false )
00704 return ;
00705
00706 usingSecurity = false;
00707 #endif
00708 }
00709
00710
00711 void RakPeer::AddToSecurityExceptionList(const char *ip)
00712 {
00713 securityExceptionMutex.Lock();
00714 securityExceptionList.Insert(RakString(ip), __FILE__, __LINE__);
00715 securityExceptionMutex.Unlock();
00716 }
00717
00718
00719 void RakPeer::RemoveFromSecurityExceptionList(const char *ip)
00720 {
00721 if (securityExceptionList.Size()==0)
00722 return;
00723
00724 if (ip==0)
00725 {
00726 securityExceptionMutex.Lock();
00727 securityExceptionList.Clear(false, __FILE__, __LINE__);
00728 securityExceptionMutex.Unlock();
00729 }
00730 else
00731 {
00732 unsigned i=0;
00733 securityExceptionMutex.Lock();
00734 while (i < securityExceptionList.Size())
00735 {
00736 if (securityExceptionList[i].IPAddressMatch(ip))
00737 {
00738 securityExceptionList[i]=securityExceptionList[securityExceptionList.Size()-1];
00739 securityExceptionList.RemoveAtIndex(securityExceptionList.Size()-1);
00740 }
00741 else
00742 i++;
00743 }
00744 securityExceptionMutex.Unlock();
00745 }
00746 }
00747
00748 bool RakPeer::IsInSecurityExceptionList(const char *ip)
00749 {
00750 if (securityExceptionList.Size()==0)
00751 return false;
00752
00753 unsigned i=0;
00754 securityExceptionMutex.Lock();
00755 for (; i < securityExceptionList.Size(); i++)
00756 {
00757 if (securityExceptionList[i].IPAddressMatch(ip))
00758 {
00759 securityExceptionMutex.Unlock();
00760 return true;
00761 }
00762 }
00763 securityExceptionMutex.Unlock();
00764 return false;
00765 }
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776 void RakPeer::SetMaximumIncomingConnections( unsigned short numberAllowed )
00777 {
00778 maximumIncomingConnections = numberAllowed;
00779 }
00780
00781
00782
00783
00784
00785 unsigned short RakPeer::GetMaximumIncomingConnections( void ) const
00786 {
00787 return maximumIncomingConnections;
00788 }
00789
00790
00791
00792
00793
00794 unsigned short RakPeer::NumberOfConnections(void) const
00795 {
00796 unsigned short i, count=0;
00797 for (i=0; i < maximumNumberOfPeers; i++)
00798 if (remoteSystemList[i].isActive)
00799 count++;
00800 return count;
00801 }
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813 void RakPeer::SetIncomingPassword( const char* passwordData, int passwordDataLength )
00814 {
00815
00816
00817
00818 if (passwordDataLength > 255)
00819 passwordDataLength=255;
00820
00821 if (passwordData==0)
00822 passwordDataLength=0;
00823
00824
00825
00826 if (passwordDataLength>0)
00827 memcpy(incomingPassword, passwordData, passwordDataLength);
00828 incomingPasswordLength=(unsigned char)passwordDataLength;
00829 }
00830
00831
00832 void RakPeer::GetIncomingPassword( char* passwordData, int *passwordDataLength )
00833 {
00834 if (passwordData==0)
00835 {
00836 *passwordDataLength=incomingPasswordLength;
00837 return;
00838 }
00839
00840 if (*passwordDataLength > incomingPasswordLength)
00841 *passwordDataLength=incomingPasswordLength;
00842
00843 if (*passwordDataLength>0)
00844 memcpy(passwordData, incomingPassword, *passwordDataLength);
00845 }
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865 bool RakPeer::Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, unsigned connectionSocketIndex, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNetTime timeoutTime )
00866 {
00867
00868 if ( host == 0 || endThreads || connectionSocketIndex>=socketList.Size() )
00869 return false;
00870
00871 connectionSocketIndex=GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex);
00872
00873 if (passwordDataLength>255)
00874 passwordDataLength=255;
00875
00876 if (passwordData==0)
00877 passwordDataLength=0;
00878
00879
00880
00881
00882
00883
00884
00885 if ( NonNumericHostString( host ) )
00886 {
00887 host = ( char* ) SocketLayer::Instance()->DomainNameToIP( host );
00888
00889 if (host==0)
00890 return false;
00891 }
00892
00893
00894
00895
00896
00897
00898 return SendConnectionRequest( host, remotePort, passwordData, passwordDataLength, connectionSocketIndex, 0, sendConnectionAttemptCount, timeBetweenSendConnectionAttemptsMS, timeoutTime);
00899 }
00900
00901
00902
00903 bool RakPeer::ConnectWithSocket(const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, RakNetSmartPtr<RakNetSocket> socket, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNetTime timeoutTime)
00904 {
00905 if ( host == 0 || endThreads || socket.IsNull() )
00906 return false;
00907
00908 if (passwordDataLength>255)
00909 passwordDataLength=255;
00910
00911 if (passwordData==0)
00912 passwordDataLength=0;
00913
00914 if ( NonNumericHostString( host ) )
00915 {
00916 host = ( char* ) SocketLayer::Instance()->DomainNameToIP( host );
00917
00918 if (host==0)
00919 return false;
00920 }
00921
00922 return SendConnectionRequest( host, remotePort, passwordData, passwordDataLength, 0, 0, sendConnectionAttemptCount, timeBetweenSendConnectionAttemptsMS, timeoutTime, socket );
00923
00924 }
00925
00926
00927
00928
00929
00930 void RakPeer::Shutdown( unsigned int blockDuration, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority )
00931 {
00932 unsigned i,j;
00933 bool anyActive;
00934 RakNetTime startWaitingTime;
00935
00936 RakNetTime time;
00937
00938 unsigned short systemListSize = maximumNumberOfPeers;
00939
00940 if ( blockDuration > 0 )
00941 {
00942 for ( i = 0; i < systemListSize; i++ )
00943 {
00944
00945 if (remoteSystemList[i].isActive)
00946 NotifyAndFlagForShutdown(remoteSystemList[i].systemAddress, false, orderingChannel, disconnectionNotificationPriority);
00947 }
00948
00949 time = RakNet::GetTime();
00950 startWaitingTime = time;
00951 while ( time - startWaitingTime < blockDuration )
00952 {
00953 anyActive=false;
00954 for (j=0; j < systemListSize; j++)
00955 {
00956
00957 if (remoteSystemList[j].isActive)
00958 {
00959 anyActive=true;
00960 break;
00961 }
00962 }
00963
00964
00965 if ( anyActive==false )
00966 break;
00967
00968
00969
00970
00971 RakSleep(15);
00972 time = RakNet::GetTime();
00973 }
00974 }
00975
00976 for (i=0; i < messageHandlerList.Size(); i++)
00977 {
00978 messageHandlerList[i]->OnRakPeerShutdown();
00979 }
00980
00981 quitAndDataEvents.SetEvent();
00982
00983 endThreads = true;
00984
00985 for (i=0; i < socketList.Size(); i++)
00986 {
00987 if (SocketLayer::Instance()->SendTo(socketList[i]->s, (const char*) &i,1,"127.0.0.1", socketList[i]->boundAddress.port, socketList[i]->remotePortRakNetWasStartedOn_PS3)!=0)
00988 break;
00989 }
00990 while ( isMainLoopThreadActive )
00991 {
00992 endThreads = true;
00993 RakSleep(15);
00994 }
00995
00996
00997
00998
00999 for ( i = 0; i < systemListSize; i++ )
01000 {
01001
01002 remoteSystemList[ i ].isActive = false;
01003
01004
01005 remoteSystemList[ i ].reliabilityLayer.Reset(false, remoteSystemList[ i ].MTUSize);
01006
01007 remoteSystemList[ i ].rakNetSocket.SetNull();
01008 }
01009
01010
01011
01012
01013 maximumNumberOfPeers = 0;
01014
01015
01016
01017 packetReturnMutex.Lock();
01018 for (unsigned int i=0; i < packetReturnQueue.Size(); i++)
01019 DeallocatePacket(packetReturnQueue[i]);
01020 packetReturnQueue.Clear(__FILE__,__LINE__);
01021 packetReturnMutex.Unlock();
01022 packetAllocationPoolMutex.Lock();
01023 packetAllocationPool.Clear(__FILE__,__LINE__);
01024 packetAllocationPoolMutex.Unlock();
01025
01026 blockOnRPCReply=false;
01027
01028 RakNetTimeMS timeout = RakNet::GetTimeMS()+1000;
01029 while ( isRecvFromLoopThreadActive && RakNet::GetTimeMS()<timeout )
01030 {
01031
01032 for (i=0; i < socketList.Size(); i++)
01033 {
01034 SocketLayer::Instance()->SendTo(socketList[i]->s, (const char*) &i,1,"127.0.0.1", socketList[i]->boundAddress.port, socketList[i]->remotePortRakNetWasStartedOn_PS3);
01035 }
01036
01037 RakSleep(30);
01038 }
01039
01040 if (isRecvFromLoopThreadActive)
01041 {
01042 timeout = RakNet::GetTimeMS()+1000;
01043 while ( isRecvFromLoopThreadActive && RakNet::GetTimeMS()<timeout )
01044 {
01045 RakSleep(30);
01046 }
01047 }
01048
01049 DerefAllSockets();
01050
01051 ClearBufferedCommands();
01052 ClearBufferedPackets();
01053 ClearSocketQueryOutput();
01054 bytesSentPerSecond = bytesReceivedPerSecond = 0;
01055
01056 ClearRequestedConnectionList();
01057
01058
01059
01060 RemoteSystemStruct * temp = remoteSystemList;
01061 remoteSystemList = 0;
01062 RakNet::OP_DELETE_ARRAY(temp, __FILE__, __LINE__);
01063
01064 ClearRemoteSystemLookup();
01065
01066 #ifdef USE_THREADED_SEND
01067 SendToThread::Deref();
01068 #endif
01069
01070 ResetSendReceipt();
01071 }
01072
01073
01074
01075
01076
01077 inline bool RakPeer::IsActive( void ) const
01078 {
01079 return endThreads == false;
01080 }
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091 bool RakPeer::GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const
01092 {
01093 int count, index;
01094 count=0;
01095
01096 if ( remoteSystemList == 0 || endThreads == true )
01097 {
01098 *numberOfSystems = 0;
01099 return false;
01100 }
01101
01102
01103 if ( remoteSystems )
01104 {
01105
01106
01107 for ( count = 0, index = 0; index < maximumNumberOfPeers; ++index )
01108 if ( remoteSystemList[ index ].isActive && remoteSystemList[ index ].connectMode==RemoteSystemStruct::CONNECTED)
01109 {
01110 if ( count < *numberOfSystems )
01111 remoteSystems[ count ] = remoteSystemList[ index ].systemAddress;
01112
01113 ++count;
01114 }
01115 }
01116 else
01117 {
01118
01119
01120 for ( count = 0, index = 0; index < maximumNumberOfPeers; ++index )
01121 if ( remoteSystemList[ index ].isActive && remoteSystemList[ index ].connectMode==RemoteSystemStruct::CONNECTED)
01122 ++count;
01123 }
01124
01125 *numberOfSystems = ( unsigned short ) count;
01126
01127 return true;
01128 }
01129
01130
01131 uint32_t RakPeer::GetNextSendReceipt(void)
01132 {
01133 sendReceiptSerialMutex.Lock();
01134 uint32_t retVal = sendReceiptSerial;
01135 sendReceiptSerialMutex.Unlock();
01136 return retVal;
01137 }
01138
01139 uint32_t RakPeer::IncrementNextSendReceipt(void)
01140 {
01141 sendReceiptSerialMutex.Lock();
01142 uint32_t returned = sendReceiptSerial;
01143 if (++sendReceiptSerial==0)
01144 sendReceiptSerial=1;
01145 sendReceiptSerialMutex.Unlock();
01146 return returned;
01147 }
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168 uint32_t RakPeer::Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceipt )
01169 {
01170 #ifdef _DEBUG
01171 RakAssert( data && length > 0 );
01172 #endif
01173 RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) );
01174 RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) );
01175 RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) );
01176
01177 if ( data == 0 || length < 0 )
01178 return 0;
01179
01180 if ( remoteSystemList == 0 || endThreads == true )
01181 return 0;
01182
01183 if ( broadcast == false && systemIdentifier.IsUndefined())
01184 return 0;
01185
01186 uint32_t usedSendReceipt;
01187 if (forceReceipt!=0)
01188 usedSendReceipt=forceReceipt;
01189 else
01190 usedSendReceipt=IncrementNextSendReceipt();
01191
01192 if (broadcast==false && IsLoopbackAddress(systemIdentifier,true))
01193 {
01194 SendLoopback(data,length);
01195
01196 if (reliability>=UNRELIABLE_WITH_ACK_RECEIPT)
01197 {
01198 char buff[5];
01199 buff[0]=ID_SND_RECEIPT_ACKED;
01200 sendReceiptSerialMutex.Lock();
01201 memcpy(buff+1, &sendReceiptSerial, 4);
01202 sendReceiptSerialMutex.Unlock();
01203 SendLoopback( buff, 5 );
01204 }
01205
01206 return usedSendReceipt;
01207 }
01208
01209 if (broadcast==false && router && IsConnected(systemIdentifier.systemAddress)==false)
01210 {
01211 router->Send(data, BYTES_TO_BITS(length), priority, reliability, orderingChannel, systemIdentifier.systemAddress);
01212 }
01213 else
01214 {
01215 SendBuffered(data, length*8, priority, reliability, orderingChannel, systemIdentifier, broadcast, RemoteSystemStruct::NO_ACTION, usedSendReceipt);
01216 }
01217
01218 return usedSendReceipt;
01219 }
01220
01221 void RakPeer::SendLoopback( const char *data, const int length )
01222 {
01223 if ( data == 0 || length < 0 )
01224 return;
01225
01226 Packet *packet = AllocPacket(length, __FILE__, __LINE__);
01227 memcpy(packet->data, data, length);
01228 packet->systemAddress = GetLoopbackAddress();
01229 packet->guid=myGuid;
01230 PushBackPacket(packet, false);
01231 }
01232
01233 uint32_t RakPeer::Send( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceipt )
01234 {
01235 #ifdef _DEBUG
01236 RakAssert( bitStream->GetNumberOfBytesUsed() > 0 );
01237 #endif
01238
01239 RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) );
01240 RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) );
01241 RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) );
01242
01243 if ( bitStream->GetNumberOfBytesUsed() == 0 )
01244 return 0;
01245
01246 if ( remoteSystemList == 0 || endThreads == true )
01247 return 0;
01248
01249 if ( broadcast == false && systemIdentifier.IsUndefined() )
01250 return 0;
01251
01252 uint32_t usedSendReceipt;
01253 if (forceReceipt!=0)
01254 usedSendReceipt=forceReceipt;
01255 else
01256 usedSendReceipt=IncrementNextSendReceipt();
01257
01258 if (broadcast==false && IsLoopbackAddress(systemIdentifier,true))
01259 {
01260 SendLoopback((const char*) bitStream->GetData(),bitStream->GetNumberOfBytesUsed());
01261 if (reliability>=UNRELIABLE_WITH_ACK_RECEIPT)
01262 {
01263 char buff[5];
01264 buff[0]=ID_SND_RECEIPT_ACKED;
01265 sendReceiptSerialMutex.Lock();
01266 memcpy(buff+1, &sendReceiptSerial,4);
01267 sendReceiptSerialMutex.Unlock();
01268 SendLoopback( buff, 5 );
01269 }
01270 return usedSendReceipt;
01271 }
01272
01273 if (broadcast==false && router && IsConnected(systemIdentifier.systemAddress)==false)
01274 {
01275 router->Send((const char*)bitStream->GetData(), bitStream->GetNumberOfBitsUsed(), priority, reliability, orderingChannel, systemIdentifier.systemAddress);
01276 }
01277 else
01278 {
01279
01280
01281 SendBuffered((const char*)bitStream->GetData(), bitStream->GetNumberOfBitsUsed(), priority, reliability, orderingChannel, systemIdentifier, broadcast, RemoteSystemStruct::NO_ACTION, usedSendReceipt);
01282 }
01283
01284 return usedSendReceipt;
01285 }
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307 uint32_t RakPeer::SendList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceipt )
01308 {
01309 #ifdef _DEBUG
01310 RakAssert( data );
01311 #endif
01312
01313 if ( data == 0 || lengths == 0 )
01314 return 0;
01315
01316 if ( remoteSystemList == 0 || endThreads == true )
01317 return 0;
01318
01319 if (numParameters==0)
01320 return 0;
01321
01322 if (lengths==0)
01323 return 0;
01324
01325 if ( broadcast == false && systemIdentifier.IsUndefined() )
01326 return 0;
01327
01328 uint32_t usedSendReceipt;
01329 if (forceReceipt!=0)
01330 usedSendReceipt=forceReceipt;
01331 else
01332 usedSendReceipt=IncrementNextSendReceipt();
01333
01334 SendBufferedList(data, lengths, numParameters, priority, reliability, orderingChannel, systemIdentifier, broadcast, RemoteSystemStruct::NO_ACTION, usedSendReceipt);
01335
01336 return usedSendReceipt;
01337 }
01338
01339
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349 Packet* RakPeer::Receive( void )
01350 {
01351 Packet *packet = ReceiveIgnoreRPC();
01352 while (packet && (packet->data[ 0 ] == ID_RPC || (packet->length>sizeof(unsigned char)+sizeof(RakNetTime) && packet->data[0]==ID_TIMESTAMP && packet->data[sizeof(unsigned char)+sizeof(RakNetTime)]==ID_RPC)))
01353 {
01354
01355
01356 HandleRPCPacket( ( char* ) packet->data, packet->length, packet->systemAddress );
01357 DeallocatePacket( packet );
01358
01359 packet = ReceiveIgnoreRPC();
01360 }
01361
01362 return packet;
01363 }
01364
01365
01366
01367
01368 #ifdef _MSC_VER
01369 #pragma warning( disable : 4701 ) // warning C4701: local variable <variable name> may be used without having been initialized
01370 #endif
01371 Packet* RakPeer::ReceiveIgnoreRPC( void )
01372 {
01373 if ( !( IsActive() ) )
01374 return 0;
01375
01376 Packet *packet;
01377
01378 PluginReceiveResult pluginResult;
01379
01380 int offset;
01381 unsigned int i;
01382
01383 for (i=0; i < messageHandlerList.Size(); i++)
01384 {
01385 messageHandlerList[i]->Update();
01386 }
01387
01388 do
01389 {
01390 packetReturnMutex.Lock();
01391 if (packetReturnQueue.IsEmpty())
01392 packet=0;
01393 else
01394 packet = packetReturnQueue.Pop();
01395 packetReturnMutex.Unlock();
01396 if (packet==0)
01397 return 0;
01398
01399 unsigned char msgId;
01400 if ( ( packet->length >= sizeof(unsigned char) + sizeof( RakNetTime ) ) &&
01401 ( (unsigned char) packet->data[ 0 ] == ID_TIMESTAMP ) )
01402 {
01403 offset = sizeof(unsigned char);
01404 ShiftIncomingTimestamp( packet->data + offset, packet->systemAddress );
01405 msgId=packet->data[sizeof(unsigned char) + sizeof( RakNetTime )];
01406 }
01407 else
01408 msgId=packet->data[0];
01409
01410 if ( (unsigned char) packet->data[ 0 ] == ID_RPC_REPLY )
01411 {
01412 HandleRPCReplyPacket( ( char* ) packet->data, packet->length, packet->systemAddress );
01413 DeallocatePacket( packet );
01414 packet=0;
01415 }
01416 else
01417 {
01418 for (i=0; i < messageHandlerList.Size(); i++)
01419 {
01420 switch (msgId)
01421 {
01422 case ID_DISCONNECTION_NOTIFICATION:
01423 messageHandlerList[i]->OnClosedConnection(packet->systemAddress, packet->guid, LCR_DISCONNECTION_NOTIFICATION);
01424 break;
01425 case ID_CONNECTION_LOST:
01426 messageHandlerList[i]->OnClosedConnection(packet->systemAddress, packet->guid, LCR_CONNECTION_LOST);
01427 break;
01428 case ID_NEW_INCOMING_CONNECTION:
01429 messageHandlerList[i]->OnNewConnection(packet->systemAddress, packet->guid, true);
01430 break;
01431 case ID_CONNECTION_REQUEST_ACCEPTED:
01432 messageHandlerList[i]->OnNewConnection(packet->systemAddress, packet->guid, false);
01433 break;
01434 case ID_CONNECTION_ATTEMPT_FAILED:
01435 messageHandlerList[i]->OnFailedConnectionAttempt(packet, FCAR_CONNECTION_ATTEMPT_FAILED);
01436 break;
01437 case ID_ALREADY_CONNECTED:
01438 messageHandlerList[i]->OnFailedConnectionAttempt(packet, FCAR_ALREADY_CONNECTED);
01439 break;
01440 case ID_NO_FREE_INCOMING_CONNECTIONS:
01441 messageHandlerList[i]->OnFailedConnectionAttempt(packet, FCAR_NO_FREE_INCOMING_CONNECTIONS);
01442 break;
01443 case ID_RSA_PUBLIC_KEY_MISMATCH:
01444 messageHandlerList[i]->OnFailedConnectionAttempt(packet, FCAR_RSA_PUBLIC_KEY_MISMATCH);
01445 break;
01446 case ID_CONNECTION_BANNED:
01447 messageHandlerList[i]->OnFailedConnectionAttempt(packet, FCAR_CONNECTION_BANNED);
01448 break;
01449 case ID_INVALID_PASSWORD:
01450 messageHandlerList[i]->OnFailedConnectionAttempt(packet, FCAR_INVALID_PASSWORD);
01451 break;
01452 case ID_INCOMPATIBLE_PROTOCOL_VERSION:
01453 messageHandlerList[i]->OnFailedConnectionAttempt(packet, FCAR_INCOMPATIBLE_PROTOCOL);
01454 break;
01455 case ID_IP_RECENTLY_CONNECTED:
01456 messageHandlerList[i]->OnFailedConnectionAttempt(packet, FCAR_IP_RECENTLY_CONNECTED);
01457 break;
01458 }
01459
01460
01461 pluginResult=messageHandlerList[i]->OnReceive(packet);
01462 if (pluginResult==RR_STOP_PROCESSING_AND_DEALLOCATE)
01463 {
01464 DeallocatePacket( packet );
01465 packet=0;
01466 break;
01467 }
01468 else if (pluginResult==RR_STOP_PROCESSING)
01469 {
01470 packet=0;
01471 break;
01472 }
01473 }
01474 }
01475
01476 } while(packet==0);
01477
01478 #ifdef _DEBUG
01479 RakAssert( packet->data );
01480 #endif
01481
01482 return packet;
01483 }
01484
01485
01486
01487
01488
01489 void RakPeer::DeallocatePacket( Packet *packet )
01490 {
01491 if ( packet == 0 )
01492 return;
01493
01494 if (packet->deleteData)
01495 {
01496 rakFree_Ex(packet->data, __FILE__, __LINE__ );
01497 packet->~Packet();
01498 packetAllocationPoolMutex.Lock();
01499 packetAllocationPool.Release(packet,__FILE__,__LINE__);
01500 packetAllocationPoolMutex.Unlock();
01501 }
01502 else
01503 {
01504 rakFree_Ex(packet, __FILE__, __LINE__ );
01505 }
01506 }
01507
01508
01509
01510
01511
01512 unsigned short RakPeer::GetMaximumNumberOfPeers( void ) const
01513 {
01514 return maximumNumberOfPeers;
01515 }
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527 void RakPeer::RegisterAsRemoteProcedureCall( const char* uniqueID, void ( *functionPointer ) ( RPCParameters *rpcParms ) )
01528 {
01529 if ( uniqueID == 0 || uniqueID[ 0 ] == 0 || functionPointer == 0 )
01530 return;
01531
01532 rpcMap.AddIdentifierWithFunction(uniqueID, (void*)functionPointer, false);
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557 }
01558
01559 void RakPeer::RegisterClassMemberRPC( const char* uniqueID, void *functionPointer )
01560 {
01561 if ( uniqueID == 0 || uniqueID[ 0 ] == 0 || functionPointer == 0 )
01562 return;
01563
01564 rpcMap.AddIdentifierWithFunction(uniqueID, functionPointer, true);
01565 }
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576 void RakPeer::UnregisterAsRemoteProcedureCall( const char* uniqueID )
01577 {
01578 if ( uniqueID == 0 || uniqueID[ 0 ] == 0 )
01579 return;
01580
01581
01582 #ifdef _DEBUG
01583 RakAssert(IsActive()==false);
01584
01585 #endif
01586
01587 rpcMap.RemoveNode(uniqueID);
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609
01610
01611 }
01612
01613
01614 void RakPeer::SetNetworkIDManager( NetworkIDManager *manager )
01615 {
01616 networkIDManager=manager;
01617 if (manager)
01618 manager->SetGuid(myGuid);
01619 }
01620
01621 NetworkIDManager *RakPeer::GetNetworkIDManager(void) const
01622 {
01623 return networkIDManager;
01624 }
01625
01626 bool RakPeer::RPC( const char* uniqueID, const char *data, BitSize_t bitLength, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RakNetTime *includedTimestamp, NetworkID networkID, RakNet::BitStream *replyFromTarget )
01627 {
01628 #ifdef _DEBUG
01629 RakAssert( uniqueID && uniqueID[ 0 ] );
01630 RakAssert(orderingChannel >=0 && orderingChannel < 32);
01631 #endif
01632
01633 if ( uniqueID == 0 )
01634 return false;
01635
01636 if ( strlen( uniqueID ) > 256 )
01637 {
01638 #ifdef _DEBUG
01639 RakAssert( 0 );
01640 #endif
01641 return false;
01642 }
01643 if (replyFromTarget && blockOnRPCReply==true)
01644 {
01645
01646
01647
01648
01649 return false;
01650 }
01651
01652 unsigned *sendList;
01653
01654 unsigned sendListSize;
01655
01656
01657 RPCIndex rpcIndex;
01658
01659
01660 unsigned remoteSystemIndex, sendListIndex;
01661
01662
01663
01664 sendListSize=0;
01665 bool routeSend;
01666 routeSend=false;
01667
01668 if (broadcast==false)
01669 {
01670 #if !defined(_XBOX) && !defined(X360)
01671 sendList=(unsigned *)alloca(sizeof(unsigned));
01672 #else
01673 sendList = (unsigned int*) rakMalloc_Ex(sizeof(unsigned), __FILE__, __LINE__);
01674 #endif
01675 remoteSystemIndex=GetIndexFromSystemAddress( systemIdentifier.systemAddress, false );
01676 if (remoteSystemIndex!=(unsigned)-1 &&
01677 remoteSystemList[remoteSystemIndex].isActive &&
01678 remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ASAP &&
01679 remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY &&
01680 remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ON_NO_ACK)
01681 {
01682 sendList[0]=remoteSystemIndex;
01683 sendListSize=1;
01684 }
01685 else if (router)
01686 routeSend=true;
01687 }
01688 else
01689 {
01690 #if !defined(_XBOX) && !defined(X360)
01691 sendList=(unsigned *)alloca(sizeof(unsigned)*maximumNumberOfPeers);
01692 #else
01693 sendList = (unsigned int*) rakMalloc_Ex(sizeof(unsigned)*maximumNumberOfPeers, __FILE__, __LINE__);
01694 #endif
01695
01696 for ( remoteSystemIndex = 0; remoteSystemIndex < maximumNumberOfPeers; remoteSystemIndex++ )
01697 {
01698 if ( remoteSystemList[ remoteSystemIndex ].isActive && remoteSystemList[ remoteSystemIndex ].systemAddress != systemIdentifier.systemAddress )
01699 sendList[sendListSize++]=remoteSystemIndex;
01700 }
01701 }
01702
01703 if (sendListSize==0 && routeSend==false)
01704 {
01705 #if defined(_XBOX) || defined(X360)
01706
01707 #endif
01708
01709 return false;
01710 }
01711 if (routeSend)
01712 sendListSize=1;
01713
01714 RakNet::BitStream outgoingBitStream;
01715
01716 for (sendListIndex=0; sendListIndex < (unsigned)sendListSize; sendListIndex++)
01717 {
01718 outgoingBitStream.ResetWritePointer();
01719
01720 if (includedTimestamp)
01721 {
01722 outgoingBitStream.Write((MessageID)ID_TIMESTAMP);
01723 outgoingBitStream.Write(*includedTimestamp);
01724 }
01725 outgoingBitStream.Write((MessageID)ID_RPC);
01726 if (routeSend)
01727 rpcIndex=UNDEFINED_RPC_INDEX;
01728 else
01729 rpcIndex=remoteSystemList[sendList[sendListIndex]].rpcMap.GetIndexFromFunctionName(uniqueID);
01730 if (rpcIndex!=UNDEFINED_RPC_INDEX)
01731 {
01732
01733 outgoingBitStream.Write(false);
01734 outgoingBitStream.WriteCompressed(rpcIndex);
01735 }
01736 else
01737 {
01738
01739 outgoingBitStream.Write(true);
01740 stringCompressor->EncodeString(uniqueID, 256, &outgoingBitStream);
01741 }
01742 outgoingBitStream.Write((bool) ((replyFromTarget!=0)==true));
01743 outgoingBitStream.WriteCompressed( bitLength );
01744 if (networkID==UNASSIGNED_NETWORK_ID)
01745 {
01746
01747 outgoingBitStream.Write(false);
01748 }
01749 else
01750 {
01751
01752 outgoingBitStream.Write(true);
01753 outgoingBitStream.Write(networkID);
01754 }
01755
01756
01757 if ( bitLength > 0 )
01758 outgoingBitStream.WriteBits( (const unsigned char *) data, bitLength, false );
01759 else
01760 outgoingBitStream.WriteCompressed( ( unsigned int ) 0 );
01761
01762 if (routeSend)
01763 router->Send((const char*)outgoingBitStream.GetData(), outgoingBitStream.GetNumberOfBitsUsed(), priority,reliability,orderingChannel,systemIdentifier.systemAddress);
01764 else
01765 Send(&outgoingBitStream, priority, reliability, orderingChannel, remoteSystemList[sendList[sendListIndex]].systemAddress, false);
01766 }
01767
01768 #if defined(_XBOX) || defined(X360)
01769
01770 #endif
01771
01772 if (replyFromTarget)
01773 {
01774 blockOnRPCReply=true;
01775
01776
01777
01778
01779 replyFromTargetBS=replyFromTarget;
01780 replyFromTargetPlayer=systemIdentifier.systemAddress;
01781 replyFromTargetBroadcast=broadcast;
01782 }
01783
01784
01785 if (replyFromTarget)
01786
01787 {
01788
01789 RakNetTime stopWaitingTime=RakNet::GetTime()+30000;
01790
01791
01792 if (reliability==UNRELIABLE)
01793 if (systemIdentifier.systemAddress==UNASSIGNED_SYSTEM_ADDRESS)
01794 stopWaitingTime=RakNet::GetTime()+1500;
01795 else
01796 stopWaitingTime=RakNet::GetTime()+GetAveragePing(systemIdentifier.systemAddress)*3;
01797
01798
01799
01800 while (blockOnRPCReply &&
01801 ((
01802 reliability==RELIABLE ||
01803 reliability==RELIABLE_ORDERED ||
01804 reliability==RELIABLE_SEQUENCED ||
01805 reliability==RELIABLE_WITH_ACK_RECEIPT ||
01806 reliability==RELIABLE_ORDERED_WITH_ACK_RECEIPT
01807
01808
01809 ) ||
01810 RakNet::GetTime() < stopWaitingTime))
01811 {
01812
01813 RakSleep(30);
01814
01815 if (routeSend==false && ValidSendTarget(systemIdentifier.systemAddress, broadcast)==false)
01816 return false;
01817
01818 unsigned i;
01819 i=0;
01820
01821
01822 packetReturnMutex.Lock();
01823 while (i < packetReturnQueue.Size())
01824 {
01825 if ((unsigned char) packetReturnQueue[i]->data[ 0 ] == ID_RPC_REPLY )
01826 {
01827 HandleRPCReplyPacket( ( char* ) packetReturnQueue[i]->data, packetReturnQueue[i]->length, packetReturnQueue[i]->systemAddress );
01828 DeallocatePacket( packetReturnQueue[i] );
01829 packetReturnQueue.RemoveAtIndex(i);
01830 }
01831 else
01832 i++;
01833 }
01834 packetReturnMutex.Unlock();
01835
01836
01837
01838
01839
01840
01841
01842
01843
01844
01845
01846
01847
01848
01849
01850
01851
01852
01853
01854 PushBackPacket(ReceiveIgnoreRPC(), false);
01855
01856
01857
01858
01859
01860
01861
01862
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882
01883
01884
01885
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911 }
01912
01913 blockOnRPCReply=false;
01914 }
01915
01916 return true;
01917 }
01918
01919
01920 #ifdef _MSC_VER
01921 #pragma warning( disable : 4701 ) // warning C4701: local variable <variable name> may be used without having been initialized
01922 #endif
01923 bool RakPeer::RPC( const char* uniqueID, const RakNet::BitStream *bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RakNetTime *includedTimestamp, NetworkID networkID, RakNet::BitStream *replyFromTarget )
01924 {
01925 if (bitStream)
01926 return RPC(uniqueID, (const char*) bitStream->GetData(), bitStream->GetNumberOfBitsUsed(), priority, reliability, orderingChannel, systemIdentifier.systemAddress, broadcast, includedTimestamp, networkID, replyFromTarget);
01927 else
01928 return RPC(uniqueID, 0,0, priority, reliability, orderingChannel, systemIdentifier.systemAddress, broadcast, includedTimestamp, networkID, replyFromTarget);
01929 }
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940 void RakPeer::CloseConnection( const SystemAddress target, bool sendDisconnectionNotification, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority )
01941 {
01942
01943
01944 unsigned j;
01945 for (j=0; j < messageHandlerList.Size(); j++)
01946 {
01947 messageHandlerList[j]->OnClosedConnection(target, GetGuidFromSystemAddress(target), LCR_CLOSED_BY_USER);
01948 }
01949
01950 CloseConnectionInternal(target, sendDisconnectionNotification, false, orderingChannel, disconnectionNotificationPriority);
01951
01952
01953 if (sendDisconnectionNotification==false && IsConnected(target,false,false))
01954 {
01955 Packet *packet=AllocPacket(sizeof( char ), __FILE__, __LINE__);
01956 packet->data[ 0 ] = ID_CONNECTION_LOST;
01957 packet->guid = GetGuidFromSystemAddress(target);
01958 packet->systemAddress = target;
01959 packet->systemAddress.systemIndex = (SystemIndex) GetIndexFromSystemAddress(target,false);
01960 packet->guid.systemIndex=packet->systemAddress.systemIndex;
01961 AddPacketToProducer(packet);
01962 }
01963 }
01964
01965
01966
01967
01968
01969
01970 void RakPeer::CancelConnectionAttempt( const SystemAddress target )
01971 {
01972 unsigned int i;
01973
01974
01975 i=0;
01976 requestedConnectionQueueMutex.Lock();
01977 while (i < requestedConnectionQueue.Size())
01978 {
01979 if (requestedConnectionQueue[i]->systemAddress==target)
01980 {
01981 RakNet::OP_DELETE(requestedConnectionQueue[i], __FILE__, __LINE__ );
01982 requestedConnectionQueue.RemoveAtIndex(i);
01983 break;
01984 }
01985 else
01986 i++;
01987 }
01988 requestedConnectionQueueMutex.Unlock();
01989
01990 }
01991
01992 bool RakPeer::IsConnectionAttemptPending( const SystemAddress systemAddress )
01993 {
01994 unsigned int i=0;
01995 requestedConnectionQueueMutex.Lock();
01996 for (; i < requestedConnectionQueue.Size(); i++)
01997 {
01998 if (requestedConnectionQueue[i]->systemAddress==systemAddress)
01999 {
02000 requestedConnectionQueueMutex.Unlock();
02001 return true;
02002 }
02003 }
02004 requestedConnectionQueueMutex.Unlock();
02005
02006 int index = GetIndexFromSystemAddress(systemAddress, false);
02007 return index!=-1 && remoteSystemList[index].isActive &&
02008 (((remoteSystemList[index].connectMode==RemoteSystemStruct::REQUESTED_CONNECTION ||
02009 remoteSystemList[index].connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST ||
02010 remoteSystemList[index].connectMode==RemoteSystemStruct::UNVERIFIED_SENDER ||
02011 remoteSystemList[index].connectMode==RemoteSystemStruct::SET_ENCRYPTION_ON_MULTIPLE_16_BYTE_PACKET))
02012 )
02013 ;
02014 }
02015
02016
02017
02018
02019
02020
02021 bool RakPeer::IsConnected( const AddressOrGUID systemIdentifier, bool includeInProgress, bool includeDisconnecting )
02022 {
02023 if (includeInProgress && systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
02024 {
02025 unsigned int i=0;
02026 requestedConnectionQueueMutex.Lock();
02027 for (; i < requestedConnectionQueue.Size(); i++)
02028 {
02029 if (requestedConnectionQueue[i]->systemAddress==systemIdentifier.systemAddress)
02030 {
02031 requestedConnectionQueueMutex.Unlock();
02032 return true;
02033 }
02034 }
02035 requestedConnectionQueueMutex.Unlock();
02036 }
02037
02038
02039 int index;
02040 if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
02041 {
02042 if (IsLoopbackAddress(systemIdentifier.systemAddress,true))
02043 return true;
02044 index = GetIndexFromSystemAddress(systemIdentifier.systemAddress, false);
02045 return index!=-1 && remoteSystemList[index].isActive &&
02046 (((includeInProgress && (remoteSystemList[index].connectMode==RemoteSystemStruct::REQUESTED_CONNECTION ||
02047 remoteSystemList[index].connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST ||
02048 remoteSystemList[index].connectMode==RemoteSystemStruct::UNVERIFIED_SENDER ||
02049 remoteSystemList[index].connectMode==RemoteSystemStruct::SET_ENCRYPTION_ON_MULTIPLE_16_BYTE_PACKET))
02050 ||
02051 (includeDisconnecting && (
02052 remoteSystemList[index].connectMode==RemoteSystemStruct::DISCONNECT_ASAP ||
02053 remoteSystemList[index].connectMode==RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY ||
02054 remoteSystemList[index].connectMode==RemoteSystemStruct::DISCONNECT_ON_NO_ACK))
02055 ||
02056 remoteSystemList[index].connectMode==RemoteSystemStruct::CONNECTED))
02057 ;
02058 }
02059 else
02060 {
02061 index = GetIndexFromGuid(systemIdentifier.rakNetGuid);
02062 return index!=-1 && remoteSystemList[index].isActive &&
02063 (((includeInProgress && (remoteSystemList[index].connectMode==RemoteSystemStruct::REQUESTED_CONNECTION ||
02064 remoteSystemList[index].connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST ||
02065 remoteSystemList[index].connectMode==RemoteSystemStruct::UNVERIFIED_SENDER ||
02066 remoteSystemList[index].connectMode==RemoteSystemStruct::SET_ENCRYPTION_ON_MULTIPLE_16_BYTE_PACKET))
02067 ||
02068 (includeDisconnecting && (
02069 remoteSystemList[index].connectMode==RemoteSystemStruct::DISCONNECT_ASAP ||
02070 remoteSystemList[index].connectMode==RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY ||
02071 remoteSystemList[index].connectMode==RemoteSystemStruct::DISCONNECT_ON_NO_ACK))
02072 ||
02073 remoteSystemList[index].connectMode==RemoteSystemStruct::CONNECTED))
02074 ;
02075 }
02076 }
02077
02078
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088 int RakPeer::GetIndexFromSystemAddress( const SystemAddress systemAddress ) const
02089 {
02090 return GetIndexFromSystemAddress(systemAddress, false);
02091 }
02092
02093
02094
02095
02096
02097
02098
02099
02100
02101
02102
02103 SystemAddress RakPeer::GetSystemAddressFromIndex( int index )
02104 {
02105
02106
02107 if ( index >= 0 && index < maximumNumberOfPeers )
02108 if (remoteSystemList[index].isActive && remoteSystemList[ index ].connectMode==RakPeer::RemoteSystemStruct::CONNECTED)
02109 return remoteSystemList[ index ].systemAddress;
02110
02111 return UNASSIGNED_SYSTEM_ADDRESS;
02112 }
02113
02114
02115
02116
02117
02118
02119 RakNetGUID RakPeer::GetGUIDFromIndex( int index )
02120 {
02121
02122
02123 if ( index >= 0 && index < maximumNumberOfPeers )
02124 if (remoteSystemList[index].isActive && remoteSystemList[ index ].connectMode==RakPeer::RemoteSystemStruct::CONNECTED)
02125 return remoteSystemList[ index ].guid;
02126
02127 return UNASSIGNED_RAKNET_GUID;
02128 }
02129
02130
02131
02132
02133
02134
02135
02136 void RakPeer::GetSystemList(DataStructures::List<SystemAddress> &addresses, DataStructures::List<RakNetGUID> &guids)
02137 {
02138 addresses.Clear(false, __FILE__, __LINE__);
02139 guids.Clear(false, __FILE__, __LINE__);
02140 int index;
02141 for (index=0; index < maximumNumberOfPeers; index++)
02142 {
02143
02144 if (remoteSystemList[index].isActive && remoteSystemList[ index ].connectMode==RakPeer::RemoteSystemStruct::CONNECTED)
02145 {
02146 addresses.Push(remoteSystemList[index].systemAddress, __FILE__, __LINE__ );
02147 guids.Push(remoteSystemList[index].guid, __FILE__, __LINE__ );
02148 }
02149 }
02150 }
02151
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161 void RakPeer::AddToBanList( const char *IP, RakNetTime milliseconds )
02162 {
02163 unsigned index;
02164 RakNetTime time = RakNet::GetTime();
02165
02166 if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 )
02167 return ;
02168
02169
02170 index = 0;
02171
02172 banListMutex.Lock();
02173
02174 for ( ; index < banList.Size(); index++ )
02175 {
02176 if ( strcmp( IP, banList[ index ]->IP ) == 0 )
02177 {
02178
02179 if (milliseconds==0)
02180 banList[ index ]->timeout=0;
02181 else
02182 banList[ index ]->timeout=time+milliseconds;
02183 banListMutex.Unlock();
02184 return;
02185 }
02186 }
02187
02188 banListMutex.Unlock();
02189
02190 BanStruct *banStruct = RakNet::OP_NEW<BanStruct>( __FILE__, __LINE__ );
02191 banStruct->IP = (char*) rakMalloc_Ex( 16, __FILE__, __LINE__ );
02192 if (milliseconds==0)
02193 banStruct->timeout=0;
02194 else
02195 banStruct->timeout=time+milliseconds;
02196 strcpy( banStruct->IP, IP );
02197 banListMutex.Lock();
02198 banList.Insert( banStruct, __FILE__, __LINE__ );
02199 banListMutex.Unlock();
02200 }
02201
02202
02203
02204
02205
02206
02207
02208
02209
02210 void RakPeer::RemoveFromBanList( const char *IP )
02211 {
02212 unsigned index;
02213 BanStruct *temp;
02214
02215 if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 )
02216 return ;
02217
02218 index = 0;
02219 temp=0;
02220
02221 banListMutex.Lock();
02222
02223 for ( ; index < banList.Size(); index++ )
02224 {
02225 if ( strcmp( IP, banList[ index ]->IP ) == 0 )
02226 {
02227 temp = banList[ index ];
02228 banList[ index ] = banList[ banList.Size() - 1 ];
02229 banList.RemoveAtIndex( banList.Size() - 1 );
02230 break;
02231 }
02232 }
02233
02234 banListMutex.Unlock();
02235
02236 if (temp)
02237 {
02238 rakFree_Ex(temp->IP, __FILE__, __LINE__ );
02239 RakNet::OP_DELETE(temp, __FILE__, __LINE__);
02240 }
02241
02242 }
02243
02244
02245
02246
02247
02248 void RakPeer::ClearBanList( void )
02249 {
02250 unsigned index;
02251 index = 0;
02252 banListMutex.Lock();
02253
02254 for ( ; index < banList.Size(); index++ )
02255 {
02256 rakFree_Ex(banList[ index ]->IP, __FILE__, __LINE__ );
02257 RakNet::OP_DELETE(banList[ index ], __FILE__, __LINE__);
02258 }
02259
02260 banList.Clear(false, __FILE__, __LINE__);
02261
02262 banListMutex.Unlock();
02263 }
02264
02265 void RakPeer::SetLimitIPConnectionFrequency(bool b)
02266 {
02267 limitConnectionFrequencyFromTheSameIP=b;
02268 }
02269
02270
02271
02272
02273
02274
02275
02276
02277
02278
02279
02280
02281 bool RakPeer::IsBanned( const char *IP )
02282 {
02283 unsigned banListIndex, characterIndex;
02284 RakNetTime time;
02285 BanStruct *temp;
02286
02287 if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 )
02288 return false;
02289
02290 banListIndex = 0;
02291
02292 if ( banList.Size() == 0 )
02293 return false;
02294
02295 time = RakNet::GetTime();
02296
02297 banListMutex.Lock();
02298
02299 while ( banListIndex < banList.Size() )
02300 {
02301 if (banList[ banListIndex ]->timeout>0 && banList[ banListIndex ]->timeout<time)
02302 {
02303
02304 temp = banList[ banListIndex ];
02305 banList[ banListIndex ] = banList[ banList.Size() - 1 ];
02306 banList.RemoveAtIndex( banList.Size() - 1 );
02307 rakFree_Ex(temp->IP, __FILE__, __LINE__ );
02308 RakNet::OP_DELETE(temp, __FILE__, __LINE__);
02309 }
02310 else
02311 {
02312 characterIndex = 0;
02313
02314 #ifdef _MSC_VER
02315 #pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant
02316 #endif
02317 while ( true )
02318 {
02319 if ( banList[ banListIndex ]->IP[ characterIndex ] == IP[ characterIndex ] )
02320 {
02321
02322
02323 if ( IP[ characterIndex ] == 0 )
02324 {
02325 banListMutex.Unlock();
02326
02327
02328 return true;
02329 }
02330
02331 characterIndex++;
02332 }
02333
02334 else
02335 {
02336 if ( banList[ banListIndex ]->IP[ characterIndex ] == 0 || IP[ characterIndex ] == 0 )
02337 {
02338
02339 break;
02340 }
02341
02342
02343 if ( banList[ banListIndex ]->IP[ characterIndex ] == '*' )
02344 {
02345 banListMutex.Unlock();
02346
02347
02348 return true;
02349 }
02350
02351
02352 break;
02353 }
02354 }
02355
02356 banListIndex++;
02357 }
02358 }
02359
02360 banListMutex.Unlock();
02361
02362
02363 return false;
02364 }
02365
02366
02367
02368
02369
02370
02371
02372
02373 void RakPeer::Ping( const SystemAddress target )
02374 {
02375 PingInternal(target, false, UNRELIABLE);
02376 }
02377
02378
02379
02380
02381
02382
02383
02384
02385
02386
02387
02388
02389
02390 bool RakPeer::Ping( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections, unsigned connectionSocketIndex )
02391 {
02392 if ( host == 0 )
02393 return false;
02394
02395
02396 RakAssert(connectionSocketIndex < socketList.Size());
02397
02398
02399
02400
02401 if ( NonNumericHostString( host ) )
02402 {
02403 host = ( char* ) SocketLayer::Instance()->DomainNameToIP( host );
02404 if (host==0)
02405 return false;
02406 }
02407
02408 SystemAddress systemAddress;
02409 systemAddress.SetBinaryAddress(host);
02410 systemAddress.port=remotePort;
02411
02412 RakNet::BitStream bitStream( sizeof(unsigned char) + sizeof(RakNetTime) );
02413 if ( onlyReplyOnAcceptingConnections )
02414 bitStream.Write((MessageID)ID_PING_OPEN_CONNECTIONS);
02415 else
02416 bitStream.Write((MessageID)ID_PING);
02417
02418 bitStream.Write(RakNet::GetTime());
02419
02420 bitStream.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
02421
02422 unsigned i;
02423 for (i=0; i < messageHandlerList.Size(); i++)
02424 messageHandlerList[i]->OnDirectSocketSend((const char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), systemAddress);
02425
02426 unsigned int realIndex = GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex);
02427 SocketLayer::Instance()->SendTo( socketList[realIndex]->s, (const char*)bitStream.GetData(), (int) bitStream.GetNumberOfBytesUsed(), ( char* ) host, remotePort, socketList[realIndex]->remotePortRakNetWasStartedOn_PS3 );
02428
02429 return true;
02430 }
02431
02432
02433
02434
02435
02436
02437
02438
02439 int RakPeer::GetAveragePing( const AddressOrGUID systemIdentifier )
02440 {
02441 int sum, quantity;
02442 RemoteSystemStruct *remoteSystem = GetRemoteSystem( systemIdentifier, false, false );
02443
02444 if ( remoteSystem == 0 )
02445 return -1;
02446
02447 for ( sum = 0, quantity = 0; quantity < PING_TIMES_ARRAY_SIZE; quantity++ )
02448 {
02449 if ( remoteSystem->pingAndClockDifferential[ quantity ].pingTime == 65535 )
02450 break;
02451 else
02452 sum += remoteSystem->pingAndClockDifferential[ quantity ].pingTime;
02453 }
02454
02455 if ( quantity > 0 )
02456 return sum / quantity;
02457 else
02458 return -1;
02459 }
02460
02461
02462
02463
02464
02465
02466
02467
02468 int RakPeer::GetLastPing( const AddressOrGUID systemIdentifier ) const
02469 {
02470 RemoteSystemStruct * remoteSystem = GetRemoteSystem( systemIdentifier, false, false );
02471
02472 if ( remoteSystem == 0 )
02473 return -1;
02474
02475
02476
02477 if ( remoteSystem->pingAndClockDifferentialWriteIndex == 0 )
02478 return remoteSystem->pingAndClockDifferential[ PING_TIMES_ARRAY_SIZE - 1 ].pingTime;
02479 else
02480 return remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex - 1 ].pingTime;
02481 }
02482
02483
02484
02485
02486
02487
02488
02489
02490 int RakPeer::GetLowestPing( const AddressOrGUID systemIdentifier ) const
02491 {
02492 RemoteSystemStruct * remoteSystem = GetRemoteSystem( systemIdentifier, false, false );
02493
02494 if ( remoteSystem == 0 )
02495 return -1;
02496
02497 return remoteSystem->lowestPing;
02498 }
02499
02500
02501
02502
02503
02504
02505
02506
02507
02508 void RakPeer::SetOccasionalPing( bool doPing )
02509 {
02510 occasionalPing = doPing;
02511 }
02512
02513
02514
02515
02516
02517
02518
02519
02520 void RakPeer::SetOfflinePingResponse( const char *data, const unsigned int length )
02521 {
02522 RakAssert(length < 400);
02523
02524 rakPeerMutexes[ offlinePingResponse_Mutex ].Lock();
02525 offlinePingResponse.Reset();
02526
02527 if ( data && length > 0 )
02528 offlinePingResponse.Write( data, length );
02529
02530 rakPeerMutexes[ offlinePingResponse_Mutex ].Unlock();
02531 }
02532
02533
02534
02535
02536
02537
02538
02539 void RakPeer::GetOfflinePingResponse( char **data, unsigned int *length )
02540 {
02541 rakPeerMutexes[ offlinePingResponse_Mutex ].Lock();
02542 *data = (char*) offlinePingResponse.GetData();
02543 *length = (int) offlinePingResponse.GetNumberOfBytesUsed();
02544 rakPeerMutexes[ offlinePingResponse_Mutex ].Unlock();
02545 }
02546
02547
02548
02549
02550
02551
02552 SystemAddress RakPeer::GetInternalID( const SystemAddress systemAddress, const int index ) const
02553 {
02554 if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS)
02555 {
02556 return mySystemAddress[index];
02557 }
02558 else
02559 {
02560 #if !defined(_XBOX) && !defined(X360)
02561
02562 RemoteSystemStruct * remoteSystem = GetRemoteSystemFromSystemAddress( systemAddress, false, true );
02563 if (remoteSystem==0)
02564 return UNASSIGNED_SYSTEM_ADDRESS;
02565
02566 return remoteSystem->theirInternalSystemAddress[index];
02567
02568
02569
02570
02571
02572
02573
02574
02575
02576 #else
02577 return UNASSIGNED_SYSTEM_ADDRESS;
02578 #endif
02579 }
02580 }
02581
02582
02583
02584
02585
02586
02587
02588
02589
02590
02591 SystemAddress RakPeer::GetExternalID( const SystemAddress target ) const
02592 {
02593 unsigned i;
02594 SystemAddress inactiveExternalId;
02595
02596 inactiveExternalId=UNASSIGNED_SYSTEM_ADDRESS;
02597
02598 if (target==UNASSIGNED_SYSTEM_ADDRESS)
02599 return firstExternalID;
02600
02601
02602 for ( i = 0; i < maximumNumberOfPeers; i++ )
02603 {
02604 if (remoteSystemList[ i ].systemAddress == target )
02605 {
02606 if ( remoteSystemList[ i ].isActive )
02607 return remoteSystemList[ i ].myExternalSystemAddress;
02608 else if (remoteSystemList[ i ].myExternalSystemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
02609 inactiveExternalId=remoteSystemList[ i ].myExternalSystemAddress;
02610 }
02611 }
02612
02613 return inactiveExternalId;
02614 }
02615
02616
02617
02618 const RakNetGUID& RakPeer::GetGuidFromSystemAddress( const SystemAddress input ) const
02619 {
02620 if (input==UNASSIGNED_SYSTEM_ADDRESS)
02621 return myGuid;
02622
02623 if (input.systemIndex!=(SystemIndex)-1 && input.systemIndex<maximumNumberOfPeers && remoteSystemList[ input.systemIndex ].systemAddress == input)
02624 return remoteSystemList[ input.systemIndex ].guid;
02625
02626 unsigned int i;
02627 for ( i = 0; i < maximumNumberOfPeers; i++ )
02628 {
02629 if (remoteSystemList[ i ].systemAddress == input )
02630 {
02631 return remoteSystemList[ i ].guid;
02632 }
02633 }
02634
02635 return UNASSIGNED_RAKNET_GUID;
02636 }
02637
02638
02639
02640 unsigned int RakPeer::GetSystemIndexFromGuid( const RakNetGUID input ) const
02641 {
02642 if (input==UNASSIGNED_RAKNET_GUID)
02643 return (unsigned int) -1;
02644
02645 if (input==myGuid)
02646 return (unsigned int) -1;
02647
02648 if (input.systemIndex!=(SystemIndex)-1 && input.systemIndex<maximumNumberOfPeers && remoteSystemList[ input.systemIndex ].guid == input)
02649 return input.systemIndex;
02650
02651 unsigned int i;
02652 for ( i = 0; i < maximumNumberOfPeers; i++ )
02653 {
02654 if (remoteSystemList[ i ].guid == input )
02655 {
02656 return i;
02657 }
02658 }
02659
02660 return (unsigned int) -1;
02661 }
02662
02663
02664
02665 SystemAddress RakPeer::GetSystemAddressFromGuid( const RakNetGUID input ) const
02666 {
02667 if (input==UNASSIGNED_RAKNET_GUID)
02668 return UNASSIGNED_SYSTEM_ADDRESS;
02669
02670 if (input==myGuid)
02671 return GetInternalID(UNASSIGNED_SYSTEM_ADDRESS);
02672
02673 if (input.systemIndex!=(SystemIndex)-1 && input.systemIndex<maximumNumberOfPeers && remoteSystemList[ input.systemIndex ].guid == input)
02674 return remoteSystemList[ input.systemIndex ].systemAddress;
02675
02676 unsigned int i;
02677 for ( i = 0; i < maximumNumberOfPeers; i++ )
02678 {
02679 if (remoteSystemList[ i ].guid == input )
02680 {
02681 return remoteSystemList[ i ].systemAddress;
02682 }
02683 }
02684
02685 return UNASSIGNED_SYSTEM_ADDRESS;
02686 }
02687
02688
02689
02690
02691
02692 void RakPeer::SetTimeoutTime( RakNetTime timeMS, const SystemAddress target )
02693 {
02694 if (target==UNASSIGNED_SYSTEM_ADDRESS)
02695 {
02696 defaultTimeoutTime=timeMS;
02697
02698 unsigned i;
02699 for ( i = 0; i < maximumNumberOfPeers; i++ )
02700 {
02701 if (remoteSystemList[ i ].isActive)
02702 {
02703 if ( remoteSystemList[ i ].isActive )
02704 remoteSystemList[ i ].reliabilityLayer.SetTimeoutTime(timeMS);
02705 }
02706 }
02707 }
02708 else
02709 {
02710 RemoteSystemStruct * remoteSystem = GetRemoteSystemFromSystemAddress( target, false, true );
02711
02712 if ( remoteSystem != 0 )
02713 remoteSystem->reliabilityLayer.SetTimeoutTime(timeMS);
02714 }
02715 }
02716
02717
02718
02719 RakNetTime RakPeer::GetTimeoutTime( const SystemAddress target )
02720 {
02721 if (target==UNASSIGNED_SYSTEM_ADDRESS)
02722 {
02723 return defaultTimeoutTime;
02724 }
02725 else
02726 {
02727 RemoteSystemStruct * remoteSystem = GetRemoteSystemFromSystemAddress( target, false, true );
02728
02729 if ( remoteSystem != 0 )
02730 remoteSystem->reliabilityLayer.GetTimeoutTime();
02731 }
02732 return defaultTimeoutTime;
02733 }
02734
02735
02736
02737
02738
02739
02740
02741
02742
02743
02744
02745
02746
02747
02748
02749
02750
02751
02752
02753
02754
02755
02756
02757
02758
02759
02760
02761
02762
02763
02764
02765
02766
02767
02768
02769
02770
02771
02772
02773
02774
02775
02776
02777
02778
02779
02780
02781
02782
02783
02784
02785
02786
02787
02788
02789
02790
02791
02792
02793
02794
02795
02796
02797 int RakPeer::GetMTUSize( const SystemAddress target ) const
02798 {
02799 if (target!=UNASSIGNED_SYSTEM_ADDRESS)
02800 {
02801 RemoteSystemStruct *rss=GetRemoteSystemFromSystemAddress(target, false, true);
02802 if (rss)
02803 return rss->MTUSize;
02804 }
02805 return defaultMTUSize;
02806 }
02807
02808
02809
02810
02811 unsigned int RakPeer::GetNumberOfAddresses( void )
02812 {
02813 #if !defined(_XBOX) && !defined(X360)
02814 int i = 0;
02815
02816 while ( ipList[ i ][ 0 ] )
02817 i++;
02818
02819 return i;
02820 #else
02821 RakAssert(0);
02822 return 0;
02823 #endif
02824 }
02825
02826
02827
02828
02829
02830
02831 const char* RakPeer::GetLocalIP( unsigned int index )
02832 {
02833 if (IsActive()==false)
02834 {
02835
02836 #if !defined(_XBOX) && !defined(X360)
02837 memset( ipList, 0, sizeof( char ) * 16 * MAXIMUM_NUMBER_OF_INTERNAL_IDS );
02838 SocketLayer::Instance()->GetMyIP( ipList,binaryAddresses );
02839 #endif
02840 }
02841
02842 #if !defined(_XBOX) && !defined(X360)
02843 return ipList[ index ];
02844 #else
02845 RakAssert(0);
02846 return 0;
02847 #endif
02848 }
02849
02850
02851
02852
02853
02854
02855 bool RakPeer::IsLocalIP( const char *ip )
02856 {
02857 if (ip==0 || ip[0]==0)
02858 return false;
02859
02860 #if !defined(_XBOX) && !defined(X360)
02861 if (strcmp(ip, "127.0.0.1")==0)
02862 return true;
02863
02864 int num = GetNumberOfAddresses();
02865 int i;
02866 for (i=0; i < num; i++)
02867 {
02868 if (strcmp(ip, GetLocalIP(i))==0)
02869 return true;
02870 }
02871 #else
02872 if (strcmp(ip, "2130706433")==0)
02873 return true;
02874 #endif
02875 return false;
02876 }
02877
02878
02879
02880
02881
02882
02883
02884
02885
02886 void RakPeer::AllowConnectionResponseIPMigration( bool allow )
02887 {
02888 allowConnectionResponseIPMigration = allow;
02889 }
02890
02891
02892
02893
02894
02895
02896
02897
02898
02899
02900
02901
02902 bool RakPeer::AdvertiseSystem( const char *host, unsigned short remotePort, const char *data, int dataLength, unsigned connectionSocketIndex )
02903 {
02904 RakNet::BitStream bs;
02905 bs.Write((MessageID)ID_ADVERTISE_SYSTEM);
02906 bs.WriteAlignedBytes((const unsigned char*) data,dataLength);
02907 return SendOutOfBand(host, remotePort, (const char*) bs.GetData(), bs.GetNumberOfBytesUsed(), connectionSocketIndex );
02908 }
02909
02910
02911
02912
02913
02914
02915
02916 void RakPeer::SetSplitMessageProgressInterval(int interval)
02917 {
02918 RakAssert(interval>=0);
02919 splitMessageProgressInterval=interval;
02920 for ( unsigned short i = 0; i < maximumNumberOfPeers; i++ )
02921 remoteSystemList[ i ].reliabilityLayer.SetSplitMessageProgressInterval(splitMessageProgressInterval);
02922 }
02923
02924
02925
02926
02927 int RakPeer::GetSplitMessageProgressInterval(void) const
02928 {
02929 return splitMessageProgressInterval;
02930 }
02931
02932
02933
02934
02935
02936
02937
02938 void RakPeer::SetUnreliableTimeout(RakNetTime timeoutMS)
02939 {
02940 unreliableTimeout=timeoutMS;
02941 for ( unsigned short i = 0; i < maximumNumberOfPeers; i++ )
02942 remoteSystemList[ i ].reliabilityLayer.SetUnreliableTimeout(unreliableTimeout);
02943 }
02944
02945
02946
02947
02948
02949
02950 void RakPeer::SendTTL( const char* host, unsigned short remotePort, int ttl, unsigned connectionSocketIndex )
02951 {
02952 char fakeData[2];
02953 fakeData[0]=0;
02954 fakeData[1]=1;
02955 unsigned int realIndex = GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex);
02956 SocketLayer::Instance()->SendToTTL( socketList[realIndex]->s, (char*)fakeData, 2, (char*) host, remotePort, ttl );
02957 }
02958
02959
02960
02961
02962
02963
02964
02965
02966
02967
02968
02969
02970 void RakPeer::SetCompileFrequencyTable( bool doCompile )
02971 {
02972 trackFrequencyTable = doCompile;
02973 }
02974
02975
02976
02977
02978
02979
02980
02981
02982
02983
02984
02985
02986
02987
02988
02989 bool RakPeer::GetOutgoingFrequencyTable( unsigned int outputFrequencyTable[ 256 ] )
02990 {
02991 if ( IsActive() )
02992 return false;
02993
02994 if ( trackFrequencyTable == false )
02995 return false;
02996
02997 memcpy( outputFrequencyTable, frequencyTable, sizeof( unsigned int ) * 256 );
02998
02999 return true;
03000 }
03001
03002
03003
03004
03005
03006
03007
03008
03009
03010
03011
03012
03013
03014
03015
03016
03017
03018 bool RakPeer::GenerateCompressionLayer( unsigned int inputFrequencyTable[ 256 ], bool inputLayer )
03019 {
03020 if ( IsActive() )
03021 return false;
03022
03023 DeleteCompressionLayer( inputLayer );
03024
03025 if ( inputLayer )
03026 {
03027 inputTree = RakNet::OP_NEW<HuffmanEncodingTree>( __FILE__, __LINE__ );
03028 inputTree->GenerateFromFrequencyTable( inputFrequencyTable );
03029 }
03030
03031 else
03032 {
03033 outputTree = RakNet::OP_NEW<HuffmanEncodingTree>( __FILE__, __LINE__ );
03034 outputTree->GenerateFromFrequencyTable( inputFrequencyTable );
03035 }
03036
03037 return true;
03038 }
03039
03040
03041
03042
03043
03044
03045
03046
03047
03048
03049
03050
03051 bool RakPeer::DeleteCompressionLayer( bool inputLayer )
03052 {
03053 if ( IsActive() )
03054 return false;
03055
03056 if ( inputLayer )
03057 {
03058 if ( inputTree )
03059 {
03060 RakNet::OP_DELETE(inputTree, __FILE__, __LINE__);
03061 inputTree = 0;
03062 }
03063 }
03064
03065 else
03066 {
03067 if ( outputTree )
03068 {
03069 RakNet::OP_DELETE(outputTree, __FILE__, __LINE__);
03070 outputTree = 0;
03071 }
03072 }
03073
03074 return true;
03075 }
03076
03077
03078
03079
03080
03081 float RakPeer::GetCompressionRatio( void ) const
03082 {
03083 if ( rawBytesSent > 0 )
03084 {
03085 return ( float ) compressedBytesSent / ( float ) rawBytesSent;
03086 }
03087
03088 else
03089 return 0.0f;
03090 }
03091
03092
03093
03094
03095
03096 float RakPeer::GetDecompressionRatio( void ) const
03097 {
03098 if ( rawBytesReceived > 0 )
03099 {
03100 return ( float ) compressedBytesReceived / ( float ) rawBytesReceived;
03101 }
03102
03103 else
03104 return 0.0f;
03105 }
03106
03107
03108
03109
03110
03111
03112 void RakPeer::AttachPlugin( PluginInterface2 *plugin )
03113 {
03114 if (messageHandlerList.GetIndexOf(plugin)==MAX_UNSIGNED_LONG)
03115 {
03116 plugin->SetRakPeerInterface(this);
03117 plugin->OnAttach();
03118 messageHandlerList.Insert(plugin, __FILE__, __LINE__);
03119 }
03120 }
03121
03122
03123
03124
03125
03126
03127 void RakPeer::DetachPlugin( PluginInterface2 *plugin )
03128 {
03129 if (plugin==0)
03130 return;
03131
03132 unsigned int index;
03133 index = messageHandlerList.GetIndexOf(plugin);
03134 if (index!=MAX_UNSIGNED_LONG)
03135 {
03136
03137 messageHandlerList[index]=messageHandlerList[messageHandlerList.Size()-1];
03138 messageHandlerList.RemoveFromEnd();
03139 plugin->OnDetach();
03140 plugin->SetRakPeerInterface(0);
03141 }
03142 }
03143
03144
03145
03146
03147
03148
03149
03150 void RakPeer::PushBackPacket( Packet *packet, bool pushAtHead)
03151 {
03152 if (packet==0)
03153 return;
03154
03155 unsigned i;
03156 for (i=0; i < messageHandlerList.Size(); i++)
03157 messageHandlerList[i]->OnPushBackPacket((const char*) packet->data, packet->bitSize, packet->systemAddress);
03158
03159 packetReturnMutex.Lock();
03160 if (pushAtHead)
03161 packetReturnQueue.PushAtHead(packet,0,__FILE__,__LINE__);
03162 else
03163 packetReturnQueue.Push(packet,__FILE__,__LINE__);
03164 packetReturnMutex.Unlock();
03165 }
03166
03167
03168 void RakPeer::SetRouterInterface( RouterInterface *routerInterface )
03169 {
03170 router=routerInterface;
03171 }
03172
03173 void RakPeer::RemoveRouterInterface( RouterInterface *routerInterface )
03174 {
03175 if (router==routerInterface)
03176 router=0;
03177 }
03178
03179 void RakPeer::ChangeSystemAddress(RakNetGUID guid, SystemAddress systemAddress)
03180 {
03181 BufferedCommandStruct *bcs;
03182
03183 #ifdef _RAKNET_THREADSAFE
03184 bcs=bufferedCommands.Allocate( __FILE__, __LINE__ );
03185 #else
03186 bcs=bufferedCommands.WriteLock();
03187 #endif
03188 bcs->data = 0;
03189 bcs->systemIdentifier.systemAddress=systemAddress;
03190 bcs->systemIdentifier.rakNetGuid=guid;
03191 bcs->command=BufferedCommandStruct::BCS_CHANGE_SYSTEM_ADDRESS;
03192 #ifdef _RAKNET_THREADSAFE
03193 bufferedCommands.Push(bcs);
03194 #else
03195 bufferedCommands.WriteUnlock();
03196 #endif
03197 }
03198
03199 Packet* RakPeer::AllocatePacket(unsigned dataSize)
03200 {
03201 return AllocPacket(dataSize, __FILE__, __LINE__);
03202 }
03203
03204 RakNetSmartPtr<RakNetSocket> RakPeer::GetSocket( const SystemAddress target )
03205 {
03206
03207 BufferedCommandStruct *bcs;
03208 #ifdef _RAKNET_THREADSAFE
03209 bcs=bufferedCommands.Allocate( __FILE__, __LINE__ );
03210 bcs->command=BufferedCommandStruct::BCS_GET_SOCKET;
03211 bcs->systemIdentifier=target;
03212 bcs->data=0;
03213 bufferedCommands.Push(bcs);
03214 #else
03215 bcs=bufferedCommands.WriteLock();
03216 bcs->command=BufferedCommandStruct::BCS_GET_SOCKET;
03217 bcs->systemIdentifier=target;
03218 bcs->data=0;
03219 bufferedCommands.WriteUnlock();
03220 #endif
03221
03222
03223 SocketQueryOutput *sqo;
03224 RakNetTime stopWaiting = RakNet::GetTime()+1000;
03225 DataStructures::List<RakNetSmartPtr<RakNetSocket> > output;
03226 while (RakNet::GetTime() < stopWaiting)
03227 {
03228 if (isMainLoopThreadActive==false)
03229 return RakNetSmartPtr<RakNetSocket>();
03230
03231 RakSleep(0);
03232
03233 #ifdef _RAKNET_THREADSAFE
03234 sqo = socketQueryOutput.Pop();
03235 if (sqo)
03236 {
03237 output=sqo->sockets;
03238 sqo->sockets.Clear(false, __FILE__, __LINE__);
03239 socketQueryOutput.Deallocate(sqo, __FILE__,__LINE__);
03240 if (output.Size())
03241 return output[0];
03242 break;
03243 }
03244 #else
03245 sqo = socketQueryOutput.ReadLock();
03246 if (sqo)
03247 {
03248 output=sqo->sockets;
03249 sqo->sockets.Clear(false, __FILE__, __LINE__);
03250 socketQueryOutput.ReadUnlock();
03251 if (output.Size())
03252 return output[0];
03253 break;
03254 }
03255 #endif
03256 }
03257 return RakNetSmartPtr<RakNetSocket>();
03258 }
03259
03260 void RakPeer::GetSockets( DataStructures::List<RakNetSmartPtr<RakNetSocket> > &sockets )
03261 {
03262 sockets.Clear(false, __FILE__, __LINE__);
03263
03264
03265 BufferedCommandStruct *bcs;
03266
03267 #ifdef _RAKNET_THREADSAFE
03268 bcs=bufferedCommands.Allocate( __FILE__, __LINE__ );
03269 bcs->command=BufferedCommandStruct::BCS_GET_SOCKET;
03270 bcs->systemIdentifier=UNASSIGNED_SYSTEM_ADDRESS;
03271 bcs->data=0;
03272 bufferedCommands.Push(bcs);
03273 #else
03274 bcs=bufferedCommands.WriteLock();
03275 bcs->command=BufferedCommandStruct::BCS_GET_SOCKET;
03276 bcs->systemIdentifier=UNASSIGNED_SYSTEM_ADDRESS;
03277 bcs->data=0;
03278 bufferedCommands.WriteUnlock();
03279 #endif
03280
03281
03282 SocketQueryOutput *sqo;
03283 RakNetTime stopWaiting = RakNet::GetTime()+1000;
03284 RakNetSmartPtr<RakNetSocket> output;
03285 while (RakNet::GetTime() < stopWaiting)
03286 {
03287 if (isMainLoopThreadActive==false)
03288 return;
03289
03290 RakSleep(0);
03291
03292 #ifdef _RAKNET_THREADSAFE
03293 sqo = socketQueryOutput.Pop();
03294 if (sqo)
03295 {
03296 sockets=sqo->sockets;
03297 sqo->sockets.Clear(false, __FILE__, __LINE__);
03298 socketQueryOutput.Deallocate(sqo, __FILE__,__LINE__);
03299 return;
03300 }
03301 #else
03302 sqo = socketQueryOutput.ReadLock();
03303 if (sqo)
03304 {
03305 sockets=sqo->sockets;
03306 sqo->sockets.Clear(false, __FILE__, __LINE__);
03307 socketQueryOutput.ReadUnlock();
03308 return;
03309 }
03310 #endif
03311 }
03312 return;
03313 }
03314
03315
03316
03317
03318 void RakPeer::ApplyNetworkSimulator( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance)
03319 {
03320 #ifdef _DEBUG
03321 if (remoteSystemList)
03322 {
03323 unsigned short i;
03324 for (i=0; i < maximumNumberOfPeers; i++)
03325
03326 remoteSystemList[i].reliabilityLayer.ApplyNetworkSimulator(packetloss, minExtraPing, extraPingVariance);
03327 }
03328
03329 _packetloss=packetloss;
03330 _minExtraPing=minExtraPing;
03331 _extraPingVariance=extraPingVariance;
03332 #endif
03333 }
03334
03335
03336
03337 void RakPeer::SetPerConnectionOutgoingBandwidthLimit( unsigned maxBitsPerSecond )
03338 {
03339 maxOutgoingBPS=maxBitsPerSecond;
03340 }
03341
03342
03343
03344
03345 bool RakPeer::IsNetworkSimulatorActive( void )
03346 {
03347 #ifdef _DEBUG
03348 return _packetloss>0 || _minExtraPing>0 || _extraPingVariance>0;
03349 #else
03350 return false;
03351 #endif
03352 }
03353
03354
03355
03356
03357
03358
03359
03360
03361
03362
03363
03364
03365
03366
03367
03368
03369
03370
03371
03372
03373
03374
03375
03376
03377
03378
03379
03380
03381
03382
03383
03384
03385
03386
03387
03388
03389
03390
03391
03392
03393
03394
03395
03396
03397
03398
03399
03400
03401
03402
03403
03404
03405
03406 char *RakPeer::GetRPCString( const char *data, const BitSize_t bitSize, const SystemAddress systemAddress)
03407 {
03408 bool nameIsEncoded=false;
03409 static char uniqueIdentifier[256];
03410 RPCIndex rpcIndex;
03411 RPCMap *_rpcMap;
03412 RakNet::BitStream rpcDecode((unsigned char*) data, BITS_TO_BYTES(bitSize), false);
03413 rpcDecode.IgnoreBits(8);
03414 if (data[0]==ID_TIMESTAMP)
03415 rpcDecode.IgnoreBits(sizeof(unsigned char)+sizeof(RakNetTime));
03416 rpcDecode.Read(nameIsEncoded);
03417 if (nameIsEncoded)
03418 {
03419 stringCompressor->DecodeString((char*)uniqueIdentifier, 256, &rpcDecode);
03420 }
03421 else
03422 {
03423 rpcDecode.ReadCompressed( rpcIndex );
03424 RPCNode *rpcNode;
03425
03426 if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS)
03427 _rpcMap=&rpcMap;
03428 else
03429 {
03430 RemoteSystemStruct *rss=GetRemoteSystemFromSystemAddress(systemAddress, false, true);
03431 if (rss)
03432 _rpcMap=&(rss->rpcMap);
03433 else
03434 _rpcMap=0;
03435 }
03436
03437 if (_rpcMap)
03438 rpcNode = _rpcMap->GetNodeFromIndex(rpcIndex);
03439 else
03440 rpcNode=0;
03441
03442 if (_rpcMap && rpcNode)
03443 strcpy((char*)uniqueIdentifier, rpcNode->uniqueIdentifier);
03444 else
03445 strcpy((char*)uniqueIdentifier, "[UNKNOWN]");
03446 }
03447
03448 return uniqueIdentifier;
03449 }
03450
03451
03452 void RakPeer::WriteOutOfBandHeader(RakNet::BitStream *bitStream)
03453 {
03454 bitStream->Write((MessageID)ID_OUT_OF_BAND_INTERNAL);
03455 bitStream->Write(myGuid);
03456 bitStream->WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
03457 }
03458
03459 void RakPeer::SetUserUpdateThread(void (*_userUpdateThreadPtr)(RakPeerInterface *, void *), void *_userUpdateThreadData)
03460 {
03461 userUpdateThreadPtr=_userUpdateThreadPtr;
03462 userUpdateThreadData=_userUpdateThreadData;
03463 }
03464
03465 bool RakPeer::SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex )
03466 {
03467 if ( IsActive() == false )
03468 return false;
03469
03470 if (host==0 || host[0]==0)
03471 return false;
03472
03473
03474 RakAssert(connectionSocketIndex < socketList.Size());
03475
03476
03477 RakAssert(dataLength <= MAX_OFFLINE_DATA_LENGTH);
03478
03479 if ( NonNumericHostString( host ) )
03480 {
03481 host = ( char* ) SocketLayer::Instance()->DomainNameToIP( host );
03482
03483 if (host==0)
03484 return false;
03485 }
03486
03487 if (host==0)
03488 return false;
03489
03490 SystemAddress systemAddress;
03491 systemAddress.SetBinaryAddress(host);
03492 systemAddress.port=remotePort;
03493
03494
03495 RakNet::BitStream bitStream;
03496 WriteOutOfBandHeader(&bitStream);
03497
03498 if (dataLength>0)
03499 {
03500 bitStream.Write(data, dataLength);
03501 }
03502 unsigned i;
03503 for (i=0; i < messageHandlerList.Size(); i++)
03504 messageHandlerList[i]->OnDirectSocketSend((const char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), systemAddress);
03505 unsigned int realIndex = GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex);
03506 SocketLayer::Instance()->SendTo( socketList[realIndex]->s, (const char*)bitStream.GetData(), (int) bitStream.GetNumberOfBytesUsed(), ( char* ) host, remotePort, socketList[realIndex]->remotePortRakNetWasStartedOn_PS3 );
03507
03508 return true;
03509 }
03510
03511
03512 RakNetStatistics * const RakPeer::GetStatistics( const SystemAddress systemAddress, RakNetStatistics *rns )
03513 {
03514 static RakNetStatistics staticStatistics;
03515 RakNetStatistics *systemStats;
03516 if (rns==0)
03517 systemStats=&staticStatistics;
03518 else
03519 systemStats=rns;
03520
03521 if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS)
03522 {
03523 bool firstWrite=false;
03524
03525 for ( unsigned short i = 0; i < maximumNumberOfPeers; i++ )
03526 {
03527 if (remoteSystemList[ i ].isActive)
03528 {
03529 RakNetStatistics rnsTemp;
03530 remoteSystemList[ i ].reliabilityLayer.GetStatistics(&rnsTemp);
03531
03532 if (firstWrite==false)
03533 {
03534 memcpy(systemStats, &rnsTemp, sizeof(RakNetStatistics));
03535 firstWrite=true;
03536 }
03537 else
03538 (*systemStats)+=rnsTemp;
03539 }
03540 }
03541 return systemStats;
03542 }
03543 else
03544 {
03545 RemoteSystemStruct * rss;
03546 rss = GetRemoteSystemFromSystemAddress( systemAddress, false, false );
03547 if ( rss && endThreads==false )
03548 {
03549 rss->reliabilityLayer.GetStatistics(systemStats);
03550 return systemStats;
03551 }
03552 }
03553
03554 return 0;
03555 }
03556
03557
03558 bool RakPeer::GetStatistics( const int index, RakNetStatistics *rns )
03559 {
03560 if (index < maximumNumberOfPeers && remoteSystemList[ index ].isActive)
03561 {
03562 remoteSystemList[ index ].reliabilityLayer.GetStatistics(rns);
03563 return true;
03564 }
03565 return false;
03566 }
03567
03568 unsigned int RakPeer::GetReceiveBufferSize(void)
03569 {
03570 unsigned int size;
03571 packetReturnMutex.Lock();
03572 size=packetReturnQueue.Size();
03573 packetReturnMutex.Unlock();
03574 return size;
03575 }
03576
03577 int RakPeer::GetIndexFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread ) const
03578 {
03579 unsigned i;
03580
03581 if ( systemAddress == UNASSIGNED_SYSTEM_ADDRESS )
03582 return -1;
03583
03584 if (systemAddress.systemIndex!=(SystemIndex)-1 && systemAddress.systemIndex < maximumNumberOfPeers && remoteSystemList[systemAddress.systemIndex].systemAddress==systemAddress && remoteSystemList[ systemAddress.systemIndex ].isActive)
03585 return systemAddress.systemIndex;
03586
03587 if (calledFromNetworkThread)
03588 {
03589 return GetRemoteSystemIndex(systemAddress);
03590 }
03591 else
03592 {
03593
03594 for ( i = 0; i < maximumNumberOfPeers; i++ )
03595 if ( remoteSystemList[ i ].isActive && remoteSystemList[ i ].systemAddress == systemAddress )
03596 return i;
03597
03598
03599 for ( i = 0; i < maximumNumberOfPeers; i++ )
03600 if ( remoteSystemList[ i ].systemAddress == systemAddress )
03601 return i;
03602 }
03603
03604 return -1;
03605 }
03606
03607 int RakPeer::GetIndexFromGuid( const RakNetGUID guid )
03608 {
03609 unsigned i;
03610
03611 if ( guid == UNASSIGNED_RAKNET_GUID )
03612 return -1;
03613
03614 if (guid.systemIndex!=(SystemIndex)-1 && guid.systemIndex < maximumNumberOfPeers && remoteSystemList[guid.systemIndex].guid==guid && remoteSystemList[ guid.systemIndex ].isActive)
03615 return guid.systemIndex;
03616
03617
03618 for ( i = 0; i < maximumNumberOfPeers; i++ )
03619 if ( remoteSystemList[ i ].isActive && remoteSystemList[ i ].guid == guid )
03620 return i;
03621
03622
03623 for ( i = 0; i < maximumNumberOfPeers; i++ )
03624 if ( remoteSystemList[ i ].guid == guid )
03625 return i;
03626
03627 return -1;
03628 }
03629
03630 bool RakPeer::SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNetTime timeoutTime )
03631 {
03632 RakAssert(passwordDataLength <= 256);
03633 RakAssert(remotePort!=0);
03634 SystemAddress systemAddress;
03635 systemAddress.SetBinaryAddress(host);
03636 systemAddress.port=remotePort;
03637
03638
03639 if (GetRemoteSystemFromSystemAddress(systemAddress, false, true))
03640 return false;
03641
03642
03643 RequestedConnectionStruct *rcs = RakNet::OP_NEW<RequestedConnectionStruct>(__FILE__,__LINE__);
03644
03645 rcs->systemAddress=systemAddress;
03646 rcs->nextRequestTime=RakNet::GetTime();
03647 rcs->requestsMade=0;
03648 rcs->data=0;
03649 rcs->extraData=extraData;
03650 rcs->socketIndex=connectionSocketIndex;
03651 rcs->actionToTake=RequestedConnectionStruct::CONNECT;
03652 rcs->sendConnectionAttemptCount=sendConnectionAttemptCount;
03653 rcs->timeBetweenSendConnectionAttemptsMS=timeBetweenSendConnectionAttemptsMS;
03654 memcpy(rcs->outgoingPassword, passwordData, passwordDataLength);
03655 rcs->outgoingPasswordLength=(unsigned char) passwordDataLength;
03656 rcs->timeoutTime=timeoutTime;
03657
03658
03659 unsigned int i=0;
03660 requestedConnectionQueueMutex.Lock();
03661 for (; i < requestedConnectionQueue.Size(); i++)
03662 {
03663 if (requestedConnectionQueue[i]->systemAddress==systemAddress)
03664 {
03665 requestedConnectionQueueMutex.Unlock();
03666 RakNet::OP_DELETE(rcs,__FILE__,__LINE__);
03667 return false;
03668 }
03669 }
03670 requestedConnectionQueue.Push(rcs, __FILE__, __LINE__ );
03671 requestedConnectionQueueMutex.Unlock();
03672
03673 return true;
03674 }
03675 bool RakPeer::SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNetTime timeoutTime, RakNetSmartPtr<RakNetSocket> socket )
03676 {
03677 RakAssert(passwordDataLength <= 256);
03678 SystemAddress systemAddress;
03679 systemAddress.SetBinaryAddress(host);
03680 systemAddress.port=remotePort;
03681
03682
03683 if (GetRemoteSystemFromSystemAddress(systemAddress, false, true))
03684 return false;
03685
03686
03687 RequestedConnectionStruct *rcs = RakNet::OP_NEW<RequestedConnectionStruct>(__FILE__,__LINE__);
03688
03689 rcs->systemAddress=systemAddress;
03690 rcs->nextRequestTime=RakNet::GetTime();
03691 rcs->requestsMade=0;
03692 rcs->data=0;
03693 rcs->extraData=extraData;
03694 rcs->socketIndex=connectionSocketIndex;
03695 rcs->actionToTake=RequestedConnectionStruct::CONNECT;
03696 rcs->sendConnectionAttemptCount=sendConnectionAttemptCount;
03697 rcs->timeBetweenSendConnectionAttemptsMS=timeBetweenSendConnectionAttemptsMS;
03698 memcpy(rcs->outgoingPassword, passwordData, passwordDataLength);
03699 rcs->outgoingPasswordLength=(unsigned char) passwordDataLength;
03700 rcs->timeoutTime=timeoutTime;
03701 rcs->socket=socket;
03702
03703
03704 unsigned int i=0;
03705 requestedConnectionQueueMutex.Lock();
03706 for (; i < requestedConnectionQueue.Size(); i++)
03707 {
03708 if (requestedConnectionQueue[i]->systemAddress==systemAddress)
03709 {
03710 requestedConnectionQueueMutex.Unlock();
03711 RakNet::OP_DELETE(rcs,__FILE__,__LINE__);
03712 return false;
03713 }
03714 }
03715 requestedConnectionQueue.Push(rcs, __FILE__, __LINE__ );
03716 requestedConnectionQueueMutex.Unlock();
03717
03718 return true;
03719 }
03720
03721 void RakPeer::ValidateRemoteSystemLookup(void) const
03722 {
03723 }
03724
03725 RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystem( const AddressOrGUID systemIdentifier, bool calledFromNetworkThread, bool onlyActive ) const
03726 {
03727 if (systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID)
03728 return GetRemoteSystemFromGUID(systemIdentifier.rakNetGuid, onlyActive);
03729 else
03730 return GetRemoteSystemFromSystemAddress(systemIdentifier.systemAddress, calledFromNetworkThread, onlyActive);
03731 }
03732
03733 RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystemFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread, bool onlyActive ) const
03734 {
03735 unsigned i;
03736
03737 if ( systemAddress == UNASSIGNED_SYSTEM_ADDRESS )
03738 return 0;
03739
03740 if (calledFromNetworkThread)
03741 {
03742 unsigned int index = GetRemoteSystemIndex(systemAddress);
03743 if (index!=(unsigned int) -1)
03744 {
03745 if (onlyActive==false || remoteSystemList[ index ].isActive==true )
03746 {
03747 RakAssert(remoteSystemList[index].systemAddress==systemAddress);
03748 return remoteSystemList + index;
03749 }
03750 }
03751 }
03752 else
03753 {
03754 int deadConnectionIndex=-1;
03755
03756
03757 for ( i = 0; i < maximumNumberOfPeers; i++ )
03758 {
03759 if (remoteSystemList[ i ].systemAddress == systemAddress)
03760 {
03761 if ( remoteSystemList[ i ].isActive )
03762 return remoteSystemList + i;
03763 else if (deadConnectionIndex==-1)
03764 deadConnectionIndex=i;
03765 }
03766 }
03767
03768 if (deadConnectionIndex!=-1 && onlyActive==false)
03769 return remoteSystemList + deadConnectionIndex;
03770 }
03771
03772 return 0;
03773 }
03774
03775 RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystemFromGUID( const RakNetGUID guid, bool onlyActive ) const
03776 {
03777 if (guid==UNASSIGNED_RAKNET_GUID)
03778 return 0;
03779
03780 unsigned i;
03781 for ( i = 0; i < maximumNumberOfPeers; i++ )
03782 {
03783 if (remoteSystemList[ i ].guid == guid && (onlyActive==false || remoteSystemList[ i ].isActive))
03784 {
03785 return remoteSystemList + i;
03786 }
03787 }
03788 return 0;
03789 }
03790
03791 void RakPeer::ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, SystemAddress systemAddress, const char *data, int byteSize )
03792 {
03793 RakNet::BitStream bs((unsigned char*) data,byteSize,false);
03794 bs.IgnoreBytes(sizeof(MessageID));
03795 bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID));
03796 RakNetGUID guid;
03797 bs.Read(guid);
03798 RakNetTime incomingTimestamp;
03799 bs.Read(incomingTimestamp);
03800
03801
03802
03803 if ( 0 )
03804 {
03805 RakNet::BitStream bs;
03806 bs.Write((MessageID)ID_NO_FREE_INCOMING_CONNECTIONS);
03807 bs.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
03808 bs.Write(GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS));
03809 SendImmediate((char*) bs.GetData(), bs.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, RakNet::GetTimeNS(), 0);
03810 remoteSystem->connectMode=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY;
03811 }
03812 else
03813 {
03814 const char *password = data + sizeof(MessageID) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID) + sizeof(RakNetTime);
03815 int passwordLength = byteSize - (int) (sizeof(MessageID) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID) + sizeof(RakNetTime));
03816
03817 if ( incomingPasswordLength == passwordLength &&
03818 memcmp( password, incomingPassword, incomingPasswordLength ) == 0 )
03819 {
03820 remoteSystem->connectMode=RemoteSystemStruct::HANDLING_CONNECTION_REQUEST;
03821
03822 #if !defined(_XBOX) && !defined(X360)
03823 char str1[64];
03824 systemAddress.ToString(false, str1);
03825 if ( usingSecurity == false ||
03826 IsInSecurityExceptionList(str1))
03827 #endif
03828 {
03829 #ifdef _TEST_AES
03830 unsigned char AESKey[ 16 ];
03831
03832 for ( i = 0; i < 16; i++ )
03833 AESKey[ i ] = i;
03834
03835 OnConnectionRequest( remoteSystem, AESKey, true );
03836 #else
03837
03838 OnConnectionRequest( remoteSystem, 0, false, incomingTimestamp );
03839 #endif
03840 }
03841 #if !defined(_XBOX) && !defined(X360)
03842 else
03843 SecuredConnectionResponse( systemAddress );
03844 #endif
03845 }
03846 else
03847 {
03848
03849 RakNet::BitStream bitStream;
03850 bitStream.Write((MessageID)ID_INVALID_PASSWORD);
03851 bitStream.Write(GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS));
03852 SendImmediate((char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, RakNet::GetTimeNS(), 0);
03853 remoteSystem->connectMode=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY;
03854 }
03855 }
03856 }
03857
03858 void RakPeer::OnConnectionRequest( RakPeer::RemoteSystemStruct *remoteSystem, unsigned char *AESKey, bool setAESKey, RakNetTime incomingTimestamp )
03859 {
03860
03861
03862 {
03863 SendConnectionRequestAccepted(remoteSystem, incomingTimestamp);
03864
03865
03866
03867
03868 remoteSystem->setAESKey=setAESKey;
03869 if ( setAESKey )
03870 {
03871 memcpy(remoteSystem->AESKey, AESKey, 16);
03872 remoteSystem->connectMode=RemoteSystemStruct::SET_ENCRYPTION_ON_MULTIPLE_16_BYTE_PACKET;
03873 }
03874 }
03875
03876
03877
03878
03879
03880
03881
03882
03883
03884
03885 }
03886
03887 void RakPeer::SendConnectionRequestAccepted(RakPeer::RemoteSystemStruct *remoteSystem, RakNetTime incomingTimestamp)
03888 {
03889 RakNet::BitStream bitStream;
03890 bitStream.Write((MessageID)ID_CONNECTION_REQUEST_ACCEPTED);
03891 bitStream.Write(remoteSystem->systemAddress);
03892 SystemIndex systemIndex = (SystemIndex) GetIndexFromSystemAddress( remoteSystem->systemAddress, true );
03893 RakAssert(systemIndex!=65535);
03894 bitStream.Write(systemIndex);
03895 for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++)
03896 bitStream.Write(mySystemAddress[i]);
03897 bitStream.Write(incomingTimestamp);
03898 bitStream.Write(RakNet::GetTime());
03899
03900 SendImmediate((char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, RELIABLE_ORDERED, 0, remoteSystem->systemAddress, false, false, RakNet::GetTimeNS(), 0);
03901 }
03902
03903
03904 void RakPeer::NotifyAndFlagForShutdown( const SystemAddress systemAddress, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority )
03905 {
03906 RakNet::BitStream temp( sizeof(unsigned char) );
03907 temp.Write( (MessageID)ID_DISCONNECTION_NOTIFICATION );
03908 if (performImmediate)
03909 {
03910 SendImmediate((char*)temp.GetData(), temp.GetNumberOfBitsUsed(), disconnectionNotificationPriority, RELIABLE_ORDERED, orderingChannel, systemAddress, false, false, RakNet::GetTimeNS(), 0);
03911 RemoteSystemStruct *rss=GetRemoteSystemFromSystemAddress(systemAddress, true, true);
03912 rss->connectMode=RemoteSystemStruct::DISCONNECT_ASAP;
03913 }
03914 else
03915 {
03916 SendBuffered((const char*)temp.GetData(), temp.GetNumberOfBitsUsed(), disconnectionNotificationPriority, RELIABLE_ORDERED, orderingChannel, systemAddress, false, RemoteSystemStruct::DISCONNECT_ASAP, 0);
03917 }
03918 }
03919
03920 unsigned short RakPeer::GetNumberOfRemoteInitiatedConnections( void ) const
03921 {
03922 unsigned short i, numberOfIncomingConnections;
03923
03924 if ( remoteSystemList == 0 || endThreads == true )
03925 return 0;
03926
03927 numberOfIncomingConnections = 0;
03928
03929
03930 for ( i = 0; i < maximumNumberOfPeers; i++ )
03931
03932 {
03933 if ( remoteSystemList[ i ].isActive && remoteSystemList[ i ].weInitiatedTheConnection == false && remoteSystemList[i].connectMode==RemoteSystemStruct::CONNECTED)
03934 numberOfIncomingConnections++;
03935 }
03936
03937 return numberOfIncomingConnections;
03938 }
03939
03940
03941 RakPeer::RemoteSystemStruct * RakPeer::AssignSystemAddressToRemoteSystemList( const SystemAddress systemAddress, RemoteSystemStruct::ConnectMode connectionMode, RakNetSmartPtr<RakNetSocket> incomingRakNetSocket, bool *thisIPConnectedRecently, SystemAddress bindingAddress, int incomingMTU, RakNetGUID guid )
03942 {
03943 RemoteSystemStruct * remoteSystem;
03944 unsigned i,j,assignedIndex;
03945 RakNetTime time = RakNet::GetTime();
03946 #ifdef _DEBUG
03947 RakAssert(systemAddress!=UNASSIGNED_SYSTEM_ADDRESS);
03948 #endif
03949
03950 if (limitConnectionFrequencyFromTheSameIP)
03951 {
03952 if (IsLoopbackAddress(systemAddress,false)==false)
03953 {
03954 for ( i = 0; i < maximumNumberOfPeers; i++ )
03955 {
03956 if ( remoteSystemList[ i ].isActive==true &&
03957 remoteSystemList[ i ].systemAddress.binaryAddress==systemAddress.binaryAddress &&
03958 time >= remoteSystemList[ i ].connectionTime &&
03959 time - remoteSystemList[ i ].connectionTime < 100
03960 )
03961 {
03962
03963
03964 *thisIPConnectedRecently=true;
03965 ValidateRemoteSystemLookup();
03966 return 0;
03967 }
03968 }
03969 }
03970 }
03971
03972
03973 bindingAddress.port=incomingRakNetSocket->boundAddress.port;
03974
03975 *thisIPConnectedRecently=false;
03976 for ( assignedIndex = 0; assignedIndex < maximumNumberOfPeers; assignedIndex++ )
03977 {
03978 if ( remoteSystemList[ assignedIndex ].isActive==false )
03979 {
03980 remoteSystem=remoteSystemList+assignedIndex;
03981 remoteSystem->rpcMap.Clear();
03982 ReferenceRemoteSystem(systemAddress, assignedIndex);
03983 remoteSystem->MTUSize=defaultMTUSize;
03984 remoteSystem->guid=guid;
03985 remoteSystem->isActive = true;
03986
03987 if (incomingMTU > remoteSystem->MTUSize)
03988 remoteSystem->MTUSize=incomingMTU;
03989 remoteSystem->reliabilityLayer.Reset(true, remoteSystem->MTUSize);
03990 remoteSystem->reliabilityLayer.SetSplitMessageProgressInterval(splitMessageProgressInterval);
03991 remoteSystem->reliabilityLayer.SetUnreliableTimeout(unreliableTimeout);
03992 remoteSystem->reliabilityLayer.SetTimeoutTime(defaultTimeoutTime);
03993 remoteSystem->reliabilityLayer.SetEncryptionKey( 0 );
03994 if (incomingRakNetSocket->boundAddress==bindingAddress)
03995 {
03996 remoteSystem->rakNetSocket=incomingRakNetSocket;
03997 }
03998 else
03999 {
04000 char str[256];
04001 bindingAddress.ToString(true,str);
04002
04003
04004 unsigned int ipListIndex, foundIndex=(unsigned int)-1;
04005
04006 for (ipListIndex=0; ipListIndex < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ipListIndex++)
04007 {
04008 if (ipList[ipListIndex][0]==0)
04009 break;
04010 if (bindingAddress.binaryAddress==binaryAddresses[ipListIndex])
04011 {
04012 foundIndex=ipListIndex;
04013 break;
04014 }
04015 }
04016
04017
04018
04019
04020
04021 if (1 || foundIndex==(unsigned int)-1)
04022 {
04023
04024 remoteSystem->rakNetSocket=incomingRakNetSocket;
04025 }
04026 else
04027 {
04028
04029 unsigned int socketListIndex;
04030 for (socketListIndex=0; socketListIndex < socketList.Size(); socketListIndex++)
04031 {
04032 if (socketList[socketListIndex]->boundAddress==bindingAddress)
04033 {
04034
04035 remoteSystem->rakNetSocket=socketList[socketListIndex];
04036 break;
04037 }
04038 }
04039
04040 if (socketListIndex==socketList.Size())
04041 {
04042
04043 RakNetSmartPtr<RakNetSocket> rns(RakNet::OP_NEW<RakNetSocket>(__FILE__,__LINE__));
04044 if (incomingRakNetSocket->remotePortRakNetWasStartedOn_PS3==0)
04045 rns->s = (unsigned int) SocketLayer::Instance()->CreateBoundSocket( bindingAddress.port, true, ipList[foundIndex], 0 );
04046 else
04047 rns->s = (unsigned int) SocketLayer::Instance()->CreateBoundSocket_PS3Lobby( bindingAddress.port, true, ipList[foundIndex] );
04048 if ((SOCKET)rns->s==(SOCKET)-1)
04049 {
04050
04051 remoteSystem->rakNetSocket=incomingRakNetSocket;
04052 }
04053 else
04054 {
04055 rns->boundAddress=bindingAddress;
04056 rns->remotePortRakNetWasStartedOn_PS3=incomingRakNetSocket->remotePortRakNetWasStartedOn_PS3;
04057 rns->userConnectionSocketIndex=(unsigned int)-1;
04058 socketList.Push(rns, __FILE__, __LINE__ );
04059 remoteSystem->rakNetSocket=rns;
04060
04061 RakPeerAndIndex rpai;
04062 rpai.remotePortRakNetWasStartedOn_PS3=rns->remotePortRakNetWasStartedOn_PS3;
04063 rpai.s=rns->s;
04064 rpai.rakPeer=this;
04065 #ifdef _WIN32
04066 int highPriority=THREAD_PRIORITY_ABOVE_NORMAL;
04067 #else
04068 int highPriority=-10;
04069 #endif
04070
04071
04072 highPriority=0;
04073
04074 isRecvFromLoopThreadActive=false;
04075 int errorCode = RakNet::RakThread::Create(RecvFromLoop, &rpai, highPriority);
04076 RakAssert(errorCode!=0);
04077 while ( isRecvFromLoopThreadActive == false )
04078 RakSleep(10);
04079
04080
04081
04082
04083
04084
04085
04086
04087
04088
04089
04090 }
04091 }
04092 }
04093 }
04094
04095 for ( j = 0; j < (unsigned) PING_TIMES_ARRAY_SIZE; j++ )
04096 {
04097 remoteSystem->pingAndClockDifferential[ j ].pingTime = 65535;
04098 remoteSystem->pingAndClockDifferential[ j ].clockDifferential = 0;
04099 }
04100
04101 remoteSystem->connectMode=connectionMode;
04102 remoteSystem->pingAndClockDifferentialWriteIndex = 0;
04103 remoteSystem->lowestPing = 65535;
04104 remoteSystem->nextPingTime = 0;
04105 remoteSystem->weInitiatedTheConnection = false;
04106 remoteSystem->connectionTime = time;
04107 remoteSystem->myExternalSystemAddress = UNASSIGNED_SYSTEM_ADDRESS;
04108 remoteSystem->setAESKey=false;
04109 remoteSystem->lastReliableSend=time;
04110
04111 #ifdef _DEBUG
04112 int indexLoopupCheck=GetIndexFromSystemAddress( systemAddress, true );
04113 if (indexLoopupCheck!=assignedIndex)
04114 {
04115 RakAssert(indexLoopupCheck==assignedIndex);
04116 }
04117 #endif
04118
04119 return remoteSystem;
04120 }
04121 }
04122
04123 return 0;
04124 }
04125
04126
04127
04128
04129 void RakPeer::ShiftIncomingTimestamp( unsigned char *data, SystemAddress systemAddress ) const
04130 {
04131 #ifdef _DEBUG
04132 RakAssert( IsActive() );
04133 RakAssert( data );
04134 #endif
04135
04136 RakNet::BitStream timeBS( data, sizeof(RakNetTime), false);
04137 RakNetTime encodedTimestamp;
04138 timeBS.Read(encodedTimestamp);
04139
04140 encodedTimestamp = encodedTimestamp - GetBestClockDifferential( systemAddress );
04141 timeBS.SetWriteOffset(0);
04142 timeBS.Write(encodedTimestamp);
04143 }
04144
04145
04146
04147 RakNetTime RakPeer::GetBestClockDifferential( const SystemAddress systemAddress ) const
04148 {
04149 int counter, lowestPingSoFar;
04150 RakNetTime clockDifferential;
04151 RemoteSystemStruct *remoteSystem = GetRemoteSystemFromSystemAddress( systemAddress, true, true );
04152
04153 if ( remoteSystem == 0 )
04154 return 0;
04155
04156 lowestPingSoFar = 65535;
04157
04158 clockDifferential = 0;
04159
04160 for ( counter = 0; counter < PING_TIMES_ARRAY_SIZE; counter++ )
04161 {
04162 if ( remoteSystem->pingAndClockDifferential[ counter ].pingTime == 65535 )
04163 break;
04164
04165 if ( remoteSystem->pingAndClockDifferential[ counter ].pingTime < lowestPingSoFar )
04166 {
04167 clockDifferential = remoteSystem->pingAndClockDifferential[ counter ].clockDifferential;
04168 lowestPingSoFar = remoteSystem->pingAndClockDifferential[ counter ].pingTime;
04169 }
04170 }
04171
04172 return clockDifferential;
04173 }
04174
04175 unsigned int RakPeer::RemoteSystemLookupHashIndex(SystemAddress sa) const
04176 {
04177 unsigned int lastHash = SuperFastHashIncremental ((const char*) & sa.binaryAddress, 4, 4 );
04178 lastHash = SuperFastHashIncremental ((const char*) & sa.port, 2, lastHash );
04179 return lastHash % ((unsigned int) maximumNumberOfPeers * REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE);
04180 }
04181
04182 void RakPeer::ReferenceRemoteSystem(SystemAddress sa, unsigned int remoteSystemListIndex)
04183 {
04184
04185
04186
04187
04188
04189
04190
04191
04192
04193
04194
04195
04196 SystemAddress oldAddress = remoteSystemList[remoteSystemListIndex].systemAddress;
04197 if (oldAddress!=UNASSIGNED_SYSTEM_ADDRESS)
04198 {
04199
04200
04201
04202
04203 if (GetRemoteSystem(oldAddress)==&remoteSystemList[remoteSystemListIndex])
04204 DereferenceRemoteSystem(oldAddress);
04205 }
04206 DereferenceRemoteSystem(sa);
04207
04208
04209
04210
04211
04212
04213
04214
04215
04216
04217
04218
04219
04220
04221
04222
04223 remoteSystemList[remoteSystemListIndex].systemAddress=sa;
04224
04225 unsigned int hashIndex = RemoteSystemLookupHashIndex(sa);
04226 RemoteSystemIndex *rsi;
04227 rsi = remoteSystemIndexPool.Allocate(__FILE__,__LINE__);
04228 if (remoteSystemLookup[hashIndex]==0)
04229 {
04230 rsi->next=0;
04231 rsi->index=remoteSystemListIndex;
04232 remoteSystemLookup[hashIndex]=rsi;
04233 }
04234 else
04235 {
04236 RemoteSystemIndex *cur = remoteSystemLookup[hashIndex];
04237 while (cur->next!=0)
04238 {
04239 cur=cur->next;
04240 }
04241
04242 rsi = remoteSystemIndexPool.Allocate(__FILE__,__LINE__);
04243 rsi->next=0;
04244 rsi->index=remoteSystemListIndex;
04245 cur->next=rsi;
04246 }
04247
04248
04249
04250
04251
04252
04253
04254
04255
04256
04257
04258
04259
04260 RakAssert(GetRemoteSystemIndex(sa)==remoteSystemListIndex);
04261 }
04262
04263 void RakPeer::DereferenceRemoteSystem(SystemAddress sa)
04264 {
04265 unsigned int hashIndex = RemoteSystemLookupHashIndex(sa);
04266 RemoteSystemIndex *cur = remoteSystemLookup[hashIndex];
04267 RemoteSystemIndex *last = 0;
04268 while (cur!=0)
04269 {
04270 if (remoteSystemList[cur->index].systemAddress==sa)
04271 {
04272 if (last==0)
04273 {
04274 remoteSystemLookup[hashIndex]=cur->next;
04275 }
04276 else
04277 {
04278 last->next=cur->next;
04279 }
04280 remoteSystemIndexPool.Release(cur,__FILE__,__LINE__);
04281 break;
04282 }
04283 last=cur;
04284 cur=cur->next;
04285 }
04286 }
04287
04288 unsigned int RakPeer::GetRemoteSystemIndex(SystemAddress sa) const
04289 {
04290 unsigned int hashIndex = RemoteSystemLookupHashIndex(sa);
04291 RemoteSystemIndex *cur = remoteSystemLookup[hashIndex];
04292 while (cur!=0)
04293 {
04294 if (remoteSystemList[cur->index].systemAddress==sa)
04295 return cur->index;
04296 cur=cur->next;
04297 }
04298 return (unsigned int) -1;
04299 }
04300
04301 RakPeer::RemoteSystemStruct* RakPeer::GetRemoteSystem(SystemAddress sa) const
04302 {
04303 unsigned int remoteSystemIndex = GetRemoteSystemIndex(sa);
04304 if (remoteSystemIndex==(unsigned int)-1)
04305 return 0;
04306 return remoteSystemList + remoteSystemIndex;
04307 }
04308
04309 void RakPeer::ClearRemoteSystemLookup(void)
04310 {
04311 remoteSystemIndexPool.Clear(__FILE__,__LINE__);
04312 RakNet::OP_DELETE_ARRAY(remoteSystemLookup,__FILE__,__LINE__);
04313 remoteSystemLookup=0;
04314 }
04315
04316
04317
04318
04319
04320
04321
04322
04323
04324
04325
04326
04327
04328
04329
04330
04331
04332
04333
04334
04335
04336
04337
04338
04339
04340
04341
04342
04343
04344
04345
04346
04347
04348
04349
04350
04351
04352
04353
04354
04355
04356
04357
04358
04359
04360
04361
04362
04363
04364
04365
04366
04367
04368
04369
04370 #ifdef _MSC_VER
04371 #pragma warning( disable : 4701 ) // warning C4701: local variable <variable name> may be used without having been initialized
04372 #endif
04373 bool RakPeer::HandleRPCPacket( const char *data, int length, SystemAddress systemAddress )
04374 {
04375
04376
04377
04378
04379
04380
04381
04382 RakNet::BitStream incomingBitStream( (unsigned char *) data, length, false );
04383 char uniqueIdentifier[ 256 ];
04384
04385 unsigned char *userData;
04386
04387 bool nameIsEncoded, networkIDIsEncoded;
04388 RPCIndex rpcIndex;
04389 RPCNode *node;
04390 RPCParameters rpcParms;
04391 NetworkID networkID;
04392 bool blockingCommand;
04393 RakNet::BitStream replyToSender;
04394 rpcParms.replyToSender=&replyToSender;
04395
04396 rpcParms.recipient=this;
04397 rpcParms.sender=systemAddress;
04398
04399
04400 incomingBitStream.IgnoreBits(8);
04401 if (data[0]==ID_TIMESTAMP)
04402 {
04403 incomingBitStream.IgnoreBits(8*(sizeof(RakNetTime)+sizeof(MessageID)));
04404 memcpy(&rpcParms.remoteTimestamp, data+sizeof(MessageID), sizeof(RakNetTime));
04405 }
04406 else
04407 rpcParms.remoteTimestamp=0;
04408 if ( incomingBitStream.Read( nameIsEncoded ) == false )
04409 {
04410 #ifdef _DEBUG
04411 RakAssert( 0 );
04412 #endif
04413 return false;
04414 }
04415
04416 if (nameIsEncoded)
04417 {
04418 if ( stringCompressor->DecodeString(uniqueIdentifier, 256, &incomingBitStream) == false )
04419 {
04420 #ifdef _DEBUG
04421 RakAssert( 0 );
04422 #endif
04423 return false;
04424 }
04425
04426 rpcIndex = rpcMap.GetIndexFromFunctionName(uniqueIdentifier);
04427 }
04428 else
04429 {
04430 if ( incomingBitStream.ReadCompressed( rpcIndex ) == false )
04431 {
04432 #ifdef _DEBUG
04433 RakAssert( 0 );
04434 #endif
04435 return false;
04436 }
04437 }
04438 if ( incomingBitStream.Read( blockingCommand ) == false )
04439 {
04440 #ifdef _DEBUG
04441 RakAssert( 0 );
04442 #endif
04443 return false;
04444 }
04445
04446
04447
04448
04449
04450
04451
04452
04453
04454
04455
04456 if ( incomingBitStream.ReadCompressed( rpcParms.numberOfBitsOfData ) == false )
04457 {
04458 #ifdef _DEBUG
04459 RakAssert( 0 );
04460 #endif
04461 return false;
04462 }
04463
04464 if ( incomingBitStream.Read( networkIDIsEncoded ) == false )
04465 {
04466 #ifdef _DEBUG
04467 RakAssert( 0 );
04468 #endif
04469 return false;
04470 }
04471
04472 if (networkIDIsEncoded)
04473 {
04474 if ( incomingBitStream.Read( networkID ) == false )
04475 {
04476 #ifdef _DEBUG
04477 RakAssert( 0 );
04478 #endif
04479 return false;
04480 }
04481 }
04482
04483 if (rpcIndex==UNDEFINED_RPC_INDEX)
04484 {
04485
04486 RakAssert(0);
04487 return false;
04488 }
04489
04490 node = rpcMap.GetNodeFromIndex(rpcIndex);
04491 if (node==0)
04492 {
04493 #ifdef _DEBUG
04494 RakAssert( 0 );
04495 #endif
04496 return false;
04497 }
04498
04499
04500 if (node->isPointerToMember==true && networkIDIsEncoded==false)
04501 {
04502
04503
04504
04505 RakAssert(0);
04506 return false;
04507 }
04508
04509 if (node->isPointerToMember==false && networkIDIsEncoded==true)
04510 {
04511
04512
04513
04514 RakAssert(0);
04515 return false;
04516 }
04517
04518 if (nameIsEncoded && GetRemoteSystemFromSystemAddress(systemAddress, false, true))
04519 {
04520
04521 RakNet::BitStream rpcMapBitStream;
04522 rpcMapBitStream.Write((MessageID)ID_RPC_MAPPING);
04523 stringCompressor->EncodeString(node->uniqueIdentifier, 256, &rpcMapBitStream);
04524 rpcMapBitStream.WriteCompressed(rpcIndex);
04525 SendBuffered( (const char*)rpcMapBitStream.GetData(), rpcMapBitStream.GetNumberOfBitsUsed(), HIGH_PRIORITY, UNRELIABLE, 0, systemAddress, false, RemoteSystemStruct::NO_ACTION, 0 );
04526 }
04527
04528 rpcParms.functionName=node->uniqueIdentifier;
04529
04530
04531 if ( rpcParms.numberOfBitsOfData == 0 )
04532 {
04533 rpcParms.input=0;
04534 if (networkIDIsEncoded)
04535 {
04536
04537 RakAssert(networkIDManager);
04538 if (networkIDManager)
04539 {
04540 void *object = networkIDManager->GET_OBJECT_FROM_ID(networkID);
04541 if (object)
04542 (node->memberFunctionPointer(object, &rpcParms));
04543 }
04544 }
04545 else
04546 {
04547 node->staticFunctionPointer( &rpcParms );
04548 }
04549 }
04550 else
04551 {
04552 if ( incomingBitStream.GetNumberOfUnreadBits() == 0 )
04553 {
04554 #ifdef _DEBUG
04555 RakAssert( 0 );
04556 #endif
04557 return false;
04558 }
04559
04560
04561 bool usedAlloca=false;
04562 #if !defined(_XBOX) && !defined(X360)
04563 if (BITS_TO_BYTES( incomingBitStream.GetNumberOfUnreadBits() ) < MAX_ALLOCA_STACK_ALLOCATION)
04564 {
04565 userData = ( unsigned char* ) alloca( (size_t) BITS_TO_BYTES( incomingBitStream.GetNumberOfUnreadBits() ) );
04566 usedAlloca=true;
04567 }
04568 else
04569 #endif
04570 userData = (unsigned char*) rakMalloc_Ex((size_t) BITS_TO_BYTES(incomingBitStream.GetNumberOfUnreadBits()), __FILE__, __LINE__);
04571
04572
04573
04574
04575
04576 if ( incomingBitStream.ReadBits( ( unsigned char* ) userData, rpcParms.numberOfBitsOfData, false ) == false )
04577 {
04578 #ifdef _DEBUG
04579 RakAssert( 0 );
04580 #endif
04581 #if defined(_XBOX) || defined(X360)
04582
04583 #endif
04584
04585 return false;
04586 }
04587
04588
04589
04590
04591
04592 rpcParms.input=userData;
04593 if (networkIDIsEncoded)
04594 {
04595
04596 RakAssert(networkIDManager);
04597 if (networkIDManager)
04598 {
04599 void *object = networkIDManager->GET_OBJECT_FROM_ID(networkID);
04600 if (object)
04601 (node->memberFunctionPointer(object, &rpcParms));
04602 }
04603 }
04604 else
04605 {
04606 node->staticFunctionPointer( &rpcParms );
04607 }
04608
04609
04610 if (usedAlloca==false)
04611 rakFree_Ex(userData, __FILE__, __LINE__ );
04612 }
04613
04614 if (blockingCommand)
04615 {
04616 RakNet::BitStream reply;
04617 reply.Write((MessageID)ID_RPC_REPLY);
04618 reply.Write((char*)replyToSender.GetData(), replyToSender.GetNumberOfBytesUsed());
04619 Send(&reply, HIGH_PRIORITY, RELIABLE, 0, systemAddress, false);
04620 }
04621
04622 return true;
04623 }
04624
04625
04635
04636 void RakPeer::HandleRPCReplyPacket( const char *data, int length, SystemAddress systemAddress )
04637 {
04638 if (blockOnRPCReply)
04639 {
04640 if ((systemAddress==replyFromTargetPlayer && replyFromTargetBroadcast==false) ||
04641 (systemAddress!=replyFromTargetPlayer && replyFromTargetBroadcast==true))
04642 {
04643 replyFromTargetBS->Write(data+1, length-1);
04644 blockOnRPCReply=false;
04645 }
04646 }
04647 }
04648
04649 bool RakPeer::IsLoopbackAddress(const AddressOrGUID &systemIdentifier, bool matchPort) const
04650 {
04651 if (systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID)
04652 return systemIdentifier.rakNetGuid==myGuid;
04653
04654 const SystemAddress sa = systemIdentifier.systemAddress;
04655
04656
04657 char str[64];
04658 sa.ToString(false,str);
04659 #if !defined(_XBOX) && !defined(X360)
04660 bool isLoopback=strcmp(str,"127.0.0.1")==0;
04661 if (matchPort==false && isLoopback)
04662 return true;
04663 if (matchPort==false)
04664 {
04665 for (int ipIndex=0; ipIndex < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ipIndex++)
04666 if (mySystemAddress[ipIndex].binaryAddress==sa.binaryAddress)
04667 return true;
04668 }
04669 else
04670 {
04671 for (int ipIndex=0; ipIndex < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ipIndex++)
04672 if (mySystemAddress[ipIndex]==sa ||
04673 (isLoopback && sa.port==mySystemAddress[ipIndex].port))
04674 return true;
04675 }
04676 #else
04677 bool isLoopback=strcmp(str,"2130706433")==0;
04678 if (isLoopback)
04679 {
04680 if (matchPort==false)
04681 {
04682 return true;
04683 }
04684 else
04685 {
04686 for (int ipIndex=0; ipIndex < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ipIndex++)
04687 if (mySystemAddress[ipIndex]==sa ||
04688 (isLoopback && sa.port==mySystemAddress[ipIndex].port))
04689 return true;
04690 }
04691 }
04692 #endif
04693 return sa==firstExternalID;
04694 }
04695
04696 SystemAddress RakPeer::GetLoopbackAddress(void) const
04697 {
04698 #if !defined(_XBOX) && !defined(X360)
04699 return mySystemAddress[0];
04700 #else
04701 return firstExternalID;
04702 #endif
04703 }
04704
04705 void RakPeer::GenerateSYNCookieRandomNumber( void )
04706 {
04707 #if !defined(_XBOX) && !defined(_WIN32_WCE) && !defined(X360)
04708 unsigned int number;
04709 int i;
04710 memcpy( oldRandomNumber, newRandomNumber, sizeof( newRandomNumber ) );
04711
04712 for ( i = 0; i < (int) sizeof( newRandomNumber ); i += (int) sizeof( number ) )
04713 {
04714 number = randomMT();
04715 memcpy( newRandomNumber + i, ( char* ) & number, sizeof( number ) );
04716 }
04717
04718 randomNumberExpirationTime = RakNet::GetTime() + SYN_COOKIE_OLD_RANDOM_NUMBER_DURATION;
04719 #endif
04720 }
04721
04722
04723 void RakPeer::SecuredConnectionResponse( const SystemAddress systemAddress )
04724 {
04725 #if !defined(_XBOX) && !defined(_WIN32_WCE) && !defined(X360)
04726 CSHA1 sha1;
04727
04728
04729
04730 uint32_t modulus[RAKNET_RSA_FACTOR_LIMBS];
04731 uint32_t e;
04732 unsigned char connectionRequestResponse[ 1 + sizeof( e ) + sizeof( modulus ) + 20 ];
04733 connectionRequestResponse[ 0 ] = ID_SECURED_CONNECTION_RESPONSE;
04734
04735 if ( randomNumberExpirationTime < RakNet::GetTime() )
04736 GenerateSYNCookieRandomNumber();
04737
04738
04739
04740 sha1.Reset();
04741 sha1.Update( ( unsigned char* ) & systemAddress.binaryAddress, sizeof( systemAddress.binaryAddress ) );
04742 sha1.Update( ( unsigned char* ) & systemAddress.port, sizeof( systemAddress.port ) );
04743 sha1.Update( ( unsigned char* ) & ( newRandomNumber ), sizeof(newRandomNumber) );
04744 sha1.Final();
04745
04746
04747 memcpy( connectionRequestResponse + 1, sha1.GetHash(), 20 );
04748
04749
04750 e = rsacrypt.getPublicExponent();
04751
04752 rsacrypt.getPublicModulus(modulus);
04753
04754
04755 if (RakNet::BitStream::DoEndianSwap())
04756 {
04757 RakNet::BitStream::ReverseBytesInPlace(( unsigned char* ) & e, sizeof(e));
04758 for (int i=0; i < RAKNET_RSA_FACTOR_LIMBS; i++)
04759 RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &modulus[i], sizeof(modulus[i]));
04760 }
04761
04762 memcpy( connectionRequestResponse + 1 + 20, ( char* ) & e, sizeof( e ) );
04763 memcpy( connectionRequestResponse + 1 + 20 + sizeof( e ), modulus, sizeof( modulus ) );
04764
04765
04766
04767
04768
04769
04770
04771
04772
04773
04774
04775
04776
04777
04778
04779
04780
04781
04782 SendImmediate(( char* ) connectionRequestResponse, (1 + sizeof( e ) + sizeof( modulus ) + 20) * 8, IMMEDIATE_PRIORITY, UNRELIABLE, 0, systemAddress, false, false, RakNet::GetTimeNS(), 0);
04783 #endif
04784 }
04785
04786 void RakPeer::SecuredConnectionConfirmation( RakPeer::RemoteSystemStruct * remoteSystem, char* data )
04787 {
04788 #if !defined(_XBOX) && !defined(_WIN32_WCE) && !defined(X360)
04789 int i, j;
04790 unsigned char randomNumber[ 20 ];
04791 unsigned int number;
04792
04793 Packet *packet;
04794
04795
04796
04797 uint32_t e;
04798 uint32_t n[RAKNET_RSA_FACTOR_LIMBS], message[RAKNET_RSA_FACTOR_LIMBS], encryptedMessage[RAKNET_RSA_FACTOR_LIMBS];
04799 RSACrypt privKeyPncrypt;
04800
04801
04802 if (remoteSystem->connectMode!=RemoteSystemStruct::REQUESTED_CONNECTION)
04803 return;
04804
04805
04806
04807
04808
04809
04810
04811
04812
04813
04814
04815
04816 memcpy( ( char* ) & e, data + 1 + 20, sizeof( e ) );
04817 memcpy( n, data + 1 + 20 + sizeof( e ), sizeof( n ) );
04818
04819 if (RakNet::BitStream::DoEndianSwap())
04820 {
04821 RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &e, sizeof(e));
04822 for (int i=0; i < RAKNET_RSA_FACTOR_LIMBS; i++)
04823 RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &n[i], sizeof(n[i]));
04824 }
04825
04826
04827
04828 if ( usingSecurity == true && keysLocallyGenerated == false )
04829 {
04830 if ( memcmp( ( char* ) & e, ( char* ) & publicKeyE, sizeof( e ) ) != 0 ||
04831 memcmp( n, publicKeyN, sizeof( n ) ) != 0 )
04832 {
04833 packet=AllocPacket(1, __FILE__, __LINE__);
04834 packet->data[ 0 ] = ID_RSA_PUBLIC_KEY_MISMATCH;
04835 packet->bitSize = sizeof( char ) * 8;
04836 packet->systemAddress = remoteSystem->systemAddress;
04837 packet->systemAddress.systemIndex = ( SystemIndex ) GetIndexFromSystemAddress( packet->systemAddress, true );
04838 packet->guid.systemIndex=packet->systemAddress.systemIndex;
04839 AddPacketToProducer(packet);
04840 remoteSystem->connectMode=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY;
04841 return;
04842 }
04843 }
04844
04845
04846 for ( i = 0; i < (int) sizeof( randomNumber ); i += (int) sizeof( number ) )
04847 {
04848 number = randomMT();
04849 memcpy( randomNumber + i, ( char* ) & number, sizeof( number ) );
04850 }
04851
04852 memset( message, 0, sizeof( message ) );
04853 RakAssert( sizeof( message ) >= sizeof( randomNumber ) );
04854
04855
04856
04857
04858
04859
04860
04861 memcpy( message, randomNumber, sizeof( randomNumber ) );
04862
04863
04864
04865
04866
04867
04868
04869
04870
04871
04872
04873 privKeyPncrypt.setPublicKey( n, RAKNET_RSA_FACTOR_LIMBS, e );
04874
04875
04876
04877
04878 privKeyPncrypt.encrypt( encryptedMessage, message );
04879
04880
04881
04882
04883 if (RakNet::BitStream::DoEndianSwap())
04884 {
04885 for (int i=0; i < RAKNET_RSA_FACTOR_LIMBS; i++)
04886 RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &encryptedMessage[i], sizeof(encryptedMessage[i]));
04887 }
04888
04889
04890
04891
04892
04893
04894
04895
04896
04897
04898
04899
04900
04901
04902
04903
04904
04905
04906
04907
04908
04909
04910
04911
04912
04913
04914
04915
04916
04917
04918
04919
04920 for ( j = 0; j < 16; j++ )
04921 remoteSystem->AESKey[ j ] = data[ 1 + j ] ^ randomNumber[ j ];
04922 remoteSystem->setAESKey = true;
04923
04924
04925
04926
04927
04928
04929
04930
04931
04932
04933
04934
04935
04936
04937
04938
04939
04940 char reply[ 1 + 20 + sizeof( uint32_t ) * RAKNET_RSA_FACTOR_LIMBS + sizeof(RakNetTime) ];
04941
04942 reply[ 0 ] = ID_SECURED_CONNECTION_CONFIRMATION;
04943 memcpy( reply + 1, data + 1, 20 );
04944 memcpy( reply + 1 + 20, encryptedMessage, sizeof( encryptedMessage ) );
04945 RakNet::BitStream bsTimestamp;
04946 bsTimestamp.Write(RakNet::GetTime());
04947 memcpy( reply + 1 + 20 + sizeof( uint32_t ) * RAKNET_RSA_FACTOR_LIMBS, bsTimestamp.GetData(), bsTimestamp.GetNumberOfBytesUsed() );
04948
04949
04950
04951 SendImmediate((char*)reply, (1 + 20 + sizeof(uint32_t) * RAKNET_RSA_FACTOR_LIMBS + sizeof(RakNetTime) ) * 8 , IMMEDIATE_PRIORITY, UNRELIABLE, 0, remoteSystem->systemAddress, false, false, RakNet::GetTimeNS(), 0);
04952
04953 #endif
04954 }
04955
04956 bool RakPeer::AllowIncomingConnections(void) const
04957 {
04958 return GetNumberOfRemoteInitiatedConnections() < GetMaximumIncomingConnections();
04959 }
04960
04961 void RakPeer::PingInternal( const SystemAddress target, bool performImmediate, PacketReliability reliability )
04962 {
04963 if ( IsActive() == false )
04964 return ;
04965
04966 RakNet::BitStream bitStream(sizeof(unsigned char)+sizeof(RakNetTime));
04967 bitStream.Write((MessageID)ID_INTERNAL_PING);
04968 RakNetTimeUS currentTimeNS = RakNet::GetTimeNS();
04969 RakNetTime currentTime = RakNet::GetTime();
04970 bitStream.Write(currentTime);
04971 if (performImmediate)
04972 SendImmediate( (char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, reliability, 0, target, false, false, currentTimeNS, 0 );
04973 else
04974 Send( &bitStream, IMMEDIATE_PRIORITY, reliability, 0, target, false );
04975 }
04976
04977 void RakPeer::CloseConnectionInternal( const AddressOrGUID& systemIdentifier, bool sendDisconnectionNotification, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority )
04978 {
04979 #ifdef _DEBUG
04980 RakAssert(orderingChannel < 32);
04981 #endif
04982
04983 if (systemIdentifier.IsUndefined())
04984 return;
04985
04986 if ( remoteSystemList == 0 || endThreads == true )
04987 return;
04988
04989 SystemAddress target;
04990 if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
04991 {
04992 target=systemIdentifier.systemAddress;
04993 }
04994 else
04995 {
04996 target=GetSystemAddressFromGuid(systemIdentifier.rakNetGuid);
04997 }
04998
04999 if (sendDisconnectionNotification)
05000 {
05001 NotifyAndFlagForShutdown(target, performImmediate, orderingChannel, disconnectionNotificationPriority);
05002 }
05003 else
05004 {
05005 if (performImmediate)
05006 {
05007 unsigned int index = GetRemoteSystemIndex(target);
05008 if (index!=(unsigned int) -1)
05009 {
05010 if ( remoteSystemList[index].isActive )
05011 {
05012
05013 remoteSystemList[index].isActive = false;
05014
05015 remoteSystemList[index].guid=UNASSIGNED_RAKNET_GUID;
05016
05017
05018
05019
05020
05021 remoteSystemList[index].reliabilityLayer.Reset(false, remoteSystemList[index].MTUSize);
05022
05023
05024 remoteSystemList[index].rakNetSocket.SetNull();
05025 }
05026 }
05027 }
05028 else
05029 {
05030 BufferedCommandStruct *bcs;
05031 #ifdef _RAKNET_THREADSAFE
05032 bcs=bufferedCommands.Allocate( __FILE__, __LINE__ );
05033 bcs->command=BufferedCommandStruct::BCS_CLOSE_CONNECTION;
05034 bcs->systemIdentifier=target;
05035 bcs->data=0;
05036 bcs->orderingChannel=orderingChannel;
05037 bcs->priority=disconnectionNotificationPriority;
05038 bufferedCommands.Push(bcs);
05039 #else
05040 bcs=bufferedCommands.WriteLock();
05041 bcs->command=BufferedCommandStruct::BCS_CLOSE_CONNECTION;
05042 bcs->systemIdentifier=target;
05043 bcs->data=0;
05044 bcs->orderingChannel=orderingChannel;
05045 bcs->priority=disconnectionNotificationPriority;
05046 bufferedCommands.WriteUnlock();
05047 #endif
05048 }
05049 }
05050 }
05051
05052 bool RakPeer::ValidSendTarget(SystemAddress systemAddress, bool broadcast)
05053 {
05054 unsigned remoteSystemIndex;
05055
05056
05057 for ( remoteSystemIndex = 0; remoteSystemIndex < maximumNumberOfPeers; remoteSystemIndex++ )
05058
05059 {
05060 if ( remoteSystemList[ remoteSystemIndex ].isActive &&
05061 remoteSystemList[ remoteSystemIndex ].connectMode==RakPeer::RemoteSystemStruct::CONNECTED &&
05062 ( ( broadcast == false && remoteSystemList[ remoteSystemIndex ].systemAddress == systemAddress ) ||
05063 ( broadcast == true && remoteSystemList[ remoteSystemIndex ].systemAddress != systemAddress ) )
05064 )
05065 return true;
05066 }
05067
05068 return false;
05069 }
05070
05071 void RakPeer::SendBuffered( const char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt )
05072 {
05073 BufferedCommandStruct *bcs;
05074
05075 #ifdef _RAKNET_THREADSAFE
05076 bcs=bufferedCommands.Allocate( __FILE__, __LINE__ );
05077 #else
05078 bcs=bufferedCommands.WriteLock();
05079 #endif
05080 bcs->data = (char*) rakMalloc_Ex( (size_t) BITS_TO_BYTES(numberOfBitsToSend), __FILE__, __LINE__ );
05081 if (bcs->data==0)
05082 {
05083 notifyOutOfMemory(__FILE__, __LINE__);
05084 #ifdef _RAKNET_THREADSAFE
05085 bufferedCommands.Deallocate(bcs, __FILE__,__LINE__);
05086 #else
05087 bufferedCommands.WriteUnlock();
05088 #endif
05089 return;
05090 }
05091 memcpy(bcs->data, data, (size_t) BITS_TO_BYTES(numberOfBitsToSend));
05092 bcs->numberOfBitsToSend=numberOfBitsToSend;
05093 bcs->priority=priority;
05094 bcs->reliability=reliability;
05095 bcs->orderingChannel=orderingChannel;
05096 bcs->systemIdentifier=systemIdentifier;
05097 bcs->broadcast=broadcast;
05098 bcs->connectionMode=connectionMode;
05099 bcs->receipt=receipt;
05100 bcs->command=BufferedCommandStruct::BCS_SEND;
05101 #ifdef _RAKNET_THREADSAFE
05102 bufferedCommands.Push(bcs);
05103 #else
05104 bufferedCommands.WriteUnlock();
05105 #endif
05106
05107 if (priority==IMMEDIATE_PRIORITY)
05108 {
05109
05110 quitAndDataEvents.SetEvent();
05111 }
05112 }
05113
05114 void RakPeer::SendBufferedList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt )
05115 {
05116 BufferedCommandStruct *bcs;
05117 unsigned int totalLength=0;
05118 unsigned int lengthOffset;
05119 int i;
05120 for (i=0; i < numParameters; i++)
05121 {
05122 if (lengths[i]>0)
05123 totalLength+=lengths[i];
05124 }
05125 if (totalLength==0)
05126 return;
05127
05128 char *dataAggregate;
05129 dataAggregate = (char*) rakMalloc_Ex( (size_t) totalLength, __FILE__, __LINE__ );
05130 if (dataAggregate==0)
05131 {
05132 notifyOutOfMemory(__FILE__, __LINE__);
05133 return;
05134 }
05135 for (i=0, lengthOffset=0; i < numParameters; i++)
05136 {
05137 if (lengths[i]>0)
05138 {
05139 memcpy(dataAggregate+lengthOffset, data[i], lengths[i]);
05140 lengthOffset+=lengths[i];
05141 }
05142 }
05143
05144 if (broadcast==false && IsLoopbackAddress(systemIdentifier.systemAddress,true))
05145 {
05146 SendLoopback(dataAggregate,totalLength);
05147 rakFree_Ex(dataAggregate,__FILE__,__LINE__);
05148 return;
05149 }
05150
05151 #ifdef _RAKNET_THREADSAFE
05152 bcs=bufferedCommands.Allocate( __FILE__, __LINE__ );
05153 #else
05154 bcs=bufferedCommands.WriteLock();
05155 #endif
05156 bcs->data = dataAggregate;
05157 bcs->numberOfBitsToSend=BYTES_TO_BITS(totalLength);
05158 bcs->priority=priority;
05159 bcs->reliability=reliability;
05160 bcs->orderingChannel=orderingChannel;
05161 bcs->systemIdentifier=systemIdentifier;
05162 bcs->broadcast=broadcast;
05163 bcs->connectionMode=connectionMode;
05164 bcs->receipt=receipt;
05165 bcs->command=BufferedCommandStruct::BCS_SEND;
05166 #ifdef _RAKNET_THREADSAFE
05167 bufferedCommands.Push(bcs);
05168 #else
05169 bufferedCommands.WriteUnlock();
05170 #endif
05171
05172 if (priority==IMMEDIATE_PRIORITY)
05173 {
05174
05175 quitAndDataEvents.SetEvent();
05176 }
05177 }
05178
05179 bool RakPeer::SendImmediate( char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, bool useCallerDataAllocation, RakNetTimeUS currentTime, uint32_t receipt )
05180 {
05181 unsigned *sendList;
05182 unsigned sendListSize;
05183 bool callerDataAllocationUsed;
05184 unsigned int remoteSystemIndex, sendListIndex;
05185 unsigned numberOfBytesUsed = (unsigned) BITS_TO_BYTES(numberOfBitsToSend);
05186 callerDataAllocationUsed=false;
05187
05188 sendListSize=0;
05189
05190 if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
05191 remoteSystemIndex=GetIndexFromSystemAddress( systemIdentifier.systemAddress, true );
05192 else if (systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID)
05193 remoteSystemIndex=GetSystemIndexFromGuid(systemIdentifier.rakNetGuid);
05194 else
05195 remoteSystemIndex=(unsigned int) -1;
05196
05197
05198 if (broadcast==false)
05199 {
05200 if (remoteSystemIndex==(unsigned int) -1)
05201 {
05202 #ifdef _DEBUG
05203
05204 #endif
05205 return false;
05206 }
05207
05208 #if !defined(_XBOX) && !defined(X360)
05209 sendList=(unsigned *)alloca(sizeof(unsigned));
05210 #else
05211 sendList = (unsigned *) rakMalloc_Ex(sizeof(unsigned), __FILE__, __LINE__);
05212 #endif
05213
05214 if (remoteSystemList[remoteSystemIndex].isActive &&
05215 remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ASAP &&
05216 remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY &&
05217 remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ON_NO_ACK)
05218 {
05219 sendList[0]=remoteSystemIndex;
05220 sendListSize=1;
05221 }
05222 }
05223 else
05224 {
05225 #if !defined(_XBOX) && !defined(X360)
05226
05227 sendList=(unsigned *)alloca(sizeof(unsigned)*maximumNumberOfPeers);
05228 #else
05229
05230 sendList = (unsigned *) rakMalloc_Ex(sizeof(unsigned)*maximumNumberOfPeers, __FILE__, __LINE__);
05231 #endif
05232
05233
05234 unsigned int idx;
05235 for ( idx = 0; idx < maximumNumberOfPeers; idx++ )
05236 {
05237 if (remoteSystemIndex!=(unsigned int) -1 && idx==remoteSystemIndex)
05238 continue;
05239
05240 if ( remoteSystemList[ idx ].isActive && remoteSystemList[ idx ].systemAddress != UNASSIGNED_SYSTEM_ADDRESS )
05241 sendList[sendListSize++]=idx;
05242 }
05243 }
05244
05245 if (sendListSize==0)
05246 {
05247 #if defined(_XBOX) && !defined(X360)
05248
05249 #endif
05250 return false;
05251 }
05252
05253 for (sendListIndex=0; sendListIndex < sendListSize; sendListIndex++)
05254 {
05255 if ( trackFrequencyTable )
05256 {
05257 unsigned i;
05258
05259 for (i=0 ; i < numberOfBytesUsed; i++ )
05260 frequencyTable[ (unsigned char)(data[i]) ]++;
05261 }
05262
05263 if ( outputTree )
05264 {
05265 RakNet::BitStream bitStreamCopy( numberOfBytesUsed );
05266 outputTree->EncodeArray( (unsigned char*) data, numberOfBytesUsed, &bitStreamCopy );
05267 rawBytesSent += numberOfBytesUsed;
05268 compressedBytesSent += (unsigned int) bitStreamCopy.GetNumberOfBytesUsed();
05269 remoteSystemList[sendList[sendListIndex]].reliabilityLayer.Send( (char*) bitStreamCopy.GetData(), bitStreamCopy.GetNumberOfBitsUsed(), priority, reliability, orderingChannel, true, remoteSystemList[sendList[sendListIndex]].MTUSize, currentTime, receipt );
05270 }
05271 else
05272 {
05273
05274 bool useData = useCallerDataAllocation && callerDataAllocationUsed==false && sendListIndex+1==sendListSize;
05275 remoteSystemList[sendList[sendListIndex]].reliabilityLayer.Send( data, numberOfBitsToSend, priority, reliability, orderingChannel, useData==false, remoteSystemList[sendList[sendListIndex]].MTUSize, currentTime, receipt );
05276 if (useData)
05277 callerDataAllocationUsed=true;
05278 }
05279
05280 if (reliability==RELIABLE ||
05281 reliability==RELIABLE_ORDERED ||
05282 reliability==RELIABLE_SEQUENCED ||
05283 reliability==RELIABLE_WITH_ACK_RECEIPT ||
05284 reliability==RELIABLE_ORDERED_WITH_ACK_RECEIPT
05285
05286
05287 )
05288 remoteSystemList[sendList[sendListIndex]].lastReliableSend=(RakNetTime)(currentTime/(RakNetTimeUS)1000);
05289 }
05290
05291 #if defined(_XBOX) && !defined(X360)
05292
05293 #endif
05294
05295
05296 return callerDataAllocationUsed;
05297 }
05298
05299 void RakPeer::ResetSendReceipt(void)
05300 {
05301 sendReceiptSerialMutex.Lock();
05302 sendReceiptSerial=1;
05303 sendReceiptSerialMutex.Unlock();
05304 }
05305
05306 void RakPeer::OnConnectedPong(RakNetTime sendPingTime, RakNetTime sendPongTime, RemoteSystemStruct *remoteSystem)
05307 {
05308 RakNetTime ping, lastPing;
05309 RakNetTimeUS timeNS = RakNet::GetTimeNS();
05310 RakNetTimeMS timeMS = (RakNetTime)(timeNS/(RakNetTimeUS)1000);
05311 if (timeMS > sendPingTime)
05312 ping = timeMS - sendPingTime;
05313 else
05314 ping=0;
05315 lastPing = remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex ].pingTime;
05316
05317
05318 if ( lastPing <= 0 || ( ( ping < ( lastPing * 3 ) ) && ping < 1200 ) )
05319 {
05320 remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex ].pingTime = ( unsigned short ) ping;
05321
05322
05323 remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex ].clockDifferential = sendPongTime - ( timeMS/2 + sendPingTime/2 );
05324
05325 if ( remoteSystem->lowestPing == (unsigned short)-1 || remoteSystem->lowestPing > (int) ping )
05326 remoteSystem->lowestPing = (unsigned short) ping;
05327
05328
05329
05330
05331
05332
05333 if ( ++( remoteSystem->pingAndClockDifferentialWriteIndex ) == PING_TIMES_ARRAY_SIZE )
05334 remoteSystem->pingAndClockDifferentialWriteIndex = 0;
05335 }
05336 }
05337
05338 void RakPeer::ClearBufferedCommands(void)
05339 {
05340 BufferedCommandStruct *bcs;
05341
05342 #ifdef _RAKNET_THREADSAFE
05343 while ((bcs=bufferedCommands.Pop())!=0)
05344 {
05345 if (bcs->data)
05346 rakFree_Ex(bcs->data, __FILE__, __LINE__ );
05347
05348 bufferedCommands.Deallocate(bcs, __FILE__,__LINE__);
05349 }
05350 bufferedCommands.Clear(__FILE__, __LINE__);
05351 #else
05352 while ((bcs=bufferedCommands.ReadLock())!=0)
05353 {
05354 if (bcs->data)
05355 rakFree_Ex(bcs->data, __FILE__, __LINE__ );
05356
05357 bufferedCommands.ReadUnlock();
05358 }
05359 bufferedCommands.Clear(__FILE__, __LINE__);
05360 #endif
05361 }
05362
05363 void RakPeer::ClearBufferedPackets(void)
05364 {
05365 RecvFromStruct *bcs;
05366
05367 #ifdef _RAKNET_THREADSAFE
05368 while ((bcs=bufferedPackets.Pop())!=0)
05369 {
05370 bufferedPackets.Deallocate(bcs, __FILE__,__LINE__);
05371 }
05372 bufferedPackets.Clear(__FILE__, __LINE__);
05373 #else
05374 while ((bcs=bufferedPackets.ReadLock())!=0)
05375 {
05376 bufferedPackets.ReadUnlock();
05377 }
05378 bufferedPackets.Clear(__FILE__, __LINE__);
05379 #endif
05380 }
05381
05382 void RakPeer::ClearSocketQueryOutput(void)
05383 {
05384 socketQueryOutput.Clear(__FILE__, __LINE__);
05385 }
05386
05387 void RakPeer::ClearRequestedConnectionList(void)
05388 {
05389 DataStructures::Queue<RequestedConnectionStruct*> freeQueue;
05390 requestedConnectionQueueMutex.Lock();
05391 while (requestedConnectionQueue.Size())
05392 freeQueue.Push(requestedConnectionQueue.Pop(), __FILE__, __LINE__ );
05393 requestedConnectionQueueMutex.Unlock();
05394 unsigned i;
05395 for (i=0; i < freeQueue.Size(); i++)
05396 RakNet::OP_DELETE(freeQueue[i], __FILE__, __LINE__ );
05397 }
05398 inline void RakPeer::AddPacketToProducer(Packet *p)
05399 {
05400 packetReturnMutex.Lock();
05401 packetReturnQueue.Push(p,__FILE__,__LINE__);
05402 packetReturnMutex.Unlock();
05403 }
05404
05405 void RakPeer::GenerateGUID(void)
05406 {
05407
05408 #if defined(_XBOX) || defined(X360)
05409
05410 #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
05411
05412 #elif defined(_WIN32)
05413 myGuid.g=RakNet::GetTimeUS();
05414
05415 RakNetTimeUS lastTime, thisTime;
05416 int j;
05417
05418 for (j=0; j < 8; j++)
05419 {
05420 lastTime = RakNet::GetTimeUS();
05421 RakSleep(1);
05422 RakSleep(0);
05423 thisTime = RakNet::GetTimeUS();
05424 RakNetTimeUS diff = thisTime-lastTime;
05425 unsigned int diff4Bits = (unsigned int) (diff & 15);
05426 diff4Bits <<= 32-4;
05427 diff4Bits >>= j*4;
05428 ((char*)&myGuid.g)[j] ^= diff4Bits;
05429 }
05430
05431 #else
05432 struct timeval tv;
05433 gettimeofday(&tv, NULL);
05434 myGuid.g=tv.tv_usec + tv.tv_sec * 1000000;
05435 #endif
05436 }
05437
05438 void ProcessPortUnreachable( unsigned int binaryAddress, unsigned short port, RakPeer *rakPeer )
05439 {
05440 (void) binaryAddress;
05441 (void) port;
05442 (void) rakPeer;
05443
05444 }
05445
05446 bool ProcessOfflineNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSmartPtr<RakNetSocket> rakNetSocket, bool *isOfflineMessage, RakNetTimeUS timeRead )
05447 {
05448 (void) timeRead;
05449 RakPeer::RemoteSystemStruct *remoteSystem;
05450 Packet *packet;
05451 unsigned i;
05452
05453 #if !defined(_XBOX) && !defined(X360)
05454 char str1[64];
05455 systemAddress.ToString(false, str1);
05456 if (rakPeer->IsBanned( str1 ))
05457 {
05458 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05459 rakPeer->messageHandlerList[i]->OnDirectSocketReceive(data, length*8, systemAddress);
05460
05461 RakNet::BitStream bs;
05462 bs.Write((MessageID)ID_CONNECTION_BANNED);
05463 bs.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
05464 bs.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS));
05465
05466 unsigned i;
05467 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05468 rakPeer->messageHandlerList[i]->OnDirectSocketSend((char*) bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress);
05469 SocketLayer::Instance()->SendTo( rakNetSocket->s, (char*) bs.GetData(), bs.GetNumberOfBytesUsed(), systemAddress.binaryAddress, systemAddress.port, rakNetSocket->remotePortRakNetWasStartedOn_PS3 );
05470
05471 return true;
05472 }
05473 #endif
05474
05475
05476
05477
05478 if (length <=2)
05479 {
05480 *isOfflineMessage=true;
05481 }
05482 else if (
05483 ((unsigned char)data[0] == ID_PING ||
05484 (unsigned char)data[0] == ID_PING_OPEN_CONNECTIONS) &&
05485 length == sizeof(unsigned char) + sizeof(RakNetTime) + sizeof(OFFLINE_MESSAGE_DATA_ID))
05486 {
05487 *isOfflineMessage=memcmp(data+sizeof(unsigned char) + sizeof(RakNetTime), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0;
05488 }
05489 else if ((unsigned char)data[0] == ID_PONG && (size_t) length >= sizeof(unsigned char) + sizeof(RakNetTime) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID))
05490 {
05491 *isOfflineMessage=memcmp(data+sizeof(unsigned char) + sizeof(RakNetTime) + RakNetGUID::size(), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0;
05492 }
05493 else if (
05494 (unsigned char)data[0] == ID_OUT_OF_BAND_INTERNAL &&
05495 (size_t) length >= sizeof(MessageID) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID))
05496 {
05497 *isOfflineMessage=memcmp(data+sizeof(MessageID) + RakNetGUID::size(), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0;
05498 }
05499 else if (
05500 (unsigned char)data[0] == ID_OPEN_CONNECTION_REQUEST &&
05501 (size_t) length >= sizeof(MessageID)*2 + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID))
05502 {
05503 *isOfflineMessage=memcmp(data+sizeof(MessageID)*2 + RakNetGUID::size(), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0;
05504 }
05505 else if (
05506 (
05507 (unsigned char)data[0] == ID_OPEN_CONNECTION_REPLY ||
05508 (unsigned char)data[0] == ID_CONNECTION_ATTEMPT_FAILED ||
05509 (unsigned char)data[0] == ID_NO_FREE_INCOMING_CONNECTIONS ||
05510 (unsigned char)data[0] == ID_CONNECTION_BANNED ||
05511 (unsigned char)data[0] == ID_ALREADY_CONNECTED ||
05512 (unsigned char)data[0] == ID_IP_RECENTLY_CONNECTED ||
05513 (unsigned char)data[0] == ID_CONNECTION_REQUEST) &&
05514 (size_t) length >= sizeof(MessageID) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID))
05515 {
05516 *isOfflineMessage=memcmp(data+sizeof(MessageID), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0;
05517 }
05518 else if (((unsigned char)data[0] == ID_INCOMPATIBLE_PROTOCOL_VERSION&&
05519 (size_t) length == sizeof(MessageID)*2 + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID)))
05520 {
05521 *isOfflineMessage=memcmp(data+sizeof(MessageID)*2, OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0;
05522 }
05523 else
05524 {
05525 *isOfflineMessage=false;
05526 }
05527
05528 if (*isOfflineMessage)
05529 {
05530 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05531 rakPeer->messageHandlerList[i]->OnDirectSocketReceive(data, length*8, systemAddress);
05532
05533
05534 if ( ( (unsigned char) data[ 0 ] == ID_PING_OPEN_CONNECTIONS
05535 || (unsigned char)(data)[0] == ID_PING) && length == sizeof(unsigned char)+sizeof(RakNetTime)+sizeof(OFFLINE_MESSAGE_DATA_ID) )
05536 {
05537 if ( (unsigned char)(data)[0] == ID_PING ||
05538 rakPeer->AllowIncomingConnections() )
05539 {
05540
05541 RakNet::BitStream inBitStream( (unsigned char *) data, length, false );
05542 inBitStream.IgnoreBits(8);
05543 RakNetTime sendPingTime;
05544 inBitStream.Read(sendPingTime);
05545
05546 RakNet::BitStream outBitStream;
05547 outBitStream.Write((MessageID)ID_PONG);
05548 outBitStream.Write(sendPingTime);
05549 outBitStream.Write(rakPeer->myGuid);
05550 outBitStream.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
05551
05552 rakPeer->rakPeerMutexes[ RakPeer::offlinePingResponse_Mutex ].Lock();
05553
05554 outBitStream.Write( (char*)rakPeer->offlinePingResponse.GetData(), rakPeer->offlinePingResponse.GetNumberOfBytesUsed() );
05555 rakPeer->rakPeerMutexes[ RakPeer::offlinePingResponse_Mutex ].Unlock();
05556
05557 unsigned i;
05558 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05559 rakPeer->messageHandlerList[i]->OnDirectSocketSend((const char*)outBitStream.GetData(), outBitStream.GetNumberOfBytesUsed(), systemAddress);
05560
05561 char str1[64];
05562 systemAddress.ToString(false, str1);
05563 SocketLayer::Instance()->SendTo( rakNetSocket->s, (const char*)outBitStream.GetData(), (unsigned int) outBitStream.GetNumberOfBytesUsed(), str1 , systemAddress.port, rakNetSocket->remotePortRakNetWasStartedOn_PS3 );
05564
05565 packet=rakPeer->AllocPacket(sizeof(MessageID), __FILE__, __LINE__);
05566 packet->data[0]=data[0];
05567 packet->systemAddress = systemAddress;
05568 packet->guid=UNASSIGNED_RAKNET_GUID;
05569 packet->systemAddress.systemIndex = ( SystemIndex ) rakPeer->GetIndexFromSystemAddress( systemAddress, true );
05570 packet->guid.systemIndex=packet->systemAddress.systemIndex;
05571 rakPeer->AddPacketToProducer(packet);
05572
05573 }
05574 }
05575
05576 else if ((unsigned char) data[ 0 ] == ID_PONG && (size_t) length >= sizeof(unsigned char)+sizeof(RakNetTime)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID) && (size_t) length < sizeof(unsigned char)+sizeof(RakNetTime)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID)+MAX_OFFLINE_DATA_LENGTH)
05577 {
05578 packet=rakPeer->AllocPacket((unsigned int) (length-sizeof(OFFLINE_MESSAGE_DATA_ID)-RakNetGUID::size()), __FILE__, __LINE__);
05579 RakNet::BitStream bs((unsigned char*) data, length, false);
05580 bs.IgnoreBytes(sizeof(unsigned char)+sizeof(RakNetTime));
05581 bs.Read(packet->guid);
05582 packet->data[0]=ID_PONG;
05583
05584 memcpy(packet->data+sizeof(unsigned char), data+sizeof(unsigned char), sizeof(RakNetTime));
05585
05586
05587
05588
05589 memcpy(packet->data+sizeof(unsigned char)+sizeof(RakNetTime), data+sizeof(unsigned char)+sizeof(RakNetTime)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID), length-sizeof(unsigned char)-sizeof(RakNetTime)-RakNetGUID::size()-sizeof(OFFLINE_MESSAGE_DATA_ID));
05590 packet->bitSize = BYTES_TO_BITS(packet->length);
05591 packet->systemAddress = systemAddress;
05592 packet->systemAddress.systemIndex = ( SystemIndex ) rakPeer->GetIndexFromSystemAddress( systemAddress, true );
05593 packet->guid.systemIndex=packet->systemAddress.systemIndex;
05594 rakPeer->AddPacketToProducer(packet);
05595 }
05596 else if ((unsigned char) data[ 0 ] == ID_OUT_OF_BAND_INTERNAL &&
05597 (size_t) length < MAX_OFFLINE_DATA_LENGTH+sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID)+RakNetGUID::size())
05598 {
05599 unsigned int dataLength = (unsigned int) (length-sizeof(OFFLINE_MESSAGE_DATA_ID)-RakNetGUID::size()-sizeof(MessageID));
05600 RakAssert(dataLength<1024);
05601 packet=rakPeer->AllocPacket(dataLength+sizeof(MessageID), __FILE__, __LINE__);
05602 RakAssert(packet->length<1024);
05603
05604 RakNet::BitStream bs2((unsigned char*) data, length, false);
05605 bs2.IgnoreBytes(sizeof(MessageID));
05606 bs2.Read(packet->guid);
05607
05608 if (data[sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID) + RakNetGUID::size()]==ID_ADVERTISE_SYSTEM)
05609 {
05610 packet->length--;
05611 packet->bitSize=BYTES_TO_BITS(packet->length);
05612 packet->data[0]=ID_ADVERTISE_SYSTEM;
05613 memcpy(packet->data+1, data+sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID)*2 + RakNetGUID::size(), dataLength);
05614 }
05615 else
05616 {
05617 packet->data[0]=ID_OUT_OF_BAND_INTERNAL;
05618 memcpy(packet->data+1, data+sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID) + RakNetGUID::size(), dataLength);
05619 }
05620
05621 packet->systemAddress = systemAddress;
05622 packet->systemAddress.systemIndex = ( SystemIndex ) rakPeer->GetIndexFromSystemAddress( systemAddress, true );
05623 packet->guid.systemIndex=packet->systemAddress.systemIndex;
05624 rakPeer->AddPacketToProducer(packet);
05625 }
05626 else if ((unsigned char)(data)[0] == (MessageID)ID_OPEN_CONNECTION_REPLY)
05627 {
05628 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05629 rakPeer->messageHandlerList[i]->OnDirectSocketReceive(data, length*8, systemAddress);
05630
05631 RakNet::BitStream bs((unsigned char*) data,length,false);
05632 bs.IgnoreBytes(sizeof(MessageID));
05633 bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID));
05634 RakNetGUID guid;
05635 bs.Read(guid);
05636 SystemAddress bindingAddress;
05637 bs.Read(bindingAddress);
05638
05639 RakPeer::RequestedConnectionStruct *rcs;
05640 bool unlock=true;
05641 unsigned i;
05642 rakPeer->requestedConnectionQueueMutex.Lock();
05643 for (i=0; i < rakPeer->requestedConnectionQueue.Size(); i++)
05644 {
05645 rcs=rakPeer->requestedConnectionQueue[i];
05646 if (rcs->systemAddress==systemAddress)
05647 {
05648 rakPeer->requestedConnectionQueueMutex.Unlock();
05649 unlock=false;
05650
05651 RakAssert(rcs->actionToTake==RakPeer::RequestedConnectionStruct::CONNECT);
05652
05653 bool thisIPConnectedRecently=false;
05654 remoteSystem=rakPeer->GetRemoteSystemFromSystemAddress( systemAddress, true, true );
05655
05656
05657 if (remoteSystem==0)
05658 {
05659
05660
05661 if (rcs->socket.IsNull())
05662 {
05663
05664
05665 remoteSystem=rakPeer->AssignSystemAddressToRemoteSystemList(systemAddress, RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER, rakNetSocket, &thisIPConnectedRecently, bindingAddress, length+UDP_HEADER_SIZE, guid);
05666 }
05667 else
05668 {
05669
05670
05671 remoteSystem=rakPeer->AssignSystemAddressToRemoteSystemList(systemAddress, RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER, rcs->socket, &thisIPConnectedRecently, bindingAddress, length+UDP_HEADER_SIZE, guid);
05672 }
05673
05674
05675
05676 }
05677
05678
05679
05680 if (thisIPConnectedRecently==false)
05681 {
05682
05683 if (remoteSystem)
05684 {
05685
05686 remoteSystem->weInitiatedTheConnection=true;
05687 remoteSystem->connectMode=RakPeer::RemoteSystemStruct::REQUESTED_CONNECTION;
05688 if (rcs->timeoutTime!=0)
05689 remoteSystem->reliabilityLayer.SetTimeoutTime(rcs->timeoutTime);
05690
05691
05692 RakNet::BitStream temp;
05693 temp.Write( (MessageID)ID_CONNECTION_REQUEST );
05694 temp.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
05695 temp.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS));
05696 temp.Write(RakNet::GetTime());
05697
05698 if ( rcs->outgoingPasswordLength > 0 )
05699 temp.Write( ( char* ) rcs->outgoingPassword, rcs->outgoingPasswordLength );
05700
05701 rakPeer->SendImmediate((char*)temp.GetData(), temp.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, timeRead, 0 );
05702 }
05703 else
05704 {
05705
05706 packet=rakPeer->AllocPacket(sizeof( char ), __FILE__, __LINE__);
05707 packet->data[ 0 ] = ID_CONNECTION_ATTEMPT_FAILED;
05708 packet->bitSize = ( sizeof( char ) * 8);
05709 packet->systemAddress = rcs->systemAddress;
05710 packet->guid=guid;
05711 rakPeer->AddPacketToProducer(packet);
05712 }
05713 }
05714
05715 rakPeer->requestedConnectionQueueMutex.Lock();
05716 for (unsigned int k=0; k < rakPeer->requestedConnectionQueue.Size(); k++)
05717 {
05718 if (rakPeer->requestedConnectionQueue[k]->systemAddress==systemAddress)
05719 {
05720 rakPeer->requestedConnectionQueue.RemoveAtIndex(k);
05721 break;
05722 }
05723 }
05724 rakPeer->requestedConnectionQueueMutex.Unlock();
05725
05726 RakNet::OP_DELETE(rcs,__FILE__,__LINE__);
05727
05728 break;
05729 }
05730 }
05731
05732 if (unlock)
05733 rakPeer->requestedConnectionQueueMutex.Unlock();
05734
05735 return true;
05736
05737 }
05738 else if ((unsigned char)(data)[0] == (MessageID)ID_CONNECTION_ATTEMPT_FAILED ||
05739 (unsigned char)(data)[0] == (MessageID)ID_NO_FREE_INCOMING_CONNECTIONS ||
05740 (unsigned char)(data)[0] == (MessageID)ID_CONNECTION_BANNED ||
05741 (unsigned char)(data)[0] == (MessageID)ID_ALREADY_CONNECTED ||
05742 (unsigned char)(data)[0] == (MessageID)ID_INVALID_PASSWORD ||
05743 (unsigned char)(data)[0] == (MessageID)ID_IP_RECENTLY_CONNECTED ||
05744 (unsigned char)(data)[0] == (MessageID)ID_INCOMPATIBLE_PROTOCOL_VERSION)
05745 {
05746
05747 RakNet::BitStream bs((unsigned char*) data,length,false);
05748 bs.IgnoreBytes(sizeof(MessageID));
05749 bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID));
05750 if ((unsigned char)(data)[0] == (MessageID)ID_INCOMPATIBLE_PROTOCOL_VERSION)
05751 bs.IgnoreBytes(sizeof(unsigned char));
05752
05753 RakNetGUID guid;
05754 bs.Read(guid);
05755
05756 RakPeer::RequestedConnectionStruct *rcs;
05757 bool connectionAttemptCancelled=false;
05758 unsigned i;
05759 rakPeer->requestedConnectionQueueMutex.Lock();
05760 for (i=0; i < rakPeer->requestedConnectionQueue.Size(); i++)
05761 {
05762 rcs=rakPeer->requestedConnectionQueue[i];
05763 if (rcs->actionToTake==RakPeer::RequestedConnectionStruct::CONNECT && rcs->systemAddress==systemAddress)
05764 {
05765 connectionAttemptCancelled=true;
05766 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
05767
05768 #endif
05769
05770 rakPeer->requestedConnectionQueue.RemoveAtIndex(i);
05771 RakNet::OP_DELETE(rcs,__FILE__,__LINE__);
05772 break;
05773 }
05774 }
05775
05776 rakPeer->requestedConnectionQueueMutex.Unlock();
05777
05778 if (connectionAttemptCancelled)
05779 {
05780
05781 packet=rakPeer->AllocPacket(sizeof( char ), __FILE__, __LINE__);
05782 packet->data[ 0 ] = data[0];
05783 packet->bitSize = ( sizeof( char ) * 8);
05784 packet->systemAddress = systemAddress;
05785 packet->guid=guid;
05786 rakPeer->AddPacketToProducer(packet);
05787 }
05788 }
05789 else if ((unsigned char)(data)[0] == ID_OPEN_CONNECTION_REQUEST && length >= sizeof(unsigned char)*2)
05790 {
05791
05792
05793
05794 unsigned int i;
05795
05796 char remoteProtocol=data[1];
05797 if (remoteProtocol!=RAKNET_PROTOCOL_VERSION)
05798 {
05799 RakNet::BitStream bs;
05800 bs.Write((MessageID)ID_INCOMPATIBLE_PROTOCOL_VERSION);
05801 bs.Write((unsigned char)RAKNET_PROTOCOL_VERSION);
05802 bs.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
05803 bs.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS));
05804
05805 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05806 rakPeer->messageHandlerList[i]->OnDirectSocketSend((char*)bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress);
05807
05808 SocketLayer::Instance()->SendTo( rakNetSocket->s, (char*)bs.GetData(), bs.GetNumberOfBytesUsed(), systemAddress.binaryAddress, systemAddress.port, rakNetSocket->remotePortRakNetWasStartedOn_PS3 );
05809 return true;
05810 }
05811
05812 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05813 rakPeer->messageHandlerList[i]->OnDirectSocketReceive(data, length*8, systemAddress);
05814
05815 RakNetGUID guid;
05816 RakNet::BitStream bsOut;
05817 RakNet::BitStream bs((unsigned char*) data, length, false);
05818 bs.IgnoreBytes(sizeof(MessageID)*2);
05819 bs.Read(guid);
05820 bs.AlignReadToByteBoundary();
05821 bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID));
05822 SystemAddress bindingAddress;
05823 bs.Read(bindingAddress);
05824
05825 RakPeer::RemoteSystemStruct *rssFromSA = rakPeer->GetRemoteSystemFromSystemAddress( systemAddress, true, true );
05826 bool IPAddrInUse = rssFromSA != 0 && rssFromSA->isActive;
05827 RakPeer::RemoteSystemStruct *rssFromGuid = rakPeer->GetRemoteSystemFromGUID(guid, true);
05828 bool GUIDInUse = rssFromGuid != 0 && rssFromGuid->isActive;
05829
05830
05831
05832
05833
05834
05835
05836 int outcome;
05837 if (IPAddrInUse & GUIDInUse)
05838 {
05839 if (rssFromSA==rssFromGuid && rssFromSA->connectMode==RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER)
05840 {
05841
05842 outcome=1;
05843 }
05844 else
05845 {
05846
05847 outcome=2;
05848 }
05849 }
05850 else if (IPAddrInUse==false && GUIDInUse==true)
05851 {
05852
05853 outcome=3;
05854 }
05855 else if (IPAddrInUse==true && GUIDInUse==false)
05856 {
05857
05858 outcome=4;
05859 }
05860 else
05861 {
05862
05863 outcome=0;
05864 }
05865
05866 if (outcome==1)
05867 {
05868
05869 bsOut.Write((MessageID)ID_OPEN_CONNECTION_REPLY);
05870 bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
05871 bsOut.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS));
05872 bsOut.Write(systemAddress);
05873 bsOut.PadWithZeroToByteLength(length);
05874 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05875 rakPeer->messageHandlerList[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress);
05876 SocketLayer::SetDoNotFragment(rakNetSocket->s, 1);
05877 SocketLayer::Instance()->SendTo( rakNetSocket->s, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress.binaryAddress, systemAddress.port, rakNetSocket->remotePortRakNetWasStartedOn_PS3 );
05878 SocketLayer::SetDoNotFragment(rakNetSocket->s, 0);
05879 return true;
05880 }
05881 else if (outcome!=0)
05882 {
05883 bsOut.Write((MessageID)ID_ALREADY_CONNECTED);
05884 bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
05885 bsOut.Write(guid);
05886 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05887 rakPeer->messageHandlerList[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress);
05888 SocketLayer::Instance()->SendTo( rakNetSocket->s, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress.binaryAddress, systemAddress.port, rakNetSocket->remotePortRakNetWasStartedOn_PS3 );
05889
05890 return true;
05891 }
05892
05893 if (rakPeer->AllowIncomingConnections()==false)
05894 {
05895 bsOut.Write((MessageID)ID_NO_FREE_INCOMING_CONNECTIONS);
05896 bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
05897 bsOut.Write(guid);
05898 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05899 rakPeer->messageHandlerList[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress);
05900 SocketLayer::Instance()->SendTo( rakNetSocket->s, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress.binaryAddress, systemAddress.port, rakNetSocket->remotePortRakNetWasStartedOn_PS3 );
05901
05902 return true;
05903 }
05904
05905 bool thisIPConnectedRecently=false;
05906 rakPeer->AssignSystemAddressToRemoteSystemList(systemAddress, RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER, rakNetSocket, &thisIPConnectedRecently, bindingAddress, length+UDP_HEADER_SIZE, guid);
05907
05908 if (thisIPConnectedRecently==true)
05909 {
05910 bsOut.Write((MessageID)ID_IP_RECENTLY_CONNECTED);
05911 bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
05912 bsOut.Write(guid);
05913 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05914 rakPeer->messageHandlerList[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress);
05915 SocketLayer::Instance()->SendTo( rakNetSocket->s, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress.binaryAddress, systemAddress.port, rakNetSocket->remotePortRakNetWasStartedOn_PS3 );
05916
05917 return true;
05918 }
05919
05920 bsOut.Write((MessageID)ID_OPEN_CONNECTION_REPLY);
05921 bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
05922 bsOut.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS));
05923 bsOut.Write(systemAddress);
05924 bsOut.PadWithZeroToByteLength(length);
05925 for (i=0; i < rakPeer->messageHandlerList.Size(); i++)
05926 rakPeer->messageHandlerList[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress);
05927 SocketLayer::SetDoNotFragment(rakNetSocket->s, 1);
05928 SocketLayer::Instance()->SendTo( rakNetSocket->s, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress.binaryAddress, systemAddress.port, rakNetSocket->remotePortRakNetWasStartedOn_PS3 );
05929 SocketLayer::SetDoNotFragment(rakNetSocket->s, 0);
05930
05931
05932 }
05933 return true;
05934 }
05935
05936 return false;
05937 }
05938
05939 void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetTimeUS timeRead )
05940 {
05941 ProcessNetworkPacket(systemAddress,data,length,rakPeer,rakPeer->socketList[0],timeRead);
05942 }
05943 void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSmartPtr<RakNetSocket> rakNetSocket, RakNetTimeUS timeRead )
05944 {
05945 RakAssert(systemAddress.port);
05946 bool isOfflineMessage;
05947 if (ProcessOfflineNetworkPacket(systemAddress, data, length, rakPeer, rakNetSocket, &isOfflineMessage, timeRead))
05948 {
05949 return;
05950 }
05951
05952 Packet *packet;
05953 RakPeer::RemoteSystemStruct *remoteSystem;
05954
05955
05956 remoteSystem = rakPeer->GetRemoteSystemFromSystemAddress( systemAddress, true, true );
05957 if ( remoteSystem )
05958 {
05959 if (remoteSystem->connectMode==RakPeer::RemoteSystemStruct::SET_ENCRYPTION_ON_MULTIPLE_16_BYTE_PACKET && (length & 15)==0)
05960 {
05961
05962 unsigned int newLength;
05963 char output[ MAXIMUM_MTU_SIZE ];
05964 DataBlockEncryptor testEncryptor;
05965 testEncryptor.SetKey(remoteSystem->AESKey);
05966
05967 if ( testEncryptor.Decrypt( ( unsigned char* ) data, length, (unsigned char*) output, &newLength ) == true )
05968 remoteSystem->reliabilityLayer.SetEncryptionKey( remoteSystem->AESKey);
05969 }
05970
05971
05972
05973 if ( isOfflineMessage==false)
05974 {
05975 if (remoteSystem->reliabilityLayer.HandleSocketReceiveFromConnectedPlayer(
05976 data, length, systemAddress, rakPeer->messageHandlerList, remoteSystem->MTUSize,
05977 rakNetSocket->s, &rnr, rakNetSocket->remotePortRakNetWasStartedOn_PS3, timeRead) == false)
05978 {
05979
05980
05981 if ( !(
05982 ( (unsigned char)data[0] == ID_CONNECTION_BANNED ) ||
05983 ( (unsigned char)data[0] == ID_OPEN_CONNECTION_REQUEST ) ||
05984 ( (unsigned char)data[0] == ID_OPEN_CONNECTION_REPLY ) ||
05985 ( (unsigned char)data[0] == ID_CONNECTION_ATTEMPT_FAILED ) ||
05986 ( (unsigned char)data[0] == ID_IP_RECENTLY_CONNECTED ) ||
05987 ( (unsigned char)data[0] == ID_INCOMPATIBLE_PROTOCOL_VERSION ))
05988 )
05989 {
05990
05991 packet=rakPeer->AllocPacket(1, __FILE__, __LINE__);
05992 packet->data[ 0 ] = ID_MODIFIED_PACKET;
05993 packet->bitSize = sizeof( char ) * 8;
05994 packet->systemAddress = systemAddress;
05995 packet->systemAddress.systemIndex = ( SystemIndex ) rakPeer->GetIndexFromSystemAddress( systemAddress, true );
05996 packet->guid=remoteSystem->guid;
05997 packet->guid.systemIndex=packet->systemAddress.systemIndex;
05998 rakPeer->AddPacketToProducer(packet);
05999 }
06000 }
06001 }
06002 }
06003 }
06004
06005 unsigned int RakPeer::GenerateSeedFromGuid(void)
06006 {
06007
06008
06009
06010
06011
06012
06013
06014
06015
06016
06017
06018
06019
06020
06021
06022
06023 return (unsigned int) ((myGuid.g >> 32) ^ myGuid.g);
06024 }
06025
06026 void RakPeer::DerefAllSockets(void)
06027 {
06028 unsigned int i;
06029 for (i=0; i < socketList.Size(); i++)
06030 {
06031 socketList[i].SetNull();
06032 }
06033 socketList.Clear(false, __FILE__, __LINE__);
06034 }
06035
06036 unsigned int RakPeer::GetRakNetSocketFromUserConnectionSocketIndex(unsigned int userIndex) const
06037 {
06038 unsigned int i;
06039 for (i=0; i < socketList.Size(); i++)
06040 {
06041 if (socketList[i]->userConnectionSocketIndex==userIndex)
06042 return i;
06043 }
06044 RakAssert("GetRakNetSocketFromUserConnectionSocketIndex failed" && 0);
06045 return (unsigned int) -1;
06046 }
06047
06048 bool RakPeer::RunUpdateCycle( void )
06049 {
06050 RakPeer::RemoteSystemStruct * remoteSystem;
06051 unsigned remoteSystemIndex;
06052 Packet *packet;
06053
06054
06055 BitSize_t numberOfBitsUsed;
06056
06057 BitSize_t bitSize;
06058 unsigned int byteSize;
06059 unsigned char *data;
06060 RakNetTimeUS timeNS;
06061 RakNetTime timeMS;
06062 SystemAddress systemAddress;
06063 BufferedCommandStruct *bcs;
06064 bool callerDataAllocationUsed;
06065 RakNetStatistics *rnss;
06066
06067
06068
06069
06070
06071
06072
06073
06074
06075
06076
06077
06078
06079
06080
06081
06082
06083
06084
06085
06086
06087
06088
06089
06090
06091
06092
06093
06094
06095
06096
06097
06098
06099
06100
06101
06102
06103
06104
06105
06106
06107
06108
06109
06110
06111
06112
06113
06114
06115
06116 if (SocketLayer::Instance()->GetSocketLayerOverride())
06117 {
06118 SystemAddress sender;
06119 char dataOut[ MAXIMUM_MTU_SIZE ];
06120 int len = SocketLayer::Instance()->GetSocketLayerOverride()->RakNetRecvFrom(socketList[0]->s,this,dataOut,&sender,true);
06121 if (len>0)
06122 {
06123 ProcessNetworkPacket( sender, dataOut, len, this, socketList[0], RakNet::GetTimeUS() );
06124 return 1;
06125 }
06126 }
06127
06128 unsigned int socketListIndex;
06129 RakPeer::RecvFromStruct *recvFromStruct;
06130 while ((recvFromStruct=bufferedPackets.PopInaccurate())!=0)
06131 {
06132 for (socketListIndex=0; socketListIndex < socketList.Size(); socketListIndex++)
06133 {
06134 if (socketList[socketListIndex]->s==recvFromStruct->s)
06135 break;
06136 }
06137 if (socketListIndex!=socketList.Size())
06138 ProcessNetworkPacket(recvFromStruct->systemAddress, recvFromStruct->data, recvFromStruct->bytesRead, this, socketList[socketListIndex], recvFromStruct->timeRead);
06139 bufferedPackets.Deallocate(recvFromStruct, __FILE__,__LINE__);
06140 }
06141
06142
06143 timeNS=0;
06144 timeMS=0;
06145
06146 #ifdef _RAKNET_THREADSAFE
06147 while ((bcs=bufferedCommands.PopInaccurate())!=0)
06148 #else
06149
06150 while ((bcs=bufferedCommands.ReadLock())!=0)
06151 #endif
06152 {
06153 if (bcs->command==BufferedCommandStruct::BCS_SEND)
06154 {
06155
06156 if (timeNS==0)
06157 {
06158 timeNS = RakNet::GetTimeNS();
06159 timeMS = (RakNetTime)(timeNS/(RakNetTimeUS)1000);
06160 }
06161
06162 callerDataAllocationUsed=SendImmediate((char*)bcs->data, bcs->numberOfBitsToSend, bcs->priority, bcs->reliability, bcs->orderingChannel, bcs->systemIdentifier, bcs->broadcast, true, timeNS, bcs->receipt);
06163 if ( callerDataAllocationUsed==false )
06164 rakFree_Ex(bcs->data, __FILE__, __LINE__ );
06165
06166
06167 if (bcs->connectionMode!=RemoteSystemStruct::NO_ACTION )
06168 {
06169 remoteSystem=GetRemoteSystem( bcs->systemIdentifier, true, true );
06170 if (remoteSystem)
06171 remoteSystem->connectMode=bcs->connectionMode;
06172 }
06173 }
06174 else if (bcs->command==BufferedCommandStruct::BCS_CLOSE_CONNECTION)
06175 {
06176 CloseConnectionInternal(bcs->systemIdentifier, false, true, bcs->orderingChannel, bcs->priority);
06177 }
06178 else if (bcs->command==BufferedCommandStruct::BCS_CHANGE_SYSTEM_ADDRESS)
06179 {
06180
06181 RakPeer::RemoteSystemStruct *rssFromGuid = GetRemoteSystem(bcs->systemIdentifier.rakNetGuid,true,true);
06182 if (rssFromGuid!=0)
06183 {
06184 unsigned int existingSystemIndex = GetRemoteSystemIndex(rssFromGuid->systemAddress);
06185 ReferenceRemoteSystem(bcs->systemIdentifier.systemAddress, existingSystemIndex);
06186 }
06187 }
06188 else if (bcs->command==BufferedCommandStruct::BCS_GET_SOCKET)
06189 {
06190 SocketQueryOutput *sqo;
06191 if (bcs->systemIdentifier.IsUndefined())
06192 {
06193 #ifdef _RAKNET_THREADSAFE
06194 sqo = socketQueryOutput.Allocate( __FILE__, __LINE__ );
06195 sqo->sockets=socketList;
06196 socketQueryOutput.Push(sqo);
06197 #else
06198 sqo = socketQueryOutput.WriteLock();
06199 sqo->sockets=socketList;
06200 socketQueryOutput.WriteUnlock();
06201 #endif
06202 }
06203 else
06204 {
06205 remoteSystem=GetRemoteSystem( bcs->systemIdentifier, true, true );
06206 #ifdef _RAKNET_THREADSAFE
06207 sqo = socketQueryOutput.Allocate( __FILE__, __LINE__ );
06208 #else
06209 sqo = socketQueryOutput.WriteLock();
06210 #endif
06211 sqo->sockets.Clear(false, __FILE__, __LINE__);
06212 if (remoteSystem)
06213 {
06214 sqo->sockets.Push(remoteSystem->rakNetSocket, __FILE__, __LINE__ );
06215 }
06216 else
06217 {
06218
06219 }
06220 #ifdef _RAKNET_THREADSAFE
06221 socketQueryOutput.Push(sqo);
06222 #else
06223 socketQueryOutput.WriteUnlock();
06224 #endif
06225 }
06226
06227 }
06228
06229 #ifdef _DEBUG
06230 bcs->data=0;
06231 #endif
06232
06233 #ifdef _RAKNET_THREADSAFE
06234 bufferedCommands.Deallocate(bcs, __FILE__,__LINE__);
06235 #else
06236 bufferedCommands.ReadUnlock();
06237 #endif
06238 }
06239
06240 if (requestedConnectionQueue.IsEmpty()==false)
06241 {
06242 if (timeNS==0)
06243 {
06244 timeNS = RakNet::GetTimeNS();
06245 timeMS = (RakNetTime)(timeNS/(RakNetTimeUS)1000);
06246 }
06247
06248 bool condition1, condition2;
06249 RequestedConnectionStruct *rcs;
06250 unsigned requestedConnectionQueueIndex=0;
06251 requestedConnectionQueueMutex.Lock();
06252 while (requestedConnectionQueueIndex < requestedConnectionQueue.Size())
06253 {
06254 rcs = requestedConnectionQueue[requestedConnectionQueueIndex];
06255 requestedConnectionQueueMutex.Unlock();
06256 if (rcs->nextRequestTime < timeMS)
06257 {
06258 condition1=rcs->requestsMade==rcs->sendConnectionAttemptCount+1;
06259 condition2=(bool)((rcs->systemAddress==UNASSIGNED_SYSTEM_ADDRESS)==1);
06260
06261 if (condition1 || condition2)
06262 {
06263 if (rcs->data)
06264 {
06265 rakFree_Ex(rcs->data, __FILE__, __LINE__ );
06266 rcs->data=0;
06267 }
06268
06269 if (condition1 && !condition2 && rcs->actionToTake==RequestedConnectionStruct::CONNECT)
06270 {
06271
06272 packet=AllocPacket(sizeof( char ), __FILE__, __LINE__);
06273 packet->data[ 0 ] = ID_CONNECTION_ATTEMPT_FAILED;
06274 packet->bitSize = ( sizeof( char ) * 8);
06275 packet->systemAddress = rcs->systemAddress;
06276 AddPacketToProducer(packet);
06277 }
06278
06279 RakNet::OP_DELETE(rcs,__FILE__,__LINE__);
06280
06281 requestedConnectionQueueMutex.Lock();
06282 for (unsigned int k=0; k < requestedConnectionQueue.Size(); k++)
06283 {
06284 if (requestedConnectionQueue[k]==rcs)
06285 {
06286 requestedConnectionQueue.RemoveAtIndex(k);
06287 break;
06288 }
06289 }
06290 requestedConnectionQueueMutex.Unlock();
06291 }
06292 else
06293 {
06294
06295 int MTUSizeIndex = rcs->requestsMade / (rcs->sendConnectionAttemptCount/NUM_MTU_SIZES);
06296 if (MTUSizeIndex>=NUM_MTU_SIZES)
06297 MTUSizeIndex=NUM_MTU_SIZES-1;
06298 rcs->requestsMade++;
06299 rcs->nextRequestTime=timeMS+rcs->timeBetweenSendConnectionAttemptsMS;
06300
06301
06302
06303
06304 RakNet::BitStream bitStream;
06305 bitStream.Write((MessageID)ID_OPEN_CONNECTION_REQUEST);
06306 bitStream.Write((MessageID)RAKNET_PROTOCOL_VERSION);
06307 bitStream.Write(myGuid);
06308 bitStream.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID));
06309 bitStream.Write(rcs->systemAddress);
06310
06311 bitStream.PadWithZeroToByteLength(mtuSizes[MTUSizeIndex]-UDP_HEADER_SIZE);
06312
06313
06314
06315
06316 char str[256];
06317 rcs->systemAddress.ToString(true,str);
06318
06319
06320
06321 unsigned i;
06322 for (i=0; i < messageHandlerList.Size(); i++)
06323 messageHandlerList[i]->OnDirectSocketSend((const char*) bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), rcs->systemAddress);
06324
06325 if (rcs->socket.IsNull())
06326 {
06327 SocketLayer::SetDoNotFragment(socketList[rcs->socketIndex]->s, 1);
06328 if (SocketLayer::Instance()->SendTo( socketList[rcs->socketIndex]->s, (const char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), rcs->systemAddress.binaryAddress, rcs->systemAddress.port, socketList[rcs->socketIndex]->remotePortRakNetWasStartedOn_PS3 )==-10040)
06329 {
06330
06331 rcs->requestsMade = (unsigned char) ((MTUSizeIndex + 1) * (rcs->sendConnectionAttemptCount/NUM_MTU_SIZES));
06332 rcs->nextRequestTime=timeMS;
06333 }
06334 SocketLayer::SetDoNotFragment(socketList[rcs->socketIndex]->s, 0);
06335 }
06336 else
06337 {
06338 SocketLayer::SetDoNotFragment(rcs->socket->s, 1);
06339 if (SocketLayer::Instance()->SendTo( rcs->socket->s, (const char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), rcs->systemAddress.binaryAddress, rcs->systemAddress.port, socketList[rcs->socketIndex]->remotePortRakNetWasStartedOn_PS3 )==-10040)
06340 {
06341
06342 rcs->requestsMade = (unsigned char) ((MTUSizeIndex + 1) * (rcs->sendConnectionAttemptCount/NUM_MTU_SIZES));
06343 rcs->nextRequestTime=timeMS;
06344 }
06345 SocketLayer::SetDoNotFragment(socketList[rcs->socketIndex]->s, 0);
06346 }
06347
06348
06349 requestedConnectionQueueIndex++;
06350 }
06351 }
06352 else
06353 requestedConnectionQueueIndex++;
06354
06355 requestedConnectionQueueMutex.Lock();
06356 }
06357 requestedConnectionQueueMutex.Unlock();
06358 }
06359
06360
06361 for ( remoteSystemIndex = 0; remoteSystemIndex < maximumNumberOfPeers; ++remoteSystemIndex )
06362
06363 {
06364
06365
06366
06367
06368
06369 if ( remoteSystemList[ remoteSystemIndex ].isActive )
06370 {
06371 systemAddress = remoteSystemList[ remoteSystemIndex ].systemAddress;
06372 RakAssert(systemAddress!=UNASSIGNED_SYSTEM_ADDRESS);
06373
06374
06375 remoteSystem = remoteSystemList + remoteSystemIndex;
06376
06377
06378
06379 if (timeNS==0)
06380 {
06381 timeNS = RakNet::GetTimeNS();
06382 timeMS = (RakNetTime)(timeNS/(RakNetTimeUS)1000);
06383
06384 }
06385
06386
06387 if (timeMS > remoteSystem->lastReliableSend && timeMS-remoteSystem->lastReliableSend > defaultTimeoutTime/2 && remoteSystem->connectMode==RemoteSystemStruct::CONNECTED)
06388 {
06389
06390 RakNetStatistics rakNetStatistics;
06391 rnss=remoteSystem->reliabilityLayer.GetStatistics(&rakNetStatistics);
06392 if (rnss->messagesInResendBuffer==0)
06393 {
06394 PingInternal( systemAddress, true, RELIABLE );
06395
06396
06397
06398
06399
06400 remoteSystem->lastReliableSend=timeMS;
06401 }
06402 }
06403
06404 remoteSystem->reliabilityLayer.Update( remoteSystem->rakNetSocket->s, systemAddress, remoteSystem->MTUSize, timeNS, maxOutgoingBPS, messageHandlerList, &rnr, remoteSystem->rakNetSocket->remotePortRakNetWasStartedOn_PS3 );
06405
06406
06407 if ( remoteSystem->reliabilityLayer.IsDeadConnection() ||
06408 ((remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ASAP || remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY) && remoteSystem->reliabilityLayer.IsOutgoingDataWaiting()==false) ||
06409 (remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ON_NO_ACK && (remoteSystem->reliabilityLayer.AreAcksWaiting()==false || remoteSystem->reliabilityLayer.AckTimeout(timeMS)==true)) ||
06410 ((
06411 (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION ||
06412 remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST ||
06413 remoteSystem->connectMode==RemoteSystemStruct::UNVERIFIED_SENDER ||
06414 remoteSystem->connectMode==RemoteSystemStruct::SET_ENCRYPTION_ON_MULTIPLE_16_BYTE_PACKET)
06415 && timeMS > remoteSystem->connectionTime && timeMS - remoteSystem->connectionTime > 10000))
06416 )
06417 {
06418
06419
06420
06421
06422
06423 if (remoteSystem->connectMode==RemoteSystemStruct::CONNECTED || remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION
06424 || remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY || remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ASAP || remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ON_NO_ACK)
06425 {
06426
06427
06428
06429
06430
06431 packet=AllocPacket(sizeof( char ), __FILE__, __LINE__);
06432 if (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION)
06433 packet->data[ 0 ] = ID_CONNECTION_ATTEMPT_FAILED;
06434 else if (remoteSystem->connectMode==RemoteSystemStruct::CONNECTED)
06435 packet->data[ 0 ] = ID_CONNECTION_LOST;
06436 else
06437 packet->data[ 0 ] = ID_DISCONNECTION_NOTIFICATION;
06438
06439
06440
06441 packet->guid = remoteSystem->guid;
06442 packet->systemAddress = systemAddress;
06443 packet->systemAddress.systemIndex = ( SystemIndex ) remoteSystemIndex;
06444 packet->guid.systemIndex=packet->systemAddress.systemIndex;
06445
06446 AddPacketToProducer(packet);
06447 }
06448
06449
06450 #ifdef _DO_PRINTF
06451 RAKNET_DEBUG_PRINTF("Connection dropped for player %i:%i\n", systemAddress.binaryAddress, systemAddress.port);
06452 #endif
06453 CloseConnectionInternal( systemAddress, false, true, 0, LOW_PRIORITY );
06454 continue;
06455 }
06456
06457
06458 if ( remoteSystem->reliabilityLayer.IsCheater() )
06459 {
06460 packet=AllocPacket(sizeof(char), __FILE__, __LINE__);
06461 packet->bitSize=8;
06462 packet->data[ 0 ] = (MessageID)ID_MODIFIED_PACKET;
06463 packet->systemAddress = systemAddress;
06464 packet->systemAddress.systemIndex = ( SystemIndex ) remoteSystemIndex;
06465 packet->guid = remoteSystem->guid;
06466 packet->guid.systemIndex=packet->systemAddress.systemIndex;
06467 AddPacketToProducer(packet);
06468 continue;
06469 }
06470
06471
06472 if ( remoteSystem->connectMode==RemoteSystemStruct::CONNECTED && timeMS > remoteSystem->nextPingTime && ( occasionalPing || remoteSystem->lowestPing == (unsigned short)-1 ) )
06473 {
06474 remoteSystem->nextPingTime = timeMS + 5000;
06475 PingInternal( systemAddress, true, UNRELIABLE );
06476
06477
06478 quitAndDataEvents.SetEvent();
06479 }
06480
06481
06482
06483
06484
06485
06486
06487 bitSize = remoteSystem->reliabilityLayer.Receive( &data );
06488
06489 while ( bitSize > 0 )
06490 {
06491
06492 if (data[0]==ID_CONNECTION_ATTEMPT_FAILED || data[0]==ID_MODIFIED_PACKET)
06493 {
06494 RakAssert(0);
06495 continue;
06496 }
06497
06498
06499 if ( inputTree )
06500 {
06501 RakNet::BitStream dataBitStream( MAXIMUM_MTU_SIZE );
06502
06503
06504 dataBitStream.Reset();
06505 dataBitStream.WriteAlignedBytes( ( unsigned char* ) data, BITS_TO_BYTES( bitSize ) );
06506 rawBytesReceived += (unsigned int) dataBitStream.GetNumberOfBytesUsed();
06507
06508
06509 numberOfBitsUsed = dataBitStream.GetNumberOfBitsUsed();
06510
06511
06512
06513 if (numberOfBitsUsed>0)
06514 {
06515 unsigned char *dataCopy = (unsigned char*) rakMalloc_Ex( (unsigned int) dataBitStream.GetNumberOfBytesUsed(), __FILE__, __LINE__ );
06516 memcpy( dataCopy, dataBitStream.GetData(), (size_t) dataBitStream.GetNumberOfBytesUsed() );
06517 dataBitStream.Reset();
06518 inputTree->DecodeArray( dataCopy, numberOfBitsUsed, &dataBitStream );
06519 compressedBytesReceived += (unsigned int) dataBitStream.GetNumberOfBytesUsed();
06520 rakFree_Ex(dataCopy, __FILE__, __LINE__ );
06521
06522 byteSize = (unsigned int) dataBitStream.GetNumberOfBytesUsed();
06523
06524 if ( byteSize > BITS_TO_BYTES( bitSize ) )
06525 {
06526 rakFree_Ex(data, __FILE__, __LINE__ );
06527 data = (unsigned char*) rakMalloc_Ex( (size_t) byteSize, __FILE__, __LINE__ );
06528 }
06529 bitSize = (BitSize_t) dataBitStream.GetNumberOfBitsUsed();
06530 memcpy( data, dataBitStream.GetData(), byteSize );
06531 }
06532 else
06533 byteSize=0;
06534 }
06535 else
06536
06537 byteSize = (unsigned int) BITS_TO_BYTES( bitSize );
06538
06539
06540 if (remoteSystem->connectMode==RemoteSystemStruct::UNVERIFIED_SENDER)
06541 {
06542 if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST )
06543 {
06544 ParseConnectionRequestPacket(remoteSystem, systemAddress, (const char*)data, byteSize);
06545 rakFree_Ex(data, __FILE__, __LINE__ );
06546 }
06547 else
06548 {
06549 CloseConnectionInternal( systemAddress, false, true, 0, LOW_PRIORITY );
06550 #ifdef _DO_PRINTF
06551 RAKNET_DEBUG_PRINTF("Temporarily banning %i:%i for sending nonsense data\n", systemAddress.binaryAddress, systemAddress.port);
06552 #endif
06553 #if !defined(_XBOX) && !defined(X360)
06554 char str1[64];
06555 systemAddress.ToString(false, str1);
06556 AddToBanList(str1, remoteSystem->reliabilityLayer.GetTimeoutTime());
06557 #endif
06558
06559 rakFree_Ex(data, __FILE__, __LINE__ );
06560 }
06561 }
06562 else
06563 {
06564
06565
06566 if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST )
06567 {
06568
06569
06570 if (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION)
06571 {
06572 ParseConnectionRequestPacket(remoteSystem, systemAddress, (const char*)data, byteSize);
06573 }
06574 else
06575 {
06576
06577 RakNet::BitStream bs((unsigned char*) data,byteSize,false);
06578 bs.IgnoreBytes(sizeof(MessageID));
06579 bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID));
06580 bs.IgnoreBytes(RakNetGUID::size());
06581 RakNetTime incomingTimestamp;
06582 bs.Read(incomingTimestamp);
06583
06584
06585
06586 SendConnectionRequestAccepted(remoteSystem, incomingTimestamp);
06587 }
06588 rakFree_Ex(data, __FILE__, __LINE__ );
06589 }
06590 else if ( (unsigned char) data[ 0 ] == ID_NEW_INCOMING_CONNECTION && byteSize > sizeof(unsigned char)+sizeof(unsigned int)+sizeof(unsigned short)+sizeof(RakNetTime)*2 )
06591 {
06592
06593 if (remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST ||
06594
06595
06596 remoteSystem->connectMode==RemoteSystemStruct::SET_ENCRYPTION_ON_MULTIPLE_16_BYTE_PACKET)
06597 {
06598
06599
06600
06601
06602 remoteSystem->connectMode=RemoteSystemStruct::CONNECTED;
06603 PingInternal( systemAddress, true, UNRELIABLE );
06604
06605
06606 quitAndDataEvents.SetEvent();
06607
06608 RakNet::BitStream inBitStream((unsigned char *) data, byteSize, false);
06609 SystemAddress bsSystemAddress;
06610
06611 inBitStream.IgnoreBits(8);
06612 inBitStream.Read(bsSystemAddress);
06613 for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++)
06614 inBitStream.Read(remoteSystem->theirInternalSystemAddress[i]);
06615
06616 RakNetTime sendPingTime, sendPongTime;
06617 inBitStream.Read(sendPingTime);
06618 inBitStream.Read(sendPongTime);
06619 OnConnectedPong(sendPingTime,sendPongTime,remoteSystem);
06620
06621
06622
06623
06624
06625
06626 remoteSystem->myExternalSystemAddress = bsSystemAddress;
06627 firstExternalID=bsSystemAddress;
06628
06629
06630 packet=AllocPacket(byteSize, data, __FILE__, __LINE__);
06631 packet->bitSize = bitSize;
06632 packet->systemAddress = systemAddress;
06633 packet->systemAddress.systemIndex = ( SystemIndex ) remoteSystemIndex;
06634 packet->guid = remoteSystem->guid;
06635 packet->guid.systemIndex=packet->systemAddress.systemIndex;
06636 AddPacketToProducer(packet);
06637 }
06638 }
06639 else if ( (unsigned char) data[ 0 ] == ID_CONNECTED_PONG && byteSize == sizeof(unsigned char)+sizeof(RakNetTime)*2 )
06640 {
06641 RakNetTime sendPingTime, sendPongTime;
06642
06643
06644
06645 RakNet::BitStream inBitStream( (unsigned char *) data, byteSize, false );
06646
06647
06648 inBitStream.IgnoreBits(8);
06649 inBitStream.Read(sendPingTime);
06650 inBitStream.Read(sendPongTime);
06651
06652 OnConnectedPong(sendPingTime,sendPongTime,remoteSystem);
06653
06654 rakFree_Ex(data, __FILE__, __LINE__ );
06655 }
06656 else if ( (unsigned char)data[0] == ID_INTERNAL_PING && byteSize == sizeof(unsigned char)+sizeof(RakNetTime) )
06657 {
06658 RakNet::BitStream inBitStream( (unsigned char *) data, byteSize, false );
06659 inBitStream.IgnoreBits(8);
06660 RakNetTime sendPingTime;
06661 inBitStream.Read(sendPingTime);
06662
06663 RakNet::BitStream outBitStream;
06664 outBitStream.Write((MessageID)ID_CONNECTED_PONG);
06665 outBitStream.Write(sendPingTime);
06666 timeMS = RakNet::GetTime();
06667 timeNS = RakNet::GetTimeNS();
06668 outBitStream.Write(timeMS);
06669 SendImmediate( (char*)outBitStream.GetData(), outBitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, UNRELIABLE, 0, systemAddress, false, false, timeNS, 0 );
06670
06671
06672 quitAndDataEvents.SetEvent();
06673
06674 rakFree_Ex(data, __FILE__, __LINE__ );
06675 }
06676 else if ( (unsigned char) data[ 0 ] == ID_DISCONNECTION_NOTIFICATION )
06677 {
06678
06679 remoteSystem->connectMode=RemoteSystemStruct::DISCONNECT_ON_NO_ACK;
06680 rakFree_Ex(data, __FILE__, __LINE__ );
06681
06682
06683 }
06684 else if ( (unsigned char) data[ 0 ] == ID_RPC_MAPPING )
06685 {
06686 RakNet::BitStream inBitStream( (unsigned char *) data, byteSize, false );
06687 RPCIndex index;
06688 char output[256];
06689 inBitStream.IgnoreBits(8);
06690 stringCompressor->DecodeString(output, 255, &inBitStream);
06691 inBitStream.ReadCompressed(index);
06692 remoteSystem->rpcMap.AddIdentifierAtIndex((char*)output,index);
06693 rakFree_Ex(data, __FILE__, __LINE__ );
06694 }
06695 #if !defined(_XBOX) && !defined(_WIN32_WCE) && !defined(X360)
06696 else if ( (unsigned char)(data)[0] == ID_SECURED_CONNECTION_RESPONSE &&
06697 byteSize == 1 + sizeof( uint32_t ) + sizeof( uint32_t ) * RAKNET_RSA_FACTOR_LIMBS + 20 )
06698 {
06699 SecuredConnectionConfirmation( remoteSystem, (char*)data );
06700 rakFree_Ex(data, __FILE__, __LINE__ );
06701 }
06702 else if ( (unsigned char)(data)[0] == ID_SECURED_CONNECTION_CONFIRMATION &&
06703 byteSize == 1 + 20 + sizeof( uint32_t ) * RAKNET_RSA_FACTOR_LIMBS + sizeof(RakNetTime) )
06704 {
06705 CSHA1 sha1;
06706 bool confirmedHash;
06707
06708
06709 confirmedHash = false;
06710
06711
06712
06713 sha1.Reset();
06714 sha1.Update( ( unsigned char* ) & systemAddress.binaryAddress, sizeof( systemAddress.binaryAddress ) );
06715 sha1.Update( ( unsigned char* ) & systemAddress.port, sizeof( systemAddress.port ) );
06716 sha1.Update( ( unsigned char* ) & ( newRandomNumber ), 20 );
06717 sha1.Final();
06718
06719
06720
06721
06722
06723
06724 if ( memcmp( sha1.GetHash(), data + 1, 20 ) == 0 )
06725 {
06726 confirmedHash = true;
06727 }
06728 else
06729 {
06730 sha1.Reset();
06731 sha1.Update( ( unsigned char* ) & systemAddress.binaryAddress, sizeof( systemAddress.binaryAddress ) );
06732 sha1.Update( ( unsigned char* ) & systemAddress.port, sizeof( systemAddress.port ) );
06733 sha1.Update( ( unsigned char* ) & ( oldRandomNumber ), 20 );
06734 sha1.Final();
06735
06736 if ( memcmp( sha1.GetHash(), data + 1, 20 ) == 0 )
06737 confirmedHash = true;
06738 }
06739 if ( confirmedHash )
06740 {
06741 int i;
06742 unsigned char AESKey[ 16 ];
06743
06744 uint32_t message[RAKNET_RSA_FACTOR_LIMBS], encryptedMessage[RAKNET_RSA_FACTOR_LIMBS];
06745
06746
06747
06748
06749 memcpy( encryptedMessage, data + 1 + 20, sizeof( encryptedMessage ) );
06750
06751
06752
06753 if (RakNet::BitStream::DoEndianSwap())
06754 {
06755 for (int i=0; i < RAKNET_RSA_FACTOR_LIMBS; i++)
06756 RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &encryptedMessage[i], sizeof(encryptedMessage[i]));
06757 }
06758
06759
06760
06761
06762 rsacrypt.decrypt( message, encryptedMessage );
06763
06764
06765
06766
06767
06768
06769
06770
06771
06772
06773
06774
06775
06776
06777
06778
06779
06780
06781
06782
06783
06784
06785
06786
06787
06788
06789
06790
06791
06792
06793
06794 for ( i = 0; i < 16; i++ )
06795 AESKey[ i ] = data[ 1 + i ] ^ ( ( unsigned char* ) ( message ) ) [ i ];
06796
06797 RakNet::BitStream bsTimestamp(data+1 + 20 + sizeof( uint32_t ) * RAKNET_RSA_FACTOR_LIMBS,sizeof(RakNetTime),false);
06798 RakNetTime timeStamp;
06799 bsTimestamp.Read(timeStamp);
06800
06801
06802 OnConnectionRequest( remoteSystem, AESKey, true, timeStamp );
06803 }
06804 rakFree_Ex(data, __FILE__, __LINE__ );
06805 }
06806 #endif // #if !defined(_XBOX) && !defined(_WIN32_WCE)
06807 else if ( (unsigned char)(data)[0] == ID_DETECT_LOST_CONNECTIONS && byteSize == sizeof(unsigned char) )
06808 {
06809
06810 rakFree_Ex(data, __FILE__, __LINE__ );
06811 }
06812 else if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST_ACCEPTED )
06813 {
06814 if (byteSize > sizeof(MessageID)+sizeof(unsigned int)+sizeof(unsigned short)+sizeof(SystemIndex)+sizeof(RakNetTime)*2)
06815 {
06816
06817 bool allowConnection, alreadyConnected;
06818
06819 if (remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST ||
06820 remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION ||
06821 allowConnectionResponseIPMigration)
06822 allowConnection=true;
06823 else
06824 allowConnection=false;
06825
06826 if (remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST)
06827 alreadyConnected=true;
06828 else
06829 alreadyConnected=false;
06830
06831 if ( allowConnection )
06832 {
06833 SystemAddress externalID;
06834 SystemIndex systemIndex;
06835
06836
06837 RakNet::BitStream inBitStream((unsigned char *) data, byteSize, false);
06838 inBitStream.IgnoreBits(8);
06839
06840 inBitStream.Read(externalID);
06841 inBitStream.Read(systemIndex);
06842 for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++)
06843 inBitStream.Read(remoteSystem->theirInternalSystemAddress[i]);
06844
06845 RakNetTime sendPingTime, sendPongTime;
06846 inBitStream.Read(sendPingTime);
06847 inBitStream.Read(sendPongTime);
06848 OnConnectedPong(sendPingTime, sendPongTime, remoteSystem);
06849
06850
06851
06852
06853
06854
06855
06856
06857 remoteSystem->myExternalSystemAddress = externalID;
06858 remoteSystem->connectMode=RemoteSystemStruct::CONNECTED;
06859
06860 firstExternalID=externalID;
06861
06862 if (alreadyConnected==false)
06863 {
06864
06865 if (remoteSystem->setAESKey)
06866 remoteSystem->reliabilityLayer.SetEncryptionKey( remoteSystem->AESKey );
06867 else
06868 remoteSystem->reliabilityLayer.SetEncryptionKey( 0 );
06869 }
06870
06871
06872 packet=AllocPacket(byteSize, data, __FILE__, __LINE__);
06873 packet->bitSize = byteSize * 8;
06874 packet->systemAddress = systemAddress;
06875 packet->systemAddress.systemIndex = ( SystemIndex ) GetIndexFromSystemAddress( systemAddress, true );
06876 packet->guid = remoteSystem->guid;
06877 packet->guid.systemIndex=packet->systemAddress.systemIndex;
06878 AddPacketToProducer(packet);
06879
06880
06881
06882
06883
06884
06885 RakNet::BitStream outBitStream;
06886 outBitStream.Write((MessageID)ID_NEW_INCOMING_CONNECTION);
06887 outBitStream.Write(systemAddress);
06888 for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++)
06889 outBitStream.Write(mySystemAddress[i]);
06890 outBitStream.Write(sendPongTime);
06891 outBitStream.Write(RakNet::GetTime());
06892
06893
06894
06895
06896
06897
06898 RakAssert((outBitStream.GetNumberOfBytesUsed()&15)!=0);
06899 SendImmediate( (char*)outBitStream.GetData(), outBitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, RELIABLE_ORDERED, 0, systemAddress, false, false, RakNet::GetTimeNS(), 0 );
06900
06901 if (alreadyConnected==false)
06902 {
06903 PingInternal( systemAddress, true, UNRELIABLE );
06904 }
06905 }
06906 else
06907 {
06908
06909 rakFree_Ex(data, __FILE__, __LINE__ );
06910 }
06911 }
06912 else
06913 {
06914
06915 RakAssert(0);
06916 rakFree_Ex(data, __FILE__, __LINE__ );
06917 }
06918 }
06919 else
06920 {
06921
06922
06923
06924 if (data[0]>=(MessageID)ID_RPC &&
06925 remoteSystem->isActive
06926
06927
06928
06929
06930
06931
06932 )
06933 {
06934 packet=AllocPacket(byteSize, data, __FILE__, __LINE__);
06935 packet->bitSize = bitSize;
06936 packet->systemAddress = systemAddress;
06937 packet->systemAddress.systemIndex = ( SystemIndex ) remoteSystemIndex;
06938 packet->guid = remoteSystem->guid;
06939 packet->guid.systemIndex=packet->systemAddress.systemIndex;
06940 AddPacketToProducer(packet);
06941 }
06942 else
06943 {
06944 rakFree_Ex(data, __FILE__, __LINE__ );
06945 }
06946 }
06947 }
06948
06949
06950
06951 bitSize = remoteSystem->reliabilityLayer.Receive( &data );
06952 }
06953 }
06954 }
06955
06956 return true;
06957 }
06958
06959 RAK_THREAD_DECLARATION(RecvFromLoop)
06960 {
06961 RakPeerAndIndex *rpai = ( RakPeerAndIndex * ) arguments;
06962 RakPeer * rakPeer = rpai->rakPeer;
06963 SOCKET s = rpai->s;
06964 unsigned short remotePortRakNetWasStartedOn_PS3 = rpai->remotePortRakNetWasStartedOn_PS3;
06965
06966 rakPeer->isRecvFromLoopThreadActive = true;
06967
06968 RakPeer::RecvFromStruct *recvFromStruct;
06969 while ( rakPeer->endThreads == false )
06970 {
06971 recvFromStruct=rakPeer->bufferedPackets.Allocate( __FILE__, __LINE__ );
06972 recvFromStruct->s=s;
06973 recvFromStruct->remotePortRakNetWasStartedOn_PS3=remotePortRakNetWasStartedOn_PS3;
06974 SocketLayer::RecvFromBlocking(s, rakPeer, remotePortRakNetWasStartedOn_PS3, recvFromStruct->data, &recvFromStruct->bytesRead, &recvFromStruct->systemAddress, &recvFromStruct->timeRead);
06975 if (recvFromStruct->bytesRead>0)
06976 {
06977 RakAssert(recvFromStruct->systemAddress.port);
06978 rakPeer->bufferedPackets.Push(recvFromStruct);
06979
06980 rakPeer->quitAndDataEvents.SetEvent();
06981 }
06982 else
06983 {
06984 rakPeer->bufferedPackets.Deallocate(recvFromStruct, __FILE__,__LINE__);
06985 }
06986 }
06987 rakPeer->isRecvFromLoopThreadActive = false;
06988 return 0;
06989 }
06990
06991 RAK_THREAD_DECLARATION(UpdateNetworkLoop)
06992 {
06993 RakPeer * rakPeer = ( RakPeer * ) arguments;
06994
06995
06996
06997
06998
06999
07000
07001
07002
07003
07004
07005
07006
07007
07008
07009
07010
07011
07012
07013
07014
07015
07016
07017
07018
07019
07020
07021 rakPeer->isMainLoopThreadActive = true;
07022
07023 while ( rakPeer->endThreads == false )
07024 {
07025 if (rakPeer->userUpdateThreadPtr)
07026 rakPeer->userUpdateThreadPtr(rakPeer, rakPeer->userUpdateThreadData);
07027
07028 rakPeer->RunUpdateCycle();
07029
07030
07031
07032 rakPeer->quitAndDataEvents.WaitOnEvent(10);
07033
07034
07035
07036
07037
07038
07039
07040
07041
07042
07043
07044
07045
07046
07047
07048
07049
07050
07051
07052
07053
07054
07055
07056
07057
07058
07059
07060
07061
07062
07063
07064
07065
07066 }
07067
07068 rakPeer->isMainLoopThreadActive = false;
07069
07070
07071
07072
07073
07074
07075
07076
07077
07078 return 0;
07079 }
07080
07081 #if defined(RMO_NEW_UNDEF_ALLOCATING_QUEUE)
07082 #pragma pop_macro("new")
07083 #undef RMO_NEW_UNDEF_ALLOCATING_QUEUE
07084 #endif
07085
07086
07087 #ifdef _MSC_VER
07088 #pragma warning( pop )
07089 #endif