lokinet/llarp/ev/ev.cpp

235 lines
5.0 KiB
C++
Raw Normal View History

2019-01-11 01:19:36 +00:00
#include <ev/ev.h>
#include <util/mem.hpp>
#include <util/string_view.hpp>
2019-09-01 13:26:16 +00:00
#include <util/thread/logic.hpp>
2019-06-17 23:19:39 +00:00
#include <net/net_addr.hpp>
2019-06-17 23:19:39 +00:00
#include <cstddef>
#include <cstring>
2019-06-14 01:36:31 +00:00
// We libuv now
#ifndef _WIN32
#include <ev/ev_libuv.hpp>
#elif 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>
#else
2019-06-14 01:36:31 +00:00
#error No async event loop for your platform, port libuv to your operating system
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()
{
2019-06-14 01:36:31 +00:00
#ifndef _WIN32
2019-06-17 13:05:37 +00:00
llarp_ev_loop_ptr r = std::make_shared< libuv::Loop >();
#elif defined(_WIN32) || defined(_WIN64) || defined(__NT__)
2019-04-08 12:01:52 +00:00
llarp_ev_loop_ptr r = std::make_shared< llarp_win32_loop >();
#else
#error no event loop subclass
#endif
r->init();
r->update_time();
return r;
}
2018-06-06 12:46:26 +00:00
void
2019-04-08 12:01:52 +00:00
llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev,
2019-05-22 16:20:50 +00:00
std::shared_ptr< llarp::Logic > logic)
2018-06-06 12:46:26 +00:00
{
2019-10-02 13:06:14 +00:00
ev->set_logic(logic);
2018-08-10 03:51:38 +00:00
while(ev->running())
2018-06-06 12:46:26 +00:00
{
ev->update_time();
2018-08-10 03:51:38 +00:00
ev->tick(EV_TICK_INTERVAL);
2019-05-16 14:30:47 +00:00
if(ev->running())
{
ev->update_time();
logic->tick(ev->time_now());
2019-05-16 14:30:47 +00:00
}
2019-05-15 15:54:26 +00:00
llarp::LogContext::Instance().logStream->Tick(ev->time_now());
2018-06-06 12:46:26 +00:00
}
2019-06-02 21:17:05 +00:00
ev->stopped();
2018-06-06 12:46:26 +00:00
}
int
2018-05-23 13:49:00 +00:00
llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp,
const struct sockaddr *src)
{
2018-05-16 16:41:20 +00:00
udp->parent = ev;
2018-05-23 13:49:00 +00:00
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
2018-05-22 19:19:06 +00:00
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
{
2018-11-15 21:47:05 +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
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const 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)
{
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
2018-12-23 13:29:11 +00:00
if(dev)
{
dev->setup();
return dev->add_ev(); // 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
2019-02-02 23:12:42 +00:00
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
2018-12-23 13:29:11 +00:00
return static_cast< win32_tun_io * >(tun->impl)->queue_write(buf.base,
buf.sz);
#endif
}
2018-08-15 15:36:34 +00:00
bool
2019-02-02 23:12:42 +00:00
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
2019-02-02 23:12:42 +00:00
size_t sz = buf.underlying.sz;
buf.underlying.cur = buf.underlying.base;
2018-10-27 18:26:08 +00:00
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");
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)
{
tcp->loop = loop;
llarp::string_view addr_str, port_str;
// try parsing address
const char *begin = tcp->remote;
const char *ptr = strstr(tcp->remote, ":");
// get end of address
if(ptr == nullptr)
{
llarp::LogError("bad address: ", tcp->remote);
if(tcp->error)
tcp->error(tcp);
return;
}
const char *end = ptr;
2018-11-08 12:31:50 +00:00
while(*end && ((end - begin) < static_cast< ptrdiff_t >(sizeof tcp->remote)))
2018-11-01 12:47:14 +00:00
{
++end;
}
addr_str = llarp::string_view(begin, ptr - begin);
++ptr;
port_str = llarp::string_view(ptr, end - ptr);
// actually parse address
llarp::Addr addr(addr_str, port_str);
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
2018-10-23 11:29:37 +00:00
llarp_tcp_serve(struct llarp_ev_loop *loop, struct llarp_tcp_acceptor *tcp,
const struct 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)
{
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)
{
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-11-01 12:47:14 +00:00
if(tcp.closed)
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;
}
2019-07-06 17:03:40 +00:00
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