Leviathan  0.8.0.0
Leviathan game engine
Leviathan::SyncedResource Class Referenceabstract

Base class for all values that are to be automatically synced between clients. More...

#include <SyncedResource.h>

+ Inheritance diagram for Leviathan::SyncedResource:

Public Member Functions

DLLEXPORT SyncedResource (const std::string &uniquename)
 Constructs a base class for synced variables that requires a unique name. More...
 
virtual DLLEXPORT ~SyncedResource ()
 
virtual DLLEXPORT void AddDataToPacket (Lock &guard, sf::Packet &packet)
 Serializes the name to a packet. More...
 
DLLEXPORT void AddDataToPacket (sf::Packet &packet)
 
virtual DLLEXPORT bool UpdateDataFromPacket (Lock &guard, sf::Packet &packet)
 Assigns data from a packet to this resource. More...
 
DLLEXPORT bool UpdateDataFromPacket (sf::Packet &packet)
 
virtual DLLEXPORT void StartSync (SyncedVariables &variablesync)
 Registers this resource with the SyncedVariables instance. More...
 
DLLEXPORT void NotifyUpdatedValue ()
 Notify that this is changed and we want a local message, too. More...
 
- Public Member Functions inherited from Leviathan::BaseNotifiableAll
 BaseNotifiableAll ()
 
 ~BaseNotifiableAll ()
 
- Public Member Functions inherited from Leviathan::BaseNotifiable< BaseNotifierAll, BaseNotifiableAll >
 BaseNotifiable (BaseNotifiableAll *ourptr)
 
virtual ~BaseNotifiable ()
 
void ReleaseParentHooks (Lock &guard)
 Release function which releases all hooks. More...
 
bool UnConnectFromNotifier (Lock &guard, BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *specificnotifier, Lock &notifierlock)
 The actual implementation of UnConnectFromNotifier. More...
 
bool UnConnectFromNotifier (Lock &guard, BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *specificnotifier)
 
FORCE_INLINE bool UnConnectFromNotifier (BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *specificnotifier)
 Disconnects this from a previously connected notifier. More...
 
bool UnConnectFromNotifier (int id)
 This searches the connected notifiers and calls the above function with it's pointer. More...
 
virtual void NotifyAll (Lock &guard)
 Notifies all the parents of this object about something. More...
 
bool IsConnectedTo (BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *check, Lock &guard)
 Actual implementation of this method. More...
 
FORCE_INLINE bool IsConnectedTo (BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *check)
 Returns true when the specified object is already connected. More...
 
bool ConnectToNotifier (BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *owner)
 Connects this to a notifier object calling all the needed functions. More...
 
bool ConnectToNotifier (Lock &unlockable, BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *owner)
 Variant for already locked objects. More...
 
void _OnUnhookNotifier (Lock &locked, BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *parent, Lock &parentlock)
 Callback called by the parent, used to not to call the unhook again on the parent. More...
 
void _OnHookNotifier (Lock &locked, BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *parent, Lock &parentlock)
 Called by parent to hook, and doesn't call the parent's functions. More...
 
BaseNotifiableAllGetActualPointerToNotifiableObject ()
 Gets the internal pointer to the actual object. More...
 
virtual void OnNotified (Lock &ownlock, BaseNotifierAll *parent, Lock &parentlock)
 Called when our parent notifies us about something. More...
 
- Public Member Functions inherited from Leviathan::ThreadSafeGeneric< MutexType >
DLLEXPORT ThreadSafeGeneric ()
 
DLLEXPORT ~ThreadSafeGeneric ()
 
FORCE_INLINE void VerifyLock (RecursiveLock &guard) const
 
FORCE_INLINE void VerifyLock (Lock &lockit) const
 

Static Public Member Functions

static DLLEXPORT std::string GetSyncedResourceNameFromPacket (sf::Packet &packet)
 Gets a name from packet leaving only the variable data there. More...
 

Protected Member Functions

virtual void UpdateCustomDataFromPacket (Lock &guard, sf::Packet &packet)=0
 Should load the custom data from a packet. More...
 
virtual void SerializeCustomDataToPacket (Lock &guard, sf::Packet &packet)=0
 Should be used to add custom data to packet. More...
 
virtual DLLEXPORT void UpdateOurNetworkValue (Lock &guard)
 Notifies our SyncedVariables of an update. More...
 
virtual void OnValueUpdated (Lock &guard)
 
SyncedResourceoperator= (const SyncedResource &other)
 
 SyncedResource (const SyncedResource &other)
 
