CloudCommon.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 || _RAKNET_SUPPORT_CloudServer==1
  12. #include "CloudCommon.h"
  13. #include "BitStream.h"
  14. using namespace RakNet;
  15. int RakNet::CloudKeyComp(const CloudKey &key, const CloudKey &data)
  16. {
  17. if (key.primaryKey < data.primaryKey)
  18. return -1;
  19. if (key.primaryKey > data.primaryKey)
  20. return 1;
  21. if (key.secondaryKey < data.secondaryKey)
  22. return -1;
  23. if (key.secondaryKey > data.secondaryKey)
  24. return 1;
  25. return 0;
  26. }
  27. CloudQueryRow* CloudAllocator::AllocateCloudQueryRow(void)
  28. {
  29. return RakNet::OP_NEW<CloudQueryRow>(_FILE_AND_LINE_);
  30. }
  31. void CloudAllocator::DeallocateCloudQueryRow(CloudQueryRow *row)
  32. {
  33. RakNet::OP_DELETE(row,_FILE_AND_LINE_);
  34. }
  35. unsigned char *CloudAllocator::AllocateRowData(uint32_t bytesNeededForData)
  36. {
  37. return (unsigned char*) rakMalloc_Ex(bytesNeededForData,_FILE_AND_LINE_);
  38. }
  39. void CloudAllocator::DeallocateRowData(void *data)
  40. {
  41. rakFree_Ex(data, _FILE_AND_LINE_);
  42. }
  43. void CloudKey::Serialize(bool writeToBitstream, BitStream *bitStream)
  44. {
  45. bitStream->Serialize(writeToBitstream, primaryKey);
  46. bitStream->Serialize(writeToBitstream, secondaryKey);
  47. }
  48. void CloudQuery::Serialize(bool writeToBitstream, BitStream *bitStream)
  49. {
  50. bool startingRowIndexIsZero=0;
  51. bool maxRowsToReturnIsZero=0;
  52. startingRowIndexIsZero=startingRowIndex==0;
  53. maxRowsToReturnIsZero=maxRowsToReturn==0;
  54. bitStream->Serialize(writeToBitstream,startingRowIndexIsZero);
  55. bitStream->Serialize(writeToBitstream,maxRowsToReturnIsZero);
  56. bitStream->Serialize(writeToBitstream,subscribeToResults);
  57. if (startingRowIndexIsZero==false)
  58. bitStream->Serialize(writeToBitstream,startingRowIndex);
  59. if (maxRowsToReturnIsZero==false)
  60. bitStream->Serialize(writeToBitstream,maxRowsToReturn);
  61. RakAssert(keys.Size()<(uint16_t)-1);
  62. uint16_t numKeys = (uint16_t) keys.Size();
  63. bitStream->Serialize(writeToBitstream,numKeys);
  64. if (writeToBitstream)
  65. {
  66. for (uint16_t i=0; i < numKeys; i++)
  67. {
  68. keys[i].Serialize(true,bitStream);
  69. }
  70. }
  71. else
  72. {
  73. CloudKey cmdk;
  74. for (uint16_t i=0; i < numKeys; i++)
  75. {
  76. cmdk.Serialize(false,bitStream);
  77. keys.Push(cmdk, _FILE_AND_LINE_);
  78. }
  79. }
  80. }
  81. void CloudQueryRow::Serialize(bool writeToBitstream, BitStream *bitStream, CloudAllocator *allocator)
  82. {
  83. key.Serialize(writeToBitstream,bitStream);
  84. bitStream->Serialize(writeToBitstream,serverSystemAddress);
  85. bitStream->Serialize(writeToBitstream,clientSystemAddress);
  86. bitStream->Serialize(writeToBitstream,serverGUID);
  87. bitStream->Serialize(writeToBitstream,clientGUID);
  88. bitStream->Serialize(writeToBitstream,length);
  89. if (writeToBitstream)
  90. {
  91. bitStream->WriteAlignedBytes((const unsigned char*) data,length);
  92. }
  93. else
  94. {
  95. if (length>0)
  96. {
  97. data = allocator->AllocateRowData(length);
  98. if (data)
  99. {
  100. bitStream->ReadAlignedBytes((unsigned char *) data,length);
  101. }
  102. else
  103. {
  104. notifyOutOfMemory(_FILE_AND_LINE_);
  105. }
  106. }
  107. else
  108. data=0;
  109. }
  110. }
  111. void CloudQueryResult::SerializeHeader(bool writeToBitstream, BitStream *bitStream)
  112. {
  113. cloudQuery.Serialize(writeToBitstream,bitStream);
  114. bitStream->Serialize(writeToBitstream,subscribeToResults);
  115. }
  116. void CloudQueryResult::SerializeNumRows(bool writeToBitstream, uint32_t &numRows, BitStream *bitStream)
  117. {
  118. bitStream->Serialize(writeToBitstream,numRows);
  119. }
  120. void CloudQueryResult::SerializeCloudQueryRows(bool writeToBitstream, uint32_t &numRows, BitStream *bitStream, CloudAllocator *allocator)
  121. {
  122. if (writeToBitstream)
  123. {
  124. for (uint16_t i=0; i < numRows; i++)
  125. {
  126. rowsReturned[i]->Serialize(true,bitStream, allocator);
  127. }
  128. }
  129. else
  130. {
  131. CloudQueryRow* cmdr;
  132. for (uint16_t i=0; i < numRows; i++)
  133. {
  134. cmdr = allocator->AllocateCloudQueryRow();
  135. if (cmdr)
  136. {
  137. cmdr->Serialize(false,bitStream,allocator);
  138. if (cmdr->data==0 && cmdr->length>0)
  139. {
  140. allocator->DeallocateCloudQueryRow(cmdr);
  141. notifyOutOfMemory(_FILE_AND_LINE_);
  142. numRows=i;
  143. return;
  144. }
  145. rowsReturned.Push(cmdr, _FILE_AND_LINE_);
  146. }
  147. else
  148. {
  149. notifyOutOfMemory(_FILE_AND_LINE_);
  150. numRows=i;
  151. return;
  152. }
  153. }
  154. }
  155. }
  156. void CloudQueryResult::Serialize(bool writeToBitstream, BitStream *bitStream, CloudAllocator *allocator)
  157. {
  158. SerializeHeader(writeToBitstream, bitStream);
  159. uint32_t numRows = (uint32_t) rowsReturned.Size();
  160. SerializeNumRows(writeToBitstream, numRows, bitStream);
  161. SerializeCloudQueryRows(writeToBitstream, numRows, bitStream, allocator);
  162. }
  163. #endif // #if _RAKNET_SUPPORT_CloudMemoryClient==1 || _RAKNET_SUPPORT_CloudMemoryServer==1
粤ICP备19079148号