SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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