Leviathan  0.8.0.0
Leviathan game engine
NetworkRequest.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2018 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "Common/SFMLPackets.h"
7 #include "CommonNetwork.h"
8 #include "Exceptions.h"
9 
11 
12 #include <memory>
13 
14 namespace Leviathan {
15 
17 
19  Connect,
20 
22  Security,
23 
27 
31 
33 
35 
37 
39 
42 
45  JoinServer,
46 
49  JoinGame,
50 
52 
54 
58 
61 
63  Echo,
64 
73 
75  Custom
76 };
77 
82 public:
83  NetworkRequest(NETWORK_REQUEST_TYPE type, uint32_t receivedmessagenumber = 0) :
84  Type(type), MessageNumber(receivedmessagenumber)
85  {}
86 
87  virtual ~NetworkRequest(){};
88 
89  inline void AddDataToPacket(sf::Packet& packet) const
90  {
91  packet << static_cast<uint16_t>(Type);
92 
93  _SerializeCustom(packet);
94  }
95 
97  {
98  return Type;
99  }
100 
102  DLLEXPORT std::string GetTypeStr() const;
103 
104  inline uint32_t GetMessageNumber() const
105  {
106  return MessageNumber;
107  }
108 
111  inline uint32_t GetIDForResponse() const
112  {
113  return MessageNumber;
114  }
115 
116  DLLEXPORT static std::shared_ptr<NetworkRequest> LoadFromPacket(
117  sf::Packet& packet, uint32_t messagenumber);
118 
119 protected:
121  DLLEXPORT virtual void _SerializeCustom(sf::Packet& packet) const = 0;
122 
124 
127 };
128 
130 public:
131  RequestCustom(std::shared_ptr<GameSpecificPacketData> actualrequest) :
133  {}
134 
135  void _SerializeCustom(sf::Packet& packet) const override
136  {
137 
138  LEVIATHAN_ASSERT(0, "_SerializeCustom called on RequestCustom");
139  }
140 
141  RequestCustom(GameSpecificPacketHandler& handler, sf::Packet& packet) :
143  {
144  ActualRequest = handler.ReadGameSpecificPacketFromPacket(false, packet);
145 
146  if(!ActualRequest) {
147 
148  throw InvalidArgument("invalid packet format for user defined request");
149  }
150  }
151 
152  inline void AddDataToPacket(GameSpecificPacketHandler& handler, sf::Packet& packet)
153  {
154 
155  packet << static_cast<uint16_t>(Type);
156 
157  handler.PassGameSpecificDataToPacket(ActualRequest.get(), packet);
158  }
159 
160  std::shared_ptr<GameSpecificPacketData> ActualRequest;
161 };
162 
166 class RequestNone : public NetworkRequest {
167 public:
168  RequestNone(NETWORK_REQUEST_TYPE actualtype) : NetworkRequest(actualtype) {}
169 
170  void _SerializeCustom(sf::Packet& packet) const override {}
171 
172  RequestNone(NETWORK_REQUEST_TYPE actualtype, uint32_t idforresponse, sf::Packet& packet) :
173  NetworkRequest(actualtype, idforresponse)
174  {}
175 };
176 
177 
178 
179 // This file is generated by the script GenerateRequest.rb
180 // and contains implementations for all the response types
181 #include "../Generated/RequestImpl.h"
182 
183 
184 } // namespace Leviathan
185 
186 #ifdef LEAK_INTO_GLOBAL
188 #endif
NETWORK_REQUEST_TYPE GetType() const
Only one side of the connection can send this request, usually the client.
void _SerializeCustom(sf::Packet &packet) const override
Base classes serialize their data.
All values above this are application specific types.
RequestCustom(std::shared_ptr< GameSpecificPacketData > actualrequest)
static DLLEXPORT std::shared_ptr< NetworkRequest > LoadFromPacket(sf::Packet &packet, uint32_t messagenumber)
Sent when a player requests the server to connect a NetworkedInput.
virtual DLLEXPORT void _SerializeCustom(sf::Packet &packet) const =0
Base classes serialize their data.
DLLEXPORT std::shared_ptr< GameSpecificPacketData > ReadGameSpecificPacketFromPacket(bool responsepacket, sf::Packet &packet)
void AddDataToPacket(GameSpecificPacketHandler &handler, sf::Packet &packet)
RequestNone(NETWORK_REQUEST_TYPE actualtype, uint32_t idforresponse, sf::Packet &packet)
void AddDataToPacket(sf::Packet &packet) const
DLLEXPORT std::string GetTypeStr() const
const NETWORK_REQUEST_TYPE Type
uint32_t GetMessageNumber() const
RequestCustom(GameSpecificPacketHandler &handler, sf::Packet &packet)
The receiving side is now allowed to open a remote console with the token.
uint32_t GetIDForResponse() const
The id number for a response to this is the same as the message number that this request is in...
unsigned short uint16_t
Definition: core.h:39
DLLEXPORT void PassGameSpecificDataToPacket(GameSpecificPacketData *datatosend, sf::Packet &packet)
Handles construction of all game specific packets.
Empty request for ones that require no data.
Sent by servers to ping (time the time a client takes to respond) clients.
#define DLLEXPORT
Definition: Include.h:115
std::shared_ptr< GameSpecificPacketData > ActualRequest
NetworkRequest(NETWORK_REQUEST_TYPE type, uint32_t receivedmessagenumber=0)
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
void _SerializeCustom(sf::Packet &packet) const override
Base classes serialize their data.
unsigned int uint32_t
Definition: core.h:40
const uint32_t MessageNumber
This is only valid when this is received.
RequestNone(NETWORK_REQUEST_TYPE actualtype)