2018-01-25 16:24:33 +00:00
|
|
|
#include <llarp/ev.h>
|
2018-06-06 12:46:26 +00:00
|
|
|
#include <llarp/logic.h>
|
2018-02-01 13:21:00 +00:00
|
|
|
#include "mem.hpp"
|
2018-01-08 13:49:05 +00:00
|
|
|
|
2018-04-30 13:18:57 +00:00
|
|
|
#ifdef __linux__
|
2018-06-06 12:46:26 +00:00
|
|
|
#include "ev_epoll.hpp"
|
2018-05-29 12:14:50 +00:00
|
|
|
#endif
|
2018-07-30 04:38:14 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || (__APPLE__ && __MACH__)
|
2018-06-06 12:46:26 +00:00
|
|
|
#include "ev_kqueue.hpp"
|
2018-04-30 13:18:57 +00:00
|
|
|
#endif
|
2018-07-30 04:38:14 +00:00
|
|
|
#if defined(_WIN32) || defined(_WIN64) || defined(__NT__)
|
|
|
|
#include "ev_win32.hpp"
|
2018-04-30 13:18:57 +00:00
|
|
|
#endif
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_ev_loop_alloc(struct llarp_ev_loop **ev)
|
|
|
|
{
|
2018-04-30 13:18:57 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
*ev = new llarp_epoll_loop;
|
|
|
|
#endif
|
2018-07-30 04:38:14 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || (__APPLE__ && __MACH__)
|
2018-05-29 12:14:50 +00:00
|
|
|
*ev = new llarp_kqueue_loop;
|
|
|
|
#endif
|
2018-07-30 04:38:14 +00:00
|
|
|
#if defined(_WIN32) || defined(_WIN64) || defined(__NT__)
|
|
|
|
*ev = new llarp_win32_loop;
|
|
|
|
#endif
|
|
|
|
// a) I assume that the libre fork of Solaris is still
|
|
|
|
// 5.10, and b) the current commercial version is 5.11, naturally.
|
|
|
|
// -despair86
|
|
|
|
#if defined(__SunOS_5_10) || defined(__SunOS_5_11)
|
|
|
|
*ev = new llarp_sun_iocp_loop;
|
2018-04-30 13:18:57 +00:00
|
|
|
#endif
|
2018-05-18 13:17:58 +00:00
|
|
|
(*ev)->init();
|
2018-01-29 14:27:24 +00:00
|
|
|
}
|
2018-01-08 13:49:05 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_ev_loop_free(struct llarp_ev_loop **ev)
|
|
|
|
{
|
2018-04-30 13:18:57 +00:00
|
|
|
delete *ev;
|
2018-01-29 14:27:24 +00:00
|
|
|
*ev = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2018-07-29 22:20:31 +00:00
|
|
|
llarp_ev_loop_run(struct llarp_ev_loop *ev, struct llarp_logic *logic)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-07-29 22:20:31 +00:00
|
|
|
while(true)
|
|
|
|
{
|
2018-07-30 00:30:10 +00:00
|
|
|
if(ev->tick(100) == -1)
|
2018-07-29 22:20:31 +00:00
|
|
|
break;
|
|
|
|
llarp_logic_tick(logic);
|
|
|
|
}
|
|
|
|
return 0;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-01-29 14:27:24 +00:00
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
void
|
|
|
|
llarp_ev_loop_run_single_process(struct llarp_ev_loop *ev,
|
|
|
|
struct llarp_threadpool *tp,
|
|
|
|
struct llarp_logic *logic)
|
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
2018-06-06 21:23:57 +00:00
|
|
|
if(ev->tick(10) == -1)
|
|
|
|
return;
|
2018-06-06 12:46:26 +00:00
|
|
|
llarp_logic_tick(logic);
|
|
|
|
llarp_threadpool_tick(tp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +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-22 15:54:19 +00:00
|
|
|
{
|
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))
|
2018-05-22 15:54:19 +00:00
|
|
|
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
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2018-05-22 19:19:06 +00:00
|
|
|
llarp_ev_close_udp(struct llarp_udp_io *udp)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
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-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_ev_loop_stop(struct llarp_ev_loop *loop)
|
2018-05-18 17:10:48 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
loop->stop();
|
2018-05-18 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
|
|
|
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const sockaddr *to,
|
|
|
|
const void *buf, size_t sz)
|
|
|
|
{
|
2018-08-02 04:34:46 +00:00
|
|
|
auto ret = static_cast< llarp::ev_io * >(udp->impl)->sendto(to, buf, sz);
|
|
|
|
if(ret == -1)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("sendto failed ", strerror(errno));
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|