FCM2HostTest.cpp 5.1 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 <cstdio>
  11. #include <cstring>
  12. #include <stdlib.h>
  13. #include "GetTime.h"
  14. #include "RakPeerInterface.h"
  15. #include "MessageIdentifiers.h"
  16. #include "RakNetTypes.h"
  17. #include "RakSleep.h"
  18. #include "FullyConnectedMesh2.h"
  19. #include "ConnectionGraph2.h"
  20. #include <assert.h>
  21. #include "SocketLayer.h"
  22. #include "Kbhit.h"
  23. #include "PacketLogger.h"
  24. #include "BitStream.h"
  25. using namespace RakNet;
  26. RakNet::RakPeerInterface *rakPeer;
  27. int main()
  28. {
  29. FullyConnectedMesh2 fcm2;
  30. ConnectionGraph2 cg2;
  31. rakPeer=RakNet::RakPeerInterface::GetInstance();
  32. rakPeer->AttachPlugin(&fcm2);
  33. rakPeer->AttachPlugin(&cg2);
  34. fcm2.SetAutoparticipateConnections(true);
  35. RakNet::SocketDescriptor sd;
  36. sd.socketFamily=AF_INET; // Only IPV4 supports broadcast on 255.255.255.255
  37. sd.port=60000;
  38. while (IRNS2_Berkley::IsPortInUse(sd.port, sd.hostAddress, sd.socketFamily, SOCK_DGRAM)==true)
  39. sd.port++;
  40. StartupResult sr = rakPeer->Startup(8,&sd,1);
  41. RakAssert(sr==RAKNET_STARTED);
  42. rakPeer->SetMaximumIncomingConnections(8);
  43. rakPeer->SetTimeoutTime(1000,RakNet::UNASSIGNED_SYSTEM_ADDRESS);
  44. printf("Our guid is %s\n", rakPeer->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString());
  45. printf("Started on %s\n", rakPeer->GetMyBoundAddress().ToString(true));
  46. BitStream contextBs;
  47. contextBs.Write(RakString("Our guid is %s\n", rakPeer->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString()));
  48. fcm2.SetMyContext(&contextBs);
  49. // PacketLogger packetLogger;
  50. // rakPeer->AttachPlugin(&packetLogger);
  51. // packetLogger.SetLogDirectMessages(false);
  52. bool quit=false;
  53. RakNet::Packet *packet;
  54. char ch;
  55. while (!quit)
  56. {
  57. for (packet = rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet = rakPeer->Receive())
  58. {
  59. switch (packet->data[0])
  60. {
  61. case ID_DISCONNECTION_NOTIFICATION:
  62. // Connection lost normally
  63. printf("ID_DISCONNECTION_NOTIFICATION\n");
  64. break;
  65. case ID_NEW_INCOMING_CONNECTION:
  66. // Somebody connected. We have their IP now
  67. printf("ID_NEW_INCOMING_CONNECTION from %s. guid=%s.\n", packet->systemAddress.ToString(true), packet->guid.ToString());
  68. break;
  69. case ID_CONNECTION_REQUEST_ACCEPTED:
  70. // Somebody connected. We have their IP now
  71. printf("ID_CONNECTION_REQUEST_ACCEPTED from %s. guid=%s.\n", packet->systemAddress.ToString(true), packet->guid.ToString());
  72. break;
  73. case ID_CONNECTION_LOST:
  74. // Couldn't deliver a reliable packet - i.e. the other system was abnormally
  75. // terminated
  76. printf("ID_CONNECTION_LOST\n");
  77. break;
  78. case ID_ADVERTISE_SYSTEM:
  79. if (packet->guid!=rakPeer->GetMyGUID())
  80. rakPeer->Connect(packet->systemAddress.ToString(false), packet->systemAddress.GetPort(),0,0);
  81. break;
  82. case ID_FCM2_NEW_HOST:
  83. {
  84. if (packet->guid==rakPeer->GetMyGUID())
  85. printf("Got new host (ourselves)");
  86. else
  87. printf("Got new host %s, GUID=%s", packet->systemAddress.ToString(true), packet->guid.ToString());
  88. RakNet::BitStream bs(packet->data,packet->length,false);
  89. bs.IgnoreBytes(1);
  90. RakNetGUID oldHost;
  91. bs.Read(oldHost);
  92. // If oldHost is different then the current host, then we lost connection to the host
  93. if (oldHost!=UNASSIGNED_RAKNET_GUID)
  94. printf(". Oldhost Guid=%s\n", oldHost.ToString());
  95. else
  96. printf(". (First reported host)\n");
  97. }
  98. break;
  99. // case ID_REMOTE_NEW_INCOMING_CONNECTION:
  100. // {
  101. // uint32_t count;
  102. // RakNet::BitStream bsIn(packet->data, packet->length,false);
  103. // bsIn.IgnoreBytes(1);
  104. // bsIn.Read(count);
  105. // SystemAddress sa;
  106. // RakNetGUID guid;
  107. // printf("ID_REMOTE_NEW_INCOMING_CONNECTION from %s\n", packet->systemAddress.ToString(true));
  108. // for (uint32_t i=0; i < count; i++)
  109. // {
  110. // bsIn.Read(sa);
  111. // bsIn.Read(guid);
  112. //
  113. // printf("%s ", sa.ToString(true));
  114. // }
  115. // printf("\n");
  116. // }
  117. }
  118. }
  119. if (kbhit())
  120. {
  121. ch=getch();
  122. if (ch==' ')
  123. {
  124. DataStructures::List<RakNetGUID> participantList;
  125. fcm2.GetParticipantList(participantList);
  126. printf("%i participants\n", participantList.Size());
  127. for (int i=0; i < participantList.Size(); i++)
  128. {
  129. BitStream userContext;
  130. fcm2.GetParticipantContext(participantList[i], &userContext);
  131. RakString str;
  132. userContext.Read(str);
  133. printf("%i. %s: %s", i+1, participantList[i].ToString(), str.C_String());
  134. }
  135. }
  136. if (ch=='q' || ch=='Q')
  137. {
  138. printf("Quitting.\n");
  139. quit=true;
  140. }
  141. }
  142. RakSleep(30);
  143. for (int i=0; i < 32; i++)
  144. {
  145. if (rakPeer->GetInternalID(RakNet::UNASSIGNED_SYSTEM_ADDRESS,0).GetPort()!=60000+i)
  146. rakPeer->AdvertiseSystem("255.255.255.255", 60000+i, 0,0,0);
  147. }
  148. }
  149. RakNet::RakPeerInterface::DestroyInstance(rakPeer);
  150. return 0;
  151. }
粤ICP备19079148号