1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/ev.cpp

158 lines
3.1 KiB
C++
Raw Normal View History

2018-01-25 17:24:33 +01:00
#include <llarp/ev.h>
2018-06-06 14:46:26 +02:00
#include <llarp/logic.h>
2018-02-01 14:21:00 +01:00
#include "mem.hpp"
2018-01-08 14:49:05 +01:00
2018-08-10 05:51:38 +02:00
#define EV_TICK_INTERVAL 100
2018-08-15 04:42:33 +02:00
// apparently current Solaris will emulate epoll.
#if __linux__ || __sun__
2018-06-06 14:46:26 +02:00
#include "ev_epoll.hpp"
2018-05-29 14:14:50 +02:00
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
|| (__APPLE__ && __MACH__)
2018-06-06 14:46:26 +02:00
#include "ev_kqueue.hpp"
2018-04-30 15:18:57 +02:00
#endif
#if defined(_WIN32) || defined(_WIN64) || defined(__NT__)
#include "ev_win32.hpp"
2018-04-30 15:18:57 +02:00
#endif
void
llarp_ev_loop_alloc(struct llarp_ev_loop **ev)
{
#if __linux__ || __sun__
2018-04-30 15:18:57 +02:00
*ev = new llarp_epoll_loop;
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
|| (__APPLE__ && __MACH__)
2018-05-29 14:14:50 +02:00
*ev = new llarp_kqueue_loop;
#endif
#if defined(_WIN32) || defined(_WIN64) || defined(__NT__)
*ev = new llarp_win32_loop;
2018-04-30 15:18:57 +02:00
#endif
2018-05-18 15:17:58 +02:00
(*ev)->init();
2018-01-29 15:27:24 +01:00
}
2018-01-08 14:49:05 +01:00
void
llarp_ev_loop_free(struct llarp_ev_loop **ev)
{
2018-04-30 15:18:57 +02:00
delete *ev;
2018-01-29 15:27:24 +01:00
*ev = nullptr;
}
int
llarp_ev_loop_run(struct llarp_ev_loop *ev, struct llarp_logic *logic)
{
2018-08-29 22:40:26 +02:00
while(ev->running())
{
2018-09-21 14:30:57 +02:00
ev->tick(EV_TICK_INTERVAL);
if(ev->running())
llarp_logic_tick(logic);
}
return 0;
}
2018-01-29 15:27:24 +01:00
2018-06-06 14:46:26 +02:00
void
llarp_ev_loop_run_single_process(struct llarp_ev_loop *ev,
struct llarp_threadpool *tp,
struct llarp_logic *logic)
{
2018-08-10 05:51:38 +02:00
while(ev->running())
2018-06-06 14:46:26 +02:00
{
2018-08-10 05:51:38 +02:00
ev->tick(EV_TICK_INTERVAL);
2018-09-21 14:30:57 +02:00
if(ev->running())
{
llarp_logic_tick_async(logic);
llarp_threadpool_tick(tp);
}
2018-06-06 14:46:26 +02:00
}
}
int
2018-05-23 15:49:00 +02:00
llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp,
const struct sockaddr *src)
{
2018-05-16 18:41:20 +02:00
udp->parent = ev;
2018-05-23 15:49:00 +02:00
if(ev->udp_listen(udp, src))
return 0;
2018-05-16 18:41:20 +02:00
return -1;
2018-01-29 15:27:24 +01:00
}
2018-01-19 17:51:27 +01:00
int
2018-05-22 21:19:06 +02:00
llarp_ev_close_udp(struct llarp_udp_io *udp)
{
if(udp->parent->udp_close(udp))
return 0;
2018-05-16 18:41:20 +02:00
return -1;
2018-01-29 15:27:24 +01:00
}
2018-01-29 15:19:00 +01:00
void
llarp_ev_loop_stop(struct llarp_ev_loop *loop)
2018-05-18 19:10:48 +02:00
{
loop->stop();
2018-05-18 19:10:48 +02:00
}
int
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const sockaddr *to,
const void *buf, size_t sz)
{
2018-08-02 06:34:46 +02:00
auto ret = static_cast< llarp::ev_io * >(udp->impl)->sendto(to, buf, sz);
2018-10-21 19:07:17 +02:00
if(ret == -1 || errno)
2018-08-02 06:34:46 +02:00
{
llarp::LogWarn("sendto failed ", strerror(errno));
errno = 0;
}
return ret;
}
2018-08-15 17:36:34 +02:00
bool
llarp_ev_add_tun(struct llarp_ev_loop *loop, struct llarp_tun_io *tun)
{
auto dev = loop->create_tun(tun);
tun->impl = dev;
2018-08-20 21:12:12 +02:00
if(dev)
{
return loop->add_ev(dev, true);
}
2018-08-20 21:12:12 +02:00
return false;
2018-08-15 17:36:34 +02:00
}
bool
llarp_ev_tun_async_write(struct llarp_tun_io *tun, const void *pkt, size_t sz)
{
// TODO: queue write
return static_cast< llarp::ev_io * >(tun->impl)->do_write((void *)pkt, sz);
}
2018-10-09 14:06:30 +02:00
bool
2018-10-23 13:29:37 +02:00
llarp_tcp_serve(struct llarp_ev_loop *loop, struct llarp_tcp_acceptor *tcp,
const struct sockaddr *bindaddr)
2018-10-09 14:06:30 +02:00
{
2018-10-23 13:29:37 +02:00
tcp->loop = loop;
2018-10-09 14:06:30 +02:00
// TODO: implement me
return false;
}
2018-10-19 13:41:36 +02:00
void
llarp_tcp_acceptor_close(struct llarp_tcp_acceptor *tcp)
{
// TODO: implement me
}
2018-10-09 14:06:30 +02:00
void
llarp_tcp_conn_close(struct llarp_tcp_conn *conn)
{
if(!conn)
return;
llarp::ev_io *impl = static_cast< llarp::ev_io * >(conn->impl);
conn->impl = nullptr;
// deregister
conn->loop->close_ev(impl);
// close fd and delete impl
delete impl;
// call hook if needed
if(conn->closed)
conn->closed(conn);
// delete
delete conn;
}