Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
TcpTransporter.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
20
21#pragma once
22
25#include "sese/io/ByteBuilder.h"
26
27#include <atomic>
28
29#ifdef _WIN32
30#pragma warning(disable : 4275)
31#endif
32
33namespace sese::service {
34
37 virtual ~TcpConnection() = default;
38
45 std::atomic_bool isAsync = false;
46
47 void *ssl = nullptr;
48 event::BaseEvent *event = nullptr;
52};
53
56 uint32_t keepalive = 30;
58
59 virtual ~TcpTransporterConfig() = default;
61 virtual void freeConnection(TcpConnection *conn);
62};
63
66public:
67 explicit TcpTransporter(TcpTransporterConfig *transporter_config) noexcept;
68 ~TcpTransporter() override;
69
70protected:
71 void onAccept(int fd) override;
72 void onRead(event::BaseEvent *event) override;
73 void onWrite(event::BaseEvent *event) override;
74 void onClose(event::BaseEvent *event) override;
75 void onTimeout(v1::TimeoutEvent *timeout_event) override;
76
77protected:
78 virtual void postRead(TcpConnection *conn);
79 virtual void postWrite(TcpConnection *conn);
80 virtual int onProcAlpnSelect(
81 const uint8_t **out, uint8_t *out_length,
82 const uint8_t *in, uint32_t in_length
83 ) = 0;
84 virtual void onProcAlpnGet(
85 TcpConnection *conn,
86 const uint8_t *in, uint32_t in_length
87 ) = 0;
88 virtual void onProcHandle(TcpConnection *conn) = 0;
89 virtual void onProcClose(TcpConnection *conn) = 0;
90
91protected:
92 event::BaseEvent *createEventEx(int fd, unsigned int events, TcpConnection *conn) noexcept;
93 void freeEventEx(event::BaseEvent *event) noexcept;
94 static int64_t read(int fd, void *buffer, size_t len, void *ssl) noexcept;
95 static int64_t write(int fd, const void *buffer, size_t len, void *ssl) noexcept;
96
98 std::map<int, event::BaseEvent *> eventMap;
99
100private:
101 static int alpnCallbackFunction(
102 void *ssl,
103 const uint8_t **out, uint8_t *out_length,
104 const uint8_t *in, uint32_t in_length,
105 TcpTransporter *transporter
106 );
107};
108
109} // namespace sese::service