/
NetLibrary.h
420 lines (285 loc) · 9.92 KB
/
NetLibrary.h
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* This file is part of the CitizenFX project - http://citizen.re/
*
* See LICENSE and MENTIONS in the root of the source tree for information
* regarding licensing.
*/
#pragma once
#include <queue>
#include <bitset>
#include <functional>
#include <thread>
#include <ppltasks.h>
#include <WS2tcpip.h>
#include "HttpClient.h"
#include "CrossLibraryInterfaces.h"
#include "INetMetricSink.h"
#include "NetLibraryImplBase.h"
#include "ComponentHolder.h"
#include <NetAddress.h>
#include <shared_mutex>
// hacky include path to not conflict with our own NetBuffer.h
#include <../../components/net-base/include/NetBuffer.h>
#ifdef COMPILING_NET
#include <enet/enet.h>
#endif
#include <concurrent_queue.h>
#define NETWORK_PROTOCOL 12
enum NetAddressType
{
NA_NONE,
NA_INET4,
NA_INET6
};
class
#ifdef COMPILING_NET
__declspec(dllexport)
#endif
NetAddress
{
private:
NetAddressType m_type;
union
{
sockaddr_in m_in4;
sockaddr_in6 m_in6;
};
public:
NetAddress() { m_type = NA_NONE; }
NetAddress(const sockaddr* addr);
NetAddress(const sockaddr_in* addr) : NetAddress((const sockaddr*)addr) {}
NetAddress(const sockaddr_in6* addr) : NetAddress((const sockaddr*)addr) {}
#ifdef COMPILING_NET
NetAddress(const ENetAddress* addr);
#endif
NetAddress(const char* address, uint16_t port);
bool operator==(const NetAddress& right) const;
bool operator!=(const NetAddress& right) const;
fwString GetAddress();
fwWString GetWAddress();
int GetPort();
void GetSockAddr(sockaddr_storage* addr, int* addrLen) const;
#ifdef COMPILING_NET
ENetAddress GetENetAddress() const;
#endif
};
#include "NetBuffer.h"
class NetLibrary;
struct NetLibraryClientInfo
{
int netId;
int slotId;
std::string name;
};
class
#ifdef COMPILING_NET
__declspec(dllexport)
#endif
NetLibrary : public INetLibrary, public INetLibraryInherit, public fx::ComponentHolderImpl<NetLibrary>
{
public:
enum ConnectionState
{
CS_IDLE,
CS_INITING,
CS_INITRECEIVED,
CS_DOWNLOADING,
CS_DOWNLOADCOMPLETE,
CS_FETCHING,
CS_CONNECTING,
CS_CONNECTED,
CS_ACTIVE
};
private:
inline std::shared_ptr<NetLibraryImplBase> GetImpl()
{
std::shared_lock _(m_implMutex);
return m_impl;
}
private:
std::shared_ptr<NetLibraryImplBase> m_impl;
std::shared_mutex m_implMutex;
uint16_t m_serverNetID;
uint16_t m_serverSlotID;
uint16_t m_hostNetID;
uint32_t m_serverBase;
uint32_t m_hostBase;
uint64_t m_serverTime;
volatile ConnectionState m_connectionState;
ConnectionState m_lastConnectionState;
NetAddress m_currentServer;
net::PeerAddress m_currentServerPeer;
std::string m_currentServerUrl;
std::string m_token;
uint32_t m_lastConnect;
uint32_t m_connectAttempts;
uint32_t m_lastReconnect;
uint32_t m_reconnectAttempts;
HttpClient* m_httpClient;
HttpRequestPtr m_handshakeRequest;
uint32_t m_outSequence;
uint32_t m_serverProtocol;
std::string m_playerName;
fwRefContainer<INetMetricSink> m_metricSink;
std::string m_infoString;
std::string m_targetContext;
std::string m_richError;
HANDLE m_receiveEvent;
bool m_disconnecting = false;
concurrency::concurrent_queue<std::function<void()>> m_mainFrameQueue;
std::function<void(const std::string&, const std::string&)> m_cardResponseHandler;
private:
typedef std::function<void(const char* buf, size_t len)> ReliableHandlerType;
std::unordered_multimap<uint32_t, std::tuple<ReliableHandlerType, bool>> m_reliableHandlers;
private:
std::mutex m_incomingPacketMutex;
std::queue<RoutingPacket> m_incomingPackets;
concurrency::concurrent_queue<RoutingPacket> m_outgoingPackets;
private:
void HandleReliableCommand(uint32_t msgType, const char* buf, size_t length) override;
NetLibrary();
public:
inline bool AreDownloadsComplete()
{
return m_connectionState >= CS_DOWNLOADCOMPLETE;
}
virtual void ProcessOOB(const NetAddress& from, const char* oob, size_t length) override;
virtual uint16_t GetServerNetID() override;
virtual uint16_t GetHostNetID() override;
virtual uint16_t GetServerSlotID() override;
virtual uint32_t GetHostBase() override;
virtual const char* GetPlayerName() override;
virtual void SetPlayerName(const char* name) override;
virtual void SetBase(uint32_t base) override;
virtual void RunFrame() override;
virtual concurrency::task<void> ConnectToServer(const std::string& rootUrl);
virtual void Disconnect(const char* reason = "[not set]") override;
virtual bool DequeueRoutedPacket(char* buffer, size_t* length, uint16_t* netID) override;
virtual void RoutePacket(const char* buffer, size_t length, uint16_t netID) override;
virtual void SendReliableCommand(const char* type, const char* buffer, size_t length) override;
void SendUnreliableCommand(const char* type, const char* buffer, size_t length);
void RunMainFrame();
void HandleConnected(int serverNetID, int hostNetID, int hostBase, int slotID, uint64_t serverTime) override;
bool GetOutgoingPacket(RoutingPacket& packet) override;
bool WaitForRoutedPacket(uint32_t timeout);
void EnqueueRoutedPacket(uint16_t netID, const std::string& packet) override;
void SendOutOfBand(const NetAddress& address, const char* format, ...) override;
void SendData(const NetAddress& address, const char* data, size_t length);
void CreateResources();
void SetHost(uint16_t netID, uint32_t base);
void DownloadsComplete();
bool IsPendingInGameReconnect();
// waits for connection during the pre-game loading sequence
bool ProcessPreGameTick();
void AddReliableHandler(const char* type, const ReliableHandlerType& function, bool runOnMainThreadOnly = false);
void Death();
void Resurrection();
void CancelDeferredConnection();
void SubmitCardResponse(const std::string& dataJson, const std::string& token);
uint64_t GetGUID();
void SendNetEvent(const std::string& eventName, const std::string& argsSerialized, int target);
void SetRichError(const std::string& data = "{}");
inline uint32_t GetServerBase() { return m_serverBase; }
inline bool IsDisconnected() { return m_connectionState == CS_IDLE; }
inline INetMetricSink* GetMetricSink() override
{
return m_metricSink.GetRef();
}
inline virtual NetAddress GetCurrentServer() override
{
return m_currentServer;
}
inline virtual net::PeerAddress GetCurrentPeer()
{
return m_currentServerPeer;
}
inline virtual const std::string& GetCurrentServerUrl() override
{
return m_currentServerUrl;
}
inline int GetServerProtocol() override
{
return m_serverProtocol;
}
inline int GetConnectionState() override
{
return m_connectionState;
}
inline void SetConnectionState(int state) override
{
m_connectionState = (ConnectionState)state;
}
inline uint64_t GetServerInitTime()
{
return m_serverTime;
}
inline const std::string& GetTargetContext()
{
return m_targetContext;
}
int32_t GetPing();
int32_t GetVariance();
void SetMetricSink(fwRefContainer<INetMetricSink>& sink);
virtual void AddReceiveTick() override;
virtual void AddSendTick() override;
public:
static NetLibrary* Create();
public:
static
#ifndef COMPILING_NET
__declspec(dllimport)
#endif
fwEvent<NetLibrary*> OnNetLibraryCreate;
fwEvent<int /* build */, int /* pure level */> OnRequestBuildSwitch;
fwEvent<const char*> OnAttemptDisconnect;
fwEvent<NetAddress> OnInitReceived;
fwEvent<NetAddress> OnConnectOKReceived;
fwEvent<NetAddress> OnFinalizeDisconnect;
fwEvent<const char*> OnConnectionErrorEvent;
fwEvent<const std::string&, const std::string&> OnConnectionErrorRichEvent;
virtual void OnConnectionError(const std::string& errorString, const std::string& metaData = "{}");
// a1: adaptive card JSON
// a2: connection token
fwEvent<const std::string&, const std::string&> OnConnectionCardPresent;
// a1: status message
// a2: current progress
// a3: total progress
// a4: cancelable/closable UI
fwEvent<const std::string&, int, int, bool> OnConnectionProgress;
// a1: detailed progress message
fwEvent<const std::string&> OnConnectionSubProgress;
// a1: message to spam the player with
fwEvent<const std::string&> OnReconnectProgress;
// event to intercept connection requests
// return false to intercept connection (and call the callback to continue)
// this won't like more than one interception attempt, however
// a1: connection address
// a2: continuation callback
fwEvent<const std::string&, const std::function<void()>&> OnInterceptConnection;
// same as the other routine, except it's for authentication
// a2 -> license key token
fwEvent<const std::string&, const std::string&, const std::function<void(bool success, const std::map<std::string, std::string>& additionalPostData)>&> OnInterceptConnectionForAuth;
// event to intercept server events for debugging
// a1: event name
// a2: event payload
fwEvent<const std::string&, const std::string&> OnTriggerServerEvent;
static
#ifndef COMPILING_NET
__declspec(dllimport)
#endif
fwEvent<const std::function<void(uint32_t, const char*, int)>&> OnBuildMessage;
fwEvent<> OnConnectionTimedOut;
// a1: new connection state
// a2: previous connection state
fwEvent<ConnectionState, ConnectionState> OnStateChanged;
// a1: the info.json data, raw
// a2: callback when completed
// #TODO: fxSingleEvent maybe?
fwEvent<std::string_view, const std::function<void()>&> OnInfoBlobReceived;
// for use from high-level code calling down to lower-level code
fwEvent<const NetLibraryClientInfo&> OnClientInfoReceived;
fwEvent<const NetLibraryClientInfo&> OnClientInfoDropped;
};
DECLARE_INSTANCE_TYPE(NetLibrary);
extern DLL_IMPORT fwEvent<const std::string&> OnRichPresenceSetTemplate;
extern DLL_IMPORT fwEvent<int, const std::string&> OnRichPresenceSetValue;