forked from facebookarchive/RakNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudServer.cpp
More file actions
1685 lines (1526 loc) · 58.5 KB
/
Copy pathCloudServer.cpp
File metadata and controls
1685 lines (1526 loc) · 58.5 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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_CloudServer==1
#include "CloudServer.h"
#include "GetTime.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "RakPeerInterface.h"
enum ServerToServerCommands
{
STSC_PROCESS_GET_REQUEST,
STSC_PROCESS_GET_RESPONSE,
STSC_ADD_UPLOADED_AND_SUBSCRIBED_KEYS,
STSC_ADD_UPLOADED_KEY,
STSC_ADD_SUBSCRIBED_KEY,
STSC_REMOVE_UPLOADED_KEY,
STSC_REMOVE_SUBSCRIBED_KEY,
STSC_DATA_CHANGED,
};
using namespace RakNet;
int CloudServer::RemoteServerComp(const RakNetGUID &key, RemoteServer* const &data )
{
if (key < data->serverAddress)
return -1;
if (key > data->serverAddress)
return 1;
return 0;
}
int CloudServer::KeySubscriberIDComp(const CloudKey &key, KeySubscriberID * const &data )
{
if (key.primaryKey < data->key.primaryKey)
return -1;
if (key.primaryKey > data->key.primaryKey)
return 1;
if (key.secondaryKey < data->key.secondaryKey)
return -1;
if (key.secondaryKey > data->key.secondaryKey)
return 1;
return 0;
}
int CloudServer::KeyDataPtrComp( const RakNetGUID &key, CloudData* const &data )
{
if (key < data->clientGUID)
return -1;
if (key > data->clientGUID)
return 1;
return 0;
}
int CloudServer::KeyDataListComp( const CloudKey &key, CloudDataList * const &data )
{
if (key.primaryKey < data->key.primaryKey)
return -1;
if (key.primaryKey > data->key.primaryKey)
return 1;
if (key.secondaryKey < data->key.secondaryKey)
return -1;
if (key.secondaryKey > data->key.secondaryKey)
return 1;
return 0;
}
int CloudServer::BufferedGetResponseFromServerComp(const RakNetGUID &key, CloudServer::BufferedGetResponseFromServer* const &data )
{
if (key < data->serverAddress)
return -1;
if (key > data->serverAddress)
return 1;
return 0;
}
int CloudServer::GetRequestComp(const uint32_t &key, CloudServer::GetRequest* const &data )
{
if (key < data->requestId)
return -1;
if (key > data->requestId)
return -1;
return 0;
}
void CloudServer::CloudQueryWithAddresses::Serialize(bool writeToBitstream, BitStream *bitStream)
{
cloudQuery.Serialize(writeToBitstream, bitStream);
if (writeToBitstream)
{
bitStream->WriteCasted<uint16_t>(specificSystems.Size());
RakAssert(specificSystems.Size() < (uint16_t)-1 );
for (uint16_t i=0; i < specificSystems.Size(); i++)
{
bitStream->Write(specificSystems[i]);
}
}
else
{
uint16_t specificSystemsCount;
RakNetGUID addressOrGuid;
bitStream->Read(specificSystemsCount);
for (uint16_t i=0; i < specificSystemsCount; i++)
{
bitStream->Read(addressOrGuid);
specificSystems.Push(addressOrGuid, _FILE_AND_LINE_);
}
}
}
bool CloudServer::GetRequest::AllRemoteServersHaveResponded(void) const
{
unsigned int i;
for (i=0; i < remoteServerResponses.Size(); i++)
if (remoteServerResponses[i]->gotResult==false)
return false;
return true;
}
void CloudServer::GetRequest::Clear(CloudAllocator *allocator)
{
unsigned int i;
for (i=0; i < remoteServerResponses.Size(); i++)
{
remoteServerResponses[i]->Clear(allocator);
RakNet::OP_DELETE(remoteServerResponses[i], _FILE_AND_LINE_);
}
remoteServerResponses.Clear(false, _FILE_AND_LINE_);
}
void CloudServer::BufferedGetResponseFromServer::Clear(CloudAllocator *allocator)
{
unsigned int i;
for (i=0; i < queryResult.rowsReturned.Size(); i++)
{
allocator->DeallocateRowData(queryResult.rowsReturned[i]->data);
allocator->DeallocateCloudQueryRow(queryResult.rowsReturned[i]);
}
queryResult.rowsReturned.Clear(false, _FILE_AND_LINE_);
}
CloudServer::CloudServer()
{
maxUploadBytesPerClient=0;
maxBytesPerDowload=0;
nextGetRequestId=0;
nextGetRequestsCheck=0;
}
CloudServer::~CloudServer()
{
Clear();
}
void CloudServer::SetMaxUploadBytesPerClient(uint64_t bytes)
{
maxUploadBytesPerClient=bytes;
}
void CloudServer::SetMaxBytesPerDownload(uint64_t bytes)
{
maxBytesPerDowload=bytes;
}
void CloudServer::Update(void)
{
// Timeout getRequests
RakNet::Time time = RakNet::Time();
if (time > nextGetRequestsCheck)
{
nextGetRequestsCheck=time+1000;
unsigned int i=0;
while (i < getRequests.Size())
{
if (time - getRequests[i]->requestStartTime > 3000)
{
// Remote server is not responding, just send back data with whoever did respond
ProcessAndTransmitGetRequest(getRequests[i]);
getRequests[i]->Clear(this);
RakNet::OP_DELETE(getRequests[i],_FILE_AND_LINE_);
getRequests.RemoveAtIndex(i);
}
else
{
i++;
}
}
}
}
PluginReceiveResult CloudServer::OnReceive(Packet *packet)
{
switch (packet->data[0])
{
case ID_CLOUD_POST_REQUEST:
OnPostRequest(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_CLOUD_RELEASE_REQUEST:
OnReleaseRequest(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_CLOUD_GET_REQUEST:
OnGetRequest(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_CLOUD_UNSUBSCRIBE_REQUEST:
OnUnsubscribeRequest(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_CLOUD_SERVER_TO_SERVER_COMMAND:
if (packet->length>1)
{
switch (packet->data[1])
{
case STSC_PROCESS_GET_REQUEST:
OnServerToServerGetRequest(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case STSC_PROCESS_GET_RESPONSE:
OnServerToServerGetResponse(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case STSC_ADD_UPLOADED_AND_SUBSCRIBED_KEYS:
OnSendUploadedAndSubscribedKeysToServer(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case STSC_ADD_UPLOADED_KEY:
OnSendUploadedKeyToServers(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case STSC_ADD_SUBSCRIBED_KEY:
OnSendSubscribedKeyToServers(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case STSC_REMOVE_UPLOADED_KEY:
OnRemoveUploadedKeyFromServers(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case STSC_REMOVE_SUBSCRIBED_KEY:
OnRemoveSubscribedKeyFromServers(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case STSC_DATA_CHANGED:
OnServerDataChanged(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
}
}
return RR_STOP_PROCESSING_AND_DEALLOCATE;
}
return RR_CONTINUE_PROCESSING;
}
void CloudServer::OnPostRequest(Packet *packet)
{
RakNet::BitStream bsIn(packet->data, packet->length, false);
bsIn.IgnoreBytes(sizeof(MessageID));
CloudKey key;
key.Serialize(false,&bsIn);
uint32_t dataLengthBytes;
bsIn.Read(dataLengthBytes);
if (maxUploadBytesPerClient>0 && dataLengthBytes>maxUploadBytesPerClient)
return; // Exceeded max upload bytes
bsIn.AlignReadToByteBoundary();
for (unsigned int filterIndex=0; filterIndex < queryFilters.Size(); filterIndex++)
{
if (queryFilters[filterIndex]->OnPostRequest(packet->guid, packet->systemAddress, key, dataLengthBytes, (const char*) bsIn.GetData()+BITS_TO_BYTES(bsIn.GetReadOffset()))==false)
return;
}
unsigned char *data;
if (dataLengthBytes>CLOUD_SERVER_DATA_STACK_SIZE)
{
data = (unsigned char *) rakMalloc_Ex(dataLengthBytes,_FILE_AND_LINE_);
if (data==0)
{
notifyOutOfMemory(_FILE_AND_LINE_);
return;
}
bsIn.ReadAlignedBytes(data,dataLengthBytes);
}
else
data=0;
// Add this system to remoteSystems if they aren't there already
DataStructures::HashIndex remoteSystemsHashIndex = remoteSystems.GetIndexOf(packet->guid);
RemoteCloudClient *remoteCloudClient;
if (remoteSystemsHashIndex.IsInvalid())
{
remoteCloudClient = RakNet::OP_NEW<RemoteCloudClient>(_FILE_AND_LINE_);
remoteCloudClient->uploadedKeys.Insert(key,key,true,_FILE_AND_LINE_);
remoteCloudClient->uploadedBytes=0;
remoteSystems.Push(packet->guid, remoteCloudClient, _FILE_AND_LINE_);
}
else
{
remoteCloudClient = remoteSystems.ItemAtIndex(remoteSystemsHashIndex);
bool objectExists;
// Add to RemoteCloudClient::uploadedKeys if it isn't there already
unsigned int uploadedKeysIndex = remoteCloudClient->uploadedKeys.GetIndexFromKey(key,&objectExists);
if (objectExists==false)
{
remoteCloudClient->uploadedKeys.InsertAtIndex(key, uploadedKeysIndex, _FILE_AND_LINE_);
}
}
bool cloudDataAlreadyUploaded;
unsigned int dataRepositoryIndex;
bool dataRepositoryExists;
CloudDataList* cloudDataList = GetOrAllocateCloudDataList(key, &dataRepositoryExists, dataRepositoryIndex);
if (dataRepositoryExists==false)
{
cloudDataList->uploaderCount=1;
cloudDataAlreadyUploaded=false;
}
else
{
cloudDataAlreadyUploaded=cloudDataList->uploaderCount>0;
cloudDataList->uploaderCount++;
}
CloudData *cloudData;
bool keyDataListExists;
unsigned int keyDataListIndex = cloudDataList->keyData.GetIndexFromKey(packet->guid, &keyDataListExists);
if (keyDataListExists==false)
{
if (maxUploadBytesPerClient>0 && remoteCloudClient->uploadedBytes+dataLengthBytes>maxUploadBytesPerClient)
{
// Undo prior insertion of cloudDataList into cloudData if needed
if (keyDataListExists==false)
{
RakNet::OP_DELETE(cloudDataList,_FILE_AND_LINE_);
dataRepository.RemoveAtIndex(dataRepositoryIndex);
}
if (remoteCloudClient->IsUnused())
{
RakNet::OP_DELETE(remoteCloudClient, _FILE_AND_LINE_);
remoteSystems.Remove(packet->guid, _FILE_AND_LINE_);
}
if (dataLengthBytes>CLOUD_SERVER_DATA_STACK_SIZE)
rakFree_Ex(data, _FILE_AND_LINE_);
return;
}
cloudData = RakNet::OP_NEW<CloudData>(_FILE_AND_LINE_);
cloudData->dataLengthBytes=dataLengthBytes;
cloudData->isUploaded=true;
if (forceAddress!=UNASSIGNED_SYSTEM_ADDRESS)
{
cloudData->serverSystemAddress=forceAddress;
cloudData->serverSystemAddress.SetPortHostOrder(rakPeerInterface->GetExternalID(packet->systemAddress).GetPort());
}
else
{
cloudData->serverSystemAddress=rakPeerInterface->GetExternalID(packet->systemAddress);
if (cloudData->serverSystemAddress.IsLoopback())
cloudData->serverSystemAddress.FromString(rakPeerInterface->GetLocalIP(0));
}
if (cloudData->serverSystemAddress.GetPort()==0)
{
// Fix localhost port
cloudData->serverSystemAddress.SetPortHostOrder(rakPeerInterface->GetSocket(UNASSIGNED_SYSTEM_ADDRESS)->GetBoundAddress().GetPort());
}
cloudData->clientSystemAddress=packet->systemAddress;
cloudData->serverGUID=rakPeerInterface->GetMyGUID();
cloudData->clientGUID=packet->guid;
cloudDataList->keyData.Insert(packet->guid,cloudData,true,_FILE_AND_LINE_);
}
else
{
cloudData = cloudDataList->keyData[keyDataListIndex];
if (cloudDataAlreadyUploaded==false)
{
if (forceAddress!=UNASSIGNED_SYSTEM_ADDRESS)
{
cloudData->serverSystemAddress=forceAddress;
cloudData->serverSystemAddress.SetPortHostOrder(rakPeerInterface->GetExternalID(packet->systemAddress).GetPort());
}
else
{
cloudData->serverSystemAddress=rakPeerInterface->GetExternalID(packet->systemAddress);
}
if (cloudData->serverSystemAddress.GetPort()==0)
{
// Fix localhost port
cloudData->serverSystemAddress.SetPortHostOrder(rakPeerInterface->GetSocket(UNASSIGNED_SYSTEM_ADDRESS)->GetBoundAddress().GetPort());
}
cloudData->clientSystemAddress=packet->systemAddress;
}
if (maxUploadBytesPerClient>0 && remoteCloudClient->uploadedBytes-cloudData->dataLengthBytes+dataLengthBytes>maxUploadBytesPerClient)
{
// Undo prior insertion of cloudDataList into cloudData if needed
if (dataRepositoryExists==false)
{
RakNet::OP_DELETE(cloudDataList,_FILE_AND_LINE_);
dataRepository.RemoveAtIndex(dataRepositoryIndex);
}
return;
}
else
{
// Subtract already used bytes we are overwriting
remoteCloudClient->uploadedBytes-=cloudData->dataLengthBytes;
}
if (cloudData->allocatedData!=0)
rakFree_Ex(cloudData->allocatedData,_FILE_AND_LINE_);
}
if (dataLengthBytes>CLOUD_SERVER_DATA_STACK_SIZE)
{
// Data already allocated
cloudData->allocatedData=data;
cloudData->dataPtr=data;
}
else
{
// Read to stack
if (dataLengthBytes>0)
bsIn.ReadAlignedBytes(cloudData->stackData,dataLengthBytes);
cloudData->allocatedData=0;
cloudData->dataPtr=cloudData->stackData;
}
// Update how many bytes were written for this data
cloudData->dataLengthBytes=dataLengthBytes;
remoteCloudClient->uploadedBytes+=dataLengthBytes;
if (cloudDataAlreadyUploaded==false)
{
// New data field
SendUploadedKeyToServers(cloudDataList->key);
}
// Existing data field changed
NotifyClientSubscribersOfDataChange(cloudData, cloudDataList->key, cloudData->specificSubscribers, true );
NotifyClientSubscribersOfDataChange(cloudData, cloudDataList->key, cloudDataList->nonSpecificSubscribers, true );
// Send update to all remote servers that subscribed to this key
NotifyServerSubscribersOfDataChange(cloudData, cloudDataList->key, true);
// I could have also subscribed to a key not yet updated locally
// This means I have to go through every RemoteClient that wants this key
// Seems like cloudData->specificSubscribers is unnecessary in that case
}
void CloudServer::OnReleaseRequest(Packet *packet)
{
RakNet::BitStream bsIn(packet->data, packet->length, false);
bsIn.IgnoreBytes(sizeof(MessageID));
uint16_t keyCount;
bsIn.Read(keyCount);
if (keyCount==0)
return;
DataStructures::HashIndex remoteSystemIndex = remoteSystems.GetIndexOf(packet->guid);
if (remoteSystemIndex.IsInvalid()==true)
return;
RemoteCloudClient* remoteCloudClient = remoteSystems.ItemAtIndex(remoteSystemIndex);
CloudKey key;
// Read all in a list first so I can run filter on it
DataStructures::List<CloudKey> cloudKeys;
for (uint16_t keyCountIndex=0; keyCountIndex < keyCount; keyCountIndex++)
{
key.Serialize(false, &bsIn);
cloudKeys.Push(key, _FILE_AND_LINE_);
}
for (unsigned int filterIndex=0; filterIndex < queryFilters.Size(); filterIndex++)
{
if (queryFilters[filterIndex]->OnReleaseRequest(packet->guid, packet->systemAddress, cloudKeys)==false)
return;
}
for (uint16_t keyCountIndex=0; keyCountIndex < keyCount; keyCountIndex++)
{
// Serialize in list above so I can run the filter on it
// key.Serialize(false, &bsIn);
key=cloudKeys[keyCountIndex];
// Remove remote systems uploaded keys
bool objectExists;
unsigned int uploadedKeysIndex = remoteCloudClient->uploadedKeys.GetIndexFromKey(key,&objectExists);
if (objectExists)
{
bool dataRepositoryExists;
unsigned int dataRepositoryIndex = dataRepository.GetIndexFromKey(key, &dataRepositoryExists);
CloudDataList* cloudDataList = dataRepository[dataRepositoryIndex];
RakAssert(cloudDataList);
CloudData *cloudData;
bool keyDataListExists;
unsigned int keyDataListIndex = cloudDataList->keyData.GetIndexFromKey(packet->guid, &keyDataListExists);
cloudData = cloudDataList->keyData[keyDataListIndex];
remoteCloudClient->uploadedKeys.RemoveAtIndex(uploadedKeysIndex);
remoteCloudClient->uploadedBytes-=cloudData->dataLengthBytes;
cloudDataList->uploaderCount--;
// Broadcast destruction of this key to subscribers
NotifyClientSubscribersOfDataChange(cloudData, cloudDataList->key, cloudData->specificSubscribers, false );
NotifyClientSubscribersOfDataChange(cloudData, cloudDataList->key, cloudDataList->nonSpecificSubscribers, false );
NotifyServerSubscribersOfDataChange(cloudData, cloudDataList->key, false );
cloudData->Clear();
if (cloudData->IsUnused())
{
RakNet::OP_DELETE(cloudData, _FILE_AND_LINE_);
cloudDataList->keyData.RemoveAtIndex(keyDataListIndex);
if (cloudDataList->IsNotUploaded())
{
// Tell other servers that this key is no longer uploaded, so they do not request it from us
RemoveUploadedKeyFromServers(cloudDataList->key);
}
if (cloudDataList->IsUnused())
{
RakNet::OP_DELETE(cloudDataList, _FILE_AND_LINE_);
dataRepository.RemoveAtIndex(dataRepositoryIndex);
}
}
if (remoteCloudClient->IsUnused())
{
RakNet::OP_DELETE(remoteCloudClient, _FILE_AND_LINE_);
remoteSystems.RemoveAtIndex(remoteSystemIndex, _FILE_AND_LINE_);
break;
}
}
}
}
void CloudServer::OnGetRequest(Packet *packet)
{
RakNet::BitStream bsIn(packet->data, packet->length, false);
bsIn.IgnoreBytes(sizeof(MessageID));
uint16_t specificSystemsCount;
CloudKey cloudKey;
// Create a new GetRequest
GetRequest *getRequest;
getRequest = RakNet::OP_NEW<GetRequest>(_FILE_AND_LINE_);
getRequest->cloudQueryWithAddresses.cloudQuery.Serialize(false, &bsIn);
getRequest->requestingClient=packet->guid;
RakNetGUID addressOrGuid;
bsIn.Read(specificSystemsCount);
for (uint16_t i=0; i < specificSystemsCount; i++)
{
bsIn.Read(addressOrGuid);
getRequest->cloudQueryWithAddresses.specificSystems.Push(addressOrGuid, _FILE_AND_LINE_);
}
if (getRequest->cloudQueryWithAddresses.cloudQuery.keys.Size()==0)
{
RakNet::OP_DELETE(getRequest, _FILE_AND_LINE_);
return;
}
for (unsigned int filterIndex=0; filterIndex < queryFilters.Size(); filterIndex++)
{
if (queryFilters[filterIndex]->OnGetRequest(packet->guid, packet->systemAddress, getRequest->cloudQueryWithAddresses.cloudQuery, getRequest->cloudQueryWithAddresses.specificSystems )==false)
return;
}
getRequest->requestStartTime=RakNet::GetTime();
getRequest->requestId=nextGetRequestId++;
// Send request to servers that have this data
DataStructures::List<RemoteServer*> remoteServersWithData;
GetServersWithUploadedKeys(getRequest->cloudQueryWithAddresses.cloudQuery.keys, remoteServersWithData);
if (remoteServersWithData.Size()==0)
{
ProcessAndTransmitGetRequest(getRequest);
}
else
{
RakNet::BitStream bsOut;
bsOut.Write((MessageID)ID_CLOUD_SERVER_TO_SERVER_COMMAND);
bsOut.Write((MessageID)STSC_PROCESS_GET_REQUEST);
getRequest->cloudQueryWithAddresses.Serialize(true, &bsOut);
bsOut.Write(getRequest->requestId);
for (unsigned int remoteServerIndex=0; remoteServerIndex < remoteServersWithData.Size(); remoteServerIndex++)
{
BufferedGetResponseFromServer* bufferedGetResponseFromServer = RakNet::OP_NEW<BufferedGetResponseFromServer>(_FILE_AND_LINE_);
bufferedGetResponseFromServer->serverAddress=remoteServersWithData[remoteServerIndex]->serverAddress;
bufferedGetResponseFromServer->gotResult=false;
getRequest->remoteServerResponses.Insert(remoteServersWithData[remoteServerIndex]->serverAddress, bufferedGetResponseFromServer, true, _FILE_AND_LINE_);
SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, remoteServersWithData[remoteServerIndex]->serverAddress, false);
}
// Record that this system made this request
getRequests.Insert(getRequest->requestId, getRequest, true, _FILE_AND_LINE_);
}
if (getRequest->cloudQueryWithAddresses.cloudQuery.subscribeToResults)
{
// Add to key subscription list for the client, which contains a keyId / specificUploaderList pair
DataStructures::HashIndex remoteSystemsHashIndex = remoteSystems.GetIndexOf(packet->guid);
RemoteCloudClient *remoteCloudClient;
if (remoteSystemsHashIndex.IsInvalid())
{
remoteCloudClient = RakNet::OP_NEW<RemoteCloudClient>(_FILE_AND_LINE_);
remoteCloudClient->uploadedBytes=0;
remoteSystems.Push(packet->guid, remoteCloudClient, _FILE_AND_LINE_);
}
else
{
remoteCloudClient = remoteSystems.ItemAtIndex(remoteSystemsHashIndex);
}
unsigned int keyIndex;
for (keyIndex=0; keyIndex < getRequest->cloudQueryWithAddresses.cloudQuery.keys.Size(); keyIndex++)
{
cloudKey = getRequest->cloudQueryWithAddresses.cloudQuery.keys[keyIndex];
unsigned int keySubscriberIndex;
bool hasKeySubscriber;
keySubscriberIndex = remoteCloudClient->subscribedKeys.GetIndexFromKey(cloudKey, &hasKeySubscriber);
KeySubscriberID* keySubscriberId;
if (hasKeySubscriber)
{
DataStructures::List<RakNetGUID> specificSystems;
UnsubscribeFromKey(remoteCloudClient, packet->guid, keySubscriberIndex, cloudKey, specificSystems);
}
keySubscriberId = RakNet::OP_NEW<KeySubscriberID>(_FILE_AND_LINE_);
keySubscriberId->key=cloudKey;
unsigned int specificSystemIndex;
for (specificSystemIndex=0; specificSystemIndex < getRequest->cloudQueryWithAddresses.specificSystems.Size(); specificSystemIndex++)
{
keySubscriberId->specificSystemsSubscribedTo.Insert(getRequest->cloudQueryWithAddresses.specificSystems[specificSystemIndex], getRequest->cloudQueryWithAddresses.specificSystems[specificSystemIndex], true, _FILE_AND_LINE_);
}
remoteCloudClient->subscribedKeys.InsertAtIndex(keySubscriberId, keySubscriberIndex, _FILE_AND_LINE_);
// Add CloudData in a similar way
unsigned int dataRepositoryIndex;
bool dataRepositoryExists;
CloudDataList* cloudDataList = GetOrAllocateCloudDataList(cloudKey, &dataRepositoryExists, dataRepositoryIndex);
// If this is the first local client to subscribe to this key, call SendSubscribedKeyToServers
if (cloudDataList->subscriberCount==0)
SendSubscribedKeyToServers(cloudKey);
// If the subscription is specific, may have to also allocate CloudData
if (getRequest->cloudQueryWithAddresses.specificSystems.Size())
{
CloudData *cloudData;
bool keyDataListExists;
unsigned int specificSystemIndex;
for (specificSystemIndex=0; specificSystemIndex < getRequest->cloudQueryWithAddresses.specificSystems.Size(); specificSystemIndex++)
{
RakNetGUID specificSystem = getRequest->cloudQueryWithAddresses.specificSystems[specificSystemIndex];
unsigned int keyDataListIndex = cloudDataList->keyData.GetIndexFromKey(specificSystem, &keyDataListExists);
if (keyDataListExists==false)
{
cloudData = RakNet::OP_NEW<CloudData>(_FILE_AND_LINE_);
cloudData->dataLengthBytes=0;
cloudData->allocatedData=0;
cloudData->isUploaded=false;
cloudData->dataPtr=0;
cloudData->serverSystemAddress=UNASSIGNED_SYSTEM_ADDRESS;
cloudData->clientSystemAddress=UNASSIGNED_SYSTEM_ADDRESS;
cloudData->serverGUID=rakPeerInterface->GetMyGUID();
cloudData->clientGUID=specificSystem;
cloudDataList->keyData.Insert(specificSystem,cloudData,true,_FILE_AND_LINE_);
}
else
{
cloudData = cloudDataList->keyData[keyDataListIndex];
}
++cloudDataList->subscriberCount;
cloudData->specificSubscribers.Insert(packet->guid, packet->guid, true, _FILE_AND_LINE_);
}
}
else
{
++cloudDataList->subscriberCount;
cloudDataList->nonSpecificSubscribers.Insert(packet->guid, packet->guid, true, _FILE_AND_LINE_);
// Remove packet->guid from CloudData::specificSubscribers among all instances of cloudDataList->keyData
unsigned int subscribedKeysIndex;
bool subscribedKeysIndexExists;
subscribedKeysIndex = remoteCloudClient->subscribedKeys.GetIndexFromKey(cloudDataList->key, &subscribedKeysIndexExists);
if (subscribedKeysIndexExists)
{
KeySubscriberID* keySubscriberId;
keySubscriberId = remoteCloudClient->subscribedKeys[subscribedKeysIndex];
unsigned int specificSystemIndex;
for (specificSystemIndex=0; specificSystemIndex < keySubscriberId->specificSystemsSubscribedTo.Size(); specificSystemIndex++)
{
bool keyDataExists;
unsigned int keyDataIndex = cloudDataList->keyData.GetIndexFromKey(keySubscriberId->specificSystemsSubscribedTo[specificSystemIndex], &keyDataExists);
if (keyDataExists)
{
CloudData *keyData = cloudDataList->keyData[keyDataIndex];
keyData->specificSubscribers.Remove(packet->guid);
--cloudDataList->subscriberCount;
}
}
}
}
}
if (remoteCloudClient->subscribedKeys.Size()==0)
{
// Didn't do anything
remoteSystems.Remove(packet->guid, _FILE_AND_LINE_);
RakNet::OP_DELETE(remoteCloudClient, _FILE_AND_LINE_);
}
}
if (remoteServersWithData.Size()==0)
RakNet::OP_DELETE(getRequest, _FILE_AND_LINE_);
}
void CloudServer::OnUnsubscribeRequest(Packet *packet)
{
RakNet::BitStream bsIn(packet->data, packet->length, false);
bsIn.IgnoreBytes(sizeof(MessageID));
DataStructures::HashIndex remoteSystemIndex = remoteSystems.GetIndexOf(packet->guid);
if (remoteSystemIndex.IsInvalid()==true)
return;
RemoteCloudClient* remoteCloudClient = remoteSystems.ItemAtIndex(remoteSystemIndex);
uint16_t keyCount, specificSystemCount;
DataStructures::List<CloudKey> cloudKeys;
DataStructures::List<RakNetGUID> specificSystems;
uint16_t index;
CloudKey cloudKey;
bsIn.Read(keyCount);
for (index=0; index < keyCount; index++)
{
cloudKey.Serialize(false, &bsIn);
cloudKeys.Push(cloudKey, _FILE_AND_LINE_);
}
RakNetGUID specificSystem;
bsIn.Read(specificSystemCount);
for (index=0; index < specificSystemCount; index++)
{
bsIn.Read(specificSystem);
specificSystems.Push(specificSystem, _FILE_AND_LINE_);
}
for (unsigned int filterIndex=0; filterIndex < queryFilters.Size(); filterIndex++)
{
if (queryFilters[filterIndex]->OnUnsubscribeRequest(packet->guid, packet->systemAddress, cloudKeys, specificSystems )==false)
return;
}
// CloudDataList *cloudDataList;
bool dataRepositoryExists;
// unsigned int dataRepositoryIndex;
for (index=0; index < keyCount; index++)
{
CloudKey cloudKey = cloudKeys[index];
// dataRepositoryIndex =
dataRepository.GetIndexFromKey(cloudKey, &dataRepositoryExists);
if (dataRepositoryExists==false)
continue;
// cloudDataList = dataRepository[dataRepositoryIndex];
unsigned int keySubscriberIndex;
bool hasKeySubscriber;
keySubscriberIndex = remoteCloudClient->subscribedKeys.GetIndexFromKey(cloudKey, &hasKeySubscriber);
if (hasKeySubscriber==false)
continue;
UnsubscribeFromKey(remoteCloudClient, packet->guid, keySubscriberIndex, cloudKey, specificSystems);
}
if (remoteCloudClient->IsUnused())
{
RakNet::OP_DELETE(remoteCloudClient, _FILE_AND_LINE_);
remoteSystems.RemoveAtIndex(remoteSystemIndex, _FILE_AND_LINE_);
}
}
void CloudServer::OnServerToServerGetRequest(Packet *packet)
{
// unsigned int remoteServerIndex;
bool objectExists;
//remoteServerIndex =
remoteServers.GetIndexFromKey(packet->guid, &objectExists);
if (objectExists==false)
return;
RakNet::BitStream bsIn(packet->data, packet->length, false);
bsIn.IgnoreBytes(sizeof(MessageID)*2);
CloudQueryWithAddresses cloudQueryWithAddresses;
uint32_t requestId;
cloudQueryWithAddresses.Serialize(false, &bsIn);
bsIn.Read(requestId);
DataStructures::List<CloudData*> cloudDataResultList;
DataStructures::List<CloudKey> cloudKeyResultList;
ProcessCloudQueryWithAddresses(cloudQueryWithAddresses, cloudDataResultList, cloudKeyResultList);
RakNet::BitStream bsOut;
bsOut.Write((MessageID)ID_CLOUD_SERVER_TO_SERVER_COMMAND);
bsOut.Write((MessageID)STSC_PROCESS_GET_RESPONSE);
bsOut.Write(requestId);
WriteCloudQueryRowFromResultList(cloudDataResultList, cloudKeyResultList, &bsOut);
SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->guid, false);
}
void CloudServer::OnServerToServerGetResponse(Packet *packet)
{
unsigned int remoteServerIndex;
bool objectExists;
remoteServerIndex = remoteServers.GetIndexFromKey(packet->guid, &objectExists);
if (objectExists==false)
return;
RemoteServer *remoteServer = remoteServers[remoteServerIndex];
if (remoteServer==0)
return;
RakNet::BitStream bsIn(packet->data, packet->length, false);
bsIn.IgnoreBytes(sizeof(MessageID)*2);
uint32_t requestId;
bsIn.Read(requestId);
// Lookup request id
bool hasGetRequest;
unsigned int getRequestIndex;
getRequestIndex = getRequests.GetIndexFromKey(requestId, &hasGetRequest);
if (hasGetRequest==false)
return;
GetRequest *getRequest = getRequests[getRequestIndex];
bool hasRemoteServer;
unsigned int remoteServerResponsesIndex;
remoteServerResponsesIndex = getRequest->remoteServerResponses.GetIndexFromKey(packet->guid, &hasRemoteServer);
if (hasRemoteServer==false)
return;
BufferedGetResponseFromServer *bufferedGetResponseFromServer;
bufferedGetResponseFromServer = getRequest->remoteServerResponses[remoteServerResponsesIndex];
if (bufferedGetResponseFromServer->gotResult==true)
return;
bufferedGetResponseFromServer->gotResult=true;
uint32_t numRows;
bufferedGetResponseFromServer->queryResult.SerializeNumRows(false, numRows, &bsIn);
bufferedGetResponseFromServer->queryResult.SerializeCloudQueryRows(false, numRows, &bsIn, this);
// If all results returned, then also process locally, and return to user
if (getRequest->AllRemoteServersHaveResponded())
{
ProcessAndTransmitGetRequest(getRequest);
getRequest->Clear(this);
RakNet::OP_DELETE(getRequest, _FILE_AND_LINE_);
getRequests.RemoveAtIndex(getRequestIndex);
}
}
void CloudServer::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
{
(void) lostConnectionReason;
(void) systemAddress;
unsigned int remoteServerIndex;
bool objectExists;
remoteServerIndex = remoteServers.GetIndexFromKey(rakNetGUID, &objectExists);
if (objectExists)
{
// Update remoteServerResponses by removing this server and sending the response if it is now complete
unsigned int getRequestIndex=0;
while (getRequestIndex < getRequests.Size())
{
GetRequest *getRequest = getRequests[getRequestIndex];
bool waitingForThisServer;
unsigned int remoteServerResponsesIndex = getRequest->remoteServerResponses.GetIndexFromKey(rakNetGUID, &waitingForThisServer);
if (waitingForThisServer)
{
getRequest->remoteServerResponses[remoteServerResponsesIndex]->Clear(this);
RakNet::OP_DELETE(getRequest->remoteServerResponses[remoteServerResponsesIndex], _FILE_AND_LINE_);
getRequest->remoteServerResponses.RemoveAtIndex(remoteServerResponsesIndex);
if (getRequest->AllRemoteServersHaveResponded())
{
ProcessAndTransmitGetRequest(getRequest);
getRequest->Clear(this);
RakNet::OP_DELETE(getRequest, _FILE_AND_LINE_);
getRequests.RemoveAtIndex(getRequestIndex);
}
else
getRequestIndex++;
}
else
getRequestIndex++;
}
RakNet::OP_DELETE(remoteServers[remoteServerIndex],_FILE_AND_LINE_);
remoteServers.RemoveAtIndex(remoteServerIndex);
}
DataStructures::HashIndex remoteSystemIndex = remoteSystems.GetIndexOf(rakNetGUID);
if (remoteSystemIndex.IsInvalid()==false)
{
RemoteCloudClient* remoteCloudClient = remoteSystems.ItemAtIndex(remoteSystemIndex);
unsigned int uploadedKeysIndex;
for (uploadedKeysIndex=0; uploadedKeysIndex < remoteCloudClient->uploadedKeys.Size(); uploadedKeysIndex++)
{
// Delete keys this system has uploaded
bool keyDataRepositoryExists;
unsigned int dataRepositoryIndex = dataRepository.GetIndexFromKey(remoteCloudClient->uploadedKeys[uploadedKeysIndex], &keyDataRepositoryExists);
if (keyDataRepositoryExists)
{
CloudDataList* cloudDataList = dataRepository[dataRepositoryIndex];
bool keyDataExists;
unsigned int keyDataIndex = cloudDataList->keyData.GetIndexFromKey(rakNetGUID, &keyDataExists);
if (keyDataExists)
{
CloudData *cloudData = cloudDataList->keyData[keyDataIndex];
cloudDataList->uploaderCount--;
NotifyClientSubscribersOfDataChange(cloudData, cloudDataList->key, cloudData->specificSubscribers, false );
NotifyClientSubscribersOfDataChange(cloudData, cloudDataList->key, cloudDataList->nonSpecificSubscribers, false );
NotifyServerSubscribersOfDataChange(cloudData, cloudDataList->key, false );
cloudData->Clear();
if (cloudData->IsUnused())
{
RakNet::OP_DELETE(cloudData,_FILE_AND_LINE_);
cloudDataList->keyData.RemoveAtIndex(keyDataIndex);
if (cloudDataList->IsNotUploaded())
{
// Tell other servers that this key is no longer uploaded, so they do not request it from us
RemoveUploadedKeyFromServers(cloudDataList->key);
}
if (cloudDataList->IsUnused())
{
// Tell other servers that this key is no longer uploaded, so they do not request it from us
RemoveUploadedKeyFromServers(cloudDataList->key);
RakNet::OP_DELETE(cloudDataList, _FILE_AND_LINE_);
dataRepository.RemoveAtIndex(dataRepositoryIndex);
}
}
}
}
}
unsigned int subscribedKeysIndex;
for (subscribedKeysIndex=0; subscribedKeysIndex < remoteCloudClient->subscribedKeys.Size(); subscribedKeysIndex++)
{
KeySubscriberID* keySubscriberId;
keySubscriberId = remoteCloudClient->subscribedKeys[subscribedKeysIndex];
bool keyDataRepositoryExists;
unsigned int keyDataRepositoryIndex = dataRepository.GetIndexFromKey(remoteCloudClient->subscribedKeys[subscribedKeysIndex]->key, &keyDataRepositoryExists);
if (keyDataRepositoryExists)
{
CloudDataList* cloudDataList = dataRepository[keyDataRepositoryIndex];
if (keySubscriberId->specificSystemsSubscribedTo.Size()==0)
{
cloudDataList->nonSpecificSubscribers.Remove(rakNetGUID);
--cloudDataList->subscriberCount;
}
else
{
unsigned int specificSystemIndex;
for (specificSystemIndex=0; specificSystemIndex < keySubscriberId->specificSystemsSubscribedTo.Size(); specificSystemIndex++)
{
bool keyDataExists;
unsigned int keyDataIndex = cloudDataList->keyData.GetIndexFromKey(keySubscriberId->specificSystemsSubscribedTo[specificSystemIndex], &keyDataExists);
if (keyDataExists)
{
CloudData *keyData = cloudDataList->keyData[keyDataIndex];
keyData->specificSubscribers.Remove(rakNetGUID);
--cloudDataList->subscriberCount;
}
}
}
}
RakNet::OP_DELETE(keySubscriberId, _FILE_AND_LINE_);
}
// Delete and remove from remoteSystems
RakNet::OP_DELETE(remoteCloudClient, _FILE_AND_LINE_);
remoteSystems.RemoveAtIndex(remoteSystemIndex, _FILE_AND_LINE_);
}
}
void CloudServer::OnRakPeerShutdown(void)
{
Clear();