main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "RakSleep.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "Kbhit.h"
  16. #include "MessageIdentifiers.h"
  17. #include "BitStream.h"
  18. #include "RakSleep.h"
  19. #include "UDPForwarder.h"
  20. #include "SocketLayer.h"
  21. int main()
  22. {
  23. RakNet::RakPeerInterface *rakPeer[2];
  24. rakPeer[0]=RakNet::RakPeerInterface::GetInstance();
  25. rakPeer[1]=RakNet::RakPeerInterface::GetInstance();
  26. RakNet::SocketDescriptor sd1(50000,0),sd2(50002,0);
  27. RakNet::StartupResult sr;
  28. #if RAKNET_SUPPORT_IPV6==1
  29. sd1.socketFamily=AF_INET6; // Test out IPV6
  30. #endif
  31. sr = rakPeer[0]->Startup(1,&sd1, 1);
  32. RakAssert(sr==RakNet::RAKNET_STARTED);
  33. #if RAKNET_SUPPORT_IPV6==1
  34. sd2.socketFamily=AF_INET6; // Test out IPV6
  35. #endif
  36. sr = rakPeer[1]->Startup(1,&sd2, 1);
  37. RakAssert(sr==RakNet::RAKNET_STARTED);
  38. rakPeer[1]->SetMaximumIncomingConnections(1);
  39. RakNet::UDPForwarder udpForwarder;
  40. printf("Demonstrates the UDP Forwarder class\n");
  41. printf("It routes datagrams from system to another, at the UDP level.\n");
  42. printf("You probably won't use UDPForwarder directly.\n");
  43. printf("See UDPProxyClient, UDPProxyServer, UDPProxyCoordinator.\n");
  44. // Start the forwarder
  45. udpForwarder.Startup();
  46. // RakNet will send a message at least every 5 seconds. Add another second to account for thread latency
  47. const RakNet::TimeMS timeoutOnNoDataMS=6000;
  48. // Address is probably 192.168.0.1. Fix it to be 127.0.0.1.
  49. // Only necessary to do this when connecting through the loopback on the local system. In a real system we'd stick with the external IP
  50. RakNet::SystemAddress peer0Addr = rakPeer[0]->GetMyBoundAddress();
  51. RakAssert(peer0Addr!=RakNet::UNASSIGNED_SYSTEM_ADDRESS);
  52. RakNet::SystemAddress peer1Addr = rakPeer[1]->GetMyBoundAddress();
  53. RakAssert(peer1Addr!=RakNet::UNASSIGNED_SYSTEM_ADDRESS);
  54. // peer0Addr.FromString("127.0.0.1");
  55. // peer1Addr.FromString("127.0.0.1");
  56. unsigned short fowardPort;
  57. SOCKET forwardingSocket;
  58. if (!udpForwarder.StartForwarding(peer0Addr,peer1Addr, timeoutOnNoDataMS, "127.0.0.1", sd1.socketFamily, &fowardPort, &forwardingSocket))
  59. {
  60. printf("Socket error\n");
  61. return 1;
  62. }
  63. RakNet::SystemAddress boundAddress;
  64. RakNet::SocketLayer::GetSystemAddress( forwardingSocket, &boundAddress );
  65. printf("UDPForwarder bound on %s\n", boundAddress.ToString(false));
  66. // Send a connect message to the forwarder, on the port to forward to rakPeer[1]
  67. RakNet::ConnectionAttemptResult car = rakPeer[0]->Connect(boundAddress.ToString(false), fowardPort, 0, 0);
  68. RakAssert(car==RakNet::CONNECTION_ATTEMPT_STARTED);
  69. printf("'q'uit.\n");
  70. RakNet::Packet *p;
  71. while (1)
  72. {
  73. for (int i=0; i < 2 ; i++)
  74. {
  75. p=rakPeer[i]->Receive();
  76. while (p)
  77. {
  78. if (p->data[0]==ID_DISCONNECTION_NOTIFICATION)
  79. printf("%s disconnected\n", p->systemAddress.ToString(true));
  80. else if (p->data[0]==ID_CONNECTION_LOST)
  81. printf("Lost connection to %s (failure)\n", p->systemAddress.ToString(true));
  82. else if (p->data[0]==ID_NO_FREE_INCOMING_CONNECTIONS)
  83. printf("%s has no free incoming connections.\n", p->systemAddress.ToString(true));
  84. else if (p->data[0]==ID_NEW_INCOMING_CONNECTION)
  85. printf("%s connected to us (success)\n", p->systemAddress.ToString(true));
  86. else if (p->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  87. printf("Connection request accepted from %s (success)\n", p->systemAddress.ToString(true));
  88. else if (p->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
  89. printf("Failed to connect to %s (failure)\n", p->systemAddress.ToString(true));
  90. rakPeer[i]->DeallocatePacket(p);
  91. p=rakPeer[i]->Receive();
  92. }
  93. }
  94. if (kbhit())
  95. {
  96. char ch = getch();
  97. if (ch=='q' || ch=='Q')
  98. break;
  99. }
  100. RakSleep(30);
  101. }
  102. rakPeer[0]->Shutdown(100,0);
  103. rakPeer[1]->Shutdown(100,0);
  104. RakNet::RakPeerInterface::DestroyInstance(rakPeer[0]);
  105. RakNet::RakPeerInterface::DestroyInstance(rakPeer[1]);
  106. return 0;
  107. }
粤ICP备19079148号