Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
NativeIOCPServer_V1.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
23#pragma once
24
25#include <sese/net/Address.h>
28#include <sese/thread/Thread.h>
29#include <sese/io/ByteBuilder.h>
30#include <sese/util/TimeWheel.h>
31
32#include <atomic>
33#include <set>
34#include <mutex>
35
37
38class NativeIOCPServer;
39struct OverlappedWrapper;
40
43 friend class NativeIOCPServer;
45 using Node = std::unique_ptr<sese::iocp::IOBufNode>;
46
47 enum class Type {
48 READ,
49 WRITE,
50 CONNECT,
51 READY,
52 CLOSE
53 };
54
56 WSABUF wsabufRead{};
57 WSABUF wsabufWrite{};
59 SOCKET fd{INVALID_SOCKET};
62 void *ssl{};
63 void *bio{};
67 void *data{};
68
69public:
74 explicit NativeContext(OverlappedWrapper *p_wrapper);
75 ~NativeContext() override;
82 int64_t read(void *buffer, size_t length) override;
89 int64_t write(const void *buffer, size_t length) override;
96 int64_t peek(void *buffer, size_t length) override;
102 int64_t trunc(size_t length) override;
107 [[nodiscard]] int32_t getFd() const { return static_cast<int32_t>(NativeContext::fd); }
112 [[nodiscard]] void *getData() const { return NativeContext::data; }
117 void setData(void *p_data) { NativeContext::data = p_data; }
118};
119
121struct OverlappedWrapper final {
122 OVERLAPPED overlapped{};
124
126};
127
130public:
132 using DeleteContextCallback = std::function<void(Context *data)>;
133
134 virtual ~NativeIOCPServer() = default;
135
140 bool init();
144 void shutdown();
149 void postRead(Context *ctx);
154 void postWrite(Context *ctx);
159 void postClose(Context *ctx);
166 void postConnect(const net::IPAddress::Ptr &to, const security::SSLContext::Ptr &cli_ctx, void *data = nullptr);
172 void setTimeout(Context *ctx, int64_t seconds);
177 void cancelTimeout(Context *ctx);
181 static void onDeleteContext(Context *) {}
186 virtual void onAcceptCompleted(Context *ctx) {}
191 virtual void onPreRead(Context *ctx) {}
196 virtual void onReadCompleted(Context *ctx) {}
201 virtual void onWriteCompleted(Context *ctx) {}
206 virtual void onTimeout(Context *ctx) {}
211 virtual void onPreConnect(Context *ctx) {}
216 virtual void onConnected(Context *ctx){};
223 virtual void onAlpnGet(Context *ctx, const uint8_t *in, uint32_t in_length){};
232 int onAlpnSelect(
233 const uint8_t **out, uint8_t *out_length,
234 const uint8_t *in, uint32_t in_length
235 );
236
237public:
247 void setThreads(size_t number_of_threads) { NativeIOCPServer::threads = number_of_threads; }
257 void setServProtos(const std::string &protos) { NativeIOCPServer::servProtos = protos; }
262 void setClientProtos(const std::string &protos) { NativeIOCPServer::clientProtos = protos; }
272 [[nodiscard]] const security::SSLContext::Ptr &getServCtx() const { return NativeIOCPServer::sslCtx; }
283
284protected:
294 void releaseContext(Context *ctx);
295
296 void acceptThreadProc();
297 void eventThreadProc();
298 static int alpnCallbackFunction(
299 void *ssl,
300 const uint8_t **out, uint8_t *out_length,
301 const uint8_t *in, uint32_t in_length,
302 NativeIOCPServer *server
303 );
304
305 void *connectEx{};
306 bool initConnectEx();
307
308 static long bioCtrl(void *bio, int cmd, long num, void *ptr);
309 static int bioWrite(void *bio, const char *in, int length);
310 static int bioRead(void *bio, char *out, int length);
311
312 std::atomic_bool isShutdown{false};
313 HANDLE iocpFd{INVALID_HANDLE_VALUE};
314 SOCKET listenFd{INVALID_SOCKET};
317
319 std::set<OverlappedWrapper *> wrapperSet{};
320 std::mutex wrapperSetMutex{};
321
322 size_t threads{2};
323 std::vector<Thread::Ptr> eventThreadGroup{};
326 void *bioMethod{};
327 std::string servProtos{};
328 std::string clientProtos{};
329
330private:
331 bool activeReleaseMode = true;
332};
333
334} // namespace sese::_windows::iocp::v1