PeerConnectDisconnectTest.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 "PeerConnectDisconnectTest.h"
  11. void PeerConnectDisconnectTest::WaitForConnectionRequestsToComplete(RakPeerInterface **peerList, int peerNum, bool isVerbose)
  12. {
  13. SystemAddress currentSystem;
  14. bool msgWasPrinted=false;
  15. for (int i=0;i<peerNum;i++)
  16. {
  17. for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together.
  18. {
  19. currentSystem.SetBinaryAddress("127.0.0.1");
  20. currentSystem.port=60000+j;
  21. while (CommonFunctions::ConnectionStateMatchesOptions (peerList[i],currentSystem,false,true,true) )
  22. {
  23. if (msgWasPrinted==false)
  24. {
  25. printf("Waiting for connection requests to complete.\n");
  26. msgWasPrinted=true;
  27. }
  28. RakSleep(30);
  29. }
  30. }
  31. }
  32. }
  33. void PeerConnectDisconnectTest::WaitAndPrintResults(RakPeerInterface **peerList, int peerNum, bool isVerbose)
  34. {
  35. WaitForConnectionRequestsToComplete(peerList,peerNum,isVerbose);
  36. Packet *packet;
  37. // Log all events per peer
  38. for (int i=0;i<peerNum;i++)//Receive for all peers
  39. {
  40. if (isVerbose)
  41. printf("For peer %i\n",i);
  42. for (packet=peerList[i]->Receive(); packet; peerList[i]->DeallocatePacket(packet), packet=peerList[i]->Receive())
  43. {
  44. switch (packet->data[0])
  45. {
  46. case ID_REMOTE_DISCONNECTION_NOTIFICATION:
  47. if (isVerbose)
  48. printf("Another client has disconnected.\n");
  49. break;
  50. case ID_REMOTE_CONNECTION_LOST:
  51. if (isVerbose)
  52. printf("Another client has lost the connection.\n");
  53. break;
  54. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  55. if (isVerbose)
  56. printf("Another client has connected.\n");
  57. break;
  58. case ID_CONNECTION_REQUEST_ACCEPTED:
  59. if (isVerbose)
  60. printf("Our connection request has been accepted.\n");
  61. break;
  62. case ID_CONNECTION_ATTEMPT_FAILED:
  63. if (isVerbose)
  64. printf("A connection has failed.\n");
  65. break;
  66. case ID_NEW_INCOMING_CONNECTION:
  67. if (isVerbose)
  68. printf("A connection is incoming.\n");
  69. break;
  70. case ID_NO_FREE_INCOMING_CONNECTIONS:
  71. if (isVerbose)
  72. printf("The server is full.\n");
  73. break;
  74. case ID_ALREADY_CONNECTED:
  75. if (isVerbose)
  76. printf("Already connected\n");
  77. break;
  78. case ID_DISCONNECTION_NOTIFICATION:
  79. if (isVerbose)
  80. printf("We have been disconnected.\n");
  81. break;
  82. case ID_CONNECTION_LOST:
  83. if (isVerbose)
  84. printf("Connection lost.\n");
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. /*
  93. What is being done here is having 8 peers all connect to eachother, disconnect, connect again.
  94. Do this for about 10 seconds. Then allow them all to connect for one last time.
  95. Good ideas for changes:
  96. After the last check run a eightpeers like test an add the conditions
  97. of that test as well.
  98. Make sure that if we initiate the connection we get a proper message
  99. and if not we get a proper message. Add proper conditions.
  100. Randomize sending the disconnect notes
  101. Success conditions:
  102. All connected normally.
  103. Failure conditions:
  104. Doesn't reconnect normally.
  105. During the very first connect loop any connect returns false.
  106. Connect function returns false and peer is not connected to anything.
  107. */
  108. int PeerConnectDisconnectTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  109. {
  110. const int peerNum= 8;
  111. const int maxConnections=peerNum*3;//Max allowed connections for test set to times 3 to eliminate problem variables
  112. RakPeerInterface *peerList[peerNum];//A list of 8 peers
  113. SystemAddress currentSystem;
  114. destroyList.Clear(false,_FILE_AND_LINE_);
  115. //Initializations of the arrays
  116. for (int i=0;i<peerNum;i++)
  117. {
  118. peerList[i]=RakPeerInterface::GetInstance();
  119. destroyList.Push(peerList[i],_FILE_AND_LINE_);
  120. peerList[i]->Startup(maxConnections, &SocketDescriptor(60000+i,0), 1);
  121. peerList[i]->SetMaximumIncomingConnections(maxConnections);
  122. }
  123. //Connect all the peers together
  124. for (int i=0;i<peerNum;i++)
  125. {
  126. for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together.
  127. {
  128. if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED)
  129. {
  130. if (isVerbose)
  131. DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__);
  132. return 1;//This fails the test, don't bother going on.
  133. }
  134. }
  135. }
  136. TimeMS entryTime=GetTimeMS();//Loop entry time
  137. DataStructures::List< SystemAddress > systemList;
  138. DataStructures::List< RakNetGUID > guidList;
  139. printf("Entering disconnect loop \n");
  140. while(GetTimeMS()-entryTime<10000)//Run for 10 Secoonds
  141. {
  142. //Disconnect all peers IF connected to any
  143. for (int i=0;i<peerNum;i++)
  144. {
  145. peerList[i]->GetSystemList(systemList,guidList);//Get connectionlist
  146. int len=systemList.Size();
  147. for (int j=0;j<len;j++)//Disconnect them all
  148. {
  149. peerList[i]->CloseConnection (systemList[j],true,0,LOW_PRIORITY);
  150. }
  151. }
  152. RakSleep(100);
  153. //Connect
  154. for (int i=0;i<peerNum;i++)
  155. {
  156. for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together.
  157. {
  158. currentSystem.SetBinaryAddress("127.0.0.1");
  159. currentSystem.port=60000+j;
  160. if(!CommonFunctions::ConnectionStateMatchesOptions (peerList[i],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ?
  161. {
  162. if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED)
  163. {
  164. if (isVerbose)
  165. DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__);
  166. return 1;//This fails the test, don't bother going on.
  167. }
  168. }
  169. }
  170. }
  171. WaitAndPrintResults(peerList,peerNum,isVerbose);
  172. }
  173. WaitAndPrintResults(peerList,peerNum,isVerbose);
  174. printf("Connecting peers\n");
  175. //Connect
  176. for (int i=0;i<peerNum;i++)
  177. {
  178. for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together.
  179. {
  180. currentSystem.SetBinaryAddress("127.0.0.1");
  181. currentSystem.port=60000+j;
  182. if(!CommonFunctions::ConnectionStateMatchesOptions (peerList[i],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ?
  183. {
  184. printf("Calling Connect() for peer %i to peer %i.\n",i,j);
  185. if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED)
  186. {
  187. peerList[i]->GetSystemList(systemList,guidList);//Get connectionlist
  188. int len=systemList.Size();
  189. if (isVerbose)
  190. DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__);
  191. return 1;//This fails the test, don't bother going on.
  192. }
  193. }
  194. else
  195. {
  196. if (CommonFunctions::ConnectionStateMatchesOptions (peerList[i],currentSystem,false,false,false,true)==false)
  197. printf("Not calling Connect() for peer %i to peer %i because it is disconnecting.\n",i,j);
  198. else if (CommonFunctions::ConnectionStateMatchesOptions (peerList[i],currentSystem,false,true,true)==false)
  199. printf("Not calling Connect() for peer %i to peer %i because it is connecting.\n",i,j);
  200. else if (CommonFunctions::ConnectionStateMatchesOptions (peerList[i],currentSystem,true)==false)
  201. printf("Not calling Connect() for peer %i to peer %i because it is connected).\n",i,j);
  202. }
  203. }
  204. }
  205. WaitAndPrintResults(peerList,peerNum,isVerbose);
  206. for (int i=0;i<peerNum;i++)
  207. {
  208. peerList[i]->GetSystemList(systemList,guidList);
  209. int connNum=guidList.Size();//Get the number of connections for the current peer
  210. if (connNum!=peerNum-1)//Did we connect to all?
  211. {
  212. if (isVerbose)
  213. {
  214. printf("Not all peers reconnected normally.\nFailed on peer number %i with %i peers\n",i,connNum);
  215. DebugTools::ShowError("",!noPauses && isVerbose,__LINE__,__FILE__);
  216. }
  217. return 2;
  218. }
  219. }
  220. if (isVerbose)
  221. printf("Pass\n");
  222. return 0;
  223. }
  224. RakString PeerConnectDisconnectTest::GetTestName()
  225. {
  226. return "PeerConnectDisconnectTest";
  227. }
  228. RakString PeerConnectDisconnectTest::ErrorCodeToString(int errorCode)
  229. {
  230. switch (errorCode)
  231. {
  232. case 0:
  233. return "No error";
  234. break;
  235. case 1:
  236. return "The connect function failed.";
  237. break;
  238. case 2:
  239. return "Peers did not connect normally.";
  240. break;
  241. default:
  242. return "Undefined Error";
  243. }
  244. }
  245. PeerConnectDisconnectTest::PeerConnectDisconnectTest(void)
  246. {
  247. }
  248. PeerConnectDisconnectTest::~PeerConnectDisconnectTest(void)
  249. {
  250. }
  251. void PeerConnectDisconnectTest::DestroyPeers()
  252. {
  253. int theSize=destroyList.Size();
  254. for (int i=0; i < theSize; i++)
  255. RakPeerInterface::DestroyInstance(destroyList[i]);
  256. }
粤ICP备19079148号