CrossConnectionConvertTest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "CrossConnectionConvertTest.h"
  11. /*
  12. Description: Tests what happens if two instances of RakNet connect to each other at the same time. This has caused handshaking problems in the past.
  13. Success conditions:
  14. Everything connects and sends normally.
  15. Failure conditions:
  16. Expected values from ping/pong do not occur within expected time.
  17. */
  18. int CrossConnectionConvertTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  19. {
  20. static const unsigned short SERVER_PORT=1234;
  21. // char serverMode[32];
  22. char serverIP[64];
  23. strcpy(serverIP,"127.0.0.1");
  24. char clientIP[64];
  25. RakPeerInterface *server,*client;
  26. unsigned short clientPort;
  27. bool gotNotification;
  28. server=RakPeerInterface::GetInstance();
  29. destroyList.Clear(false,_FILE_AND_LINE_);
  30. destroyList.Push(server,_FILE_AND_LINE_);
  31. client=RakPeerInterface::GetInstance();
  32. destroyList.Push(client,_FILE_AND_LINE_);
  33. server->Startup(1,&SocketDescriptor(SERVER_PORT,0), 1);
  34. server->SetMaximumIncomingConnections(1);
  35. client->Startup(1,&SocketDescriptor(0,0), 1);
  36. client->Ping(serverIP,SERVER_PORT,false);
  37. // PacketLogger pl;
  38. // pl.LogHeader();
  39. // rakPeer->AttachPlugin(&pl);
  40. TimeMS connectionAttemptTime=0,connectionResultDeterminationTime=0,nextTestStartTime=0;
  41. TimeMS entryTime=GetTimeMS();//Loop entry time
  42. bool printedYet=false;
  43. while(GetTimeMS()-entryTime<10000)//Run for 10 Secoonds
  44. {
  45. Packet *p;
  46. printedYet=false;
  47. for (p=server->Receive(); p; server->DeallocatePacket(p), p=server->Receive())
  48. {
  49. if (isVerbose&&!printedYet)
  50. {
  51. printf("Server:\n");
  52. printedYet=true;
  53. }
  54. if (p->data[0]==ID_NEW_INCOMING_CONNECTION)
  55. {
  56. if (isVerbose)
  57. printf("ID_NEW_INCOMING_CONNECTION\n");
  58. gotNotification=true;
  59. }
  60. else if (p->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  61. {
  62. if (isVerbose)
  63. printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
  64. gotNotification=true;
  65. }
  66. else if (p->data[0]==ID_UNCONNECTED_PING)
  67. {
  68. if (isVerbose)
  69. printf("ID_PING\n");
  70. connectionAttemptTime=GetTimeMS()+1000;
  71. p->systemAddress.ToString(false,clientIP);
  72. clientPort=p->systemAddress.port;
  73. gotNotification=false;
  74. }
  75. else if (p->data[0]==ID_UNCONNECTED_PONG)
  76. {
  77. if (isVerbose)
  78. printf("ID_UNCONNECTED_PONG\n");
  79. TimeMS sendPingTime;
  80. BitStream bs(p->data,p->length,false);
  81. bs.IgnoreBytes(1);
  82. bs.Read(sendPingTime);
  83. TimeMS rtt = GetTimeMS() - sendPingTime;
  84. if (rtt/2<=500)
  85. connectionAttemptTime=GetTimeMS()+1000-rtt/2;
  86. else
  87. connectionAttemptTime=GetTimeMS();
  88. gotNotification=false;
  89. }
  90. }
  91. printedYet=false;
  92. for (p=client->Receive(); p; client->DeallocatePacket(p), p=client->Receive())
  93. {
  94. if (isVerbose&&!printedYet)
  95. {
  96. printf("Client:\n");
  97. printedYet=true;
  98. }
  99. if (p->data[0]==ID_NEW_INCOMING_CONNECTION)
  100. {
  101. if (isVerbose)
  102. printf("ID_NEW_INCOMING_CONNECTION\n");
  103. gotNotification=true;
  104. }
  105. else if (p->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  106. {
  107. if (isVerbose)
  108. printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
  109. gotNotification=true;
  110. }
  111. else if (p->data[0]==ID_UNCONNECTED_PING)
  112. {
  113. if (isVerbose)
  114. printf("ID_PING\n");
  115. connectionAttemptTime=GetTimeMS()+1000;
  116. p->systemAddress.ToString(false,clientIP);
  117. clientPort=p->systemAddress.port;
  118. gotNotification=false;
  119. }
  120. else if (p->data[0]==ID_UNCONNECTED_PONG)
  121. {
  122. if (isVerbose)
  123. printf("ID_UNCONNECTED_PONG\n");
  124. TimeMS sendPingTime;
  125. BitStream bs(p->data,p->length,false);
  126. bs.IgnoreBytes(1);
  127. bs.Read(sendPingTime);
  128. TimeMS rtt = GetTimeMS() - sendPingTime;
  129. if (rtt/2<=500)
  130. connectionAttemptTime=GetTimeMS()+1000-rtt/2;
  131. else
  132. connectionAttemptTime=GetTimeMS();
  133. gotNotification=false;
  134. }
  135. }
  136. if (connectionAttemptTime!=0 && GetTimeMS()>=connectionAttemptTime)
  137. {
  138. if (isVerbose)
  139. printf("Attemping connection\n");
  140. connectionAttemptTime=0;
  141. server->Connect(clientIP,clientPort,0,0);
  142. client->Connect(serverIP,SERVER_PORT,0,0);
  143. connectionResultDeterminationTime=GetTimeMS()+2000;
  144. }
  145. if (connectionResultDeterminationTime!=0 && GetTimeMS()>=connectionResultDeterminationTime)
  146. {
  147. connectionResultDeterminationTime=0;
  148. if (gotNotification==false)
  149. {
  150. DebugTools::ShowError("Did not recieve expected response. \n",!noPauses && isVerbose,__LINE__,__FILE__);
  151. return 1;
  152. }
  153. SystemAddress sa;
  154. sa.SetBinaryAddress(serverIP);
  155. sa.port=SERVER_PORT;
  156. client->CancelConnectionAttempt(sa);
  157. sa.SetBinaryAddress(clientIP);
  158. sa.port=clientPort;
  159. server->CancelConnectionAttempt(sa);
  160. server->CloseConnection(server->GetSystemAddressFromIndex(0),true,0);
  161. client->CloseConnection(client->GetSystemAddressFromIndex(0),true,0);
  162. //if (isServer==false)
  163. nextTestStartTime=GetTimeMS()+1000;
  164. }
  165. if (nextTestStartTime!=0 && GetTimeMS()>=nextTestStartTime)
  166. {
  167. client->Ping(serverIP,SERVER_PORT,false);
  168. nextTestStartTime=0;
  169. }
  170. RakSleep(0);
  171. }
  172. if (isVerbose)
  173. printf("Test succeeded.\n");
  174. return 0;
  175. }
  176. RakString CrossConnectionConvertTest::GetTestName()
  177. {
  178. return "CrossConnectionConvertTest";
  179. }
  180. RakString CrossConnectionConvertTest::ErrorCodeToString(int errorCode)
  181. {
  182. switch (errorCode)
  183. {
  184. case 0:
  185. return "No error";
  186. break;
  187. case 1:
  188. return "Did not recieve expected response";
  189. break;
  190. default:
  191. return "Undefined Error";
  192. }
  193. }
  194. void CrossConnectionConvertTest::DestroyPeers()
  195. {
  196. int theSize=destroyList.Size();
  197. for (int i=0; i < theSize; i++)
  198. RakPeerInterface::DestroyInstance(destroyList[i]);
  199. }
  200. CrossConnectionConvertTest::CrossConnectionConvertTest(void)
  201. {
  202. }
  203. CrossConnectionConvertTest::~CrossConnectionConvertTest(void)
  204. {
  205. }
粤ICP备19079148号