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