Leviathan  0.8.0.0
Leviathan game engine
Leviathan::NetworkInterface Class Referenceabstract

Class that encapsulates common networking functionality that is required by all networked programs. More...

#include <NetworkInterface.h>

+ Inheritance diagram for Leviathan::NetworkInterface:

Public Member Functions

DLLEXPORT NetworkInterface (NETWORKED_TYPE type)
 
virtual DLLEXPORT ~NetworkInterface ()
 
virtual DLLEXPORT std::vector< std::shared_ptr< Connection > > & GetClientConnections ()=0
 If this is a server returns all the clients. More...
 
virtual DLLEXPORT void HandleRequestPacket (const std::shared_ptr< NetworkRequest > &request, Connection &connection)=0
 Called by ConnectionInfo to handle incoming packets. More...
 
virtual DLLEXPORT bool PreHandleResponse (const std::shared_ptr< NetworkResponse > &response, SentNetworkThing *originalrequest, Connection &connection)
 Called by ConnectionInfo to verify that a response is good. More...
 
virtual DLLEXPORT void HandleResponseOnlyPacket (const std::shared_ptr< NetworkResponse > &message, Connection &connection)=0
 Called by ConnectionInfo when it receives a response without a matching request object. More...
 
virtual DLLEXPORT bool CanConnectionTerminate (Connection &connection)
 Called by Connection just before terminating an inactive connection. More...
 
virtual DLLEXPORT void TickIt ()=0
 Should be used to update various network interfaces. More...
 
virtual DLLEXPORT void CloseDown ()=0
 Called when the program is closing. More...
 
DLLEXPORT void VerifyType (NETWORKED_TYPE type) const
 Asserts if types don't match. More...
 
NetworkHandlerGetOwner ()
 

Protected Member Functions

virtual bool _CustomHandleRequestPacket (const std::shared_ptr< NetworkRequest > &request, Connection &connection)
 
virtual bool _CustomHandleResponseOnlyPacket (const std::shared_ptr< NetworkResponse > &message, Connection &connection)
 
DLLEXPORT bool _HandleDefaultRequest (const std::shared_ptr< NetworkRequest > &request, Connection &connectiontosendresult)
 Utility function for subclasses to call for default handling. More...
 
