Leviathan  0.8.0.0
Leviathan game engine
Leviathan::NetworkCache Class Reference

Centralized variables that AI can use on clients to replicate server behavior. More...

#include <NetworkCache.h>

+ Inheritance diagram for Leviathan::NetworkCache:

Public Member Functions

DLLEXPORT NetworkCache (NETWORKED_TYPE serverside)
 
DLLEXPORT ~NetworkCache ()
 
DLLEXPORT bool Init (NetworkHandler *owner)
 Call after creating this object. More...
 
DLLEXPORT void Release ()
 Call before deleting. More...
 
DLLEXPORT bool UpdateVariable (const NamedVariableList &updatedvalue)
 Updates a variable. More...
 
DLLEXPORT bool RemoveVariable (const std::string &name)
 Removes a variable. More...
 
DLLEXPORT NamedVariableListGetVariable (const std::string &name) const
 Retrieves a variable. More...
 
DLLEXPORT ScriptSafeVariableBlockGetVariableWrapper (const std::string &name)
 Script wrapper for GetVariable. More...
 
DLLEXPORT void SetVariableWrapper (ScriptSafeVariableBlock *variable)
 Script wrapper for UpdateVariable. More...
 
DLLEXPORT bool HandleUpdatePacket (ResponseCacheUpdated *data)
 Handles an update packet. More...
 
DLLEXPORT bool HandleUpdatePacket (ResponseCacheRemoved *data)
 Handles an update packet. More...
 
DLLEXPORT void _OnNewConnection (std::shared_ptr< Connection > connection)
 Sends all variables to a new connection. 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
 

Protected Member Functions

void _OnVariableUpdated (Lock &guard, const NamedVariableList &variable)
 Called when a variable needs to be updated on the clients. More...
 

Protected Attributes

const bool IsServer
 True on the server. More...
 
std::vector< std::shared_ptr< NamedVariableList > > CurrentVariables
 States of the AI variables, exists both on the client and the server. More...
 
NetworkHandlerOwner = nullptr
 Connections that need updating are now fetched through this. More...
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Detailed Description

Centralized variables that AI can use on clients to replicate server behavior.

Can be used for example to set the position an npc is walking towards to allow clients interpolate its position better

Todo:
Make the QueuedTasks used in this class completely thread safe in case someone decides to delete lots of variables

Definition at line 21 of file NetworkCache.h.

Constructor & Destructor Documentation

◆ NetworkCache()

DLLEXPORT NetworkCache::NetworkCache ( NETWORKED_TYPE  serverside)

Definition at line 10 of file NetworkCache.cpp.

10  :
11  IsServer(serverside == NETWORKED_TYPE::Server)
12 {
13 
14 }
const bool IsServer
True on the server.
Definition: NetworkCache.h:82

◆ ~NetworkCache()

DLLEXPORT NetworkCache::~NetworkCache ( )

Definition at line 16 of file NetworkCache.cpp.

16  {
17 
18 }

Member Function Documentation

◆ _OnNewConnection()

DLLEXPORT void Leviathan::NetworkCache::_OnNewConnection ( std::shared_ptr< Connection connection)

Sends all variables to a new connection.

Definition at line 128 of file NetworkCache.cpp.

130 {
131  if(!IsServer)
132  return;
133 
134  if(!connection->IsValidForSend())
135  return;
136 
137  // TODO: make sure this doesn't use a deleted object
138  ThreadingManager::Get()->QueueTask(new QueuedTask(std::bind<void>([](NetworkCache* cache,
139  std::shared_ptr<Connection> connection)
140  -> void
141  {
142 
143  size_t vars = 0;
144 
145  {
146  GUARD_LOCK_OTHER(cache);
147  vars = cache->CurrentVariables.size();
148  }
149 
150  for (size_t index = 0; index < vars; index++) {
151 
152  std::shared_ptr<NamedVariableList> target;
153 
154  {
155  GUARD_LOCK_OTHER(cache);
156  if(index >= cache->CurrentVariables.size())
157  return;
158 
159  target = cache->CurrentVariables[index];
160  }
161 
162  if(!target)
163  continue;
164 
165  connection->SendPacketToConnection(std::make_shared<ResponseCacheUpdated>(0,
166  *target), RECEIVE_GUARANTEE::Critical);
167  }
168 
169  }, this, connection)));
170 }
DLLEXPORT void QueueTask(std::shared_ptr< QueuedTask > task)
Adds a task to the queue.
static DLLEXPORT ThreadingManager * Get()
Encapsulates a function that can later be ran in a free thread.
Definition: QueuedTask.h:34
const bool IsServer
True on the server.
Definition: NetworkCache.h:82
std::vector< std::shared_ptr< NamedVariableList > > CurrentVariables
States of the AI variables, exists both on the client and the server.
Definition: NetworkCache.h:85
#define GUARD_LOCK_OTHER(x)
Definition: ThreadSafe.h:110
Centralized variables that AI can use on clients to replicate server behavior.
Definition: NetworkCache.h:21

