Leviathan  0.8.0.0
Leviathan game engine
Leviathan::NetworkHandler Class Reference

Handles everything related to connections. More...

#include <NetworkHandler.h>

+ Inheritance diagram for Leviathan::NetworkHandler:

Public Member Functions

DLLEXPORT NetworkHandler (NETWORKED_TYPE ntype, NetworkInterface *packethandler)
 
virtual DLLEXPORT ~NetworkHandler ()
 
virtual DLLEXPORT bool Init (const MasterServerInformation &info)
 
DLLEXPORT bool Init (uint16_t port=0)
 Don't use this version. This just creates a socket with the port. More...
 
virtual DLLEXPORT void Release ()
 
virtual DLLEXPORT void UpdateAllConnections ()
 
virtual DLLEXPORT void RemoveClosedConnections (Lock &guard)
 
DLLEXPORT std::shared_ptr< std::promise< std::string > > QueryMasterServer (const MasterServerInformation &info)
 
DLLEXPORT bool IsConnectionValid (Connection &connection) const
 Checks whether a connection is open and ready for sending. More...
 
DLLEXPORT std::shared_ptr< ConnectionGetConnection (Connection *directptr) const
 Returns a persistent pointer to a connection. More...
 
DLLEXPORT std::shared_ptr< ConnectionOpenConnectionTo (const std::string &targetaddress)
 Opens a new connection to the provided address. More...
 
DLLEXPORT std::shared_ptr< ConnectionOpenConnectionTo (Lock &guard, const sf::IpAddress &targetaddress, unsigned short port)
 Variant for already resolved address. More...
 
std::shared_ptr< ConnectionOpenConnectionTo (const sf::IpAddress &targetaddress, unsigned short port)
 
uint16_t GetOurPort () const
 Returns the port to which our socket has been bind. More...
 
NETWORKED_TYPE GetNetworkType () const
 Gets the type of network this program uses. More...
 
NetworkInterfaceGetInterface ()
 Returns interface object. Type depends on AppType. More...
 
NetworkClientInterfaceGetClientInterface ()
 
NetworkCacheGetCache ()
 
SyncedVariablesGetSyncedVariables ()
 
DLLEXPORT void ShutdownCache ()
 Destroys the network cache permanently. More...
 
DLLEXPORT void ReleaseInputHandler ()
 Destroys the networked input handler and all input objects. More...
 
virtual DLLEXPORT void CloseConnection (Connection &connection)
 Marks a connection as closing. More...
 
DLLEXPORT void _RegisterConnection (std::shared_ptr< Connection > connection)
 
- 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 GetServerAddressPartOfAddress (const std::string &fulladdress, const std::string &regextouse="http://.*?/")
 Retrieves base address from an http protocol URL For example if passed http://boostslair.com/Pong/MastersList.php returns http://boostslair.com/ //. More...
 

Protected Member Functions

DLLEXPORT void DisconnectInterface ()
 Unhooks the NetworkInterfaces from this object. More...
 
Lock LockSocketForUse ()
 
void _ReleaseSocket ()
 
bool _RunUpdateOnce (Lock &guard)
 Returns false if the socket has been closed. More...
 
void _RunListenerThread ()
 Constantly listens for packets in a blocked state. More...
 
void _SaveMasterServerList ()
 
bool _LoadMasterServerList ()
 

Protected Attributes

Mutex ConnectionsToTerminateMutex
 Closes connections on next update. More...
 
std::vector< Connection * > ConnectionsToTerminate
 
std::vector< std::shared_ptr< Connection > > OpenConnections
 
NETWORKED_TYPE AppType
 Type of application. More...
 
NetworkServerInterfaceServerInterface = nullptr
 
NetworkClientInterfaceClientInterface = nullptr
 
sf::UdpSocket _Socket
 Main socket for listening for incoming packets and sending. More...
 
Mutex SocketMutex
 Used to control the locking of the socket. More...
 
bool BlockingMode = false
 If true uses a blocking socket and async handling. More...
 
uint16_t PortNumber
 Our local port number. More...
 
std::unique_ptr< SyncedVariablesVariableSyncer
 The syncable variable holder associated with this instance. More...
 
std::unique_ptr< NetworkCache_NetworkCache
 Networked variable cache. More...
 
std::unique_ptr< GameSpecificPacketHandler_GameSpecificPacketHandler
 Game specific packet handler that allows programs to register their own packets. More...
 
std::vector< std::shared_ptr< std::string > > MasterServers
 
std::shared_ptr< ConnectionMasterServerConnection
 Stores a "working" (meaning the server has responded something) master server address. More...
 
MasterServerInformation StoredMasterServerInfo
 
std::thread MasterServerConnectionThread
 Makes sure that master server thread is graciously closed. More...
 
bool CloseMasterServerConnection
 
std::thread ListenerThread
 Thread that constantly blocks on the socket and waits for packets. More...
 
