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 (std::shared_ptr< NetworkRequest > request, Connection &connection)=0
 Called by ConnectionInfo to handle incoming packets. More...
 
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 void HandleResponseOnlyPacket (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 (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

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 17 of file NetworkInterface.h.

Constructor & Destructor Documentation

◆ NetworkInterface()

DLLEXPORT NetworkInterface::NetworkInterface ( NETWORKED_TYPE  type)

Definition at line 16 of file NetworkInterface.cpp.

16  :
17  OurNetworkType(type)
18 {
19 
20 }

◆ ~NetworkInterface()

DLLEXPORT NetworkInterface::~NetworkInterface ( )
virtual

Definition at line 22 of file NetworkInterface.cpp.

22  {
23 
24 }

Member Function Documentation

◆ _CustomHandleRequestPacket()

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

Definition at line 92 of file NetworkInterface.h.

94  {
95  return false;
96  }

◆ _CustomHandleResponseOnlyPacket()

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

Definition at line 98 of file NetworkInterface.h.

100  {
101  return false;
102  }

◆ _HandleDefaultRequest()

bool NetworkInterface::_HandleDefaultRequest ( 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 42 of file NetworkInterface.cpp.

44 {
45  // Switch based on type //
46 
47  // See if it is a sync packet //
48  if(Owner->GetSyncedVariables()->HandleSyncRequests(request, &connection))
49  return true;
50 
51  switch(request->GetType()){
53  {
54  // Avoid allowing DDOS amplification
55  const uint32_t maxlength = static_cast<uint32_t>(
56  static_cast<RequestIdentification*>(request.get())->DDOSBlock.size());
57 
58  // Fetch the data from the configuration object //
59  std::string userreadable, gamename, gameversion;
60 
61  AppDef::GetDefault()->GetGameIdentificationData(userreadable, gamename,
62  gameversion);
63 
64  auto response = ResponseIdentification(0, userreadable, gamename,
65  gameversion, LEVIATHAN_VERSION_ANSIS);
66 
67  NetworkResponse::LimitResponseSize(response, maxlength);
68 
69  // Should be critical, but it is not to avoid DDoS amplification
70  connection.SendPacketToConnection(response);
71 
72  return true;
73  }
75  {
76  // Send an empty response back //
77  connection.SendPacketToConnection(std::make_shared<ResponseNone>(
80 
81  return true;
82  }
85  {
87  Owner->GetConnection(&connection));
88 
89  return true;
90  }
92  {
93  DEBUG_BREAK;
94  //if (!Owner->GetInputHandler())
95  // return false;
96 
97  //Owner->GetInputHandler()->HandleInputPacket(request, connection);
98 
99  return true;
100  }
101  default:
102  return false;
103  }
104 
105  // Unhandled //
106  return false;
107 }
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:90
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:165
Empty response, used for keeping alive/nothing.
RemoteConsole * GetRemoteConsole()
Definition: Engine.h:196
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:82
unsigned int uint32_t
Definition: core.h:40
static DLLEXPORT AppDef * GetDefault()
Definition: AppDefine.h:112

◆ _HandleDefaultResponseOnly()

bool NetworkInterface::_HandleDefaultResponseOnly ( 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 109 of file NetworkInterface.cpp.

111 {
112  // See if it is a sync packet //
113  if(Owner->GetSyncedVariables()->HandleResponseOnlySync(message, &connection))
114  return true;
115 
116  // Switch on type //
117  switch(message->GetType()){
120  {
121  // Empty packets without a matching request are just ignored,
122  // but marked as received
123  return true;
124  }
126  {
127  // This connection should be closed //
128  Logger::Get()->Info("NetworkInterface: dropping connection due to "
129  "receiving a connection close packet (" +
130  connection.GenerateFormatedAddressString() + ")");
131 
132  Owner->CloseConnection(connection);
133  return true;
134  }
137  {
138  // Pass to remote console //
140  connection, NULL);
141  return true;
142  }
145  {
146  DEBUG_BREAK;
147 
148  //if (!Owner->GetInputHandler())
149  // return false;
150 
151  //Owner->GetInputHandler()->HandleInputPacket(request, connection);
152 
153  return true;
154  }
155 
156  default:
157  return false;
158  }
159  // Not handled //
160  return false;
161 }
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.
Definition: Connection.cpp:930
RemoteConsole * GetRemoteConsole()
Definition: Engine.h:196
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
Contains control state updates regarding a NetworkedInput.
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:82
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 163 of file NetworkInterface.cpp.

163  {
164  // By default allow connections to close //
165  return true;
166 }

◆ 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 84 of file NetworkInterface.h.

84  {
85 
86  return Owner;
87  }

◆ HandleRequestPacket()

DLLEXPORT void NetworkInterface::HandleRequestPacket ( 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 31 of file NetworkInterface.cpp.

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

◆ HandleResponseOnlyPacket()

virtual DLLEXPORT void Leviathan::NetworkInterface::HandleResponseOnlyPacket ( 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 ( 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 26 of file NetworkInterface.cpp.

27  {
28  return true;
29 }

◆ SetOwner()

DLLEXPORT void NetworkInterface::SetOwner ( NetworkHandler owner)
protected

Definition at line 173 of file NetworkInterface.cpp.

173  {
174 
175  Owner = owner;
176 }

◆ 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 168 of file NetworkInterface.cpp.

168  {
169 
170  LEVIATHAN_ASSERT(type == OurNetworkType, "NetworkInterface::VerifyType doesn't match");
171 }

Member Data Documentation

◆ OurNetworkType

NETWORKED_TYPE Leviathan::NetworkInterface::OurNetworkType
protected

Definition at line 121 of file NetworkInterface.h.

◆ Owner

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

Definition at line 122 of file NetworkInterface.h.


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