CloudClient.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "NativeFeatureIncludes.h"
  11. #if _RAKNET_SUPPORT_CloudClient==1
  12. #include "CloudClient.h"
  13. #include "GetTime.h"
  14. #include "MessageIdentifiers.h"
  15. #include "BitStream.h"
  16. #include "RakPeerInterface.h"
  17. using namespace RakNet;
  18. STATIC_FACTORY_DEFINITIONS(CloudClient,CloudClient);
  19. CloudClient::CloudClient()
  20. {
  21. callback=0;
  22. allocator=&unsetDefaultAllocator;
  23. }
  24. CloudClient::~CloudClient()
  25. {
  26. }
  27. void CloudClient::SetDefaultCallbacks(CloudAllocator *_allocator, CloudClientCallback *_callback)
  28. {
  29. callback=_callback;
  30. allocator=_allocator;
  31. }
  32. void CloudClient::Post(CloudKey *cloudKey, const unsigned char *data, uint32_t dataLengthBytes, RakNetGUID systemIdentifier)
  33. {
  34. RakAssert(cloudKey);
  35. RakNet::BitStream bsOut;
  36. bsOut.Write((MessageID)ID_CLOUD_POST_REQUEST);
  37. cloudKey->Serialize(true,&bsOut);
  38. if (data==0)
  39. dataLengthBytes=0;
  40. bsOut.Write(dataLengthBytes);
  41. if (dataLengthBytes>0)
  42. bsOut.WriteAlignedBytes((const unsigned char*) data, dataLengthBytes);
  43. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, systemIdentifier, false);
  44. }
  45. void CloudClient::Release(DataStructures::List<CloudKey> &keys, RakNetGUID systemIdentifier)
  46. {
  47. RakNet::BitStream bsOut;
  48. bsOut.Write((MessageID)ID_CLOUD_RELEASE_REQUEST);
  49. RakAssert(keys.Size() < (uint16_t)-1 );
  50. bsOut.WriteCasted<uint16_t>(keys.Size());
  51. for (uint16_t i=0; i < keys.Size(); i++)
  52. {
  53. keys[i].Serialize(true,&bsOut);
  54. }
  55. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, systemIdentifier, false);
  56. }
  57. bool CloudClient::Get(CloudQuery *keyQuery, RakNetGUID systemIdentifier)
  58. {
  59. RakNet::BitStream bsOut;
  60. bsOut.Write((MessageID)ID_CLOUD_GET_REQUEST);
  61. keyQuery->Serialize(true, &bsOut);
  62. bsOut.WriteCasted<uint16_t>(0); // Specific systems
  63. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, systemIdentifier, false);
  64. return true;
  65. }
  66. bool CloudClient::Get(CloudQuery *keyQuery, DataStructures::List<RakNetGUID> &specificSystems, RakNetGUID systemIdentifier)
  67. {
  68. RakNet::BitStream bsOut;
  69. bsOut.Write((MessageID)ID_CLOUD_GET_REQUEST);
  70. keyQuery->Serialize(true, &bsOut);
  71. bsOut.WriteCasted<uint16_t>(specificSystems.Size());
  72. RakAssert(specificSystems.Size() < (uint16_t)-1 );
  73. for (uint16_t i=0; i < specificSystems.Size(); i++)
  74. {
  75. bsOut.Write(specificSystems[i]);
  76. }
  77. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, systemIdentifier, false);
  78. return true;
  79. }
  80. bool CloudClient::Get(CloudQuery *keyQuery, DataStructures::List<CloudQueryRow*> &specificSystems, RakNetGUID systemIdentifier)
  81. {
  82. RakNet::BitStream bsOut;
  83. bsOut.Write((MessageID)ID_CLOUD_GET_REQUEST);
  84. keyQuery->Serialize(true, &bsOut);
  85. bsOut.WriteCasted<uint16_t>(specificSystems.Size());
  86. RakAssert(specificSystems.Size() < (uint16_t)-1 );
  87. for (uint16_t i=0; i < specificSystems.Size(); i++)
  88. {
  89. if (specificSystems[i]->clientGUID!=UNASSIGNED_RAKNET_GUID)
  90. {
  91. bsOut.Write(true);
  92. bsOut.Write(specificSystems[i]->clientGUID);
  93. }
  94. else
  95. {
  96. bsOut.Write(false);
  97. bsOut.Write(specificSystems[i]->clientSystemAddress);
  98. }
  99. }
  100. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, systemIdentifier, false);
  101. return true;
  102. }
  103. void CloudClient::Unsubscribe(DataStructures::List<CloudKey> &keys, RakNetGUID systemIdentifier)
  104. {
  105. RakNet::BitStream bsOut;
  106. bsOut.Write((MessageID)ID_CLOUD_UNSUBSCRIBE_REQUEST);
  107. RakAssert(keys.Size() < (uint16_t)-1 );
  108. bsOut.WriteCasted<uint16_t>(keys.Size());
  109. for (uint16_t i=0; i < keys.Size(); i++)
  110. {
  111. keys[i].Serialize(true,&bsOut);
  112. }
  113. bsOut.WriteCasted<uint16_t>(0);
  114. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, systemIdentifier, false);
  115. }
  116. void CloudClient::Unsubscribe(DataStructures::List<CloudKey> &keys, DataStructures::List<RakNetGUID> &specificSystems, RakNetGUID systemIdentifier)
  117. {
  118. RakNet::BitStream bsOut;
  119. bsOut.Write((MessageID)ID_CLOUD_UNSUBSCRIBE_REQUEST);
  120. RakAssert(keys.Size() < (uint16_t)-1 );
  121. bsOut.WriteCasted<uint16_t>(keys.Size());
  122. for (uint16_t i=0; i < keys.Size(); i++)
  123. {
  124. keys[i].Serialize(true,&bsOut);
  125. }
  126. bsOut.WriteCasted<uint16_t>(specificSystems.Size());
  127. RakAssert(specificSystems.Size() < (uint16_t)-1 );
  128. for (uint16_t i=0; i < specificSystems.Size(); i++)
  129. {
  130. bsOut.Write(specificSystems[i]);
  131. }
  132. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, systemIdentifier, false);
  133. }
  134. void CloudClient::Unsubscribe(DataStructures::List<CloudKey> &keys, DataStructures::List<CloudQueryRow*> &specificSystems, RakNetGUID systemIdentifier)
  135. {
  136. RakNet::BitStream bsOut;
  137. bsOut.Write((MessageID)ID_CLOUD_UNSUBSCRIBE_REQUEST);
  138. RakAssert(keys.Size() < (uint16_t)-1 );
  139. bsOut.WriteCasted<uint16_t>(keys.Size());
  140. for (uint16_t i=0; i < keys.Size(); i++)
  141. {
  142. keys[i].Serialize(true,&bsOut);
  143. }
  144. bsOut.WriteCasted<uint16_t>(specificSystems.Size());
  145. RakAssert(specificSystems.Size() < (uint16_t)-1 );
  146. for (uint16_t i=0; i < specificSystems.Size(); i++)
  147. {
  148. if (specificSystems[i]->clientGUID!=UNASSIGNED_RAKNET_GUID)
  149. {
  150. bsOut.Write(true);
  151. bsOut.Write(specificSystems[i]->clientGUID);
  152. }
  153. else
  154. {
  155. bsOut.Write(false);
  156. bsOut.Write(specificSystems[i]->clientSystemAddress);
  157. }
  158. }
  159. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, systemIdentifier, false);
  160. }
  161. PluginReceiveResult CloudClient::OnReceive(Packet *packet)
  162. {
  163. (void) packet;
  164. return RR_CONTINUE_PROCESSING;
  165. }
  166. void CloudClient::OnGetReponse(Packet *packet, CloudClientCallback *_callback, CloudAllocator *_allocator)
  167. {
  168. if (_callback==0)
  169. _callback=callback;
  170. if (_allocator==0)
  171. _allocator=allocator;
  172. CloudQueryResult cloudQueryResult;
  173. RakNet::BitStream bsIn(packet->data, packet->length, false);
  174. bsIn.IgnoreBytes(sizeof(MessageID));
  175. cloudQueryResult.Serialize(false,&bsIn,_allocator);
  176. bool deallocateRowsAfterReturn=true;
  177. _callback->OnGet(&cloudQueryResult, &deallocateRowsAfterReturn);
  178. if (deallocateRowsAfterReturn)
  179. {
  180. unsigned int i;
  181. for (i=0; i < cloudQueryResult.rowsReturned.Size(); i++)
  182. {
  183. _allocator->DeallocateRowData(cloudQueryResult.rowsReturned[i]->data);
  184. _allocator->DeallocateCloudQueryRow(cloudQueryResult.rowsReturned[i]);
  185. }
  186. }
  187. }
  188. void CloudClient::OnGetReponse(CloudQueryResult *cloudQueryResult, Packet *packet, CloudAllocator *_allocator)
  189. {
  190. if (_allocator==0)
  191. _allocator=allocator;
  192. RakNet::BitStream bsIn(packet->data, packet->length, false);
  193. bsIn.IgnoreBytes(sizeof(MessageID));
  194. cloudQueryResult->Serialize(false,&bsIn,_allocator);
  195. }
  196. void CloudClient::OnSubscriptionNotification(Packet *packet, CloudClientCallback *_callback, CloudAllocator *_allocator)
  197. {
  198. if (_callback==0)
  199. _callback=callback;
  200. if (_allocator==0)
  201. _allocator=allocator;
  202. bool wasUpdated=false;
  203. CloudQueryRow row;
  204. RakNet::BitStream bsIn(packet->data, packet->length, false);
  205. bsIn.IgnoreBytes(sizeof(MessageID));
  206. bsIn.Read(wasUpdated);
  207. row.Serialize(false,&bsIn,_allocator);
  208. bool deallocateRowAfterReturn=true;
  209. _callback->OnSubscriptionNotification(&row, wasUpdated, &deallocateRowAfterReturn);
  210. if (deallocateRowAfterReturn)
  211. {
  212. _allocator->DeallocateRowData(row.data);
  213. }
  214. }
  215. void CloudClient::OnSubscriptionNotification(bool *wasUpdated, CloudQueryRow *row, Packet *packet, CloudAllocator *_allocator)
  216. {
  217. if (_allocator==0)
  218. _allocator=allocator;
  219. RakNet::BitStream bsIn(packet->data, packet->length, false);
  220. bsIn.IgnoreBytes(sizeof(MessageID));
  221. bool b=false;
  222. bsIn.Read(b);
  223. *wasUpdated=b;
  224. row->Serialize(false,&bsIn,_allocator);
  225. }
  226. void CloudClient::DeallocateWithDefaultAllocator(CloudQueryResult *cloudQueryResult)
  227. {
  228. unsigned int i;
  229. for (i=0; i < cloudQueryResult->rowsReturned.Size(); i++)
  230. {
  231. allocator->DeallocateRowData(cloudQueryResult->rowsReturned[i]->data);
  232. allocator->DeallocateCloudQueryRow(cloudQueryResult->rowsReturned[i]);
  233. }
  234. cloudQueryResult->rowsReturned.Clear(false, _FILE_AND_LINE_);
  235. cloudQueryResult->resultKeyIndices.Clear(false, _FILE_AND_LINE_);
  236. cloudQueryResult->cloudQuery.keys.Clear(false, _FILE_AND_LINE_);
  237. }
  238. void CloudClient::DeallocateWithDefaultAllocator(CloudQueryRow *row)
  239. {
  240. allocator->DeallocateRowData(row->data);
  241. }
  242. #endif
粤ICP备19079148号