forked from mas-bandwidth/cubes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackets.h
332 lines (296 loc) · 9.76 KB
/
packets.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
// Copyright © 2015, The Network Protocol Company, Inc. All Rights Reserved.
#ifndef PACKETS_H
#define PACKETS_H
#include "protocol.h"
#include "game.h"
#include "vectorial/vec3f.h"
#include "vectorial/quat4f.h"
using namespace vectorial;
enum PacketType
{
PACKET_TYPE_CONNECTION_REQUEST,
PACKET_TYPE_CONNECTION_ACCEPTED,
PACKET_TYPE_CONNECTION_DENIED,
PACKET_TYPE_INPUT,
PACKET_TYPE_SNAPSHOT,
NUM_PACKET_TYPES
};
struct Packet
{
uint32_t type;
};
struct ConnectionRequestPacket : public Packet
{
uint64_t client_guid;
uint16_t connect_sequence;
SERIALIZE_OBJECT( stream )
{
serialize_uint64( stream, client_guid );
serialize_uint16( stream, connect_sequence );
}
};
struct ConnectionAcceptedPacket : public Packet
{
uint64_t client_guid;
uint16_t connect_sequence;
SERIALIZE_OBJECT( stream )
{
serialize_uint64( stream, client_guid );
serialize_uint16( stream, connect_sequence );
}
};
struct ConnectionDeniedPacket : public Packet
{
uint64_t client_guid;
uint16_t connect_sequence;
SERIALIZE_OBJECT( stream )
{
serialize_uint64( stream, client_guid );
}
};
struct InputPacket : public Packet
{
bool synchronizing = false;
bool bracketed = false;
uint16_t sync_offset = 0;
uint16_t sync_sequence = 0;
uint16_t adjustment_sequence = 0;
uint64_t tick = 0;
int num_inputs = 0;
Input input[MaxInputsPerPacket];
SERIALIZE_OBJECT( stream )
{
serialize_bool( stream, synchronizing );
if ( synchronizing )
{
serialize_uint16( stream, sync_offset );
serialize_uint16( stream, sync_sequence );
serialize_uint64( stream, tick );
}
else
{
serialize_uint64( stream, tick );
serialize_bool( stream, bracketed );
serialize_uint16( stream, adjustment_sequence );
serialize_int( stream, num_inputs, 0, MaxInputsPerPacket );
for ( int i = 0; i < num_inputs; ++i )
{
serialize_bool( stream, input[i].left );
serialize_bool( stream, input[i].right );
serialize_bool( stream, input[i].up );
serialize_bool( stream, input[i].down );
serialize_bool( stream, input[i].push );
serialize_bool( stream, input[i].pull );
/*
if ( i > 0 )
{
bool different = Stream::IsWriting ? ( input[i] != input[i-i] ) : false;
serialize_bool( stream, different );
if ( different )
{
serialize_bool( stream, input[i].left );
serialize_bool( stream, input[i].right );
serialize_bool( stream, input[i].up );
serialize_bool( stream, input[i].down );
serialize_bool( stream, input[i].push );
serialize_bool( stream, input[i].pull );
}
else if ( Stream::IsReading )
{
input[i] = input[i-1];
}
}
else
{
serialize_bool( stream, input[i].left );
serialize_bool( stream, input[i].right );
serialize_bool( stream, input[i].up );
serialize_bool( stream, input[i].down );
serialize_bool( stream, input[i].push );
serialize_bool( stream, input[i].pull );
}
*/
}
}
}
};
template <typename Stream> inline void serialize_vector( Stream & stream, vec3f & vector )
{
float values[3];
if ( Stream::IsWriting )
vector.store( values );
serialize_float( stream, values[0] );
serialize_float( stream, values[1] );
serialize_float( stream, values[2] );
if ( Stream::IsReading )
vector.load( values );
}
template <typename Stream> inline void serialize_quaternion( Stream & stream, vectorial::quat4f & quaternion )
{
float values[4];
if ( Stream::IsWriting )
quaternion.store( values );
serialize_float( stream, values[0] );
serialize_float( stream, values[1] );
serialize_float( stream, values[2] );
serialize_float( stream, values[3] );
if ( Stream::IsReading )
quaternion.load( values );
}
struct SnapshotPacket : public Packet
{
bool synchronizing = false;
bool bracketing = false;
bool reconnect = false;
uint16_t sync_offset = 0;
uint16_t bracket_offset = 0;
uint16_t adjustment_sequence = 0;
int adjustment_offset = 0;
uint64_t tick = 0;
uint64_t input_ack = 0;
vec3f cube_position;
quat4f cube_orientation;
vec3f cube_linear_velocity;
vec3f cube_angular_velocity;
SERIALIZE_OBJECT( stream )
{
serialize_bool( stream, synchronizing );
if ( synchronizing )
{
serialize_uint64( stream, tick );
serialize_uint16( stream, sync_offset );
}
else
{
serialize_bool( stream, reconnect );
serialize_bool( stream, bracketing );
serialize_uint16( stream, bracket_offset );
if ( !bracketing )
{
serialize_uint16( stream, adjustment_sequence );
serialize_int( stream, adjustment_offset, AdjustmentOffsetMinimum, AdjustmentOffsetMaximum );
}
serialize_uint64( stream, tick );
serialize_uint64( stream, input_ack );
serialize_vector( stream, cube_position );
serialize_quaternion( stream, cube_orientation );
serialize_vector( stream, cube_linear_velocity );
serialize_vector( stream, cube_angular_velocity );
}
}
};
bool write_packet( WriteStream & stream, Packet & base_packet, int & packet_bytes )
{
typedef WriteStream Stream;
serialize_int( stream, base_packet.type, 0, NUM_PACKET_TYPES - 1 );
switch ( base_packet.type )
{
// todo: convert these to macros
case PACKET_TYPE_CONNECTION_REQUEST:
{
ConnectionRequestPacket & packet = (ConnectionRequestPacket&) base_packet;
serialize_object( stream, packet );
}
break;
case PACKET_TYPE_CONNECTION_ACCEPTED:
{
ConnectionAcceptedPacket & packet = (ConnectionAcceptedPacket&) base_packet;
serialize_object( stream, packet );
}
break;
case PACKET_TYPE_CONNECTION_DENIED:
{
ConnectionDeniedPacket & packet = (ConnectionDeniedPacket&) base_packet;
serialize_object( stream, packet );
}
break;
case PACKET_TYPE_INPUT:
{
InputPacket & packet = (InputPacket&) base_packet;
serialize_object( stream, packet );
}
break;
case PACKET_TYPE_SNAPSHOT:
{
SnapshotPacket & packet = (SnapshotPacket&) base_packet;
serialize_object( stream, packet );
}
break;
}
stream.Flush();
packet_bytes = stream.GetBytesProcessed();
return !stream.IsOverflow();
}
extern bool process_packet( const class Address & from, Packet & packet, void * context );
bool read_packet( const class Address & from, uint8_t * buffer, int buffer_size, void * context )
{
typedef ReadStream Stream;
int packet_type;
ReadStream stream( buffer, buffer_size );
serialize_int( stream, packet_type, 0, NUM_PACKET_TYPES - 1 );
switch( packet_type )
{
// todo: convert these to macros
case PACKET_TYPE_CONNECTION_REQUEST:
{
ConnectionRequestPacket packet;
packet.type = packet_type;
serialize_object( stream, packet );
if ( !stream.IsOverflow() )
return process_packet( from, packet, context );
}
break;
case PACKET_TYPE_CONNECTION_ACCEPTED:
{
ConnectionAcceptedPacket packet;
packet.type = packet_type;
serialize_object( stream, packet );
if ( !stream.IsOverflow() )
return process_packet( from, packet, context );
}
break;
case PACKET_TYPE_CONNECTION_DENIED:
{
ConnectionDeniedPacket packet;
packet.type = packet_type;
serialize_object( stream, packet );
if ( !stream.IsOverflow() )
return process_packet( from, packet, context );
}
break;
case PACKET_TYPE_INPUT:
{
InputPacket packet;
packet.type = packet_type;
serialize_object( stream, packet );
if ( !stream.IsOverflow() )
return process_packet( from, packet, context );
}
break;
case PACKET_TYPE_SNAPSHOT:
{
SnapshotPacket packet;
packet.type = packet_type;
serialize_object( stream, packet );
if ( !stream.IsOverflow() )
return process_packet( from, packet, context );
}
break;
}
return false;
}
const char * packet_type_string( int packet_type )
{
switch ( packet_type )
{
case PACKET_TYPE_CONNECTION_REQUEST: return "connection request";
case PACKET_TYPE_CONNECTION_ACCEPTED: return "connection accepted";
case PACKET_TYPE_CONNECTION_DENIED: return "connection denied";
case PACKET_TYPE_INPUT: return "input";
case PACKET_TYPE_SNAPSHOT: return "snapshot";
default:
assert( false );
return "???";
}
}
#endif // #ifndef PACKETS_H