Leviathan  0.8.0.0
Leviathan game engine
Leviathan::NetworkServerInterface Class Reference

Class that encapsulates common networking functionality required by server programs. More...

#include <NetworkServerInterface.h>

+ Inheritance diagram for Leviathan::NetworkServerInterface:

Public Member Functions

DLLEXPORT NetworkServerInterface (int maxplayers, const std::string &servername, SERVER_JOIN_RESTRICT restricttype=SERVER_JOIN_RESTRICT::Localhost, int additionalflags=0)
 Initializes some values to defaults and requires others to be provided by the subclass that inherits from this. More...
 
virtual DLLEXPORT ~NetworkServerInterface ()
 Default destructor. More...
 
virtual DLLEXPORT void HandleRequestPacket (std::shared_ptr< NetworkRequest > request, Connection &connection) override
 Called by ConnectionInfo to handle incoming packets. More...
 
virtual DLLEXPORT void HandleResponseOnlyPacket (std::shared_ptr< NetworkResponse > message, Connection &connection) override
 Called by ConnectionInfo when it receives a response without a matching request object. More...
 
DLLEXPORT void TickIt () override
 Updates status of the status of the server's clients. More...
 
DLLEXPORT std::shared_ptr< ConnectedPlayerGetPlayerForConnection (Connection &connection)
 Gets corresponding player from a connection. More...
 
virtual DLLEXPORT std::vector< std::shared_ptr< Connection > > & GetClientConnections () override
 If this is a server returns all the clients. More...
 
DLLEXPORT void RespondToServerStatusRequest (std::shared_ptr< NetworkRequest > request, Connection &connectiontouse)
 Sends a response to a NETWORKREQUESTTYPE_SERVERSTATUS. More...
 
DLLEXPORT void SetServerStatus (SERVER_STATUS newstatus)
 Sets the status of the server to newstatus. More...
 
DLLEXPORT void SetServerAllowPlayers (bool allowingplayers)
 Sets whether the server allows new players. More...
 
virtual DLLEXPORT void CloseDown () override
 Call this before shutting down the server to kick all players properly. More...
 
DLLEXPORT void SendToAllButOnePlayer (const std::shared_ptr< NetworkResponse > &response, Connection *skipme, RECEIVE_GUARANTEE guarantee)
 Sends a response packet to all players except for the player(s) whose connection matches skipme. More...
 
DLLEXPORT void SendToAllPlayers (const std::shared_ptr< NetworkResponse > &response, RECEIVE_GUARANTEE guarantee)
 Sends a response packet to all of the players. More...
 
virtual DLLEXPORT void VerifyWorldIsSyncedWithPlayers (std::shared_ptr< GameWorld > world)
 Verifies that all current players are receiving world updates. More...
 
- Public Member Functions inherited from Leviathan::NetworkInterface
DLLEXPORT NetworkInterface (NETWORKED_TYPE type)
 
virtual DLLEXPORT ~NetworkInterface ()
 
virtual DLLEXPORT bool PreHandleResponse (std::shared_ptr< NetworkResponse > response, SentNetworkThing *originalrequest, Connection &connection)
 Called by ConnectionInfo to verify that a response is good. More...
 
virtual DLLEXPORT bool CanConnectionTerminate (Connection &connection)
 Called by Connection just before terminating an inactive connection. More...
 
DLLEXPORT void VerifyType (NETWORKED_TYPE type) const
 Asserts if types don't match. More...
 
NetworkHandlerGetOwner ()
 

Protected Member Functions

DLLEXPORT void _HandleServerJoinRequest (std::shared_ptr< NetworkRequest > request, Connection &connection)
 Used to handle server join request packets. More...
 
virtual DLLEXPORT void PlayerPreconnect (Connection &connection, std::shared_ptr< NetworkRequest > joinrequest)
 Called when a player is about to connect. More...
 
virtual DLLEXPORT void _OnPlayerConnected (std::shared_ptr< ConnectedPlayer > newplayer)
 
virtual DLLEXPORT void _OnPlayerDisconnect (std::shared_ptr< ConnectedPlayer > newplayer)
 
virtual DLLEXPORT bool PlayerPotentiallyKicked (ConnectedPlayer *player)
 
virtual DLLEXPORT void _OnCloseDown ()
 
