Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
IOCPServer_V1.h
浏览该文件的文档.
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
28#include <sese/io/ByteBuilder.h>
29
30#include <set>
31
32namespace sese::iocp::v1 {
33
34class Context;
35class IOCPServer;
36class IOCPService;
37
40public:
41 virtual ~IOCPServer() = default;
42 using DeleteContextCallback = std::function<void(Context *data)>;
43
44 IOCPServer();
45
50 bool init();
54 void shutdown();
59 static void postRead(Context *ctx);
64 static void postWrite(Context *ctx);
69 static void postClose(Context *ctx);
76 void postConnect(const net::IPAddress::Ptr &to, const security::SSLContext::Ptr &cli_ctx, void *data = nullptr);
82 static void setTimeout(Context *ctx, int64_t seconds);
87 static void cancelTimeout(Context *ctx);
91 static void onDeleteContext(Context *) {}
96 virtual void onAcceptCompleted(Context *ctx) {}
101 virtual void onPreRead(Context *ctx) {}
106 virtual void onReadCompleted(Context *ctx) {}
111 virtual void onWriteCompleted(Context *ctx) {}
116 virtual void onTimeout(Context *ctx) {}
121 virtual void onPreConnect(Context *ctx) {}
126 virtual void onConnected(Context *ctx) {}
133 virtual void onAlpnGet(Context *ctx, const uint8_t *in, uint32_t in_length) {}
142 int onAlpnSelect(
143 const uint8_t **out, uint8_t *out_length,
144 const uint8_t *in, uint32_t in_length
145 );
146
147public:
157 void setThreads(size_t threads) { balanceLoader.setThreads(threads); }
167 void setServProtos(const std::string &protos) { IOCPServer::servProtos = protos; }
172 void setClientProtos(const std::string &protos) { IOCPServer::clientProtos = protos; }
178
183 [[nodiscard]] const security::SSLContext::Ptr &getServCtx() const { return IOCPServer::sslCtx; }
189
194 [[nodiscard]] bool isActiveReleaseMode() const { return activeReleaseMode; }
195
196public:
201 [[maybe_unused]] void setAcceptTimeout(uint32_t seconds) { balanceLoader.setAcceptTimeout(seconds); }
206 [[maybe_unused]] void setDispatchTimeout(uint32_t seconds) { balanceLoader.setDispatchTimeout(seconds); }
207
208protected:
209 void preConnectCallback(int fd, sese::event::EventLoop *event_loop, Context *ctx);
210
215 void setActiveReleaseMode(bool enable) { activeReleaseMode = enable; }
216
219 std::string servProtos{};
220 std::string clientProtos{};
222
223private:
224 bool activeReleaseMode = true;
225};
226
228class Context final : public io::InputStream, public io::OutputStream, public io::PeekableStream {
229 friend class IOCPServer;
230 friend class IOCPService;
234 void *ssl{};
235 bool isConn{false};
240 void *data{};
241
242public:
249 int64_t read(void *buffer, size_t length) override;
256 int64_t write(const void *buffer, size_t length) override;
263 int64_t peek(void *buffer, size_t length) override;
269 int64_t trunc(size_t length) override;
274 [[nodiscard]] int32_t getFd() const { return static_cast<int32_t>(Context::fd); }
279 [[nodiscard]] void *getData() const { return Context::data; }
284 [[maybe_unused]] void setData(void *p_data) { Context::data = p_data; }
285};
286
289public:
293 explicit IOCPService(IOCPServer *master, bool active_release_mode);
295 ~IOCPService() override;
296
301 void postRead(Context *ctx);
306 void postWrite(Context *ctx);
311 void postClose(Context *ctx);
316 static void onAcceptCompleted(Context *ctx);
321 static void onPreRead(Context *ctx);
326 static void onReadCompleted(Context *ctx);
331 static void onWriteCompleted(Context *ctx);
336 static void onTimeout(Context *ctx);
341 static void onConnected(Context *ctx);
348 static void onAlpnGet(Context *ctx, const uint8_t *in, uint32_t in_length);
359 static int alpnCallbackFunction(
360 void *ssl,
361 const uint8_t **out, uint8_t *out_length,
362 const uint8_t *in, uint32_t in_length,
363 IOCPService *service
364 );
365
366private:
367 void onAccept(int fd) override;
368 void onRead(event::BaseEvent *event) override;
369 void onWrite(event::BaseEvent *event) override;
370 void onClose(event::BaseEvent *event) override;
371 void onTimeout(service::v2::TimeoutEvent *event) override;
372
373 event::BaseEvent *createEventEx(int fd, unsigned int events, void *data);
375
376 void releaseContext(Context *ctx);
377
378 static int64_t read(int fd, void *buffer, size_t len, void *ssl);
379 static int64_t write(int fd, const void *buffer, size_t len, void *ssl);
380
382 std::set<event::BaseEvent *> eventSet;
383};
384
385} // namespace sese::iocp::v1