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
 
DLLEXPORT std::string GetTypeStr () 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 141 of file NetworkResponse.h.

Constructor & Destructor Documentation

◆ NetworkResponse()

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

Definition at line 143 of file NetworkResponse.h.

143  :
144  Type(type), ResponseID(responseid)
145  {
146  LEVIATHAN_ASSERT(responseid != static_cast<uint32_t>(-1),
147  "Response should use 0 if in response to anything");
148  }
const NETWORK_RESPONSE_TYPE Type
Type of response. Specifies which subclass this object is.
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:98

◆ ~NetworkResponse()

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

Definition at line 150 of file NetworkResponse.h.

150 {};

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 152 of file NetworkResponse.h.

153  {
154  packet << static_cast<uint16_t>(Type) << ResponseID;
155 
156  _SerializeCustom(packet);
157  }
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 167 of file NetworkResponse.h.

168  {
169  return ResponseID;
170  }

◆ GetType()

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

Definition at line 159 of file NetworkResponse.h.

160  {
161  return Type;
162  }
const NETWORK_RESPONSE_TYPE Type
Type of response. Specifies which subclass this object is.

◆ GetTypeStr()

DLLEXPORT std::string NetworkResponse::GetTypeStr ( ) const
Returns
Name of this type. For prettier debug printing

Definition at line 73 of file NetworkResponse.cpp.

74 {
75  switch(Type) {
76  case NETWORK_RESPONSE_TYPE::Connect: return "ResponseConnect";
77  case NETWORK_RESPONSE_TYPE::Security: return "ResponseSecurity";
78  case NETWORK_RESPONSE_TYPE::Authenticate: return "ResponseAuthenticate";
79  case NETWORK_RESPONSE_TYPE::Identification: return "ResponseIdentification";
80  case NETWORK_RESPONSE_TYPE::Keepalive: return "ResponseKeepalive";
81  case NETWORK_RESPONSE_TYPE::CloseConnection: return "ResponseCloseConnection";
82  case NETWORK_RESPONSE_TYPE::RemoteConsoleClosed: return "ResponseRemoteConsoleClosed";
83  case NETWORK_RESPONSE_TYPE::RemoteConsoleOpened: return "ResponseRemoteConsoleOpened";
84  case NETWORK_RESPONSE_TYPE::InvalidRequest: return "ResponseInvalidRequest";
85  case NETWORK_RESPONSE_TYPE::ServerDisallow: return "ResponseServerDisallow";
86  case NETWORK_RESPONSE_TYPE::ServerAllow: return "ResponseServerAllow";
87  case NETWORK_RESPONSE_TYPE::ServerStatus: return "ResponseServerStatus";
88  case NETWORK_RESPONSE_TYPE::SyncValData: return "ResponseSyncValData";
89  case NETWORK_RESPONSE_TYPE::SyncDataEnd: return "ResponseSyncDataEnd";
90  case NETWORK_RESPONSE_TYPE::SyncResourceData: return "ResponseSyncResourceData";
91  case NETWORK_RESPONSE_TYPE::CreateNetworkedInput: return "ResponseCreateNetworkedInput";
92  case NETWORK_RESPONSE_TYPE::UpdateNetworkedInput: return "ResponseUpdateNetworkedInput";
93  case NETWORK_RESPONSE_TYPE::DisconnectInput: return "ResponseDisconnectInput";
94  case NETWORK_RESPONSE_TYPE::StartWorldReceive: return "ResponseStartWorldReceive";
95  case NETWORK_RESPONSE_TYPE::EntityCreation: return "ResponseEntityCreation";
96  case NETWORK_RESPONSE_TYPE::EntityUpdate: return "ResponseEntityUpdate";
97  case NETWORK_RESPONSE_TYPE::EntityDestruction: return "ResponseEntityDestruction";
99  return "ResponseEntityLocalControlStatus";
100  case NETWORK_RESPONSE_TYPE::CacheUpdated: return "ResponseCacheUpdated";
101  case NETWORK_RESPONSE_TYPE::CacheRemoved: return "ResponseCacheRemoved";
102  case NETWORK_RESPONSE_TYPE::WorldFrozen: return "ResponseWorldFrozen";
103  case NETWORK_RESPONSE_TYPE::ServerHeartbeat: return "ResponseServerHeartbeat";
104  case NETWORK_RESPONSE_TYPE::StartHeartbeats: return "ResponseStartHeartbeats";
105  case NETWORK_RESPONSE_TYPE::None: return "ResponseNone";
106  default: break;
107  }
108 
110 
111  return "ResponseCustom(+" +
112  std::to_string(
113  static_cast<int>(Type) - static_cast<int>(NETWORK_RESPONSE_TYPE::Custom)) +
114  ")";
115 
116  } else {
117 
118  return std::to_string(static_cast<int>(Type));
119  }
120 }
Only one side of the connection can send this request, usually the client.
All values above this are application specific types.
Contains an updated cache variable.
Sent by a server when a request is allowed.
Sends a update/new SyncedValue.
Client sents this when they want input to be destroyed.
Sent when the server changes physics frozen state.
Empty response, used for keeping alive/nothing.
Contains (list) an ID for entity to be deleted.
const NETWORK_RESPONSE_TYPE Type
Type of response. Specifies which subclass this object is.
Contains the name of a removed cache variable.
Contains SyncedResource update notification.
Contains information about a world that is about to be sent to the client.
Contains control state updates regarding a NetworkedInput.
Marks that the client is required to send heartbeats.
Sent by a server when it disallows a made request.
A new entity was created on the server.
Sent in response to a NETWORK_REQUEST_TYPE::Connect.

