Leviathan  0.8.0.0
Leviathan game engine
NetworkInterface.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "NetworkInterface.h"
3 
4 #include "../Utility/Convert.h"
6 #include "Connection.h"
7 #include "Engine.h"
8 #include "Exceptions.h"
9 #include "NetworkHandler.h"
10 #include "NetworkRequest.h"
11 #include "NetworkResponse.h"
12 #include "RemoteConsole.h"
13 #include "SyncedVariables.h"
14 using namespace Leviathan;
15 // ------------------------------------ //
17 
19 // ------------------------------------ //
21  const std::shared_ptr<NetworkResponse>& response, SentNetworkThing* originalrequest,
22  Connection& connection)
23 {
24  return true;
25 }
26 // ------------------------------------ //
28  const std::shared_ptr<NetworkRequest>& request, Connection& connection)
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 }
37 // ------------------------------------ //
39  const std::shared_ptr<NetworkRequest>& request, Connection& connection)
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 }
98 // ------------------------------------ //
100  const std::shared_ptr<NetworkResponse>& message, Connection& connection)
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 }
148 // ------------------------------------ //
150 {
151  // By default allow connections to close //
152  return true;
153 }
154 // ------------------------------------ //
156 {
157  LEVIATHAN_ASSERT(type == OurNetworkType, "NetworkInterface::VerifyType doesn't match");
158 }
159 // ------------------------------------ //
161 {
162  Owner = owner;
163 }
NETWORK_REQUEST_TYPE GetType() const
DLLEXPORT void HandleRemoteConsoleRequestPacket(std::shared_ptr< NetworkRequest > request, std::shared_ptr< Connection > connection)
DLLEXPORT std::shared_ptr< SentRequest > SendPacketToConnection(const std::shared_ptr< NetworkRequest > &request, RECEIVE_GUARANTEE guarantee)
Definition: Connection.cpp:139
Packet is sent once and if lost won't be resent.
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
virtual DLLEXPORT ~NetworkInterface()
virtual DLLEXPORT bool PreHandleResponse(const std::shared_ptr< NetworkResponse > &response, SentNetworkThing *originalrequest, Connection &connection)
Called by ConnectionInfo to verify that a response is good.
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.
DLLEXPORT void VerifyType(NETWORKED_TYPE type) const
Asserts if types don't match.
SyncedVariables * GetSyncedVariables()
DLLEXPORT void GetGameIdentificationData(std::string &userreadable, std::string &gamename, std::string &gameversion)
Definition: AppDefine.cpp:185
DLLEXPORT void HandleRemoteConsoleResponse(std::shared_ptr< NetworkResponse > response, Connection &connection, std::shared_ptr< NetworkRequest > potentialrequest)
NETWORK_RESPONSE_TYPE GetType() const
DLLEXPORT bool HandleResponseOnlySync(std::shared_ptr< NetworkResponse > response, Connection *connection)
Handles a response only packet, if it is a sync packet.
DLLEXPORT NetworkInterface(NETWORKED_TYPE type)
DLLEXPORT bool _HandleDefaultResponseOnly(const std::shared_ptr< NetworkResponse > &message, Connection &connection)
Utility function for subclasses to call for default handling of non-request responses.
virtual DLLEXPORT bool CanConnectionTerminate(Connection &connection)
Called by Connection just before terminating an inactive connection.
NETWORKED_TYPE
Type of networked application.
Definition: CommonNetwork.h:22
Empty response, used for keeping alive/nothing.
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:100
DLLEXPORT std::string GenerateFormatedAddressString() const
Returns a nicely formated address string for this connection.
RemoteConsole * GetRemoteConsole()
Definition: Engine.h:235
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void SetOwner(NetworkHandler *owner)
DLLEXPORT std::shared_ptr< Connection > GetConnection(Connection *directptr) const
Returns a persistent pointer to a connection.
Class that handles a single connection to another instance.
Definition: Connection.h:105
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.
DLLEXPORT bool _HandleDefaultRequest(const std::shared_ptr< NetworkRequest > &request, Connection &connectiontosendresult)
Utility function for subclasses to call for default handling.
#define DLLEXPORT
Definition: Include.h:84
Contains control state updates regarding a NetworkedInput.
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:86
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
unsigned int uint32_t
Definition: core.h:40
Handles everything related to connections.
virtual DLLEXPORT void CloseConnection(Connection &connection)
Marks a connection as closing.
static DLLEXPORT AppDef * GetDefault()
Definition: AppDefine.h:112
virtual DLLEXPORT void HandleRequestPacket(const std::shared_ptr< NetworkRequest > &request, Connection &connection)=0
Called by ConnectionInfo to handle incoming packets.
Represents a sent packet and holds all kinds of data for it.