Leviathan  0.8.0.0
Leviathan game engine
NetworkResponse.cpp
Go to the documentation of this file.
1 // ----------------------------------- //
2 #include "NetworkResponse.h"
3 
6 using namespace Leviathan;
7 // ------------------------------------ //
8 DLLEXPORT std::shared_ptr<NetworkResponse> NetworkResponse::LoadFromPacket(sf::Packet& packet)
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 }
60 // ------------------------------------ //
62  ResponseIdentification& response, uint32_t maxsize)
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 }
72 // ------------------------------------ //
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.
DLLEXPORT std::string GetTypeStr() const
Sends a update/new SyncedValue.
static DLLEXPORT std::shared_ptr< NetworkResponse > LoadFromPacket(sf::Packet &packet)
Client sents this when they want input to be destroyed.
DLLEXPORT void Warning(const std::string &data) override
Definition: Logger.cpp:190
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.
unsigned short uint16_t
Definition: core.h:39
static std::string ToString(const T &val)
Definition: Convert.h:72
Contains the name of a removed cache variable.
Contains SyncedResource update notification.
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
Contains information about a world that is about to be sent to the client.
static DLLEXPORT void LimitResponseSize(ResponseIdentification &response, uint32_t maxsize)
Limits size of response to avoid the application being used for DDoS amplification.
#define DLLEXPORT
Definition: Include.h:84
Contains control state updates regarding a NetworkedInput.
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
unsigned int uint32_t
Definition: core.h:40
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.