lokinet/llarp/ev/ev.cpp

74 lines
1.5 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>
2018-04-30 13:18:57 +00:00
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
{
if (ev == nullptr or logic == nullptr)
return;
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
llarp_ev_add_udp(const llarp_ev_loop_ptr& ev, struct llarp_udp_io* udp, const llarp::SockAddr& src)
{
if (ev == nullptr or udp == nullptr)
{
llarp::LogError("Attempting llarp_ev_add_udp() with null event loop or udp io struct.");
return -1;
}
udp->parent = ev.get();
if (ev->udp_listen(udp, src))
return 0;
llarp::LogError("llarp_ev_add_udp() call to udp_listen failed.");
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);
}