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-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
|
|
|
{
|
|
|
|
bool
|
|
|
|
init() override;
|
|
|
|
|
|
|
|
int
|
|
|
|
run() override
|
|
|
|
{
|
2019-06-12 13:14:38 +00:00
|
|
|
return -1;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
running() const override;
|
|
|
|
|
|
|
|
void
|
|
|
|
update_time() override;
|
|
|
|
|
|
|
|
/// return false on socket error (non blocking)
|
|
|
|
bool
|
|
|
|
tcp_connect(llarp_tcp_connecter* tcp, const sockaddr* addr) override;
|
|
|
|
|
|
|
|
int
|
|
|
|
tick(int ms) override;
|
|
|
|
|
|
|
|
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
|
|
|
|
udp_listen(llarp_udp_io* l, const sockaddr* src) override;
|
|
|
|
|
|
|
|
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
|
|
|
|
tcp_listen(llarp_tcp_acceptor* tcp, const sockaddr* addr) override;
|
|
|
|
|
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*
|
|
|
|
bind_tcp(llarp_tcp_acceptor*, const sockaddr*) override
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:39:09 +00:00
|
|
|
bool
|
|
|
|
add_ticker(std::function< void(void) > ticker) override;
|
|
|
|
|
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
|
2019-10-02 13:06:14 +00:00
|
|
|
set_logic(std::shared_ptr< llarp::Logic > l) override
|
2019-09-16 10:21:12 +00:00
|
|
|
{
|
|
|
|
m_Logic = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// call function in logic thread
|
|
|
|
template < typename F >
|
|
|
|
void
|
|
|
|
Call(F f)
|
|
|
|
{
|
|
|
|
m_Logic->queue_func(f);
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
private:
|
2019-10-16 11:45:52 +00:00
|
|
|
uv_loop_t m_Impl;
|
2019-06-02 21:17:05 +00:00
|
|
|
uv_timer_t m_TickTimer;
|
|
|
|
std::atomic< bool > m_Run;
|
2019-09-16 10:21:12 +00:00
|
|
|
std::shared_ptr< llarp::Logic > m_Logic;
|
2019-06-02 21:17:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace libuv
|
|
|
|
|
2019-06-16 21:44:11 +00:00
|
|
|
#endif
|