lokinet/llarp/ev.cpp
Jeff Becker b9f07e8593 * tweak event loop parameters
* various code cleanup
2018-07-30 10:30:10 +10:00

93 lines
1.7 KiB
C++

#include <llarp/ev.h>
#include <llarp/logic.h>
#include "mem.hpp"
#ifdef __linux__
#include "ev_epoll.hpp"
#endif
#if(__APPLE__ && __MACH__)
#include "ev_kqueue.hpp"
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#include "ev_kqueue.hpp"
#endif
void
llarp_ev_loop_alloc(struct llarp_ev_loop **ev)
{
#ifdef __linux__
*ev = new llarp_epoll_loop;
#endif
#if(__APPLE__ && __MACH__)
*ev = new llarp_kqueue_loop;
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
*ev = new llarp_kqueue_loop;
#endif
(*ev)->init();
}
void
llarp_ev_loop_free(struct llarp_ev_loop **ev)
{
delete *ev;
*ev = nullptr;
}
int
llarp_ev_loop_run(struct llarp_ev_loop *ev, struct llarp_logic *logic)
{
while(true)
{
if(ev->tick(100) == -1)
break;
llarp_logic_tick(logic);
}
return 0;
}
void
llarp_ev_loop_run_single_process(struct llarp_ev_loop *ev,
struct llarp_threadpool *tp,
struct llarp_logic *logic)
{
while(true)
{
if(ev->tick(10) == -1)
return;
llarp_logic_tick(logic);
llarp_threadpool_tick(tp);
}
}
int
llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp,
const struct sockaddr *src)
{
udp->parent = ev;
if(ev->udp_listen(udp, src))
return 0;
return -1;
}
int
llarp_ev_close_udp(struct llarp_udp_io *udp)
{
if(udp->parent->udp_close(udp))
return 0;
return -1;
}
void
llarp_ev_loop_stop(struct llarp_ev_loop *loop)
{
loop->stop();
}
int
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const sockaddr *to,
const void *buf, size_t sz)
{
return static_cast< llarp::ev_io * >(udp->impl)->sendto(to, buf, sz);
}