DLLEXPORT bool _HandleDefaultResponseOnly (const 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

NETWORKED_TYPE OurNetworkType
 
NetworkHandlerOwner = nullptr
 

Detailed Description

Class that encapsulates common networking functionality that is required by all networked programs.

See also
NetworkServerInterface NetworkClientInterface

Definition at line 18 of file NetworkInterface.h.

Constructor & Destructor Documentation

◆ NetworkInterface()

DLLEXPORT NetworkInterface::NetworkInterface ( NETWORKED_TYPE  type)

Definition at line 16 of file NetworkInterface.cpp.

16 : OurNetworkType(type) {}

◆ ~NetworkInterface()

DLLEXPORT NetworkInterface::~NetworkInterface ( )
virtual

Definition at line 18 of file NetworkInterface.cpp.

18 {}

Member Function Documentation

◆ _CustomHandleRequestPacket()

virtual bool Leviathan::NetworkInterface::_CustomHandleRequestPacket ( const std::shared_ptr< NetworkRequest > &  request,
Connection connection 
)
inlineprotectedvirtual

Definition at line 93 of file NetworkInterface.h.

95  {
96  return false;
97  }

◆ _CustomHandleResponseOnlyPacket()

virtual bool Leviathan::NetworkInterface::_CustomHandleResponseOnlyPacket ( const std::shared_ptr< NetworkResponse > &  message,
Connection connection 
)
inlineprotectedvirtual

Definition at line 99 of file NetworkInterface.h.

101  {
102  return false;
103  }

◆ _HandleDefaultRequest()

bool NetworkInterface::_HandleDefaultRequest ( const std::shared_ptr< NetworkRequest > &  request,
Connection connectiontosendresult 
)
protected

Utility function for subclasses to call for default handling.

Handles default types of request packages and returns true if processed.

Definition at line 38 of file NetworkInterface.cpp.

40 {
41  // Switch based on type //
42 
43  // See if it is a sync packet //
44  if(Owner->GetSyncedVariables()->HandleSyncRequests(request, &connection))
45  return true;
46 
47  switch(request->GetType()) {
49  // Avoid allowing DDOS amplification
50  const uint32_t maxlength = static_cast<uint32_t>(
51  static_cast<RequestIdentification*>(request.get())->DDOSBlock.size());
52 
53  // Fetch the data from the configuration object //
54  std::string userreadable, gamename, gameversion;
55 
56  AppDef::GetDefault()->GetGameIdentificationData(userreadable, gamename, gameversion);
57 
58  auto response = ResponseIdentification(
59  0, userreadable, gamename, gameversion, LEVIATHAN_VERSION_ANSIS);
60 
61  NetworkResponse::LimitResponseSize(response, maxlength);
62 
63  // Should be critical, but it is not to avoid DDoS amplification
64  connection.SendPacketToConnection(response);
65 
66  return true;
67  }
69  // Send an empty response back //
70  connection.SendPacketToConnection(
71  std::make_shared<ResponseNone>(NETWORK_RESPONSE_TYPE::None),
73 
74  return true;
75  }
79  request, Owner->GetConnection(&connection));
80 
81  return true;
82  }
84  DEBUG_BREAK;
85  // if (!Owner->GetInputHandler())
86  // return false;
87 
88  // Owner->GetInputHandler()->HandleInputPacket(request, connection);
89 
90  return true;
91  }
92  default: return false;
93  }
94 
95  // Unhandled //
96  return false;
97 }
DLLEXPORT void HandleRemoteConsoleRequestPacket(std::shared_ptr< NetworkRequest > request, std::shared_ptr< Connection > connection)
Packet is sent once and if lost won&#39;t be resent.
Sent when a player requests the server to connect a NetworkedInput.
#define LEVIATHAN_VERSION_ANSIS
Definition: Include.h:56
DLLEXPORT bool HandleSyncRequests(std::shared_ptr< NetworkRequest > request, Connection *connection)
Handles all requests aimed at the synchronized variables.
SyncedVariables * GetSyncedVariables()
DLLEXPORT void GetGameIdentificationData(std::string &userreadable, std::string &gamename, std::string &gameversion)
Definition: AppDefine.cpp:188
Empty response, used for keeping alive/nothing.
RemoteConsole * GetRemoteConsole()
Definition: Engine.h:229
DLLEXPORT std::shared_ptr< Connection > GetConnection(Connection *directptr) const
Returns a persistent pointer to a connection.
Sent by servers to ping (time the time a client takes to respond) clients.
static DLLEXPORT void LimitResponseSize(ResponseIdentification &response, uint32_t maxsize)
Limits size of response to avoid the application being used for DDoS amplification.
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85
unsigned int uint32_t
Definition: core.h:40
static DLLEXPORT AppDef * GetDefault()
Definition: AppDefine.h:112

◆ _HandleDefaultResponseOnly()

bool NetworkInterface::_HandleDefaultResponseOnly ( const std::shared_ptr< NetworkResponse > &  message,
Connection connection 
)
protected

Utility function for subclasses to call for default handling of non-request responses.

Handles default types of response packages and returns true if processed.

Definition at line 99 of file NetworkInterface.cpp.

101 {
102  // See if it is a sync packet //
103  if(Owner->GetSyncedVariables()->HandleResponseOnlySync(message, &connection))
104  return true;
105 
106  // Switch on type //
107  switch(message->GetType()) {
110  // Empty packets without a matching request are just ignored,
111  // but marked as received
112  return true;
113  }
115  // This connection should be closed //
116  Logger::Get()->Info("NetworkInterface: dropping connection due to "
117  "receiving a connection close packet (" +
118  connection.GenerateFormatedAddressString() + ")");
119 
120  Owner->CloseConnection(connection);
121  return true;
122  }
125  // Pass to remote console //
127  message, connection, NULL);
128  return true;
129  }
132  DEBUG_BREAK;
133 
134  // if (!Owner->GetInputHandler())
135  // return false;
136 
137  // Owner->GetInputHandler()->HandleInputPacket(request, connection);
138 
139  return true;
140  }
141 
142  default: return false;
143  }
144 
145  // Not handled //
146  return false;
147 }
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
SyncedVariables * GetSyncedVariables()
DLLEXPORT void HandleRemoteConsoleResponse(std::shared_ptr< NetworkResponse > response, Connection &connection, std::shared_ptr< NetworkRequest > potentialrequest)
DLLEXPORT bool HandleResponseOnlySync(std::shared_ptr< NetworkResponse > response, Connection *connection)
Handles a response only packet, if it is a sync packet.
Empty response, used for keeping alive/nothing.
DLLEXPORT std::string GenerateFormatedAddressString() const
Returns a nicely formated address string for this connection.
RemoteConsole * GetRemoteConsole()
Definition: Engine.h:229
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
Contains control state updates regarding a NetworkedInput.
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85
virtual DLLEXPORT void CloseConnection(Connection &connection)
Marks a connection as closing.

