lokinet/llarp/ev/ev_libuv.hpp

163 lines
3.1 KiB
C++
Raw Normal View History

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>
#include <util/thread/logic.hpp>
2019-12-10 16:05:19 +00:00
#include <util/thread/queue.hpp>
#include <util/meta/memfn.hpp>
2019-06-02 21:17:05 +00:00
#include <map>
2019-06-02 21:17:05 +00:00
namespace libuv
{
struct Loop final : public llarp_ev_loop
2019-06-02 21:17:05 +00:00
{
typedef std::function<void(void)> Callback;
struct PendingTimer
{
uint64_t job_id;
llarp_time_t delay_ms;
Callback callback;
};
2020-06-01 17:31:12 +00:00
Loop(size_t queue_size);
2019-06-02 21:17:05 +00:00
bool
init() override;
int
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;
uint32_t
call_after_delay(llarp_time_t delay_ms, std::function<void(void)> callback) override;
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
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;
}
void
set_logic(std::shared_ptr<llarp::Logic> l) override
{
m_Logic = l;
m_Logic->SetQueuer(llarp::util::memFn(&Loop::call_soon, this));
}
std::shared_ptr<llarp::Logic> m_Logic;
void
call_soon(std::function<void(void)> f) override;
void
register_poll_fd_readable(int fd, Callback callback) override;
void
deregister_poll_fd_readable(int fd) override;
void
FlushLogic();
2019-06-02 21:17:05 +00:00
private:
uv_loop_t m_Impl;
uv_timer_t* m_TickTimer;
uv_async_t m_WakeUp;
std::atomic<bool> m_Run;
using AtomicQueue_t = llarp::thread::Queue<std::function<void(void)>>;
AtomicQueue_t m_LogicCalls;
#ifdef LOKINET_DEBUG
uint64_t last_time;
uint64_t loop_run_count;
#endif
std::atomic<uint32_t> m_nextID;
std::map<uint32_t, Callback> m_pendingCalls;
std::unordered_map<int, uv_poll_t> m_Polls;
llarp::thread::Queue<PendingTimer> m_timerQueue;
llarp::thread::Queue<uint32_t> m_timerCancelQueue;
std::optional<std::thread::id> m_EventLoopThreadID;
2019-06-02 21:17:05 +00:00
};
} // namespace libuv
2019-06-16 21:44:11 +00:00
#endif