SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CloudCommon.cpp
Go to the documentation of this file.
1 /*
2  * Original work: 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  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
17 #if _RAKNET_SUPPORT_CloudClient==1 || _RAKNET_SUPPORT_CloudServer==1
18 
19 #include "slikenet/CloudCommon.h"
20 #include "slikenet/BitStream.h"
21 
22 using namespace SLNet;
23 
24 int SLNet::CloudKeyComp(const CloudKey &key, const CloudKey &data)
25 {
26  if (key.primaryKey < data.primaryKey)
27  return -1;
28  if (key.primaryKey > data.primaryKey)
29  return 1;
30  if (key.secondaryKey < data.secondaryKey)
31  return -1;
32  if (key.secondaryKey > data.secondaryKey)
33  return 1;
34  return 0;
35 }
36 
38 {
39  return SLNet::OP_NEW<CloudQueryRow>(_FILE_AND_LINE_);
40 }
42 {
44 }
45 unsigned char *CloudAllocator::AllocateRowData(uint32_t bytesNeededForData)
46 {
47  return (unsigned char*) rakMalloc_Ex(bytesNeededForData,_FILE_AND_LINE_);
48 }
49 void CloudAllocator::DeallocateRowData(void *data)
50 {
52 }
53 void CloudKey::Serialize(bool writeToBitstream, BitStream *bitStream)
54 {
55  bitStream->Serialize(writeToBitstream, primaryKey);
56  bitStream->Serialize(writeToBitstream, secondaryKey);
57 }
58 void CloudQuery::Serialize(bool writeToBitstream, BitStream *bitStream)
59 {
60  bool startingRowIndexIsZero=0;
61  bool maxRowsToReturnIsZero=0;
62  startingRowIndexIsZero=startingRowIndex==0;
63  maxRowsToReturnIsZero=maxRowsToReturn==0;
64  bitStream->Serialize(writeToBitstream,startingRowIndexIsZero);
65  bitStream->Serialize(writeToBitstream,maxRowsToReturnIsZero);
66  bitStream->Serialize(writeToBitstream,subscribeToResults);
67  if (startingRowIndexIsZero==false)
68  bitStream->Serialize(writeToBitstream,startingRowIndex);
69  if (maxRowsToReturnIsZero==false)
70  bitStream->Serialize(writeToBitstream,maxRowsToReturn);
71  RakAssert(keys.Size()<(uint16_t)-1);
72  uint16_t numKeys = (uint16_t) keys.Size();
73  bitStream->Serialize(writeToBitstream,numKeys);
74  if (writeToBitstream)
75  {
76  for (uint16_t i=0; i < numKeys; i++)
77  {
78  keys[i].Serialize(true,bitStream);
79  }
80  }
81  else
82  {
83  CloudKey cmdk;
84  for (uint16_t i=0; i < numKeys; i++)
85  {
86  cmdk.Serialize(false,bitStream);
87  keys.Push(cmdk, _FILE_AND_LINE_);
88  }
89  }
90 }
91 void CloudQueryRow::Serialize(bool writeToBitstream, BitStream *bitStream, CloudAllocator *allocator)
92 {
93  key.Serialize(writeToBitstream,bitStream);
94  bitStream->Serialize(writeToBitstream,serverSystemAddress);
95  bitStream->Serialize(writeToBitstream,clientSystemAddress);
96  bitStream->Serialize(writeToBitstream,serverGUID);
97  bitStream->Serialize(writeToBitstream,clientGUID);
98  bitStream->Serialize(writeToBitstream,length);
99  if (writeToBitstream)
100  {
101  bitStream->WriteAlignedBytes((const unsigned char*) data,length);
102  }
103  else
104  {
105  if (length>0)
106  {
107  data = allocator->AllocateRowData(length);
108  if (data)
109  {
110  bitStream->ReadAlignedBytes((unsigned char *) data,length);
111  }
112  else
113  {
115  }
116  }
117  else
118  data=0;
119  }
120 }
121 void CloudQueryResult::SerializeHeader(bool writeToBitstream, BitStream *bitStream)
122 {
123  cloudQuery.Serialize(writeToBitstream,bitStream);
124  bitStream->Serialize(writeToBitstream,subscribeToResults);
125 }
126 void CloudQueryResult::SerializeNumRows(bool writeToBitstream, uint32_t &numRows, BitStream *bitStream)
127 {
128  bitStream->Serialize(writeToBitstream,numRows);
129 }
130 void CloudQueryResult::SerializeCloudQueryRows(bool writeToBitstream, uint32_t &numRows, BitStream *bitStream, CloudAllocator *allocator)
131 {
132  if (writeToBitstream)
133  {
134  for (uint16_t i=0; i < numRows; i++)
135  {
136  rowsReturned[i]->Serialize(true,bitStream, allocator);
137  }
138  }
139  else
140  {
141  CloudQueryRow* cmdr;
142  for (uint16_t i=0; i < numRows; i++)
143  {
144  cmdr = allocator->AllocateCloudQueryRow();
145  if (cmdr)
146  {
147  cmdr->Serialize(false,bitStream,allocator);
148  if (cmdr->data==0 && cmdr->length>0)
149  {
150  allocator->DeallocateCloudQueryRow(cmdr);
152  numRows=i;
153  return;
154  }
156  }
157  else
158  {
160  numRows=i;
161  return;
162  }
163  }
164  }
165 }
166 void CloudQueryResult::Serialize(bool writeToBitstream, BitStream *bitStream, CloudAllocator *allocator)
167 {
168  SerializeHeader(writeToBitstream, bitStream);
169  uint32_t numRows = (uint32_t) rowsReturned.Size();
170  SerializeNumRows(writeToBitstream, numRows, bitStream);
171  SerializeCloudQueryRows(writeToBitstream, numRows, bitStream, allocator);
172 }
173 
174 #endif // #if _RAKNET_SUPPORT_CloudMemoryClient==1 || _RAKNET_SUPPORT_CloudMemoryServer==1