ComprehensiveTest.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "RakPeerInterface.h"
  11. #include "BitStream.h"
  12. #include <stdlib.h> // For atoi
  13. #include <cstring> // For strlen
  14. #include "Rand.h"
  15. #include "RakNetStatistics.h"
  16. #include "MessageIdentifiers.h"
  17. #include <stdio.h>
  18. #include "GetTime.h"
  19. using namespace RakNet;
  20. #ifdef _WIN32
  21. #include "WindowsIncludes.h" // Sleep
  22. #else
  23. #include <unistd.h> // usleep
  24. #include <cstdio>
  25. #endif
  26. //#define _VERIFY_RECIPIENTS
  27. #define _DO_PRINTF
  28. #define NUM_PEERS 10
  29. #define CONNECTIONS_PER_SYSTEM 4
  30. int main(void)
  31. {
  32. RakPeerInterface *peers[NUM_PEERS];
  33. int peerIndex;
  34. float nextAction;
  35. int i;
  36. printf("This is just a test app to run a bit of everything to test for crashes.\n");
  37. printf("Difficulty: Intermediate\n\n");
  38. char data[8096];
  39. int seed = 12345;
  40. printf("Using seed %i\n", seed);
  41. seedMT(seed);
  42. for (i=0; i < NUM_PEERS; i++)
  43. {
  44. peers[i]=RakNet::RakPeerInterface::GetInstance();
  45. peers[i]->SetMaximumIncomingConnections(CONNECTIONS_PER_SYSTEM);
  46. RakNet::SocketDescriptor socketDescriptor(60000+i, 0);
  47. peers[i]->Startup(NUM_PEERS, &socketDescriptor, 1);
  48. peers[i]->SetOfflinePingResponse("Offline Ping Data", (int)strlen("Offline Ping Data")+1);
  49. }
  50. for (i=0; i < NUM_PEERS; i++)
  51. {
  52. peers[i]->Connect("127.0.0.1", 60000+(randomMT()%NUM_PEERS), 0, 0);
  53. }
  54. RakNet::TimeMS endTime = RakNet::GetTimeMS()+600000;
  55. while (RakNet::GetTimeMS()<endTime)
  56. {
  57. nextAction = frandomMT();
  58. if (nextAction < .04f)
  59. {
  60. // Initialize
  61. peerIndex=randomMT()%NUM_PEERS;
  62. RakNet::SocketDescriptor socketDescriptor(60000+peerIndex, 0);
  63. peers[peerIndex]->Startup(NUM_PEERS, &socketDescriptor, 1);
  64. peers[peerIndex]->Connect("127.0.0.1", 60000+randomMT() % NUM_PEERS, 0, 0);
  65. }
  66. else if (nextAction < .09f)
  67. {
  68. // Connect
  69. peerIndex=randomMT()%NUM_PEERS;
  70. peers[peerIndex]->Connect("127.0.0.1", 60000+randomMT() % NUM_PEERS, 0, 0);
  71. }
  72. else if (nextAction < .10f)
  73. {
  74. // Disconnect
  75. peerIndex=randomMT()%NUM_PEERS;
  76. // peers[peerIndex]->Shutdown(randomMT() % 100);
  77. }
  78. else if (nextAction < .12f)
  79. {
  80. // GetConnectionList
  81. peerIndex=randomMT()%NUM_PEERS;
  82. SystemAddress remoteSystems[NUM_PEERS];
  83. unsigned short numSystems=NUM_PEERS;
  84. peers[peerIndex]->GetConnectionList(remoteSystems, &numSystems);
  85. if (numSystems>0)
  86. {
  87. #ifdef _DO_PRINTF
  88. printf("%i: ", 60000+numSystems);
  89. for (i=0; i < numSystems; i++)
  90. {
  91. printf("%i: ", remoteSystems[i].GetPort());
  92. }
  93. printf("\n");
  94. #endif
  95. }
  96. }
  97. else if (nextAction < .14f)
  98. {
  99. // Send
  100. int dataLength;
  101. PacketPriority priority;
  102. PacketReliability reliability;
  103. unsigned char orderingChannel;
  104. SystemAddress target;
  105. bool broadcast;
  106. // data[0]=ID_RESERVED1+(randomMT()%10);
  107. data[0]=ID_USER_PACKET_ENUM;
  108. dataLength=3+(randomMT()%8000);
  109. // dataLength=600+(randomMT()%7000);
  110. priority=(PacketPriority)(randomMT()%(int)NUMBER_OF_PRIORITIES);
  111. reliability=(PacketReliability)(randomMT()%((int)RELIABLE_SEQUENCED+1));
  112. orderingChannel=randomMT()%32;
  113. if ((randomMT()%NUM_PEERS)==0)
  114. target=RakNet::UNASSIGNED_SYSTEM_ADDRESS;
  115. else
  116. target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
  117. broadcast=(bool)(randomMT()%2);
  118. #ifdef _VERIFY_RECIPIENTS
  119. broadcast=false; // Temporarily in so I can check recipients
  120. #endif
  121. peerIndex=randomMT()%NUM_PEERS;
  122. sprintf(data+3, "dataLength=%i priority=%i reliability=%i orderingChannel=%i target=%i broadcast=%i\n", dataLength, priority, reliability, orderingChannel, target.GetPort(), broadcast);
  123. //unsigned short localPort=60000+i;
  124. #ifdef _VERIFY_RECIPIENTS
  125. memcpy((char*)data+1, (char*)&target.port, sizeof(unsigned short));
  126. #endif
  127. data[dataLength-1]=0;
  128. peers[peerIndex]->Send(data, dataLength, priority, reliability, orderingChannel, target, broadcast);
  129. }
  130. else if (nextAction < .18f)
  131. {
  132. int dataLength;
  133. PacketPriority priority;
  134. PacketReliability reliability;
  135. unsigned char orderingChannel;
  136. SystemAddress target;
  137. bool broadcast;
  138. data[0]=ID_USER_PACKET_ENUM+(randomMT()%10);
  139. dataLength=3+(randomMT()%8000);
  140. // dataLength=600+(randomMT()%7000);
  141. priority=(PacketPriority)(randomMT()%(int)NUMBER_OF_PRIORITIES);
  142. reliability=(PacketReliability)(randomMT()%((int)RELIABLE_SEQUENCED+1));
  143. orderingChannel=randomMT()%32;
  144. peerIndex=randomMT()%NUM_PEERS;
  145. if ((randomMT()%NUM_PEERS)==0)
  146. target=RakNet::UNASSIGNED_SYSTEM_ADDRESS;
  147. else
  148. target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
  149. broadcast=(bool)(randomMT()%2);
  150. #ifdef _VERIFY_RECIPIENTS
  151. broadcast=false; // Temporarily in so I can check recipients
  152. #endif
  153. sprintf(data+3, "dataLength=%i priority=%i reliability=%i orderingChannel=%i target=%i broadcast=%i\n", dataLength, priority, reliability, orderingChannel, target.GetPort(), broadcast);
  154. #ifdef _VERIFY_RECIPIENTS
  155. memcpy((char*)data, (char*)&target.port, sizeof(unsigned short));
  156. #endif
  157. data[dataLength-1]=0;
  158. }
  159. else if (nextAction < .181f)
  160. {
  161. // CloseConnection
  162. SystemAddress target;
  163. peerIndex=randomMT()%NUM_PEERS;
  164. target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
  165. peers[peerIndex]->CloseConnection(target, (bool)(randomMT()%2), 0);
  166. }
  167. else if (nextAction < .20f)
  168. {
  169. // Offline Ping
  170. peerIndex=randomMT()%NUM_PEERS;
  171. peers[peerIndex]->Ping("127.0.0.1", 60000+(randomMT()%NUM_PEERS), (bool)(randomMT()%2));
  172. }
  173. else if (nextAction < .21f)
  174. {
  175. // Online Ping
  176. SystemAddress target;
  177. target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
  178. peerIndex=randomMT()%NUM_PEERS;
  179. peers[peerIndex]->Ping(target);
  180. }
  181. else if (nextAction < .24f)
  182. {
  183. }
  184. else if (nextAction < .25f)
  185. {
  186. // GetStatistics
  187. SystemAddress target, mySystemAddress;
  188. RakNetStatistics *rss;
  189. mySystemAddress=peers[peerIndex]->GetInternalID();
  190. target=peers[peerIndex]->GetSystemAddressFromIndex(randomMT()%NUM_PEERS);
  191. peerIndex=randomMT()%NUM_PEERS;
  192. rss=peers[peerIndex]->GetStatistics(mySystemAddress);
  193. if (rss)
  194. {
  195. StatisticsToString(rss, data, 0);
  196. #ifdef _DO_PRINTF
  197. printf("Statistics for local system %i:\n%s", mySystemAddress.GetPort(), data);
  198. #endif
  199. }
  200. rss=peers[peerIndex]->GetStatistics(target);
  201. if (rss)
  202. {
  203. StatisticsToString(rss, data, 0);
  204. #ifdef _DO_PRINTF
  205. printf("Statistics for target system %i:\n%s", target.GetPort(), data);
  206. #endif
  207. }
  208. }
  209. for (i=0; i < NUM_PEERS; i++)
  210. peers[i]->DeallocatePacket(peers[i]->Receive());
  211. #ifdef _WIN32
  212. Sleep(0);
  213. #else
  214. usleep(0);
  215. #endif
  216. }
  217. for (i=0; i < NUM_PEERS; i++)
  218. RakNet::RakPeerInterface::DestroyInstance(peers[i]);
  219. return 0;
  220. }
粤ICP备19079148号