FCMTest.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "Rand.h"
  15. #include "RakPeerInterface.h"
  16. #include "MessageIdentifiers.h"
  17. #include "FullyConnectedMesh2.h"
  18. #include "ConnectionGraph2.h"
  19. #include <assert.h>
  20. #include "Kbhit.h"
  21. #include "RakSleep.h"
  22. static const int NUM_PEERS=8;
  23. RakNet::RakPeerInterface *rakPeer[NUM_PEERS];
  24. RakNet::FullyConnectedMesh2 fullyConnectedMeshPlugin[NUM_PEERS];
  25. RakNet::ConnectionGraph2 connectionGraphPlugin[NUM_PEERS];
  26. void PrintConnections(void);
  27. int main(void)
  28. {
  29. int i;
  30. for (i=0; i < NUM_PEERS; i++)
  31. rakPeer[i]=RakNet::RakPeerInterface::GetInstance();
  32. printf("This project tests and demonstrates the fully connected mesh plugin.\n");
  33. printf("No data is actually sent so it's mostly a sample of how to use a plugin.\n");
  34. printf("Difficulty: Beginner\n\n");
  35. int peerIndex;
  36. // Initialize the message handlers
  37. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  38. {
  39. // fullyConnectedMeshPlugin[peerIndex].Startup(0,0);
  40. rakPeer[peerIndex]->AttachPlugin(&fullyConnectedMeshPlugin[peerIndex]);
  41. // The fully connected mesh relies on the connection graph plugin also being attached
  42. rakPeer[peerIndex]->AttachPlugin(&connectionGraphPlugin[peerIndex]);
  43. rakPeer[peerIndex]->SetMaximumIncomingConnections(NUM_PEERS);
  44. }
  45. // Initialize the peers
  46. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  47. {
  48. RakNet::SocketDescriptor socketDescriptor(60000+peerIndex,0);
  49. rakPeer[peerIndex]->Startup(NUM_PEERS, &socketDescriptor, 1);
  50. }
  51. // Give the threads time to properly start
  52. RakSleep(200);
  53. printf("Peers initialized. ");
  54. printf("Connecting each peer to the prior peer\n");
  55. // Connect each peer to the prior peer
  56. for (peerIndex=1; peerIndex < NUM_PEERS; peerIndex++)
  57. {
  58. rakPeer[peerIndex]->Connect("127.0.0.1", 60000+peerIndex-1, 0, 0);
  59. }
  60. PrintConnections();
  61. // Close all connections
  62. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  63. {
  64. rakPeer[peerIndex]->Shutdown(100);
  65. }
  66. // Reinitialize the peers
  67. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  68. {
  69. RakNet::SocketDescriptor socketDescriptor(60000+peerIndex,0);
  70. rakPeer[peerIndex]->Startup(NUM_PEERS,&socketDescriptor, 1 );
  71. }
  72. printf("Connecting each peer to a central peer.\n");
  73. // Connect each peer to a central peer
  74. for (peerIndex=1; peerIndex < NUM_PEERS; peerIndex++)
  75. {
  76. rakPeer[peerIndex]->Connect("127.0.0.1", 60000, 0, 0);
  77. }
  78. PrintConnections();
  79. // Close all connections
  80. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  81. {
  82. rakPeer[peerIndex]->Shutdown(100);
  83. }
  84. // Reinitialize the peers
  85. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  86. {
  87. RakNet::SocketDescriptor socketDescriptor(60000+peerIndex,0);
  88. rakPeer[peerIndex]->Startup(NUM_PEERS, &socketDescriptor, 1);
  89. }
  90. printf("Cross connecting each pair of peers, then first and last peer.\n");
  91. // Connect each peer to a central peer
  92. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  93. {
  94. rakPeer[peerIndex]->Connect("127.0.0.1", 60000+peerIndex+(((peerIndex%2)==0) ? 1 : -1), 0, 0);
  95. }
  96. printf("Pairs Connected\n");
  97. PrintConnections();
  98. rakPeer[0]->Connect("127.0.0.1", 60000+NUM_PEERS-1, 0, 0);
  99. printf("First and last connected\n");
  100. PrintConnections();
  101. // Close all connections
  102. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  103. {
  104. rakPeer[peerIndex]->Shutdown(100);
  105. }
  106. // Reinitialize the peers
  107. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  108. {
  109. RakNet::SocketDescriptor socketDescriptor(60000+peerIndex,0);
  110. rakPeer[peerIndex]->Startup(NUM_PEERS, &socketDescriptor, 1);
  111. }
  112. unsigned int seed = (unsigned int) RakNet::GetTimeMS();
  113. seedMT(seed);
  114. printf("Connecting each peer to a random peer with seed %u.\n", seed);
  115. int connectTo=0;
  116. // Connect each peer to a central peer
  117. for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
  118. {
  119. do
  120. {
  121. connectTo=randomMT() % NUM_PEERS;
  122. } while (connectTo==peerIndex);
  123. rakPeer[peerIndex]->Connect("127.0.0.1", 60000+connectTo, 0, 0);
  124. }
  125. PrintConnections();
  126. for (i=0; i < NUM_PEERS; i++)
  127. RakNet::RakPeerInterface::DestroyInstance(rakPeer[i]);
  128. return 1;
  129. }
  130. void PrintConnections()
  131. {
  132. int i,j;
  133. char ch=0;
  134. RakNet::SystemAddress systemAddress;
  135. RakNet::Packet *packet;
  136. printf("Connecting. Press space to see status or c to continue.\n");
  137. while (ch!='c' && ch!='C')
  138. {
  139. if (kbhit())
  140. ch=getch();
  141. if (ch==' ')
  142. {
  143. printf("--------------------------------\n");
  144. for (i=0; i < NUM_PEERS; i++)
  145. {
  146. if (NUM_PEERS<=10)
  147. {
  148. /*
  149. printf("%i (Mesh): ", 60000+i);
  150. for (j=0; j < (int)fullyConnectedMeshPlugin[i].GetMeshPeerListSize(); j++)
  151. {
  152. systemAddress=fullyConnectedMeshPlugin[i].GetPeerIDAtIndex(j);
  153. if (systemAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  154. printf("%i ", systemAddress.GetPort());
  155. }
  156. printf("\n");
  157. */
  158. printf("%i (Conn): ", 60000+i);
  159. for (j=0; j < NUM_PEERS; j++)
  160. {
  161. systemAddress=rakPeer[i]->GetSystemAddressFromIndex(j);
  162. if (systemAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  163. printf("%i ", systemAddress.GetPort());
  164. }
  165. printf("\n");
  166. }
  167. else
  168. {
  169. int connCount;
  170. //int meshCount;
  171. for (connCount=0, j=0; j < NUM_PEERS; j++)
  172. {
  173. systemAddress=rakPeer[i]->GetSystemAddressFromIndex(j);
  174. if (systemAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  175. connCount++;
  176. }
  177. /*
  178. for (meshCount=0, j=0; j < (int)fullyConnectedMeshPlugin[i].GetMeshPeerListSize(); j++)
  179. {
  180. systemAddress=fullyConnectedMeshPlugin[i].GetPeerIDAtIndex(j);
  181. if (systemAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  182. meshCount++;
  183. }
  184. */
  185. //printf("%i (Mesh): %i peers should be connected\n", 60000+i, meshCount);
  186. printf("%i (Conn): %i peers are connected\n", 60000+i, connCount);
  187. }
  188. }
  189. printf("\n");
  190. ch=0;
  191. printf("--------------------------------\n");
  192. }
  193. for (i=0; i < NUM_PEERS; i++)
  194. {
  195. packet=rakPeer[i]->Receive();
  196. if (packet)
  197. {
  198. if (packet->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
  199. printf("%i: ID_CONNECTION_ATTEMPT_FAILED to %i\n", 60000+i, packet->systemAddress.GetPort());
  200. // if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
  201. // printf("%i: ID_NEW_INCOMING_CONNECTION from %i\n", 60000+i, packet->systemAddress.GetPort());
  202. // if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  203. // printf("%i: ID_CONNECTION_REQUEST_ACCEPTED from %i\n", 60000+i, packet->systemAddress.GetPort());
  204. rakPeer[i]->DeallocatePacket(packet);
  205. }
  206. }
  207. // Keep raknet threads responsive
  208. RakSleep(30);
  209. }
  210. }
粤ICP备19079148号