ReadyEventSample.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "ReadyEvent.h"
  18. #include <assert.h>
  19. #include "Kbhit.h"
  20. #include "RakSleep.h"
  21. #include "SocketLayer.h"
  22. #include "FullyConnectedMesh2.h"
  23. #include "ConnectionGraph2.h"
  24. void PrintConnections();
  25. using namespace RakNet;
  26. RakPeerInterface *rakPeer;
  27. ReadyEvent readyEventPlugin;
  28. // These two plugins are just to automatically create a fully connected mesh so I don't have to call connect more than once
  29. FullyConnectedMesh2 fcm2;
  30. ConnectionGraph2 cg2;
  31. int main(void)
  32. {
  33. rakPeer=RakPeerInterface::GetInstance();
  34. printf("This project tests and demonstrates the ready event plugin.\n");
  35. printf("It is used in a peer to peer environment to have a group of\nsystems signal an event.\n");
  36. printf("It is useful for changing turns in a turn based game,\nor for lobby systems where everyone has to set ready before the game starts\n");
  37. printf("Difficulty: Beginner\n\n");
  38. rakPeer->AttachPlugin(&readyEventPlugin);
  39. rakPeer->AttachPlugin(&fcm2);
  40. rakPeer->AttachPlugin(&cg2);
  41. rakPeer->SetMaximumIncomingConnections(8);
  42. fcm2.SetAutoparticipateConnections(true);
  43. fcm2.SetConnectOnNewRemoteConnection(true, "");
  44. cg2.SetAutoProcessNewConnections(true);
  45. // Initialize the peers
  46. SocketDescriptor sd(60000,0);
  47. while (IRNS2_Berkley::IsPortInUse(sd.port,sd.hostAddress,sd.socketFamily,SOCK_DGRAM)==true)
  48. sd.port++;
  49. StartupResult sr = rakPeer->Startup(8, &sd, 1);
  50. RakAssert(sr==RAKNET_STARTED);
  51. printf("Started on port %i\n", sd.port);
  52. // Give the threads time to properly start
  53. RakSleep(200);
  54. printf("Peers initialized.\n");
  55. printf("'C' to connect\n");
  56. printf("'D' to disconnect\n");
  57. printf("'S' to signal\n");
  58. printf("'U' to unsignal\n");
  59. printf("'F' to force all systems to be completed (cannot be unset)\n");
  60. printf("'Q' to quit\n");
  61. printf("' ' to print wait status\n");
  62. char str[128];
  63. char ch=0;
  64. while (1)
  65. {
  66. if (kbhit())
  67. ch=getch();
  68. if (ch=='s' || ch=='S')
  69. {
  70. ch=0;
  71. if (readyEventPlugin.SetEvent(0,true))
  72. printf("This system is signaled\n");
  73. else
  74. printf("This system is signaled FAILED\n");
  75. }
  76. if (ch=='u' || ch=='U')
  77. {
  78. ch=0;
  79. if (readyEventPlugin.SetEvent(0,false))
  80. printf("This system is unsignaled\n");
  81. else
  82. printf("This system is unsignaled FAILED\n");
  83. }
  84. if (ch=='c' || ch=='C')
  85. {
  86. ch=0;
  87. printf("Which IP? (Press enter for 127.0.0.1)");
  88. gets(str);
  89. if (str[0]==0)
  90. strcpy(str, "127.0.0.1");
  91. char port[64];
  92. printf("Which port? (Press enter for 60000)");
  93. gets(port);
  94. if (port[0]==0)
  95. strcpy(port, "60000");
  96. ConnectionAttemptResult car = rakPeer->Connect(str, atoi(port), 0, 0, 0);
  97. RakAssert(car==CONNECTION_ATTEMPT_STARTED);
  98. printf("Connecting.\n");
  99. }
  100. if (ch=='d' || ch=='D')
  101. {
  102. ch=0;
  103. rakPeer->Shutdown(100,0);
  104. sr = rakPeer->Startup(8, &sd, 1);
  105. RakAssert(sr==RAKNET_STARTED);
  106. printf("Restarting RakPeerInterface.\n");
  107. }
  108. if (ch=='f' || ch=='F')
  109. {
  110. ch=0;
  111. readyEventPlugin.ForceCompletion(0);
  112. printf("Called ForceCompletion()\nIsEventCompleted() will be fixed at true for all systems.\n");
  113. }
  114. if (ch==' ')
  115. {
  116. ch=0;
  117. printf("\n");
  118. PrintConnections();
  119. if (readyEventPlugin.IsEventSet(0))
  120. printf("Signaled=True, ");
  121. else
  122. printf("Signaled=False, ");
  123. if (readyEventPlugin.IsEventCompleted(0))
  124. printf("Completed=True\n");
  125. else if (readyEventPlugin.IsEventCompletionProcessing(0))
  126. printf("Completed=InProgress\n");
  127. else
  128. printf("Completed=False\n");
  129. DataStructures::List<SystemAddress> addresses;
  130. DataStructures::List<RakNetGUID> guids;
  131. rakPeer->GetSystemList(addresses, guids);
  132. for (unsigned short i=0; i < guids.Size(); i++)
  133. {
  134. ReadyEventSystemStatus ress = readyEventPlugin.GetReadyStatus(0, guids[i]);
  135. printf(" Remote system %i, status = ", i);
  136. switch (ress)
  137. {
  138. case RES_NOT_WAITING:
  139. printf("RES_NOT_WAITING\n");
  140. break;
  141. case RES_WAITING:
  142. printf("RES_WAITING\n");
  143. break;
  144. case RES_READY:
  145. printf("RES_READY\n");
  146. break;
  147. case RES_ALL_READY:
  148. printf("RES_ALL_READY\n");
  149. break;
  150. case RES_UNKNOWN_EVENT:
  151. printf("RES_UNKNOWN_EVENT\n");
  152. break;
  153. }
  154. }
  155. }
  156. if (ch=='Q' || ch=='q')
  157. {
  158. break;
  159. }
  160. Packet *p = rakPeer->Receive();
  161. if (p)
  162. {
  163. switch (p->data[0])
  164. {
  165. case ID_NEW_INCOMING_CONNECTION:
  166. printf("ID_NEW_INCOMING_CONNECTION\n");
  167. readyEventPlugin.AddToWaitList(0, p->guid);
  168. break;
  169. case ID_CONNECTION_REQUEST_ACCEPTED:
  170. printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
  171. readyEventPlugin.AddToWaitList(0, p->guid);
  172. break;
  173. case ID_READY_EVENT_ALL_SET:
  174. printf("Got ID_READY_EVENT_ALL_SET from %s\n", p->guid.ToString());
  175. break;
  176. case ID_READY_EVENT_SET:
  177. printf("Got ID_READY_EVENT_SET from %s\n", p->guid.ToString());
  178. break;
  179. case ID_READY_EVENT_UNSET:
  180. printf("Got ID_READY_EVENT_UNSET from %s\n", p->guid.ToString());
  181. break;
  182. case ID_DISCONNECTION_NOTIFICATION:
  183. // Connection lost normally
  184. printf("ID_DISCONNECTION_NOTIFICATION\n");
  185. break;
  186. case ID_ALREADY_CONNECTED:
  187. // Connection lost normally
  188. printf("ID_ALREADY_CONNECTED with guid %" PRINTF_64_BIT_MODIFIER "u\n", p->guid);
  189. break;
  190. case ID_INCOMPATIBLE_PROTOCOL_VERSION:
  191. printf("ID_INCOMPATIBLE_PROTOCOL_VERSION\n");
  192. break;
  193. case ID_REMOTE_DISCONNECTION_NOTIFICATION: // Server telling the clients of another client disconnecting gracefully. You can manually broadcast this in a peer to peer enviroment if you want.
  194. printf("ID_REMOTE_DISCONNECTION_NOTIFICATION\n");
  195. break;
  196. case ID_REMOTE_CONNECTION_LOST: // Server telling the clients of another client disconnecting forcefully. You can manually broadcast this in a peer to peer enviroment if you want.
  197. printf("ID_REMOTE_CONNECTION_LOST\n");
  198. break;
  199. case ID_REMOTE_NEW_INCOMING_CONNECTION: // Server telling the clients of another client connecting. You can manually broadcast this in a peer to peer enviroment if you want.
  200. printf("ID_REMOTE_NEW_INCOMING_CONNECTION\n");
  201. break;
  202. case ID_CONNECTION_BANNED: // Banned from this server
  203. printf("We are banned from this server.\n");
  204. break;
  205. case ID_CONNECTION_ATTEMPT_FAILED:
  206. printf("Connection attempt failed\n");
  207. break;
  208. case ID_NO_FREE_INCOMING_CONNECTIONS:
  209. // Sorry, the server is full. I don't do anything here but
  210. // A real app should tell the user
  211. printf("ID_NO_FREE_INCOMING_CONNECTIONS\n");
  212. break;
  213. case ID_INVALID_PASSWORD:
  214. printf("ID_INVALID_PASSWORD\n");
  215. break;
  216. case ID_CONNECTION_LOST:
  217. // Couldn't deliver a reliable packet - i.e. the other system was abnormally
  218. // terminated
  219. printf("ID_CONNECTION_LOST\n");
  220. break;
  221. case ID_CONNECTED_PING:
  222. case ID_UNCONNECTED_PING:
  223. printf("Ping from %s\n", p->systemAddress.ToString(true));
  224. break;
  225. }
  226. rakPeer->DeallocatePacket(p);
  227. }
  228. // Keep raknet threads responsive
  229. RakSleep(30);
  230. }
  231. RakPeerInterface::DestroyInstance(rakPeer);
  232. return 1;
  233. }
  234. void PrintConnections()
  235. {
  236. int i,j;
  237. char ch=0;
  238. SystemAddress systemAddress;
  239. printf("--------------------------------\n");
  240. SystemAddress remoteSystems[8];
  241. unsigned short numberOfSystems;
  242. rakPeer->GetConnectionList(remoteSystems, &numberOfSystems);
  243. for (i=0; i < numberOfSystems; i++)
  244. {
  245. printf("%i (Conn): ", 60000+i);
  246. for (j=0; j < numberOfSystems; j++)
  247. {
  248. systemAddress=rakPeer->GetSystemAddressFromIndex(j);
  249. if (systemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
  250. printf("%i ", systemAddress.GetPort());
  251. }
  252. printf("\n");
  253. }
  254. printf("\n");
  255. printf("--------------------------------\n");
  256. }
粤ICP备19079148号