virtual DLLEXPORT void RegisterCustomCommandHandlers (CommandHandler *addhere)
 Called when the application should register custom command handling providers. More...
 
virtual DLLEXPORT bool AllowPlayerConnectVeto (std::shared_ptr< NetworkRequest > request, Connection &connection, std::string &message)
 Called by _HandleServerJoinRequest to allow specific games to disallow players. More...
 
void _OnReportCloseConnection (std::shared_ptr< ConnectedPlayer > plyptr)
 
void _OnReportPlayerConnected (std::shared_ptr< ConnectedPlayer > plyptr, Connection &connection)
 Internally used to detect when a new player has connected. More...
 
- Protected Member Functions inherited from Leviathan::NetworkInterface
virtual bool _CustomHandleRequestPacket (std::shared_ptr< NetworkRequest > request, Connection &connection)
 
virtual bool _CustomHandleResponseOnlyPacket (std::shared_ptr< NetworkResponse > message, Connection &connection)
 
DLLEXPORT bool _HandleDefaultRequest (std::shared_ptr< NetworkRequest > request, Connection &connectiontosendresult)
 Utility function for subclasses to call for default handling. More...
 
DLLEXPORT bool _HandleDefaultResponseOnly (std::shared_ptr< NetworkResponse > message, Connection &connection)
 Utility function for subclasses to call for default handling of non-request responses. More...
 
DLLEXPORT void SetOwner (NetworkHandler *owner)
 

Protected Attributes

std::vector< std::shared_ptr< ConnectedPlayer > > ServerPlayers
 Holds the list of currently connected players. More...
 
std::vector< std::shared_ptr< Connection > > ServerPlayersConnections
 All the connections used in ServerPlayers. More...
 
int MaxPlayers = 20
 Maximum allowed player count. More...
 
std::vector< int > ActiveBots
 Currently active bots. More...
 
std::string ServerName = "Default Name"
 Name displayed in the server list. More...
 
bool AllowJoin = false
 
SERVER_JOIN_RESTRICT JoinRestrict = SERVER_JOIN_RESTRICT::Localhost
 Type of join restriction, defaults to NETWORKRESPONSE_SERVERJOINRESTRICT_NONE. More...
 
SERVER_STATUS ServerStatus = SERVER_STATUS::Starting
 
int ExtraServerFlags = 0
 This can contain anything the specific game wants. More...
 
int CurrentPlayerID = 1000
 Player ID counter for assigning unique ids for all players. More...
 
std::shared_ptr< CommandHandler_CommandHandler = nullptr
 The object used to handle all player submitted commands. More...
 
- Protected Attributes inherited from Leviathan::NetworkInterface
NETWORKED_TYPE OurNetworkType
 
NetworkHandlerOwner = nullptr
 

Detailed Description

Class that encapsulates common networking functionality required by server programs.

More specific version of NetworkInterface and should be included additionally in server network interface classes.

See also
NetworkInterface

Definition at line 25 of file NetworkServerInterface.h.

Constructor & Destructor Documentation

◆ NetworkServerInterface()

DLLEXPORT NetworkServerInterface::NetworkServerInterface ( int  maxplayers,
const std::string &  servername,
SERVER_JOIN_RESTRICT  restricttype = SERVER_JOIN_RESTRICT::Localhost,
int  additionalflags = 0 
)

Initializes some values to defaults and requires others to be provided by the subclass that inherits from this.

These can be later changed with various set functions

Parameters
maxplayersSets the initial maximum player count
servernameSets the server's name visible in various listings
restricttypeControls who can join the server
additionalflagsSets the application specific flags for this server

Definition at line 15 of file NetworkServerInterface.cpp.

17  :
19  MaxPlayers(maxplayers), ServerName(servername), JoinRestrict(restricttype),
20  ExtraServerFlags(additionalflags),
22 {
23 
24 }
int ExtraServerFlags
This can contain anything the specific game wants.
DLLEXPORT NetworkInterface(NETWORKED_TYPE type)
std::string ServerName
Name displayed in the server list.
std::shared_ptr< CommandHandler > _CommandHandler
The object used to handle all player submitted commands.
SERVER_JOIN_RESTRICT JoinRestrict
Type of join restriction, defaults to NETWORKRESPONSE_SERVERJOINRESTRICT_NONE.
int MaxPlayers
Maximum allowed player count.
Handles all commands sent by the players on the server.