◆ CanConnectionTerminate()

DLLEXPORT bool NetworkInterface::CanConnectionTerminate ( Connection connection)
virtual

Called by Connection just before terminating an inactive connection.

Return true if you want to allow the connection to close (it will close before next packet receive update) By default will allow all requesting connections to terminate.

Definition at line 149 of file NetworkInterface.cpp.

150 {
151  // By default allow connections to close //
152  return true;
153 }

◆ CloseDown()

virtual DLLEXPORT void Leviathan::NetworkInterface::CloseDown ( )
pure virtual

◆ GetClientConnections()

virtual DLLEXPORT std::vector<std::shared_ptr<Connection> >& Leviathan::NetworkInterface::GetClientConnections ( )
pure virtual

If this is a server returns all the clients.

Implemented in Leviathan::NetworkClientInterface, Leviathan::NetworkServerInterface, and Leviathan::NetworkMasterServerInterface.

◆ GetOwner()

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

Definition at line 86 of file NetworkInterface.h.

87  {
88 
89  return Owner;
90  }

◆ HandleRequestPacket()

DLLEXPORT void NetworkInterface::HandleRequestPacket ( const std::shared_ptr< NetworkRequest > &  request,
Connection connection 
)
pure virtual

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

Implemented in Leviathan::NetworkClientInterface, Leviathan::NetworkServerInterface, and Leviathan::NetworkMasterServerInterface.

Definition at line 27 of file NetworkInterface.cpp.

29 {
30  // We can only try the default handle function //
31  if(!_HandleDefaultRequest(request, connection)) {
32  // We couldn't handle it //
33 
34  throw InvalidArgument("could not handle request with default handler");
35  }
36 }
DLLEXPORT bool _HandleDefaultRequest(const std::shared_ptr< NetworkRequest > &request, Connection &connectiontosendresult)
Utility function for subclasses to call for default handling.

◆ HandleResponseOnlyPacket()

virtual DLLEXPORT void Leviathan::NetworkInterface::HandleResponseOnlyPacket ( const std::shared_ptr< NetworkResponse > &  message,
Connection connection 
)
pure virtual

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.

Implemented in Leviathan::NetworkClientInterface, Leviathan::NetworkServerInterface, and Leviathan::NetworkMasterServerInterface.

◆ PreHandleResponse()

DLLEXPORT bool NetworkInterface::PreHandleResponse ( const std::shared_ptr< NetworkResponse > &  response,
SentNetworkThing originalrequest,
Connection connection 
)
virtual

Called by ConnectionInfo to verify that a response is good.

By default this will always return true, but that can be overloaded to return false if the response is no good When returning false the connection will pretend that the response never arrived and possibly resends the request

Definition at line 20 of file NetworkInterface.cpp.

23 {
24  return true;
25 }

◆ SetOwner()

DLLEXPORT void NetworkInterface::SetOwner ( NetworkHandler owner)
protected

Definition at line 160 of file NetworkInterface.cpp.

161 {
162  Owner = owner;
163 }

◆ TickIt()

virtual DLLEXPORT void Leviathan::NetworkInterface::TickIt ( )
pure virtual

Should be used to update various network interfaces.

Note
This NetworkInterface doesn't need any ticking, but NetworkClientInterface does
See also
NetworkClientInterface::UpdateClientStatus

Implemented in Leviathan::NetworkClientInterface, Leviathan::NetworkServerInterface, and Leviathan::NetworkMasterServerInterface.

◆ VerifyType()

DLLEXPORT void NetworkInterface::VerifyType ( NETWORKED_TYPE  type) const

Asserts if types don't match.

Definition at line 155 of file NetworkInterface.cpp.

156 {
157  LEVIATHAN_ASSERT(type == OurNetworkType, "NetworkInterface::VerifyType doesn't match");
158 }
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:98

Member Data Documentation

◆ OurNetworkType

NETWORKED_TYPE Leviathan::NetworkInterface::OurNetworkType
protected

Definition at line 122 of file NetworkInterface.h.

◆ Owner

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

Definition at line 123 of file NetworkInterface.h.


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