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
25
#include <
sese/security/SSLContext.h
>
26
#include <
sese/service/TimerableService_V2.h
>
27
#include <
sese/service/UserBalanceLoader.h
>
28
#include <
sese/io/ByteBuilder.h
>
29
30
#include <set>
31
32
namespace
sese::iocp::v1
{
33
34
class
Context
;
35
class
IOCPServer
;
36
class
IOCPService
;
37
39
class
IOCPServer
{
40
public
:
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
147
public
:
152
void
setAddress
(
const
net::IPAddress::Ptr
&addr) {
balanceLoader
.
setAddress
(addr); }
157
void
setThreads
(
size_t
threads) {
balanceLoader
.
setThreads
(threads); }
162
void
setServCtx
(
const
security::SSLContext::Ptr
&ctx) {
IOCPServer::sslCtx
= ctx; }
167
void
setServProtos
(
const
std::string &protos) {
IOCPServer::servProtos
= protos; }
172
void
setClientProtos
(
const
std::string &protos) {
IOCPServer::clientProtos
= protos; }
177
void
setDeleteContextCallback
(
const
DeleteContextCallback
&
callback
) {
IOCPServer::deleteContextCallback
=
callback
; }
178
183
[[nodiscard]]
const
security::SSLContext::Ptr
&
getServCtx
()
const
{
return
IOCPServer::sslCtx
; }
188
[[nodiscard]]
const
DeleteContextCallback
&
getDeleteContextCallback
()
const
{
return
IOCPServer::deleteContextCallback
; };
189
194
[[nodiscard]]
bool
isActiveReleaseMode
()
const
{
return
activeReleaseMode
; }
195
196
public
:
201
[[maybe_unused]]
void
setAcceptTimeout
(uint32_t seconds) {
balanceLoader
.
setAcceptTimeout
(seconds); }
206
[[maybe_unused]]
void
setDispatchTimeout
(uint32_t seconds) {
balanceLoader
.
setDispatchTimeout
(seconds); }
207
208
protected
:
209
void
preConnectCallback
(
int
fd, sese::event::EventLoop *event_loop,
Context
*ctx);
210
215
void
setActiveReleaseMode
(
bool
enable) {
activeReleaseMode
= enable; }
216
217
DeleteContextCallback
deleteContextCallback
=
onDeleteContext
;
218
security::SSLContext::Ptr
sslCtx
{};
219
std::string
servProtos
{};
220
std::string
clientProtos
{};
221
service::UserBalanceLoader
balanceLoader
;
222
223
private
:
224
bool
activeReleaseMode
=
true
;
225
};
226
228
class
Context
final :
public
io::InputStream
,
public
io::OutputStream
,
public
io::PeekableStream
{
229
friend
class
IOCPServer
;
230
friend
class
IOCPService
;
231
IOCPServer
*
self
{};
232
IOCPService
*
client
{};
233
socket_t
fd
{0};
234
void
*
ssl
{};
235
bool
isConn
{
false
};
236
event::BaseEvent
*
event
{};
237
service::v2::TimeoutEvent
*
timeoutEvent
{};
238
io::ByteBuilder
send
{8192, 8192};
239
io::ByteBuilder
recv
{8192, 8192};
240
void
*
data
{};
241
242
public
:
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
288
class
IOCPService
final :
public
service::v2::TimerableService
{
289
public
:
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
366
private
:
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);
374
void
freeEventEx
(
sese::event::BaseEvent
*event);
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
381
IOCPServer
*
master
{};
382
std::set<event::BaseEvent *>
eventSet
;
383
};
384
385
}
// namespace sese::iocp::v1
sese
service
iocp
IOCPServer_V1.h
生成于 2024年 十一月 4日 星期一 09:58:02 , 为 Sese Framework使用
1.11.0