-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
soak.c
258 lines (203 loc) · 8.09 KB
/
soak.c
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
/*
reliable
Copyright © 2017 - 2024, Mas Bandwidth LLC
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 "reliable.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <inttypes.h>
#define MAX_PACKET_BYTES (16*1024)
static volatile int quit = 0;
void interrupt_handler( int signal )
{
(void) signal;
quit = 1;
}
int random_int( int a, int b )
{
assert( a < b );
int result = a + rand() % ( b - a + 1 );
assert( result >= a );
assert( result <= b );
return result;
}
struct test_context_t
{
struct reliable_endpoint_t * client;
struct reliable_endpoint_t * server;
};
double global_time = 100.0;
struct test_context_t global_context;
void test_transmit_packet_function( void * _context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
(void) sequence;
struct test_context_t * context = (struct test_context_t*) _context;
if ( random_int(0,100) < 5 )
return;
if ( id == 0 )
{
reliable_endpoint_receive_packet( context->server, packet_data, packet_bytes );
}
else if ( id == 1 )
{
reliable_endpoint_receive_packet( context->client, packet_data, packet_bytes );
}
}
int generate_packet_data( uint16_t sequence, uint8_t * packet_data )
{
int packet_bytes = ( ( (int)sequence * 1023 ) % ( MAX_PACKET_BYTES - 2 ) ) + 2;
assert( packet_bytes >= 2 );
assert( packet_bytes <= MAX_PACKET_BYTES );
packet_data[0] = (uint8_t) ( sequence & 0xFF );
packet_data[1] = (uint8_t) ( (sequence>>8) & 0xFF );
int i;
for ( i = 2; i < packet_bytes; ++i )
{
packet_data[i] = (uint8_t) ( ( (int)i + sequence ) % 256 );
}
return packet_bytes;
}
static void check_handler( char * condition,
char * function,
char * file,
int line )
{
printf( "check failed: ( %s ), function %s, file %s, line %d\n", condition, function, file, line );
#ifdef RELIABLE_DEBUG
#if defined( __GNUC__ )
__builtin_trap();
#elif defined( _MSC_VER )
__debugbreak();
#endif
#endif
exit( 1 );
}
#define check( condition ) \
do \
{ \
if ( !(condition) ) \
{ \
check_handler( #condition, (char*) __FUNCTION__, (char*) __FILE__, __LINE__ ); \
} \
} while(0)
void check_packet_data( uint8_t * packet_data, int packet_bytes )
{
assert( packet_bytes >= 2 );
assert( packet_bytes <= MAX_PACKET_BYTES );
uint16_t sequence = 0;
sequence |= (uint16_t) packet_data[0];
sequence |= ( (uint16_t) packet_data[1] ) << 8;
check( packet_bytes == ( ( (int)sequence * 1023 ) % ( MAX_PACKET_BYTES - 2 ) ) + 2 );
int i;
for ( i = 2; i < packet_bytes; ++i )
{
check( packet_data[i] == (uint8_t) ( ( (int)i + sequence ) % 256 ) );
}
}
int test_process_packet_function( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
assert( packet_data );
assert( packet_bytes > 0 );
assert( packet_bytes <= MAX_PACKET_BYTES );
(void) context;
(void) id;
(void) sequence;
check_packet_data( packet_data, packet_bytes );
return 1;
}
void soak_initialize()
{
printf( "initializing\n" );
reliable_init();
reliable_log_level( RELIABLE_LOG_LEVEL_DEBUG );
memset( &global_context, 0, sizeof( global_context ) );
struct reliable_config_t client_config;
struct reliable_config_t server_config;
reliable_default_config( &client_config );
reliable_default_config( &server_config );
client_config.fragment_above = 500;
server_config.fragment_above = 500;
reliable_copy_string( client_config.name, "client", sizeof( client_config.name ) );
client_config.context = &global_context;
client_config.id = 0;
client_config.transmit_packet_function = &test_transmit_packet_function;
client_config.process_packet_function = &test_process_packet_function;
reliable_copy_string( server_config.name, "server", sizeof( server_config.name ) );
server_config.context = &global_context;
server_config.id = 1;
server_config.transmit_packet_function = &test_transmit_packet_function;
server_config.process_packet_function = &test_process_packet_function;
global_context.client = reliable_endpoint_create( &client_config, global_time );
global_context.server = reliable_endpoint_create( &server_config, global_time );
}
void soak_shutdown()
{
printf( "shutdown\n" );
reliable_endpoint_destroy( global_context.client );
reliable_endpoint_destroy( global_context.server );
reliable_term();
}
void soak_iteration( double time )
{
uint8_t packet_data[MAX_PACKET_BYTES];
memset( packet_data, 0, MAX_PACKET_BYTES );
int packet_bytes;
uint16_t sequence;
sequence = reliable_endpoint_next_packet_sequence( global_context.client );
packet_bytes = generate_packet_data( sequence, packet_data );
reliable_endpoint_send_packet( global_context.client, packet_data, packet_bytes );
sequence = reliable_endpoint_next_packet_sequence( global_context.server );
packet_bytes = generate_packet_data( sequence, packet_data );
reliable_endpoint_send_packet( global_context.server, packet_data, packet_bytes );
reliable_endpoint_update( global_context.client, time );
reliable_endpoint_update( global_context.server, time );
reliable_endpoint_clear_acks( global_context.client );
reliable_endpoint_clear_acks( global_context.server );
}
int main( int argc, char ** argv )
{
int num_iterations = -1;
if ( argc == 2 )
num_iterations = atoi( argv[1] );
soak_initialize();
signal( SIGINT, interrupt_handler );
double delta_time = 0.1;
if ( num_iterations > 0 )
{
int i;
for ( i = 0; i < num_iterations; ++i )
{
if ( quit )
break;
soak_iteration( global_time );
global_time += delta_time;
}
}
else
{
while ( !quit )
{
soak_iteration( global_time );
global_time += delta_time;
}
}
soak_shutdown();
return 0;
}