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-2018 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 <memory>
17 #include <vector>
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 
53 constexpr auto PACKET_NUMBERING_OFFSET = 1000;
54 
56 enum class PING_FAIL_REASON {
57 
60 };
61 
64 
69 enum class CONNECTION_STATE {
73 
75  Initial,
76 
78  Connected,
79 
82  Secured,
83 
88 
92  Closed,
93 
97 };
98 
99 
105 class Connection {
106 public:
109  DLLEXPORT Connection(const std::string& hostname);
110  DLLEXPORT Connection(const sf::IpAddress& targetaddress, unsigned short port);
112 
117  DLLEXPORT bool Init(NetworkHandler* owninghandler);
118  DLLEXPORT void Release();
119 
121  inline bool IsValidForSend() const
122  {
124  }
125 
126  inline CONNECTION_STATE GetState() const
127  {
128  return State;
129  }
130 
133 
136  inline bool IsThisYours(const sf::IpAddress& sender, unsigned short sentport)
137  {
138  return sentport == TargetPortNumber && sender == TargetHost;
139  }
140 
142  DLLEXPORT void HandlePacket(sf::Packet& packet);
143 
146 
148  DLLEXPORT void UpdateListening();
149 
152  DLLEXPORT std::shared_ptr<SentRequest> SendPacketToConnection(
153  const std::shared_ptr<NetworkRequest>& request, RECEIVE_GUARANTEE guarantee);
154 
156  DLLEXPORT bool SendPacketToConnection(const NetworkResponse& response);
157 
160  const NetworkResponse& response);
161 
164  DLLEXPORT std::shared_ptr<SentResponse> SendPacketToConnection(
165  const std::shared_ptr<NetworkResponse>& response, RECEIVE_GUARANTEE guarantee);
166 
169 
173 
178  DLLEXPORT std::string GenerateFormatedAddressString() const;
179 
191  DLLEXPORT void CalculateNetworkPing(int packets, int allowedfails,
192  std::function<void(int, int)> onsucceeded,
193  std::function<void(PING_FAIL_REASON, int)> onfailed);
194 
198  DLLEXPORT void HandleRemoteAck(uint32_t localidconfirmedassent);
199 
200 
202  DLLEXPORT std::vector<uint32_t> GetCurrentlySentAcks();
203 
204  inline std::string GetRawAddress() const
205  {
206  return RawAddress;
207  }
208 
211  const auto& GetReceivedPackets() const
212  {
213  return ReceivedRemotePackets;
214  }
215 
217  const auto& GetPendingRequests() const
218  {
219  return PendingRequests;
220  }
221 
225  {
227  }
228 
229 protected:
232  sf::Packet& packet, uint32_t messagenumber, bool alreadyreceived);
233 
235  DLLEXPORT void _HandleResponsePacket(sf::Packet& packet, bool alreadyreceived);
236 
237 
242 
248 
250  DLLEXPORT void _SendPacketToSocket(sf::Packet& actualpackettosend);
251 
252 
254  DLLEXPORT void _FailPacketAcks(uint32_t packetid);
255 
258  bool _IsAlreadyReceived(uint32_t messagenumber);
259 
262 
263 protected:
264  DLLEXPORT bool _HandleInternalRequest(const std::shared_ptr<NetworkRequest>& request);
265 
266  DLLEXPORT bool _HandleInternalResponse(const std::shared_ptr<NetworkResponse>& response);
267 
270  void _Resend(SentRequest& toresend);
271 
272  void _Resend(SentResponse& toresend);
273 
274  template<class TSentType>
275  void _HandleTimeouts(int64_t timems, std::vector<std::shared_ptr<TSentType>> sentthing);
276 
277 
279  std::shared_ptr<SentRequest> _GetPossibleRequestForResponse(
280  const std::shared_ptr<NetworkResponse>& response);
281 
284  DLLEXPORT std::shared_ptr<NetworkAckField> _GetAcksToSend(
285  uint32_t localpacketid, bool autoaddtosent = true);
286 
291  // bool _MarkNewAsReceived(uint32_t remotepacketid);
292 
293 protected:
296 
298  NetworkHandler* Owner = nullptr;
299 
302 
308  uint32_t LastUsedLocalID = PACKET_NUMBERING_OFFSET;
309 
312 
315 
316 
320 
325  bool FrontAcks = false;
326 
330 
332  int64_t LastSentPacketTime = 0;
334 
336  std::vector<std::shared_ptr<SentRequest>> PendingRequests;
337 
339  std::vector<std::shared_ptr<SentResponse>> ResponsesNeedingConfirmation;
340 
344  std::vector<std::shared_ptr<SentAcks>> SentAckPackets;
345 
348  boost::circular_buffer<uint32_t> LastReceivedMessageNumbers{KEEP_IDS_FOR_DISCARD};
349 
352 
355  std::string RawAddress;
356 
359  std::string HostName;
360 
362  sf::IpAddress TargetHost;
363 
366  bool AddressGot = false;
367 
368 private:
372  sf::Packet StoredWireData;
373 };
374 
375 } // namespace Leviathan
376 
377 #ifdef LEAK_INTO_GLOBAL
379 #endif
DLLEXPORT std::shared_ptr< SentRequest > SendPacketToConnection(const std::shared_ptr< NetworkRequest > &request, RECEIVE_GUARANTEE guarantee)
Definition: Connection.cpp:139
int64_t LastReceivedPacketTime
Definition: Connection.h:333
std::vector< std::shared_ptr< SentResponse > > ResponsesNeedingConfirmation
Sent responses that need to be confirmed (or resent if they are lost)
Definition: Connection.h:339
std::vector< std::shared_ptr< SentRequest > > PendingRequests
Sent requests that are waiting for a response.
Definition: Connection.h:336
Stores Responses that want confirmation that they have arrived.
DLLEXPORT Connection(const std::string &hostname)
Creates a new connection to hostname.
Definition: Connection.cpp:34
DLLEXPORT void _HandleResponsePacket(sf::Packet &packet, bool alreadyreceived)
Definition: Connection.cpp:641
void _HandleTimeouts(int64_t timems, std::vector< std::shared_ptr< TSentType >> sentthing)
Definition: Connection.cpp:371
PING_FAIL_REASON
If true 2 ack only packets are created each time one is sent.
Definition: Connection.h:56
std::string GetRawAddress() const
Definition: Connection.h:204
int64_t LastSentPacketTime
These are used to time out the connection.
Definition: Connection.h:332
std::shared_ptr< SentRequest > _GetPossibleRequestForResponse(const std::shared_ptr< NetworkResponse > &response)
Returns a request matching the response&#39;s reference ID or NULL.
bool IsThisYours(const sf::IpAddress &sender, unsigned short sentport)
Checks does the sender and port match our corresponding values.
Definition: Connection.h:136
const auto & GetResponsesNeedingConfirmation() const
Returns responses that are tracked and haven&#39;t been acknowledged by the other side.
Definition: Connection.h:224
CONNECTION_STATE State
Marks a remote id as received.
Definition: Connection.h:295
DLLEXPORT void HandlePacket(sf::Packet &packet)
Handles a packet.
Definition: Connection.cpp:563
CONNECTION_RESTRICTION RestrictType
Definition: Connection.h:319
DLLEXPORT ~Connection()
Definition: Connection.cpp:75
sf::IpAddress TargetHost
Resolved address of the remote.
Definition: Connection.h:362
DLLEXPORT void SendCloseConnectionPacket()
Sends a packet that tells the other side to disconnect.
Definition: Connection.cpp:260
RECEIVE_GUARANTEE
Controls whether a packet is critical.
Definition: CommonNetwork.h:49
DLLEXPORT std::shared_ptr< NetworkAckField > _GetAcksToSend(uint32_t localpacketid, bool autoaddtosent=true)
Returns acks to be sent with a normal packet, or null if no acks to send.
uint32_t LastConfirmedSent
Holds the id of last local sent packet that we have received an ack for.
Definition: Connection.h:314
uint32_t LastUsedLocalID
Definition: Connection.h:308
DLLEXPORT void SetRestrictionMode(CONNECTION_RESTRICTION type)
Adds special restriction on the connection.
std::string HostName
Definition: Connection.h:359
NETWORK_RESPONSE_TYPE
Defines the type of response that the packet contains.
DLLEXPORT void _HandleRequestPacket(sf::Packet &packet, uint32_t messagenumber, bool alreadyreceived)
Definition: Connection.cpp:757
CONNECTION_STATE GetState() const
Definition: Connection.h:126
bool IsValidForSend() const
Returns true if this socket is valid for sending.
Definition: Connection.h:121
uint16_t TargetPortNumber
The remote port.
Definition: Connection.h:351
std::string RawAddress
Definition: Connection.h:355
DLLEXPORT void _SendPacketToSocket(sf::Packet &actualpackettosend)
Sends actualpackettosend to our Owner&#39;s socket.
unsigned char uint8_t
Definition: core.h:38
DLLEXPORT std::shared_ptr< SentResponse > SendPacketToConnectionWithTrackingWithoutGuarantee(const NetworkResponse &response)
Sends a response that won&#39;t be resent but it will be tracked.
Definition: Connection.cpp:192
DLLEXPORT bool Init(NetworkHandler *owninghandler)
Definition: Connection.cpp:77
DLLEXPORT void UpdateListening()
Ticks this connection, times out sent packets.
Definition: Connection.cpp:460
DLLEXPORT void _FailPacketAcks(uint32_t packetid)
Marks acks depending on packet to be lost.
std::vector< std::shared_ptr< SentAcks > > SentAckPackets
Definition: Connection.h:344
DLLEXPORT bool _HandleInternalRequest(const std::shared_ptr< NetworkRequest > &request)
Definition: Connection.cpp:822
DLLEXPORT void SetPacketsReceivedIfNotSet(NetworkAckField &acks)
Sets acks in a packet as properly sent in this.
CONNECTION_RESTRICTION
Allows restricting connections to allow only certain packets.
Definition: Connection.h:63
uint32_t LastUsedMessageNumber
Holds the number of last sent message (NetworkResponse or NetworkRequest object)
Definition: Connection.h:311
DLLEXPORT void Release()
Definition: Connection.cpp:111
unsigned short uint16_t
Definition: core.h:39
DLLEXPORT std::string GenerateFormatedAddressString() const
Returns a nicely formated address string for this connection.
DLLEXPORT void HandleRemoteAck(uint32_t localidconfirmedassent)
Called when the other side sends us an ack.
Definition: Connection.cpp:325
Moves to this state once handshake is complete.
DLLEXPORT void RemoveSucceededAcks(NetworkAckField &acks)
Removes acks that were successful in the packet from target.
void _Resend(SentRequest &toresend)
Sends a message again. Preserves message number but changes packet id.
Definition: Connection.cpp:271
Definition: Connection.h:19
std::map< uint32_t, RECEIVED_STATE > PacketReceiveStatus
DLLEXPORT std::vector< uint32_t > GetCurrentlySentAcks()
Basically a debug method (this is very slow)
Class that handles a single connection to another instance.
Definition: Connection.h:105
CONNECTION_STATE
Main state of a connection.
Definition: Connection.h:69
DLLEXPORT void CalculateNetworkPing(int packets, int allowedfails, std::function< void(int, int)> onsucceeded, std::function< void(PING_FAIL_REASON, int)> onfailed)
Calculates the ping (round-trip time) on this connection.
DLLEXPORT void _OnRestrictFail(uint16_t type)
Closes the connection and reports an error.
NetworkHandler * Owner
Packets are handled by this object.
Definition: Connection.h:298
const auto & GetPendingRequests() const
Returns the pending requests.
Definition: Connection.h:217
#define DLLEXPORT
Definition: Include.h:115
Begins the handshake with version number check.
DLLEXPORT bool IsTargetHostLocalhost()
DLLEXPORT bool _HandleInternalResponse(const std::shared_ptr< NetworkResponse > &response)
Definition: Connection.cpp:924
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:301
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:211
bool _IsAlreadyReceived(uint32_t messagenumber)
Returns true if we have already received message with number.
boost::circular_buffer< uint32_t > LastReceivedMessageNumbers
Definition: Connection.h:348
DLLEXPORT void SendKeepAlivePacket()
Sends a keep alive packet if enough time has passed.
Definition: Connection.cpp:255