◆ 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 61 of file NetworkResponse.cpp.

63 {
64  if(response.GameName.length() + response.GameVersionString.length() +
65  response.LeviathanVersionString.length() + response.UserReadableData.length() >
66  maxsize) {
67  // Need to trim something //
68  DEBUG_BREAK;
69  response.UserReadableData = "";
70  }
71 }

◆ LoadFromPacket()

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

Definition at line 8 of file NetworkResponse.cpp.

9 {
10  // First thing is the type, based on which we handle the rest of the data
11  // This is before responseid to look more like a request packet
12  uint16_t rawtype;
13  packet >> rawtype;
14 
15  // Second thing is the response ID //
16  uint32_t responseid = 0;
17  packet >> responseid;
18 
19  if(!packet)
20  throw InvalidArgument("packet has invalid format");
21 
22  const auto responsetype = static_cast<NETWORK_RESPONSE_TYPE>(rawtype);
23 
24  // Process based on the type //
25  switch(responsetype) {
27  return std::make_shared<ResponseConnect>(responseid, packet);
29  return std::make_shared<ResponseAuthenticate>(responseid, packet);
31  return std::make_shared<ResponseSecurity>(responseid, packet);
33  return std::make_shared<ResponseServerAllow>(responseid, packet);
35  return std::make_shared<ResponseServerDisallow>(responseid, packet);
37  return std::make_shared<ResponseStartWorldReceive>(responseid, packet);
39  return std::make_shared<ResponseEntityCreation>(responseid, packet);
41  return std::make_shared<ResponseEntityDestruction>(responseid, packet);
43  return std::make_shared<ResponseEntityUpdate>(responseid, packet);
45  return std::make_shared<ResponseEntityLocalControlStatus>(responseid, packet);
46  // None based types
50  return std::make_shared<ResponseNone>(responsetype, responseid, packet);
51 
52  default: {
53  Logger::Get()->Warning("NetworkResponse: unused type: " +
54  Convert::ToString(static_cast<int>(responsetype)));
55  throw InvalidArgument("packet has response type that is missing from "
56  "switch(responsetype)");
57  } break;
58  }
59 }
Only one side of the connection can send this request, usually the client.
Sent by a server when a request is allowed.
DLLEXPORT void Warning(const std::string &data) override
Definition: Logger.cpp:190
Empty response, used for keeping alive/nothing.
Contains (list) an ID for entity to be deleted.
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
Contains information about a world that is about to be sent to the client.
unsigned int uint32_t
Definition: core.h:40
Sent by a server when it disallows a made request.
A new entity was created on the server.
Sent in response to a NETWORK_REQUEST_TYPE::Connect.

Member Data Documentation

◆ ResponseID

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

Definition at line 186 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 184 of file NetworkResponse.h.


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