Leviathan  0.8.0.0
Leviathan game engine
NetworkHandler.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 "CommonNetwork.h"
8 #include "MasterServerInfo.h"
10 #include "NetworkInterface.h"
11 #include "NetworkServerInterface.h"
12 
13 #include "Connection.h"
14 
15 
16 #include "Common/ThreadSafe.h"
17 
18 #include "SFML/Network/UdpSocket.hpp"
19 
20 #include <future>
21 #include <memory>
22 #include <thread>
23 
24 namespace Leviathan {
25 
26 class Engine;
27 
29  NetworkHandler* instance, std::shared_ptr<std::promise<std::string>> resultvar);
30 
32 class NetworkHandler : public ThreadSafe {
33  friend void RunGetResponseFromMaster(
34  NetworkHandler* instance, std::shared_ptr<std::promise<std::string>> resultvar);
35 
36  friend Connection;
37  friend Engine;
38 
39 public:
40  // Either a client or a server handler //
42  DLLEXPORT virtual ~NetworkHandler();
43 
44  DLLEXPORT virtual bool Init(const MasterServerInformation& info);
45 
47  DLLEXPORT bool Init(uint16_t port = 0);
48 
49  // \note This waits for all connections to terminate
50  DLLEXPORT virtual void Release();
51 
53  DLLEXPORT virtual void UpdateAllConnections();
54 
55  DLLEXPORT virtual void RemoveClosedConnections(Lock& guard);
56 
57  DLLEXPORT std::shared_ptr<std::promise<std::string>> QueryMasterServer(
58  const MasterServerInformation& info);
59 
63  DLLEXPORT bool IsConnectionValid(Connection& connection) const;
64 
66  DLLEXPORT std::shared_ptr<Connection> GetConnection(Connection* directptr) const;
67 
76  DLLEXPORT std::shared_ptr<Connection> OpenConnectionTo(const std::string& targetaddress);
77 
80  DLLEXPORT std::shared_ptr<Connection> OpenConnectionTo(
81  Lock& guard, const sf::IpAddress& targetaddress, unsigned short port);
82 
83  inline std::shared_ptr<Connection> OpenConnectionTo(
84  const sf::IpAddress& targetaddress, unsigned short port)
85  {
86  GUARD_LOCK();
87  return OpenConnectionTo(guard, targetaddress, port);
88  }
89 
91  inline uint16_t GetOurPort() const
92  {
93  return PortNumber;
94  }
95 
100  {
101  return AppType;
102  }
103 
104  inline const char* GetNetworkTypeStr() const
105  {
107  return "client";
109  return "server";
111  return "master";
112 
113  return "unknown";
114  }
115 
118  {
120  return ClientInterface;
122  return ServerInterface;
123 
124  return nullptr;
125  }
126 
128  {
129 
130  return ClientInterface;
131  }
132 
134  {
135 
136  return _NetworkCache.get();
137  }
138 
140  {
141 
142  return VariableSyncer.get();
143  }
144 
148  DLLEXPORT void ShutdownCache();
149 
154 
159  DLLEXPORT virtual void CloseConnection(Connection& connection);
160 
161  // Common network functions //
165  DLLEXPORT static std::string GetServerAddressPartOfAddress(
166  const std::string& fulladdress, const std::string& regextouse = "http://.*?/");
167 
168 
171  DLLEXPORT void _RegisterConnection(std::shared_ptr<Connection> connection);
172 
173 
174 protected:
180 
181 
183 
184  // Closes the socket //
185  void _ReleaseSocket();
186 
188  bool _RunUpdateOnce(Lock& guard);
189 
191  void _RunListenerThread();
192 
193  void _SaveMasterServerList();
194  bool _LoadMasterServerList();
195 
196  // ------------------------------------ //
197 
198  // Internal listing of all connections //
199 
202  std::vector<Connection*> ConnectionsToTerminate;
203 
204  std::vector<std::shared_ptr<Connection>> OpenConnections;
205 
208 
211 
213  sf::UdpSocket _Socket;
216 
218  bool BlockingMode = false;
219 
222 
224  std::unique_ptr<SyncedVariables> VariableSyncer;
225 
227  std::unique_ptr<NetworkCache> _NetworkCache;
228 
229 
231  std::unique_ptr<GameSpecificPacketHandler> _GameSpecificPacketHandler;
232 
233  // The master server list //
234  std::vector<std::shared_ptr<std::string>> MasterServers;
235 
237  std::shared_ptr<Connection> MasterServerConnection;
238 
240 
244 
246  std::thread ListenerThread;
247 
249 };
250 
251 } // namespace Leviathan
const char * GetNetworkTypeStr() const
DLLEXPORT bool IsConnectionValid(Connection &connection) const
Checks whether a connection is open and ready for sending.
Class that synchronizes some key variables with another instance.
std::unique_ptr< NetworkCache > _NetworkCache
Networked variable cache.
std::shared_ptr< Connection > MasterServerConnection
Stores a "working" (meaning the server has responded something) master server address.
DLLEXPORT void ShutdownCache()
Destroys the network cache permanently.
virtual DLLEXPORT void UpdateAllConnections()
sf::UdpSocket _Socket
Main socket for listening for incoming packets and sending.
Allows the inherited object to be locked.
Definition: ThreadSafe.h:137
DLLEXPORT void DisconnectInterface()
Unhooks the NetworkInterfaces from this object.
bool _RunUpdateOnce(Lock &guard)
Returns false if the socket has been closed.
NetworkClientInterface * ClientInterface
NetworkClientInterface * GetClientInterface()
DLLEXPORT std::shared_ptr< std::promise< std::string > > QueryMasterServer(const MasterServerInformation &info)
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....
NETWORKED_TYPE AppType
Type of application.
NetworkInterface * GetInterface()
Returns interface object. Type depends on AppType.
friend void RunGetResponseFromMaster(NetworkHandler *instance, std::shared_ptr< std::promise< std::string >> resultvar)
bool BlockingMode
If true uses a blocking socket and async handling.
The main class of the Leviathan Game Engine.
Definition: Engine.h:37
void RunGetResponseFromMaster(NetworkHandler *instance, std::shared_ptr< std::promise< std::string >> resultvar)
SyncedVariables * GetSyncedVariables()
DLLEXPORT NetworkHandler(NETWORKED_TYPE ntype, NetworkInterface *packethandler)
uint16_t PortNumber
Our local port number.
DLLEXPORT void _RegisterConnection(std::shared_ptr< Connection > connection)
std::unique_ptr< GameSpecificPacketHandler > _GameSpecificPacketHandler
Game specific packet handler that allows programs to register their own packets.
std::thread ListenerThread
Thread that constantly blocks on the socket and waits for packets.
DLLEXPORT std::shared_ptr< Connection > OpenConnectionTo(const std::string &targetaddress)
Opens a new connection to the provided address.
NETWORKED_TYPE
Type of networked application.
Definition: CommonNetwork.h:22
unsigned short uint16_t
Definition: core.h:39
std::mutex Mutex
Definition: ThreadSafe.h:16
Centralized variables that AI can use on clients to replicate server behavior.
Definition: NetworkCache.h:21
Mutex ConnectionsToTerminateMutex
Closes connections on next update.
std::vector< std::shared_ptr< Connection > > OpenConnections
virtual DLLEXPORT ~NetworkHandler()
virtual DLLEXPORT void RemoveClosedConnections(Lock &guard)
DLLEXPORT std::shared_ptr< Connection > GetConnection(Connection *directptr) const
Returns a persistent pointer to a connection.
std::thread MasterServerConnectionThread
Makes sure that master server thread is graciously closed.
Class that handles a single connection to another instance.
Definition: Connection.h:105
virtual DLLEXPORT void Release()
MasterServerInformation StoredMasterServerInfo
void _RunListenerThread()
Constantly listens for packets in a blocked state.
NETWORKED_TYPE GetNetworkType() const
Gets the type of network this program uses.
NetworkServerInterface * ServerInterface
#define DLLEXPORT
Definition: Include.h:84
uint16_t GetOurPort() const
Returns the port to which our socket has been bind.
std::vector< std::shared_ptr< std::string > > MasterServers
Class that encapsulates common networking functionality required by server programs.
Mutex SocketMutex
Used to control the locking of the socket.
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
DLLEXPORT void ReleaseInputHandler()
Destroys the networked input handler and all input objects.
std::vector< Connection * > ConnectionsToTerminate
NetworkCache * GetCache()
Handles everything related to connections.
virtual DLLEXPORT void CloseConnection(Connection &connection)
Marks a connection as closing.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
Class that encapsulates common networking functionality required by client programs.
std::shared_ptr< Connection > OpenConnectionTo(const sf::IpAddress &targetaddress, unsigned short port)
std::unique_ptr< SyncedVariables > VariableSyncer
The syncable variable holder associated with this instance.
Class that encapsulates common networking functionality that is required by all networked programs.
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:18
virtual DLLEXPORT bool Init(const MasterServerInformation &info)
std::string MasterServerMustPassIdentification