2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/buffer.hpp>
|
|
|
|
#include <llarp/util/time.hpp>
|
|
|
|
#include <llarp/util/thread/threading.hpp>
|
|
|
|
#include <llarp/constants/evloop.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-11-02 12:35:20 +00:00
|
|
|
#include <algorithm>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <deque>
|
|
|
|
#include <list>
|
2019-03-03 20:51:47 +00:00
|
|
|
#include <future>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <utility>
|
2018-10-04 11:20:08 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2021-03-02 02:06:20 +00:00
|
|
|
struct SockAddr;
|
|
|
|
struct UDPHandle;
|
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
namespace vpn
|
2018-10-29 16:48:36 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
class NetworkInterface;
|
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
namespace net
|
2021-02-22 13:26:32 +00:00
|
|
|
{
|
2021-03-02 02:06:20 +00:00
|
|
|
struct IPPacket;
|
|
|
|
}
|
2021-02-22 13:26:32 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
/// distinct event loop waker upper; used to idempotently schedule a task on the next event loop
|
|
|
|
///
|
|
|
|
/// Created via EventLoop::make_waker(...).
|
|
|
|
class EventLoopWakeup
|
|
|
|
{
|
2021-02-22 13:26:32 +00:00
|
|
|
public:
|
2021-03-02 02:06:20 +00:00
|
|
|
/// Destructor: remove the task from the event loop task. (Note that destruction here only
|
|
|
|
/// initiates removal of the task from the underlying event loop: it is *possible* for the
|
|
|
|
/// callback to fire again if already triggered depending on the underlying implementation).
|
2021-02-22 13:26:32 +00:00
|
|
|
virtual ~EventLoopWakeup() = default;
|
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
/// trigger this task to run on the next event loop iteration; does nothing if already
|
|
|
|
/// triggered.
|
2021-02-22 13:26:32 +00:00
|
|
|
virtual void
|
2021-03-02 02:06:20 +00:00
|
|
|
Trigger() = 0;
|
|
|
|
};
|
2021-02-22 13:26:32 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
/// holds a repeated task on the event loop; the task is removed on destruction
|
|
|
|
class EventLoopRepeater
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Destructor: if the task has been started then it is removed from the event loop. Note
|
|
|
|
// that it is possible for a task to fire *after* destruction of this container;
|
|
|
|
// destruction only initiates removal of the periodic task.
|
|
|
|
virtual ~EventLoopRepeater() = default;
|
|
|
|
|
|
|
|
// Starts the repeater to call `task` every `every` period.
|
2021-02-22 13:26:32 +00:00
|
|
|
virtual void
|
2021-03-02 02:06:20 +00:00
|
|
|
start(llarp_time_t every, std::function<void()> task) = 0;
|
2021-02-22 13:26:32 +00:00
|
|
|
};
|
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
// this (nearly!) abstract base class
|
|
|
|
// is overriden for each platform
|
2021-03-02 07:02:59 +00:00
|
|
|
class EventLoop
|
2021-01-11 23:13:22 +00:00
|
|
|
{
|
2021-03-02 07:02:59 +00:00
|
|
|
public:
|
2021-03-02 02:06:20 +00:00
|
|
|
// Runs the event loop. This does not return until sometime after `stop()` is called (and so
|
|
|
|
// typically you want to run this in its own thread).
|
|
|
|
virtual void
|
2021-03-02 07:02:59 +00:00
|
|
|
run() = 0;
|
2018-10-24 18:02:42 +00:00
|
|
|
|
2018-10-25 12:39:32 +00:00
|
|
|
virtual bool
|
2021-01-11 23:13:22 +00:00
|
|
|
running() const = 0;
|
2018-10-24 18:02:42 +00:00
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
virtual llarp_time_t
|
|
|
|
time_now() const
|
2018-10-24 18:02:42 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
return llarp::time_now_ms();
|
2018-10-24 18:02:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 18:59:39 +00:00
|
|
|
// Triggers an event loop wakeup; use when something has been done that requires the event loop
|
|
|
|
// to wake up (e.g. adding to queues). This is called implicitly by call() and call_soon().
|
|
|
|
// Idempotent and thread-safe.
|
|
|
|
virtual void
|
|
|
|
wakeup() = 0;
|
|
|
|
|
2021-03-02 07:02:59 +00:00
|
|
|
// Calls a function/lambda/etc. If invoked from within the event loop itself this calls the
|
|
|
|
// given lambda immediately; otherwise it passes it to `call_soon()` to be queued to run at the
|
|
|
|
// next event loop iteration.
|
2021-03-02 15:23:38 +00:00
|
|
|
template <typename Callable>
|
|
|
|
void
|
|
|
|
call(Callable&& f)
|
|
|
|
{
|
2021-03-02 07:02:59 +00:00
|
|
|
if (inEventLoop())
|
2021-03-03 18:59:39 +00:00
|
|
|
{
|
2021-03-02 07:02:59 +00:00
|
|
|
f();
|
2021-03-03 18:59:39 +00:00
|
|
|
wakeup();
|
|
|
|
}
|
2021-03-02 07:02:59 +00:00
|
|
|
else
|
|
|
|
call_soon(std::forward<Callable>(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queues a function to be called on the next event loop cycle and triggers it to be called as
|
|
|
|
// soon as possible; can be called from any thread. Note that, unlike `call()`, this queues the
|
|
|
|
// job even if called from the event loop thread itself and so you *usually* want to use
|
|
|
|
// `call()` instead.
|
2021-01-11 23:13:22 +00:00
|
|
|
virtual void
|
2021-03-02 07:02:59 +00:00
|
|
|
call_soon(std::function<void(void)> f) = 0;
|
2018-11-02 12:35:20 +00:00
|
|
|
|
2021-03-02 07:02:59 +00:00
|
|
|
// Adds a timer to the event loop to invoke the given callback after a delay.
|
2021-01-11 23:13:22 +00:00
|
|
|
virtual void
|
2021-03-02 07:02:59 +00:00
|
|
|
call_later(llarp_time_t delay_ms, std::function<void(void)> callback) = 0;
|
|
|
|
|
|
|
|
// Created a repeated timer that fires ever `repeat` time unit. Lifetime of the event
|
|
|
|
// is tied to `owner`: callbacks will be invoked so long as `owner` remains alive, but
|
|
|
|
// the first time it repeats after `owner` has been destroyed the internal timer object will
|
|
|
|
// be destroyed and no more callbacks will be invoked.
|
|
|
|
//
|
|
|
|
// Intended to be used as:
|
|
|
|
//
|
|
|
|
// loop->call_every(100ms, weak_from_this(), [this] { some_func(); });
|
|
|
|
//
|
|
|
|
// Alternative, when shared_from_this isn't available for a type, you can use a local member
|
|
|
|
// shared_ptr (or even create a simple one, for more fine-grained control) to tie the lifetime:
|
|
|
|
//
|
|
|
|
// m_keepalive = std::make_shared<int>(42);
|
|
|
|
// loop->call_every(100ms, m_keepalive, [this] { some_func(); });
|
|
|
|
//
|
2021-03-02 15:23:38 +00:00
|
|
|
template <typename Callable> // Templated so that the compiler can inline the call
|
2021-03-02 07:02:59 +00:00
|
|
|
void
|
|
|
|
call_every(llarp_time_t repeat, std::weak_ptr<void> owner, Callable f)
|
|
|
|
{
|
|
|
|
auto repeater = make_repeater();
|
2021-03-02 15:23:38 +00:00
|
|
|
auto& r = *repeater; // reference *before* we pass ownership into the lambda below
|
2021-03-02 07:02:59 +00:00
|
|
|
r.start(
|
|
|
|
repeat,
|
|
|
|
[repeater = std::move(repeater), owner = std::move(owner), f = std::move(f)]() mutable {
|
|
|
|
if (auto ptr = owner.lock())
|
|
|
|
f();
|
|
|
|
else
|
2021-03-02 15:23:38 +00:00
|
|
|
repeater.reset(); // Trigger timer removal on tied object destruction (we should be
|
|
|
|
// the only thing holding the repeater; ideally it would be a
|
|
|
|
// unique_ptr, but std::function says nuh-uh).
|
2021-03-02 07:02:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wraps a lambda with a lambda that triggers it to be called via loop->call()
|
|
|
|
// when invoked. E.g.:
|
|
|
|
//
|
|
|
|
// auto x = loop->make_caller([] (int a) { std::cerr << a; });
|
|
|
|
// x(42);
|
|
|
|
// x(99);
|
|
|
|
//
|
|
|
|
// will schedule two calls of the inner lambda (with different arguments) in the event loop.
|
|
|
|
// Arguments are forwarded to the inner lambda (allowing moving arguments into it).
|
|
|
|
template <typename Callable>
|
|
|
|
auto
|
|
|
|
make_caller(Callable f)
|
|
|
|
{
|
|
|
|
return [this, f = std::move(f)](auto&&... args) {
|
|
|
|
if (inEventLoop())
|
|
|
|
return f(std::forward<decltype(args)>(args)...);
|
|
|
|
|
|
|
|
// This shared pointer in a pain in the ass but needed because this lambda is going into a
|
|
|
|
// std::function that only accepts copyable lambdas. I *want* to simply capture:
|
|
|
|
// args=std::make_tuple(std::forward<decltype(args)>(args)...) but that fails if any given
|
|
|
|
// arguments aren't copyable (because of std::function). Dammit.
|
|
|
|
auto args_tuple_ptr = std::make_shared<std::tuple<std::decay_t<decltype(args)>...>>(
|
|
|
|
std::forward<decltype(args)>(args)...);
|
|
|
|
call_soon([f, args = std::move(args_tuple_ptr)]() mutable {
|
|
|
|
// Moving away the tuple args here is okay because this lambda will only be invoked once
|
|
|
|
std::apply(f, std::move(*args));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2018-10-25 12:00:29 +00:00
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
virtual bool
|
|
|
|
add_network_interface(
|
|
|
|
std::shared_ptr<vpn::NetworkInterface> netif,
|
|
|
|
std::function<void(net::IPPacket)> packetHandler) = 0;
|
2018-10-25 13:40:07 +00:00
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
virtual bool
|
|
|
|
add_ticker(std::function<void(void)> ticker) = 0;
|
2018-10-29 23:56:05 +00:00
|
|
|
|
2018-11-01 12:47:14 +00:00
|
|
|
virtual void
|
2021-01-11 23:13:22 +00:00
|
|
|
stop() = 0;
|
2018-10-29 23:56:05 +00:00
|
|
|
|
2021-03-02 20:44:30 +00:00
|
|
|
virtual ~EventLoop() = default;
|
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
using UDPReceiveFunc = std::function<void(UDPHandle&, SockAddr src, llarp::OwnedBuffer buf)>;
|
2018-10-29 23:56:05 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
// Constructs a UDP socket that can be used for sending and/or receiving
|
|
|
|
virtual std::shared_ptr<UDPHandle>
|
2021-03-02 20:44:30 +00:00
|
|
|
make_udp(UDPReceiveFunc on_recv) = 0;
|
2018-10-29 23:56:05 +00:00
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
/// set the function that is called once per cycle the flush all the queues
|
2018-12-14 15:43:44 +00:00
|
|
|
virtual void
|
2021-01-11 23:13:22 +00:00
|
|
|
set_pump_function(std::function<void(void)> pumpll) = 0;
|
2018-12-14 15:43:44 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
/// Make a thread-safe event loop waker (an "async" in libuv terminology) on this event loop;
|
|
|
|
/// you can call `->Trigger()` on the returned shared pointer to fire the callback at the next
|
|
|
|
/// available event loop iteration. (Multiple Trigger calls invoked before the call is actually
|
|
|
|
/// made are coalesced into one call).
|
|
|
|
virtual std::shared_ptr<EventLoopWakeup>
|
|
|
|
make_waker(std::function<void()> callback) = 0;
|
2018-10-29 23:56:05 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
// Initializes a new repeated task object. Note that the task is not actually added to the event
|
2021-03-02 07:02:59 +00:00
|
|
|
// loop until you call start() on the returned object. Typically invoked via call_every.
|
2021-03-02 02:06:20 +00:00
|
|
|
virtual std::shared_ptr<EventLoopRepeater>
|
|
|
|
make_repeater() = 0;
|
|
|
|
|
|
|
|
// Constructs and initializes a new default (libuv) event loop
|
|
|
|
static std::shared_ptr<EventLoop>
|
|
|
|
create(size_t queueLength = event_loop_queue_size);
|
2021-02-22 13:26:32 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
// Returns true if called from within the event loop thread, false otherwise.
|
|
|
|
virtual bool
|
2021-03-02 07:02:59 +00:00
|
|
|
inEventLoop() const = 0;
|
2018-10-25 12:00:29 +00:00
|
|
|
};
|
2021-03-02 02:06:20 +00:00
|
|
|
|
|
|
|
using EventLoop_ptr = std::shared_ptr<EventLoop>;
|
|
|
|
|
2019-04-25 23:21:19 +00:00
|
|
|
} // namespace llarp
|