2019-06-02 21:17:05 +00:00
|
|
|
#ifndef LLARP_EV_LIBUV_HPP
|
2019-07-13 13:05:00 +00:00
|
|
|
#define LLARP_EV_LIBUV_HPP
|
2019-06-02 21:17:05 +00:00
|
|
|
#include <ev/ev.hpp>
|
2019-08-22 11:18:05 +00:00
|
|
|
#include <ev/pipe.hpp>
|
2019-06-02 21:17:05 +00:00
|
|
|
#include <uv.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
2019-09-16 10:21:12 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2019-12-10 16:05:19 +00:00
|
|
|
#include <util/thread/queue.hpp>
|
2019-12-10 15:21:52 +00:00
|
|
|
#include <util/meta/memfn.hpp>
|
2019-06-02 21:17:05 +00:00
|
|
|
|
2019-11-23 04:47:08 +00:00
|
|
|
#include <map>
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
namespace libuv
|
|
|
|
{
|
2019-06-12 13:14:38 +00:00
|
|
|
struct Loop final : public llarp_ev_loop
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
typedef std::function<void(void)> Callback;
|
2019-11-23 04:47:08 +00:00
|
|
|
|
|
|
|
struct PendingTimer
|
|
|
|
{
|
|
|
|
uint64_t job_id;
|
|
|
|
llarp_time_t delay_ms;
|
|
|
|
Callback callback;
|
|
|
|
};
|
|
|
|
|
2019-12-10 15:21:52 +00:00
|
|
|
Loop();
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
bool
|
|
|
|
init() override;
|
|
|
|
|
|
|
|
int
|
2020-05-15 12:36:38 +00:00
|
|
|
run() override;
|
2019-06-02 21:17:05 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
running() const override;
|
|
|
|
|
|
|
|
void
|
|
|
|
update_time() override;
|
|
|
|
|
|
|
|
/// return false on socket error (non blocking)
|
|
|
|
bool
|
2020-05-06 20:38:44 +00:00
|
|
|
tcp_connect(llarp_tcp_connecter* tcp, const llarp::SockAddr& addr) override;
|
2019-06-02 21:17:05 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
tick(int ms) override;
|
|
|
|
|
2019-11-23 04:47:08 +00:00
|
|
|
uint32_t
|
2020-04-07 18:38:56 +00:00
|
|
|
call_after_delay(llarp_time_t delay_ms, std::function<void(void)> callback) override;
|
2019-11-23 04:47:08 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
cancel_delayed_call(uint32_t job_id) override;
|
|
|
|
|
|
|
|
void
|
|
|
|
process_timer_queue();
|
|
|
|
|
|
|
|
void
|
|
|
|
process_cancel_queue();
|
|
|
|
|
|
|
|
void
|
|
|
|
do_timer_job(uint64_t job_id);
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
void
|
|
|
|
stop() override;
|
|
|
|
|
|
|
|
void
|
2019-06-20 18:28:26 +00:00
|
|
|
stopped() override;
|
|
|
|
|
|
|
|
void
|
|
|
|
CloseAll();
|
2019-06-02 21:17:05 +00:00
|
|
|
|
|
|
|
bool
|
2020-05-06 20:38:44 +00:00
|
|
|
udp_listen(llarp_udp_io* l, const llarp::SockAddr& src) override;
|
2019-06-02 21:17:05 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
udp_close(llarp_udp_io* l) override;
|
|
|
|
|
|
|
|
/// deregister event listener
|
|
|
|
bool
|
|
|
|
close_ev(llarp::ev_io*) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
tun_listen(llarp_tun_io* tun) override;
|
|
|
|
|
|
|
|
llarp::ev_io*
|
|
|
|
create_tun(llarp_tun_io*) override
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-05-06 20:38:44 +00:00
|
|
|
tcp_listen(llarp_tcp_acceptor* tcp, const llarp::SockAddr& addr) override;
|
2019-06-02 21:17:05 +00:00
|
|
|
|
2019-08-22 11:18:05 +00:00
|
|
|
bool
|
|
|
|
add_pipe(llarp_ev_pkt_pipe* p) override;
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
llarp::ev_io*
|
2020-05-06 20:38:44 +00:00
|
|
|
bind_tcp(llarp_tcp_acceptor*, const llarp::SockAddr&) override
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/// register event listener
|
|
|
|
bool
|
|
|
|
add_ev(llarp::ev_io*, bool) override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-16 10:21:12 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
set_logic(std::shared_ptr<llarp::Logic> l) override
|
2019-09-16 10:21:12 +00:00
|
|
|
{
|
|
|
|
m_Logic = l;
|
2019-12-10 15:21:52 +00:00
|
|
|
m_Logic->SetQueuer(llarp::util::memFn(&Loop::call_soon, this));
|
2019-09-16 10:21:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::shared_ptr<llarp::Logic> m_Logic;
|
2019-09-16 10:21:12 +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
|
|
|
|
2020-05-15 12:36:38 +00:00
|
|
|
void
|
|
|
|
FlushLogic();
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
private:
|
2019-10-16 11:45:52 +00:00
|
|
|
uv_loop_t m_Impl;
|
2019-11-23 04:47:08 +00:00
|
|
|
uv_timer_t* m_TickTimer;
|
|
|
|
uv_async_t 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
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::thread::Queue<PendingTimer> m_timerQueue;
|
|
|
|
llarp::thread::Queue<uint32_t> m_timerCancelQueue;
|
2019-06-02 21:17:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace libuv
|
|
|
|
|
2019-06-16 21:44:11 +00:00
|
|
|
#endif
|