ManyClientsOneServerBlockingTest.cpp 10 KB

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