Leviathan  0.8.0.0
Leviathan game engine
SyncedVariables.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2016 Henri Hyyryläinen
3 #pragma once
4 // ------------------------------------ //
5 #include "Define.h"
6 // ------------------------------------ //
7 #include "Common/ThreadSafe.h"
8 #include "NetworkResponse.h"
9 #include "SyncedResource.h"
10 #include "Common/BaseNotifier.h"
11 
12 namespace Leviathan{
13 
14 
15 
19  friend SyncedVariables;
20 public:
27  DLLEXPORT SyncedValue(NamedVariableList* newddata, bool passtoclients = true,
28  bool allowevents = true);
30 
31 
33  DLLEXPORT void NotifyUpdated();
34 
38 
39 protected:
40 
43  void _MasterYouCalled(SyncedVariables* owner);
44 
45 
46  // ------------------------------------ //
47 
51 
54 
57 
58  SyncedVariables* Owner = nullptr;
59 };
60 
61 
68  friend SyncedValue;
69  friend SyncedResource;
70 
71  struct SendDataSyncAllStruct;
72 
73 public:
76  NetworkInterface* handlinginterface);
79 
80 
90  DLLEXPORT bool AddNewVariable(std::shared_ptr<SyncedValue> newvalue);
91 
92 
94  DLLEXPORT bool HandleSyncRequests(std::shared_ptr<NetworkRequest> request,
95  Connection* connection);
96 
99  DLLEXPORT bool HandleResponseOnlySync(std::shared_ptr<NetworkResponse> response,
100  Connection* connection);
101 
105 
108  DLLEXPORT bool IsSyncDone();
109 
111  DLLEXPORT bool IsVariableNameUsed(Lock &guard, const std::string &name);
112 
114  DLLEXPORT FORCE_INLINE bool IsVariableNameUsed(const std::string &name){
115  GUARD_LOCK();
116  return IsVariableNameUsed(guard, name);
117  }
118 
125 
126 protected:
127 
129  inline void _NotifyUpdatedValue(const SyncedValue* const valtosync, int useid = -1){
130 
131  GUARD_LOCK();
132  _NotifyUpdatedValue(guard, valtosync, useid);
133  }
134 
135  void _NotifyUpdatedValue(Lock &guard, const SyncedValue* const valtosync, int useid = -1);
136 
137  void _NotifyUpdatedValue(Lock &guard, SyncedResource* valtosync, int useid = -1);
138 
139  std::shared_ptr<SentNetworkThing> _SendValueToSingleReceiver(Connection* unsafeptr,
140  const SyncedValue* const valtosync);
141  std::shared_ptr<SentNetworkThing> _SendValueToSingleReceiver(Connection* unsafeptr,
142  SyncedResource* valtosync);
143 
144  void _UpdateFromNetworkReceive(ResponseSyncValData* datatouse, Lock &guard);
145 
147  void _IWasUpdated(SyncedResource* me);
148 
150  void _OnSyncedResourceReceived(const std::string &name, sf::Packet &packetdata);
151 
153  void _UpdateReceiveCount(const std::string &nameofthing);
154 
155 
156  static void _RunSendAllVariablesTask(Connection* connection,
157  SyncedVariables* instance, std::shared_ptr<SendDataSyncAllStruct> data);
158 
159  // ------------------------------------ //
160 
164 
168 
172  bool IsHost;
173 
175  bool SyncDone = false;
176 
181  std::vector<std::unique_ptr<std::string>> ValueNamesUpdated;
182 
184  size_t ExpectedThingCount = 0;
185 
188 
190  std::vector<std::shared_ptr<SyncedValue>> ToSyncValues;
191 };
192 
193 }
194 
195 #ifdef LEAK_INTO_GLOBAL
197 #endif
198 
NamedVariableList * HeldVariables
Holds the actual variables.
Class that synchronizes some key variables with another instance.
DLLEXPORT bool IsVariableNameUsed(Lock &guard, const std::string &name)
Checks whether a name is already in use.
bool AllowSendEvents
If set to true sends generic update events.
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
DLLEXPORT void NotifyUpdated()
Call after changing the HeldVariables.
NetworkInterface * CorrespondingInterface
DLLEXPORT SyncedVariables(NetworkHandler *owner, NETWORKED_TYPE type, NetworkInterface *handlinginterface)
Construct an instance that must be owned by a NetworkHandler.
DLLEXPORT bool AddNewVariable(std::shared_ptr< SyncedValue > newvalue)
Adds a new variable to be synced.
DLLEXPORT bool IsSyncDone()
Returns true if we have received a sync complete notification.
DLLEXPORT FORCE_INLINE bool IsVariableNameUsed(const std::string &name)
Short version for IsVariableNameUsed.
Specialized class for accepting all parent/child objects.
Definition: BaseNotifier.h:103
DLLEXPORT ~SyncedVariables()
The destructor doesn't force other instances to remove their variables.
#define FORCE_INLINE
Definition: Include.h:95
DLLEXPORT bool HandleSyncRequests(std::shared_ptr< NetworkRequest > request, Connection *connection)
Handles all requests aimed at the synchronized variables.
void _MasterYouCalled(SyncedVariables *owner)
Sets the owner, which is very important.
void _UpdateReceiveCount(const std::string &nameofthing)
Updates the number of synced values received during SyncDone.
DLLEXPORT bool HandleResponseOnlySync(std::shared_ptr< NetworkResponse > response, Connection *connection)
Handles a response only packet, if it is a sync packet.
DLLEXPORT SyncedValue(NamedVariableList *newddata, bool passtoclients=true, bool allowevents=true)
Creates a new variable that can be synced with others.
void _IWasUpdated(SyncedResource *me)
SyncedResource calls this when it is updated.
std::vector< std::unique_ptr< std::string > > ValueNamesUpdated
void _OnSyncedResourceReceived(const std::string &name, sf::Packet &packetdata)
This is called when an update to a SyncedResource is received through the network.
NETWORKED_TYPE
Type of networked application.
Definition: CommonNetwork.h:22
Class that encapsulates a value that can be used for syncing.
size_t ActualGotThingCount
The number of variables received thus far.
DLLEXPORT void PrepareForFullSync()
Call before requesting full value sync.
DLLEXPORT NamedVariableList * GetVariableAccess() const
Gets a direct access to the variables.
static void _RunSendAllVariablesTask(Connection *connection, SyncedVariables *instance, std::shared_ptr< SendDataSyncAllStruct > data)
Base class for all values that are to be automatically synced between clients.
DLLEXPORT void SetExpectedNumberOfVariablesReceived(size_t amount)
Sets the expected number of variables received.
std::vector< std::shared_ptr< SyncedValue > > ToSyncValues
Contains the values that are to be synced.
bool SyncDone
Set when sync complete packet is received.
Class that handles a single connection to another instance.
Definition: Connection.h:105
void _NotifyUpdatedValue(const SyncedValue *const valtosync, int useid=-1)
Sends update notifications about a variable.
#define DLLEXPORT
Definition: Include.h:84
void _UpdateFromNetworkReceive(ResponseSyncValData *datatouse, Lock &guard)
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
SyncedVariables * Owner
Handles everything related to connections.
size_t ExpectedThingCount
The expected number of variables to receive during SyncDone is false.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
Class that encapsulates common networking functionality that is required by all networked programs.
std::shared_ptr< SentNetworkThing > _SendValueToSingleReceiver(Connection *unsafeptr, const SyncedValue *const valtosync)
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:18