Leviathan  0.8.0.0
Leviathan game engine
SentNetworkThing.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "SentNetworkThing.h"
3 
4 #include "NetworkRequest.h"
5 #include "NetworkResponse.h"
6 
7 #include "TimeIncludes.h"
8 
9 using namespace Leviathan;
10 // ------------------------------------ //
11 
13  uint32_t packetid, uint32_t messagenumber, RECEIVE_GUARANTEE guarantee) :
14  PacketNumber(packetid),
15  MessageNumber(messagenumber), Resend(guarantee), RequestStartTime(Time::GetTimeMs64())
16 {}
17 
19 {
20 
22 }
23 
25 {
26  IsDone.store(status ? DONE_STATUS::DONE : DONE_STATUS::FAILED, std::memory_order_release);
27 
28  if(Callback)
29  (*Callback)(status, *this);
30 }
31 
33 {
34  if(!succeeded) {
35 
36  SetWaitStatus(false);
37  return;
38  }
39 
40  if(ConfirmReceiveTime.load(std::memory_order_consume) == 1) {
41 
42  ConfirmReceiveTime.store(Time::GetTimeMs64(), std::memory_order_release);
43  }
44 
45  // We want to notify all waiters that it has been received //
46  SetWaitStatus(true);
47 }
48 
50 {
51 
52  while(IsDone.load(std::memory_order_acquire) == DONE_STATUS::WAITING) {
53 
54  std::this_thread::sleep_for(std::chrono::milliseconds(10));
55  }
56 
57  return IsDone == DONE_STATUS::DONE;
58 }
59 
61 {
62 
63  ConfirmReceiveTime.store(1, std::memory_order_release);
64 }
65 
66 DLLEXPORT void SentNetworkThing::SetCallback(std::shared_ptr<CallbackType> func)
67 {
68  Callback = func;
69 }
70 
72 {
73 
74  Callback = std::make_shared<CallbackType>(std::move(func));
75 }
76 
77 // ------------------------------------ //
78 // SentRequest
80  RECEIVE_GUARANTEE guarantee, const std::shared_ptr<NetworkRequest>& request) :
81  SentNetworkThing(sentinpacket, messagenumber, guarantee),
82  SentRequestData(request)
83 {}
84 
86 {
87  if(SentRequestData) {
88  return SentRequestData->GetTypeStr();
89  } else {
90  return "null";
91  }
92 }
93 
94 // ------------------------------------ //
95 // SentRequest
97  RECEIVE_GUARANTEE guarantee, const std::shared_ptr<NetworkResponse>& response) :
98  SentNetworkThing(sentinpacket, messagenumber, guarantee),
99  SentResponseData(response), StoredType(static_cast<int>(response->GetType()))
100 {}
101 
103  uint32_t sentinpacket, uint32_t messagenumber, const NetworkResponse& response) :
104  SentNetworkThing(sentinpacket, messagenumber, RECEIVE_GUARANTEE::None),
105  StoredType(static_cast<int>(response.GetType()))
106 {}
107 
109 {
110  // A bit hacky way to call the member function
111  return ResponseNone(static_cast<NETWORK_RESPONSE_TYPE>(StoredType), 0).GetTypeStr();
112 }
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)
Helper class for getting the time.
Definition: TimeIncludes.h:23
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() const
DLLEXPORT std::string GetTypeStr() override
static DLLEXPORT int64_t GetTimeMs64()
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.
#define DLLEXPORT
Definition: Include.h:84
std::shared_ptr< std::function< void(bool, SentNetworkThing &)> > Callback
Empty keep alive response.
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
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.