2019-01-11 01:19:36 +00:00
|
|
|
#include <ev/ev.h>
|
2019-01-10 19:41:51 +00:00
|
|
|
#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>
|
2018-11-06 22:48:17 +00:00
|
|
|
|
2019-06-17 23:19:39 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2020-05-01 19:51:15 +00:00
|
|
|
#include <string_view>
|
2018-11-06 22:48:17 +00:00
|
|
|
|
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
|
2020-06-01 13:17:44 +00:00
|
|
|
llarp_make_ev_loop(size_t queueLength)
|
2019-02-11 14:43:48 +00:00
|
|
|
{
|
2020-06-01 13:17:44 +00:00
|
|
|
llarp_ev_loop_ptr r = std::make_shared<libuv::Loop>(queueLength);
|
2019-02-11 14:43:48 +00:00
|
|
|
r->init();
|
2019-03-15 14:31:54 +00:00
|
|
|
r->update_time();
|
2019-02-11 14:43:48 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
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
|
|
|
{
|
2020-06-11 11:44:02 +00:00
|
|
|
if (ev == nullptr or logic == nullptr)
|
|
|
|
return;
|
2020-05-21 14:20:47 +00:00
|
|
|
ev->run();
|
2019-11-23 04:47:08 +00:00
|
|
|
logic->clear_event_loop();
|
2019-06-02 21:17:05 +00:00
|
|
|
ev->stopped();
|
2018-06-06 12:46:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2021-01-11 23:13:22 +00:00
|
|
|
llarp_ev_add_udp(const llarp_ev_loop_ptr& ev, struct llarp_udp_io* udp, const llarp::SockAddr& src)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2020-06-11 11:44:02 +00:00
|
|
|
if (ev == nullptr or udp == nullptr)
|
2020-10-21 13:06:43 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("Attempting llarp_ev_add_udp() with null event loop or udp io struct.");
|
2020-06-11 11:44:02 +00:00
|
|
|
return -1;
|
2020-10-21 13:06:43 +00:00
|
|
|
}
|
2021-01-11 23:13:22 +00:00
|
|
|
udp->parent = ev.get();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ev->udp_listen(udp, src))
|
2018-05-22 15:54:19 +00:00
|
|
|
return 0;
|
2020-10-21 13:06:43 +00:00
|
|
|
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
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_close_udp(struct llarp_udp_io* udp)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (udp->parent->udp_close(udp))
|
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-29 14:19:00 +00:00
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
llarp_time_t
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_loop_time_now_ms(const llarp_ev_loop_ptr& loop)
|
2018-10-29 16:48:36 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (loop)
|
2019-03-15 14:31:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_loop_stop(const llarp_ev_loop_ptr& 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
|
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)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-06-02 21:17:05 +00:00
|
|
|
return udp->sendto(udp, to, buf.base, buf.sz);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|