std::string MasterServerMustPassIdentification
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Friends

void RunGetResponseFromMaster (NetworkHandler *instance, std::shared_ptr< std::promise< std::string >> resultvar)
 

Detailed Description

Handles everything related to connections.

Definition at line 32 of file NetworkHandler.h.

Constructor & Destructor Documentation

◆ NetworkHandler()

DLLEXPORT Leviathan::NetworkHandler::NetworkHandler ( NETWORKED_TYPE  ntype,
NetworkInterface packethandler 
)

Definition at line 24 of file NetworkHandler.cpp.

26  : AppType(ntype), CloseMasterServerConnection(false)
27 {
28  // This will assert if we were passed the wrong type
29  packethandler->VerifyType(AppType);
30  packethandler->SetOwner(this);
31 
33 
34  ClientInterface = dynamic_cast<NetworkClientInterface*>(packethandler);
35 
36  } else if(AppType == NETWORKED_TYPE::Server){
37 
38  ServerInterface = dynamic_cast<NetworkServerInterface*>(packethandler);
39  }
40 
41  // Create the variable sync object //
42  VariableSyncer = std::make_unique<SyncedVariables>(this, AppType, packethandler);
43 
44  _NetworkCache = std::make_unique<NetworkCache>(AppType);
45 
46  if(!_NetworkCache || !_NetworkCache->Init(this)){
47 
48  LOG_ERROR("NetworkHandler: failed to create NetworkCache");
49  LEVIATHAN_ASSERT(0, "NetworkHandler: failed to create NetworkCache");
50  }
51 
52  // Create the custom packet handler //
53  _GameSpecificPacketHandler = std::make_unique<GameSpecificPacketHandler>(
54  packethandler);
55 }
std::unique_ptr< NetworkCache > _NetworkCache
Networked variable cache.
#define LOG_ERROR(x)
Definition: Define.h:83
NetworkClientInterface * ClientInterface
NETWORKED_TYPE AppType
Type of application.
DLLEXPORT void VerifyType(NETWORKED_TYPE type) const
Asserts if types don&#39;t match.
std::unique_ptr< GameSpecificPacketHandler > _GameSpecificPacketHandler
Game specific packet handler that allows programs to register their own packets.
DLLEXPORT void SetOwner(NetworkHandler *owner)
NetworkServerInterface * ServerInterface
Class that encapsulates common networking functionality required by server programs.
Class that encapsulates common networking functionality required by client programs.
std::unique_ptr< SyncedVariables > VariableSyncer
The syncable variable holder associated with this instance.

◆ ~NetworkHandler()

DLLEXPORT NetworkHandler::~NetworkHandler ( )
virtual

Definition at line 57 of file NetworkHandler.cpp.

57  {
58 
59  _NetworkCache.reset();
60 }
std::unique_ptr< NetworkCache > _NetworkCache
Networked variable cache.

Member Function Documentation

◆ _LoadMasterServerList()

bool Leviathan::NetworkHandler::_LoadMasterServerList ( )
protected

Definition at line 387 of file NetworkHandler.cpp.

387  {
388 
389  // Skip if there is no file defined //
391  return false;
392 
395 
396  if(!fileobj)
397  return false;
398 
399  // Try to get the right variable with the name //
400  auto foundvar = fileobj->GetVariables()->GetValueDirectRaw("MasterServers");
401 
402  if(!foundvar)
403  return false;
404 
405  // Found value, load //
406  size_t maxval = foundvar->GetVariableCount();
407 
408  // We need locking for this add //
409  GUARD_LOCK();
410 
411  MasterServers.reserve(maxval);
412 
413  for(size_t i = 0; i < maxval; i++){
414  MasterServers.push_back(unique_ptr<string>(new std::string(
415  foundvar->GetValueDirect(i)->ConvertAndReturnVariable<std::string>())));
416  }
417 
418  return true;
419 }
static DLLEXPORT std::unique_ptr< ObjectFile > ProcessObjectFile(const std::string &file, LErrorReporter *reporterror)
Reads an ObjectFile to an in-memory data structure.
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
MasterServerInformation StoredMasterServerInfo
std::vector< std::shared_ptr< std::string > > MasterServers
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ _RegisterConnection()

DLLEXPORT void Leviathan::NetworkHandler::_RegisterConnection ( std::shared_ptr< Connection connection)

Adds a connection to the list of open connections. Use ONLY if you have manually created a Connection object

Definition at line 446 of file NetworkHandler.cpp.

448 {
449  // Skip duplicates //
450  if(GetConnection(connection.get()))
451  return;
452 
453  GUARD_LOCK();
454 
455  OpenConnections.push_back(connection);
456 }
std::vector< std::shared_ptr< Connection > > OpenConnections
DLLEXPORT std::shared_ptr< Connection > GetConnection(Connection *directptr) const
Returns a persistent pointer to a connection.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ _ReleaseSocket()

