Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
IOCPServer_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
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; }
182 [[nodiscard]] const security::SSLContext::Ptr &getServCtx() const { return IOCPServer::sslCtx; }
188
193 [[nodiscard]] bool isActiveReleaseMode() const { return activeReleaseMode; }
194
195public:
200 [[maybe_unused]] void setAcceptTimeout(uint32_t seconds) { balanceLoader.setAcceptTimeout(seconds); }
205 [[maybe_unused]] void setDispatchTimeout(uint32_t seconds) { balanceLoader.setDispatchTimeout(seconds); }
206
207protected:
208 void preConnectCallback(int fd, sese::event::EventLoop *event_loop, Context *ctx);
209
214 void setActiveReleaseMode(bool enable) { activeReleaseMode = enable; }
215
218 std::string servProtos{};
219 std::string clientProtos{};
221
222private:
223 bool activeReleaseMode = true;
224};
225
227class Context final : public io::InputStream, public io::OutputStream, public io::PeekableStream {
228 friend class IOCPServer;
229 friend class IOCPService;
233 void *ssl{};
234 bool isConn{false};
239 void *data{};
240
241public:
248 int64_t read(void *buffer, size_t length) override;
255 int64_t write(const void *buffer, size_t length) override;
262 int64_t peek(void *buffer, size_t length) override;
268 int64_t trunc(size_t length) override;
273 [[nodiscard]] int32_t getFd() const { return static_cast<int32_t>(Context::fd); }
278 [[nodiscard]] void *getData() const { return Context::data; }
283 [[maybe_unused]] void setData(void *p_data) { Context::data = p_data; }
284};
285
288public:
292 explicit IOCPService(IOCPServer *master, bool active_release_mode);
293 ~IOCPService() override;
294
299 void postRead(Context *ctx);
304 void postWrite(Context *ctx);
309 void postClose(Context *ctx);
314 static void onAcceptCompleted(Context *ctx);
319 static void onPreRead(Context *ctx);
324 static void onReadCompleted(Context *ctx);
329 static void onWriteCompleted(Context *ctx);
334 static void onTimeout(Context *ctx);
339 static void onConnected(Context *ctx);
346 static void onAlpnGet(Context *ctx, const uint8_t *in, uint32_t in_length);
357 static int alpnCallbackFunction(
358 void *ssl,
359 const uint8_t **out, uint8_t *out_length,
360 const uint8_t *in, uint32_t in_length,
361 IOCPService *service
362 );
363
364private:
365 void onAccept(int fd) override;
366 void onRead(event::BaseEvent *event) override;
367 void onWrite(event::BaseEvent *event) override;
368 void onClose(event::BaseEvent *event) override;
369 void onTimeout(service::v2::TimeoutEvent *event) override;
370
371 event::BaseEvent *createEventEx(int fd, unsigned int events, void *data);
373
374 void releaseContext(Context *ctx);
375
376 static int64_t read(int fd, void *buffer, size_t len, void *ssl);
377 static int64_t write(int fd, const void *buffer, size_t len, void *ssl);
378
380 std::set<event::BaseEvent *> eventSet;
381};
382
383} // namespace sese::iocp::v1