ConnectWithSocketTest.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "ConnectWithSocketTest.h"
  11. /*
  12. Description:
  13. virtual bool RakPeerInterface::ConnectWithSocket ( const char * host, unsigned short remotePort, const char * passwordData, int passwordDataLength, RakNetSmartPtr< RakNetSocket > socket, unsigned sendConnectionAttemptCount = 7, unsigned timeBetweenSendConnectionAttemptsMS = 500, TimeMS timeoutTime = 0 )
  14. virtual void RakPeerInterface::GetSockets ( DataStructures::List< RakNetSmartPtr< RakNetSocket > > & sockets )
  15. virtual RakNetSmartPtr<RakNetSocket> RakPeerInterface::GetSocket ( const SystemAddress target ) [pure virtual]
  16. Success conditions:
  17. Failure conditions:
  18. RakPeerInterface Functions used, tested indirectly by its use:
  19. Startup
  20. SetMaximumIncomingConnections
  21. Receive
  22. DeallocatePacket
  23. Send
  24. IsConnected
  25. RakPeerInterface Functions Explicitly Tested:
  26. ConnectWithSocket
  27. GetSockets
  28. GetSocket
  29. */
  30. int ConnectWithSocketTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  31. {
  32. destroyList.Clear(false,_FILE_AND_LINE_);
  33. RakPeerInterface *server,*client;
  34. DataStructures::List< RakNetSmartPtr< RakNetSocket > > sockets;
  35. TestHelpers::StandardClientPrep(client,destroyList);
  36. TestHelpers::StandardServerPrep(server,destroyList);
  37. SystemAddress serverAddress;
  38. serverAddress.SetBinaryAddress("127.0.0.1");
  39. serverAddress.port=60000;
  40. printf("Testing normal connect before test\n");
  41. if (!TestHelpers::WaitAndConnectTwoPeersLocally(client,server,5000))
  42. {
  43. if (isVerbose)
  44. DebugTools::ShowError(errorList[1-1],!noPauses && isVerbose,__LINE__,__FILE__);
  45. return 1;
  46. }
  47. TestHelpers::BroadCastTestPacket(client);
  48. if (!TestHelpers::WaitForTestPacket(server,5000))
  49. {
  50. if (isVerbose)
  51. DebugTools::ShowError(errorList[2-1],!noPauses && isVerbose,__LINE__,__FILE__);
  52. return 2;
  53. }
  54. printf("Disconnecting client\n");
  55. CommonFunctions::DisconnectAndWait(client,"127.0.0.1",60000);
  56. RakNetSmartPtr<RakNetSocket> theSocket;
  57. client->GetSockets(sockets);
  58. theSocket=sockets[0];
  59. RakTimer timer2(5000);
  60. printf("Testing ConnectWithSocket using socket from GetSockets\n");
  61. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&!timer2.IsExpired())
  62. {
  63. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  64. {
  65. client->ConnectWithSocket("127.0.0.1",serverAddress.port,0,0,theSocket);
  66. }
  67. RakSleep(100);
  68. }
  69. if (!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  70. {
  71. if (isVerbose)
  72. DebugTools::ShowError(errorList[3-1],!noPauses && isVerbose,__LINE__,__FILE__);
  73. return 3;
  74. }
  75. TestHelpers::BroadCastTestPacket(client);
  76. if (!TestHelpers::WaitForTestPacket(server,5000))
  77. {
  78. if (isVerbose)
  79. DebugTools::ShowError(errorList[4-1],!noPauses && isVerbose,__LINE__,__FILE__);
  80. return 4;
  81. }
  82. printf("Disconnecting client\n");
  83. CommonFunctions::DisconnectAndWait(client,"127.0.0.1",60000);
  84. printf("Testing ConnectWithSocket using socket from GetSocket\n");
  85. theSocket=client->GetSocket(UNASSIGNED_SYSTEM_ADDRESS);//Get open Socket
  86. timer2.Start();
  87. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&!timer2.IsExpired())
  88. {
  89. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  90. {
  91. client->ConnectWithSocket("127.0.0.1",serverAddress.port,0,0,theSocket);
  92. }
  93. RakSleep(100);
  94. }
  95. if (!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  96. {
  97. if (isVerbose)
  98. DebugTools::ShowError(errorList[5-1],!noPauses && isVerbose,__LINE__,__FILE__);
  99. return 5;
  100. }
  101. TestHelpers::BroadCastTestPacket(client);
  102. if (!TestHelpers::WaitForTestPacket(server,5000))
  103. {
  104. if (isVerbose)
  105. DebugTools::ShowError(errorList[6-1],!noPauses && isVerbose,__LINE__,__FILE__);
  106. return 6;
  107. }
  108. return 0;
  109. }
  110. RakString ConnectWithSocketTest::GetTestName()
  111. {
  112. return "ConnectWithSocketTest";
  113. }
  114. RakString ConnectWithSocketTest::ErrorCodeToString(int errorCode)
  115. {
  116. if (errorCode>0&&(unsigned int)errorCode<=errorList.Size())
  117. {
  118. return errorList[errorCode-1];
  119. }
  120. else
  121. {
  122. return "Undefined Error";
  123. }
  124. }
  125. ConnectWithSocketTest::ConnectWithSocketTest(void)
  126. {
  127. errorList.Push("Client did not connect after 5 seconds",_FILE_AND_LINE_);
  128. errorList.Push("Control test send didn't work",_FILE_AND_LINE_);
  129. errorList.Push("Client did not connect after 5 secods Using ConnectWithSocket, could be GetSockets or ConnectWithSocket problem",_FILE_AND_LINE_);
  130. errorList.Push("Server did not recieve test packet from client",_FILE_AND_LINE_);
  131. errorList.Push("Client did not connect after 5 secods Using ConnectWithSocket, could be GetSocket or ConnectWithSocket problem",_FILE_AND_LINE_);
  132. errorList.Push("Server did not recieve test packet from client",_FILE_AND_LINE_);
  133. }
  134. ConnectWithSocketTest::~ConnectWithSocketTest(void)
  135. {
  136. }
  137. void ConnectWithSocketTest::DestroyPeers()
  138. {
  139. int theSize=destroyList.Size();
  140. for (int i=0; i < theSize; i++)
  141. RakPeerInterface::DestroyInstance(destroyList[i]);
  142. }
粤ICP备19079148号