void Leviathan::NetworkHandler::_ReleaseSocket ( )
protected

Definition at line 198 of file NetworkHandler.cpp.

198  {
199  // This might cause the game to hang... //
200 
201  bool blockunbind = false;
202 
203  {
205  variables->GetValueAndConvertTo<bool>("DisableSocketUnbind", blockunbind);
206  }
207 
208  auto lock = LockSocketForUse();
209 
210  // This should do the trick //
211  if(!blockunbind || !BlockingMode){
212 
213  _Socket.unbind();
214 
215  } else {
216  Logger::Get()->Info("NetworkHandler: _ReleaseSocket: blocked unbind");
217  }
218 }
#define GAMECONFIGURATION_GET_VARIABLEACCESS(x)
sf::UdpSocket _Socket
Main socket for listening for incoming packets and sending.
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
bool BlockingMode
If true uses a blocking socket and async handling.
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106

◆ _RunListenerThread()

void Leviathan::NetworkHandler::_RunListenerThread ( )
protected

Constantly listens for packets in a blocked state.

Definition at line 622 of file NetworkHandler.cpp.

622  {
623 
624  // Let's listen for things //
625  Logger::Get()->Info("NetworkHandler: running listening thread");
626  {
627 
628  GUARD_LOCK();
629  while (_RunUpdateOnce(guard)) {
630 
631  }
632  }
633 
634  Logger::Get()->Info("NetworkHandler: listening socket thread quitting");
635 }
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
bool _RunUpdateOnce(Lock &guard)
Returns false if the socket has been closed.
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ _RunUpdateOnce()

bool Leviathan::NetworkHandler::_RunUpdateOnce ( Lock guard)
protected

Returns false if the socket has been closed.

Definition at line 220 of file NetworkHandler.cpp.

221 {
222  sf::Packet receivedpacket;
223 
224  sf::IpAddress sender;
225  unsigned short sentport;
226 
228 
229  sf::Socket::Status status;
230 
231  while (true) {
232 
233  guard.unlock();
234 
235  {
236  auto lock = LockSocketForUse();
237  status = _Socket.receive(receivedpacket, sender, sentport);
238  }
239 
240  guard.lock();
241 
242  if(status != sf::Socket::Done)
243  break;
244 
245  // Process packet //
246 
247  // Pass to a connection //
248  bool Passed = false;
249 
250 
251  for (size_t i = 0; i < OpenConnections.size(); i++) {
252  // Keep passing until somebody handles it //
253  if(OpenConnections[i]->IsThisYours(sender, sentport)) {
254 
255  auto curconnection = OpenConnections[i];
256 
257  // Prevent deaclocks, TODO: make NetworkHandler only usable by the main thread
258  guard.unlock();
259  curconnection->HandlePacket(receivedpacket);
260  guard.lock();
261 
262  Passed = true;
263  break;
264  }
265  }
266 
267  if(Passed)
268  continue;
269 
270  shared_ptr<Connection> tmpconnect;
271 
272  // TODO: Check is it a close or a keep alive packet //
273 
274  // We might want to open a new connection to this client //
275  Logger::Get()->Info("Received a new connection from " + sender.toString() + ":" +
276  Convert::ToString(sentport));
277 
278  // \todo Make sure that the console won't be deleted between this and the actual check
279 
281  // Accept the connection //
282  LOG_WRITE("\t> Connection accepted");
283 
284  tmpconnect = OpenConnectionTo(guard, sender, sentport);
285 
286  } else if(rcon && rcon->IsAwaitingConnections()) {
287 
288  // We might allow a remote start remote console session //
289  LOG_WRITE("\t> Connection accepted for remote console receive");
290 
291  tmpconnect = OpenConnectionTo(guard, sender, sentport);
292 
293  // We need a special restriction for this connection //
294  if(tmpconnect)
295  tmpconnect->SetRestrictionMode(CONNECTION_RESTRICTION::ReceiveRemoteConsole);
296 
297  } else {
298  // Deny the connection //
299  LOG_WRITE("\t> Dropping connection due to not being a server "
300  "(and not expecting anything)");
301  continue;
302  }
303 
304  if(!tmpconnect) {
305 
306  LOG_WRITE("\t> Failed to create connection object");
307  continue;
308  }
309 
310  // Try to handle the packet //
311  if(!tmpconnect->IsThisYours(sender, sentport)) {
312  // That's an error //
313  Logger::Get()->Error("NetworkHandler: UpdateAllConnections: new connection "
314  "refused to process its packet from " + sender.toString() + ":" +
315  Convert::ToString(sentport));
316  CloseConnection(*tmpconnect);
317  } else {
318 
319  tmpconnect->HandlePacket(receivedpacket);
320  }
321  }
322 
323  return (status != sf::Socket::Error) && (status != sf::Socket::Disconnected);
324 }
DLLEXPORT bool IsAwaitingConnections()
Returns true if connections are marked as awaiting.
sf::UdpSocket _Socket
Main socket for listening for incoming packets and sending.
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
NETWORKED_TYPE AppType
Type of application.
#define LOG_WRITE(x)
Definition: Define.h:84
DLLEXPORT std::shared_ptr< Connection > OpenConnectionTo(const std::string &targetaddress)
Opens a new connection to the provided address.
static std::string ToString(const T &val)
Definition: Convert.h:72
RemoteConsole * GetRemoteConsole()
Definition: Engine.h:196
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
std::vector< std::shared_ptr< Connection > > OpenConnections
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:82
Class used to handle remote server commands and receiving messages.
Definition: RemoteConsole.h:45
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177
virtual DLLEXPORT void CloseConnection(Connection &connection)
Marks a connection as closing.