- Protected Member Functions inherited from Leviathan::BaseNotifiable< BaseNotifierAll, BaseNotifiableAll >
virtual void _OnNotifierConnected (Lock &guard, BaseNotifierAll *parentadded, Lock &parentlock)
 
virtual void _OnNotifierDisconnected (Lock &guard, BaseNotifierAll *parentremoved, Lock &parentlock)
 

Protected Attributes

const std::string Name
 
- Protected Attributes inherited from Leviathan::BaseNotifiable< BaseNotifierAll, BaseNotifiableAll >
BaseNotifiableAllPointerToOurNotifiable
 Stores a pointer to the object that is inherited from this. More...
 
std::vector< BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *> ConnectedToParents
 Vector of other objects that this is connected to. More...
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Additional Inherited Members

- Public Types inherited from Leviathan::ThreadSafeGeneric< MutexType >
using LockT = typename LockTypeResolver< MutexType >::LType
 

Detailed Description

Base class for all values that are to be automatically synced between clients.

Note
The variable is not sent to other clients unless they allow new resources from the net
Warning
The variable must be registered for syncing after SyncedVariables is created
Todo:
Possibly do a global static class that will automatically register all when single StartSync is called

Definition at line 15 of file SyncedResource.h.

Constructor & Destructor Documentation

◆ SyncedResource() [1/2]

DLLEXPORT Leviathan::SyncedResource::SyncedResource ( const std::string &  uniquename)

Constructs a base class for synced variables that requires a unique name.

Todo:
Actually check if the name is actually unique

Definition at line 9 of file SyncedResource.cpp.

9  :
10  Name(uniquename)
11 {
12 
13 }
const std::string Name

◆ ~SyncedResource()

DLLEXPORT Leviathan::SyncedResource::~SyncedResource ( )
virtual

Definition at line 15 of file SyncedResource.cpp.

15  {
16 
17  GUARD_LOCK();
18 
19  // Unregister (if not already done) //
20  ReleaseParentHooks(guard);
21 }
void ReleaseParentHooks(Lock &guard)
Release function which releases all hooks.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ SyncedResource() [2/2]

Leviathan::SyncedResource::SyncedResource ( const SyncedResource other)
protected

Member Function Documentation

◆ AddDataToPacket() [1/2]

DLLEXPORT void Leviathan::SyncedResource::AddDataToPacket ( Lock guard,
sf::Packet &  packet 
)
virtual

Serializes the name to a packet.

Definition at line 67 of file SyncedResource.cpp.

67  {
68 
69  // First add the name //
70  packet << Name;
71 
72  // Then add our data //
73  SerializeCustomDataToPacket(guard, packet);
74 }
const std::string Name
virtual void SerializeCustomDataToPacket(Lock &guard, sf::Packet &packet)=0
Should be used to add custom data to packet.

◆ AddDataToPacket() [2/2]

DLLEXPORT void Leviathan::SyncedResource::AddDataToPacket ( sf::Packet &  packet)
inline

Definition at line 27 of file SyncedResource.h.

27  {
28 
29  GUARD_LOCK();
30  AddDataToPacket(guard, packet);
31  }
virtual DLLEXPORT void AddDataToPacket(Lock &guard, sf::Packet &packet)
Serializes the name to a packet.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ GetSyncedResourceNameFromPacket()

DLLEXPORT std::string Leviathan::SyncedResource::GetSyncedResourceNameFromPacket ( sf::Packet &  packet)
static

Gets a name from packet leaving only the variable data there.

Definition at line 76 of file SyncedResource.cpp.

76  {
77  // Get the name from the packet //
78  std::string tmpstr;
79 
80  packet >> tmpstr;
81 
82  if(!packet)
83  throw InvalidArgument("resource sync packet has invalid format");
84 
85  return tmpstr;
86 }

◆ NotifyUpdatedValue()

DLLEXPORT void Leviathan::SyncedResource::NotifyUpdatedValue ( )

Notify that this is changed and we want a local message, too.

If you do not want a local message call UpdateOurNetworkValue directly

Definition at line 50 of file SyncedResource.cpp.

50  {
51  GUARD_LOCK();
52 
53  // Update the networked value //
54  UpdateOurNetworkValue(guard);
55 
56  // Notify us about the update //
57  OnValueUpdated(guard);
58 
59  // Notify listeners //
60  NotifyAll(guard);
61 }
virtual void OnValueUpdated(Lock &guard)
virtual DLLEXPORT void UpdateOurNetworkValue(Lock &guard)
Notifies our SyncedVariables of an update.
virtual void NotifyAll(Lock &guard)
Notifies all the parents of this object about something.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ OnValueUpdated()

