Leviathan  0.8.0.0
Leviathan game engine
Leviathan::SentNetworkThing Class Reference

Represents a sent packet and holds all kinds of data for it. More...

#include <SentNetworkThing.h>

+ Inheritance diagram for Leviathan::SentNetworkThing:

Public Types

enum  DONE_STATUS { DONE_STATUS::WAITING, DONE_STATUS::DONE, DONE_STATUS::FAILED }
 
using CallbackType = std::function< void(bool, SentNetworkThing &)>
 

Public Member Functions

DLLEXPORT SentNetworkThing (uint32_t packetid, uint32_t messagenumber, RECEIVE_GUARANTEE guarantee)
 
DLLEXPORT ~SentNetworkThing ()
 
bool IsFinalized ()
 Returns true once the packet has been received by the target or lost too many times. More...
 
DLLEXPORT void OnFinalized (bool succeeded)
 Called by Connection once this is done. More...
 
DLLEXPORT bool GetStatus ()
 Gets the status once IsFinalized returns true blocks otherwise. More...
 
DLLEXPORT void SetWaitStatus (bool status)
 Sets the status of the wait object notifying all waiters that this has succeeded or failed. More...
 
DLLEXPORT void SetAsTimed ()
 Sets this packet as a timed packet. More...
 
DLLEXPORT void ResetStartTime ()
 Resets the start time. More...
 
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. More...
 
DLLEXPORT void SetCallbackFunc (CallbackType func)
 

Public Attributes

uint32_t PacketNumber
 Contained in Local packet id. More...
 
const uint32_t MessageNumber
 
RECEIVE_GUARANTEE Resend = RECEIVE_GUARANTEE::None
 If not RECEIVE_GUARANTEE::None this packet will be resent if considered lost. More...
 
uint8_t AttemptNumber = 1
 
std::shared_ptr< std::function< void(bool, SentNetworkThing &)> > Callback
 
std::atomic< int64_t > ConfirmReceiveTime { 0 }
 The time when this packed got marked as received. More...
 
int64_t RequestStartTime { 0 }
 Time this was started. Used to time out this packet and calculate round trip time. More...
 
std::atomic< DONE_STATUSIsDone { DONE_STATUS::WAITING }
 Set to true once this object is no longer used. More...
 

Detailed Description

Represents a sent packet and holds all kinds of data for it.

Definition at line 14 of file SentNetworkThing.h.

Member Typedef Documentation

◆ CallbackType

using Leviathan::SentNetworkThing::CallbackType = std::function<void(bool, SentNetworkThing&)>

Definition at line 24 of file SentNetworkThing.h.

Member Enumeration Documentation

◆ DONE_STATUS

Enumerator
WAITING 
DONE 
FAILED 

Definition at line 17 of file SentNetworkThing.h.

17  {
18 
19  WAITING,
20  DONE,
21  FAILED
22  };

Constructor & Destructor Documentation

◆ SentNetworkThing()

DLLEXPORT Leviathan::SentNetworkThing::SentNetworkThing ( uint32_t  packetid,
uint32_t  messagenumber,
RECEIVE_GUARANTEE  guarantee 
)

Definition at line 9 of file SentNetworkThing.cpp.

10  :
11  PacketNumber(packetid), MessageNumber(messagenumber), Resend(guarantee),
13 {
14 
15 }
int64_t RequestStartTime
Time this was started. Used to time out this packet and calculate round trip time.
static DLLEXPORT int64_t GetTimeMs64()
RECEIVE_GUARANTEE Resend
If not RECEIVE_GUARANTEE::None this packet will be resent if considered lost.
uint32_t PacketNumber
Contained in Local packet id.

◆ ~SentNetworkThing()

DLLEXPORT Leviathan::SentNetworkThing::~SentNetworkThing ( )
inline

Definition at line 29 of file SentNetworkThing.h.

29 { }

Member Function Documentation

◆ GetStatus()

DLLEXPORT bool SentNetworkThing::GetStatus ( )

Gets the status once IsFinalized returns true blocks otherwise.

Returns
True when the packet has been successfully received, false if lost
Todo:
Make sure this cannot deadlock

Definition at line 48 of file SentNetworkThing.cpp.

48  {
49 
50  while(IsDone.load(std::memory_order_acquire) == DONE_STATUS::WAITING){
51 
52  std::this_thread::sleep_for(std::chrono::milliseconds(10));
53  }
54 
55  return IsDone == DONE_STATUS::DONE;
56 }
std::atomic< DONE_STATUS > IsDone
Set to true once this object is no longer used.

◆ IsFinalized()

bool Leviathan::SentNetworkThing::IsFinalized ( )
inline

Returns true once the packet has been received by the target or lost too many times.

Definition at line 33 of file SentNetworkThing.h.

33  {
34 
35  return IsDone.load(std::memory_order_consume) != DONE_STATUS::WAITING;
36  }
std::atomic< DONE_STATUS > IsDone
Set to true once this object is no longer used.

◆ OnFinalized()