◆ _OnVariableUpdated()

void Leviathan::NetworkCache::_OnVariableUpdated ( Lock guard,
const NamedVariableList variable 
)
protected

Called when a variable needs to be updated on the clients.

Definition at line 172 of file NetworkCache.cpp.

174 {
175  auto& connections = Owner->GetInterface()->GetClientConnections();
176 
177  for (auto& connection : connections) {
178 
179  connection->SendPacketToConnection(std::make_shared<ResponseCacheUpdated>(0, variable),
181  }
182 }
virtual DLLEXPORT std::vector< std::shared_ptr< Connection > > & GetClientConnections()=0
If this is a server returns all the clients.
NetworkHandler * Owner
Connections that need updating are now fetched through this.
Definition: NetworkCache.h:88
NetworkInterface * GetInterface()
Returns interface object. Type depends on AppType.

◆ GetVariable()

DLLEXPORT NamedVariableList * NetworkCache::GetVariable ( const std::string &  name) const

Retrieves a variable.

Can be used both on the client and on the server

Note
You may not change the variable. On the server you can use UpdateVariable to change it.

Definition at line 113 of file NetworkCache.cpp.

113  {
114 
115  GUARD_LOCK();
116 
117  for(auto iter = CurrentVariables.begin(); iter != CurrentVariables.end(); ++iter){
118 
119  if((*iter)->GetName() == name){
120 
121  return (*iter).get();
122  }
123  }
124 
125  return nullptr;
126 }
std::vector< std::shared_ptr< NamedVariableList > > CurrentVariables
States of the AI variables, exists both on the client and the server.
Definition: NetworkCache.h:85
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetVariableWrapper()

DLLEXPORT ScriptSafeVariableBlock * NetworkCache::GetVariableWrapper ( const std::string &  name)

Script wrapper for GetVariable.

Definition at line 184 of file NetworkCache.cpp.

184  {
185 
186  auto variable = GetVariable(name);
187 
188  if(!variable)
189  return nullptr;
190 
191  auto value = variable->GetValueDirect();
192 
193  if(!value)
194  return nullptr;
195 
196  return new ScriptSafeVariableBlock(value, variable->GetName());
197 }
DLLEXPORT NamedVariableList * GetVariable(const std::string &name) const
Retrieves a variable.
Reference counted version for scripts of VariableBlock.
Definition: DataBlock.h:840

◆ HandleUpdatePacket() [1/2]

DLLEXPORT bool NetworkCache::HandleUpdatePacket ( ResponseCacheUpdated *  data)

Handles an update packet.

Note
Works both on the client and the server but shouldn't be called on the server unless you really know what you are doing.

Definition at line 86 of file NetworkCache.cpp.

86  {
87 
88  GUARD_LOCK();
89 
90  // Remove old variable (if any) //
91  for(auto iter = CurrentVariables.begin(); iter != CurrentVariables.end(); ++iter){
92 
93  if(data->Variable.CompareName((*iter)->GetName())){
94 
95  CurrentVariables.erase(iter);
96  break;
97  }
98  }
99 
100  // Add it //
101  CurrentVariables.push_back(std::make_shared<NamedVariableList>(data->Variable));
102 
103  return true;
104 }
std::vector< std::shared_ptr< NamedVariableList > > CurrentVariables
States of the AI variables, exists both on the client and the server.
Definition: NetworkCache.h:85
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ HandleUpdatePacket() [2/2]

DLLEXPORT bool Leviathan::NetworkCache::HandleUpdatePacket ( ResponseCacheRemoved *  data)

Handles an update packet.

Note
Works both on the client and the server but shouldn't be called on the server unless you really know what you are doing.

Definition at line 106 of file NetworkCache.cpp.

106  {
107 
108  DEBUG_BREAK;
109  return true;
110 }

◆ Init()

DLLEXPORT bool Leviathan::NetworkCache::Init ( NetworkHandler owner)

Call after creating this object.

Definition at line 20 of file NetworkCache.cpp.

20  {
21 
22  Owner = owner;
23 
24  LEVIATHAN_ASSERT(Owner, "NetworkCache no owner");
25  return Owner != nullptr;
26 }
NetworkHandler * Owner
Connections that need updating are now fetched through this.
Definition: NetworkCache.h:88

◆ Release()

DLLEXPORT void NetworkCache::Release ( )

Call before deleting.

Drops all active connections and clears the cache

Definition at line 28 of file NetworkCache.cpp.

28  {
29 
30  GUARD_LOCK();
31 
32  CurrentVariables.clear();
33 }
std::vector< std::shared_ptr< NamedVariableList > > CurrentVariables
States of the AI variables, exists both on the client and the server.
Definition: NetworkCache.h:85
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ RemoveVariable()

DLLEXPORT bool NetworkCache::RemoveVariable ( const std::string &  name)

Removes a variable.

Note
This can only be called on the server
Todo:
send remove message to clients

Definition at line 68 of file NetworkCache.cpp.

68  {
69 
70  GUARD_LOCK();
71 
72  auto end = CurrentVariables.end();
73  for(auto iter = CurrentVariables.begin(); iter != end; ++iter){
74 
75  if((*iter)->GetName() == name){
76 
77  CurrentVariables.erase(iter);
78  Logger::Get()->Info("NetworkCache: todo: send remove message to clients");
79  return true;
80  }
81  }
82 
83  return false;
84 }
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
std::vector< std::shared_ptr< NamedVariableList > > CurrentVariables
States of the AI variables, exists both on the client and the server.
Definition: NetworkCache.h:85
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ SetVariableWrapper()

DLLEXPORT void NetworkCache::SetVariableWrapper ( ScriptSafeVariableBlock variable)

Script wrapper for UpdateVariable.

Definition at line 199 of file NetworkCache.cpp.

199  {
200 
202  variable->GetBlockConst()->AllocateNewFromThis()));
203 }
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
std::string GetName() const
Definition: DataBlock.h:813
DLLEXPORT bool UpdateVariable(const NamedVariableList &updatedvalue)
Updates a variable.
virtual DLLEXPORT DataBlockAll * AllocateNewFromThis() const =0
const DataBlockAll * GetBlockConst() const
Definition: DataBlock.h:504

