forked from mas-bandwidth/yojimbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyojimbo_client.h
350 lines (217 loc) · 12 KB
/
yojimbo_client.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
/*
Yojimbo Network Library.
Copyright © 2016, The Network Protocol Company, Inc.
*/
#ifndef YOJIMBO_CLIENT_H
#define YOJIMBO_CLIENT_H
#include "yojimbo_config.h"
#include "yojimbo_adapter.h"
#include "yojimbo_address.h"
#include "yojimbo_allocator.h"
struct netcode_client_t;
struct reliable_endpoint_t;
/** @file */
namespace yojimbo
{
class Connection;
class NetworkSimulator;
/**
The set of client states.
*/
enum ClientState
{
CLIENT_STATE_ERROR = -1,
CLIENT_STATE_DISCONNECTED = 0,
CLIENT_STATE_CONNECTING,
CLIENT_STATE_CONNECTED,
};
/**
Client interface.
*/
class ClientInterface
{
public:
virtual ~ClientInterface() {}
/**
Set the context for reading and writing packets.
This is optional. It lets you pass in a pointer to some structure that you want to have available when reading and writing packets via Stream::GetContext.
Typical use case is to pass in an array of min/max ranges for values determined by some data that is loaded from a toolchain vs. being known at compile time.
If you do use a context, make sure the same context data is set on client and server, and include a checksum of the context data in the protocol id.
*/
virtual void SetContext( void * context ) = 0;
/**
Disconnect from the server.
*/
virtual void Disconnect() = 0;
/**
Send packets to server.
*/
virtual void SendPackets() = 0;
/**
Receive packets from the server.
*/
virtual void ReceivePackets() = 0;
/**
Advance client time.
Call this at the end of each frame to advance the client time forward.
IMPORTANT: Please use a double for your time value so it maintains sufficient accuracy as time increases.
*/
virtual void AdvanceTime( double time ) = 0;
/**
Is the client connecting to a server?
This is true while the client is negotiation connection with a server.
@returns true if the client is currently connecting to, but is not yet connected to a server.
*/
virtual bool IsConnecting() const = 0;
/**
Is the client connected to a server?
This is true once a client successfully finishes connection negotiatio, and connects to a server. It is false while connecting to a server.
@returns true if the client is connected to a server.
*/
virtual bool IsConnected() const = 0;
/**
Is the client in a disconnected state?
A disconnected state corresponds to the client being in the disconnected, or in an error state. Both are logically "disconnected".
@returns true if the client is disconnected.
*/
virtual bool IsDisconnected() const = 0;
/**
Is the client in an error state?
When the client disconnects because of an error, it enters into this error state.
@returns true if the client is in an error state.
*/
virtual bool ConnectionFailed() const = 0;
/**
Get the current client state.
*/
virtual ClientState GetClientState() const = 0;
/**
Get the client index.
The client index is the slot number that the client is occupying on the server.
@returns The client index in [0,maxClients-1], where maxClients is the number of client slots allocated on the server in Server::Start.
*/
virtual int GetClientIndex() const = 0;
/**
Get the current client time.
@see Client::AdvanceTime
*/
virtual double GetTime() const = 0;
// todo: document these methods
virtual Message * CreateMessage( int type ) = 0;
virtual uint8_t * AllocateBlock( int bytes ) = 0;
virtual void AttachBlockToMessage( Message * message, uint8_t * block, int bytes ) = 0;
virtual void FreeBlock( uint8_t * block ) = 0;
virtual bool CanSendMessage( int channelIndex ) const = 0;
virtual void SendMessage( int channelIndex, Message * message ) = 0;
virtual Message * ReceiveMessage( int channelIndex ) = 0;
virtual void ReleaseMessage( Message * message ) = 0;
};
/**
Functionality shared across all client implementations.
*/
class BaseClient : public ClientInterface
{
public:
/**
Base client constructor.
@param allocator The allocator for all memory used by the client.
@param config The base client/server configuration.
@param time The current time in seconds. See ClientInterface::AdvanceTime
@param allocator The adapter to the game program. Specifies allocators, message factory to use etc.
*/
explicit BaseClient( Allocator & allocator, const BaseClientServerConfig & config, Adapter & adapter, double time );
~BaseClient();
void SetContext( void * context ) { yojimbo_assert( IsDisconnected() ); m_context = context; }
void Disconnect();
void AdvanceTime( double time );
bool IsConnecting() const { return m_clientState == CLIENT_STATE_CONNECTING; }
bool IsConnected() const { return m_clientState == CLIENT_STATE_CONNECTED; }
bool IsDisconnected() const { return m_clientState <= CLIENT_STATE_DISCONNECTED; }
bool ConnectionFailed() const { return m_clientState == CLIENT_STATE_ERROR; }
ClientState GetClientState() const { return m_clientState; }
int GetClientIndex() const { return m_clientIndex; }
double GetTime() const { return m_time; }
void SetLatency( float milliseconds );
void SetJitter( float milliseconds );
void SetPacketLoss( float percent );
void SetDuplicates( float percent );
Message * CreateMessage( int type );
uint8_t * AllocateBlock( int bytes );
void AttachBlockToMessage( Message * message, uint8_t * block, int bytes );
void FreeBlock( uint8_t * block );
bool CanSendMessage( int channelIndex ) const;
void SendMessage( int channelIndex, Message * message );
Message * ReceiveMessage( int channelIndex );
void ReleaseMessage( Message * message );
protected:
void * GetContext() { return m_context; }
void CreateInternal();
void DestroyInternal();
void SetClientState( ClientState clientState );
Allocator & GetClientAllocator() { yojimbo_assert( m_clientAllocator ); return *m_clientAllocator; }
MessageFactory & GetMessageFactory() { yojimbo_assert( m_messageFactory ); return *m_messageFactory; }
NetworkSimulator * GetNetworkSimulator() { return m_networkSimulator; }
reliable_endpoint_t * GetEndpoint() { return m_endpoint; }
Connection & GetConnection() { yojimbo_assert( m_connection ); return *m_connection; }
virtual void TransmitPacketFunction( uint16_t packetSequence, uint8_t * packetData, int packetBytes ) = 0;
virtual int ProcessPacketFunction( uint16_t packetSequence, uint8_t * packetData, int packetBytes ) = 0;
static void StaticTransmitPacketFunction( void * context, int index, uint16_t packetSequence, uint8_t * packetData, int packetBytes );
static int StaticProcessPacketFunction( void * context, int index, uint16_t packetSequence, uint8_t * packetData, int packetBytes );
static void * StaticAllocateFunction( void * context, uint64_t bytes );
static void StaticFreeFunction( void * context, void * pointer );
private:
BaseClientServerConfig m_config; ///< The base client/server configuration.
Allocator * m_allocator; ///< The allocator passed to the client on creation.
Adapter * m_adapter; ///< The adapter specifies the allocator to use, and the message factory class.
void * m_context; ///< Context lets the user pass information to packet serialize functions.
uint8_t * m_clientMemory; ///< The memory backing the client allocator. Allocated from m_allocator.
Allocator * m_clientAllocator; ///< The client allocator. Everything allocated between connect and disconnected is allocated and freed via this allocator.
reliable_endpoint_t * m_endpoint; ///< reliable.io endpoint.
MessageFactory * m_messageFactory; ///< The client message factory. Created and destroyed on each connection attempt.
Connection * m_connection; ///< The client connection for exchanging messages with the server.
NetworkSimulator * m_networkSimulator; ///< The network simulator used to simulate packet loss, latency, jitter etc. Optional.
ClientState m_clientState; ///< The current client state. See ClientInterface::GetClientState
int m_clientIndex; ///< The client slot index on the server [0,maxClients-1]. -1 if not connected.
double m_time; ///< The current client time. See ClientInterface::AdvanceTime
private:
BaseClient( const BaseClient & other );
const BaseClient & operator = ( const BaseClient & other );
};
/**
Client implementation.
*/
class Client : public BaseClient
{
public:
/**
The client constructor.
@param allocator The allocator for all memory used by the client.
@param address The address the client should bind to.
@param config The client/server configuration.
@param time The current time in seconds. See ClientInterface::AdvanceTime
*/
explicit Client( Allocator & allocator, const Address & address, const ClientServerConfig & config, Adapter & adapter, double time );
~Client();
void InsecureConnect( const uint8_t privateKey[], uint64_t clientId, const Address & address );
void InsecureConnect( const uint8_t privateKey[], uint64_t clientId, const Address serverAddresses[], int numServerAddresses );
void Connect( uint64_t clientId, uint8_t * connectToken );
void Disconnect();
void SendPackets();
void ReceivePackets();
void AdvanceTime( double time );
int GetClientIndex() const;
private:
bool GenerateInsecureConnectToken( uint8_t * connectToken, const uint8_t privateKey[], uint64_t clientId, const Address serverAddresses[], int numServerAddresses, int timeout = 45 );
void CreateClient( const Address & address );
void DestroyClient();
void StateChangeCallbackFunction( int previous, int current );
static void StaticStateChangeCallbackFunction( void * context, int previous, int current );
void TransmitPacketFunction( uint16_t packetSequence, uint8_t * packetData, int packetBytes );
int ProcessPacketFunction( uint16_t packetSequence, uint8_t * packetData, int packetBytes );
ClientServerConfig m_config; ///< Client/server configuration.
netcode_client_t * m_client; ///< netcode.io client data.
Address m_address; ///< The client address.
uint64_t m_clientId; ///< The globally unique client id (set on each call to connect)
};
}
#endif // #ifndef YOJIMBO_CLIENT_H