lokinet/llarp/ev/ev.cpp

196 lines
4.0 KiB
C++
Raw Normal View History

2019-01-11 01:19:36 +00:00
#include <ev/ev.h>
#include <util/mem.hpp>
2020-05-06 20:38:44 +00:00
#include <util/str.hpp>
2019-09-01 13:26:16 +00:00
#include <util/thread/logic.hpp>
2019-06-17 23:19:39 +00:00
#include <cstddef>
#include <cstring>
#include <string_view>
2019-06-14 01:36:31 +00:00
// We libuv now
#include <ev/ev_libuv.hpp>
2019-12-11 21:05:40 +00:00
#if defined(_WIN32) || defined(_WIN64) || defined(__NT__)
2019-03-25 18:37:11 +00:00
#define SHUT_RDWR SD_BOTH
2019-01-11 01:19:36 +00:00
#include <ev/ev_win32.hpp>
2018-04-30 13:18:57 +00:00
#endif
2019-04-08 12:01:52 +00:00
llarp_ev_loop_ptr
llarp_make_ev_loop(size_t queueLength)
{
llarp_ev_loop_ptr r = std::make_shared<libuv::Loop>(queueLength);
r->init();
r->update_time();
return r;
}
2018-06-06 12:46:26 +00:00
void
llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev, std::shared_ptr<llarp::Logic> logic)
2018-06-06 12:46:26 +00:00
{
ev->run();
logic->clear_event_loop();
2019-06-02 21:17:05 +00:00
ev->stopped();
2018-06-06 12:46:26 +00:00
}
int
2020-05-06 20:38:44 +00:00
llarp_ev_add_udp(struct llarp_ev_loop* ev, struct llarp_udp_io* udp, const llarp::SockAddr& src)
{
2018-05-16 16:41:20 +00:00
udp->parent = ev;
if (ev->udp_listen(udp, src))
return 0;
2018-05-16 16:41:20 +00:00
return -1;
2018-01-29 14:27:24 +00:00
}
2018-01-19 16:51:27 +00:00
int
llarp_ev_close_udp(struct llarp_udp_io* udp)
{
if (udp->parent->udp_close(udp))
return 0;
2018-05-16 16:41:20 +00:00
return -1;
2018-01-29 14:27:24 +00:00
}
2018-01-29 14:19:00 +00:00
2018-10-29 16:48:36 +00:00
llarp_time_t
llarp_ev_loop_time_now_ms(const llarp_ev_loop_ptr& loop)
2018-10-29 16:48:36 +00:00
{
if (loop)
return loop->time_now();
2018-11-20 12:44:18 +00:00
return llarp::time_now_ms();
2018-10-29 16:48:36 +00:00
}
void
llarp_ev_loop_stop(const llarp_ev_loop_ptr& loop)
2018-05-18 17:10:48 +00:00
{
loop->stop();
2018-05-18 17:10:48 +00:00
}
int
2020-05-06 20:38:44 +00:00
llarp_ev_udp_sendto(struct llarp_udp_io* udp, const llarp::SockAddr& to, const llarp_buffer_t& buf)
{
2019-06-02 21:17:05 +00:00
return udp->sendto(udp, to, buf.base, buf.sz);
}
2018-08-15 15:36:34 +00:00
bool
llarp_ev_add_tun(struct llarp_ev_loop* loop, struct llarp_tun_io* tun)
2018-08-15 15:36:34 +00:00
{
if (tun->ifaddr[0] == 0 || strcmp(tun->ifaddr, "auto") == 0)
{
LogError("invalid ifaddr on tun: ", tun->ifaddr);
return false;
}
if (tun->ifname[0] == 0 || strcmp(tun->ifname, "auto") == 0)
{
LogError("invalid ifname on tun: ", tun->ifname);
return false;
}
#if !defined(_WIN32)
2019-06-02 21:17:05 +00:00
return loop->tun_listen(tun);
#else
UNREFERENCED_PARAMETER(loop);
auto dev = new win32_tun_io(tun);
tun->impl = dev;
// We're not even going to add this to the socket event loop
if (dev)
{
dev->setup();
2019-12-11 21:18:47 +00:00
return dev->add_ev(loop); // start up tun and add to event queue
}
llarp::LogWarn("Loop could not create tun");
return false;
2019-06-11 16:44:05 +00:00
#endif
}
bool
llarp_ev_tun_async_write(struct llarp_tun_io* tun, const llarp_buffer_t& buf)
{
if (buf.sz > EV_WRITE_BUF_SZ)
{
llarp::LogWarn("packet too big, ", buf.sz, " > ", EV_WRITE_BUF_SZ);
return false;
}
#ifndef _WIN32
2019-06-03 12:02:54 +00:00
return tun->writepkt(tun, buf.base, buf.sz);
#else
return static_cast<win32_tun_io*>(tun->impl)->queue_write(buf.base, buf.sz);
#endif
}
2018-08-15 15:36:34 +00:00
bool
llarp_tcp_conn_async_write(struct llarp_tcp_conn* conn, const llarp_buffer_t& b)
2018-08-15 15:36:34 +00:00
{
2019-02-03 00:48:10 +00:00
ManagedBuffer buf{b};
2019-06-03 12:02:54 +00:00
size_t sz = buf.underlying.sz;
2019-02-02 23:12:42 +00:00
buf.underlying.cur = buf.underlying.base;
while (sz > EV_WRITE_BUF_SZ)
2018-10-25 12:09:29 +00:00
{
2019-06-03 12:02:54 +00:00
ssize_t amount = conn->write(conn, buf.underlying.cur, EV_WRITE_BUF_SZ);
if (amount <= 0)
2019-06-03 14:03:59 +00:00
{
llarp::LogError("write underrun");
2020-01-07 11:59:17 +00:00
llarp_tcp_conn_close(conn);
2018-10-25 12:09:29 +00:00
return false;
2019-06-03 14:03:59 +00:00
}
2019-06-03 12:02:54 +00:00
buf.underlying.cur += amount;
sz -= amount;
2018-10-25 12:09:29 +00:00
}
2019-06-03 14:03:59 +00:00
return conn->write(conn, buf.underlying.cur, sz) > 0;
}
2018-10-09 12:06:30 +00:00
2018-11-01 12:47:14 +00:00
void
llarp_tcp_async_try_connect(struct llarp_ev_loop* loop, struct llarp_tcp_connecter* tcp)
2018-11-01 12:47:14 +00:00
{
tcp->loop = loop;
2020-05-06 20:38:44 +00:00
llarp::IpAddress address(tcp->remote);
if (not address.getPort())
throw std::runtime_error(llarp::stringify("Address with no port: ", address));
llarp::SockAddr addr = address.createSockAddr();
2018-11-01 12:47:14 +00:00
if (!loop->tcp_connect(tcp, addr))
2018-11-01 12:47:14 +00:00
{
llarp::LogError("async connect failed");
if (tcp->error)
tcp->error(tcp);
2018-11-01 12:47:14 +00:00
}
}
2018-10-09 12:06:30 +00:00
bool
llarp_tcp_serve(
2020-05-08 17:23:21 +00:00
struct llarp_ev_loop* loop, struct llarp_tcp_acceptor* tcp, const llarp::SockAddr& bindaddr)
2018-10-09 12:06:30 +00:00
{
2019-06-02 21:17:05 +00:00
tcp->loop = loop;
return loop->tcp_listen(tcp, bindaddr);
2018-10-09 12:06:30 +00:00
}
2018-10-19 11:41:36 +00:00
void
llarp_tcp_acceptor_close(struct llarp_tcp_acceptor* tcp)
2018-10-19 11:41:36 +00:00
{
2019-06-03 14:03:59 +00:00
tcp->close(tcp);
}
2018-10-09 12:06:30 +00:00
void
llarp_tcp_conn_close(struct llarp_tcp_conn* conn)
2018-10-09 12:06:30 +00:00
{
2019-06-02 21:17:05 +00:00
conn->close(conn);
2018-10-09 12:06:30 +00:00
}
namespace llarp
{
2018-10-25 12:39:32 +00:00
bool
tcp_conn::tick()
{
if (_shouldClose)
2018-10-25 12:39:32 +00:00
{
if (tcp.closed)
2018-11-01 12:47:14 +00:00
tcp.closed(&tcp);
2019-03-25 15:41:37 +00:00
::shutdown(fd, SHUT_RDWR);
2018-10-25 12:39:32 +00:00
return false;
}
if (tcp.tick)
2018-11-01 12:47:14 +00:00
tcp.tick(&tcp);
2018-10-25 12:39:32 +00:00
return true;
}
} // namespace llarp