2019-08-02 09:27:27 +00:00
|
|
|
#include <ev/ev_libuv.hpp>
|
|
|
|
#include <net/net_addr.hpp>
|
2019-09-16 10:21:12 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2019-10-28 14:15:36 +00:00
|
|
|
#include <util/thread/queue.hpp>
|
2019-08-02 09:27:27 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <cstring>
|
2019-06-02 21:17:05 +00:00
|
|
|
|
|
|
|
namespace libuv
|
|
|
|
{
|
2019-11-14 21:56:01 +00:00
|
|
|
#define LoopCall(h, ...) \
|
|
|
|
LogicCall(static_cast< Loop* >((h)->loop->data)->m_Logic, __VA_ARGS__)
|
2019-09-16 10:21:12 +00:00
|
|
|
|
2019-06-20 18:28:26 +00:00
|
|
|
struct glue
|
|
|
|
{
|
2019-06-21 13:00:17 +00:00
|
|
|
virtual ~glue() = default;
|
2019-06-20 18:28:26 +00:00
|
|
|
virtual void
|
|
|
|
Close() = 0;
|
|
|
|
};
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
/// tcp connection glue between llarp and libuv
|
2019-06-20 18:28:26 +00:00
|
|
|
struct conn_glue : public glue
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-10-28 14:15:36 +00:00
|
|
|
using WriteBuffer_t = std::vector< char >;
|
|
|
|
|
|
|
|
struct WriteEvent
|
|
|
|
{
|
|
|
|
WriteBuffer_t data;
|
|
|
|
uv_write_t request;
|
|
|
|
|
|
|
|
WriteEvent() = default;
|
|
|
|
|
|
|
|
explicit WriteEvent(WriteBuffer_t buf) : data(std::move(buf))
|
|
|
|
{
|
|
|
|
request.data = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_buf_t
|
|
|
|
Buffer()
|
|
|
|
{
|
|
|
|
return uv_buf_init(data.data(), data.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_write_t*
|
|
|
|
Request()
|
|
|
|
{
|
|
|
|
return &request;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
uv_tcp_t m_Handle;
|
|
|
|
uv_connect_t m_Connect;
|
2019-06-20 18:28:26 +00:00
|
|
|
uv_check_t m_Ticker;
|
2019-06-02 21:17:05 +00:00
|
|
|
llarp_tcp_connecter* const m_TCP;
|
|
|
|
llarp_tcp_acceptor* const m_Accept;
|
|
|
|
llarp_tcp_conn m_Conn;
|
|
|
|
llarp::Addr m_Addr;
|
2019-10-28 14:15:36 +00:00
|
|
|
llarp::thread::Queue< WriteBuffer_t > m_WriteQueue;
|
|
|
|
uv_async_t m_WriteNotify;
|
2019-06-03 14:03:59 +00:00
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
conn_glue(uv_loop_t* loop, llarp_tcp_connecter* tcp, const sockaddr* addr)
|
2019-10-28 14:15:36 +00:00
|
|
|
: m_TCP(tcp), m_Accept(nullptr), m_Addr(*addr), m_WriteQueue(32)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
m_Connect.data = this;
|
2019-06-03 14:03:59 +00:00
|
|
|
m_Handle.data = this;
|
2019-06-20 18:28:26 +00:00
|
|
|
m_TCP->impl = this;
|
2019-06-02 21:17:05 +00:00
|
|
|
uv_tcp_init(loop, &m_Handle);
|
2019-06-03 12:02:54 +00:00
|
|
|
m_Ticker.data = this;
|
2019-06-20 18:28:26 +00:00
|
|
|
uv_check_init(loop, &m_Ticker);
|
2019-10-28 14:15:36 +00:00
|
|
|
m_Conn.close = &ExplicitClose;
|
|
|
|
m_Conn.write = &ExplicitWrite;
|
|
|
|
m_WriteNotify.data = this;
|
|
|
|
uv_async_init(loop, &m_WriteNotify, &OnShouldWrite);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conn_glue(uv_loop_t* loop, llarp_tcp_acceptor* tcp, const sockaddr* addr)
|
2019-10-28 14:15:36 +00:00
|
|
|
: m_TCP(nullptr), m_Accept(tcp), m_Addr(*addr), m_WriteQueue(32)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-06-20 18:28:26 +00:00
|
|
|
m_Connect.data = nullptr;
|
|
|
|
m_Handle.data = this;
|
2019-06-02 21:17:05 +00:00
|
|
|
uv_tcp_init(loop, &m_Handle);
|
2019-06-03 12:02:54 +00:00
|
|
|
m_Ticker.data = this;
|
2019-06-20 18:28:26 +00:00
|
|
|
uv_check_init(loop, &m_Ticker);
|
2019-10-28 14:15:36 +00:00
|
|
|
m_Accept->close = &ExplicitCloseAccept;
|
|
|
|
m_Conn.write = nullptr;
|
|
|
|
m_Conn.closed = nullptr;
|
|
|
|
m_WriteNotify.data = this;
|
|
|
|
uv_async_init(loop, &m_WriteNotify, &OnShouldWrite);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 14:15:36 +00:00
|
|
|
conn_glue(conn_glue* parent)
|
|
|
|
: m_TCP(nullptr), m_Accept(nullptr), m_WriteQueue(32)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-06-20 18:28:26 +00:00
|
|
|
m_Connect.data = nullptr;
|
|
|
|
m_Conn.close = &ExplicitClose;
|
|
|
|
m_Conn.write = &ExplicitWrite;
|
|
|
|
m_Handle.data = this;
|
2019-06-02 21:17:05 +00:00
|
|
|
uv_tcp_init(parent->m_Handle.loop, &m_Handle);
|
2019-06-03 12:02:54 +00:00
|
|
|
m_Ticker.data = this;
|
2019-06-20 18:28:26 +00:00
|
|
|
uv_check_init(parent->m_Handle.loop, &m_Ticker);
|
2019-10-28 14:15:36 +00:00
|
|
|
m_WriteNotify.data = this;
|
|
|
|
uv_async_init(parent->m_Handle.loop, &m_WriteNotify, &OnShouldWrite);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnOutboundConnect(uv_connect_t* c, int status)
|
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
conn_glue* self = static_cast< conn_glue* >(c->data);
|
2019-11-14 21:56:01 +00:00
|
|
|
LoopCall(self->Stream(),
|
|
|
|
std::bind(&conn_glue::HandleConnectResult, self, status));
|
2019-06-20 18:28:26 +00:00
|
|
|
c->data = nullptr;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConnectAsync()
|
|
|
|
{
|
|
|
|
return uv_tcp_connect(&m_Connect, &m_Handle, m_Addr, &OnOutboundConnect)
|
|
|
|
!= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ExplicitClose(llarp_tcp_conn* conn)
|
|
|
|
{
|
|
|
|
static_cast< conn_glue* >(conn->impl)->Close();
|
|
|
|
}
|
2019-06-03 14:03:59 +00:00
|
|
|
static void
|
|
|
|
ExplicitCloseAccept(llarp_tcp_acceptor* tcp)
|
|
|
|
{
|
|
|
|
static_cast< conn_glue* >(tcp->impl)->Close();
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
|
2019-06-03 12:02:54 +00:00
|
|
|
static ssize_t
|
|
|
|
ExplicitWrite(llarp_tcp_conn* conn, const byte_t* ptr, size_t sz)
|
|
|
|
{
|
2019-06-03 14:03:59 +00:00
|
|
|
return static_cast< conn_glue* >(conn->impl)->WriteAsync((char*)ptr, sz);
|
2019-06-03 12:02:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
static void
|
|
|
|
OnRead(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)
|
|
|
|
{
|
2019-06-03 14:03:59 +00:00
|
|
|
if(nread >= 0)
|
2019-09-05 21:28:50 +00:00
|
|
|
{
|
|
|
|
auto* conn = static_cast< conn_glue* >(stream->data);
|
2019-11-14 21:56:01 +00:00
|
|
|
LoopCall(stream, std::bind(&conn_glue::Read, conn, buf->base, nread));
|
2019-10-11 11:47:21 +00:00
|
|
|
return;
|
2019-09-05 21:28:50 +00:00
|
|
|
}
|
2019-06-03 14:03:59 +00:00
|
|
|
else if(nread < 0)
|
2019-09-05 21:28:50 +00:00
|
|
|
{
|
2019-06-03 14:03:59 +00:00
|
|
|
static_cast< conn_glue* >(stream->data)->Close();
|
2019-09-05 21:28:50 +00:00
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
delete[] buf->base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Alloc(uv_handle_t*, size_t suggested_size, uv_buf_t* buf)
|
|
|
|
{
|
|
|
|
buf->base = new char[suggested_size];
|
|
|
|
buf->len = suggested_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Read(const char* ptr, ssize_t sz)
|
|
|
|
{
|
2019-06-03 14:03:59 +00:00
|
|
|
if(m_Conn.read)
|
|
|
|
{
|
|
|
|
llarp::LogDebug("tcp read ", sz, " bytes");
|
|
|
|
const llarp_buffer_t buf(ptr, sz);
|
|
|
|
m_Conn.read(&m_Conn, buf);
|
|
|
|
}
|
2019-10-11 11:47:21 +00:00
|
|
|
delete[] ptr;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HandleConnectResult(int status)
|
|
|
|
{
|
|
|
|
if(m_TCP && m_TCP->connected)
|
|
|
|
{
|
|
|
|
if(status == 0)
|
|
|
|
{
|
|
|
|
m_Conn.impl = this;
|
|
|
|
m_Conn.loop = m_TCP->loop;
|
|
|
|
m_Conn.close = &ExplicitClose;
|
2019-06-03 12:02:54 +00:00
|
|
|
m_Conn.write = &ExplicitWrite;
|
2019-06-02 21:17:05 +00:00
|
|
|
m_TCP->connected(m_TCP, &m_Conn);
|
2019-06-03 12:02:54 +00:00
|
|
|
Start();
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
else if(m_TCP->error)
|
2019-06-03 14:03:59 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to connect tcp ", uv_strerror(status));
|
2019-06-02 21:17:05 +00:00
|
|
|
m_TCP->error(m_TCP);
|
2019-06-03 14:03:59 +00:00
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WriteFail()
|
|
|
|
{
|
2019-06-03 14:03:59 +00:00
|
|
|
if(m_Conn.close)
|
|
|
|
m_Conn.close(&m_Conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_stream_t*
|
|
|
|
Stream()
|
|
|
|
{
|
|
|
|
return (uv_stream_t*)&m_Handle;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnWritten(uv_write_t* req, int status)
|
|
|
|
{
|
2019-10-28 14:15:36 +00:00
|
|
|
WriteEvent* ev = static_cast< WriteEvent* >(req->data);
|
|
|
|
if(status == 0)
|
2019-06-03 14:03:59 +00:00
|
|
|
{
|
2019-10-28 14:15:36 +00:00
|
|
|
llarp::LogDebug("wrote ", ev->data.size());
|
2019-06-03 14:03:59 +00:00
|
|
|
}
|
2019-06-20 18:28:26 +00:00
|
|
|
else
|
2019-10-28 14:15:36 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("write fail");
|
|
|
|
}
|
|
|
|
delete ev;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 14:15:36 +00:00
|
|
|
int
|
|
|
|
WriteAsync(char* data, size_t sz)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-10-28 14:15:36 +00:00
|
|
|
if(uv_is_closing((const uv_handle_t*)&m_Handle))
|
|
|
|
return -1;
|
|
|
|
if(uv_is_closing((const uv_handle_t*)&m_WriteNotify))
|
|
|
|
return -1;
|
|
|
|
WriteBuffer_t buf(sz);
|
|
|
|
std::copy_n(data, sz, buf.begin());
|
|
|
|
if(m_WriteQueue.pushBack(std::move(buf))
|
|
|
|
== llarp::thread::QueueReturn::Success)
|
|
|
|
{
|
|
|
|
uv_async_send(&m_WriteNotify);
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
return -1;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 14:15:36 +00:00
|
|
|
void
|
|
|
|
FlushWrite()
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-10-28 14:15:36 +00:00
|
|
|
while(not m_WriteQueue.empty())
|
|
|
|
{
|
|
|
|
auto data = m_WriteQueue.popFront();
|
|
|
|
WriteEvent* ev = new WriteEvent(std::move(data));
|
|
|
|
auto buf = ev->Buffer();
|
|
|
|
if(uv_write(ev->Request(), Stream(), &buf, 1, &OnWritten) != 0)
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnClosed(uv_handle_t* h)
|
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
conn_glue* conn = static_cast< conn_glue* >(h->data);
|
2019-11-14 21:56:01 +00:00
|
|
|
LoopCall(h, std::bind(&conn_glue::HandleClosed, conn));
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 18:28:26 +00:00
|
|
|
static void
|
|
|
|
FullClose(uv_handle_t* h)
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
auto* self = static_cast< conn_glue* >(h->data);
|
|
|
|
h->data = nullptr;
|
2019-06-20 18:28:26 +00:00
|
|
|
delete self;
|
2019-08-12 12:40:38 +00:00
|
|
|
llarp::LogDebug("deleted");
|
2019-06-20 18:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
void
|
|
|
|
HandleClosed()
|
|
|
|
{
|
|
|
|
m_Handle.data = nullptr;
|
2019-06-20 18:28:26 +00:00
|
|
|
if(m_Accept)
|
2019-06-03 14:03:59 +00:00
|
|
|
{
|
2019-06-20 18:28:26 +00:00
|
|
|
if(m_Accept->closed)
|
|
|
|
m_Accept->closed(m_Accept);
|
2019-06-03 14:03:59 +00:00
|
|
|
m_Accept->impl = nullptr;
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
if(m_Conn.closed)
|
2019-06-03 14:03:59 +00:00
|
|
|
{
|
2019-06-02 21:17:05 +00:00
|
|
|
m_Conn.closed(&m_Conn);
|
2019-06-03 14:03:59 +00:00
|
|
|
}
|
2019-06-20 18:28:26 +00:00
|
|
|
m_Conn.impl = nullptr;
|
2019-08-12 12:40:38 +00:00
|
|
|
llarp::LogDebug("closed");
|
2019-06-20 18:28:26 +00:00
|
|
|
uv_close((uv_handle_t*)&m_Ticker, &FullClose);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnShutdown(uv_shutdown_t* shut, int code)
|
|
|
|
{
|
|
|
|
llarp::LogDebug("shut down ", code);
|
2019-07-30 23:42:13 +00:00
|
|
|
auto* self = static_cast< conn_glue* >(shut->data);
|
2019-06-20 18:28:26 +00:00
|
|
|
uv_close((uv_handle_t*)&self->m_Handle, &OnClosed);
|
|
|
|
delete shut;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 14:15:36 +00:00
|
|
|
static void
|
|
|
|
OnWriteClosed(uv_handle_t* h)
|
|
|
|
{
|
|
|
|
conn_glue* conn = static_cast< conn_glue* >(h->data);
|
|
|
|
auto* shut = new uv_shutdown_t();
|
|
|
|
shut->data = conn;
|
|
|
|
uv_shutdown(shut, conn->Stream(), &OnShutdown);
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
void
|
2019-06-20 18:28:26 +00:00
|
|
|
Close() override
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-10-28 14:15:36 +00:00
|
|
|
if(uv_is_closing((uv_handle_t*)&m_WriteNotify))
|
|
|
|
return;
|
2019-06-03 14:03:59 +00:00
|
|
|
llarp::LogDebug("close tcp connection");
|
2019-10-28 14:15:36 +00:00
|
|
|
m_WriteQueue.disable();
|
|
|
|
uv_close((uv_handle_t*)&m_WriteNotify, &OnWriteClosed);
|
2019-06-20 18:28:26 +00:00
|
|
|
uv_check_stop(&m_Ticker);
|
|
|
|
uv_read_stop(Stream());
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnAccept(uv_stream_t* stream, int status)
|
|
|
|
{
|
|
|
|
if(status == 0)
|
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
conn_glue* conn = static_cast< conn_glue* >(stream->data);
|
2019-11-14 21:56:01 +00:00
|
|
|
LoopCall(stream, std::bind(&conn_glue::Accept, conn));
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
2019-06-03 14:03:59 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogError("tcp accept failed: ", uv_strerror(status));
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 14:15:36 +00:00
|
|
|
static void
|
|
|
|
OnShouldWrite(uv_async_t* h)
|
|
|
|
{
|
|
|
|
static_cast< conn_glue* >(h->data)->FlushWrite();
|
|
|
|
}
|
|
|
|
|
2019-06-03 12:02:54 +00:00
|
|
|
static void
|
2019-06-20 18:28:26 +00:00
|
|
|
OnTick(uv_check_t* t)
|
2019-06-03 12:02:54 +00:00
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
conn_glue* conn = static_cast< conn_glue* >(t->data);
|
2019-11-14 21:56:01 +00:00
|
|
|
LoopCall(t, std::bind(&conn_glue::Tick, conn));
|
2019-06-03 12:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Tick()
|
|
|
|
{
|
|
|
|
if(m_Accept && m_Accept->tick)
|
|
|
|
m_Accept->tick(m_Accept);
|
|
|
|
if(m_Conn.tick)
|
|
|
|
m_Conn.tick(&m_Conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Start()
|
|
|
|
{
|
2019-06-20 18:28:26 +00:00
|
|
|
auto result = uv_check_start(&m_Ticker, &OnTick);
|
2019-06-03 14:03:59 +00:00
|
|
|
if(result)
|
|
|
|
llarp::LogError("failed to start timer ", uv_strerror(result));
|
|
|
|
result = uv_read_start(Stream(), &Alloc, &OnRead);
|
|
|
|
if(result)
|
|
|
|
llarp::LogError("failed to start reader ", uv_strerror(result));
|
2019-06-03 12:02:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
void
|
|
|
|
Accept()
|
|
|
|
{
|
|
|
|
if(m_Accept && m_Accept->accepted)
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
auto* child = new conn_glue(this);
|
2019-06-03 14:03:59 +00:00
|
|
|
llarp::LogDebug("accepted new connection");
|
|
|
|
child->m_Conn.impl = child;
|
2019-06-03 12:02:54 +00:00
|
|
|
child->m_Conn.loop = m_Accept->loop;
|
|
|
|
child->m_Conn.close = &ExplicitClose;
|
|
|
|
child->m_Conn.write = &ExplicitWrite;
|
2019-06-03 14:03:59 +00:00
|
|
|
auto res = uv_accept(Stream(), child->Stream());
|
|
|
|
if(res)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to accept tcp connection ", uv_strerror(res));
|
|
|
|
child->Close();
|
|
|
|
return;
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
m_Accept->accepted(m_Accept, &child->m_Conn);
|
2019-06-03 12:02:54 +00:00
|
|
|
child->Start();
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Server()
|
|
|
|
{
|
2019-06-03 14:03:59 +00:00
|
|
|
m_Accept->close = &ExplicitCloseAccept;
|
2019-06-03 11:14:31 +00:00
|
|
|
return uv_tcp_bind(&m_Handle, m_Addr, 0) == 0
|
|
|
|
&& uv_listen(Stream(), 5, &OnAccept) == 0;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-05 17:39:09 +00:00
|
|
|
struct ticker_glue : public glue
|
|
|
|
{
|
|
|
|
std::function< void(void) > func;
|
|
|
|
|
|
|
|
ticker_glue(uv_loop_t* loop, std::function< void(void) > tick) : func(tick)
|
|
|
|
{
|
|
|
|
m_Ticker.data = this;
|
|
|
|
uv_check_init(loop, &m_Ticker);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnTick(uv_check_t* t)
|
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
ticker_glue* ticker = static_cast< ticker_glue* >(t->data);
|
2019-11-14 21:56:01 +00:00
|
|
|
LoopCall(t, ticker->func);
|
2019-09-05 17:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Start()
|
|
|
|
{
|
|
|
|
return uv_check_start(&m_Ticker, &OnTick) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Close() override
|
|
|
|
{
|
|
|
|
uv_check_stop(&m_Ticker);
|
2019-11-20 19:45:23 +00:00
|
|
|
uv_close((uv_handle_t*)&m_Ticker, [](auto h) {
|
|
|
|
ticker_glue* self = (ticker_glue*)h->data;
|
|
|
|
h->data = nullptr;
|
|
|
|
delete self;
|
|
|
|
});
|
2019-09-05 17:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uv_check_t m_Ticker;
|
|
|
|
};
|
|
|
|
|
2019-06-20 18:28:26 +00:00
|
|
|
struct udp_glue : public glue
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
uv_udp_t m_Handle;
|
2019-06-04 14:36:54 +00:00
|
|
|
uv_check_t m_Ticker;
|
2019-06-02 21:17:05 +00:00
|
|
|
llarp_udp_io* const m_UDP;
|
|
|
|
llarp::Addr m_Addr;
|
2019-10-02 13:06:14 +00:00
|
|
|
llarp_pkt_list m_LastPackets;
|
|
|
|
std::array< char, 1500 > m_Buffer;
|
2019-06-02 21:17:05 +00:00
|
|
|
|
|
|
|
udp_glue(uv_loop_t* loop, llarp_udp_io* udp, const sockaddr* src)
|
|
|
|
: m_UDP(udp), m_Addr(*src)
|
|
|
|
{
|
|
|
|
m_Handle.data = this;
|
|
|
|
m_Ticker.data = this;
|
|
|
|
uv_udp_init(loop, &m_Handle);
|
2019-06-04 14:36:54 +00:00
|
|
|
uv_check_init(loop, &m_Ticker);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Alloc(uv_handle_t*, size_t suggested_size, uv_buf_t* buf)
|
|
|
|
{
|
2019-10-02 13:06:14 +00:00
|
|
|
const size_t sz = std::min(suggested_size, size_t{1500});
|
|
|
|
buf->base = new char[sz];
|
|
|
|
buf->len = sz;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnRecv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf,
|
|
|
|
const struct sockaddr* addr, unsigned)
|
|
|
|
{
|
2019-10-02 13:06:14 +00:00
|
|
|
udp_glue* glue = static_cast< udp_glue* >(handle->data);
|
2019-06-02 21:17:05 +00:00
|
|
|
if(addr)
|
2019-10-02 13:06:14 +00:00
|
|
|
glue->RecvFrom(nread, buf, addr);
|
2019-11-03 19:53:41 +00:00
|
|
|
if(nread <= 0 || glue->m_UDP == nullptr
|
2019-11-03 19:38:34 +00:00
|
|
|
|| glue->m_UDP->recvfrom != nullptr)
|
2019-10-02 13:06:14 +00:00
|
|
|
delete[] buf->base;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RecvMany(llarp_pkt_list* pkts)
|
|
|
|
{
|
|
|
|
*pkts = std::move(m_LastPackets);
|
|
|
|
m_LastPackets = llarp_pkt_list();
|
|
|
|
return pkts->size() > 0;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RecvFrom(ssize_t sz, const uv_buf_t* buf, const struct sockaddr* fromaddr)
|
|
|
|
{
|
2019-10-02 13:06:14 +00:00
|
|
|
if(sz > 0 && m_UDP)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
const size_t pktsz = sz;
|
2019-10-02 13:06:14 +00:00
|
|
|
if(m_UDP->recvfrom)
|
2019-11-03 19:38:34 +00:00
|
|
|
{
|
|
|
|
const llarp_buffer_t pkt((const byte_t*)buf->base, pktsz);
|
2019-10-02 13:06:14 +00:00
|
|
|
m_UDP->recvfrom(m_UDP, fromaddr, ManagedBuffer{pkt});
|
2019-11-03 19:38:34 +00:00
|
|
|
}
|
2019-10-02 13:06:14 +00:00
|
|
|
else
|
|
|
|
{
|
2019-11-03 19:38:34 +00:00
|
|
|
PacketBuffer pbuf(buf->base, pktsz);
|
|
|
|
m_LastPackets.emplace_back(PacketEvent{*fromaddr, std::move(pbuf)});
|
2019-10-02 13:06:14 +00:00
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-06-04 14:36:54 +00:00
|
|
|
OnTick(uv_check_t* t)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
udp_glue* udp = static_cast< udp_glue* >(t->data);
|
|
|
|
udp->Tick();
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Tick()
|
|
|
|
{
|
2019-06-03 12:02:54 +00:00
|
|
|
if(m_UDP && m_UDP->tick)
|
|
|
|
m_UDP->tick(m_UDP);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
SendTo(llarp_udp_io* udp, const sockaddr* to, const byte_t* ptr, size_t sz)
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
auto* self = static_cast< udp_glue* >(udp->impl);
|
2019-06-20 18:28:26 +00:00
|
|
|
if(self == nullptr)
|
|
|
|
return -1;
|
|
|
|
uv_buf_t buf = uv_buf_init((char*)ptr, sz);
|
2019-06-02 21:17:05 +00:00
|
|
|
return uv_udp_try_send(&self->m_Handle, &buf, 1, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Bind()
|
|
|
|
{
|
|
|
|
auto ret = uv_udp_bind(&m_Handle, m_Addr, 0);
|
|
|
|
if(ret)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to bind to ", m_Addr, " ", uv_strerror(ret));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(uv_udp_recv_start(&m_Handle, &Alloc, &OnRecv))
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to start recving packets via ", m_Addr);
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-04 14:36:54 +00:00
|
|
|
if(uv_check_start(&m_Ticker, &OnTick))
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to start ticker");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(uv_fileno((const uv_handle_t*)&m_Handle, &m_UDP->fd))
|
|
|
|
return false;
|
|
|
|
m_UDP->sendto = &SendTo;
|
2019-09-16 10:21:12 +00:00
|
|
|
m_UDP->impl = this;
|
2019-06-02 21:17:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnClosed(uv_handle_t* h)
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
auto* glue = static_cast< udp_glue* >(h->data);
|
2019-06-20 18:28:26 +00:00
|
|
|
if(glue)
|
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
h->data = nullptr;
|
2019-06-20 18:28:26 +00:00
|
|
|
delete glue;
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-20 18:28:26 +00:00
|
|
|
Close() override
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
m_UDP->impl = nullptr;
|
2019-06-04 14:36:54 +00:00
|
|
|
uv_check_stop(&m_Ticker);
|
2019-06-02 21:17:05 +00:00
|
|
|
uv_close((uv_handle_t*)&m_Handle, &OnClosed);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-22 11:18:05 +00:00
|
|
|
struct pipe_glue : public glue
|
|
|
|
{
|
|
|
|
byte_t m_Buffer[1024 * 8];
|
|
|
|
llarp_ev_pkt_pipe* const m_Pipe;
|
|
|
|
pipe_glue(uv_loop_t* loop, llarp_ev_pkt_pipe* pipe) : m_Pipe(pipe)
|
|
|
|
{
|
|
|
|
m_Handle.data = this;
|
|
|
|
m_Ticker.data = this;
|
|
|
|
uv_poll_init(loop, &m_Handle, m_Pipe->fd);
|
|
|
|
uv_check_init(loop, &m_Ticker);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Tick()
|
|
|
|
{
|
2019-11-14 21:56:01 +00:00
|
|
|
LoopCall(&m_Handle, std::bind(&llarp_ev_pkt_pipe::tick, m_Pipe));
|
2019-08-22 11:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnRead(uv_poll_t* handle, int status, int)
|
|
|
|
{
|
|
|
|
if(status)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pipe_glue* glue = static_cast< pipe_glue* >(handle->data);
|
|
|
|
int r = glue->m_Pipe->read(glue->m_Buffer, sizeof(glue->m_Buffer));
|
|
|
|
if(r <= 0)
|
|
|
|
return;
|
2019-08-23 12:55:28 +00:00
|
|
|
const llarp_buffer_t buf{glue->m_Buffer, static_cast< size_t >(r)};
|
2019-08-22 11:18:05 +00:00
|
|
|
glue->m_Pipe->OnRead(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnClosed(uv_handle_t* h)
|
|
|
|
{
|
|
|
|
auto* self = static_cast< pipe_glue* >(h->data);
|
|
|
|
if(self)
|
|
|
|
{
|
|
|
|
h->data = nullptr;
|
|
|
|
delete self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Close() override
|
|
|
|
{
|
|
|
|
uv_check_stop(&m_Ticker);
|
|
|
|
uv_close((uv_handle_t*)&m_Handle, &OnClosed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnTick(uv_check_t* h)
|
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
pipe_glue* pipe = static_cast< pipe_glue* >(h->data);
|
2019-11-14 21:56:01 +00:00
|
|
|
LoopCall(h, std::bind(&pipe_glue::Tick, pipe));
|
2019-08-22 11:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Start()
|
|
|
|
{
|
|
|
|
if(uv_poll_start(&m_Handle, UV_READABLE, &OnRead))
|
|
|
|
return false;
|
|
|
|
if(uv_check_start(&m_Ticker, &OnTick))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_poll_t m_Handle;
|
|
|
|
uv_check_t m_Ticker;
|
|
|
|
};
|
|
|
|
|
2019-06-20 18:28:26 +00:00
|
|
|
struct tun_glue : public glue
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
uv_poll_t m_Handle;
|
2019-06-04 14:36:54 +00:00
|
|
|
uv_check_t m_Ticker;
|
2019-06-02 21:17:05 +00:00
|
|
|
llarp_tun_io* const m_Tun;
|
|
|
|
device* const m_Device;
|
2019-06-02 21:37:29 +00:00
|
|
|
byte_t m_Buffer[1500];
|
|
|
|
bool readpkt;
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
tun_glue(llarp_tun_io* tun) : m_Tun(tun), m_Device(tuntap_init())
|
|
|
|
{
|
|
|
|
m_Handle.data = this;
|
|
|
|
m_Ticker.data = this;
|
2019-06-02 21:37:29 +00:00
|
|
|
readpkt = false;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
~tun_glue() override
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
tuntap_destroy(m_Device);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-06-04 14:36:54 +00:00
|
|
|
OnTick(uv_check_t* timer)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
tun_glue* tun = static_cast< tun_glue* >(timer->data);
|
2019-11-20 19:45:23 +00:00
|
|
|
tun->Tick();
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 21:37:29 +00:00
|
|
|
static void
|
2019-06-03 12:02:54 +00:00
|
|
|
OnPoll(uv_poll_t* h, int, int events)
|
2019-06-02 21:37:29 +00:00
|
|
|
{
|
2019-06-03 12:02:54 +00:00
|
|
|
if(events & UV_READABLE)
|
2019-06-02 21:37:29 +00:00
|
|
|
{
|
|
|
|
static_cast< tun_glue* >(h->data)->Read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Read()
|
|
|
|
{
|
|
|
|
auto sz = tuntap_read(m_Device, m_Buffer, sizeof(m_Buffer));
|
2019-06-03 12:02:54 +00:00
|
|
|
if(sz > 0)
|
|
|
|
{
|
|
|
|
llarp::LogDebug("tun read ", sz);
|
2019-11-20 19:45:23 +00:00
|
|
|
const llarp_buffer_t pkt(m_Buffer, sz);
|
2019-06-03 12:02:54 +00:00
|
|
|
if(m_Tun && m_Tun->recvpkt)
|
|
|
|
m_Tun->recvpkt(m_Tun, pkt);
|
|
|
|
}
|
2019-06-02 21:37:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
void
|
|
|
|
Tick()
|
|
|
|
{
|
2019-06-03 12:02:54 +00:00
|
|
|
if(m_Tun->before_write)
|
|
|
|
m_Tun->before_write(m_Tun);
|
2019-06-04 14:36:54 +00:00
|
|
|
if(m_Tun->tick)
|
|
|
|
m_Tun->tick(m_Tun);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnClosed(uv_handle_t* h)
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
auto* self = static_cast< tun_glue* >(h->data);
|
2019-06-20 18:28:26 +00:00
|
|
|
if(self)
|
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
h->data = nullptr;
|
2019-06-20 18:28:26 +00:00
|
|
|
delete self;
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-20 18:28:26 +00:00
|
|
|
Close() override
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
2019-11-20 19:45:23 +00:00
|
|
|
if(m_Tun->impl == nullptr)
|
|
|
|
return;
|
2019-09-16 10:21:12 +00:00
|
|
|
m_Tun->impl = nullptr;
|
2019-06-20 18:28:26 +00:00
|
|
|
uv_check_stop(&m_Ticker);
|
2019-11-20 19:45:23 +00:00
|
|
|
uv_close((uv_handle_t*)&m_Ticker, [](uv_handle_t* h) {
|
|
|
|
tun_glue* glue = static_cast< tun_glue* >(h->data);
|
|
|
|
uv_close((uv_handle_t*)&glue->m_Handle, &OnClosed);
|
|
|
|
});
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:02:54 +00:00
|
|
|
bool
|
|
|
|
Write(const byte_t* pkt, size_t sz)
|
|
|
|
{
|
|
|
|
return tuntap_write(m_Device, (void*)pkt, sz) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
WritePkt(llarp_tun_io* tun, const byte_t* pkt, size_t sz)
|
|
|
|
{
|
2019-09-16 10:21:12 +00:00
|
|
|
tun_glue* glue = static_cast< tun_glue* >(tun->impl);
|
|
|
|
return glue && glue->Write(pkt, sz);
|
2019-06-03 12:02:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
bool
|
|
|
|
Init(uv_loop_t* loop)
|
|
|
|
{
|
2019-08-02 09:27:27 +00:00
|
|
|
memcpy(m_Device->if_name, m_Tun->ifname, sizeof(m_Device->if_name));
|
2019-06-02 21:17:05 +00:00
|
|
|
if(tuntap_start(m_Device, TUNTAP_MODE_TUNNEL, 0) == -1)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to start up ", m_Tun->ifname);
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-03 12:02:54 +00:00
|
|
|
if(tuntap_set_ip(m_Device, m_Tun->ifaddr, m_Tun->ifaddr, m_Tun->netmask)
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to set address on ", m_Tun->ifname);
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
if(tuntap_up(m_Device) == -1)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to put up ", m_Tun->ifname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(m_Device->tun_fd == -1)
|
|
|
|
{
|
|
|
|
llarp::LogError("tun interface ", m_Tun->ifname,
|
|
|
|
" has invalid fd: ", m_Device->tun_fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(uv_poll_init(loop, &m_Handle, m_Device->tun_fd) == -1)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to start polling on ", m_Tun->ifname);
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-02 21:37:29 +00:00
|
|
|
if(uv_poll_start(&m_Handle, UV_READABLE, &OnPoll))
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to start polling on ", m_Tun->ifname);
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-04 14:36:54 +00:00
|
|
|
if(uv_check_init(loop, &m_Ticker) != 0
|
|
|
|
|| uv_check_start(&m_Ticker, &OnTick) != 0)
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to set up tun interface timer for ",
|
|
|
|
m_Tun->ifname);
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-03 12:02:54 +00:00
|
|
|
m_Tun->writepkt = &WritePkt;
|
2019-09-16 10:21:12 +00:00
|
|
|
m_Tun->impl = this;
|
2019-06-02 21:17:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
Loop::init()
|
|
|
|
{
|
2019-10-16 11:45:52 +00:00
|
|
|
if(uv_loop_init(&m_Impl) == -1)
|
2019-06-02 21:17:05 +00:00
|
|
|
return false;
|
2019-11-28 02:35:59 +00:00
|
|
|
|
|
|
|
#ifdef LOKINET_DEBUG
|
|
|
|
last_time = 0;
|
|
|
|
loop_run_count = 0;
|
|
|
|
#endif
|
|
|
|
|
2019-10-16 11:45:52 +00:00
|
|
|
m_Impl.data = this;
|
|
|
|
uv_loop_configure(&m_Impl, UV_LOOP_BLOCK_SIGNAL, SIGPIPE);
|
2019-06-02 21:17:05 +00:00
|
|
|
m_TickTimer.data = this;
|
|
|
|
m_Run.store(true);
|
2019-10-16 11:45:52 +00:00
|
|
|
return uv_timer_init(&m_Impl, &m_TickTimer) != -1;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::update_time()
|
|
|
|
{
|
2019-11-05 16:58:53 +00:00
|
|
|
llarp_ev_loop::update_time();
|
2019-10-16 11:45:52 +00:00
|
|
|
uv_update_time(&m_Impl);
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Loop::running() const
|
|
|
|
{
|
|
|
|
return m_Run.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Loop::tcp_connect(llarp_tcp_connecter* tcp, const sockaddr* addr)
|
|
|
|
{
|
2019-10-16 11:45:52 +00:00
|
|
|
auto* impl = new conn_glue(&m_Impl, tcp, addr);
|
2019-07-30 23:42:13 +00:00
|
|
|
tcp->impl = impl;
|
2019-06-03 11:14:31 +00:00
|
|
|
if(impl->ConnectAsync())
|
|
|
|
return true;
|
2019-06-02 21:17:05 +00:00
|
|
|
delete impl;
|
|
|
|
tcp->impl = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
OnTickTimeout(uv_timer_t* timer)
|
|
|
|
{
|
|
|
|
uv_stop(timer->loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Loop::tick(int ms)
|
|
|
|
{
|
2019-11-20 19:45:23 +00:00
|
|
|
if(m_Run)
|
|
|
|
{
|
2019-11-28 02:35:59 +00:00
|
|
|
#ifdef LOKINET_DEBUG
|
|
|
|
if ((uv_now(&m_Impl) - last_time) > 1000)
|
|
|
|
{
|
|
|
|
llarp::LogInfo("UV EVENT LOOP TICKS LAST SECOND: ", loop_run_count);
|
|
|
|
loop_run_count = 0;
|
|
|
|
last_time = uv_now(&m_Impl);
|
|
|
|
}
|
|
|
|
loop_run_count++;
|
|
|
|
#endif
|
|
|
|
|
2019-11-20 19:45:23 +00:00
|
|
|
uv_timer_start(&m_TickTimer, &OnTickTimeout, ms, 0);
|
|
|
|
uv_run(&m_Impl, UV_RUN_ONCE);
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::stop()
|
|
|
|
{
|
2019-11-20 19:45:23 +00:00
|
|
|
if(m_Run)
|
|
|
|
{
|
|
|
|
llarp::LogInfo("stopping event loop");
|
|
|
|
CloseAll();
|
|
|
|
// uv_stop(&m_Impl);
|
|
|
|
}
|
2019-06-02 21:17:05 +00:00
|
|
|
m_Run.store(false);
|
2019-06-20 18:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::CloseAll()
|
|
|
|
{
|
|
|
|
llarp::LogInfo("Closing all handles");
|
2019-08-13 21:38:00 +00:00
|
|
|
uv_walk(
|
2019-10-16 11:45:52 +00:00
|
|
|
&m_Impl,
|
2019-08-13 21:38:00 +00:00
|
|
|
[](uv_handle_t* h, void*) {
|
|
|
|
if(uv_is_closing(h))
|
|
|
|
return;
|
|
|
|
if(h->data && uv_is_active(h))
|
|
|
|
{
|
|
|
|
static_cast< glue* >(h->data)->Close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
nullptr);
|
2019-06-20 18:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loop::stopped()
|
|
|
|
{
|
|
|
|
tick(50);
|
|
|
|
llarp::LogInfo("we have stopped");
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Loop::udp_listen(llarp_udp_io* udp, const sockaddr* src)
|
|
|
|
{
|
2019-10-16 11:45:52 +00:00
|
|
|
auto* impl = new udp_glue(&m_Impl, udp, src);
|
2019-07-30 23:42:13 +00:00
|
|
|
udp->impl = impl;
|
2019-06-02 21:17:05 +00:00
|
|
|
if(impl->Bind())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-20 18:28:26 +00:00
|
|
|
delete impl;
|
2019-06-02 21:17:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:39:09 +00:00
|
|
|
bool
|
|
|
|
Loop::add_ticker(std::function< void(void) > func)
|
|
|
|
{
|
2019-10-16 11:45:52 +00:00
|
|
|
auto* ticker = new ticker_glue(&m_Impl, func);
|
2019-09-05 17:39:09 +00:00
|
|
|
if(ticker->Start())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
delete ticker;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
bool
|
|
|
|
Loop::udp_close(llarp_udp_io* udp)
|
|
|
|
{
|
|
|
|
if(udp == nullptr)
|
|
|
|
return false;
|
2019-07-30 23:42:13 +00:00
|
|
|
auto* glue = static_cast< udp_glue* >(udp->impl);
|
2019-06-02 21:17:05 +00:00
|
|
|
if(glue == nullptr)
|
|
|
|
return false;
|
|
|
|
glue->Close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Loop::tun_listen(llarp_tun_io* tun)
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
auto* glue = new tun_glue(tun);
|
|
|
|
tun->impl = glue;
|
2019-10-16 11:45:52 +00:00
|
|
|
if(glue->Init(&m_Impl))
|
2019-06-02 21:17:05 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
delete glue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Loop::tcp_listen(llarp_tcp_acceptor* tcp, const sockaddr* addr)
|
|
|
|
{
|
2019-10-16 11:45:52 +00:00
|
|
|
auto* glue = new conn_glue(&m_Impl, tcp, addr);
|
2019-07-30 23:42:13 +00:00
|
|
|
tcp->impl = glue;
|
2019-06-03 11:14:31 +00:00
|
|
|
if(glue->Server())
|
|
|
|
return true;
|
|
|
|
tcp->impl = nullptr;
|
|
|
|
delete glue;
|
|
|
|
return false;
|
2019-06-02 21:17:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 11:18:05 +00:00
|
|
|
bool
|
|
|
|
Loop::add_pipe(llarp_ev_pkt_pipe* p)
|
|
|
|
{
|
2019-10-16 11:45:52 +00:00
|
|
|
auto* glue = new pipe_glue(&m_Impl, p);
|
2019-08-22 11:18:05 +00:00
|
|
|
if(glue->Start())
|
|
|
|
return true;
|
|
|
|
delete glue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:17:05 +00:00
|
|
|
} // namespace libuv
|
2019-10-02 13:06:14 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_ev_udp_recvmany(struct llarp_udp_io* u, struct llarp_pkt_list* pkts)
|
|
|
|
{
|
|
|
|
return static_cast< libuv::udp_glue* >(u->impl)->RecvMany(pkts);
|
2019-11-28 02:35:59 +00:00
|
|
|
}
|