BurstTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "BitStream.h"
  14. #include <cstdio>
  15. #include <memory.h>
  16. #include <cstring>
  17. #include <stdlib.h>
  18. #include "Rand.h"
  19. #include "RakNetStatistics.h"
  20. #include "RakSleep.h"
  21. #include "RakMemoryOverride.h"
  22. #include <stdio.h>
  23. #include "Gets.h"
  24. #include "Kbhit.h"
  25. #include "RakSleep.h"
  26. using namespace RakNet;
  27. int main(int argc, char **argv)
  28. {
  29. RakPeerInterface *rakPeer;
  30. char str[256];
  31. char ip[32];
  32. unsigned short remotePort, localPort;
  33. RakNet::Packet *packet;
  34. printf("This project tests sending a burst of messages to a remote system.\n");
  35. printf("Difficulty: Beginner\n\n");
  36. rakPeer = RakNet::RakPeerInterface::GetInstance();
  37. printf("Enter remote IP (enter to not connect): ");
  38. Gets(ip, sizeof(ip));
  39. if (ip[0])
  40. {
  41. printf("Enter remote port: ");
  42. Gets(str, sizeof(str));
  43. if (str[0]==0)
  44. strcpy(str, "60000");
  45. remotePort=atoi(str);
  46. printf("Enter local port: ");
  47. Gets(str, sizeof(str));
  48. if (str[0]==0)
  49. strcpy(str, "0");
  50. localPort=atoi(str);
  51. RakNet::SocketDescriptor socketDescriptor(localPort,0);
  52. rakPeer->Startup(32, &socketDescriptor, 1);
  53. printf("Connecting...\n");
  54. rakPeer->Connect(ip, remotePort, 0, 0);
  55. }
  56. else
  57. {
  58. printf("Enter local port: ");
  59. Gets(str, sizeof(str));
  60. if (str[0]==0)
  61. strcpy(str, "60000");
  62. localPort=atoi(str);
  63. RakNet::SocketDescriptor socketDescriptor(localPort,0);
  64. rakPeer->Startup(32, &socketDescriptor, 1);
  65. }
  66. rakPeer->SetMaximumIncomingConnections(32);
  67. printf("'s' to send. ' ' for statistics. 'q' to quit.\n");
  68. while (1)
  69. {
  70. if (kbhit())
  71. {
  72. char ch=getch();
  73. if (ch=='q')
  74. return 1;
  75. else if (ch==' ')
  76. {
  77. RakNetStatistics *rss;
  78. char message[2048];
  79. rss=rakPeer->GetStatistics(rakPeer->GetSystemAddressFromIndex(0));
  80. StatisticsToString(rss, message, 2);
  81. printf("%s", message);
  82. }
  83. else if (ch=='s')
  84. {
  85. char msgSizeStr[128], msgCountStr[128];
  86. uint32_t msgSize, msgCount,index;
  87. printf("Enter message size in bytes: ");
  88. Gets(msgSizeStr, sizeof(msgSizeStr));
  89. if (msgSizeStr[0]==0)
  90. msgSize=4096;
  91. else
  92. msgSize=atoi(msgSizeStr);
  93. printf("Enter times to repeatedly send message: ");
  94. Gets(msgCountStr, sizeof(msgCountStr));
  95. if (msgCountStr[0]==0)
  96. msgCount=128;
  97. else
  98. msgCount=atoi(msgCountStr);
  99. RakNet::BitStream bitStream;
  100. for (index=0; index < msgCount; index++)
  101. {
  102. bitStream.Reset();
  103. bitStream.Write((MessageID)ID_USER_PACKET_ENUM);
  104. bitStream.Write(msgSize);
  105. bitStream.Write(index);
  106. bitStream.Write(msgCount);
  107. bitStream.PadWithZeroToByteLength(msgSize);
  108. rakPeer->Send(&bitStream, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_SYSTEM_ADDRESS, true);
  109. }
  110. printf("Sent\n");
  111. }
  112. }
  113. for (packet=rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet=rakPeer->Receive())
  114. {
  115. switch(packet->data[0])
  116. {
  117. case ID_CONNECTION_REQUEST_ACCEPTED:
  118. printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
  119. break;
  120. case ID_NEW_INCOMING_CONNECTION:
  121. printf("ID_NEW_INCOMING_CONNECTION\n");
  122. break;
  123. case ID_NO_FREE_INCOMING_CONNECTIONS:
  124. printf("ID_NO_FREE_INCOMING_CONNECTIONS\n");
  125. break;
  126. case ID_DISCONNECTION_NOTIFICATION:
  127. printf("ID_DISCONNECTION_NOTIFICATION\n");
  128. break;
  129. case ID_CONNECTION_LOST:
  130. printf("ID_CONNECTION_LOST\n");
  131. break;
  132. case ID_CONNECTION_ATTEMPT_FAILED:
  133. printf("Connection attempt failed\n");
  134. break;
  135. case ID_USER_PACKET_ENUM:
  136. {
  137. uint32_t msgSize, msgCount, index;
  138. RakNet::BitStream bitStream(packet->data, packet->length, false);
  139. bitStream.IgnoreBytes(sizeof(MessageID));
  140. bitStream.Read(msgSize);
  141. bitStream.Read(index);
  142. bitStream.Read(msgCount);
  143. printf("%i/%i len=%i", index+1, msgCount, packet->length);
  144. if (msgSize > BITS_TO_BYTES(bitStream.GetReadOffset()) && packet->length!=msgSize)
  145. printf("UNDERLENGTH!\n");
  146. else
  147. printf("\n");
  148. break;
  149. }
  150. default:
  151. printf("Unknown message type %i\n", packet->data[0]);
  152. }
  153. }
  154. RakSleep(30);
  155. }
  156. rakPeer->Shutdown(100);
  157. RakNet::RakPeerInterface::DestroyInstance(rakPeer);
  158. return 1;
  159. }
粤ICP备19079148号