ComprehensiveConvertTest.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 "ComprehensiveConvertTest.h"
  11. /*
  12. Description: Does a little bit of everything forever. This is an internal sample just to see if RakNet crashes or leaks memory over a long period of time.
  13. Todo:
  14. RPC replacement tests when RPC3 includes work.
  15. Success conditions:
  16. No failures.
  17. Failure conditions:
  18. Connect fails without pending ops or current connection.
  19. */
  20. int ComprehensiveConvertTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  21. {
  22. static const int CONNECTIONS_PER_SYSTEM =4;
  23. SystemAddress currentSystem;
  24. // DebugTools::ShowError("Note: The conversion of this is on hold until the original sample's problem is known.",!noPauses && isVerbose,__LINE__,__FILE__);
  25. // return 55;
  26. // AutoRPC autoRpcs[NUM_PEERS];
  27. //AutoRPC autoRpcs[NUM_PEERS];
  28. int peerIndex;
  29. float nextAction;
  30. int i;
  31. int portAdd;
  32. char data[8096];
  33. int seed = 12345;
  34. if (isVerbose)
  35. printf("Using seed %i\n", seed);
  36. seedMT(seed);
  37. for (i=0; i < NUM_PEERS; i++)
  38. {
  39. //autoRpcs[i].RegisterFunction("RPC1", RPC1, false);
  40. //autoRpcs[i].RegisterFunction("RPC2", RPC2, false);
  41. //autoRpcs[i].RegisterFunction("RPC3", RPC3, false);
  42. //autoRpcs[i].RegisterFunction("RPC4", RPC4, false);
  43. peers[i]=RakPeerInterface::GetInstance();
  44. peers[i]->SetMaximumIncomingConnections(CONNECTIONS_PER_SYSTEM);
  45. SocketDescriptor socketDescriptor(60000+i, 0);
  46. peers[i]->Startup(NUM_PEERS, &socketDescriptor, 1);
  47. peers[i]->SetOfflinePingResponse("Offline Ping Data", (int)strlen("Offline Ping Data")+1);
  48. }
  49. for (i=0; i < NUM_PEERS; i++)
  50. {
  51. portAdd=randomMT()%NUM_PEERS;
  52. currentSystem.SetBinaryAddress("127.0.0.1");
  53. currentSystem.port=60000+portAdd;
  54. if(!CommonFunctions::ConnectionStateMatchesOptions (peers[i],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ?
  55. {
  56. ConnectionAttemptResult resultReturn = peers[i]->Connect("127.0.0.1", 60000+portAdd, 0, 0);
  57. if (resultReturn!=CONNECTION_ATTEMPT_STARTED && resultReturn!=ALREADY_CONNECTED_TO_ENDPOINT)
  58. {
  59. DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__);
  60. return 1;
  61. }
  62. }
  63. }
  64. TimeMS endTime = GetTimeMS()+10000;
  65. while (GetTimeMS()<endTime)
  66. {
  67. nextAction = frandomMT();
  68. if (nextAction < .04f)
  69. {
  70. // Initialize
  71. peerIndex=randomMT()%NUM_PEERS;
  72. SocketDescriptor socketDescriptor(60000+peerIndex, 0);
  73. peers[peerIndex]->Startup(NUM_PEERS, &socketDescriptor, 1);
  74. portAdd=randomMT()%NUM_PEERS;
  75. currentSystem.SetBinaryAddress("127.0.0.1");
  76. currentSystem.port=60000+portAdd;
  77. if(!CommonFunctions::ConnectionStateMatchesOptions (peers[peerIndex],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ?
  78. {
  79. ConnectionAttemptResult resultReturn = peers[peerIndex]->Connect("127.0.0.1", 60000+portAdd, 0, 0);
  80. if (resultReturn!=CONNECTION_ATTEMPT_STARTED && resultReturn!=ALREADY_CONNECTED_TO_ENDPOINT)
  81. {
  82. DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__);
  83. return 1;
  84. }
  85. }
  86. }
  87. else if (nextAction < .09f)
  88. {
  89. // Connect
  90. peerIndex=randomMT()%NUM_PEERS;
  91. portAdd=randomMT()%NUM_PEERS;
  92. currentSystem.SetBinaryAddress("127.0.0.1");
  93. currentSystem.port=60000+portAdd;
  94. if(!CommonFunctions::ConnectionStateMatchesOptions (peers[peerIndex],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ?
  95. {
  96. ConnectionAttemptResult resultReturn = peers[peerIndex]->Connect("127.0.0.1", 60000+portAdd, 0, 0);
  97. if (resultReturn!=CONNECTION_ATTEMPT_STARTED && resultReturn!=ALREADY_CONNECTED_TO_ENDPOINT)
  98. {
  99. DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__);
  100. return 1;
  101. }
  102. }
  103. }
  104. else if (nextAction < .10f)
  105. {
  106. // Disconnect
  107. peerIndex=randomMT()%NUM_PEERS;
  108. // peers[peerIndex]->Shutdown(randomMT() % 100);
  109. }
  110. else if (nextAction < .12f)
  111. {
  112. // GetConnectionList
  113. peerIndex=randomMT()%NUM_PEERS;
  114. SystemAddress remoteSystems[NUM_PEERS];
  115. unsigned short numSystems=NUM_PEERS;
  116. peers[peerIndex]->GetConnectionList(remoteSystems, &numSystems);
  117. if (numSystems>0)
  118. {
  119. if (isVerbose){
  120. printf("%i: ", 60000+numSystems);
  121. for (i=0; i < numSystems; i++)
  122. {
  123. printf("%i: ", remoteSystems[i].port);
  124. }
  125. printf("\n");
  126. }
  127. }
  128. }
  129. else if (nextAction < .14f)
  130. {
  131. // Send
  132. int dataLength;
  133. PacketPriority priority;
  134. PacketReliability reliability;
  135. unsigned char orderingChannel;
  136. SystemAddress target;
  137. bool broadcast;
  138. // data[0]=ID_RESERVED1+(randomMT()%10);
  139. data[0]=ID_USER_PACKET_ENUM;
  140. dataLength=3+(randomMT()%8000);
  141. // dataLength=600+(randomMT()%7000);
  142. priority=(PacketPriority)(randomMT()%(int)NUMBER_OF_PRIORITIES);
  143. reliability=(PacketReliability)(randomMT()%((int)RELIABLE_SEQUENCED+1));
  144. orderingChannel=randomMT()%32;
  145. if ((randomMT()%NUM_PEERS)==0)
  146. target=UNASSIGNED_SYSTEM_ADDRESS;
  147. else
  148. target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
  149. broadcast=(randomMT()%2)>0;
  150. #ifdef _VERIFY_RECIPIENTS
  151. broadcast=false; // Temporarily in so I can check recipients
  152. #endif
  153. peerIndex=randomMT()%NUM_PEERS;
  154. sprintf(data+3, "dataLength=%i priority=%i reliability=%i orderingChannel=%i target=%i broadcast=%i\n", dataLength, priority, reliability, orderingChannel, target.port, broadcast);
  155. //unsigned short localPort=60000+i;
  156. #ifdef _VERIFY_RECIPIENTS
  157. memcpy((char*)data+1, (char*)&target.port, sizeof(unsigned short));
  158. #endif
  159. data[dataLength-1]=0;
  160. peers[peerIndex]->Send(data, dataLength, priority, reliability, orderingChannel, target, broadcast);
  161. }
  162. else if (nextAction < .18f)
  163. {
  164. // RPC
  165. int dataLength;
  166. PacketPriority priority;
  167. PacketReliability reliability;
  168. unsigned char orderingChannel;
  169. SystemAddress target;
  170. bool broadcast;
  171. char RPCName[10];
  172. data[0]=ID_USER_PACKET_ENUM+(randomMT()%10);
  173. dataLength=3+(randomMT()%8000);
  174. // dataLength=600+(randomMT()%7000);
  175. priority=(PacketPriority)(randomMT()%(int)NUMBER_OF_PRIORITIES);
  176. reliability=(PacketReliability)(randomMT()%((int)RELIABLE_SEQUENCED+1));
  177. orderingChannel=randomMT()%32;
  178. peerIndex=randomMT()%NUM_PEERS;
  179. if ((randomMT()%NUM_PEERS)==0)
  180. target=UNASSIGNED_SYSTEM_ADDRESS;
  181. else
  182. target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
  183. broadcast=(randomMT()%2)>0;
  184. #ifdef _VERIFY_RECIPIENTS
  185. broadcast=false; // Temporarily in so I can check recipients
  186. #endif
  187. sprintf(data+3, "dataLength=%i priority=%i reliability=%i orderingChannel=%i target=%i broadcast=%i\n", dataLength, priority, reliability, orderingChannel, target.port, broadcast);
  188. #ifdef _VERIFY_RECIPIENTS
  189. memcpy((char*)data, (char*)&target.port, sizeof(unsigned short));
  190. #endif
  191. data[dataLength-1]=0;
  192. sprintf(RPCName, "RPC%i", (randomMT()%4)+1);
  193. // autoRpc[i]->Call(RPCName);
  194. //peers[peerIndex]->RPC(RPCName, data, dataLength*8, priority, reliability, orderingChannel, target, broadcast, 0, UNASSIGNED_NETWORK_ID,0);
  195. }
  196. else if (nextAction < .181f)
  197. {
  198. // CloseConnection
  199. SystemAddress target;
  200. peerIndex=randomMT()%NUM_PEERS;
  201. target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
  202. peers[peerIndex]->CloseConnection(target, (randomMT()%2)>0, 0);
  203. }
  204. else if (nextAction < .20f)
  205. {
  206. // Offline Ping
  207. peerIndex=randomMT()%NUM_PEERS;
  208. peers[peerIndex]->Ping("127.0.0.1", 60000+(randomMT()%NUM_PEERS), (randomMT()%2)>0);
  209. }
  210. else if (nextAction < .21f)
  211. {
  212. // Online Ping
  213. SystemAddress target;
  214. target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
  215. peerIndex=randomMT()%NUM_PEERS;
  216. peers[peerIndex]->Ping(target);
  217. }
  218. else if (nextAction < .24f)
  219. {
  220. // SetCompileFrequencyTable
  221. peerIndex=randomMT()%NUM_PEERS;
  222. }
  223. else if (nextAction < .25f)
  224. {
  225. // GetStatistics
  226. SystemAddress target, mySystemAddress;
  227. RakNetStatistics *rss;
  228. mySystemAddress=peers[peerIndex]->GetInternalID();
  229. target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
  230. peerIndex=randomMT()%NUM_PEERS;
  231. rss=peers[peerIndex]->GetStatistics(mySystemAddress);
  232. if (rss)
  233. {
  234. StatisticsToString(rss, data, 0);
  235. if (isVerbose)
  236. printf("Statistics for local system %i:\n%s", mySystemAddress.port, data);
  237. }
  238. rss=peers[peerIndex]->GetStatistics(target);
  239. if (rss)
  240. {
  241. StatisticsToString(rss, data, 0);
  242. if (isVerbose)
  243. printf("Statistics for target system %i:\n%s", target.port, data);
  244. }
  245. }
  246. for (i=0; i < NUM_PEERS; i++)
  247. peers[i]->DeallocatePacket(peers[i]->Receive());
  248. RakSleep(0);
  249. }
  250. return 0;
  251. }
  252. void ComprehensiveConvertTest::DestroyPeers()
  253. {
  254. for (int i=0; i < NUM_PEERS; i++)
  255. RakPeerInterface::DestroyInstance(peers[i]);
  256. }
  257. RakString ComprehensiveConvertTest::GetTestName()
  258. {
  259. return "ComprehensiveConvertTest";
  260. }
  261. RakString ComprehensiveConvertTest::ErrorCodeToString(int errorCode)
  262. {
  263. switch (errorCode)
  264. {
  265. case 0:
  266. return "No error";
  267. break;
  268. case 1:
  269. return "Connect function failed";
  270. break;
  271. default:
  272. return "Undefined Error";
  273. }
  274. }
  275. ComprehensiveConvertTest::ComprehensiveConvertTest(void)
  276. {
  277. }
  278. ComprehensiveConvertTest::~ComprehensiveConvertTest(void)
  279. {
  280. }
粤ICP备19079148号