◆ ~NetworkServerInterface()

DLLEXPORT NetworkServerInterface::~NetworkServerInterface ( )
virtual

Default destructor.

Definition at line 26 of file NetworkServerInterface.cpp.

26  {
27 
28  // Release the memory //
29  for(auto iter = ServerPlayers.begin(); iter != ServerPlayers.end(); ){
30 
31  // We can now report errors here as this needs to be deleted
32  // before engine is released
33  LOG_WARNING("Warning! server quitting while player list has data in it");
34 
35  iter = ServerPlayers.erase(iter);
36  }
37 }
std::vector< std::shared_ptr< ConnectedPlayer > > ServerPlayers
Holds the list of currently connected players.
#define LOG_WARNING(x)
Definition: Define.h:82

Member Function Documentation

◆ _HandleServerJoinRequest()

DLLEXPORT void NetworkServerInterface::_HandleServerJoinRequest ( std::shared_ptr< NetworkRequest request,
Connection connection 
)
protected

Used to handle server join request packets.

Todo:

Check connection security status

Add basic connection checking and master server authentication check

Definition at line 193 of file NetworkServerInterface.cpp.

196 {
197  if(!AllowJoin){
198 
199  ResponseServerDisallow response(request->GetIDForResponse(),
200  "Server is not accepting any players at this time",
202 
203  connection.SendPacketToConnection(response);
204  return;
205  }
206 
207  // Check is the player already connected //
208  if(GetPlayerForConnection(connection)) {
209 
210  ResponseServerDisallow response(request->GetIDForResponse(),
211  "You are already connected to this server, disconnect first",
213 
214  connection.SendPacketToConnection(response);
215  return;
216  }
217 
218  // Call this here, so this can potentially kick players for reserved slots //
219  PlayerPreconnect(connection, request);
220 
221  // Check if we can fit a new player //
222  if((int)(ServerPlayers.size()+1) > MaxPlayers){
223 
224  std::string plys = Convert::ToString(ServerPlayers.size());
225 
226  ResponseServerDisallow response(request->GetIDForResponse(),
227  "Server is at maximum capacity, " + plys + "/" + plys,
229 
230  connection.SendPacketToConnection(response);
231  return;
232  }
233 
234  // Connection security check //
235  if(connection.GetState() != CONNECTION_STATE::Authenticated) {
236 
237  ResponseServerDisallow response(request->GetIDForResponse(),
238  "Connection state is invalid",
240 
241  connection.SendPacketToConnection(response);
242  return;
243  }
244 
245  // Do something with join restrict things //
246 
247 
248  // Check if the program wants to veto this join //
249  std::string disallowmessage;
250 
251  if(!AllowPlayerConnectVeto(request, connection, disallowmessage)){
252 
253  ResponseServerDisallow response(request->GetIDForResponse(),
254  disallowmessage,
256 
257  connection.SendPacketToConnection(response);
258  return;
259  }
260 
261  // Player joined! //
262  int newid = ++CurrentPlayerID;
263 
264  ServerPlayers.push_back(std::make_shared<ConnectedPlayer>(
265  Owner->GetConnection(&connection), this, newid));
266 
267  _OnReportPlayerConnected(ServerPlayers.back(), connection);
268 
269  Logger::Get()->Info("NetworkServerInterface: accepted a new player, ID: "+
270  Convert::ToString(newid));
271 
272  // Send connection notification back to the client //
273  ResponseServerAllow response(request->GetIDForResponse(),
274  SERVER_ACCEPTED_TYPE::ConnectAccepted, "Allowed, ID: " +
275  Convert::ToString(newid));
276 
277  connection.SendPacketToConnection(response);
278 }
DLLEXPORT std::shared_ptr< SentRequest > SendPacketToConnection(const std::shared_ptr< NetworkRequest > &request, RECEIVE_GUARANTEE guarantee)
Definition: Connection.cpp:139
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
std::vector< std::shared_ptr< ConnectedPlayer > > ServerPlayers
Holds the list of currently connected players.
DLLEXPORT std::shared_ptr< ConnectedPlayer > GetPlayerForConnection(Connection &connection)
Gets corresponding player from a connection.
CONNECTION_STATE GetState() const
Definition: Connection.h:125
The server has used a custom rule to disallow this.
static std::string ToString(const T &val)
Definition: Convert.h:72
int MaxPlayers
Maximum allowed player count.
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
virtual DLLEXPORT void PlayerPreconnect(Connection &connection, std::shared_ptr< NetworkRequest > joinrequest)
Called when a player is about to connect.
int CurrentPlayerID
Player ID counter for assigning unique ids for all players.
DLLEXPORT std::shared_ptr< Connection > GetConnection(Connection *directptr) const
Returns a persistent pointer to a connection.
virtual DLLEXPORT bool AllowPlayerConnectVeto(std::shared_ptr< NetworkRequest > request, Connection &connection, std::string &message)
Called by _HandleServerJoinRequest to allow specific games to disallow players.
The client has already connected to the server, and must disconnect before trying again...
Server has accepted your join request.
void _OnReportPlayerConnected(std::shared_ptr< ConnectedPlayer > plyptr, Connection &connection)
Internally used to detect when a new player has connected.
Server has maximum number of players.

◆ _OnCloseDown()

virtual DLLEXPORT void Leviathan::NetworkServerInterface::_OnCloseDown ( )
inlineprotectedvirtual

Definition at line 109 of file NetworkServerInterface.h.

109 {}

◆ _OnPlayerConnected()

DLLEXPORT void NetworkServerInterface::_OnPlayerConnected ( std::shared_ptr< ConnectedPlayer newplayer)
protectedvirtual

Definition at line 280 of file NetworkServerInterface.cpp.

282 {
283 
284 }

◆ _OnPlayerDisconnect()

DLLEXPORT void NetworkServerInterface::_OnPlayerDisconnect ( std::shared_ptr< ConnectedPlayer newplayer)
protectedvirtual

Definition at line 286 of file NetworkServerInterface.cpp.

288 {
289 
290 }

◆ _OnReportCloseConnection()

void NetworkServerInterface::_OnReportCloseConnection ( std::shared_ptr< ConnectedPlayer plyptr)
protected

Internally called when a player is about to be deleted

Will call virtual notify functions

Definition at line 314 of file NetworkServerInterface.cpp.

314  {
315  // Close common interfaces that might be using this player //
316 
317  // Make sure player's connection is removed from client connections
318  for (auto iter = ServerPlayersConnections.begin(); iter != ServerPlayersConnections.end();
319  ++iter)
320  {
321  if((*iter) == plyptr->GetConnection()) {
322 
323  ServerPlayersConnections.erase(iter);
324  break;
325  }
326  }
327 
328  Logger::Get()->Info("NetworkServerInterface: player \"" + plyptr->GetNickname() +
329  "\" unconnected");
330 
331  _OnPlayerDisconnect(plyptr);
332 }
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
virtual DLLEXPORT void _OnPlayerDisconnect(std::shared_ptr< ConnectedPlayer > newplayer)
std::vector< std::shared_ptr< Connection > > ServerPlayersConnections
All the connections used in ServerPlayers.
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106

◆ _OnReportPlayerConnected()

void NetworkServerInterface::_OnReportPlayerConnected ( std::shared_ptr< ConnectedPlayer plyptr,
Connection connection 
)
protected

Internally used to detect when a new player has connected.

Definition at line 334 of file NetworkServerInterface.cpp.

336 {
337  Logger::Get()->Info("NetworkServerInterface: player \"" + plyptr->GetNickname() +
338  "\" connected");
339 
340  // Make sure player's connection is in client connections
341  ServerPlayersConnections.push_back((plyptr->GetConnection()));
342 
343  _OnPlayerConnected(plyptr);
344 }
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
virtual DLLEXPORT void _OnPlayerConnected(std::shared_ptr< ConnectedPlayer > newplayer)
std::vector< std::shared_ptr< Connection > > ServerPlayersConnections
All the connections used in ServerPlayers.
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106

◆ AllowPlayerConnectVeto()

DLLEXPORT bool NetworkServerInterface::AllowPlayerConnectVeto ( std::shared_ptr< NetworkRequest request,
Connection connection,
std::string &  message 
)
protectedvirtual

Called by _HandleServerJoinRequest to allow specific games to disallow players.

Returns
true to allow join false to disallow join
Parameters
messageThe error message to give back to the player

Definition at line 296 of file NetworkServerInterface.cpp.

298 {
299  return true;
300 }

◆ CloseDown()

DLLEXPORT void NetworkServerInterface::CloseDown ( )
overridevirtual

Call this before shutting down the server to kick all players properly.

Todo:
Actually call this, maybe make this an event listener

Implements Leviathan::NetworkInterface.

Definition at line 133 of file NetworkServerInterface.cpp.

133  {
134 
135  _OnCloseDown();
136 
137  // Prevent new players //
139  AllowJoin = false;
140 
141  for(auto iter = ServerPlayers.begin(); iter != ServerPlayers.end(); ){
142 
143  // Kick them //
144  (*iter)->OnKicked("Server closing");
145 
146  iter = ServerPlayers.erase(iter);
147  }
148 
149  _CommandHandler.reset();
150 }
std::vector< std::shared_ptr< ConnectedPlayer > > ServerPlayers
Holds the list of currently connected players.
std::shared_ptr< CommandHandler > _CommandHandler
The object used to handle all player submitted commands.

◆ GetClientConnections()

DLLEXPORT std::vector< std::shared_ptr< Leviathan::Connection > > & NetworkServerInterface::GetClientConnections ( )
overridevirtual

If this is a server returns all the clients.

Implements Leviathan::NetworkInterface.

Definition at line 167 of file NetworkServerInterface.cpp.

168 {
170 }
std::vector< std::shared_ptr< Connection > > ServerPlayersConnections
All the connections used in ServerPlayers.

◆ GetPlayerForConnection()

DLLEXPORT std::shared_ptr< ConnectedPlayer > NetworkServerInterface::GetPlayerForConnection ( Connection connection)

Gets corresponding player from a connection.

Returns
Returns a pointer from PlayerList

Definition at line 152 of file NetworkServerInterface.cpp.

154 {
155  // Search through the connections //
156 
157  for(size_t i = 0; i < ServerPlayers.size(); i++){
158  // Check with the pointer //
159  if(ServerPlayers[i]->IsConnectionYours(&connection))
160  return ServerPlayers[i];
161  }
162 
163  return nullptr;
164 }
std::vector< std::shared_ptr< ConnectedPlayer > > ServerPlayers
Holds the list of currently connected players.

◆ HandleRequestPacket()

DLLEXPORT void NetworkServerInterface::HandleRequestPacket ( std::shared_ptr< NetworkRequest request,
Connection connection 
)
overridevirtual

Called by ConnectionInfo to handle incoming packets.

This function is responsible for interpreting the packet data and generating a response. If the response could take a long time to generate it is recommended to queue a task to the ThreadingManager.

Note
The connection parameter shouldn't be stored directly since it can become invalid after this function returns. Get a shared pointer from NetworkHandler instead

Implements Leviathan::NetworkInterface.

Definition at line 39 of file NetworkServerInterface.cpp.

41 {
42  LEVIATHAN_ASSERT(request, "request is null");
43 
44  if(_HandleDefaultRequest(request, connection))
45  return;
46 
47  switch (request->GetType()) {
49  {
50  // Get the matching player //
51  auto ply = GetPlayerForConnection(connection);
52 
53  // Drop it if no matching players //
54  if(!ply)
55  return;
56 
57  // Send a response to the sender //
58  connection.SendPacketToConnection(std::make_shared<ResponseNone>(
60 
61  // Extract the command //
62  auto* data = static_cast<RequestRequestCommandExecution*>(request.get());
63 
64  // Execute it //
65  _CommandHandler->QueueCommand(data->Command, ply.get());
66 
67  return;
68  }
70  {
71  RespondToServerStatusRequest(request, connection);
72  return;
73  }
75  {
76  // Call handling function //
77  Logger::Get()->Info("NetworkServerInterface: player on " + connection.
78  GenerateFormatedAddressString() + "is trying to connect");
79 
80  _HandleServerJoinRequest(request, connection);
81  return;
82  }
83  default:
84  break;
85  }
86 
87  if(_CustomHandleRequestPacket(request, connection))
88  return;
89 
90  LOG_ERROR("NetworkServerInterface: failed to handle request of type: " +
91  Convert::ToString(static_cast<int>(request->GetType())));
92 }
DLLEXPORT std::shared_ptr< SentRequest > SendPacketToConnection(const std::shared_ptr< NetworkRequest > &request, RECEIVE_GUARANTEE guarantee)
Definition: Connection.cpp:139
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
#define LOG_ERROR(x)
Definition: Define.h:83
DLLEXPORT bool _HandleDefaultRequest(std::shared_ptr< NetworkRequest > request, Connection &connectiontosendresult)
Utility function for subclasses to call for default handling.
DLLEXPORT std::shared_ptr< ConnectedPlayer > GetPlayerForConnection(Connection &connection)
Gets corresponding player from a connection.
std::shared_ptr< CommandHandler > _CommandHandler
The object used to handle all player submitted commands.
Empty response, used for keeping alive/nothing.
static std::string ToString(const T &val)
Definition: Convert.h:72
virtual bool _CustomHandleRequestPacket(std::shared_ptr< NetworkRequest > request, Connection &connection)
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void RespondToServerStatusRequest(std::shared_ptr< NetworkRequest > request, Connection &connectiontouse)
Sends a response to a NETWORKREQUESTTYPE_SERVERSTATUS.
DLLEXPORT void _HandleServerJoinRequest(std::shared_ptr< NetworkRequest > request, Connection &connection)
Used to handle server join request packets.

◆ HandleResponseOnlyPacket()

DLLEXPORT void NetworkServerInterface::HandleResponseOnlyPacket ( std::shared_ptr< NetworkResponse message,
Connection connection 
)
overridevirtual

Called by ConnectionInfo when it receives a response without a matching request object.

This is called when the host on the connection sends a response without a matching request. Usually the other program instance wants us to do something without expecting a response, for example they could want us to add a new message to our inbox without expecting a response (other than an ack which is automatically sent) from us. The function can optionally ignore keepalive acks (to reduce spam between clients) by setting dontmarkasreceived as true. This function shouldn't throw any exceptions.

Implements Leviathan::NetworkInterface.

Definition at line 94 of file NetworkServerInterface.cpp.

96 {
97  LEVIATHAN_ASSERT(message, "message is null");
98 
99  if(_HandleDefaultResponseOnly(message, connection))
100  return;
101 
102  switch (message->GetType()) {
104  {
105  // Notify the matching player object about a heartbeat //
106  auto ply = GetPlayerForConnection(connection);
107 
108  if(!ply) {
109 
110  Logger::Get()->Warning("NetworkServerInterface: received a heartbeat packet "
111  "from a non-existing player");
112  return;
113  }
114 
115  ply->HeartbeatReceived();
116 
117  // Avoid spamming packets back //
118  //dontmarkasreceived = true;
119 
120  return;
121  }
122  default:
123  break;
124  }
125 
126  if(_CustomHandleResponseOnlyPacket(message, connection))
127  return;
128 
129  LOG_ERROR("NetworkServerInterface: failed to handle response of type: " +
130  Convert::ToString(static_cast<int>(message->GetType())));
131 }
#define LOG_ERROR(x)
Definition: Define.h:83
DLLEXPORT bool _HandleDefaultResponseOnly(std::shared_ptr< NetworkResponse > message, Connection &connection)
Utility function for subclasses to call for default handling of non-request responses.
DLLEXPORT std::shared_ptr< ConnectedPlayer > GetPlayerForConnection(Connection &connection)
Gets corresponding player from a connection.
virtual bool _CustomHandleResponseOnlyPacket(std::shared_ptr< NetworkResponse > message, Connection &connection)
DLLEXPORT void Warning(const std::string &data) override
Definition: Logger.cpp:190
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106

◆ PlayerPotentiallyKicked()

DLLEXPORT bool NetworkServerInterface::PlayerPotentiallyKicked ( ConnectedPlayer player)
protectedvirtual

Definition at line 292 of file NetworkServerInterface.cpp.

292  {
293  return true;
294 }

◆ PlayerPreconnect()

DLLEXPORT void NetworkServerInterface::PlayerPreconnect ( Connection connection,
std::shared_ptr< NetworkRequest joinrequest 
)
protectedvirtual

Called when a player is about to connect.

Definition at line 302 of file NetworkServerInterface.cpp.

304 {
305 
306 }

◆ RegisterCustomCommandHandlers()

DLLEXPORT void NetworkServerInterface::RegisterCustomCommandHandlers ( CommandHandler addhere)
protectedvirtual

Called when the application should register custom command handling providers.

Note
If you actually want to use this call to this should be added to your subclass constructor

Definition at line 308 of file NetworkServerInterface.cpp.

310 {
311 
312 }

◆ RespondToServerStatusRequest()

DLLEXPORT void NetworkServerInterface::RespondToServerStatusRequest ( std::shared_ptr< NetworkRequest request,
Connection connectiontouse 
)

Sends a response to a NETWORKREQUESTTYPE_SERVERSTATUS.

Definition at line 172 of file NetworkServerInterface.cpp.

174 {
175  // Gather info for a response //
176  ResponseServerStatus response(request->GetIDForResponse(), ServerName,
177  AllowJoin, JoinRestrict, ServerStatus, static_cast<int32_t>(ServerPlayers.size()),
178  MaxPlayers,
179  static_cast<int32_t>(ActiveBots.size()), ExtraServerFlags);
180 
181  // Send it //
182  connectiontouse.SendPacketToConnection(response);
183 }
int ExtraServerFlags
This can contain anything the specific game wants.
DLLEXPORT std::shared_ptr< SentRequest > SendPacketToConnection(const std::shared_ptr< NetworkRequest > &request, RECEIVE_GUARANTEE guarantee)
Definition: Connection.cpp:139
std::vector< std::shared_ptr< ConnectedPlayer > > ServerPlayers
Holds the list of currently connected players.
std::vector< int > ActiveBots
Currently active bots.
std::string ServerName
Name displayed in the server list.
SERVER_JOIN_RESTRICT JoinRestrict
Type of join restriction, defaults to NETWORKRESPONSE_SERVERJOINRESTRICT_NONE.
int MaxPlayers
Maximum allowed player count.

◆ SendToAllButOnePlayer()

DLLEXPORT void NetworkServerInterface::SendToAllButOnePlayer ( const std::shared_ptr< NetworkResponse > &  response,
Connection skipme,
RECEIVE_GUARANTEE  guarantee 
)

Sends a response packet to all players except for the player(s) whose connection matches skipme.

Definition at line 372 of file NetworkServerInterface.cpp.

375 {
376  // Loop the players and send to their connections //
377  for(auto iter = ServerPlayers.begin(); iter != ServerPlayers.end(); ++iter){
378 
379  Connection* curconnection = (*iter)->GetConnection().get();
380 
381  if(curconnection != skipme){
382 
383  curconnection->SendPacketToConnection(response, guarantee);
384  }
385  }
386 }
DLLEXPORT std::shared_ptr< SentRequest > SendPacketToConnection(const std::shared_ptr< NetworkRequest > &request, RECEIVE_GUARANTEE guarantee)
Definition: Connection.cpp:139
std::vector< std::shared_ptr< ConnectedPlayer > > ServerPlayers
Holds the list of currently connected players.
Class that handles a single connection to another instance.
Definition: Connection.h:104

◆ SendToAllPlayers()

DLLEXPORT void NetworkServerInterface::SendToAllPlayers ( const std::shared_ptr< NetworkResponse > &  response,
RECEIVE_GUARANTEE  guarantee 
)

Sends a response packet to all of the players.

Definition at line 388 of file NetworkServerInterface.cpp.

390 {
391  // Loop the players and send to their connections //
392  for(auto iter = ServerPlayers.begin(); iter != ServerPlayers.end(); ++iter){
393 
394  (*iter)->GetConnection()->SendPacketToConnection(response, guarantee);
395  }
396 }
std::vector< std::shared_ptr< ConnectedPlayer > > ServerPlayers
Holds the list of currently connected players.

◆ SetServerAllowPlayers()

DLLEXPORT void NetworkServerInterface::SetServerAllowPlayers ( bool  allowingplayers)

Sets whether the server allows new players.

Definition at line 189 of file NetworkServerInterface.cpp.

189  {
190  AllowJoin = allowingplayers;
191 }

◆ SetServerStatus()

DLLEXPORT void NetworkServerInterface::SetServerStatus ( SERVER_STATUS  newstatus)

Sets the status of the server to newstatus.

Definition at line 185 of file NetworkServerInterface.cpp.

185  {
186  ServerStatus = newstatus;
187 }

◆ TickIt()

DLLEXPORT void NetworkServerInterface::TickIt ( )
overridevirtual

Updates status of the status of the server's clients.

Note
Should be called by NetworkInterface::TickIt

Implements Leviathan::NetworkInterface.

Definition at line 346 of file NetworkServerInterface.cpp.

346  {
347 
348  // Check for closed connections //
349  auto end = ServerPlayers.end();
350  for(auto iter = ServerPlayers.begin(); iter != end; ){
351 
352  if((*iter)->IsConnectionClosed()){
353 
354  // The player has disconnected //
356 
357  iter = ServerPlayers.erase(iter);
358  // The end iterator is now also invalid //
359  end = ServerPlayers.end();
360  continue;
361  }
362 
363  (*iter)->UpdateHeartbeats();
364  ++iter;
365  }
366 
367 
368  // Update the command handling //
369  _CommandHandler->UpdateStatus();
370 }
std::vector< std::shared_ptr< ConnectedPlayer > > ServerPlayers
Holds the list of currently connected players.
void _OnReportCloseConnection(std::shared_ptr< ConnectedPlayer > plyptr)
std::shared_ptr< CommandHandler > _CommandHandler
The object used to handle all player submitted commands.

◆ VerifyWorldIsSyncedWithPlayers()

DLLEXPORT void NetworkServerInterface::VerifyWorldIsSyncedWithPlayers ( std::shared_ptr< GameWorld world)
virtual

Verifies that all current players are receiving world updates.

Note
Prior to calling this (if your players will move) you should bind positionable objects to the players for them to receive updates based on their location

Definition at line 398 of file NetworkServerInterface.cpp.

400 {
401  // We can safely add all players as they will only be added if they aren't there already //
402  for (auto iter = ServerPlayers.begin(); iter != ServerPlayers.end(); ++iter)
403  world->SetPlayerReceiveWorld(*iter);
404 }
std::vector< std::shared_ptr< ConnectedPlayer > > ServerPlayers
Holds the list of currently connected players.

Member Data Documentation

◆ _CommandHandler

std::shared_ptr<CommandHandler> Leviathan::NetworkServerInterface::_CommandHandler = nullptr
protected

The object used to handle all player submitted commands.

Definition at line 168 of file NetworkServerInterface.h.

◆ ActiveBots

std::vector<int> Leviathan::NetworkServerInterface::ActiveBots
protected

Currently active bots.

Definition at line 145 of file NetworkServerInterface.h.

◆ AllowJoin

bool Leviathan::NetworkServerInterface::AllowJoin = false
protected

Controls whether players can join Used to prevent players joining during start up

Definition at line 152 of file NetworkServerInterface.h.

◆ CurrentPlayerID

int Leviathan::NetworkServerInterface::CurrentPlayerID = 1000
protected

Player ID counter for assigning unique ids for all players.

Definition at line 165 of file NetworkServerInterface.h.

◆ ExtraServerFlags

int Leviathan::NetworkServerInterface::ExtraServerFlags = 0
protected

This can contain anything the specific game wants.

Definition at line 162 of file NetworkServerInterface.h.

◆ JoinRestrict

SERVER_JOIN_RESTRICT Leviathan::NetworkServerInterface::JoinRestrict = SERVER_JOIN_RESTRICT::Localhost
protected

Type of join restriction, defaults to NETWORKRESPONSE_SERVERJOINRESTRICT_NONE.

Definition at line 155 of file NetworkServerInterface.h.

◆ MaxPlayers

int Leviathan::NetworkServerInterface::MaxPlayers = 20
protected

Maximum allowed player count.

Definition at line 142 of file NetworkServerInterface.h.

◆ ServerName

std::string Leviathan::NetworkServerInterface::ServerName = "Default Name"
protected

Name displayed in the server list.

Definition at line 148 of file NetworkServerInterface.h.

◆ ServerPlayers

std::vector<std::shared_ptr<ConnectedPlayer> > Leviathan::NetworkServerInterface::ServerPlayers
protected

Holds the list of currently connected players.

Definition at line 137 of file NetworkServerInterface.h.

◆ ServerPlayersConnections

std::vector<std::shared_ptr<Connection> > Leviathan::NetworkServerInterface::ServerPlayersConnections
protected

All the connections used in ServerPlayers.

Definition at line 139 of file NetworkServerInterface.h.

◆ ServerStatus

SERVER_STATUS Leviathan::NetworkServerInterface::ServerStatus = SERVER_STATUS::Starting
protected

Server status, initially NETWORKRESPONSE_SERVERSTATUS_STARTING and should be set by the application when appropriate

Definition at line 159 of file NetworkServerInterface.h.


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