◆ _SaveMasterServerList()

void Leviathan::NetworkHandler::_SaveMasterServerList ( )
protected

Definition at line 363 of file NetworkHandler.cpp.

363  {
364 
365  // Set up the values //
366  vector<VariableBlock*> vals;
367  {
368  vals.reserve(MasterServers.size());
369 
370  // Only this scope requires locking //
371  GUARD_LOCK();
372 
373  for(size_t i = 0; i < MasterServers.size(); i++){
374  vals.push_back(new VariableBlock(*MasterServers[i].get()));
375  }
376  }
377 
378  NamedVars headervars;
379  headervars.Add(std::make_shared<NamedVariableList>("MasterServers", vals));
380 
381  ObjectFile file(headervars);
382 
384  Logger::Get());
385 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
static DLLEXPORT bool WriteObjectFile(ObjectFile &data, const std::string &file, LErrorReporter *reporterror)
Writes an ObjectFile&#39;s data structure to a file.
DLLEXPORT bool Add(std::shared_ptr< NamedVariableList > value)
Adds a value.
Definition: NamedVars.cpp:879
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
MasterServerInformation StoredMasterServerInfo
std::vector< std::shared_ptr< std::string > > MasterServers
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ CloseConnection()

DLLEXPORT void NetworkHandler::CloseConnection ( Connection connection)
virtual

Marks a connection as closing.

The connection will send a close packet and if provided a reason a reason packet, also. The connection will no longer be processed in update

Definition at line 493 of file NetworkHandler.cpp.

493  {
494 
496 
497  // Return if it is already queued //
498  for(auto& connection : ConnectionsToTerminate){
499  if(connection == &to)
500  return;
501  }
502 
503  ConnectionsToTerminate.push_back(&to);
504 }
Mutex ConnectionsToTerminateMutex
Closes connections on next update.
std::vector< Connection * > ConnectionsToTerminate
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:15

◆ DisconnectInterface()

DLLEXPORT void NetworkHandler::DisconnectInterface ( )
protected

Unhooks the NetworkInterfaces from this object.

This uses locking but the reading methods don't use locks. So this should only be called from the main thread

Definition at line 192 of file NetworkHandler.cpp.

192  {
193 
194  ServerInterface = nullptr;
195  ClientInterface = nullptr;
196 }
NetworkClientInterface * ClientInterface
NetworkServerInterface * ServerInterface

◆ GetCache()

NetworkCache* Leviathan::NetworkHandler::GetCache ( )
inline

Definition at line 120 of file NetworkHandler.h.

120  {
121 
122  return _NetworkCache.get();
123  }
std::unique_ptr< NetworkCache > _NetworkCache
Networked variable cache.

◆ GetClientInterface()

NetworkClientInterface* Leviathan::NetworkHandler::GetClientInterface ( )
inline

Definition at line 115 of file NetworkHandler.h.

115  {
116 
117  return ClientInterface;
118  }
NetworkClientInterface * ClientInterface

◆ GetConnection()

DLLEXPORT std::shared_ptr< Connection > NetworkHandler::GetConnection ( Connection directptr) const

Returns a persistent pointer to a connection.

Definition at line 554 of file NetworkHandler.cpp.

556 {
557  GUARD_LOCK();
558 
559  for(auto& connection : OpenConnections){
560 
561  if(connection.get() == directptr)
562  return connection;
563  }
564 
565  return nullptr;
566 }
std::vector< std::shared_ptr< Connection > > OpenConnections
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ GetInterface()

NetworkInterface* Leviathan::NetworkHandler::GetInterface ( )
inline

Returns interface object. Type depends on AppType.

Definition at line 105 of file NetworkHandler.h.

105  {
106 
108  return ClientInterface;
110  return ServerInterface;
111 
112  return nullptr;
113  }
NetworkClientInterface * ClientInterface
NETWORKED_TYPE AppType
Type of application.
NetworkServerInterface * ServerInterface

◆ GetNetworkType()

