OfflineMessagesTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <stdio.h>
  15. #include "MessageIdentifiers.h"
  16. #include "GetTime.h"
  17. #include "Gets.h"
  18. #ifdef _WIN32
  19. #include "WindowsIncludes.h" // Sleep
  20. #else
  21. #include <unistd.h> // usleep
  22. #include <cstdio>
  23. #endif
  24. int nextTest;
  25. RakNet::RakPeerInterface *peer1=RakNet::RakPeerInterface::GetInstance();
  26. RakNet::RakPeerInterface *peer2=RakNet::RakPeerInterface::GetInstance();
  27. int main(void)
  28. {
  29. char text[1024];
  30. bool sentPacket=false;
  31. nextTest=0;
  32. printf("This project tests the advertise system and offline ping messages.\n");
  33. printf("Difficulty: Beginner\n\n");
  34. peer1->SetMaximumIncomingConnections(1);
  35. RakNet::SocketDescriptor socketDescriptor(60001, 0);
  36. peer1->Startup(1, &socketDescriptor, 1);
  37. socketDescriptor.port=60002;
  38. peer2->Startup(1, &socketDescriptor, 1);
  39. peer1->SetOfflinePingResponse("Offline Ping Data", (int)strlen("Offline Ping Data")+1);
  40. printf("Peer 1 guid = %s\n", peer1->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString());
  41. printf("Peer 2 guid = %s\n", peer2->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString());
  42. printf("Systems started. Waiting for advertise system packet\n");
  43. // Wait for connection to complete
  44. #ifdef _WIN32
  45. Sleep(300);
  46. #else
  47. usleep(300 * 1000);
  48. #endif
  49. printf("Sending advertise system from %s\n", peer1->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString());
  50. peer1->AdvertiseSystem("127.0.0.1", 60002,"hello world", (int)strlen("hello world")+1);
  51. while (nextTest!=2)
  52. {
  53. peer1->DeallocatePacket(peer1->Receive());
  54. RakNet::Packet *packet = peer2->Receive();
  55. if (packet)
  56. {
  57. if (packet->data[0]==ID_ADVERTISE_SYSTEM)
  58. {
  59. if (packet->length>1)
  60. printf("Got Advertise system with data: %s\n", packet->data+1);
  61. else
  62. printf("Got Advertise system with no data\n");
  63. printf("Was sent from GUID %s\n", packet->guid.ToString());
  64. printf("Sending ping from %s\n", peer2->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString());
  65. peer2->Ping("127.0.0.1", 60001, false);
  66. nextTest++;
  67. }
  68. else if (packet->data[0]==ID_UNCONNECTED_PONG)
  69. {
  70. // Peer or client. Response from a ping for an unconnected system.
  71. RakNet::TimeMS packetTime, dataLength;
  72. RakNet::TimeMS curTime = RakNet::GetTimeMS();
  73. RakNet::BitStream bsIn(packet->data,packet->length,false);
  74. bsIn.IgnoreBytes(1);
  75. bsIn.Read(packetTime);
  76. dataLength = packet->length - sizeof( unsigned char ) - sizeof( RakNet::TimeMS );
  77. if (peer2->IsLocalIP(packet->systemAddress.ToString(false)))
  78. printf("ID_UNCONNECTED_PONG from our own");
  79. else
  80. printf( "ID_UNCONNECTED_PONG from");
  81. printf(" %s on %p.\nPing is %i\nData is %i bytes long.\n", packet->systemAddress.ToString(), peer2, curTime-packetTime, dataLength );
  82. printf("Was sent from GUID %s\n", packet->guid.ToString());
  83. if ( dataLength > 0 )
  84. {
  85. printf( "Data is %s\n", packet->data + sizeof( unsigned char ) + sizeof( RakNet::TimeMS ) );
  86. RakAssert((int)strlen("Offline Ping Data")+1==packet->length-(sizeof( unsigned char ) + sizeof( RakNet::TimeMS )));
  87. }
  88. nextTest++;
  89. // ProcessUnhandledPacket(packet, ID_UNCONNECTED_PONG,interfaceType);
  90. }
  91. peer2->DeallocatePacket(packet);
  92. }
  93. #ifdef _WIN32
  94. Sleep(30);
  95. #else
  96. usleep(30 * 1000);
  97. #endif
  98. }
  99. printf("Test complete. Press enter to quit\n");
  100. Gets(text,sizeof(text));
  101. RakNet::RakPeerInterface::DestroyInstance(peer1);
  102. RakNet::RakPeerInterface::DestroyInstance(peer2);
  103. return 0;
  104. }
粤ICP备19079148号