DroppedConnectionTest.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "Rand.h" // randomMT
  12. #include "MessageIdentifiers.h" // Enumerations
  13. #include "RakNetTypes.h" // SystemAddress
  14. #include <cstdio>
  15. using namespace RakNet;
  16. #ifdef _WIN32
  17. #include "Kbhit.h"
  18. #include "WindowsIncludes.h" // Sleep
  19. #else
  20. #include "Kbhit.h"
  21. #include <unistd.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #endif
  26. #ifdef _DEBUG
  27. static const int NUMBER_OF_CLIENTS=9;
  28. #else
  29. static const int NUMBER_OF_CLIENTS=100;
  30. #endif
  31. void ShowHelp(void)
  32. {
  33. printf("System started.\n(D)isconnect a random client silently\n(C)onnect a random client\n(R)andom silent disconnects and connects for all clients.\n(N)otify server of random disconnections.\nSpace to verify connection list.\n(H)elp\n(Q)uit\n");
  34. }
  35. int main(void)
  36. {
  37. RakPeerInterface *server;
  38. RakPeerInterface *clients[NUMBER_OF_CLIENTS];
  39. unsigned index, connectionCount;
  40. unsigned char ch;
  41. SystemAddress serverID;
  42. RakNet::Packet *p;
  43. unsigned short numberOfSystems;
  44. int sender;
  45. // Buffer for input (an ugly hack to keep *nix happy)
  46. #ifndef _WIN32
  47. char buff[256];
  48. #endif
  49. printf("This is a project I use internally to test if dropped connections are detected\n");
  50. printf("Difficulty: Intermediate\n\n");
  51. printf("Dropped Connection Test.\n");
  52. unsigned short serverPort = 20000;
  53. server=RakNet::RakPeerInterface::GetInstance();
  54. // server->InitializeSecurity(0,0,0,0);
  55. RakNet::SocketDescriptor socketDescriptor(serverPort,0);
  56. server->Startup(NUMBER_OF_CLIENTS, &socketDescriptor, 1);
  57. server->SetMaximumIncomingConnections(NUMBER_OF_CLIENTS);
  58. server->SetTimeoutTime(10000,UNASSIGNED_SYSTEM_ADDRESS);
  59. for (index=0; index < NUMBER_OF_CLIENTS; index++)
  60. {
  61. clients[index]=RakNet::RakPeerInterface::GetInstance();
  62. RakNet::SocketDescriptor socketDescriptor2(serverPort+1+index,0);
  63. clients[index]->Startup(1, &socketDescriptor2, 1);
  64. clients[index]->Connect("127.0.0.1", serverPort, 0, 0);
  65. clients[index]->SetTimeoutTime(5000,RakNet::UNASSIGNED_SYSTEM_ADDRESS);
  66. #ifdef _WIN32
  67. Sleep(10);
  68. #else
  69. usleep(10 * 1000);
  70. #endif
  71. printf("%i. ", index);
  72. }
  73. ShowHelp();
  74. while (1)
  75. {
  76. // User input
  77. if (kbhit())
  78. {
  79. #ifndef _WIN32
  80. Gets(buff,sizeof(buff));
  81. ch=buff[0];
  82. #else
  83. ch=getch();
  84. #endif
  85. if (ch=='d' || ch=='D')
  86. {
  87. index = randomMT() % NUMBER_OF_CLIENTS;
  88. clients[index]->GetConnectionList(0, &numberOfSystems);
  89. clients[index]->CloseConnection(serverID, false,0);
  90. if (numberOfSystems==0)
  91. printf("Client %i silently closing inactive connection.\n",index);
  92. else
  93. printf("Client %i silently closing active connection.\n",index);
  94. }
  95. else if (ch=='c' || ch=='C')
  96. {
  97. index = randomMT() % NUMBER_OF_CLIENTS;
  98. clients[index]->GetConnectionList(0, &numberOfSystems);
  99. clients[index]->Connect("127.0.0.1", serverPort, 0, 0);
  100. if (numberOfSystems==0)
  101. printf("Client %i connecting to same existing connection.\n",index);
  102. else
  103. printf("Client %i connecting to closed connection.\n",index);
  104. }
  105. else if (ch=='r' || ch=='R' || ch=='n' || ch=='N')
  106. {
  107. printf("Randomly connecting and disconnecting each client\n");
  108. for (index=0; index < NUMBER_OF_CLIENTS; index++)
  109. {
  110. if (NUMBER_OF_CLIENTS==1 || (randomMT()%2)==0)
  111. {
  112. if (clients[index]->IsActive())
  113. {
  114. if (ch=='r' || ch=='R')
  115. clients[index]->CloseConnection(serverID, false, 0);
  116. else
  117. clients[index]->CloseConnection(serverID, true, 0);
  118. }
  119. }
  120. else
  121. {
  122. clients[index]->Connect("127.0.0.1", serverPort, 0, 0);
  123. }
  124. }
  125. }
  126. else if (ch==' ')
  127. {
  128. server->GetConnectionList(0, &numberOfSystems);
  129. printf("The server thinks %i clients are connected.\n", numberOfSystems);
  130. connectionCount=0;
  131. for (index=0; index < NUMBER_OF_CLIENTS; index++)
  132. {
  133. clients[index]->GetConnectionList(0, &numberOfSystems);
  134. if (numberOfSystems>1)
  135. printf("Bug: Client %i has %i connections\n", index, numberOfSystems);
  136. if (numberOfSystems==1)
  137. {
  138. connectionCount++;
  139. }
  140. }
  141. printf("%i clients are actually connected.\n", connectionCount);
  142. printf("server->NumberOfConnections==%i.\n", server->NumberOfConnections());
  143. }
  144. else if (ch=='h' || ch=='H')
  145. {
  146. ShowHelp();
  147. }
  148. else if (ch=='q' || ch=='Q')
  149. {
  150. break;
  151. }
  152. ch=0;
  153. }
  154. // Parse messages
  155. while (1)
  156. {
  157. p = server->Receive();
  158. sender=NUMBER_OF_CLIENTS;
  159. if (p==0)
  160. {
  161. for (index=0; index < NUMBER_OF_CLIENTS; index++)
  162. {
  163. p = clients[index]->Receive();
  164. if (p!=0)
  165. {
  166. sender=index;
  167. break;
  168. }
  169. }
  170. }
  171. if (p)
  172. {
  173. switch (p->data[0])
  174. {
  175. case ID_CONNECTION_REQUEST_ACCEPTED:
  176. printf("%i: %ID_CONNECTION_REQUEST_ACCEPTED from %i.\n",sender, p->systemAddress.GetPort());
  177. serverID=p->systemAddress;
  178. break;
  179. case ID_DISCONNECTION_NOTIFICATION:
  180. // Connection lost normally
  181. printf("%i: ID_DISCONNECTION_NOTIFICATION from %i.\n",sender, p->systemAddress.GetPort());
  182. break;
  183. case ID_NEW_INCOMING_CONNECTION:
  184. // Somebody connected. We have their IP now
  185. printf("%i: ID_NEW_INCOMING_CONNECTION from %i.\n",sender, p->systemAddress.GetPort());
  186. break;
  187. case ID_CONNECTION_LOST:
  188. // Couldn't deliver a reliable packet - i.e. the other system was abnormally
  189. // terminated
  190. printf("%i: ID_CONNECTION_LOST from %i.\n",sender, p->systemAddress.GetPort());
  191. break;
  192. case ID_NO_FREE_INCOMING_CONNECTIONS:
  193. printf("%i: ID_NO_FREE_INCOMING_CONNECTIONS from %i.\n",sender, p->systemAddress.GetPort());
  194. break;
  195. default:
  196. // Ignore anything else
  197. break;
  198. }
  199. }
  200. else
  201. break;
  202. if (sender==NUMBER_OF_CLIENTS)
  203. server->DeallocatePacket(p);
  204. else
  205. clients[sender]->DeallocatePacket(p);
  206. }
  207. // 11/29/05 - No longer necessary since I added the keepalive
  208. /*
  209. // Have everyone send a reliable packet so dropped connections are noticed.
  210. ch=255;
  211. server->Send((char*)&ch, 1, HIGH_PRIORITY, RELIABLE, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  212. for (index=0; index < NUMBER_OF_CLIENTS; index++)
  213. clients[index]->Send((char*)&ch, 1, HIGH_PRIORITY, RELIABLE, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  214. */
  215. // Sleep so this loop doesn't take up all the CPU time
  216. #ifdef _WIN32
  217. Sleep(30);
  218. #else
  219. usleep(30 * 1000);
  220. #endif
  221. }
  222. RakNet::RakPeerInterface::DestroyInstance(server);
  223. for (index=0; index < NUMBER_OF_CLIENTS; index++)
  224. RakNet::RakPeerInterface::DestroyInstance(clients[index]);
  225. return 1;
  226. }
粤ICP备19079148号