Leviathan  0.8.0.0
Leviathan game engine
SentNetworkThing.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 <future>
9 #include <memory>
10 
11 namespace Leviathan {
12 
15 public:
16  enum class DONE_STATUS {
17 
18  WAITING,
19  DONE,
20  FAILED
21  };
22 
23  using CallbackType = std::function<void(bool, SentNetworkThing&)>;
24 
26  uint32_t packetid, uint32_t messagenumber, RECEIVE_GUARANTEE guarantee);
27 
29 
32  inline bool IsFinalized()
33  {
34  return IsDone.load(std::memory_order_consume) != DONE_STATUS::WAITING;
35  }
36 
38  DLLEXPORT void OnFinalized(bool succeeded);
39 
43  DLLEXPORT bool GetStatus();
44 
50  DLLEXPORT void SetWaitStatus(bool status);
51 
55  DLLEXPORT void SetAsTimed();
56 
59 
63  DLLEXPORT void SetCallback(std::shared_ptr<CallbackType> func = nullptr);
64 
65 
67 
68  virtual std::string GetTypeStr() = 0;
69 
70 
73 
77 
80 
84 
89  std::shared_ptr<std::function<void(bool, SentNetworkThing&)>> Callback;
90 
98  std::atomic<int64_t> ConfirmReceiveTime{0};
99 
102  int64_t RequestStartTime{0};
103 
105  std::atomic<DONE_STATUS> IsDone{DONE_STATUS::WAITING};
106 };
107 
108 
111 public:
112  DLLEXPORT SentRequest(uint32_t sentinpacket, uint32_t messagenumber,
113  RECEIVE_GUARANTEE guarantee, const std::shared_ptr<NetworkRequest>& request);
114 
115  DLLEXPORT std::string GetTypeStr() override;
116 
117  std::shared_ptr<NetworkResponse> GotResponse;
118 
119  std::shared_ptr<NetworkRequest> SentRequestData;
120 };
121 
124 public:
125  DLLEXPORT SentResponse(uint32_t sentinpacket, uint32_t messagenumber,
126  RECEIVE_GUARANTEE guarantee, const std::shared_ptr<NetworkResponse>& response);
127 
130  uint32_t sentinpacket, uint32_t messagenumber, const NetworkResponse& response);
131 
132  DLLEXPORT std::string GetTypeStr() override;
133 
134  std::shared_ptr<NetworkResponse> SentResponseData;
135 
138 };
139 
140 
141 } // namespace Leviathan
142 
143 #ifdef LEAK_INTO_GLOBAL
146 #endif
DLLEXPORT void OnFinalized(bool succeeded)
Called by Connection once this is done.
DLLEXPORT bool GetStatus()
Gets the status once IsFinalized returns true blocks otherwise.
DLLEXPORT SentResponse(uint32_t sentinpacket, uint32_t messagenumber, RECEIVE_GUARANTEE guarantee, const std::shared_ptr< NetworkResponse > &response)
Stores Responses that want confirmation that they have arrived.
Packet is sent once and if lost won't be resent.
std::shared_ptr< NetworkResponse > SentResponseData
DLLEXPORT SentNetworkThing(uint32_t packetid, uint32_t messagenumber, RECEIVE_GUARANTEE guarantee)
RECEIVE_GUARANTEE
Controls whether a packet is critical.
Definition: CommonNetwork.h:49
int StoredType
Stored for GetTypeStr with non-resendable responses.
DLLEXPORT std::string GetTypeStr() override
int64_t RequestStartTime
Time this was started. Used to time out this packet and calculate round trip time.
DLLEXPORT void SetCallback(std::shared_ptr< CallbackType > func=nullptr)
Binds a callback function that is called either when the packet is successfully sent or it times out.
DLLEXPORT void ResetStartTime()
Resets the start time.
DLLEXPORT std::string GetTypeStr() override
unsigned char uint8_t
Definition: core.h:38
bool IsFinalized()
Returns true once the packet has been received by the target or lost too many times.
std::atomic< int64_t > ConfirmReceiveTime
The time when this packed got marked as received.
DLLEXPORT void SetWaitStatus(bool status)
Sets the status of the wait object notifying all waiters that this has succeeded or failed.
std::shared_ptr< NetworkRequest > SentRequestData
std::function< void(bool, SentNetworkThing &)> CallbackType
DLLEXPORT void SetCallbackFunc(CallbackType func)
DLLEXPORT void SetAsTimed()
Sets this packet as a timed packet.
RECEIVE_GUARANTEE Resend
If not RECEIVE_GUARANTEE::None this packet will be resent if considered lost.
virtual std::string GetTypeStr()=0
uint32_t PacketNumber
Contained in Local packet id.
#define DLLEXPORT
Definition: Include.h:84
std::shared_ptr< std::function< void(bool, SentNetworkThing &)> > Callback
std::shared_ptr< NetworkResponse > GotResponse
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
std::atomic< DONE_STATUS > IsDone
Set to true once this object is no longer used.
unsigned int uint32_t
Definition: core.h:40
Stores Requests while they are waiting for a response.
DLLEXPORT SentRequest(uint32_t sentinpacket, uint32_t messagenumber, RECEIVE_GUARANTEE guarantee, const std::shared_ptr< NetworkRequest > &request)
Represents a sent packet and holds all kinds of data for it.