FCM2HostSimultaneousTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "Gets.h"
  25. using namespace RakNet;
  26. #define NUM_PEERS 8
  27. RakNet::RakPeerInterface *rakPeer[NUM_PEERS];
  28. int main()
  29. {
  30. FullyConnectedMesh2 fcm2[NUM_PEERS];
  31. ConnectionGraph2 cg2[NUM_PEERS];
  32. for (int i=0; i < NUM_PEERS; i++)
  33. {
  34. rakPeer[i]=RakNet::RakPeerInterface::GetInstance();
  35. rakPeer[i]->AttachPlugin(&fcm2[i]);
  36. rakPeer[i]->AttachPlugin(&cg2[i]);
  37. fcm2[i].SetAutoparticipateConnections(true);
  38. RakNet::SocketDescriptor sd;
  39. sd.port=60000+i;
  40. StartupResult sr = rakPeer[i]->Startup(NUM_PEERS,&sd,1);
  41. RakAssert(sr==RAKNET_STARTED);
  42. rakPeer[i]->SetMaximumIncomingConnections(NUM_PEERS);
  43. rakPeer[i]->SetTimeoutTime(1000,RakNet::UNASSIGNED_SYSTEM_ADDRESS);
  44. printf("Our guid is %s\n", rakPeer[i]->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString());
  45. }
  46. for (int i=0; i < NUM_PEERS; i++)
  47. {
  48. for (int j=0; j < NUM_PEERS; j++)
  49. {
  50. if (i==j)
  51. continue;
  52. ConnectionAttemptResult car = rakPeer[i]->Connect("127.0.0.1", 60000+j, 0, 0 );
  53. RakAssert(car==CONNECTION_ATTEMPT_STARTED);
  54. }
  55. }
  56. bool quit=false;
  57. RakNet::Packet *packet;
  58. char ch;
  59. while (!quit)
  60. {
  61. for (int i=0; i < NUM_PEERS; i++)
  62. {
  63. for (packet = rakPeer[i]->Receive(); packet; rakPeer[i]->DeallocatePacket(packet), packet = rakPeer[i]->Receive())
  64. {
  65. switch (packet->data[0])
  66. {
  67. case ID_DISCONNECTION_NOTIFICATION:
  68. // Connection lost normally
  69. printf("%i. ID_DISCONNECTION_NOTIFICATION\n", i);
  70. break;
  71. case ID_CONNECTION_ATTEMPT_FAILED:
  72. printf("%i. ID_CONNECTION_ATTEMPT_FAILED\n", i);
  73. break;
  74. case ID_NEW_INCOMING_CONNECTION:
  75. // Somebody connected. We have their IP now
  76. printf("%i. ID_NEW_INCOMING_CONNECTION from %s. guid=%s.\n", i, packet->systemAddress.ToString(true), packet->guid.ToString());
  77. break;
  78. case ID_CONNECTION_REQUEST_ACCEPTED:
  79. // Somebody connected. We have their IP now
  80. printf("%i. ID_CONNECTION_REQUEST_ACCEPTED from %s. guid=%s.\n", i, packet->systemAddress.ToString(true), packet->guid.ToString());
  81. break;
  82. case ID_CONNECTION_LOST:
  83. // Couldn't deliver a reliable packet - i.e. the other system was abnormally
  84. // terminated
  85. printf("%i. ID_CONNECTION_LOST\n", i);
  86. break;
  87. case ID_FCM2_NEW_HOST:
  88. if (packet->systemAddress==RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  89. printf("%i. Got new host (ourselves)\n", i);
  90. else
  91. printf("%i. Got new host %s, %s\n", i, packet->systemAddress.ToString(true), packet->guid.ToString());
  92. break;
  93. }
  94. }
  95. }
  96. if (kbhit())
  97. {
  98. ch=getch();
  99. if (ch==' ')
  100. {
  101. unsigned int participantList;
  102. RakNetGUID hostGuid;
  103. bool weAreHost;
  104. for (int i=0; i < NUM_PEERS; i++)
  105. {
  106. if (rakPeer[i]->IsActive()==false)
  107. continue;
  108. fcm2[i].GetParticipantCount(&participantList);
  109. weAreHost=fcm2[i].IsHostSystem();
  110. hostGuid=fcm2[i].GetHostSystem();
  111. if (weAreHost)
  112. printf("%i. %iP myGuid=%s, hostGuid=%s tcc=%i (Y)\n",i, participantList, rakPeer[i]->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString(), hostGuid.ToString(), fcm2[i].GetTotalConnectionCount());
  113. else
  114. printf("%i. %iP myGuid=%s, hostGuid=%s tcc=%i (N)\n",i, participantList, rakPeer[i]->GetGuidFromSystemAddress(RakNet::UNASSIGNED_SYSTEM_ADDRESS).ToString(), hostGuid.ToString(), fcm2[i].GetTotalConnectionCount());
  115. }
  116. }
  117. if (ch=='d' || ch=='D')
  118. {
  119. char str[32];
  120. printf("Enter system index to disconnect.\n");
  121. Gets(str, sizeof(str));
  122. rakPeer[atoi(str)]->Shutdown(100);
  123. printf("Done.\n");
  124. }
  125. if (ch=='q' || ch=='Q')
  126. {
  127. printf("Quitting.\n");
  128. quit=true;
  129. }
  130. }
  131. RakSleep(30);
  132. }
  133. for (int i=0; i < NUM_PEERS; i++)
  134. {
  135. RakNet::RakPeerInterface::DestroyInstance(rakPeer[i]);
  136. }
  137. return 0;
  138. }
粤ICP备19079148号