NETWORKED_TYPE Leviathan::NetworkHandler::GetNetworkType ( ) const
inline

Gets the type of network this program uses.

Will usually be NETWORKED_TYPE_CLIENT or NETWORKED_TYPE_SERVER

Definition at line 99 of file NetworkHandler.h.

99  {
100 
101  return AppType;
102  }
NETWORKED_TYPE AppType
Type of application.

◆ GetOurPort()

uint16_t Leviathan::NetworkHandler::GetOurPort ( ) const
inline

Returns the port to which our socket has been bind.

Definition at line 91 of file NetworkHandler.h.

91  {
92 
93  return PortNumber;
94  }
uint16_t PortNumber
Our local port number.

◆ GetServerAddressPartOfAddress()

DLLEXPORT string Leviathan::NetworkHandler::GetServerAddressPartOfAddress ( const std::string &  fulladdress,
const std::string &  regextouse = "http://.*?/" 
)
static

Retrieves base address from an http protocol URL For example if passed http://boostslair.com/Pong/MastersList.php returns http://boostslair.com/ //.

Definition at line 431 of file NetworkHandler.cpp.

433 {
434  // Create a regex //
435  regex findaddressregex(regextouse, regex_constants::ECMAScript | regex_constants::icase);
436 
437  match_results<const char*> addressmatch;
438 
439  // Return the match //
440  regex_search(fulladdress.c_str(), addressmatch, findaddressregex);
441 
442 
443  return string(addressmatch[1]);
444 }

◆ GetSyncedVariables()

SyncedVariables* Leviathan::NetworkHandler::GetSyncedVariables ( )
inline

Definition at line 125 of file NetworkHandler.h.

125  {
126 
127  return VariableSyncer.get();
128  }
std::unique_ptr< SyncedVariables > VariableSyncer
The syncable variable holder associated with this instance.

◆ Init() [1/2]

DLLEXPORT bool Leviathan::NetworkHandler::Init ( const MasterServerInformation info)
virtual

Definition at line 62 of file NetworkHandler.cpp.

62  {
63 
65 
67  // Query master server //
68  QueryMasterServer(info);
69 
70  } else {
71  // We are our own master! //
72 
73  // Get out port number here //
75 
76  int tmpport = 0;
77 
78  if(!vars->GetValueAndConvertTo<int>("MasterServerPort", tmpport)){
79  // This is quite bad //
80  Logger::Get()->Error("NetworkHandler: Init: no port configured, config missing "
81  "'MasterServerPort' of type int");
82  }
83 
84  PortNumber = (unsigned short)tmpport;
85  }
86 
88  // We can use any port we get //
89  PortNumber = sf::Socket::AnyPort;
90 
91  } else if(AppType == NETWORKED_TYPE::Server){
92  // We need to use a specific port //
94 
95  int tmpport = 0;
96 
97  if(!vars->GetValueAndConvertTo<int>("DefaultServerPort", tmpport)){
98  // This is quite bad //
99  Logger::Get()->Error("NetworkHandler: Init: no port configured, config missing "
100  "'ServerPort' of type int");
101  }
102 
103  PortNumber = (unsigned short)tmpport;
104  }
105 
106  // We want to receive responses //
107  if(_Socket.bind(PortNumber) != sf::Socket::Done){
108 
109  Logger::Get()->Error("NetworkHandler: Init: failed to bind to port "+
111  return false;
112  }
113 
114  PortNumber = _Socket.getLocalPort();
115 
116  // Set the socket as blocking //
117  if(BlockingMode) {
118 
119  _Socket.setBlocking(true);
120 
121  // Run the listening thread //
122  ListenerThread = std::thread(std::bind(&NetworkHandler::_RunListenerThread, this));
123  } else {
124 
125  _Socket.setBlocking(false);
126  }
127 
128  // Report success //
129  Logger::Get()->Info("NetworkHandler: running listening socket on port "+Convert::ToString(
130  _Socket.getLocalPort()));
131 
132  return true;
133 }
#define GAMECONFIGURATION_GET_VARIABLEACCESS(x)
sf::UdpSocket _Socket
Main socket for listening for incoming packets and sending.
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
DLLEXPORT std::shared_ptr< std::promise< std::string > > QueryMasterServer(const MasterServerInformation &info)
NETWORKED_TYPE AppType
Type of application.
bool BlockingMode
If true uses a blocking socket and async handling.
uint16_t PortNumber
Our local port number.
std::thread ListenerThread
Thread that constantly blocks on the socket and waits for packets.
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
void _RunListenerThread()
Constantly listens for packets in a blocked state.
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177
std::string MasterServerMustPassIdentification

◆ Init() [2/2]

DLLEXPORT bool Leviathan::NetworkHandler::Init ( uint16_t  port = 0)

Don't use this version. This just creates a socket with the port.

Definition at line 135 of file NetworkHandler.cpp.

