Leviathan  0.8.0.0
Leviathan game engine
NetworkResponse.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2016 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
7 #include "Common/SFMLPackets.h"
8 #include "CommonNetwork.h"
9 #include "Exceptions.h"
10 
12 
13 #include <memory>
14 
15 namespace Leviathan {
16 
19 
21  Connect,
22 
25  Security,
26 
29 
33 
35  Keepalive,
36 
40 
42 
44 
46 
49 
52 
69 
72 
76 
79 
82 
85 
88 
91 
94 
100  EntityUpdate,
101 
104 
108 
110  CacheUpdated,
111 
113  CacheRemoved,
114 
115  // //! Instructs a world to create or destroy a constraint
116  // //! Create When false the constraint is to be deleted
117  // EntityConstraint,
118 
120  WorldFrozen,
121 
124 
127 
129  None,
130 
133  Custom
134 };
135 
136 
142 public:
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  }
149 
150  virtual ~NetworkResponse(){};
151 
152  inline void AddDataToPacket(sf::Packet& packet) const
153  {
154  packet << static_cast<uint16_t>(Type) << ResponseID;
155 
156  _SerializeCustom(packet);
157  }
158 
160  {
161  return Type;
162  }
163 
165  DLLEXPORT std::string GetTypeStr() const;
166 
167  inline auto GetResponseID() const
168  {
169  return ResponseID;
170  }
171 
172  DLLEXPORT static std::shared_ptr<NetworkResponse> LoadFromPacket(sf::Packet& packet);
173 
176  DLLEXPORT static void LimitResponseSize(
177  ResponseIdentification& response, uint32_t maxsize);
178 
179 protected:
181  DLLEXPORT virtual void _SerializeCustom(sf::Packet& packet) const = 0;
182 
185 
186  const uint32_t ResponseID = 0;
187 };
188 
191 public:
193  uint32_t responseid, std::shared_ptr<GameSpecificPacketData> actualresponse) :
195  ActualResponse(actualresponse)
196  {}
197 
198  void _SerializeCustom(sf::Packet& packet) const override
199  {
200 
201  LEVIATHAN_ASSERT(0, "_SerializeCustom called on ResponseCustom");
202  }
203 
204  inline void AddDataToPacket(GameSpecificPacketHandler& handler, sf::Packet& packet)
205  {
206 
207  packet << ResponseID << static_cast<uint16_t>(Type);
208 
209  handler.PassGameSpecificDataToPacket(ActualResponse.get(), packet);
210  }
211 
213  GameSpecificPacketHandler& handler, uint32_t responseid, sf::Packet& packet) :
215  {
216  ActualResponse = handler.ReadGameSpecificPacketFromPacket(true, packet);
217  if(!ActualResponse) {
218 
219  throw InvalidArgument("invalid packet format for user defined response");
220  }
221  }
222 
223  std::shared_ptr<GameSpecificPacketData> ActualResponse;
224 };
225 
230 public:
231  ResponseNone(NETWORK_RESPONSE_TYPE actualtype, uint32_t responseid = 0) :
232  NetworkResponse(actualtype, responseid)
233  {}
234 
235  void _SerializeCustom(sf::Packet& packet) const override {}
236 
237  ResponseNone(NETWORK_RESPONSE_TYPE actualtype, uint32_t responseid, sf::Packet& packet) :
238  NetworkResponse(actualtype, responseid)
239  {}
240 };
241 
242 
243 // This file is generated by the script GenerateResponse.rb
244 // and contains implementations for all the response types
245 #include "../Generated/ResponseImpl.h"
246 
247 } // namespace Leviathan
248 
249 #ifdef LEAK_INTO_GLOBAL
251 #endif
Used for BaseGameSpecificResponsePacket storing.
Only one side of the connection can send this request, usually the client.
All values above this are application specific types.
ResponseCustom(GameSpecificPacketHandler &handler, uint32_t responseid, sf::Packet &packet)
void AddDataToPacket(sf::Packet &packet) const
NetworkResponse(NETWORK_RESPONSE_TYPE type, uint32_t responseid)
DLLEXPORT std::shared_ptr< GameSpecificPacketData > ReadGameSpecificPacketFromPacket(bool responsepacket, sf::Packet &packet)
ResponseNone(NETWORK_RESPONSE_TYPE actualtype, uint32_t responseid, sf::Packet &packet)
Contains an updated cache variable.
Sent by a server when a request is allowed.
NETWORK_RESPONSE_TYPE
Defines the type of response that the packet contains.
DLLEXPORT std::string GetTypeStr() const
Sends a update/new SyncedValue.
NETWORK_RESPONSE_TYPE GetType() const
static DLLEXPORT std::shared_ptr< NetworkResponse > LoadFromPacket(sf::Packet &packet)
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.
unsigned short uint16_t
Definition: core.h:39
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:104
void _SerializeCustom(sf::Packet &packet) const override
Base classes serialize their data.
ResponseCustom(uint32_t responseid, std::shared_ptr< GameSpecificPacketData > actualresponse)
Contains the name of a removed cache variable.
Contains SyncedResource update notification.
DLLEXPORT void PassGameSpecificDataToPacket(GameSpecificPacketData *datatosend, sf::Packet &packet)
Handles construction of all game specific packets.
std::shared_ptr< GameSpecificPacketData > ActualResponse
void _SerializeCustom(sf::Packet &packet) const override
Base classes serialize their data.
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.
virtual DLLEXPORT void _SerializeCustom(sf::Packet &packet) const =0
Base classes serialize their data.
#define DLLEXPORT
Definition: Include.h:84
Contains control state updates regarding a NetworkedInput.
Empty keep alive response.
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.
ResponseNone(NETWORK_RESPONSE_TYPE actualtype, uint32_t responseid=0)
void AddDataToPacket(GameSpecificPacketHandler &handler, sf::Packet &packet)
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.