FlowControlTest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "GetTime.h"
  12. #include "MessageIdentifiers.h"
  13. #include "RakNetStatistics.h"
  14. #include <cstdio>
  15. #include <memory.h>
  16. #include <cstring>
  17. #include <stdlib.h>
  18. #include "Gets.h"
  19. using namespace RakNet;
  20. #ifdef WIN32
  21. #include "Kbhit.h"
  22. #else
  23. #include "Kbhit.h"
  24. #endif
  25. int main(void)
  26. {
  27. RakPeerInterface *rakServer;
  28. RakPeerInterface *rakClient;
  29. char ch;
  30. char str[255], remoteIP[255];
  31. char randomData[8192];
  32. int localPort, remotePort;
  33. int packetSize;
  34. int sendinterval;
  35. RakNet::TimeMS time;
  36. RakNet::Packet *p;
  37. RakNet::TimeMS lastPacketReceipt, lastNotification, lastSend;
  38. #ifndef _WIN32
  39. char buff[256];
  40. #endif
  41. memset(randomData, 255, sizeof(randomData));
  42. printf("This project is used to test two systems sending to each other with\n");
  43. printf("variable message sends rates and payload sizes\n");
  44. printf("Difficulty: Beginner\n\n");
  45. printf("Start relay (s)erver or start (c)lient?\n");
  46. #ifndef _WIN32
  47. Gets(buff,sizeof(buff));
  48. ch=buff[0];
  49. #else
  50. ch=getch();
  51. #endif
  52. if (ch=='s' || ch=='S')
  53. {
  54. printf("Acting as server.\n");
  55. rakServer=RakNet::RakPeerInterface::GetInstance();
  56. rakClient=0;
  57. }
  58. else
  59. {
  60. printf("Acting as client.\n");
  61. rakClient=RakNet::RakPeerInterface::GetInstance();
  62. rakServer=0;
  63. }
  64. printf("Enter local port: ");
  65. Gets(str, sizeof(str));
  66. if (str[0]==0)
  67. {
  68. if (rakServer)
  69. localPort=60000;
  70. else
  71. localPort=0;
  72. }
  73. else
  74. localPort=atoi(str);
  75. if (rakServer)
  76. {
  77. RakNet::SocketDescriptor socketDescriptor(localPort,0);
  78. rakServer->Startup(100, &socketDescriptor, 1);
  79. rakServer->SetMaximumIncomingConnections(100);
  80. }
  81. else
  82. {
  83. printf("Enter remote IP: ");
  84. Gets(remoteIP,sizeof(remoteIP));
  85. if (remoteIP[0]==0)
  86. strcpy(remoteIP, "127.0.0.1");
  87. printf("Enter remote port: ");
  88. Gets(str, sizeof(str));
  89. if (str[0]==0)
  90. remotePort=60000;
  91. else
  92. remotePort=atoi(str);
  93. RakNet::SocketDescriptor socketDescriptor(localPort,0);
  94. rakClient->Startup(1, &socketDescriptor, 1);
  95. rakClient->Connect(remoteIP, remotePort, 0, 0);
  96. }
  97. printf("Entering loop.\nHit 'h' for help.\nHit 'q' to quit\n'i' to increase send interval\n'd' to decrease send interval\n'+' to increase packet size\n'-' to decrease packet size.\nSpace to show current statistics\n");
  98. sendinterval=128;
  99. packetSize=64;
  100. lastPacketReceipt=lastNotification=RakNet::GetTimeMS();
  101. lastSend=0;
  102. while (1)
  103. {
  104. time=RakNet::GetTimeMS();
  105. if (kbhit())
  106. {
  107. #ifndef _WIN32
  108. Gets(buff,sizeof(buff));
  109. ch=buff[0];
  110. #else
  111. ch=getch();
  112. #endif
  113. if (ch=='q')
  114. {
  115. printf("Quitting\n");
  116. break;
  117. }
  118. else if (ch=='i')
  119. {
  120. sendinterval*=2;
  121. printf("Send interval is now %i\n", sendinterval);
  122. }
  123. else if (ch=='d')
  124. {
  125. if (sendinterval>1)
  126. sendinterval/=2;
  127. printf("Send interval is now %i\n", sendinterval);
  128. }
  129. if (ch=='h')
  130. printf("Hit 'h' for help.\nHit 'q' to quit\n'i' to increase send interval\n'd' to decrease send interval\n'+' to increase packet size\n'-' to decrease packet size.\nSpace to show current statistics\n");
  131. else if (ch=='+')
  132. {
  133. if (packetSize < 8192)
  134. packetSize*=2;
  135. printf("Packet size is now %i\n", packetSize);
  136. }
  137. else if (ch=='-')
  138. {
  139. if (packetSize>1)
  140. packetSize/=2;
  141. printf("Packet size is now %i\n", packetSize);
  142. }
  143. else if (ch==' ')
  144. {
  145. if (rakServer)
  146. {
  147. StatisticsToString(rakServer->GetStatistics(rakServer->GetSystemAddressFromIndex(0)), randomData, 1);
  148. printf("%s", randomData);
  149. }
  150. else
  151. {
  152. StatisticsToString(rakClient->GetStatistics(rakClient->GetSystemAddressFromIndex(0)), randomData, 1);
  153. printf("%s", randomData);
  154. }
  155. printf("Send interval is %i\n", sendinterval);
  156. printf("Packet size is %i\n", packetSize);
  157. printf("\n");
  158. }
  159. ch=0;
  160. }
  161. // get packets
  162. if (rakServer)
  163. p = rakServer->Receive();
  164. else
  165. p=rakClient->Receive();
  166. if (p)
  167. {
  168. lastPacketReceipt=RakNet::GetTimeMS();
  169. switch (p->data[0])
  170. {
  171. case ID_CONNECTION_REQUEST_ACCEPTED:
  172. printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
  173. break;
  174. case ID_DISCONNECTION_NOTIFICATION:
  175. // Connection lost normally
  176. printf("ID_DISCONNECTION_NOTIFICATION\n");
  177. break;
  178. case ID_NEW_INCOMING_CONNECTION:
  179. // Somebody connected. We have their IP now
  180. printf("ID_NEW_INCOMING_CONNECTION\n");
  181. break;
  182. case ID_CONNECTION_LOST:
  183. // Couldn't deliver a reliable packet - i.e. the other system was abnormally
  184. // terminated
  185. printf("ID_CONNECTION_LOST\n");
  186. break;
  187. default:
  188. // Relay
  189. if (rakServer)
  190. rakServer->Send((char*)p->data, p->length, HIGH_PRIORITY, RELIABLE_ORDERED, 0, p->systemAddress, true);
  191. }
  192. if (rakServer)
  193. rakServer->DeallocatePacket(p);
  194. else
  195. rakClient->DeallocatePacket(p);
  196. }
  197. if ((rakServer && rakServer->NumberOfConnections()>0) ||
  198. (rakClient && rakClient->NumberOfConnections()>0))
  199. {
  200. // Do sends
  201. if (lastSend + (RakNet::TimeMS)sendinterval < time)
  202. {
  203. if (rakServer)
  204. {
  205. rakServer->Send((char*)randomData, packetSize, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  206. }
  207. else if (rakClient)
  208. {
  209. rakClient->Send((char*)randomData, packetSize, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  210. }
  211. lastSend=time;
  212. }
  213. if (lastPacketReceipt + 500 < (unsigned int)time && lastNotification + 500 < (unsigned int)time)
  214. {
  215. lastNotification=time;
  216. printf("Warning: No packets for %i ms. Possibly a spike.\n", time - lastPacketReceipt);
  217. }
  218. }
  219. }
  220. if (rakServer)
  221. RakNet::RakPeerInterface::DestroyInstance(rakServer);
  222. else
  223. RakNet::RakPeerInterface::DestroyInstance(rakClient);
  224. return 1;
  225. }
粤ICP备19079148号