Leviathan  0.8.0.0
Leviathan game engine
Leviathan::NetworkResponse Class Referenceabstract

#include <NetworkResponse.h>

+ Inheritance diagram for Leviathan::NetworkResponse:

Public Member Functions

 NetworkResponse (NETWORK_RESPONSE_TYPE type, uint32_t responseid)
 
virtual ~NetworkResponse ()
 
void AddDataToPacket (sf::Packet &packet) const
 
NETWORK_RESPONSE_TYPE GetType () const
 
auto GetResponseID () const
 

Static Public Member Functions

static DLLEXPORT std::shared_ptr< NetworkResponseLoadFromPacket (sf::Packet &packet)
 
static DLLEXPORT void LimitResponseSize (ResponseIdentification &response, uint32_t maxsize)
 Limits size of response to avoid the application being used for DDoS amplification. More...
 

Protected Member Functions

virtual DLLEXPORT void _SerializeCustom (sf::Packet &packet) const =0
 Base classes serialize their data. More...
 

Protected Attributes

const NETWORK_RESPONSE_TYPE Type
 Type of response. Specifies which subclass this object is. More...
 
const uint32_t ResponseID = 0
 

Detailed Description

Base class for all request objects

Note
Even though it cannot be required by the base class, sub classes should implement a constructor taking in an sf::Packet object
Todo:
Re-implement limiting string lengths in messages

Definition at line 135 of file NetworkResponse.h.

Constructor & Destructor Documentation

◆ NetworkResponse()

Leviathan::NetworkResponse::NetworkResponse ( NETWORK_RESPONSE_TYPE  type,
uint32_t  responseid 
)
inline

Definition at line 138 of file NetworkResponse.h.

138  :
139  Type(type), ResponseID(responseid)
140  {
141  LEVIATHAN_ASSERT(responseid != static_cast<uint32_t>(-1),
142  "Response should use 0 if in response to anything");
143  }
const NETWORK_RESPONSE_TYPE Type
Type of response. Specifies which subclass this object is.

◆ ~NetworkResponse()

virtual Leviathan::NetworkResponse::~NetworkResponse ( )
inlinevirtual

Definition at line 145 of file NetworkResponse.h.

145 {};

Member Function Documentation

◆ _SerializeCustom()

virtual DLLEXPORT void Leviathan::NetworkResponse::_SerializeCustom ( sf::Packet &  packet) const
protectedpure virtual

Base classes serialize their data.

Implemented in Leviathan::ResponseNone, and Leviathan::ResponseCustom.

◆ AddDataToPacket()

void Leviathan::NetworkResponse::AddDataToPacket ( sf::Packet &  packet) const
inline

Definition at line 147 of file NetworkResponse.h.

147  {
148 
149  packet << static_cast<uint16_t>(Type) << ResponseID;
150 
151  _SerializeCustom(packet);
152  }
const NETWORK_RESPONSE_TYPE Type
Type of response. Specifies which subclass this object is.
virtual DLLEXPORT void _SerializeCustom(sf::Packet &packet) const =0
Base classes serialize their data.

◆ GetResponseID()

auto Leviathan::NetworkResponse::GetResponseID ( ) const
inline

Definition at line 159 of file NetworkResponse.h.

159  {
160  return ResponseID;
161  }

◆ GetType()

NETWORK_RESPONSE_TYPE Leviathan::NetworkResponse::GetType ( ) const
inline

Definition at line 154 of file NetworkResponse.h.

154  {
155 
156  return Type;
157  }
const NETWORK_RESPONSE_TYPE Type
Type of response. Specifies which subclass this object is.

◆ LimitResponseSize()

DLLEXPORT void Leviathan::NetworkResponse::LimitResponseSize ( ResponseIdentification &  response,
uint32_t  maxsize 
)
static

Limits size of response to avoid the application being used for DDoS amplification.

Definition at line 56 of file NetworkResponse.cpp.

58 {
59  if (response.GameName.length() + response.GameVersionString.length() +
60  response.LeviathanVersionString.length() + response.UserReadableData.length() >
61  maxsize)
62  {
63  // Need to trim something //
64  DEBUG_BREAK;
65  response.UserReadableData = "";
66  }
67 }

◆ LoadFromPacket()

DLLEXPORT std::shared_ptr< NetworkResponse > NetworkResponse::LoadFromPacket ( sf::Packet &  packet)
static

Definition at line 10 of file NetworkResponse.cpp.

10  {
11 
12  // First thing is the type, based on which we handle the rest of the data
13  // This is before responseid to look more like a request packet
14  uint16_t rawtype;
15  packet >> rawtype;
16 
17  // Second thing is the response ID //
18  uint32_t responseid = 0;
19  packet >> responseid;
20 
21  if(!packet)
22  throw InvalidArgument("packet has invalid format");
23 
24  const auto responsetype = static_cast<NETWORK_RESPONSE_TYPE>(rawtype);
25 
26  // Process based on the type //
27  switch(responsetype){
30 
32  return std::make_shared<ResponseConnect>(responseid, packet);
34  return std::make_shared<ResponseAuthenticate>(responseid, packet);
36  return std::make_shared<ResponseSecurity>(responseid, packet);
38  return std::make_shared<ResponseServerAllow>(responseid, packet);
40  return std::make_shared<ResponseServerDisallow>(responseid, packet);
41  // None based types
43  return std::make_shared<ResponseNone>(responsetype, responseid, packet);
44 
45  default:
46  {
47  Logger::Get()->Warning("NetworkResponse: unused type: "+
48  Convert::ToString(static_cast<int>(responsetype)));
49  throw InvalidArgument("packet has response type that is missing from "
50  "switch(responsetype)");
51  }
52  break;
53  }
54 }
Only one side of the connection can send this request, usually the client.
Sent by a server when a request is allowed.
NETWORK_RESPONSE_TYPE
Defines the type of response that the packet contains.
DLLEXPORT void Warning(const std::string &data) override
Definition: Logger.cpp:190
Empty response, used for keeping alive/nothing.
unsigned short uint16_t
Definition: core.h:39
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
unsigned int uint32_t
Definition: core.h:40
Sent by a server when it disallows a made request.
Sent in response to a NETWORK_REQUEST_TYPE::Connect.

Member Data Documentation

◆ ResponseID

const uint32_t Leviathan::NetworkResponse::ResponseID = 0
protected

Definition at line 178 of file NetworkResponse.h.

◆ Type

const NETWORK_RESPONSE_TYPE Leviathan::NetworkResponse::Type
protected

Type of response. Specifies which subclass this object is.

Definition at line 176 of file NetworkResponse.h.


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