BigPacketTest.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 "RakNetStatistics.h"
  15. #include "GetTime.h"
  16. #include "MessageIdentifiers.h"
  17. #include "MTUSize.h"
  18. #include <stdio.h>
  19. #include "Kbhit.h"
  20. #include "RakSleep.h"
  21. #include "Gets.h"
  22. bool quit;
  23. bool sentPacket=false;
  24. #define BIG_PACKET_SIZE 83296256
  25. using namespace RakNet;
  26. RakPeerInterface *client, *server;
  27. char *text;
  28. int main(void)
  29. {
  30. client=server=0;
  31. text= new char [BIG_PACKET_SIZE];
  32. quit=false;
  33. char ch;
  34. printf("This is a test I use to test the packet splitting capabilities of RakNet\n");
  35. printf("All it does is send a large block of data to the feedback loop\n");
  36. printf("Difficulty: Beginner\n\n");
  37. printf("Enter 's' to run as server, 'c' to run as client, space to run local.\n");
  38. ch=' ';
  39. Gets(text,BIG_PACKET_SIZE);
  40. ch=text[0];
  41. if (ch=='c')
  42. {
  43. client=RakNet::RakPeerInterface::GetInstance();
  44. printf("Working as client\n");
  45. printf("Enter remote IP: ");
  46. Gets(text,BIG_PACKET_SIZE);
  47. if (text[0]==0)
  48. strcpy(text, "natpunch.jenkinssoftware.com"); // dx in Europe
  49. }
  50. else if (ch=='s')
  51. {
  52. server=RakNet::RakPeerInterface::GetInstance();
  53. printf("Working as server\n");
  54. }
  55. else
  56. {
  57. client=RakNet::RakPeerInterface::GetInstance();
  58. server=RakNet::RakPeerInterface::GetInstance();;
  59. strcpy(text, "127.0.0.1");
  60. }
  61. // Test IPV6
  62. int socketFamily;
  63. socketFamily=AF_INET;
  64. //socketFamily=AF_INET6;
  65. if (server)
  66. {
  67. server->SetTimeoutTime(5000,RakNet::UNASSIGNED_SYSTEM_ADDRESS);
  68. RakNet::SocketDescriptor socketDescriptor(3000,0);
  69. socketDescriptor.socketFamily=socketFamily;
  70. server->SetMaximumIncomingConnections(4);
  71. StartupResult sr;
  72. sr=server->Startup(4, &socketDescriptor, 1);
  73. if (sr!=RAKNET_STARTED)
  74. {
  75. printf("Server failed to start. Error=%i\n", sr);
  76. return 1;
  77. }
  78. // server->SetPerConnectionOutgoingBandwidthLimit(40000);
  79. printf("Started server on %s\n", server->GetMyBoundAddress().ToString(true));
  80. }
  81. if (client)
  82. {
  83. client->SetTimeoutTime(5000,RakNet::UNASSIGNED_SYSTEM_ADDRESS);
  84. RakNet::SocketDescriptor socketDescriptor(0,0);
  85. socketDescriptor.socketFamily=socketFamily;
  86. StartupResult sr;
  87. sr=client->Startup(4, &socketDescriptor, 1);
  88. if (sr!=RAKNET_STARTED)
  89. {
  90. printf("Client failed to start. Error=%i\n", sr);
  91. return 1;
  92. }
  93. client->SetSplitMessageProgressInterval(10000); // Get ID_DOWNLOAD_PROGRESS notifications
  94. // client->SetPerConnectionOutgoingBandwidthLimit(28800);
  95. printf("Started client on %s\n", client->GetMyBoundAddress().ToString(true));
  96. client->Connect(text, 3000, 0, 0);
  97. }
  98. RakSleep(500);
  99. printf("My IP addresses:\n");
  100. RakPeerInterface *rakPeer;
  101. if (server)
  102. rakPeer=server;
  103. else
  104. rakPeer=client;
  105. unsigned int i;
  106. for (i=0; i < rakPeer->GetNumberOfAddresses(); i++)
  107. {
  108. printf("%i. %s\n", i+1, rakPeer->GetLocalIP(i));
  109. }
  110. // Always apply the network simulator on two systems, never just one, with half the values on each.
  111. // Otherwise the flow control gets confused.
  112. // if (client)
  113. // client->ApplyNetworkSimulator(.01, 0, 0);
  114. // if (server)
  115. // server->ApplyNetworkSimulator(.01, 0, 0);
  116. RakNet::TimeMS start,stop;
  117. RakNet::TimeMS nextStatTime = RakNet::GetTimeMS() + 1000;
  118. RakNet::Packet *packet;
  119. start=RakNet::GetTimeMS();
  120. while (!quit)
  121. {
  122. if (server)
  123. {
  124. for (packet = server->Receive(); packet; server->DeallocatePacket(packet), packet=server->Receive())
  125. {
  126. if (packet->data[0]==ID_NEW_INCOMING_CONNECTION || packet->data[0]==253)
  127. {
  128. printf("Starting send\n");
  129. start=RakNet::GetTimeMS();
  130. if (BIG_PACKET_SIZE<=100000)
  131. {
  132. for (int i=0; i < BIG_PACKET_SIZE; i++)
  133. text[i]=255-(i&255);
  134. }
  135. else
  136. text[0]=(unsigned char) 255;
  137. server->Send(text, BIG_PACKET_SIZE, LOW_PRIORITY, RELIABLE_ORDERED_WITH_ACK_RECEIPT, 0, packet->systemAddress, false);
  138. // Keep the stat from updating until the messages move to the thread or it quits right away
  139. nextStatTime=RakNet::GetTimeMS()+1000;
  140. }
  141. if (packet->data[0]==ID_CONNECTION_LOST)
  142. printf("ID_CONNECTION_LOST from %s\n", packet->systemAddress.ToString());
  143. else if (packet->data[0]==ID_DISCONNECTION_NOTIFICATION)
  144. printf("ID_DISCONNECTION_NOTIFICATION from %s\n", packet->systemAddress.ToString());
  145. else if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
  146. printf("ID_NEW_INCOMING_CONNECTION from %s\n", packet->systemAddress.ToString());
  147. else if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  148. printf("ID_CONNECTION_REQUEST_ACCEPTED from %s\n", packet->systemAddress.ToString());
  149. }
  150. if (kbhit())
  151. {
  152. char ch=getch();
  153. if (ch==' ')
  154. {
  155. printf("Sending medium priority message\n");
  156. char t[1];
  157. t[0]=(unsigned char) 254;
  158. server->Send(t, 1, MEDIUM_PRIORITY, RELIABLE_ORDERED, 1, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  159. }
  160. if (ch=='q')
  161. quit=true;
  162. }
  163. }
  164. if (client)
  165. {
  166. packet = client->Receive();
  167. while (packet)
  168. {
  169. if (packet->data[0]==ID_DOWNLOAD_PROGRESS)
  170. {
  171. RakNet::BitStream progressBS(packet->data, packet->length, false);
  172. progressBS.IgnoreBits(8); // ID_DOWNLOAD_PROGRESS
  173. unsigned int progress;
  174. unsigned int total;
  175. unsigned int partLength;
  176. // Disable endian swapping on reading this, as it's generated locally in ReliabilityLayer.cpp
  177. progressBS.ReadBits( (unsigned char* ) &progress, BYTES_TO_BITS(sizeof(progress)), true );
  178. progressBS.ReadBits( (unsigned char* ) &total, BYTES_TO_BITS(sizeof(total)), true );
  179. progressBS.ReadBits( (unsigned char* ) &partLength, BYTES_TO_BITS(sizeof(partLength)), true );
  180. printf("Progress: msgID=%i Progress %i/%i Partsize=%i\n",
  181. (unsigned char) packet->data[0],
  182. progress,
  183. total,
  184. partLength);
  185. }
  186. else if (packet->data[0]==255)
  187. {
  188. if (packet->length!=BIG_PACKET_SIZE)
  189. {
  190. printf("Test failed. %i bytes (wrong number of bytes).\n", packet->length);
  191. quit=true;
  192. break;
  193. }
  194. if (BIG_PACKET_SIZE<=100000)
  195. {
  196. for (int i=0; i < BIG_PACKET_SIZE; i++)
  197. {
  198. if (packet->data[i]!=255-(i&255))
  199. {
  200. printf("Test failed. %i bytes (bad data).\n", packet->length);
  201. quit=true;
  202. break;
  203. }
  204. }
  205. }
  206. if (quit==false)
  207. {
  208. printf("Test succeeded. %i bytes.\n", packet->length);
  209. bool repeat=false;
  210. if (repeat)
  211. {
  212. printf("Rerequesting send.\n");
  213. unsigned char ch=(unsigned char) 253;
  214. client->Send((const char*) &ch, 1, MEDIUM_PRIORITY, RELIABLE_ORDERED, 1, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  215. }
  216. else
  217. {
  218. quit=true;
  219. break;
  220. }
  221. }
  222. }
  223. else if (packet->data[0]==254)
  224. {
  225. printf("Got high priority message.\n");
  226. }
  227. else if (packet->data[0]==ID_CONNECTION_LOST)
  228. printf("ID_CONNECTION_LOST from %s\n", packet->systemAddress.ToString());
  229. else if (packet->data[0]==ID_DISCONNECTION_NOTIFICATION)
  230. printf("ID_DISCONNECTION_NOTIFICATION from %s\n", packet->systemAddress.ToString());
  231. else if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
  232. printf("ID_NEW_INCOMING_CONNECTION from %s\n", packet->systemAddress.ToString());
  233. else if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  234. {
  235. start=RakNet::GetTimeMS();
  236. printf("ID_CONNECTION_REQUEST_ACCEPTED from %s\n", packet->systemAddress.ToString());
  237. }
  238. else if (packet->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
  239. printf("ID_CONNECTION_ATTEMPT_FAILED from %s\n", packet->systemAddress.ToString());
  240. client->DeallocatePacket(packet);
  241. packet = client->Receive();
  242. }
  243. }
  244. if (RakNet::GetTimeMS() > nextStatTime)
  245. {
  246. nextStatTime=RakNet::GetTimeMS()+1000;
  247. RakNetStatistics rssSender;
  248. RakNetStatistics rssReceiver;
  249. if (server)
  250. {
  251. unsigned int i;
  252. unsigned short numSystems;
  253. server->GetConnectionList(0,&numSystems);
  254. if (numSystems>0)
  255. {
  256. for (i=0; i < numSystems; i++)
  257. {
  258. server->GetStatistics(server->GetSystemAddressFromIndex(i), &rssSender);
  259. StatisticsToString(&rssSender, text,2);
  260. printf("==== System %i ====\n", i+1);
  261. printf("%s\n\n", text);
  262. }
  263. }
  264. }
  265. if (client && server==0 && client->GetGUIDFromIndex(0)!=UNASSIGNED_RAKNET_GUID)
  266. {
  267. client->GetStatistics(client->GetSystemAddressFromIndex(0), &rssReceiver);
  268. StatisticsToString(&rssReceiver, text,2);
  269. printf("%s\n\n", text);
  270. }
  271. }
  272. RakSleep(100);
  273. }
  274. stop=RakNet::GetTimeMS();
  275. double seconds = (double)(stop-start)/1000.0;
  276. if (server)
  277. {
  278. RakNetStatistics *rssSender2=server->GetStatistics(server->GetSystemAddressFromIndex(0));
  279. StatisticsToString(rssSender2, text, 1);
  280. printf("%s", text);
  281. }
  282. printf("%i bytes per second (%.2f seconds). Press enter to quit\n", (int)((double)(BIG_PACKET_SIZE) / seconds ), seconds) ;
  283. Gets(text,BIG_PACKET_SIZE);
  284. delete []text;
  285. RakNet::RakPeerInterface::DestroyInstance(client);
  286. RakNet::RakPeerInterface::DestroyInstance(server);
  287. return 0;
  288. }
粤ICP备19079148号