Sese Framework
2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
NativeIOCPServer_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/net/Address.h
>
26
#include <
sese/security/SSLContext.h
>
27
#include <
sese/service/iocp/IOBuf.h
>
28
#include <
sese/thread/Thread.h
>
29
#include <
sese/io/ByteBuilder.h
>
30
#include <
sese/util/TimeWheel.h
>
31
32
#include <atomic>
33
#include <set>
34
#include <mutex>
35
36
namespace
sese::_windows::iocp::v1
{
37
38
class
NativeIOCPServer;
39
struct
OverlappedWrapper;
40
42
class
NativeContext
final :
public
io::InputStream
,
public
io::OutputStream
,
public
io::PeekableStream
{
43
friend
class
NativeIOCPServer
;
44
using
IOBuf
=
sese::iocp::IOBuf
;
45
using
Node
= std::unique_ptr<sese::iocp::IOBufNode>;
46
47
enum class
Type
{
48
READ
,
49
WRITE
,
50
CONNECT
,
51
READY
,
52
CLOSE
53
};
54
55
OverlappedWrapper
*
pWrapper
{};
56
WSABUF
wsabufRead
{};
57
WSABUF
wsabufWrite
{};
58
Type
type
{
Type::READ
};
59
SOCKET
fd
{INVALID_SOCKET};
60
NativeIOCPServer
*
self
{};
61
TimeoutEvent
*
timeoutEvent
{};
62
void
*
ssl
{};
63
void
*
bio
{};
64
Node
readNode
;
65
IOBuf
recv
{};
66
io::ByteBuilder
send
{
IOCP_WSABUF_SIZE
,
IOCP_WSABUF_SIZE
};
67
void
*
data
{};
68
69
public
:
74
explicit
NativeContext
(
OverlappedWrapper
*p_wrapper);
75
~NativeContext
()
override
;
82
int64_t
read
(
void
*buffer,
size_t
length)
override
;
89
int64_t
write
(
const
void
*buffer,
size_t
length)
override
;
96
int64_t
peek
(
void
*buffer,
size_t
length)
override
;
102
int64_t
trunc
(
size_t
length)
override
;
107
[[nodiscard]] int32_t
getFd
()
const
{
return
static_cast<
int32_t
>
(
NativeContext::fd
); }
112
[[nodiscard]]
void
*
getData
()
const
{
return
NativeContext::data
; }
117
void
setData
(
void
*p_data) {
NativeContext::data
= p_data; }
118
};
119
121
struct
OverlappedWrapper
final {
122
OVERLAPPED
overlapped
{};
123
NativeContext
ctx
;
124
125
OverlappedWrapper
();
126
};
127
129
class
NativeIOCPServer
{
130
public
:
131
using
Context
=
NativeContext
;
132
using
DeleteContextCallback
= std::function<void(
Context
*data)>;
133
134
virtual
~NativeIOCPServer
() =
default
;
135
140
bool
init
();
144
void
shutdown
();
149
void
postRead
(
Context
*ctx);
154
void
postWrite
(
Context
*ctx);
159
void
postClose
(
Context
*ctx);
166
void
postConnect
(
const
net::IPAddress::Ptr
&to,
const
security::SSLContext::Ptr
&cli_ctx,
void
*data =
nullptr
);
172
void
setTimeout
(
Context
*ctx, int64_t seconds);
177
void
cancelTimeout
(
Context
*ctx);
181
static
void
onDeleteContext
(
Context
*) {}
186
virtual
void
onAcceptCompleted
(
Context
*ctx) {}
191
virtual
void
onPreRead
(
Context
*ctx) {}
196
virtual
void
onReadCompleted
(
Context
*ctx) {}
201
virtual
void
onWriteCompleted
(
Context
*ctx) {}
206
virtual
void
onTimeout
(
Context
*ctx) {}
211
virtual
void
onPreConnect
(
Context
*ctx) {}
216
virtual
void
onConnected
(
Context
*ctx){};
223
virtual
void
onAlpnGet
(
Context
*ctx,
const
uint8_t *
in
, uint32_t in_length){};
232
int
onAlpnSelect
(
233
const
uint8_t **out, uint8_t *out_length,
234
const
uint8_t *
in
, uint32_t in_length
235
);
236
237
public
:
242
void
setAddress
(
const
net::IPAddress::Ptr
&addr) {
NativeIOCPServer::address
= addr; }
247
void
setThreads
(
size_t
number_of_threads) {
NativeIOCPServer::threads
= number_of_threads; }
252
void
setServCtx
(
const
security::SSLContext::Ptr
&ctx) {
NativeIOCPServer::sslCtx
= ctx; }
257
void
setServProtos
(
const
std::string &protos) {
NativeIOCPServer::servProtos
= protos; }
262
void
setClientProtos
(
const
std::string &protos) {
NativeIOCPServer::clientProtos
= protos; }
267
void
setDeleteContextCallback
(
const
DeleteContextCallback
&
callback
) {
NativeIOCPServer::deleteContextCallback
=
callback
; }
272
[[nodiscard]]
const
security::SSLContext::Ptr
&
getServCtx
()
const
{
return
NativeIOCPServer::sslCtx
; }
277
[[nodiscard]]
const
DeleteContextCallback
&
getDeleteContextCallback
()
const
{
return
NativeIOCPServer::deleteContextCallback
; };
282
bool
isActiveReleaseMode
()
const
{
return
NativeIOCPServer::activeReleaseMode
; }
283
284
protected
:
289
void
setActiveReleaseMode
(
bool
enable) {
NativeIOCPServer::activeReleaseMode
= enable; }
294
void
releaseContext
(
Context
*ctx);
295
296
void
acceptThreadProc
();
297
void
eventThreadProc
();
298
static
int
alpnCallbackFunction
(
299
void
*ssl,
300
const
uint8_t **out, uint8_t *out_length,
301
const
uint8_t *
in
, uint32_t in_length,
302
NativeIOCPServer
*server
303
);
304
305
void
*
connectEx
{};
306
bool
initConnectEx
();
307
308
static
long
bioCtrl
(
void
*bio,
int
cmd,
long
num,
void
*ptr);
309
static
int
bioWrite
(
void
*bio,
const
char
*
in
,
int
length);
310
static
int
bioRead
(
void
*bio,
char
*out,
int
length);
311
312
std::atomic_bool
isShutdown
{
false
};
313
HANDLE
iocpFd
{INVALID_HANDLE_VALUE};
314
SOCKET
listenFd
{INVALID_SOCKET};
315
net::IPAddress::Ptr
address
{};
316
Thread::Ptr
acceptThread
{};
317
318
TimeWheel
wheel
{};
319
std::set<OverlappedWrapper *>
wrapperSet
{};
320
std::mutex
wrapperSetMutex
{};
321
322
size_t
threads
{2};
323
std::vector<Thread::Ptr>
eventThreadGroup
{};
324
DeleteContextCallback
deleteContextCallback
=
onDeleteContext
;
325
security::SSLContext::Ptr
sslCtx
{};
326
void
*
bioMethod
{};
327
std::string
servProtos
{};
328
std::string
clientProtos
{};
329
330
private
:
331
bool
activeReleaseMode
=
true
;
332
};
333
334
}
// namespace sese::_windows::iocp::v1
sese
native
win
service
iocp
NativeIOCPServer_V1.h
Generated on Tue Jan 7 2025 15:49:05 for Sese Framework by
1.11.0