ServerClientTest2.cpp 8.4 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 "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 "Kbhit.h"
  19. #include "GetTime.h"
  20. #include "RakAssert.h"
  21. #include "RakSleep.h"
  22. #include "Gets.h"
  23. using namespace RakNet;
  24. #ifdef _WIN32
  25. #include "WindowsIncludes.h" // Sleep64
  26. #else
  27. #include <unistd.h> // usleep
  28. #include <cstdio>
  29. #include "Getche.h"
  30. #endif
  31. static const int NUM_CLIENTS=100;
  32. #define SERVER_PORT 1234
  33. #define RANDOM_DATA_SIZE_1 50
  34. char randomData1[RANDOM_DATA_SIZE_1];
  35. #define RANDOM_DATA_SIZE_2 100
  36. char randomData2[RANDOM_DATA_SIZE_2];
  37. char *remoteIPAddress=0;
  38. // Connects, sends data over time, disconnects, repeat
  39. class Client
  40. {
  41. public:
  42. Client()
  43. {
  44. peer = RakNet::RakPeerInterface::GetInstance();
  45. }
  46. ~Client()
  47. {
  48. RakNet::RakPeerInterface::DestroyInstance(peer);
  49. }
  50. void Startup(void)
  51. {
  52. RakNet::SocketDescriptor socketDescriptor;
  53. socketDescriptor.port=0;
  54. nextSendTime=0;
  55. StartupResult b = peer->Startup(1,&socketDescriptor,1);
  56. RakAssert(b==RAKNET_STARTED);
  57. isConnected=false;
  58. }
  59. void Connect(void)
  60. {
  61. bool b;
  62. b=peer->Connect(remoteIPAddress, (unsigned short) SERVER_PORT, 0, 0, 0)==RakNet::CONNECTION_ATTEMPT_STARTED;
  63. if (b==false)
  64. {
  65. printf("Client connect call failed!\n");
  66. }
  67. }
  68. void Disconnect(void)
  69. {
  70. peer->CloseConnection(peer->GetSystemAddressFromIndex(0),true,0);
  71. isConnected=false;
  72. }
  73. void Update(RakNet::TimeMS curTime)
  74. {
  75. Packet *p = peer->Receive();
  76. while (p)
  77. {
  78. switch (p->data[0])
  79. {
  80. case ID_CONNECTION_REQUEST_ACCEPTED:
  81. printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
  82. isConnected=true;
  83. break;
  84. // print out errors
  85. case ID_CONNECTION_ATTEMPT_FAILED:
  86. printf("Client Error: ID_CONNECTION_ATTEMPT_FAILED\n");
  87. isConnected=false;
  88. break;
  89. case ID_ALREADY_CONNECTED:
  90. printf("Client Error: ID_ALREADY_CONNECTED\n");
  91. break;
  92. case ID_CONNECTION_BANNED:
  93. printf("Client Error: ID_CONNECTION_BANNED\n");
  94. break;
  95. case ID_INVALID_PASSWORD:
  96. printf("Client Error: ID_INVALID_PASSWORD\n");
  97. break;
  98. case ID_INCOMPATIBLE_PROTOCOL_VERSION:
  99. printf("Client Error: ID_INCOMPATIBLE_PROTOCOL_VERSION\n");
  100. break;
  101. case ID_NO_FREE_INCOMING_CONNECTIONS:
  102. printf("Client Error: ID_NO_FREE_INCOMING_CONNECTIONS\n");
  103. isConnected=false;
  104. break;
  105. case ID_DISCONNECTION_NOTIFICATION:
  106. //printf("ID_DISCONNECTION_NOTIFICATION\n");
  107. isConnected=false;
  108. break;
  109. case ID_CONNECTION_LOST:
  110. printf("Client Error: ID_CONNECTION_LOST\n");
  111. isConnected=false;
  112. break;
  113. }
  114. peer->DeallocatePacket(p);
  115. p = peer->Receive();
  116. }
  117. if (curTime>nextSendTime && isConnected)
  118. {
  119. if (randomMT()%10==0)
  120. {
  121. peer->Send((const char*)&randomData2,RANDOM_DATA_SIZE_2,HIGH_PRIORITY,RELIABLE_ORDERED,0,RakNet::UNASSIGNED_SYSTEM_ADDRESS,true);
  122. }
  123. else
  124. {
  125. peer->Send((const char*)&randomData1,RANDOM_DATA_SIZE_1,HIGH_PRIORITY,RELIABLE_ORDERED,0,RakNet::UNASSIGNED_SYSTEM_ADDRESS,true);
  126. }
  127. nextSendTime=curTime+50;
  128. }
  129. }
  130. bool isConnected;
  131. RakPeerInterface *peer;
  132. RakNet::TimeMS nextSendTime;
  133. };
  134. // Just listens for ID_USER_PACKET_ENUM and validates its integrity
  135. class Server
  136. {
  137. public:
  138. Server()
  139. {
  140. peer = RakNet::RakPeerInterface::GetInstance();
  141. nextSendTime=0;
  142. }
  143. ~Server()
  144. {
  145. RakNet::RakPeerInterface::DestroyInstance(peer);
  146. }
  147. void Start(void)
  148. {
  149. RakNet::SocketDescriptor socketDescriptor;
  150. socketDescriptor.port=(unsigned short) SERVER_PORT;
  151. bool b = peer->Startup((unsigned short) 600,&socketDescriptor,1)==RakNet::RAKNET_STARTED;
  152. RakAssert(b);
  153. peer->SetMaximumIncomingConnections(600);
  154. }
  155. unsigned ConnectionCount(void) const
  156. {
  157. unsigned short numberOfSystems;
  158. peer->GetConnectionList(0,&numberOfSystems);
  159. return numberOfSystems;
  160. }
  161. void Update(RakNet::TimeMS curTime)
  162. {
  163. Packet *p = peer->Receive();
  164. while (p)
  165. {
  166. switch (p->data[0])
  167. {
  168. case ID_CONNECTION_LOST:
  169. case ID_DISCONNECTION_NOTIFICATION:
  170. case ID_NEW_INCOMING_CONNECTION:
  171. printf("Connections = %i\n", ConnectionCount());
  172. break;
  173. // case ID_USER_PACKET_ENUM:
  174. // {
  175. // peer->Send((const char*) p->data, p->length, HIGH_PRIORITY, RELIABLE_ORDERED, 0, p->guid, true);
  176. // break;
  177. // }
  178. }
  179. peer->DeallocatePacket(p);
  180. p = peer->Receive();
  181. }
  182. if (curTime>nextSendTime)
  183. {
  184. if (randomMT()%10==0)
  185. {
  186. peer->Send((const char*)&randomData2,RANDOM_DATA_SIZE_2,HIGH_PRIORITY,RELIABLE_ORDERED,0,RakNet::UNASSIGNED_SYSTEM_ADDRESS,true);
  187. }
  188. else
  189. {
  190. peer->Send((const char*)&randomData1,RANDOM_DATA_SIZE_1,HIGH_PRIORITY,RELIABLE_ORDERED,0,RakNet::UNASSIGNED_SYSTEM_ADDRESS,true);
  191. }
  192. nextSendTime=curTime+100;
  193. }
  194. }
  195. RakNet::TimeMS nextSendTime;
  196. RakPeerInterface *peer;
  197. };
  198. int main(void)
  199. {
  200. Client clients[NUM_CLIENTS];
  201. Server server;
  202. // int clientIndex;
  203. int mode;
  204. printf("Connects many clients to a single server.\n");
  205. printf("Difficulty: Intermediate\n\n");
  206. printf("Run as (S)erver or (C)lient or (B)oth? ");
  207. char ch = getche();
  208. static char *defaultRemoteIP="127.0.0.1";
  209. char remoteIP[128];
  210. static char *localIP="127.0.0.1";
  211. if (ch=='s' || ch=='S')
  212. {
  213. mode=0;
  214. }
  215. else if (ch=='c' || ch=='c')
  216. {
  217. mode=1;
  218. remoteIPAddress=remoteIP;
  219. }
  220. else
  221. {
  222. mode=2;
  223. remoteIPAddress=localIP;
  224. }
  225. printf("\n");
  226. if (mode==1 || mode==2)
  227. {
  228. printf("Enter remote IP: ");
  229. Gets(remoteIP, sizeof(remoteIP));
  230. if (remoteIP[0]==0)
  231. {
  232. strcpy(remoteIP, defaultRemoteIP);
  233. printf("Using %s\n", defaultRemoteIP);
  234. }
  235. }
  236. unsigned i;
  237. randomData1[0]=(char) ID_USER_PACKET_ENUM;
  238. for (i=0; i < RANDOM_DATA_SIZE_1-1; i++)
  239. randomData1[i+1]=i;
  240. randomData2[0]=(char) ID_USER_PACKET_ENUM;
  241. for (i=0; i < RANDOM_DATA_SIZE_2-1; i++)
  242. randomData2[i+1]=i;
  243. if (mode==0 || mode==2)
  244. {
  245. server.Start();
  246. printf("Started server\n");
  247. }
  248. if (mode==1 || mode==2)
  249. {
  250. printf("Starting clients...\n");
  251. for (i=0; i < NUM_CLIENTS; i++)
  252. clients[i].Startup();
  253. printf("Started clients\n");
  254. printf("Connecting clients...\n");
  255. for (i=0; i < NUM_CLIENTS; i++)
  256. clients[i].Connect();
  257. printf("Done.\n");
  258. }
  259. RakNet::TimeMS endTime = RakNet::GetTimeMS()+60000*5;
  260. RakNet::TimeMS time = RakNet::GetTimeMS();
  261. while (time < endTime)
  262. {
  263. if (mode==0 || mode==2)
  264. server.Update(time);
  265. if (mode==1 || mode==2)
  266. {
  267. for (i=0; i < NUM_CLIENTS; i++)
  268. clients[i].Update(time);
  269. }
  270. if (kbhit())
  271. {
  272. char ch = getch();
  273. if (ch==' ')
  274. {
  275. FILE *fp;
  276. char text[2048];
  277. if (mode==0 || mode==2)
  278. {
  279. printf("Logging server statistics to ServerStats.txt\n");
  280. fp=fopen("ServerStats.txt","wt");
  281. for (i=0; i < NUM_CLIENTS; i++)
  282. {
  283. RakNetStatistics *rssSender;
  284. rssSender=server.peer->GetStatistics(server.peer->GetSystemAddressFromIndex(i));
  285. StatisticsToString(rssSender, text, 3);
  286. fprintf(fp,"==== System %i ====\n", i+1);
  287. fprintf(fp,"%s\n\n", text);
  288. }
  289. fclose(fp);
  290. }
  291. if (mode==1 || mode==2)
  292. {
  293. printf("Logging client statistics to ClientStats.txt\n");
  294. fp=fopen("ClientStats.txt","wt");
  295. for (i=0; i < NUM_CLIENTS; i++)
  296. {
  297. RakNetStatistics *rssSender;
  298. rssSender=clients[i].peer->GetStatistics(clients[i].peer->GetSystemAddressFromIndex(0));
  299. StatisticsToString(rssSender, text, 3);
  300. fprintf(fp,"==== Client %i ====\n", i+1);
  301. fprintf(fp,"%s\n\n", text);
  302. }
  303. fclose(fp);
  304. }
  305. }
  306. if (ch=='q' || ch==0)
  307. break;
  308. }
  309. time = RakNet::GetTimeMS();
  310. RakSleep(30);
  311. }
  312. if (mode==0 || mode==2)
  313. server.peer->Shutdown(0);
  314. if (mode==1 || mode==2)
  315. for (i=0; i < NUM_CLIENTS; i++)
  316. clients[i].peer->Shutdown(0);
  317. printf("Test completed");
  318. return 0;
  319. }
粤ICP备19079148号