DLLEXPORT void Leviathan::SentNetworkThing::OnFinalized ( bool  succeeded)

Called by Connection once this is done.

Definition at line 30 of file SentNetworkThing.cpp.

30  {
31 
32  if(!succeeded){
33 
34  SetWaitStatus(false);
35  return;
36  }
37 
38  if(ConfirmReceiveTime.load(std::memory_order_consume) == 1){
39 
41  std::memory_order_release);
42  }
43 
44  // We want to notify all waiters that it has been received //
45  SetWaitStatus(true);
46 }
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...

◆ ResetStartTime()

DLLEXPORT void SentNetworkThing::ResetStartTime ( )

Resets the start time.

Definition at line 17 of file SentNetworkThing.cpp.

17  {
18 
20 }
int64_t RequestStartTime
Time this was started. Used to time out this packet and calculate round trip time.
static DLLEXPORT int64_t GetTimeMs64()

◆ SetAsTimed()

DLLEXPORT void SentNetworkThing::SetAsTimed ( )

Sets this packet as a timed packet.

Note
A timed package will have the ConfirmReceiveTime set to the time a response (or receive notification) is received

Definition at line 58 of file SentNetworkThing.cpp.

58  {
59 
60  ConfirmReceiveTime.store(1, std::memory_order_release);
61 }
std::atomic< int64_t > ConfirmReceiveTime
The time when this packed got marked as received.

◆ SetCallback()

DLLEXPORT void SentNetworkThing::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.

Bug:
This can corrupt the arguments passed to this function, not recommended for use

Definition at line 63 of file SentNetworkThing.cpp.

64 {
65  Callback = func;
66 }
std::shared_ptr< std::function< void(bool, SentNetworkThing &)> > Callback

◆ SetCallbackFunc()

DLLEXPORT void Leviathan::SentNetworkThing::SetCallbackFunc ( CallbackType  func)

Definition at line 68 of file SentNetworkThing.cpp.

68  {
69 
70  Callback = std::make_shared<CallbackType>(std::move(func));
71 }
std::shared_ptr< std::function< void(bool, SentNetworkThing &)> > Callback

◆ SetWaitStatus()

DLLEXPORT void SentNetworkThing::SetWaitStatus ( bool  status)

Sets the status of the wait object notifying all waiters that this has succeeded or failed.

Will also call the Callback if one is set

Note
May only be called once

Definition at line 22 of file SentNetworkThing.cpp.

22  {
23 
24  IsDone.store(status ? DONE_STATUS::DONE : DONE_STATUS::FAILED, std::memory_order_release);
25 
26  if(Callback)
27  (*Callback)(status, *this);
28 }
std::shared_ptr< std::function< void(bool, SentNetworkThing &)> > Callback
std::atomic< DONE_STATUS > IsDone
Set to true once this object is no longer used.

Member Data Documentation

◆ AttemptNumber

uint8_t Leviathan::SentNetworkThing::AttemptNumber = 1

Used to detect when a critical packet is lost or if this packet has a specific number of resends

Definition at line 82 of file SentNetworkThing.h.

◆ Callback

std::shared_ptr<std::function<void(bool, SentNetworkThing&)> > Leviathan::SentNetworkThing::Callback

Callback function called when succeeded or failed May only be called by the receiving thread when removing this from the queue. May not be changed after settings to make sure that no race conditions exist

Definition at line 88 of file SentNetworkThing.h.

◆ ConfirmReceiveTime

std::atomic<int64_t> Leviathan::SentNetworkThing::ConfirmReceiveTime { 0 }

The time when this packed got marked as received.

This will roughly be the time it took for the packet to reach the destination and return the round-trip time

Note
This will only be set if this value is set to 1 before the packet is sent
This value is only valid if the packet wasn't lost (failed requests have this unset)

Definition at line 97 of file SentNetworkThing.h.

◆ IsDone

std::atomic<DONE_STATUS> Leviathan::SentNetworkThing::IsDone { DONE_STATUS::WAITING }

Set to true once this object is no longer used.

Definition at line 104 of file SentNetworkThing.h.

◆ MessageNumber

const uint32_t Leviathan::SentNetworkThing::MessageNumber

The ID of the message that was sent (this stays same until this has failed or succeeded)

Definition at line 75 of file SentNetworkThing.h.

◆ PacketNumber

uint32_t Leviathan::SentNetworkThing::PacketNumber

Contained in Local packet id.

Definition at line 71 of file SentNetworkThing.h.

◆ RequestStartTime

int64_t Leviathan::SentNetworkThing::RequestStartTime { 0 }

Time this was started. Used to time out this packet and calculate round trip time.

Definition at line 101 of file SentNetworkThing.h.

◆ Resend

RECEIVE_GUARANTEE Leviathan::SentNetworkThing::Resend = RECEIVE_GUARANTEE::None

If not RECEIVE_GUARANTEE::None this packet will be resent if considered lost.

Definition at line 78 of file SentNetworkThing.h.


The documentation for this class was generated from the following files: