2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2019-08-22 20:53:27 +00:00
|
|
|
#include <vector>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/constants/link_layer.hpp>
|
|
|
|
#include <llarp/link/session.hpp>
|
|
|
|
#include <llarp/util/aligned.hpp>
|
|
|
|
#include <llarp/util/buffer.hpp>
|
|
|
|
#include <llarp/util/types.hpp>
|
2019-08-22 20:53:27 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace iwp
|
|
|
|
{
|
|
|
|
enum Command
|
|
|
|
{
|
|
|
|
/// keep alive message
|
|
|
|
ePING = 0,
|
|
|
|
/// begin transission
|
|
|
|
eXMIT = 1,
|
|
|
|
/// fragment data
|
|
|
|
eDATA = 2,
|
|
|
|
/// acknolege fragments
|
|
|
|
eACKS = 3,
|
2019-08-23 11:32:52 +00:00
|
|
|
/// negative ack
|
|
|
|
eNACK = 4,
|
2019-09-05 13:21:35 +00:00
|
|
|
/// multiack
|
2019-09-05 13:36:46 +00:00
|
|
|
eMACK = 5,
|
|
|
|
/// close session
|
2019-09-16 16:12:05 +00:00
|
|
|
eCLOS = 0xff,
|
2019-08-22 20:53:27 +00:00
|
|
|
};
|
|
|
|
|
2019-09-16 16:12:05 +00:00
|
|
|
/// max size of data fragments
|
2019-08-22 20:53:27 +00:00
|
|
|
static constexpr size_t FragmentSize = 1024;
|
2019-09-16 16:12:05 +00:00
|
|
|
/// plaintext header overhead size
|
|
|
|
static constexpr size_t CommandOverhead = 2;
|
2019-08-22 20:53:27 +00:00
|
|
|
|
|
|
|
struct OutboundMessage
|
|
|
|
{
|
2019-08-26 11:16:46 +00:00
|
|
|
OutboundMessage() = default;
|
2020-04-07 18:38:56 +00:00
|
|
|
OutboundMessage(
|
|
|
|
uint64_t msgid,
|
|
|
|
ILinkSession::Message_t data,
|
|
|
|
llarp_time_t now,
|
2022-03-30 20:21:57 +00:00
|
|
|
ILinkSession::CompletionHandler handler,
|
|
|
|
uint16_t priority);
|
2019-08-22 20:53:27 +00:00
|
|
|
|
2019-09-12 14:34:27 +00:00
|
|
|
ILinkSession::Message_t m_Data;
|
2019-08-22 20:53:27 +00:00
|
|
|
uint64_t m_MsgID = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
std::bitset<MAX_LINK_MSG_SIZE / FragmentSize> m_Acks;
|
2019-08-22 20:53:27 +00:00
|
|
|
ILinkSession::CompletionHandler m_Completed;
|
2020-02-24 19:40:45 +00:00
|
|
|
llarp_time_t m_LastFlush = 0s;
|
2019-09-12 14:34:27 +00:00
|
|
|
ShortHash m_Digest;
|
2020-02-24 19:40:45 +00:00
|
|
|
llarp_time_t m_StartedAt = 0s;
|
2022-03-30 20:21:57 +00:00
|
|
|
uint16_t m_ResendPriority;
|
|
|
|
|
|
|
|
bool
|
2022-10-13 17:19:25 +00:00
|
|
|
operator<(const OutboundMessage& other) const
|
2022-03-30 20:21:57 +00:00
|
|
|
{
|
|
|
|
// yes, the first order is reversed as higher means more important
|
|
|
|
// second part is for queue order
|
2022-10-13 17:19:25 +00:00
|
|
|
int prioA = -m_ResendPriority, prioB = -other.m_ResendPriority;
|
|
|
|
return std::tie(prioA, m_MsgID) < std::tie(prioB, other.m_MsgID);
|
2022-03-30 20:21:57 +00:00
|
|
|
}
|
2019-08-22 20:53:27 +00:00
|
|
|
|
2019-09-12 14:34:27 +00:00
|
|
|
ILinkSession::Packet_t
|
2019-08-22 20:53:27 +00:00
|
|
|
XMIT() const;
|
|
|
|
|
|
|
|
void
|
|
|
|
Ack(byte_t bitmask);
|
|
|
|
|
2019-08-23 11:32:52 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
FlushUnAcked(std::function<void(ILinkSession::Packet_t)> sendpkt, llarp_time_t now);
|
2019-08-22 20:53:27 +00:00
|
|
|
|
2019-08-23 11:32:52 +00:00
|
|
|
bool
|
2019-08-22 20:53:27 +00:00
|
|
|
ShouldFlush(llarp_time_t now) const;
|
|
|
|
|
|
|
|
void
|
|
|
|
Completed();
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsTransmitted() const;
|
2019-08-27 12:07:48 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
IsTimedOut(llarp_time_t now) const;
|
|
|
|
|
|
|
|
void
|
|
|
|
InformTimeout();
|
2019-08-22 20:53:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct InboundMessage
|
|
|
|
{
|
2019-08-26 11:16:46 +00:00
|
|
|
InboundMessage() = default;
|
2020-04-07 18:38:56 +00:00
|
|
|
InboundMessage(uint64_t msgid, uint16_t sz, ShortHash h, llarp_time_t now);
|
2019-08-22 20:53:27 +00:00
|
|
|
|
2019-09-16 16:12:05 +00:00
|
|
|
ILinkSession::Message_t m_Data;
|
2019-08-22 20:53:27 +00:00
|
|
|
ShortHash m_Digset;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint64_t m_MsgID = 0;
|
|
|
|
llarp_time_t m_LastACKSent = 0s;
|
2020-02-24 19:40:45 +00:00
|
|
|
llarp_time_t m_LastActiveAt = 0s;
|
2020-04-07 18:38:56 +00:00
|
|
|
std::bitset<MAX_LINK_MSG_SIZE / FragmentSize> m_Acks;
|
2019-08-22 20:53:27 +00:00
|
|
|
|
|
|
|
void
|
2019-09-12 14:34:27 +00:00
|
|
|
HandleData(uint16_t idx, const llarp_buffer_t& buf, llarp_time_t now);
|
2019-08-22 20:53:27 +00:00
|
|
|
|
2019-08-23 11:32:52 +00:00
|
|
|
bool
|
2019-08-22 20:53:27 +00:00
|
|
|
IsCompleted() const;
|
|
|
|
|
2019-08-27 12:07:48 +00:00
|
|
|
bool
|
|
|
|
IsTimedOut(llarp_time_t now) const;
|
|
|
|
|
2019-08-23 11:32:52 +00:00
|
|
|
bool
|
2019-08-22 20:53:27 +00:00
|
|
|
Verify() const;
|
|
|
|
|
2019-09-05 13:21:35 +00:00
|
|
|
byte_t
|
|
|
|
AcksBitmask() const;
|
|
|
|
|
2019-08-23 11:32:52 +00:00
|
|
|
bool
|
2019-08-22 20:53:27 +00:00
|
|
|
ShouldSendACKS(llarp_time_t now) const;
|
|
|
|
|
2019-08-23 11:32:52 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
SendACKS(std::function<void(ILinkSession::Packet_t)> sendpkt, llarp_time_t now);
|
2019-08-22 20:53:27 +00:00
|
|
|
|
2019-09-12 14:34:27 +00:00
|
|
|
ILinkSession::Packet_t
|
2019-08-22 20:53:27 +00:00
|
|
|
ACKS() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace iwp
|
|
|
|
} // namespace llarp
|