135  {
136 
137  if(_Socket.bind(port) != sf::Socket::Done) {
138 
139  LOG_ERROR("NetworkHandler: Init: failed to bind to port " +
141  return false;
142  }
143 
144  PortNumber = _Socket.getLocalPort();
145 
146  _Socket.setBlocking(false);
147 
148  return true;
149 }
sf::UdpSocket _Socket
Main socket for listening for incoming packets and sending.
#define LOG_ERROR(x)
Definition: Define.h:83
uint16_t PortNumber
Our local port number.
static std::string ToString(const T &val)
Definition: Convert.h:72

◆ IsConnectionValid()

DLLEXPORT bool Leviathan::NetworkHandler::IsConnectionValid ( Connection connection) const

Checks whether a connection is open and ready for sending.

Note
Will check whether the connection is in Make sure it is in OpenConnections or not Which may be a waste of time if only the connection needs to be checked for openness

Definition at line 342 of file NetworkHandler.cpp.

342  {
343 
344  GUARD_LOCK();
345 
346  bool found = false;
347 
348  for (auto& connectioniter : OpenConnections) {
349 
350  if(connectioniter.get() == &connection) {
351 
352  found = true;
353  break;
354  }
355  }
356 
357  if(!found)
358  return false;
359 
360  return connection.IsValidForSend();
361 }
bool IsValidForSend() const
Returns true if this socket is valid for sending.
Definition: Connection.h:120
std::vector< std::shared_ptr< Connection > > OpenConnections
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ LockSocketForUse()

Lock Leviathan::NetworkHandler::LockSocketForUse ( )
protected

Definition at line 488 of file NetworkHandler.cpp.

488  {
489 
490  return Lock((SocketMutex));
491 }
Mutex SocketMutex
Used to control the locking of the socket.
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:15

◆ OpenConnectionTo() [1/3]

DLLEXPORT std::shared_ptr< Connection > Leviathan::NetworkHandler::OpenConnectionTo ( const std::string &  targetaddress)

Opens a new connection to the provided address.

Parameters
targetaddressThe input should be in a form that has address:port in it. The address should be like 'google.fi' or '192.168.1.1'
Note
This function doesn't verify that there actually is something on the target.
This will return an existing connection if one matches the targetaddress
The connection will be automatically closed if the remote doesn't properly respond to our open request

Definition at line 568 of file NetworkHandler.cpp.

570 {
571  if(targetaddress.empty())
572  return nullptr;
573 
574  GUARD_LOCK();
575 
576  // Find existing one //
577  for (auto& connection : OpenConnections) {
578 
579  if(connection->GetRawAddress() == targetaddress)
580  return connection;
581  }
582 
583  // Create new //
584  auto newconnection = std::make_shared<Connection>(targetaddress);
585 
586  // Initialize the connection //
587  if(!newconnection->Init(this)){
588 
589  return nullptr;
590  }
591 
592  OpenConnections.push_back(newconnection);
593 
594  return newconnection;
595 }
std::vector< std::shared_ptr< Connection > > OpenConnections
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ OpenConnectionTo() [2/3]

DLLEXPORT std::shared_ptr< Leviathan::Connection > Leviathan::NetworkHandler::OpenConnectionTo ( Lock guard,
const sf::IpAddress &  targetaddress,
unsigned short  port 
)

Variant for already resolved address.

See also
OpenConnectionTo

Definition at line 597 of file NetworkHandler.cpp.

599 {
600  // Find existing one //
601  for (auto& connection : OpenConnections) {
602 
603  if(connection->IsThisYours(targetaddress, port))
604  return connection;
605  }
606 
607  // Create new //
608  auto newconnection = std::make_shared<Connection>(targetaddress, port);
609 
610  // Initialize the connection //
611  if(!newconnection->Init(this)) {
612 
613  return nullptr;
614  }
615 
616  OpenConnections.push_back(newconnection);
617 
618  return newconnection;
619 }
std::vector< std::shared_ptr< Connection > > OpenConnections

◆ OpenConnectionTo() [3/3]

std::shared_ptr<Connection> Leviathan::NetworkHandler::OpenConnectionTo ( const sf::IpAddress &  targetaddress,
unsigned short  port 
)
inline

Definition at line 83 of file NetworkHandler.h.

85  {
86  GUARD_LOCK();
87  return OpenConnectionTo(guard, targetaddress, port);
88  }
DLLEXPORT std::shared_ptr< Connection > OpenConnectionTo(const std::string &targetaddress)
Opens a new connection to the provided address.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ QueryMasterServer()

DLLEXPORT std::shared_ptr< std::promise< string > > Leviathan::NetworkHandler::QueryMasterServer ( const MasterServerInformation info)

Definition at line 326 of file NetworkHandler.cpp.

327  {
328  // Copy the data //
329  StoredMasterServerInfo = info;
330 
331  shared_ptr<std::promise<string>> resultvalue(new std::promise<string>());
332 
333  // Make sure it doesn't die instantly //
335 
336  // Run the task async //
337  MasterServerConnectionThread = std::thread(RunGetResponseFromMaster, this, resultvalue);
338 
339  return resultvalue;
340 }
friend void RunGetResponseFromMaster(NetworkHandler *instance, std::shared_ptr< std::promise< std::string >> resultvar)
std::thread MasterServerConnectionThread
Makes sure that master server thread is graciously closed.
MasterServerInformation StoredMasterServerInfo

◆ Release()

DLLEXPORT void Leviathan::NetworkHandler::Release ( )
virtual

Definition at line 151 of file NetworkHandler.cpp.

151  {
152 
153  {
154  GUARD_LOCK();
155 
157 
158  // Kill master server connection //
159 
160  // Notify master server connection kill //
162 
163  MasterServerConnection->Release();
164  }
165 
166  // Close all connections //
167  for(auto& connection : OpenConnections){
168 
169  connection->Release();
170  }
171 
172  OpenConnections.clear();
173 
174 
175  }
176 
177  if(MasterServerConnectionThread.joinable()){
179  MasterServerConnection.reset();
180  }
181 
182  _ReleaseSocket();
183 
184  if(BlockingMode){
185  // This thread is blocked in infinite read //
186  //ListenerThread.join();
187  if(ListenerThread.joinable())
188  ListenerThread.detach();
189  }
190 }
std::shared_ptr< Connection > MasterServerConnection
Stores a "working" (meaning the server has responded something) master server address.
bool BlockingMode
If true uses a blocking socket and async handling.
std::thread ListenerThread
Thread that constantly blocks on the socket and waits for packets.
std::vector< std::shared_ptr< Connection > > OpenConnections
std::thread MasterServerConnectionThread
Makes sure that master server thread is graciously closed.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ ReleaseInputHandler()