void Leviathan::SyncedResource::OnValueUpdated ( Lock guard)
protectedvirtual

Update notifications are received through this

Called from UpdateDataFromPacket

Reimplemented in Leviathan::SyncedPrimitive< DTypeName >.

Definition at line 63 of file SyncedResource.cpp.

63  {
64 
65 }

◆ operator=()

SyncedResource& Leviathan::SyncedResource::operator= ( const SyncedResource other)
protected

◆ SerializeCustomDataToPacket()

virtual void Leviathan::SyncedResource::SerializeCustomDataToPacket ( Lock guard,
sf::Packet &  packet 
)
protectedpure virtual

Should be used to add custom data to packet.

See also
UpdateCustomDataFromPacket

Implemented in Leviathan::SyncedPrimitive< DTypeName >.

◆ StartSync()

DLLEXPORT void Leviathan::SyncedResource::StartSync ( SyncedVariables variablesync)
virtual

Registers this resource with the SyncedVariables instance.

Postcondition
The variable is now ready for use

Definition at line 23 of file SyncedResource.cpp.

24 {
25  ConnectToNotifier(&variablesync);
26 }
bool ConnectToNotifier(BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *owner)
Connects this to a notifier object calling all the needed functions.

◆ UpdateCustomDataFromPacket()

virtual void Leviathan::SyncedResource::UpdateCustomDataFromPacket ( Lock guard,
sf::Packet &  packet 
)
protectedpure virtual

Should load the custom data from a packet.

Implemented in Leviathan::SyncedPrimitive< DTypeName >.

◆ UpdateDataFromPacket() [1/2]

DLLEXPORT bool Leviathan::SyncedResource::UpdateDataFromPacket ( Lock guard,
sf::Packet &  packet 
)
virtual

Assigns data from a packet to this resource.

Returns
False when the actual implementation throws

Definition at line 28 of file SyncedResource.cpp.

30 {
31 
32  // Load the custom data //
33  try{
34  UpdateCustomDataFromPacket(guard, packet);
35 
36  } catch(const InvalidArgument &e){
37 
38  e.PrintToLog();
39  return false;
40  }
41 
42  // Notify us about the update //
43  OnValueUpdated(guard);
44 
45  // Notify listeners //
46  NotifyAll(guard);
47  return true;
48 }
virtual void OnValueUpdated(Lock &guard)
virtual void UpdateCustomDataFromPacket(Lock &guard, sf::Packet &packet)=0
Should load the custom data from a packet.
virtual void NotifyAll(Lock &guard)
Notifies all the parents of this object about something.
virtual DLLEXPORT void PrintToLog() const noexcept
Definition: Exceptions.cpp:35

◆ UpdateDataFromPacket() [2/2]

DLLEXPORT bool Leviathan::SyncedResource::UpdateDataFromPacket ( sf::Packet &  packet)
inline

Definition at line 40 of file SyncedResource.h.

40  {
41 
42  GUARD_LOCK();
43  return UpdateDataFromPacket(guard, packet);
44  }
virtual DLLEXPORT bool UpdateDataFromPacket(Lock &guard, sf::Packet &packet)
Assigns data from a packet to this resource.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ UpdateOurNetworkValue()

DLLEXPORT void Leviathan::SyncedResource::UpdateOurNetworkValue ( Lock guard)
protectedvirtual

Notifies our SyncedVariables of an update.

Todo:
Proper locking

Definition at line 88 of file SyncedResource.cpp.

88  {
89 
90  if (ConnectedToParents.size() < 1) {
91 
92  LOG_WARNING("Updating SyncedResource that isn't attached to SyncedVariables");
93  return;
94  }
95 
96  static_cast<SyncedVariables*>(ConnectedToParents[0]->GetActualPointerToNotifierObject())
97  ->_NotifyUpdatedValue(guard, this);
98 }
Class that synchronizes some key variables with another instance.
#define LOG_WARNING(x)
Definition: Define.h:83
std::vector< BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *> ConnectedToParents
Vector of other objects that this is connected to.

Member Data Documentation

◆ Name

const std::string Leviathan::SyncedResource::Name
protected

Definition at line 80 of file SyncedResource.h.


The documentation for this class was generated from the following files: