-
Notifications
You must be signed in to change notification settings - Fork 6
/
msh3.hpp
404 lines (382 loc) · 13.1 KB
/
msh3.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*++
Copyright (c) Microsoft Corporation.
Licensed under the MIT License.
--*/
#include "msh3.h"
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
using namespace std;
using namespace std::chrono_literals;
#if MSH3_TEST_MODE
#define TEST_DEF(x) = x
#else
#define TEST_DEF(x)
#endif
struct MsH3Request;
enum MsH3CleanUpMode {
CleanUpManual,
CleanUpAutoDelete,
};
template<typename T>
struct MsH3Waitable {
T Get() const { return State; }
void Set(T state) {
std::lock_guard Lock{Mutex};
State = state;
Event.notify_all();
}
T Wait() {
if (!State) {
std::unique_lock Lock{Mutex};
Event.wait(Lock, [&]{return State;});
}
return State;
}
bool WaitFor(uint32_t milliseconds TEST_DEF(250)) {
if (!State) {
std::unique_lock Lock{Mutex};
return Event.wait_for(Lock, milliseconds*1ms, [&]{return State;});
}
return true;
}
private:
std::mutex Mutex;
std::condition_variable Event;
T State { (T)0 };
};
struct MsH3Api {
MSH3_API* Handle { MsH3ApiOpen() };
~MsH3Api() noexcept { if (Handle) { MsH3ApiClose(Handle); } }
bool IsValid() const noexcept { return Handle != nullptr; }
operator MSH3_API* () const noexcept { return Handle; }
};
struct MsH3Configuration {
MSH3_CONFIGURATION* Handle { nullptr };
MsH3Configuration(MsH3Api& Api) {
Handle = MsH3ConfigurationOpen(Api, nullptr, 0);
}
MsH3Configuration(MsH3Api& Api, const MSH3_SETTINGS* Settings) {
Handle = MsH3ConfigurationOpen(Api, Settings, sizeof(*Settings));
}
~MsH3Configuration() noexcept { if (Handle) { MsH3ConfigurationClose(Handle); } }
bool IsValid() const noexcept { return Handle != nullptr; }
operator MSH3_CONFIGURATION* () const noexcept { return Handle; }
#if MSH3_TEST_MODE
MSH3_STATUS LoadConfiguration() noexcept {
const MSH3_CREDENTIAL_CONFIG Config = { MSH3_CREDENTIAL_TYPE_SELF_SIGNED_CERTIFICATE };
return MsH3ConfigurationLoadCredential(Handle, &Config);
}
#endif
MSH3_STATUS LoadConfiguration(const MSH3_CREDENTIAL_CONFIG& Config) noexcept {
return MsH3ConfigurationLoadCredential(Handle, &Config);
}
};
struct MsH3Addr {
MSH3_ADDR Addr {0};
MsH3Addr(uint16_t Port TEST_DEF(4433)) {
SetPort(Port);
}
operator const MSH3_ADDR* () const noexcept { return &Addr; }
void SetPort(uint16_t Port) noexcept { MSH3_SET_PORT(&Addr, Port); }
};
typedef MSH3_STATUS MsH3ListenerCallback(
struct MsH3Listener* Listener,
void* Context,
MSH3_LISTENER_EVENT* Event
);
struct MsH3Listener {
MSH3_LISTENER* Handle { nullptr };
MsH3CleanUpMode CleanUpMode;
MsH3ListenerCallback* Callback{ nullptr };
void* Context{ nullptr };
MsH3Listener(
MsH3Api& Api,
const MsH3Addr& Address,
MsH3CleanUpMode CleanUpMode,
MsH3ListenerCallback* Callback,
void* Context = nullptr
) noexcept : CleanUpMode(CleanUpMode), Callback(Callback), Context(Context) {
Handle = MsH3ListenerOpen(Api, Address, (MSH3_LISTENER_CALLBACK_HANDLER)MsH3Callback, this);
}
~MsH3Listener() noexcept { if (Handle) { MsH3ListenerClose(Handle); } }
MsH3Listener(MsH3Listener& other) = delete;
MsH3Listener operator=(MsH3Listener& Other) = delete;
bool IsValid() const noexcept { return Handle != nullptr; }
operator MSH3_LISTENER* () const noexcept { return Handle; }
private:
static
MSH3_STATUS
MsH3Callback(
MSH3_LISTENER* /* Listener */,
MsH3Listener* pThis,
MSH3_LISTENER_EVENT* Event
) noexcept {
auto DeleteOnExit =
Event->Type == MSH3_LISTENER_EVENT_SHUTDOWN_COMPLETE &&
pThis->CleanUpMode == CleanUpAutoDelete;
auto Status = pThis->Callback(pThis, pThis->Context, Event);
if (DeleteOnExit) {
delete pThis;
}
return Status;
}
};
typedef MSH3_STATUS MsH3ConnectionCallback(
struct MsH3Connection* Connection,
void* Context,
MSH3_CONNECTION_EVENT* Event
);
struct MsH3Connection {
MSH3_CONNECTION* Handle { nullptr };
MsH3Waitable<bool> Connected;
MsH3Waitable<bool> ShutdownComplete;
MsH3Connection(
MsH3Api& Api,
MsH3CleanUpMode CleanUpMode = CleanUpManual,
MsH3ConnectionCallback* Callback = NoOpCallback,
void* Context = nullptr
) noexcept : CleanUp(CleanUpMode), Callback(Callback), Context(Context) {
Handle = MsH3ConnectionOpen(Api, (MSH3_CONNECTION_CALLBACK_HANDLER)MsH3Callback, this);
}
MsH3Connection(
MSH3_CONNECTION* ServerHandle,
MsH3CleanUpMode CleanUpMode,
MsH3ConnectionCallback* Callback,
void* Context = nullptr
) noexcept : Handle(ServerHandle), CleanUp(CleanUpMode), Callback(Callback), Context(Context) {
MsH3ConnectionSetCallbackHandler(Handle, (MSH3_CONNECTION_CALLBACK_HANDLER)MsH3Callback, this);
}
~MsH3Connection() noexcept { Close(); }
MsH3Connection(MsH3Connection& other) = delete;
MsH3Connection operator=(MsH3Connection& Other) = delete;
bool IsValid() const noexcept { return Handle != nullptr; }
operator MSH3_CONNECTION* () const noexcept { return Handle; }
void Close() noexcept {
#ifdef _WIN32
auto HandleToClose = (MSH3_CONNECTION*)InterlockedExchangePointer((PVOID*)&Handle, NULL);
#else
auto HandleToClose = (MSH3_CONNECTION*)__sync_fetch_and_and(&Handle, 0);
#endif
if (HandleToClose) {
MsH3ConnectionClose(HandleToClose);
}
}
MSH3_STATUS SetConfiguration(const MsH3Configuration& Configuration) noexcept {
return MsH3ConnectionSetConfiguration(Handle, Configuration);
}
MSH3_STATUS Start(
const MsH3Configuration& Configuration,
const char* ServerName TEST_DEF("localhost"),
const MsH3Addr& ServerAddress TEST_DEF(MsH3Addr())
) noexcept {
return MsH3ConnectionStart(Handle, Configuration, ServerName, ServerAddress);
}
void Shutdown(uint64_t ErrorCode = 0) noexcept {
MsH3ConnectionShutdown(Handle, ErrorCode);
}
static
MSH3_STATUS
NoOpCallback(
MsH3Connection* /* Connection */,
void* /* Context */,
MSH3_CONNECTION_EVENT* Event
) noexcept {
if (Event->Type == MSH3_CONNECTION_EVENT_NEW_REQUEST) {
//
// Not great beacuse it doesn't provide an application specific
// error code. If you expect to get streams, you should not no-op
// the callbacks.
//
MsH3RequestClose(Event->NEW_REQUEST.Request);
}
return MSH3_STATUS_SUCCESS;
}
private:
const MsH3CleanUpMode CleanUp;
MsH3ConnectionCallback* Callback;
void* Context;
static
MSH3_STATUS
MsH3Callback(
MSH3_CONNECTION* /* Connection */,
MsH3Connection* pThis,
MSH3_CONNECTION_EVENT* Event
) noexcept {
if (Event->Type == MSH3_CONNECTION_EVENT_CONNECTED) {
pThis->Connected.Set(true);
} else if (Event->Type == MSH3_CONNECTION_EVENT_SHUTDOWN_COMPLETE) {
pThis->ShutdownComplete.Set(true);
}
auto DeleteOnExit =
Event->Type == MSH3_CONNECTION_EVENT_SHUTDOWN_COMPLETE &&
pThis->CleanUp == CleanUpAutoDelete;
auto Status = pThis->Callback(pThis, pThis->Context, Event);
if (DeleteOnExit) {
delete pThis;
}
return Status;
}
};
struct MsH3AutoAcceptListener : public MsH3Listener {
const MsH3Configuration* Configuration;
MsH3ConnectionCallback* ConnectionHandler;
void* ConnectionContext;
MsH3Waitable<MsH3Connection*> NewConnection;
MsH3AutoAcceptListener(
MsH3Api& Api,
const MsH3Addr& Address,
MsH3ConnectionCallback* _ConnectionHandler,
void* _ConnectionContext = nullptr
) noexcept :
MsH3Listener(Api, Address, CleanUpManual, ListenerCallback, this),
Configuration(nullptr),
ConnectionHandler(_ConnectionHandler),
ConnectionContext(_ConnectionContext)
{ }
MsH3AutoAcceptListener(
MsH3Api& Api,
const MsH3Addr& Address,
const MsH3Configuration& Config,
MsH3ConnectionCallback* _ConnectionHandler,
void* _ConnectionContext = nullptr
) noexcept :
MsH3Listener(Api, Address, CleanUpManual, ListenerCallback, this),
Configuration(&Config),
ConnectionHandler(_ConnectionHandler),
ConnectionContext(_ConnectionContext)
{ }
private:
static
MSH3_STATUS
ListenerCallback(
MsH3Listener* /* Listener */,
void* Context,
MSH3_LISTENER_EVENT* Event
) noexcept {
auto pThis = (MsH3AutoAcceptListener*)Context;
#ifdef _WIN32
MSH3_STATUS Status = E_NOT_VALID_STATE;
#else
MSH3_STATUS Status = EINVAL;
#endif
if (Event->Type == MSH3_LISTENER_EVENT_NEW_CONNECTION) {
auto Connection = new(std::nothrow) MsH3Connection(Event->NEW_CONNECTION.Connection, CleanUpAutoDelete, pThis->ConnectionHandler, pThis->ConnectionContext);
if (Connection) {
if (pThis->Configuration &&
MSH3_FAILED(Status = Connection->SetConfiguration(*pThis->Configuration))) {
//
// The connection is being rejected. Let the library free the handle.
//
Connection->Handle = nullptr;
delete Connection;
} else {
Status = MSH3_STATUS_SUCCESS;
pThis->NewConnection.Set(Connection);
}
}
}
return Status;
}
};
typedef MSH3_STATUS MsH3RequestCallback(
struct MsH3Request* Request,
void* Context,
MSH3_REQUEST_EVENT* Event
);
struct MsH3Request {
MSH3_REQUEST* Handle { nullptr };
MsH3CleanUpMode CleanUpMode;
MsH3RequestCallback* Callback;
void* Context;
MsH3Waitable<bool> ShutdownComplete;
bool Aborted {false};
uint64_t AbortError {0};
MsH3Request(
MsH3Connection& Connection,
MSH3_REQUEST_FLAGS Flags = MSH3_REQUEST_FLAG_NONE,
MsH3CleanUpMode CleanUpMode = CleanUpManual,
MsH3RequestCallback* Callback = NoOpCallback,
void* Context = nullptr
) noexcept : CleanUpMode(CleanUpMode), Callback(Callback), Context(Context) {
if (Connection.IsValid()) {
Handle = MsH3RequestOpen(Connection, (MSH3_REQUEST_CALLBACK_HANDLER)MsH3Callback, this, Flags);
}
}
MsH3Request(
MSH3_REQUEST* ServerHandle,
MsH3CleanUpMode CleanUpMode,
MsH3RequestCallback* Callback = NoOpCallback,
void* Context = nullptr
) noexcept : Handle(ServerHandle), CleanUpMode(CleanUpMode), Callback(Callback), Context(Context) {
MsH3RequestSetCallbackHandler(Handle, (MSH3_REQUEST_CALLBACK_HANDLER)MsH3Callback, this);
}
~MsH3Request() noexcept { Close(); }
MsH3Request(MsH3Request& other) = delete;
MsH3Request operator=(MsH3Request& Other) = delete;
bool IsValid() const noexcept { return Handle != nullptr; }
operator MSH3_REQUEST* () const noexcept { return Handle; }
void
Close() noexcept {
#ifdef _WIN32
auto HandleToClose = (MSH3_REQUEST*)InterlockedExchangePointer((PVOID*)&Handle, NULL);
#else
auto HandleToClose = (MSH3_REQUEST*)__sync_fetch_and_and(&Handle, 0);
#endif
if (HandleToClose) {
MsH3RequestClose(HandleToClose);
}
}
void CompleteReceive(uint32_t Length) noexcept {
MsH3RequestCompleteReceive(Handle, Length);
};
void SetReceiveEnabled(bool Enabled) noexcept {
MsH3RequestSetReceiveEnabled(Handle, Enabled);
};
bool Send(
const MSH3_HEADER* Headers,
size_t HeadersCount,
const void* Data = nullptr,
uint32_t DataLength = 0,
MSH3_REQUEST_SEND_FLAGS Flags = MSH3_REQUEST_SEND_FLAG_NONE,
void* SendContext = nullptr
) noexcept {
return MsH3RequestSend(Handle, Flags, Headers, HeadersCount, Data, DataLength, SendContext);
}
void Shutdown(
MSH3_REQUEST_SHUTDOWN_FLAGS Flags,
uint64_t _AbortError = 0
) noexcept {
return MsH3RequestShutdown(Handle, Flags, _AbortError);
}
static
MSH3_STATUS
NoOpCallback(
MsH3Request* /* Request */,
void* /* Context */,
MSH3_REQUEST_EVENT* /* Event */
) noexcept {
return MSH3_STATUS_SUCCESS;
}
private:
static
MSH3_STATUS
MsH3Callback(
MSH3_REQUEST* /* Request */,
MsH3Request* pThis,
MSH3_REQUEST_EVENT* Event
) noexcept {
if (Event->Type == MSH3_REQUEST_EVENT_SHUTDOWN_COMPLETE) {
pThis->ShutdownComplete.Set(true);
}
auto DeleteOnExit =
Event->Type == MSH3_REQUEST_EVENT_SHUTDOWN_COMPLETE &&
pThis->CleanUpMode == CleanUpAutoDelete;
auto Status = pThis->Callback(pThis, pThis->Context, Event);
if (DeleteOnExit) {
delete pThis;
}
return Status;
}
};