lokinet/llarp/ev/ev.hpp

156 lines
3.1 KiB
C++
Raw Normal View History

2018-04-30 13:18:57 +00:00
#ifndef LLARP_EV_HPP
#define LLARP_EV_HPP
2020-05-06 20:38:44 +00:00
#include <net/ip_address.hpp>
#include <net/ip_packet.hpp>
2019-01-11 01:19:36 +00:00
#include <ev/ev.h>
2019-02-02 23:12:42 +00:00
#include <util/buffer.hpp>
#include <util/codel.hpp>
2019-09-01 13:26:16 +00:00
#include <util/thread/threading.hpp>
// writev
#ifndef _WIN32
#include <sys/uio.h>
#include <unistd.h>
#endif
#include <algorithm>
#include <deque>
#include <list>
#include <future>
2019-07-30 23:42:13 +00:00
#include <utility>
#ifdef _WIN32
2018-12-13 23:02:41 +00:00
// From the preview SDK, should take a look at that
// periodically in case its definition changes
#define UNIX_PATH_MAX 108
typedef struct sockaddr_un
{
2018-12-13 23:02:41 +00:00
ADDRESS_FAMILY sun_family; /* AF_UNIX */
char sun_path[UNIX_PATH_MAX]; /* pathname */
} SOCKADDR_UN, *PSOCKADDR_UN;
2018-12-03 20:37:25 +00:00
#else
2018-12-04 23:45:08 +00:00
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || (__APPLE__ && __MACH__)
2018-12-04 23:45:08 +00:00
#include <sys/event.h>
#endif
2018-12-03 20:37:25 +00:00
#include <sys/un.h>
#endif
2019-08-22 11:18:05 +00:00
struct llarp_ev_pkt_pipe;
2018-08-15 15:36:34 +00:00
#ifndef MAX_WRITE_QUEUE_SIZE
2018-11-09 14:48:43 +00:00
#define MAX_WRITE_QUEUE_SIZE (1024UL)
2018-08-15 15:36:34 +00:00
#endif
2018-04-30 13:18:57 +00:00
2018-09-06 20:31:58 +00:00
#ifndef EV_READ_BUF_SZ
2018-11-09 14:48:43 +00:00
#define EV_READ_BUF_SZ (4 * 1024UL)
2018-09-06 20:31:58 +00:00
#endif
2018-10-27 18:26:08 +00:00
#ifndef EV_WRITE_BUF_SZ
2020-01-07 11:59:17 +00:00
#define EV_WRITE_BUF_SZ (4 * 1024UL)
2018-10-27 18:26:08 +00:00
#endif
2018-09-06 20:31:58 +00:00
namespace llarp
{
namespace vpn
2018-10-29 16:48:36 +00:00
{
class NetworkInterface;
}
2021-02-22 13:26:32 +00:00
/// distinct event loop waker uper
class EventLoopWakeup
{
protected:
std::function<void()> callback;
public:
EventLoopWakeup(std::function<void()> cb) : callback{cb}
{}
virtual ~EventLoopWakeup() = default;
/// async wakeup and call callback once
virtual void
Wakeup() = 0;
/// end operation
virtual void
End() = 0;
};
// this (nearly!) abstract base class
// is overriden for each platform
struct EventLoop
{
virtual bool
init() = 0;
virtual int
run() = 0;
2018-10-25 12:39:32 +00:00
virtual bool
running() const = 0;
virtual void
update_time()
{}
virtual llarp_time_t
time_now() const
{
return llarp::time_now_ms();
}
virtual void
stopped(){};
virtual uint32_t
call_after_delay(llarp_time_t delay_ms, std::function<void(void)> callback) = 0;
virtual void
cancel_delayed_call(uint32_t call_id) = 0;
virtual bool
add_network_interface(
std::shared_ptr<vpn::NetworkInterface> netif,
std::function<void(net::IPPacket)> packetHandler) = 0;
virtual bool
add_ticker(std::function<void(void)> ticker) = 0;
2018-11-01 12:47:14 +00:00
virtual void
stop() = 0;
virtual bool
udp_listen(llarp_udp_io* l, const llarp::SockAddr& src) = 0;
virtual bool
udp_close(llarp_udp_io* l) = 0;
/// give this event loop a logic thread for calling
virtual void set_logic(std::shared_ptr<llarp::Logic>) = 0;
2019-07-06 17:03:40 +00:00
virtual ~EventLoop() = default;
virtual void
call_soon(std::function<void(void)> f) = 0;
/// set the function that is called once per cycle the flush all the queues
2018-12-14 15:43:44 +00:00
virtual void
set_pump_function(std::function<void(void)> pumpll) = 0;
2018-12-14 15:43:44 +00:00
virtual void
register_poll_fd_readable(int fd, std::function<void(void)> callback) = 0;
virtual void
deregister_poll_fd_readable(int fd) = 0;
2021-02-22 13:26:32 +00:00
/// make an event loop waker on this event loop
virtual EventLoopWakeup*
make_event_loop_waker(std::function<void()> callback) = 0;
};
2019-04-25 23:21:19 +00:00
} // namespace llarp
2018-04-30 13:18:57 +00:00
#endif