forked from mas-bandwidth/yojimbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soak.cpp
340 lines (279 loc) · 13.2 KB
/
soak.cpp
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
/*
Yojimbo Soak Test.
Copyright © 2016 - 2019, The Network Protocol Company, Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "shared.h"
#include <signal.h>
const int MaxPacketSize = 16 * 1024;
const int MaxSnapshotSize = 8 * 1024;
const int MaxBlockSize = 64 * 1024;
static volatile int quit = 0;
void interrupt_handler( int /*dummy*/ )
{
quit = 1;
}
static const int UNRELIABLE_UNORDERED_CHANNEL = 0;
static const int RELIABLE_ORDERED_CHANNEL = 1;
int SoakMain()
{
srand( (unsigned int) time( NULL ) );
ClientServerConfig config;
config.maxPacketSize = MaxPacketSize;
config.clientMemory = 10 * 1024 * 1024;
config.serverGlobalMemory = 10 * 1024 * 1024;
config.serverPerClientMemory = 10 * 1024 * 1024;
config.numChannels = 2;
config.channel[UNRELIABLE_UNORDERED_CHANNEL].type = CHANNEL_TYPE_UNRELIABLE_UNORDERED;
config.channel[UNRELIABLE_UNORDERED_CHANNEL].maxBlockSize = MaxSnapshotSize;
config.channel[RELIABLE_ORDERED_CHANNEL].type = CHANNEL_TYPE_RELIABLE_ORDERED;
config.channel[RELIABLE_ORDERED_CHANNEL].maxBlockSize = MaxBlockSize;
config.channel[RELIABLE_ORDERED_CHANNEL].blockFragmentSize = 1024;
uint8_t privateKey[KeyBytes];
memset( privateKey, 0, KeyBytes );
double time = 0.0;
Address serverAddress( "127.0.0.1", ServerPort );
Server server( GetDefaultAllocator(), privateKey, serverAddress, config, adapter, time );
server.Start( 1 );
uint64_t clientId = 0;
random_bytes( (uint8_t*) &clientId, 8 );
Client client( GetDefaultAllocator(), Address("0.0.0.0"), config, adapter, time );
client.InsecureConnect( privateKey, clientId, serverAddress );
uint64_t numMessagesSentToServer = 0;
uint64_t numMessagesSentToClient = 0;
uint64_t numMessagesReceivedFromClient = 0;
uint64_t numMessagesReceivedFromServer = 0;
signal( SIGINT, interrupt_handler );
bool clientConnected = false;
bool serverConnected = false;
while ( !quit )
{
client.SendPackets();
server.SendPackets();
client.ReceivePackets();
server.ReceivePackets();
if ( client.ConnectionFailed() )
{
printf( "error: client connect failed!\n" );
break;
}
time += 0.1f;
if ( client.IsConnected() )
{
clientConnected = true;
{
const int messagesToSend = random_int( 0, 64 );
for ( int i = 0; i < messagesToSend; ++i )
{
if ( !client.CanSendMessage( RELIABLE_ORDERED_CHANNEL ) )
break;
if ( rand() % 25 )
{
TestMessage * message = (TestMessage*) client.CreateMessage( TEST_MESSAGE );
if ( message )
{
message->sequence = (uint16_t) numMessagesSentToServer;
client.SendMessage( RELIABLE_ORDERED_CHANNEL, message );
numMessagesSentToServer++;
}
}
else
{
TestBlockMessage * blockMessage = (TestBlockMessage*) client.CreateMessage( TEST_BLOCK_MESSAGE );
if ( blockMessage )
{
blockMessage->sequence = (uint16_t) numMessagesSentToServer;
const int blockSize = 1 + ( int( numMessagesSentToServer ) * 33 ) % MaxBlockSize;
uint8_t * blockData = client.AllocateBlock( blockSize );
if ( blockData )
{
for ( int j = 0; j < blockSize; ++j )
blockData[j] = uint8_t( numMessagesSentToServer + j );
client.AttachBlockToMessage( blockMessage, blockData, blockSize );
client.SendMessage( RELIABLE_ORDERED_CHANNEL, blockMessage );
numMessagesSentToServer++;
}
else
{
client.ReleaseMessage( blockMessage );
}
}
}
}
}
const int clientIndex = client.GetClientIndex();
if ( server.IsClientConnected( clientIndex ) )
{
serverConnected = true;
const int messagesToSend = random_int( 0, 64 );
for ( int i = 0; i < messagesToSend; ++i )
{
if ( !server.CanSendMessage( clientIndex, RELIABLE_ORDERED_CHANNEL ) )
break;
if ( rand() % 25 )
{
TestMessage * message = (TestMessage*) server.CreateMessage( clientIndex, TEST_MESSAGE );
if ( message )
{
message->sequence = (uint16_t) numMessagesSentToClient;
server.SendMessage( clientIndex, RELIABLE_ORDERED_CHANNEL, message );
numMessagesSentToClient++;
}
}
else
{
TestBlockMessage * blockMessage = (TestBlockMessage*) server.CreateMessage( clientIndex, TEST_BLOCK_MESSAGE );
if ( blockMessage )
{
blockMessage->sequence = (uint16_t) numMessagesSentToClient;
const int blockSize = 1 + ( int( numMessagesSentToClient ) * 33 ) % MaxBlockSize;
uint8_t * blockData = server.AllocateBlock( clientIndex, blockSize );
if ( blockData )
{
for ( int j = 0; j < blockSize; ++j )
blockData[j] = uint8_t( numMessagesSentToClient + j );
server.AttachBlockToMessage( clientIndex, blockMessage, blockData, blockSize );
server.SendMessage( clientIndex, RELIABLE_ORDERED_CHANNEL, blockMessage );
numMessagesSentToClient++;
}
else
{
server.ReleaseMessage( clientIndex, blockMessage );
}
}
}
}
while ( true )
{
Message * message = server.ReceiveMessage( clientIndex, RELIABLE_ORDERED_CHANNEL );
if ( !message )
break;
yojimbo_assert( message->GetId() == (uint16_t) numMessagesReceivedFromClient );
switch ( message->GetType() )
{
case TEST_MESSAGE:
{
TestMessage * testMessage = (TestMessage*) message;
yojimbo_assert( testMessage->sequence == uint16_t( numMessagesReceivedFromClient ) );
printf( "server received message %d\n", testMessage->sequence );
server.ReleaseMessage( clientIndex, message );
numMessagesReceivedFromClient++;
}
break;
case TEST_BLOCK_MESSAGE:
{
TestBlockMessage * blockMessage = (TestBlockMessage*) message;
yojimbo_assert( blockMessage->sequence == uint16_t( numMessagesReceivedFromClient ) );
const int blockSize = blockMessage->GetBlockSize();
const int expectedBlockSize = 1 + ( int( numMessagesReceivedFromClient ) * 33 ) % MaxBlockSize;
if ( blockSize != expectedBlockSize )
{
printf( "error: block size mismatch. expected %d, got %d\n", expectedBlockSize, blockSize );
return 1;
}
const uint8_t * blockData = blockMessage->GetBlockData();
yojimbo_assert( blockData );
for ( int i = 0; i < blockSize; ++i )
{
if ( blockData[i] != uint8_t( numMessagesReceivedFromClient + i ) )
{
printf( "error: block data mismatch. expected %d, but blockData[%d] = %d\n", uint8_t( numMessagesReceivedFromClient + i ), i, blockData[i] );
return 1;
}
}
printf( "server received message %d\n", uint16_t( numMessagesReceivedFromClient ) );
server.ReleaseMessage( clientIndex, message );
numMessagesReceivedFromClient++;
}
break;
}
}
}
while ( true )
{
Message * message = client.ReceiveMessage( RELIABLE_ORDERED_CHANNEL );
if ( !message )
break;
yojimbo_assert( message->GetId() == (uint16_t) numMessagesReceivedFromServer );
switch ( message->GetType() )
{
case TEST_MESSAGE:
{
TestMessage * testMessage = (TestMessage*) message;
yojimbo_assert( testMessage->sequence == uint16_t( numMessagesReceivedFromServer ) );
printf( "client received message %d\n", testMessage->sequence );
client.ReleaseMessage( message );
numMessagesReceivedFromServer++;
}
break;
case TEST_BLOCK_MESSAGE:
{
TestBlockMessage * blockMessage = (TestBlockMessage*) message;
yojimbo_assert( blockMessage->sequence == uint16_t( numMessagesReceivedFromServer ) );
const int blockSize = blockMessage->GetBlockSize();
const int expectedBlockSize = 1 + ( int( numMessagesReceivedFromServer ) * 33 ) % MaxBlockSize;
if ( blockSize != expectedBlockSize )
{
printf( "error: block size mismatch. expected %d, got %d\n", expectedBlockSize, blockSize );
return 1;
}
const uint8_t * blockData = blockMessage->GetBlockData();
yojimbo_assert( blockData );
for ( int i = 0; i < blockSize; ++i )
{
if ( blockData[i] != uint8_t( numMessagesReceivedFromServer + i ) )
{
printf( "error: block data mismatch. expected %d, but blockData[%d] = %d\n", uint8_t( numMessagesReceivedFromServer + i ), i, blockData[i] );
return 1;
}
}
printf( "client received message %d\n", uint16_t( numMessagesReceivedFromServer ) );
client.ReleaseMessage( message );
numMessagesReceivedFromServer++;
}
break;
}
}
if ( clientConnected && !client.IsConnected() )
break;
if ( serverConnected && server.GetNumConnectedClients() == 0 )
break;
}
client.AdvanceTime( time );
server.AdvanceTime( time );
}
if ( quit )
{
printf( "\nstopped\n" );
}
client.Disconnect();
server.Stop();
return 0;
}
int main()
{
printf( "\nsoak\n" );
if ( !InitializeYojimbo() )
{
printf( "error: failed to initialize Yojimbo!\n" );
return 1;
}
yojimbo_log_level( YOJIMBO_LOG_LEVEL_INFO );
srand( (unsigned int) time( NULL ) );
int result = SoakMain();
ShutdownYojimbo();
printf( "\n" );
return result;
}