Router2Sample.cpp 5.3 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 <stdio.h>
  12. #include "Kbhit.h"
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "BitStream.h"
  16. #include "MessageIdentifiers.h"
  17. #include "Router2.h"
  18. #include "RakSleep.h"
  19. #include "GetTime.h"
  20. #include "Rand.h"
  21. #include "RakAssert.h"
  22. #include "SocketLayer.h"
  23. #include "Getche.h"
  24. #include "Gets.h"
  25. using namespace RakNet;
  26. // Global just to make the sample easier to write
  27. RakNetGUID endpointGuid;
  28. RakPeerInterface *endpoint=0, *router=0;
  29. RakPeerInterface *rakPeer;
  30. Router2 *router2Plugin;
  31. void ReadAllPackets(void)
  32. {
  33. char str[64], str2[64];
  34. Packet *packet;
  35. for (packet=rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet=rakPeer->Receive())
  36. {
  37. packet->guid.ToString(str);
  38. packet->systemAddress.ToString(true,str2);
  39. if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
  40. {
  41. printf("ID_NEW_INCOMING_CONNECTION from %s on %s\n", str, str2);
  42. }
  43. else if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  44. {
  45. printf("ID_CONNECTION_REQUEST_ACCEPTED from %s on %s\n", str, str2);
  46. }
  47. else if (packet->data[0]==ID_ROUTER_2_FORWARDING_NO_PATH)
  48. {
  49. printf("No path to endpoint exists. Routing failed.\n");
  50. }
  51. else if (packet->data[0]==ID_CONNECTION_LOST)
  52. {
  53. printf("ID_CONNECTION_LOST from %s\n", str);
  54. }
  55. else if (packet->data[0]==ID_USER_PACKET_ENUM+1)
  56. {
  57. printf("Got ID_USER_PACKET_ENUM from %s\n", str);
  58. }
  59. else if (packet->data[0]==ID_ROUTER_2_FORWARDING_ESTABLISHED)
  60. {
  61. RakNet::BitStream bs(packet->data, packet->length, false);
  62. bs.IgnoreBytes(sizeof(MessageID));
  63. bs.Read(endpointGuid);
  64. printf("Routing through %s to %s successful. Connecting.\n", str, endpointGuid.ToString());
  65. unsigned short sourceToDestPort;
  66. bs.Read(sourceToDestPort);
  67. char ipAddressString[32];
  68. packet->systemAddress.ToString(false, ipAddressString);
  69. rakPeer->Connect(ipAddressString, sourceToDestPort, 0,0);
  70. }
  71. else if (packet->data[0]==ID_ROUTER_2_REROUTED)
  72. {
  73. // You could read the endpointGuid and sourceToDestPoint if you wanted to
  74. RakNet::BitStream bs(packet->data, packet->length, false);
  75. bs.IgnoreBytes(sizeof(MessageID));
  76. RakNetGUID endpointGuid2;
  77. bs.Read(endpointGuid2);
  78. endpointGuid2.ToString(str);
  79. SystemAddress intermediateAddress=packet->systemAddress;
  80. unsigned short port;
  81. bs.Read(port);
  82. intermediateAddress.SetPortHostOrder(port);
  83. char str2[32];
  84. intermediateAddress.ToString(true, str2);
  85. printf("Connection to %s rerouted through %s\n", str, str2);
  86. // Test sending a message to the endpoint
  87. RakNet::BitStream bsOut;
  88. MessageID id = ID_USER_PACKET_ENUM+1;
  89. bsOut.Write(id);
  90. rakPeer->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,endpointGuid2,false);
  91. }
  92. }
  93. }
  94. int main(void)
  95. {
  96. printf("Demonstration of the router2 plugin.\n");
  97. printf("The router2 plugin allows you to connect to a system, routing messages through\n");
  98. printf("a third system. This is useful if you can connect to the second system, but not\n");
  99. printf("the third, due to NAT issues.\n");
  100. printf("1. Start 4 instances, A, B, C, D\n");
  101. printf("2. Connect A to B and C, B to C, and D to B and C.\n");
  102. printf("3. On A press 'r' to start routing to D\n");
  103. printf("4. Once connected, close the router that did the routing to reroute.\n");
  104. printf("Difficulty: Advanced\n\n");;
  105. endpointGuid=UNASSIGNED_RAKNET_GUID;
  106. char str[64], str2[64];
  107. rakPeer=RakNet::RakPeerInterface::GetInstance();
  108. rakPeer->SetMaximumIncomingConnections(32);
  109. SocketDescriptor sd(0,0);
  110. rakPeer->Startup(32,&sd,1);
  111. printf("Enter 'c' to connect, 'r' to start routing, 'q' to quit.\n");
  112. rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS).ToString(str);
  113. printf("My GUID is %s\n", str);
  114. printf("Started on port %i\n", rakPeer->GetSocket(UNASSIGNED_SYSTEM_ADDRESS)->GetBoundAddress().GetPort());
  115. rakPeer->SetTimeoutTime(3000,UNASSIGNED_SYSTEM_ADDRESS);
  116. router2Plugin = new Router2;
  117. // Router2DebugInterface r2di;
  118. // router2Plugin->SetDebugInterface(&r2di);
  119. rakPeer->AttachPlugin(router2Plugin);
  120. router2Plugin->SetMaximumForwardingRequests(1);
  121. printf("Sample running. Press 'q' to quit\n");
  122. while (1)
  123. {
  124. if (kbhit())
  125. {
  126. char ch=getch();
  127. if (ch=='q')
  128. break;
  129. if (ch=='r')
  130. {
  131. do
  132. {
  133. printf("Enter destination guid: ");
  134. Gets(str2,sizeof(str2));
  135. } while (str2[0]==0);
  136. RakNetGUID destinationGuid;
  137. destinationGuid.FromString(str2);
  138. router2Plugin->EstablishRouting(destinationGuid);
  139. }
  140. if (ch=='c')
  141. {
  142. printf("Enter IP address to connect to: ");
  143. Gets(str,sizeof(str));
  144. if (str[0]==0)
  145. strcpy(str, "127.0.0.1");
  146. do
  147. {
  148. printf("Enter port to connect to: ");
  149. Gets(str2,sizeof(str2));
  150. } while (str2[0]==0);
  151. // Connect
  152. rakPeer->Connect(str,atoi(str2),0,0);
  153. }
  154. }
  155. RakSleep(30);
  156. ReadAllPackets();
  157. }
  158. RakNet::RakPeerInterface::DestroyInstance(rakPeer);
  159. delete router2Plugin;
  160. }
粤ICP备19079148号