Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
KqueueProactor.hpp
Go to the documentation of this file.
1#pragma once
3#if defined(__APPLE__) || defined(__FreeBSD__)
5#include <sys/event.h>
6#include <unordered_map>
7#include <mutex>
8#include <vector>
9
10namespace orbit::network {
11
12class KqueueProactor : public Proactor {
13public:
14 KqueueProactor();
15 ~KqueueProactor() override;
16
17 void run_once(int timeout_ms) override;
18
19 void async_read(socket_t fd, void* buffer, size_t size, std::function<void(ssize_t)> callback) override;
20 void async_write(socket_t fd, const void* buffer, size_t size, std::function<void(ssize_t)> callback) override;
21 void async_wait_read(socket_t fd, std::function<void()> callback) override;
22 void async_wait_write(socket_t fd, std::function<void()> callback) override;
23 void async_sendfile(socket_t out_fd, int in_fd, off_t offset, size_t count, std::function<void(ssize_t)> callback) override;
24 void async_accept(socket_t fd, std::function<void(socket_t, sockaddr_in)> callback) override;
25 void async_connect(socket_t fd, const sockaddr_in& addr, std::function<void(int)> callback) override;
26
27 void remove(socket_t fd) override;
28
29private:
30 struct Context {
31 int fd{-1};
32 bool tracked{false};
33
34 bool reading{false};
35 void* read_buf{nullptr};
36 size_t read_size{0};
37 std::function<void(ssize_t)> read_cb;
38
39 bool writing{false};
40 const void* write_buf{nullptr};
41 size_t write_size{0};
42 std::function<void(ssize_t)> write_cb;
43
44 bool waiting_read{false};
45 std::function<void()> wait_read_cb;
46
47 bool waiting_write{false};
48 std::function<void()> wait_write_cb;
49
50 bool sendfile_in_progress{false};
51 int sendfile_in_fd{-1};
52 off_t sendfile_offset{0};
53 size_t sendfile_count{0};
54 std::function<void(ssize_t)> sendfile_cb;
55
56 bool accepting{false};
57 std::function<void(socket_t, sockaddr_in)> accept_cb;
58
59 bool connecting{false};
60 std::function<void(int)> connect_cb;
61 };
62
63 void update_kqueue(Context& ctx);
64 void handle_event(const struct kevent& event);
65
66 int kq_fd_;
67 std::unordered_map<int, Context> contexts_;
68 std::mutex ctx_mutex_;
69 std::vector<struct kevent> events_;
70};
71
72} // namespace network
73#endif
Definition Http2Session.hpp:21