DLLEXPORT void Leviathan::NetworkHandler::ReleaseInputHandler ( )

Destroys the networked input handler and all input objects.

Called by the engine when quitting the game

Definition at line 427 of file NetworkHandler.cpp.

427  {
428 
429 }

◆ RemoveClosedConnections()

DLLEXPORT void Leviathan::NetworkHandler::RemoveClosedConnections ( Lock guard)
virtual

Definition at line 506 of file NetworkHandler.cpp.

507 {
508  Lock terminatelock(ConnectionsToTerminateMutex);
509 
510  // Go through the removed connection list and remove closed connections //
511  for (size_t a = 0; a < OpenConnections.size(); ) {
512 
513  bool close = false;
514 
515  if(!OpenConnections[a]->IsValidForSend()) {
516 
517  close = true;
518 
519  } else {
520 
521  for (size_t i = 0; i < ConnectionsToTerminate.size(); i++) {
522 
523  if(OpenConnections[a].get() == ConnectionsToTerminate[i]) {
524 
525  close = true;
526  break;
527  }
528  }
529  }
530 
531  if(close) {
532 
533  auto& connection = OpenConnections[a];
534 
535  // Send a close packet //
536  connection->SendCloseConnectionPacket();
537 
538  // Close it //
539  connection->Release();
540 
541  connection.reset();
542 
543  OpenConnections.erase(OpenConnections.begin() + a);
544 
545  } else {
546 
547  a++;
548  }
549  }
550 
551  ConnectionsToTerminate.clear();
552 }
Mutex ConnectionsToTerminateMutex
Closes connections on next update.
std::vector< std::shared_ptr< Connection > > OpenConnections
std::vector< Connection * > ConnectionsToTerminate
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:15

◆ ShutdownCache()

DLLEXPORT void Leviathan::NetworkHandler::ShutdownCache ( )

Destroys the network cache permanently.

Called by the engine when quitting the game

Definition at line 421 of file NetworkHandler.cpp.

421  {
422 
423  if(_NetworkCache)
424  _NetworkCache->Release();
425 }
std::unique_ptr< NetworkCache > _NetworkCache
Networked variable cache.

◆ UpdateAllConnections()

DLLEXPORT void Leviathan::NetworkHandler::UpdateAllConnections ( )
virtual
Note
Call as often as possible to receive responses

Definition at line 459 of file NetworkHandler.cpp.

