COROIO: coroio/iocp.hpp Source File
COROIO
 
Loading...
Searching...
No Matches
iocp.hpp
1#pragma once
2
3#include "base.hpp"
4#include "socket.hpp"
5#include "poller.hpp"
6
7#include <stack>
8
9namespace NNet {
10
30template<typename T, size_t PoolSize = 1024>
31class TArenaAllocator {
32public:
33 TArenaAllocator() {
34 AllocatePool();
35 }
36
37 ~TArenaAllocator() {
38 for (auto block : Pools_) {
39 ::operator delete(block);
40 }
41 }
42
43 T* allocate() {
44 if (FreePages_.empty()) {
45 AllocatePool();
46 }
47 ++AllocatedObjects_;
48 T* ret = FreePages_.top();
49 FreePages_.pop();
50 return ret;
51 }
52
53 void deallocate(T* obj) {
54 --AllocatedObjects_;
55 FreePages_.push(obj);
56 }
57
58 int count() const {
59 return AllocatedObjects_;
60 }
61
62private:
63 void AllocatePool() {
64 T* pool = static_cast<T*>(::operator new(PoolSize * sizeof(T)));
65 Pools_.emplace_back(pool);
66 for (size_t i = 0; i < PoolSize; i++) {
67 FreePages_.push(&pool[i]);
68 }
69 }
70
71 std::vector<T*> Pools_;
72 std::stack<T*> FreePages_;
73 int AllocatedObjects_ = 0;
74};
75
101class TIOCp: public TPollerBase {
102public:
107
108 TIOCp();
109 ~TIOCp();
118 void Read(int fd, void* buf, int size, std::coroutine_handle<> handle);
127 void Write(int fd, const void* buf, int size, std::coroutine_handle<> handle);
136 void Recv(int fd, void* buf, int size, std::coroutine_handle<> handle);
145 void Send(int fd, const void* buf, int size, std::coroutine_handle<> handle);
154 void Accept(int fd, struct sockaddr* addr, socklen_t* len, std::coroutine_handle<> handle);
163 void Connect(int fd, const sockaddr* addr, socklen_t len, std::coroutine_handle<> handle);
169 void Cancel(int fd);
175 void Register(int fd);
181 int Result();
187 void Poll();
188
189private:
190 struct TIO {
191 OVERLAPPED overlapped;
192 THandle handle;
193 struct sockaddr* addr = nullptr; // for accept
194 socklen_t* len = nullptr; // for accept
195 int sock = -1; // for accept
196
197 TIO() {
198 memset(&overlapped, 0, sizeof(overlapped));
199 }
200 };
201
202 long GetTimeoutMs();
203 TIO* NewTIO();
204 void FreeTIO(TIO*);
205
206 HANDLE Port_;
207
208 // Allocator to avoid dynamic memory allocation for each IOCP event structure.
209 TArenaAllocator<TIO> Allocator_;
210 std::vector<OVERLAPPED_ENTRY> Entries_;
211 std::queue<int> Results_;
212};
213
214}
void Register(int fd)
Registers a file descriptor with the IOCP.
void Recv(int fd, void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous receive operation.
void Cancel(int fd)
Cancels all pending operations on the specified file descriptor.
NNet::TPollerDrivenSocket< TIOCp > TSocket
Alias for the poller-driven socket type.
Definition iocp.hpp:104
void Accept(int fd, struct sockaddr *addr, socklen_t *len, std::coroutine_handle<> handle)
Posts an asynchronous accept operation.
NNet::TPollerDrivenFileHandle< TIOCp > TFileHandle
Alias for the poller-driven file handle type.
Definition iocp.hpp:106
void Send(int fd, const void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous send operation.
void Poll()
Polls for IOCP events.
void Read(int fd, void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous read operation.
void Connect(int fd, const sockaddr *addr, socklen_t len, std::coroutine_handle<> handle)
Posts an asynchronous connect operation.
int Result()
Retrieves the result of the last completed IOCP operation.
void Write(int fd, const void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous write operation.
TPollerBase()=default
Default constructor.
Asynchronous file handle driven by the poller's implementation.
Definition socket.hpp:742
Socket type driven by the poller's implementation.
Definition socket.hpp:519