2019-06-26 21:39:29 +00:00
|
|
|
#ifndef LLARP_ROUTER_OUTBOUND_MESSAGE_HANDLER_HPP
|
|
|
|
#define LLARP_ROUTER_OUTBOUND_MESSAGE_HANDLER_HPP
|
|
|
|
|
|
|
|
#include <router/i_outbound_message_handler.hpp>
|
|
|
|
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2019-10-25 21:13:11 +00:00
|
|
|
#include <util/thread/queue.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/threading.hpp>
|
2019-10-25 21:13:11 +00:00
|
|
|
#include <path/path_types.hpp>
|
2019-06-26 21:39:29 +00:00
|
|
|
#include <router_id.hpp>
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
struct llarp_buffer_t;
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct ILinkManager;
|
2019-07-28 12:35:07 +00:00
|
|
|
class Logic;
|
2019-06-26 21:39:29 +00:00
|
|
|
enum class SessionResult;
|
|
|
|
|
|
|
|
struct OutboundMessageHandler final : public IOutboundMessageHandler
|
|
|
|
{
|
|
|
|
public:
|
2019-07-30 23:42:13 +00:00
|
|
|
~OutboundMessageHandler() override = default;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2019-10-25 21:13:11 +00:00
|
|
|
OutboundMessageHandler(size_t maxQueueSize = MAX_OUTBOUND_QUEUE_SIZE);
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
bool
|
|
|
|
QueueMessage(const RouterID &remote, const ILinkMessage *msg,
|
2019-08-07 23:18:56 +00:00
|
|
|
SendStatusHandler callback) override LOCKS_EXCLUDED(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2019-10-25 21:13:11 +00:00
|
|
|
void
|
|
|
|
Tick() override;
|
|
|
|
|
|
|
|
void
|
|
|
|
QueueRemoveEmptyPath(const PathID_t &pathid) override;
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const override;
|
|
|
|
|
|
|
|
void
|
|
|
|
Init(ILinkManager *linkManager, std::shared_ptr< Logic > logic);
|
|
|
|
|
|
|
|
private:
|
2019-10-25 21:13:11 +00:00
|
|
|
using Message = std::pair< std::vector< byte_t >, SendStatusHandler >;
|
|
|
|
|
|
|
|
struct MessageQueueEntry
|
|
|
|
{
|
2020-01-17 16:33:37 +00:00
|
|
|
uint16_t priority;
|
2019-10-25 21:13:11 +00:00
|
|
|
Message message;
|
|
|
|
PathID_t pathid;
|
|
|
|
RouterID router;
|
2020-01-17 16:33:37 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const MessageQueueEntry &other) const
|
|
|
|
{
|
|
|
|
return other.priority < priority;
|
|
|
|
}
|
2019-10-25 21:13:11 +00:00
|
|
|
};
|
|
|
|
|
2020-01-17 17:14:24 +00:00
|
|
|
struct MessageQueueStats
|
|
|
|
{
|
2020-01-17 18:19:53 +00:00
|
|
|
uint64_t queued = 0;
|
|
|
|
uint64_t dropped = 0;
|
|
|
|
uint64_t sent = 0;
|
2020-01-17 17:14:24 +00:00
|
|
|
uint32_t queueWatermark = 0;
|
|
|
|
|
|
|
|
uint32_t perTickMax = 0;
|
2020-01-17 18:19:53 +00:00
|
|
|
uint32_t numTicks = 0;
|
2020-01-17 17:14:24 +00:00
|
|
|
};
|
|
|
|
|
2020-01-17 16:33:37 +00:00
|
|
|
using MessageQueue = std::priority_queue< MessageQueueEntry >;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
OnSessionEstablished(const RouterID &router);
|
|
|
|
|
|
|
|
void
|
|
|
|
OnConnectTimeout(const RouterID &router);
|
|
|
|
|
|
|
|
void
|
|
|
|
OnRouterNotFound(const RouterID &router);
|
|
|
|
|
|
|
|
void
|
|
|
|
OnInvalidRouter(const RouterID &router);
|
|
|
|
|
|
|
|
void
|
|
|
|
OnNoLink(const RouterID &router);
|
|
|
|
|
|
|
|
void
|
|
|
|
OnSessionResult(const RouterID &router, const SessionResult result);
|
|
|
|
|
|
|
|
void
|
|
|
|
DoCallback(SendStatusHandler callback, SendStatus status);
|
|
|
|
|
|
|
|
void
|
|
|
|
QueueSessionCreation(const RouterID &remote);
|
|
|
|
|
|
|
|
bool
|
|
|
|
EncodeBuffer(const ILinkMessage *msg, llarp_buffer_t &buf);
|
|
|
|
|
|
|
|
bool
|
|
|
|
Send(const RouterID &remote, const Message &msg);
|
|
|
|
|
|
|
|
bool
|
|
|
|
SendIfSession(const RouterID &remote, const Message &msg);
|
|
|
|
|
2019-10-25 21:13:11 +00:00
|
|
|
bool
|
|
|
|
QueueOutboundMessage(const RouterID &remote, Message &&msg,
|
2020-01-17 16:33:37 +00:00
|
|
|
const PathID_t &pathid, uint16_t priority = 0);
|
2019-10-25 21:13:11 +00:00
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
2019-10-25 21:13:11 +00:00
|
|
|
ProcessOutboundQueue();
|
|
|
|
|
|
|
|
void
|
|
|
|
RemoveEmptyPathQueues();
|
|
|
|
|
|
|
|
void
|
|
|
|
SendRoundRobin();
|
|
|
|
|
|
|
|
void
|
|
|
|
FinalizeSessionRequest(const RouterID &router, SendStatus status)
|
2019-08-07 23:18:56 +00:00
|
|
|
LOCKS_EXCLUDED(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2019-10-25 21:13:11 +00:00
|
|
|
llarp::thread::Queue< MessageQueueEntry > outboundQueue;
|
|
|
|
llarp::thread::Queue< PathID_t > removedPaths;
|
|
|
|
bool removedSomePaths;
|
|
|
|
|
|
|
|
mutable util::Mutex _mutex; // protects pendingSessionMessageQueues
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
std::unordered_map< RouterID, MessageQueue, RouterID::Hash >
|
2019-10-25 21:13:11 +00:00
|
|
|
pendingSessionMessageQueues GUARDED_BY(_mutex);
|
|
|
|
|
|
|
|
std::unordered_map< PathID_t, MessageQueue, PathID_t::Hash >
|
2019-10-30 14:18:38 +00:00
|
|
|
outboundMessageQueues;
|
2019-10-25 21:13:11 +00:00
|
|
|
|
|
|
|
std::queue< PathID_t > roundRobinOrder;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
ILinkManager *_linkManager;
|
|
|
|
std::shared_ptr< Logic > _logic;
|
2019-10-25 21:13:11 +00:00
|
|
|
|
2019-11-14 18:50:45 +00:00
|
|
|
util::ContentionKiller m_Killer;
|
|
|
|
|
2019-10-29 02:28:59 +00:00
|
|
|
// paths cannot have pathid "0", so it can be used as the "pathid"
|
|
|
|
// for non-traffic (control) messages, so they can be prioritized.
|
|
|
|
static const PathID_t zeroID;
|
2020-01-17 17:14:24 +00:00
|
|
|
|
|
|
|
MessageQueueStats m_queueStats;
|
2019-06-26 21:39:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif // LLARP_ROUTER_OUTBOUND_MESSAGE_HANDLER_HPP
|