459  {
460 
461  // Update remote console sessions if they exist //
462  auto rconsole = Engine::Get()->GetRemoteConsole();
463  if(rconsole)
464  rconsole->UpdateStatus();
465  {
466  GUARD_LOCK();
467 
468  // Remove closed connections //
470 
471  // Do listening //
472  if(!BlockingMode) {
473 
474  _RunUpdateOnce(guard);
475  }
476 
477  // Time-out requests //
478  for (auto& connection : OpenConnections) {
479 
480  connection->UpdateListening();
481  }
482  }
483 
484  // Interface might want to do something //
485  GetInterface()->TickIt();
486 }
bool _RunUpdateOnce(Lock &guard)
Returns false if the socket has been closed.
DLLEXPORT void UpdateStatus()
Called before packets are handled.
NetworkInterface * GetInterface()
Returns interface object. Type depends on AppType.
bool BlockingMode
If true uses a blocking socket and async handling.
virtual DLLEXPORT void TickIt()=0
Should be used to update various network interfaces.
RemoteConsole * GetRemoteConsole()
Definition: Engine.h:196
std::vector< std::shared_ptr< Connection > > OpenConnections
virtual DLLEXPORT void RemoveClosedConnections(Lock &guard)
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:82
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

Friends And Related Function Documentation

◆ RunGetResponseFromMaster

void RunGetResponseFromMaster ( NetworkHandler instance,
std::shared_ptr< std::promise< std::string >>  resultvar 
)
friend

Member Data Documentation

◆ _GameSpecificPacketHandler

std::unique_ptr<GameSpecificPacketHandler> Leviathan::NetworkHandler::_GameSpecificPacketHandler
protected

Game specific packet handler that allows programs to register their own packets.

Definition at line 217 of file NetworkHandler.h.

◆ _NetworkCache

std::unique_ptr<NetworkCache> Leviathan::NetworkHandler::_NetworkCache
protected

Networked variable cache.

Definition at line 213 of file NetworkHandler.h.

◆ _Socket

sf::UdpSocket Leviathan::NetworkHandler::_Socket
protected

Main socket for listening for incoming packets and sending.

Definition at line 199 of file NetworkHandler.h.

◆ AppType

NETWORKED_TYPE Leviathan::NetworkHandler::AppType
protected

Type of application.

Definition at line 193 of file NetworkHandler.h.

◆ BlockingMode

bool Leviathan::NetworkHandler::BlockingMode = false
protected

If true uses a blocking socket and async handling.

Definition at line 204 of file NetworkHandler.h.

◆ ClientInterface

NetworkClientInterface* Leviathan::NetworkHandler::ClientInterface = nullptr
protected

Definition at line 196 of file NetworkHandler.h.

◆ CloseMasterServerConnection

bool Leviathan::NetworkHandler::CloseMasterServerConnection
protected

Definition at line 229 of file NetworkHandler.h.

◆ ConnectionsToTerminate

std::vector<Connection*> Leviathan::NetworkHandler::ConnectionsToTerminate
protected

Definition at line 188 of file NetworkHandler.h.

◆ ConnectionsToTerminateMutex

Mutex Leviathan::NetworkHandler::ConnectionsToTerminateMutex
protected

Closes connections on next update.

Definition at line 187 of file NetworkHandler.h.

◆ ListenerThread

std::thread Leviathan::NetworkHandler::ListenerThread
protected

Thread that constantly blocks on the socket and waits for packets.

Definition at line 232 of file NetworkHandler.h.

◆ MasterServerConnection

std::shared_ptr<Connection> Leviathan::NetworkHandler::MasterServerConnection
protected

Stores a "working" (meaning the server has responded something) master server address.

Definition at line 223 of file NetworkHandler.h.

◆ MasterServerConnectionThread

std::thread Leviathan::NetworkHandler::MasterServerConnectionThread
protected

Makes sure that master server thread is graciously closed.

Definition at line 228 of file NetworkHandler.h.

◆ MasterServerMustPassIdentification

std::string Leviathan::NetworkHandler::MasterServerMustPassIdentification
protected

Definition at line 234 of file NetworkHandler.h.

◆ MasterServers

std::vector<std::shared_ptr<std::string> > Leviathan::NetworkHandler::MasterServers
protected

Definition at line 220 of file NetworkHandler.h.

◆ OpenConnections

std::vector<std::shared_ptr<Connection> > Leviathan::NetworkHandler::OpenConnections
protected

Definition at line 190 of file NetworkHandler.h.

◆ PortNumber

uint16_t Leviathan::NetworkHandler::PortNumber
protected

Our local port number.

Definition at line 207 of file NetworkHandler.h.

◆ ServerInterface

NetworkServerInterface* Leviathan::NetworkHandler::ServerInterface = nullptr
protected

Definition at line 195 of file NetworkHandler.h.

◆ SocketMutex

Mutex Leviathan::NetworkHandler::SocketMutex
protected

Used to control the locking of the socket.

Definition at line 201 of file NetworkHandler.h.

◆ StoredMasterServerInfo

MasterServerInformation Leviathan::NetworkHandler::StoredMasterServerInfo
protected

Definition at line 225 of file NetworkHandler.h.

◆ VariableSyncer

std::unique_ptr<SyncedVariables> Leviathan::NetworkHandler::VariableSyncer
protected

The syncable variable holder associated with this instance.

Definition at line 210 of file NetworkHandler.h.


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