Ping.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // ----------------------------------------------------------------------
  11. // RakNet version 1.0
  12. // Filename Ping.cpp
  13. // Very basic chat engine example
  14. // ----------------------------------------------------------------------
  15. #include "MessageIdentifiers.h"
  16. #include "RakPeerInterface.h"
  17. #include "RakPeerInterface.h"
  18. #include "RakNetTypes.h"
  19. #include "GetTime.h"
  20. #include "BitStream.h"
  21. #include <assert.h>
  22. #include <cstdio>
  23. #include <cstring>
  24. #include <stdlib.h>
  25. #include "Gets.h"
  26. #ifdef WIN32
  27. #include "Kbhit.h"
  28. #else
  29. #include "Kbhit.h"
  30. #endif
  31. int main(void)
  32. {
  33. // Pointers to the interfaces of our server and client.
  34. // Note we can easily have both in the same program
  35. RakNet::RakPeerInterface *client=RakNet::RakPeerInterface::GetInstance();
  36. RakNet::RakPeerInterface *server=RakNet::RakPeerInterface::GetInstance();
  37. int i = server->GetNumberOfAddresses();
  38. // Holds packets
  39. RakNet::Packet* p;
  40. // Record the first client that connects to us so we can pass it to the ping function
  41. RakNet::SystemAddress clientID=RakNet::UNASSIGNED_SYSTEM_ADDRESS;
  42. bool packetFromServer;
  43. char portstring[30];
  44. printf("This is a sample of how to send offline pings and get offline ping\n");
  45. printf("responses.\n");
  46. printf("Difficulty: Beginner\n\n");
  47. // A server
  48. puts("Enter the server port to listen on");
  49. Gets(portstring,sizeof(portstring));
  50. if (portstring[0]==0)
  51. strcpy(portstring,"60000");
  52. // Enumeration data
  53. puts("Enter offline ping response data (for return by a LAN discovery for example)");
  54. puts("Hit enter for none.");
  55. char enumData[512];
  56. Gets(enumData,sizeof(enumData));
  57. if (enumData[0])
  58. server->SetOfflinePingResponse(enumData, (const unsigned int) strlen(enumData)+1);
  59. puts("Starting server.");
  60. // The server has to be started to respond to pings.
  61. RakNet::SocketDescriptor socketDescriptor(atoi(portstring),0);
  62. bool b = server->Startup(2, &socketDescriptor, 1)==RakNet::RAKNET_STARTED;
  63. server->SetMaximumIncomingConnections(2);
  64. if (b)
  65. puts("Server started, waiting for connections.");
  66. else
  67. {
  68. puts("Server failed to start. Terminating.");
  69. exit(1);
  70. }
  71. socketDescriptor.port=0;
  72. client->Startup(1,&socketDescriptor, 1);
  73. puts("'q' to quit, any other key to send a ping from the client.");
  74. char buff[256];
  75. // Loop for input
  76. while (1)
  77. {
  78. if (kbhit())
  79. {
  80. // Holds user data
  81. char ip[64], serverPort[30], clientPort[30];
  82. if (Gets(buff,sizeof(buff))&&(buff[0]=='q'))
  83. break;
  84. else
  85. {
  86. // Get our input
  87. puts("Enter the client port to listen on, or 0");
  88. Gets(clientPort,sizeof(clientPort));
  89. if (clientPort[0]==0)
  90. strcpy(clientPort, "0");
  91. puts("Enter IP to ping");
  92. Gets(ip, sizeof(ip));
  93. if (ip[0]==0)
  94. strcpy(ip, "127.0.0.1");
  95. puts("Enter the port to ping");
  96. Gets(serverPort,sizeof(serverPort));
  97. if (serverPort[0]==0)
  98. strcpy(serverPort, "60000");
  99. client->Ping(ip, atoi(serverPort), false);
  100. puts("Pinging");
  101. }
  102. }
  103. // Get a packet from either the server or the client
  104. p = server->Receive();
  105. if (p==0)
  106. {
  107. packetFromServer=false;
  108. p = client->Receive();
  109. }
  110. else
  111. packetFromServer=true;
  112. if (p==0)
  113. continue;
  114. // Check if this is a network message packet
  115. switch (p->data[0])
  116. {
  117. case ID_UNCONNECTED_PONG:
  118. {
  119. unsigned int dataLength;
  120. RakNet::TimeMS time;
  121. RakNet::BitStream bsIn(p->data,p->length,false);
  122. bsIn.IgnoreBytes(1);
  123. bsIn.Read(time);
  124. dataLength = p->length - sizeof(unsigned char) - sizeof(RakNet::TimeMS);
  125. printf("ID_UNCONNECTED_PONG from SystemAddress %s.\n", p->systemAddress.ToString(true));
  126. printf("Time is %i\n",time);
  127. printf("Ping is %i\n", (unsigned int)(RakNet::GetTimeMS()-time));
  128. printf("Data is %i bytes long.\n", dataLength);
  129. if (dataLength > 0)
  130. printf("Data is %s\n", p->data+sizeof(unsigned char)+sizeof(RakNet::TimeMS));
  131. // In this sample since the client is not running a game we can save CPU cycles by
  132. // Stopping the network threads after receiving the pong.
  133. client->Shutdown(100);
  134. }
  135. break;
  136. case ID_UNCONNECTED_PING:
  137. break;
  138. case ID_UNCONNECTED_PING_OPEN_CONNECTIONS:
  139. break;
  140. }
  141. // We're done with the packet
  142. if (packetFromServer)
  143. server->DeallocatePacket(p);
  144. else
  145. client->DeallocatePacket(p);
  146. }
  147. // We're done with the network
  148. RakNet::RakPeerInterface::DestroyInstance(server);
  149. RakNet::RakPeerInterface::DestroyInstance(client);
  150. return 0;
  151. }
粤ICP备19079148号