Timestamping.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "MessageIdentifiers.h"
  13. #include "GetTime.h"
  14. #include "RakSleep.h"
  15. using namespace RakNet;
  16. #include <cstdio>
  17. #include <memory.h>
  18. #include <cstring>
  19. #include "Gets.h"
  20. #include "Kbhit.h"
  21. int main(void)
  22. {
  23. char serverIP[64];
  24. RakPeerInterface *rakClient=RakNet::RakPeerInterface::GetInstance();
  25. RakPeerInterface *rakServer=RakNet::RakPeerInterface::GetInstance();
  26. rakClient->SetOccasionalPing(true);
  27. rakServer->SetOccasionalPing(true);
  28. char ch;
  29. bool isServer;
  30. printf("Demonstrates RakNet's timestamping system, used to get a common\n");
  31. printf("network time without relying on NTP.\n");
  32. printf("Difficulty: Beginner\n\n");
  33. printf("Hit 'c' to run as a client. Hit 's' to run as a server. Hit 'q' to quit\n");
  34. char buff[256];
  35. while (1)
  36. {
  37. gets(buff);
  38. ch = buff[0];
  39. if (ch=='c')
  40. {
  41. // Run as a client. If you don't have another machine, just run 2 instances of this program and use "127.0.0.1"
  42. puts ("Enter server IP\n");
  43. Gets(serverIP,sizeof(serverIP));
  44. if (serverIP[0]==0)
  45. strcpy(serverIP, "127.0.0.1");
  46. RakNet::SocketDescriptor socketDescriptor(0,0);
  47. rakClient->Startup(1, &socketDescriptor, 1);
  48. rakClient->Connect(serverIP, 2100, 0, 0);
  49. printf("Connecting client\n");
  50. isServer=false;
  51. break;
  52. }
  53. else if (ch=='s')
  54. {
  55. // Run as a server.
  56. RakNet::SocketDescriptor socketDescriptor(2100,0);
  57. rakServer->Startup(32,&socketDescriptor, 1);
  58. rakServer->SetMaximumIncomingConnections(32);
  59. printf("Server started\n");
  60. isServer=true;
  61. break;
  62. }
  63. else if (ch=='q')
  64. return 0;
  65. else
  66. {
  67. printf("Bad input. Enter 'c' 's' or 'q'.\n");
  68. }
  69. }
  70. printf("Entering main loop. Press 'q' to quit\n'c' to send from the client.\n's' to send from the server.\n");
  71. RakNet::Packet *packet;
  72. RakNet::Time time;
  73. ch=0;
  74. bool packetFromServer;
  75. while (1)
  76. {
  77. if (kbhit())
  78. {
  79. #ifndef _WIN32
  80. Gets(buff,sizeof(buff));
  81. ch=buff[0];
  82. #else
  83. ch=getch();
  84. #endif
  85. }
  86. if (ch=='q')
  87. break;
  88. if (ch=='c' && rakClient->GetSystemAddressFromIndex(0)!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  89. {
  90. BitStream bitStream;
  91. // When writing a timestamp, the first byte is ID_TIMESTAMP
  92. // The next 4 bytes is the timestamp itself.
  93. bitStream.Write((unsigned char)ID_TIMESTAMP);
  94. time=RakNet::GetTime();
  95. bitStream.Write(time);
  96. rakClient->Send(&bitStream, HIGH_PRIORITY, RELIABLE, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  97. printf("Sending message from client at time %" PRINTF_64_BIT_MODIFIER "u\n", time);
  98. }
  99. else if (ch=='s' && rakServer->IsActive())
  100. {
  101. BitStream bitStream;
  102. bitStream.Write((unsigned char)ID_TIMESTAMP);
  103. time=RakNet::GetTime();
  104. bitStream.Write(time);
  105. rakServer->Send(&bitStream, HIGH_PRIORITY, RELIABLE, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  106. printf("Sending packet from server at time %" PRINTF_64_BIT_MODIFIER "u\n", time);
  107. }
  108. if (isServer==false)
  109. {
  110. packetFromServer=false;
  111. packet=rakClient->Receive();
  112. }
  113. else
  114. {
  115. packetFromServer=true;
  116. packet=rakServer->Receive();
  117. }
  118. if (packet && packet->data[0]==ID_TIMESTAMP)
  119. {
  120. // Write the bytes after the first to a variable. That is the time the packet was sent.
  121. RakNet::BitStream timeBS(packet->data+1, sizeof(RakNet::Time), false);
  122. timeBS.Read(time);
  123. printf("Time difference is %" PRINTF_64_BIT_MODIFIER "u\n", RakNet::GetTime() - time);
  124. }
  125. if (packet)
  126. {
  127. if (packetFromServer)
  128. rakServer->DeallocatePacket(packet);
  129. else
  130. rakClient->DeallocatePacket(packet);
  131. }
  132. ch=0;
  133. RakSleep(0);
  134. }
  135. // Shutdown stuff. It's ok to call disconnect on the server if we are a client and vice-versa
  136. rakServer->Shutdown(0);
  137. rakClient->Shutdown(0);
  138. RakNet::RakPeerInterface::DestroyInstance(rakClient);
  139. RakNet::RakPeerInterface::DestroyInstance(rakServer);
  140. return 0;
  141. }
粤ICP备19079148号