| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- /*
- * 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
|