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
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
; }
182
[[nodiscard]]
const
security::SSLContext::Ptr
&
getServCtx
()
const
{
return
IOCPServer::sslCtx
; }
187
[[nodiscard]]
const
DeleteContextCallback
&
getDeleteContextCallback
()
const
{
return
IOCPServer::deleteContextCallback
; };
188
193
[[nodiscard]]
bool
isActiveReleaseMode
()
const
{
return
activeReleaseMode
; }
194
195
public
:
200
[[maybe_unused]]
void
setAcceptTimeout
(uint32_t seconds) {
balanceLoader
.
setAcceptTimeout
(seconds); }
205
[[maybe_unused]]
void
setDispatchTimeout
(uint32_t seconds) {
balanceLoader
.
setDispatchTimeout
(seconds); }
206
207
protected
:
208
void
preConnectCallback
(
int
fd, sese::event::EventLoop *event_loop,
Context
*ctx);
209
214
void
setActiveReleaseMode
(
bool
enable) {
activeReleaseMode
= enable; }
215
216
DeleteContextCallback
deleteContextCallback
=
onDeleteContext
;
217
security::SSLContext::Ptr
sslCtx
{};
218
std::string
servProtos
{};
219
std::string
clientProtos
{};
220
service::UserBalanceLoader
balanceLoader
;
221
222
private
:
223
bool
activeReleaseMode
=
true
;
224
};
225
227
class
Context
final :
public
io::InputStream
,
public
io::OutputStream
,
public
io::PeekableStream
{
228
friend
class
IOCPServer
;
229
friend
class
IOCPService
;
230
IOCPServer
*
self
{};
231
IOCPService
*
client
{};
232
socket_t
fd
{0};
233
void
*
ssl
{};
234
bool
isConn
{
false
};
235
event::BaseEvent
*
event
{};
236
service::v2::TimeoutEvent
*
timeoutEvent
{};
237
io::ByteBuilder
send
{8192, 8192};
238
io::ByteBuilder
recv
{8192, 8192};
239
void
*
data
{};
240
241
public
:
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
287
class
IOCPService
final :
public
service::v2::TimerableService
{
288
public
:
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
364
private
:
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);
372
void
freeEventEx
(
sese::event::BaseEvent
*event);
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
379
IOCPServer
*
master
{};
380
std::set<event::BaseEvent *>
eventSet
;
381
};
382
383
}
// namespace sese::iocp::v1
sese
service
iocp
IOCPServer_V1.h
Generated on Tue Jan 7 2025 15:49:06 for Sese Framework by
1.11.0