forked from 3dyne/3dyne_legacy_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g_server.cpp
334 lines (259 loc) · 6.49 KB
/
g_server.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
/*
* 3dyne Legacy Engine GPL Source Code
*
* Copyright (C) 1996-2012 Matthias C. Berger & Simon Berger.
*
* This file is part of the 3dyne Legacy Engine GPL Source Code ("3dyne Legacy
* Engine Source Code").
*
* 3dyne Legacy Engine Source Code is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* 3dyne Legacy Engine Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* 3dyne Legacy Engine Source Code. If not, see
* <http://www.gnu.org/licenses/>.
*
* In addition, the 3dyne Legacy Engine Source Code is also subject to certain
* additional terms. You should have received a copy of these additional terms
* immediately following the terms and conditions of the GNU General Public
* License which accompanied the 3dyne Legacy Engine Source Code.
*
* Contributors:
* Matthias C. Berger ([email protected]) - initial API and implementation
* Simon Berger ([email protected]) - initial API and implementation
*/
// g_server.c
#include "lib_container.h"
#include "lib_queue.h"
#include "g_server.h"
#include "g_library.h"
#include "interfaces.h"
/*
==============================
G_InitServer
==============================
*/
void G_InitServer( g_server_t *sv )
{
sh_var_t *tmp;
INITTYPE( sv, g_server_t );
sv->state = gServerState_try_init;
SHV_Printf( "\n" );
SHV_Printf( " --- Init Server ---\n" );
__chkptr( sv );
tmp = SHP_GetVar( "gc_servermode" );
__named_message( "gc_servermode is '%s'\n", tmp->string );
if ( !strcmp( tmp->string, "local" ) )
{
//
// load server dll
//
sprintf( sv->lib.name, "sv" );
if ( G_LibLoad( &sv->lib ) )
{
__error( "can't load server dll\n" );
}
sv->lib.api.Init();
sv->lib.api.Com( "SV_WorldSpring" );
sv->mode = gServerMode_local;
sv->state = gServerState_is_init;
}
else if ( !strcmp( tmp->string, "remote" ) )
{
sv->mode = gServerMode_remote;
sv->state = gServerState_is_init;
}
else
{
__error( "shvar 'gc_servermode' is invalide\n" );
}
G_ServerResetInput( sv );
SHV_Printf( "\n" );
SHV_Printf( " --- Init Server done ---\n" );
}
/*
==============================
G_CleanUpServer
==============================
*/
void G_CleanUpServer( g_server_t *sv )
{
SHV_Printf( "\n" );
SHV_Printf( " --- Clean Up Server ---\n" );
__chkptr( sv );
if ( sv->mode == gServerMode_local )
{
sv->lib.api.CleanUp();
G_LibUnload( &sv->lib );
}
else if ( sv->mode == gServerMode_remote )
{
}
else
{
__error( "unknown server mode\n" );
}
sv->state = gServerState_none;
sv->mode = gServerMode_none;
SHV_Printf( "\n" );
SHV_Printf( " --- Clean Up Server done ---\n" );
}
#if 0
/*
==============================
G_ServerSetInputEventQueue
==============================
*/
void G_ServerSetInputByteIter( g_server_t *sv, byte_iter_t *bi )
{
__chkptr( sv );
__chkptr( bi );
sv->bi_in = bi;
if ( sv->mode == gServerMode_local )
{
sv->lib.api.SetInputByteIter( bi );
}
}
/*
==============================
G_ServerSetOutputEventQueue
==============================
*/
void G_ServerSetOutputByteIter( g_server_t *sv, byte_iter_t *bi )
{
__chkptr( sv );
__chkptr( bi );
sv->bi_out = bi;
if ( sv->mode == gServerMode_local )
{
sv->lib.api.SetOutputByteIter( bi );
}
}
#endif
/*
==============================
G_ServerFillInput
==============================
*/
void G_ServerResetInput( g_server_t *sv )
{
sv->buf_in_size = 0;
}
void G_ServerFillInput( g_server_t *sv, char *buf, int size )
{
int i;
if ( sv->buf_in_size + size >= G_SERVER_INPUT_BUF_SIZE )
{
__error( "buffer overflow\n" );
}
for ( i = 0; i < size; i++ )
{
sv->buf_in[sv->buf_in_size++] = buf[i];
}
}
/*
==============================
G_ServerGetOutput
==============================
*/
void G_ServerResetOutput( g_server_t *sv )
{
sv->buf_out_size = 0;
}
char * G_ServerGetOutputBase( g_server_t *sv )
{
return sv->buf_out;
}
int G_ServerGetOutputSize( g_server_t *sv )
{
return sv->buf_out_size;
}
/*
==============================
G_ServerRunFrame
==============================
*/
void G_ServerRunFrame( g_server_t *sv )
{
__chkptr( sv );
if ( sv->state != gServerState_is_init )
__error( "state is not init\n" );
if ( sv->mode == gServerMode_local )
{
byte_iter_t bi_in;
byte_iter_t bi_out;
ByteIter_Init( &bi_in, sv->buf_in, sv->buf_in_size );
ByteIter_Init( &bi_out, sv->buf_out, G_SERVER_OUTPUT_BUF_SIZE );
sv->lib.api.SetInputByteIter( &bi_in );
sv->lib.api.SetOutputByteIter( &bi_out );
if ( 1 );
{
char com_call[256];
const char *com_result;
sprintf( com_call, "SV_RunFrame %u", ms_wfbegin );
com_result = sv->lib.api.Com( com_call );
if ( com_result )
{
// printf( "com_result: '%s'\n", com_result );
}
}
sv->buf_out_size = ByteIter_GetOfs( &bi_out );
}
else if ( sv->mode == gServerMode_remote )
{
//
// send sv_in as frags through connected_server
//
// __named_message( "client sends %d bytes of sv_in\n", ByteIter_GetSize( sv->bi_in ) );
#if 1
int i;
buffer_send_t send;
char *send_buf;
int send_num_byte;
static int send_id = 1;
int num_frag;
// int total_udp;
send_num_byte = sv->buf_in_size;
send_buf = sv->buf_in;
// total_udp = 0;
BufferSend_Init( &send, send_buf, send_num_byte, send_id++ );
for ( num_frag = 0; ; num_frag++ )
{
byte_iter_t *bi;
int frag_num_byte;
char frag_buf[128];
if ( BufferSend_IsComplete( &send ) )
break;
bi = GC_NetSendInit( gc_state );
ByteIter_Packi( bi, netEv_cl2sv_buf_frag );
frag_num_byte = 72;
BufferSend_GetFrag( &send, frag_buf, &frag_num_byte );
for ( i = 0; i < frag_num_byte; i++ )
{
ByteIter_Packb( bi, frag_buf[i] );
}
printf( "." );
GC_NetSendFromLocalPort( gc_state, &gc_state->connected_sv.host );
}
// __named_message( "send %d/%d bytes in %d fragments\n", send_num_byte, total_udp, num_frag );
#endif
//
// if connected_server got a complete buffer, put it into sv_out
//
}
else
{
__error( "unknown server mode\n" );
}
//
// init buffer for next server frame
//
G_ServerResetInput( sv );
}