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