CloudTest.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "BitStream.h"
  12. #include "GetTime.h"
  13. #include "RakSleep.h"
  14. #include "Gets.h"
  15. #include "MessageIdentifiers.h"
  16. #include "CloudServer.h"
  17. #include "CloudClient.h"
  18. #include "Kbhit.h"
  19. enum
  20. {
  21. CLIENT_1,
  22. CLIENT_2,
  23. SERVER_1,
  24. SERVER_2,
  25. RAKPEER_COUNT
  26. };
  27. enum
  28. {
  29. CLOUD_CLIENT_1,
  30. CLOUD_CLIENT_2,
  31. CLOUD_CLIENT_COUNT
  32. };
  33. enum
  34. {
  35. CLOUD_SERVER_1,
  36. CLOUD_SERVER_2,
  37. CLOUD_SERVER_COUNT
  38. };
  39. static const unsigned short STARTING_PORT=60000;
  40. class MyCallback : public RakNet::CloudClientCallback
  41. {
  42. virtual void OnGet(RakNet::CloudQueryResult *result, bool *deallocateRowsAfterReturn)
  43. {
  44. printf("On Download %i rows. IsSubscription=%i.\n", result->rowsReturned.Size(), result->subscribeToResults);
  45. }
  46. virtual void OnSubscriptionNotification(RakNet::CloudQueryRow *result, bool wasUpdated, bool *deallocateRowAfterReturn)
  47. {
  48. if (wasUpdated)
  49. printf("OnSubscriptionNotification Updated\n");
  50. else
  51. printf("OnSubscriptionNotification Deleted\n");
  52. }
  53. };
  54. class MyAllocator : public RakNet::CloudAllocator
  55. {
  56. };
  57. int main(void)
  58. {
  59. printf("Tests the CloudServer and CloudClient plugins.\n");
  60. printf("Difficulty: Intermediate\n\n");
  61. MyCallback myCloudClientCallback;
  62. MyAllocator myCloudClientAllocator;
  63. RakNet::CloudServer cloudServer[CLOUD_SERVER_COUNT];
  64. RakNet::CloudClient cloudClient[CLOUD_CLIENT_COUNT];
  65. RakNet::RakPeerInterface *rakPeer[RAKPEER_COUNT];
  66. for (unsigned int i=0; i < RAKPEER_COUNT; i++)
  67. {
  68. rakPeer[i]=RakNet::RakPeerInterface::GetInstance();
  69. RakNet::SocketDescriptor sd(STARTING_PORT+i,0);
  70. rakPeer[i]->Startup(RAKPEER_COUNT,&sd,1);
  71. }
  72. for (unsigned int i=SERVER_1; i < RAKPEER_COUNT; i++)
  73. {
  74. rakPeer[i]->SetMaximumIncomingConnections(RAKPEER_COUNT);
  75. }
  76. for (unsigned int i=CLIENT_1, j=CLOUD_CLIENT_1; i < SERVER_1; i++, j++)
  77. {
  78. rakPeer[i]->AttachPlugin(&cloudClient[j]);
  79. }
  80. for (unsigned int i=SERVER_1, j=CLOUD_SERVER_1; i < RAKPEER_COUNT; i++, j++)
  81. {
  82. rakPeer[i]->AttachPlugin(&cloudServer[j]);
  83. }
  84. // Connect servers to each other
  85. for (unsigned int i=SERVER_1; i < RAKPEER_COUNT-1; i++)
  86. {
  87. for (unsigned int j=i+1; j < RAKPEER_COUNT; j++)
  88. {
  89. rakPeer[j]->Connect("127.0.0.1", STARTING_PORT+i, 0, 0);
  90. }
  91. }
  92. RakSleep(100);
  93. // Tell servers about each other
  94. for (unsigned int i=SERVER_1; i < RAKPEER_COUNT; i++)
  95. {
  96. unsigned short numberOfSystems;
  97. rakPeer[i]->GetConnectionList(0, &numberOfSystems);
  98. for (unsigned int j=0; j < numberOfSystems; j++)
  99. {
  100. cloudServer[i-SERVER_1].AddServer(rakPeer[i]->GetGUIDFromIndex(j));
  101. }
  102. }
  103. // Connect clients to servers, assume equal counts
  104. for (unsigned int i=CLIENT_1; i < SERVER_1; i++)
  105. {
  106. rakPeer[i]->Connect("127.0.0.1", STARTING_PORT+SERVER_1+i, 0, 0);
  107. }
  108. printf("'A' to upload data set 1 from client 1 to server 1.\n");
  109. printf("'B' to upload data set 1 from client 2 to server 2.\n");
  110. printf("'C' to upload data set 2 from client 1 to server 1.\n");
  111. printf("'D' to download data sets 1 and 2 from client 1 from server 1.\n");
  112. printf("'E' To release data set 1 and 2 from client 1 to server 1.\n");
  113. printf("'F' To subscribe to data sets 1 and 2 from client 2 to server 2.\n");
  114. printf("'G' To unsubscribe to data sets 1 and 2 from client 2 to server 2.\n");
  115. printf("'H' To release data set 1 and 2 from client 2 to server 2.\n");
  116. printf("'Y' to disconnect client 1.\n");
  117. RakNet::Packet *packet;
  118. while (1)
  119. {
  120. char command;
  121. if (kbhit())
  122. {
  123. command=getch();
  124. if (command=='a' || command=='A')
  125. {
  126. printf("Uploading data set 1 from client 1\n");
  127. RakNet::CloudKey dataKey1;
  128. dataKey1.primaryKey="ApplicationName";
  129. dataKey1.secondaryKey=1;
  130. cloudClient[CLOUD_CLIENT_1].Post(&dataKey1, (const unsigned char*) "DS1C1S1", (uint32_t) strlen("DS1C1S1")+1, rakPeer[CLIENT_1]->GetGUIDFromIndex(0));
  131. }
  132. else if (command=='b' || command=='B')
  133. {
  134. printf("Uploading data set 1 from client 2\n");
  135. RakNet::CloudKey dataKey1;
  136. dataKey1.primaryKey="ApplicationName";
  137. dataKey1.secondaryKey=1;
  138. cloudClient[CLOUD_CLIENT_2].Post(&dataKey1, (const unsigned char*) "DS1C2S2", (uint32_t) strlen("DS1C2S2")+1, rakPeer[CLIENT_2]->GetGUIDFromIndex(0));
  139. }
  140. else if (command=='c' || command=='C')
  141. {
  142. printf("Uploading data set 2 from client 2\n");
  143. RakNet::CloudKey dataKey1;
  144. dataKey1.primaryKey="ApplicationName";
  145. dataKey1.secondaryKey=2;
  146. cloudClient[CLOUD_CLIENT_1].Post(&dataKey1, (const unsigned char*) "DS2C2S1", (uint32_t) strlen("DS2C2S1")+1, rakPeer[CLIENT_1]->GetGUIDFromIndex(0));
  147. }
  148. else if (command=='d' || command=='D')
  149. {
  150. printf("Downloading data sets 1 and 2 from client 1\n");
  151. RakNet::CloudKey dataKey1;
  152. dataKey1.primaryKey="ApplicationName";
  153. dataKey1.secondaryKey=1;
  154. RakNet::CloudQuery keyQuery;
  155. keyQuery.keys.Push(RakNet::CloudKey("ApplicationName", 1), _FILE_AND_LINE_);
  156. keyQuery.keys.Push(RakNet::CloudKey("ApplicationName", 2), _FILE_AND_LINE_);
  157. keyQuery.maxRowsToReturn=0;
  158. keyQuery.startingRowIndex=0;
  159. keyQuery.subscribeToResults=false;
  160. cloudClient[CLOUD_CLIENT_1].Get(&keyQuery, rakPeer[CLIENT_1]->GetGUIDFromIndex(0));
  161. }
  162. else if (command=='e' || command=='E')
  163. {
  164. printf("Releasing data sets 1 and 2 from client 1\n");
  165. DataStructures::List<RakNet::CloudKey> keys;
  166. keys.Push(RakNet::CloudKey("ApplicationName", 1), _FILE_AND_LINE_);
  167. keys.Push(RakNet::CloudKey("ApplicationName", 2), _FILE_AND_LINE_);
  168. cloudClient[CLOUD_CLIENT_1].Release(keys, rakPeer[CLIENT_1]->GetGUIDFromIndex(0));
  169. }
  170. else if (command=='f' || command=='F')
  171. {
  172. printf("Subscribing to data sets 1 and 2 from client 2 to server 2.\n");
  173. RakNet::CloudQuery keyQuery;
  174. keyQuery.keys.Push(RakNet::CloudKey("ApplicationName", 1), _FILE_AND_LINE_);
  175. keyQuery.keys.Push(RakNet::CloudKey("ApplicationName", 2), _FILE_AND_LINE_);
  176. keyQuery.maxRowsToReturn=0;
  177. keyQuery.startingRowIndex=0;
  178. keyQuery.subscribeToResults=true;
  179. DataStructures::List<RakNet::RakNetGUID> specificSystems;
  180. specificSystems.Push(rakPeer[CLIENT_1]->GetMyGUID(), _FILE_AND_LINE_);
  181. cloudClient[CLOUD_CLIENT_2].Get(&keyQuery, specificSystems, rakPeer[CLIENT_2]->GetGUIDFromIndex(0));
  182. }
  183. else if (command=='g' || command=='G')
  184. {
  185. printf("Unsubscribing to data sets 1 and 2 from client 2 to server 2.\n");
  186. DataStructures::List<RakNet::CloudKey> keys;
  187. keys.Push(RakNet::CloudKey("ApplicationName", 1), _FILE_AND_LINE_);
  188. keys.Push(RakNet::CloudKey("ApplicationName", 2), _FILE_AND_LINE_);
  189. DataStructures::List<RakNet::RakNetGUID> specificSystems;
  190. specificSystems.Push(rakPeer[CLIENT_1]->GetMyGUID(), _FILE_AND_LINE_);
  191. cloudClient[CLOUD_CLIENT_2].Unsubscribe(keys, specificSystems, rakPeer[CLIENT_2]->GetGUIDFromIndex(0));
  192. }
  193. else if (command=='h' || command=='H')
  194. {
  195. printf("Releasing data sets 1 and 2 from client 2\n");
  196. DataStructures::List<RakNet::CloudKey> keys;
  197. keys.Push(RakNet::CloudKey("ApplicationName", 1), _FILE_AND_LINE_);
  198. keys.Push(RakNet::CloudKey("ApplicationName", 2), _FILE_AND_LINE_);
  199. cloudClient[CLOUD_CLIENT_2].Unsubscribe(keys, rakPeer[CLIENT_2]->GetGUIDFromIndex(0));
  200. }
  201. else if (command=='y' || command=='Y')
  202. {
  203. printf("Disconnecting client 1\n");
  204. rakPeer[CLIENT_1]->Shutdown(100);
  205. }
  206. }
  207. for (unsigned int rakPeerInstanceIndex=0; rakPeerInstanceIndex < RAKPEER_COUNT; rakPeerInstanceIndex++)
  208. {
  209. for (packet = rakPeer[rakPeerInstanceIndex]->Receive(); packet; rakPeer[rakPeerInstanceIndex]->DeallocatePacket(packet), packet = rakPeer[rakPeerInstanceIndex]->Receive())
  210. {
  211. printf("Instance %i: ", rakPeerInstanceIndex);
  212. switch (packet->data[0])
  213. {
  214. case ID_DISCONNECTION_NOTIFICATION:
  215. printf("ID_DISCONNECTION_NOTIFICATION\n");
  216. break;
  217. case ID_NEW_INCOMING_CONNECTION:
  218. printf("ID_NEW_INCOMING_CONNECTION\n");
  219. break;
  220. case ID_ALREADY_CONNECTED:
  221. printf("ID_ALREADY_CONNECTED\n");
  222. break;
  223. case ID_INCOMPATIBLE_PROTOCOL_VERSION:
  224. printf("ID_INCOMPATIBLE_PROTOCOL_VERSION\n");
  225. break;
  226. case ID_REMOTE_DISCONNECTION_NOTIFICATION:
  227. printf("ID_REMOTE_DISCONNECTION_NOTIFICATION\n");
  228. break;
  229. case ID_REMOTE_CONNECTION_LOST:
  230. printf("ID_REMOTE_CONNECTION_LOST\n");
  231. break;
  232. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  233. printf("ID_REMOTE_NEW_INCOMING_CONNECTION\n");
  234. break;
  235. case ID_CONNECTION_BANNED:
  236. printf("We are banned from this server.\n");
  237. break;
  238. case ID_CONNECTION_ATTEMPT_FAILED:
  239. printf("Connection attempt failed\n");
  240. break;
  241. case ID_NO_FREE_INCOMING_CONNECTIONS:
  242. printf("ID_NO_FREE_INCOMING_CONNECTIONS\n");
  243. break;
  244. case ID_INVALID_PASSWORD:
  245. printf("ID_INVALID_PASSWORD\n");
  246. break;
  247. case ID_CONNECTION_LOST:
  248. printf("ID_CONNECTION_LOST\n");
  249. break;
  250. case ID_CONNECTION_REQUEST_ACCEPTED:
  251. printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
  252. break;
  253. case ID_CLOUD_GET_RESPONSE:
  254. cloudClient[rakPeerInstanceIndex].OnGetReponse(packet, &myCloudClientCallback, &myCloudClientAllocator);
  255. break;
  256. case ID_CLOUD_SUBSCRIPTION_NOTIFICATION:
  257. cloudClient[rakPeerInstanceIndex].OnSubscriptionNotification(packet, &myCloudClientCallback, &myCloudClientAllocator);
  258. break;
  259. default:
  260. printf("Packet ID %i\n", packet->data[0]);
  261. }
  262. }
  263. }
  264. RakSleep(30);
  265. }
  266. for (unsigned int i=0; i < RAKPEER_COUNT; i++)
  267. {
  268. RakNet::RakPeerInterface::DestroyInstance(rakPeer[i]);
  269. }
  270. return 0;
  271. }
粤ICP备19079148号