2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
|
|
|
#include "ev.hpp"
|
2021-03-02 02:06:20 +00:00
|
|
|
#include "udp_handle.hpp"
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/thread/queue.hpp>
|
|
|
|
#include <llarp/util/meta/memfn.hpp>
|
2019-06-02 21:17:05 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
#include <uvw/loop.h>
|
|
|
|
#include <uvw/async.h>
|
|
|
|
#include <uvw/poll.h>
|
|
|
|
#include <uvw/udp.h>
|
|
|
|
|
|
|
|
#include <functional>
|
2019-11-23 04:47:08 +00:00
|
|
|
#include <map>
|
2021-03-02 02:06:20 +00:00
|
|
|
#include <vector>
|
2019-11-23 04:47:08 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
namespace llarp::uv
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2021-02-22 13:26:32 +00:00
|
|
|
class UVWakeup;
|
2021-03-02 02:06:20 +00:00
|
|
|
class UVRepeater;
|
2021-02-22 13:26:32 +00:00
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
class Loop : public llarp::EventLoop
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2021-03-02 07:02:59 +00:00
|
|
|
public:
|
2021-03-02 02:06:20 +00:00
|
|
|
using Callback = std::function<void()>;
|
2019-11-23 04:47:08 +00:00
|
|
|
|
2020-06-01 17:31:12 +00:00
|
|
|
Loop(size_t queue_size);
|
2019-12-10 15:21:52 +00:00
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
virtual void
|
2021-03-02 07:02:59 +00:00
|
|
|
run() override;
|
2019-06-02 21:17:05 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
running() const override;
|
|
|
|
|
2021-11-09 19:37:42 +00:00
|
|
|
llarp_time_t
|
|
|
|
time_now() const override
|
|
|
|
{
|
|
|
|
return m_Impl->now();
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
void
|
2021-03-02 07:02:59 +00:00
|
|
|
call_later(llarp_time_t delay_ms, std::function<void(void)> callback) override;
|
2019-11-23 04:47:08 +00:00
|
|
|
|
|
|
|
void
|
2021-03-02 02:06:20 +00:00
|
|
|
tick_event_loop();
|
2019-11-23 04:47:08 +00:00
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
void
|
|
|
|
stop() override;
|
|
|
|
|
2019-09-05 17:39:09 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
add_ticker(std::function<void(void)> ticker) override;
|
2019-09-05 17:39:09 +00:00
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
bool
|
2021-01-11 23:13:22 +00:00
|
|
|
add_network_interface(
|
|
|
|
std::shared_ptr<llarp::vpn::NetworkInterface> netif,
|
|
|
|
std::function<void(llarp::net::IPPacket)> handler) override;
|
2019-06-02 21:17:05 +00:00
|
|
|
|
2019-12-10 15:21:52 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
call_soon(std::function<void(void)> f) override;
|
2019-12-10 15:21:52 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
std::shared_ptr<llarp::EventLoopWakeup>
|
|
|
|
make_waker(std::function<void()> callback) override;
|
2021-02-22 13:26:32 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
std::shared_ptr<EventLoopRepeater>
|
|
|
|
make_repeater() override;
|
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
virtual std::shared_ptr<llarp::UDPHandle>
|
2021-03-02 20:44:30 +00:00
|
|
|
make_udp(UDPReceiveFunc on_recv) override;
|
2021-02-22 13:26:32 +00:00
|
|
|
|
2020-05-15 12:36:38 +00:00
|
|
|
void
|
|
|
|
FlushLogic();
|
|
|
|
|
2021-03-23 15:45:42 +00:00
|
|
|
std::shared_ptr<uvw::Loop>
|
|
|
|
MaybeGetUVWLoop() override;
|
2021-03-12 13:50:21 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
bool
|
2021-03-02 07:02:59 +00:00
|
|
|
inEventLoop() const override;
|
2021-03-02 02:06:20 +00:00
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
protected:
|
2021-03-02 02:06:20 +00:00
|
|
|
std::shared_ptr<uvw::Loop> m_Impl;
|
2022-07-09 15:05:52 +00:00
|
|
|
std::optional<std::thread::id> m_EventLoopThreadID;
|
|
|
|
|
|
|
|
private:
|
2021-03-02 02:06:20 +00:00
|
|
|
std::shared_ptr<uvw::AsyncHandle> m_WakeUp;
|
2020-04-07 18:38:56 +00:00
|
|
|
std::atomic<bool> m_Run;
|
|
|
|
using AtomicQueue_t = llarp::thread::Queue<std::function<void(void)>>;
|
2019-12-10 15:21:52 +00:00
|
|
|
AtomicQueue_t m_LogicCalls;
|
2019-11-28 02:35:59 +00:00
|
|
|
|
|
|
|
#ifdef LOKINET_DEBUG
|
|
|
|
uint64_t last_time;
|
|
|
|
uint64_t loop_run_count;
|
|
|
|
#endif
|
2020-04-07 18:38:56 +00:00
|
|
|
std::atomic<uint32_t> m_nextID;
|
2019-11-23 04:47:08 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::map<uint32_t, Callback> m_pendingCalls;
|
2019-11-23 04:47:08 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
std::unordered_map<int, std::shared_ptr<uvw::PollHandle>> m_Polls;
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2021-11-12 13:25:14 +00:00
|
|
|
void
|
|
|
|
wakeup() override;
|
2019-06-02 21:17:05 +00:00
|
|
|
};
|
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
} // namespace llarp::uv
|