lokinet/llarp/ev/ev.h

89 lines
1.9 KiB
C
Raw Normal View History

2018-01-25 16:24:33 +00:00
#ifndef LLARP_EV_H
#define LLARP_EV_H
2020-05-06 20:38:44 +00:00
#include <net/ip_address.hpp>
2019-02-02 23:12:42 +00:00
#include <util/buffer.hpp>
#include <util/time.hpp>
#ifdef _WIN32
2018-07-25 00:39:52 +00:00
#include <winsock2.h>
#include <ws2tcpip.h>
#include <wspiapi.h>
2018-07-25 00:39:52 +00:00
#else
#include <netinet/in.h>
2018-07-25 00:42:14 +00:00
#include <sys/socket.h>
2018-07-25 00:39:52 +00:00
#endif
#include <net/net_if.hpp>
#include <memory>
2019-07-30 23:42:13 +00:00
#include <cstdint>
#include <cstdlib>
#include <constants/evloop.hpp>
2018-05-25 09:17:08 +00:00
/**
* ev.h
*
* event handler (cross platform high performance event system for IO)
*/
2018-05-25 09:17:08 +00:00
#define EV_TICK_INTERVAL 10
2018-12-15 16:21:52 +00:00
2018-12-10 14:14:55 +00:00
namespace llarp
{
class Logic;
struct EventLoop;
} // namespace llarp
2018-12-10 14:14:55 +00:00
using llarp_ev_loop_ptr = std::shared_ptr<llarp::EventLoop>;
2018-05-25 09:17:08 +00:00
/// make an event loop using our baked in event loop on Windows
/// make an event loop using libuv otherwise.
/// @param queue_size how big the logic job queue is
2019-04-08 12:01:52 +00:00
llarp_ev_loop_ptr
llarp_make_ev_loop(std::size_t queue_size = llarp::event_loop_queue_size);
2019-04-08 12:01:52 +00:00
// run mainloop
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
2018-10-29 16:48:36 +00:00
/// get the current time on the event loop
llarp_time_t
llarp_ev_loop_time_now_ms(const llarp_ev_loop_ptr& ev);
2018-10-29 16:48:36 +00:00
2018-05-25 09:17:08 +00:00
/// stop event loop and wait for it to complete all jobs
void
llarp_ev_loop_stop(const llarp_ev_loop_ptr& ev);
2018-01-29 14:27:24 +00:00
2018-05-25 09:17:08 +00:00
/// UDP handling configuration
struct llarp_udp_io
{
2018-09-06 20:31:58 +00:00
/// set after added
int fd;
void* user;
void* impl;
llarp::EventLoop* parent;
2018-10-29 16:48:36 +00:00
/// called every event loop tick after reads
void (*tick)(struct llarp_udp_io*);
2020-05-06 20:38:44 +00:00
void (*recvfrom)(struct llarp_udp_io*, const llarp::SockAddr& source, ManagedBuffer);
2019-06-02 21:17:05 +00:00
/// set by parent
2020-05-06 20:38:44 +00:00
int (*sendto)(struct llarp_udp_io*, const llarp::SockAddr&, const byte_t*, size_t);
2018-01-29 14:27:24 +00:00
};
2018-05-25 09:17:08 +00:00
/// add UDP handler
int
llarp_ev_add_udp(const llarp_ev_loop_ptr& ev, struct llarp_udp_io* udp, const llarp::SockAddr& src);
2018-01-29 14:27:24 +00:00
2018-11-23 14:37:26 +00:00
/// send a UDP packet
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& pkt);
2018-04-30 16:14:20 +00:00
2018-05-25 09:17:08 +00:00
/// close UDP handler
int
llarp_ev_close_udp(struct llarp_udp_io* udp);
2018-01-29 14:27:24 +00:00
2018-01-25 16:24:33 +00:00
#endif