Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ option(NETKIT_ENABLE_FALLBACK_CA "Enable fallback CA certficate (required for De
option(NETKIT_ENABLE_SOCK_CUSTOM_RESOLVER "Enable using netkit's DNS resolver instead of the system resolver" OFF)
option(NETKIT_ENABLE_EPOLL "Enable using epoll backend (Linux only)" ON)
option(NETKIT_ENABLE_WSAPOLL "Enable using wsapoll backend (Windows only)" ON)
option(NETKIT_ENABLE_KQUEUE "Enable using kqueue backend (macOS, BSD only)" ON)
option(NETKIT_ENABLE_HTTP "Enable netkit's HTTP abstractions" ON)
option(NETKIT_ENABLE_DNS "Enable netkit's DNS features" ON)
option(NETKIT_WOLFSSL_DEBUG "Enable WolfSSL debugging" OFF)
Expand Down Expand Up @@ -75,6 +76,7 @@ add_library(netkit ${NETKIT_LIB_TYPE}
src/body/multipart_part_body.cpp
src/io/io_awaitable.cpp
src/io/linux/io_backend.cpp
src/io/bsd/io_backend.cpp
src/socket/native/native_async_socket.cpp
src/socket/native/native_sync_listener.cpp
src/socket/native/native_async_listener.cpp
Expand Down Expand Up @@ -215,6 +217,10 @@ if (NETKIT_ENABLE_WSAPOLL)
target_compile_definitions(netkit PUBLIC NETKIT_WSAPOLL)
endif()

if (NETKIT_ENABLE_KQUEUE)
target_compile_definitions(netkit PUBLIC NETKIT_KQUEUE)
endif()

if (WIN32)
add_definitions(-DNOMINMAX)
endif()
Expand Down
6 changes: 6 additions & 0 deletions include/netkit/definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
#define NETKIT_LINUX 1
#endif

#if !defined(NETKIT_LINUX) && \
defined(NETKIT_UNIX) && !defined(NETKIT_DKP) && \
!defined(NETKIT_MACOS)
#define NETKIT_BSD
#endif

#ifndef NETKIT_FALLBACK_IPV4_DNS_1
#define NETKIT_FALLBACK_IPV4_DNS_1 "8.8.8.8"
#endif
Expand Down
53 changes: 53 additions & 0 deletions include/netkit/io/bsd/io_backend.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <netkit/definitions.hpp>
#include <unordered_set>

#if (defined(NETKIT_MACOS) || defined(NETKIT_BSD)) \
&& defined(NETKIT_KQUEUE)

#include <netkit/io/basic_io_backend.hpp>

#include <coroutine>
#include <unordered_map>
#include <sys/event.h>

namespace netkit::io {

class io_backend : public basic_io_backend {
public:
io_backend();
~io_backend() override;

void wake() override;

void update_state(
io_handle_t fd,
const io_handle_state& state
) override;

void register_waiter(
io_handle_t fd,
io_event ev,
std::coroutine_handle<> h
) override;

void run() override;
void stop() override;

void poll(int timeout_ms) override;
void poll() override;

private:
static constexpr uintptr_t wake_ident = 1;

io_handle_t kqueue_fd_ = -1;
bool running_ = true;

std::unordered_map<io_handle_t, io_handle_state> fd_map_;
std::unordered_set<io_handle_t> registered_fds_;
};

} // namespace netkit::io

#endif
4 changes: 4 additions & 0 deletions include/netkit/io/fallback/io_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#if !defined(NETKIT_LINUX) || !defined(NETKIT_EPOLL)
#if !defined(NETKIT_WINDOWS) || !defined(NETKIT_WSAPOLL)
#if !defined(NETKIT_BSD) || !defined(NETKIT_KQUEUE)
#if !defined(NETKIT_MACOS) || !defined(NETKIT_KQUEUE)

#include <atomic>
#include <condition_variable>
Expand Down Expand Up @@ -54,5 +56,7 @@ class NETKIT_API io_backend : public basic_io_backend {

} // namespace netkit::io

#endif
#endif
#endif
#endif
3 changes: 2 additions & 1 deletion include/netkit/io/io_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

#include <netkit/io/fallback/io_backend.hpp>
#include <netkit/io/linux/io_backend.hpp>
#include <netkit/io/windows/io_backend.hpp>
#include <netkit/io/windows/io_backend.hpp>
#include <netkit/io/bsd/io_backend.hpp>
1 change: 1 addition & 0 deletions include/netkit/netkit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#include <netkit/io/linux/io_backend.hpp>
#include <netkit/io/fallback/io_backend.hpp>
#include <netkit/io/windows/io_backend.hpp>
#include <netkit/io/bsd/io_backend.hpp>
#include <netkit/io/io_backend.hpp>
#include <netkit/io/io_awaitable.hpp>
#include <netkit/io/io_context.hpp>
Expand Down
Loading
Loading