Leviathan  0.8.0.0
Leviathan game engine
Connection.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2017 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "CommonNetwork.h"
7 
8 #include "NetworkAckField.h"
9 
10 #include "SFML/Network/IpAddress.hpp"
11 #include "SFML/Network/Packet.hpp"
12 
13 #include "boost/circular_buffer.hpp"
14 
15 #include <map>
16 #include <vector>
17 #include <memory>
18 
19 namespace sf{
20 class Packet;
21 }
22 
23 namespace Leviathan{
24 
25 class SentRequest;
26 class SentResponse;
27 
28 enum class NETWORK_RESPONSE_TYPE : uint16_t;
29 
30 constexpr auto DEFAULT_ACKCOUNT = 32;
31 constexpr auto KEEPALIVE_TIME = 120000;
32 constexpr auto ACKKEEPALIVE = 200;
33 
34 constexpr auto ACK_ONLY_DEFAULT_MAX = 4;
35 
37 constexpr auto DOUBLE_SEND_FOR_ACK_ONLY = true;
38 
42 constexpr auto DEFAULT_PACKET_FILL_AMOUNT = 512;
43 
49 constexpr auto KEEP_IDS_FOR_DISCARD = 50;
50 
52 enum class PING_FAIL_REASON {
53 
56 };
57 
60  None,
62 };
63 
68 enum class CONNECTION_STATE {
72 
74  Initial,
75 
77  Connected,
78 
81  Secured,
82 
87 
91  Closed,
92 
96 };
97 
98 
105 public:
108  DLLEXPORT Connection(const std::string &hostname);
109  DLLEXPORT Connection(const sf::IpAddress &targetaddress, unsigned short port);
110  DLLEXPORT ~Connection();
111 
116  DLLEXPORT bool Init(NetworkHandler* owninghandler);
117  DLLEXPORT void Release();
118 
120  inline bool IsValidForSend() const {
121 
122  return State != CONNECTION_STATE::Closed;
123  }
124 
125  inline CONNECTION_STATE GetState() const {
126  return State;
127  }
128 
130  DLLEXPORT void SetRestrictionMode(CONNECTION_RESTRICTION type);
131 
134  inline bool IsThisYours(const sf::IpAddress &sender, unsigned short sentport){
135 
136  return sentport == TargetPortNumber && sender == TargetHost;
137  }
138 
140  DLLEXPORT void HandlePacket(sf::Packet &packet);
141 
143  DLLEXPORT bool IsTargetHostLocalhost();
144 
146  DLLEXPORT void UpdateListening();
147 
150  DLLEXPORT std::shared_ptr<SentRequest> SendPacketToConnection(
151  const std::shared_ptr<NetworkRequest> &request, RECEIVE_GUARANTEE guarantee);
152 
154  DLLEXPORT bool SendPacketToConnection(const NetworkResponse &response);
155 
158  DLLEXPORT std::shared_ptr<SentResponse> SendPacketToConnection(
159  const std::shared_ptr<NetworkResponse> &response, RECEIVE_GUARANTEE guarantee);
160 
162  DLLEXPORT void SendKeepAlivePacket();
163 
166  DLLEXPORT void SendCloseConnectionPacket();
167 
172  DLLEXPORT std::string GenerateFormatedAddressString() const;
173 
185  DLLEXPORT void CalculateNetworkPing(int packets, int allowedfails,
186  std::function<void(int, int)> onsucceeded,
187  std::function<void(PING_FAIL_REASON, int)> onfailed);
188 
192  DLLEXPORT void HandleRemoteAck(uint32_t localidconfirmedassent);
193 
194 
196  DLLEXPORT std::vector<uint32_t> GetCurrentlySentAcks();
197 
198  inline std::string GetRawAddress() const {
199  return RawAddress;
200  }
201 
204  const auto& GetReceivedPackets() const{
205  return ReceivedRemotePackets;
206  }
207 
208 protected:
209 
211  DLLEXPORT void _HandleRequestPacket(sf::Packet &packet, uint32_t messagenumber,
212  bool alreadyreceived);
213 
215  DLLEXPORT void _HandleResponsePacket(sf::Packet &packet, bool alreadyreceived);
216 
217 
221  DLLEXPORT void SetPacketsReceivedIfNotSet(NetworkAckField &acks);
222 
227  DLLEXPORT void RemoveSucceededAcks(NetworkAckField &acks);
228 
230  DLLEXPORT void _SendPacketToSocket(sf::Packet &actualpackettosend);
231 
232 
234  DLLEXPORT void _FailPacketAcks(uint32_t packetid);
235 
238  bool _IsAlreadyReceived(uint32_t messagenumber);
239 
241  DLLEXPORT void _OnRestrictFail(uint16_t type);
242 
243 protected:
244 
245  DLLEXPORT bool _HandleInternalRequest(const std::shared_ptr<NetworkRequest> &request);
246 
247  DLLEXPORT bool _HandleInternalResponse(const std::shared_ptr<NetworkResponse> &response);
248 
251  void _Resend(SentRequest &toresend);
252 
253  void _Resend(SentResponse &toresend);
254 
255  template<class TSentType>
256  void _HandleTimeouts(int64_t timems,
257  std::vector<std::shared_ptr<TSentType>> sentthing);
258 
259 
261  std::shared_ptr<SentRequest> _GetPossibleRequestForResponse(
262  const std::shared_ptr<NetworkResponse> &response);
263 
266  DLLEXPORT std::shared_ptr<NetworkAckField> _GetAcksToSend(uint32_t localpacketid,
267  bool autoaddtosent = true);
268 
273  // bool _MarkNewAsReceived(uint32_t remotepacketid);
274 
275 protected:
276 
278  CONNECTION_STATE State = CONNECTION_STATE::NothingReceived;
279 
281  NetworkHandler* Owner = nullptr;
282 
285 
291  uint32_t LastUsedLocalID = 0;
292 
294  uint32_t LastUsedMessageNumber = 0;
295 
297  uint32_t LastConfirmedSent = 0;
298 
299 
302  CONNECTION_RESTRICTION RestrictType = CONNECTION_RESTRICTION::None;
303 
308  bool FrontAcks = false;
309 
312  uint8_t ExtraAckCount = 0;
313 
315  int64_t LastSentPacketTime = 0;
316  int64_t LastReceivedPacketTime = 0;
317 
319  std::vector<std::shared_ptr<SentRequest>> PendingRequests;
320 
322  std::vector<std::shared_ptr<SentResponse>> ResponsesNeedingConfirmation;
323 
327  std::vector<std::shared_ptr<SentAcks>> SentAckPackets;
328 
331  boost::circular_buffer<uint32_t> LastReceivedMessageNumbers;
332 
335 
338  std::string RawAddress;
339 
342  std::string HostName;
343 
345  sf::IpAddress TargetHost;
346 
349  bool AddressGot = false;
350 
351 private:
352 
356  sf::Packet StoredWireData;
357 };
358 
359 }
360 
361 #ifdef LEAK_INTO_GLOBAL
363 #endif
364 
std::vector< std::shared_ptr< SentResponse > > ResponsesNeedingConfirmation
Sent responses that need to be confirmed (or resent if they are lost)
Definition: Connection.h:322
std::vector< std::shared_ptr< SentRequest > > PendingRequests
Sent requests that are waiting for a response.
Definition: Connection.h:319
Stores Responses that want confirmation that they have arrived.
PING_FAIL_REASON
If true 2 ack only packets are created each time one is sent.
Definition: Connection.h:52
std::string GetRawAddress() const
Definition: Connection.h:198
bool IsThisYours(const sf::IpAddress &sender, unsigned short sentport)
Checks does the sender and port match our corresponding values.
Definition: Connection.h:134
sf::IpAddress TargetHost
Resolved address of the remote.
Definition: Connection.h:345
RECEIVE_GUARANTEE
Controls whether a packet is critical.
Definition: CommonNetwork.h:49
std::string HostName
Definition: Connection.h:342
NETWORK_RESPONSE_TYPE
Defines the type of response that the packet contains.
CONNECTION_STATE GetState() const
Definition: Connection.h:125
bool IsValidForSend() const
Returns true if this socket is valid for sending.
Definition: Connection.h:120
uint16_t TargetPortNumber
The remote port.
Definition: Connection.h:334
std::string RawAddress
Definition: Connection.h:338
unsigned char uint8_t
Definition: core.h:38
std::vector< std::shared_ptr< SentAcks > > SentAckPackets
Definition: Connection.h:327
CONNECTION_RESTRICTION
Allows restricting connections to allow only certain packets.
Definition: Connection.h:59
unsigned short uint16_t
Definition: core.h:39
Moves to this state once handshake is complete.
Definition: Connection.h:19
std::map< uint32_t, RECEIVED_STATE > PacketReceiveStatus
Class that handles a single connection to another instance.
Definition: Connection.h:104
CONNECTION_STATE
Main state of a connection.
Definition: Connection.h:68
#define DLLEXPORT
Definition: Include.h:118
Begins the handshake with version number check.
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
unsigned int uint32_t
Definition: core.h:40
NetworkAckField::PacketReceiveStatus ReceivedRemotePackets
Used to send acks for received remote packets.
Definition: Connection.h:284
Handles everything related to connections.
Stores Requests while they are waiting for a response.
const auto & GetReceivedPackets() const
Returns a reference to a list of received packets that haven&#39;t been acknowledged successfully to the ...
Definition: Connection.h:204
boost::circular_buffer< uint32_t > LastReceivedMessageNumbers
Definition: Connection.h:331