◆ UpdateVariable()

DLLEXPORT bool NetworkCache::UpdateVariable ( const NamedVariableList updatedvalue)

Updates a variable.

Parameters
updatedvalueThe value to replace/add
Note
This can only be called on the server

Definition at line 35 of file NetworkCache.cpp.

35  {
36 
37  GUARD_LOCK();
38 
39  if(!IsServer)
40  return false;
41 
42  // Remove old variable //
43  auto end = CurrentVariables.end();
44  for(auto iter = CurrentVariables.begin(); iter != end; ++iter){
45 
46  if(updatedvalue.CompareName((*iter)->GetName())){
47 
48  // Check does the value match //
49  if(updatedvalue == *(*iter)){
50 
51  // The value is the same //
52  return true;
53  }
54 
55  CurrentVariables.erase(iter);
56  break;
57  }
58  }
59 
60  // Add it //
61  CurrentVariables.push_back(std::make_shared<NamedVariableList>(updatedvalue));
62 
63  _OnVariableUpdated(guard, updatedvalue);
64 
65  return true;
66 }
DLLEXPORT bool CompareName(const std::string &name) const
Definition: NamedVars.cpp:485
void _OnVariableUpdated(Lock &guard, const NamedVariableList &variable)
Called when a variable needs to be updated on the clients.
const bool IsServer
True on the server.
Definition: NetworkCache.h:82
std::vector< std::shared_ptr< NamedVariableList > > CurrentVariables
States of the AI variables, exists both on the client and the server.
Definition: NetworkCache.h:85
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

Member Data Documentation

◆ CurrentVariables

std::vector<std::shared_ptr<NamedVariableList> > Leviathan::NetworkCache::CurrentVariables
protected

States of the AI variables, exists both on the client and the server.

Definition at line 85 of file NetworkCache.h.

◆ IsServer

const bool Leviathan::NetworkCache::IsServer
protected

True on the server.

Definition at line 82 of file NetworkCache.h.

◆ Owner

NetworkHandler* Leviathan::NetworkCache::Owner = nullptr
protected

Connections that need updating are now fetched through this.

Definition at line 88 of file NetworkCache.h.


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