forked from 3dyne/3dyne_legacy_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc_service.cpp
2286 lines (1741 loc) · 44.2 KB
/
gc_service.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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
*/
// gc_service.c
#ifdef win32_x86
typedef int socklen_t;
#include <winsock2.h>
#endif
#include "interfaces.h"
#include "./r_fake/r_interface.h"
#include "r_private.h"
#include "lib_tag.h"
#include "lib_queue.h"
#include "lib_parse.h"
#include "lib_digest.h"
#include "g_psys.h"
#include "r_count.h"
#include "g_server.h"
#include "g_client.h"
#include "g_library.h"
#include "snd_deamon.h"
#if !defined (win32_x86)
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#else
#endif
//#include <sys/time.h>
#include <errno.h>
//#include "shape_defs.h"
//#include "shape_instance.h"
//#include "tess_simple_face.h"
#define MSEC_WF ( 100 ) // msecs of worldframe
#define GC_ISWORLDFRAME ( 1 )
#define GC_ISRENDERFRAME ( 2 )
static char graphcursor[4][256];
static sh_var_t *rendershot;
int gc_fps = 0;
fp_t gc_fpfps;
int gc_tps = 0; // tris/sec
int gc_tpf = 0; // tris/frame
static int gc_inmainloop = 0;
static fp_t gc_ftimes[8];
unsigned int ms_win;
unsigned int ms_rfbegin = 0;
unsigned int ms_wfbegin = 0;
int ms_rfdelta = 0; // time passed since last render frame
int gc_ismenu = 0;
int gc_isshell = 0;
//int gc_ismap = 0;
//int gc_islife = 0;
//int gc_isloading = 0;
int gc_renderframe = 0;
int gc_localworldframe = 0;
int gc_textcursor;
char *gc_graphcursor;
int gc_wftimebase;
//int gci_exit; // exit if set
int gci_domuch;
//int gci_spawnmap;
//int gci_dropmap;
//int gci_restartgl;
//int gci_startlocalgame;
//int gci_startremotegame;
int gc_lastbusy;
playermod_t pmod; // test only!
int mx = 0, my = 0;
static int wf_mdx, wf_mdy;
int gc_localseconds;
sh_var_t *gc_mousescale;
sh_var_t *gc_invmouse;
static sh_var_t *gc_showfps;
gc_state_t *gc_state;
// fixme: sysdep stuff!
#if defined( win32_x86 )
static int gc_wsisup = 0;
static WSADATA winsockdata;
#endif
FILE *h_sendudp = NULL;
FILE *h_recvudp = NULL;
void GC_StartRemoteGame( void );
void GC_DropRemoteGame( void );
void GC_TryClientToServerConnect( void );
void GC_TryClientToServerJoin( void );
void GC_ClientInputUpdate();
void GC_RunClientFrame();
void GC_LoadGraphCursor()
{
TFUNC_ENTER;
strcpy( graphcursor[0], "menu.tex.bigcurs00" );
strcpy( graphcursor[1], "menu.tex.bigcurs01" );
strcpy( graphcursor[2], "menu.tex.bigcurs02" );
strcpy( graphcursor[3], "menu.tex.bigcurs03" );
TFUNC_LEAVE;
}
void GC_ClearInitFlags()
{
TFUNC_ENTER;
// gci_exit = 0;
gci_domuch = 0;
TFUNC_LEAVE;
}
int GC_CheckWFTimebase()
{
unsigned int now;
now = SYS_GetMsec();
// cprintf( "ms_frdelta: %d\n", ms_rfdelta );
if( now - ms_wfbegin > MSEC_WF )
{
// cprintf( "left time: %d", 100 - ( now - ms_wfbegin ) );
// __named_message( "its time\n" );
ms_wfbegin = SYS_GetMsec();
gc_wftimebase = 1;
// printf( "wf: %u\n", now );
return 1;
}
return 0;
}
void GC_DoMuch()
{
// test givebacktime
__error( "total crap function called\n" ); // this won't do much on most current compilers...
#if 0
int i, i2;
TFUNC_ENTER;
// GC_SpawnLife();
for( i = 0; i < 20; i++ )
{
cprintf( "working hard\n" );
for( i2 = 0; i2 < 10000000; i2++ )
{
a = i * i2;
}
GC_GiveBackTime();
}
TFUNC_LEAVE;
#endif
}
// =============================================================================
/*
==============================
GC_AddPlayer
==============================
*/
void GC_AddPlayer( unique_t id )
{
char tt[256];
cprintf( "add player to game\n" );
if ( gc_state->sv.mode == gServerMode_local )
{
sprintf( tt, "SV_AddPlayer #%u", id );
gc_state->sv.lib.api.Com( tt );
// sprintf( tt, "%u", id );
// SHP_SetVar( "gc_id_bind_view", tt, 0 );
// SHP_SetVar( "gc_id_bind_input", tt, 0 );
}
else
{
__error( "no local server, can't add player\n" );
}
}
void GC_RemovePlayer( unique_t id )
{
char str[256];
cprintf( "remove player from game\n" );
if ( gc_state->sv.mode == gServerMode_local )
{
sprintf( str, "SV_RemovePlayer #%u", id );
gc_state->sv.lib.api.Com( str );
}
else
{
__error( "no local server, can't remove player\n" );
}
}
void GC_BindControl( unique_t id )
{
char tt[256];
sprintf( tt, "%u", id );
SHP_SetVar( "gc_id_bind_view", tt, 0 );
SHP_SetVar( "gc_id_bind_input", tt, 0 );
}
/*
==============================
GC_InitDemo
==============================
*/
void GC_InitDemo( gc_state_t *state )
{
if ( state->gc_is_demo )
{
cprintf( "already in a demo\n" );
return;
}
SHP_SetVar( "gc_servermode", "local", 0 );
SHP_SetVar( "gc_map", "title1", 0 );
state->gc_is_demo = true;
// GC_InitGame( state );
}
/*
==============================
GC_CleanUpDemo
==============================
*/
void GC_CleanUpDemo( gc_state_t *state )
{
if ( !state->gc_is_demo )
{
cprintf( "not in a demo\n" );
return;
}
state->gc_is_demo = false;
// GC_CleanUpGame( state );
}
/*
==============================
GC_InitSingle
==============================
*/
void GC_InitSingle( gc_state_t *state )
{
if ( state->gc_is_single )
{
cprintf( "already in a single player game\n" );
return;
}
SHP_SetVar( "gc_servermode", "local", 0 );
// SHP_SetVar( "gc_map", "test8", 0 );
state->gc_is_single = true;
}
/*
==============================
GC_CleanUpSingle
==============================
*/
void GC_CleanUpSingle( gc_state_t *state )
{
if ( !state->gc_is_single )
{
cprintf( "not in single player game\n" );
return;
}
state->gc_is_single = false;
}
/*
==============================
GC_InitConnectPublicServer
==============================
*/
void GC_TryClientToServerConnect( void )
{
gc_host_addr_t assumed_server;
cprintf( "try to connect ...\n" );
if ( gc_state->connected_sv.state != connectedServerState_none )
__error( "expected connectedServerState_none\n" );
//
// set server address from local settings
//
{
struct sockaddr_in addr;
sh_var_t *tmp;
tmp = SHP_GetVar( "gc_remoteaddr" );
__named_message( "gc_remoteaddr is '%s'\n", tmp->string );
addr.sin_addr.s_addr = inet_addr( tmp->string );
tmp = SHP_GetVar( "gc_remoteport" );
__named_message( "gc_remoteport is '%s'\n", tmp->string );
addr.sin_port = htons( atoi( tmp->string ) );
addr.sin_family = AF_INET;
GC_InitHostAddrFromInetAddr( &assumed_server, &addr );
}
GC_ConnectedServerInit( gc_state, &gc_state->connected_sv, &assumed_server );
}
void GC_InitConnectPublicServer( gc_state_t *state )
{
if ( state->gc_has_server_connection )
{
cprintf( "already connected\n" );
return;
}
if ( GC_NetOpenLocalPort( state, true ) != true )
{
__error( "can't open local port\n" );
}
GC_TryClientToServerConnect();
state->gc_has_server_connection = true;
}
/*
==============================
GC_CleanUpConnectPublicServer
==============================
*/
void GC_CleanUpConnectPublicServer( gc_state_t *state )
{
if ( !state->gc_has_server_connection )
{
cprintf( "not connected\n" );
return;
}
GC_ConnectedServerCleanUp( state, &state->connected_sv );
GC_NetCloseLocalPort( state );
state->gc_has_server_connection = false;
}
/*
==============================
GC_InitPublicServer
==============================
*/
void GC_InitPublicServer( gc_state_t *state )
{
if ( state->gc_is_public_server )
{
cprintf( "already a public server\n" );
return;
}
SHP_SetVar( "gc_servermode", "local", 0 );
if ( GC_NetOpenLocalPort( state, true ) != true )
{
__error( "can't open local port\n" );
}
state->u_start_game = true;
state->gc_is_public_server = true;
}
/*
==============================
GC_CleanUpPublicServer
==============================
*/
void GC_CleanUpPublicServer( gc_state_t *state )
{
int i;
gc_connected_client_t *cc;
if ( !state->gc_is_public_server )
{
cprintf( "not a public server\n" );
return;
}
for ( i = 0; i < GC_STATE_MAX_CONNECTED_CLIENTS; i++ )
{
cc = &state->connected_cl_tbl[i];
if ( cc->state == connectedClientState_none )
{
continue;
}
GC_ConnectedClientCleanUp( state, cc );
}
GC_NetCloseLocalPort( state );
state->gc_is_public_server = false;
}
/*
==============================
GC_InitGame
==============================
*/
void GC_InitGame( gc_state_t *state )
{
if ( state->gc_in_game )
{
cprintf( "already in a game\n" );
return;
}
sc_readbytes = 0;
g_st = G_NewState();
G_StateInit( g_st );
state->gc_is_loading = true;
G_InitMap( &gc_state->map );
G_InitServer( &gc_state->sv );
G_InitClient( &gc_state->cl );
state->gc_is_loading = false;
state->gc_in_game = true;
if ( state->gc_is_single || state->gc_is_public_server )
{
unique_t id;
id = GC_GetUniqueID( state );
GC_AddPlayer( id );
GC_BindControl( id );
}
//
// tell server that client is ready to handle events
//
if ( state->gc_has_server_connection )
{
if ( state->connected_sv.state == connectedServerState_is_starting )
{
state->connected_sv.state = connectedServerState_is_started;
}
}
gc_localworldframe = 0;
// gc_islife = 1;
// gc_ismap = 1;
// gc_isloading = 0;
//
gc_state->state = gcState_game_ready;
}
/*
==============================
GC_CleanUpGame
==============================
*/
void GC_CleanUpGame( gc_state_t *state )
{
if ( !state->gc_in_game )
{
cprintf( "not in a game\n" );
return;
}
G_CleanUpClient( &gc_state->cl );
G_CleanUpServer( &gc_state->sv );
G_CleanUpMap( &gc_state->map );
FREE( g_st );
state->gc_in_game = false;
}
/*
==============================
GC_CleanUp
==============================
*/
void GC_CleanUp( gc_state_t *state )
{
if ( state->gc_has_server_connection )
{
GC_CleanUpConnectPublicServer( state );
}
if ( state->gc_is_public_server )
{
GC_CleanUpPublicServer( state );
}
if ( state->gc_is_demo )
{
GC_CleanUpDemo( state );
}
if ( state->gc_is_single )
{
GC_CleanUpSingle( state );
}
if ( state->gc_is_loading )
{
__error( "while loading ...\n" );
}
if ( state->gc_in_game )
{
GC_CleanUpGame( state );
}
// state->u_start_demo = true;
}
/*
==================================================
net_events handled by game_control
==================================================
*/
void GC_NetEventHandle_host2host_connect_as_client( gc_state_t *state )
{
int entry;
gc_connected_client_t *cc;
cprintf( "recv host==>host: connect_as_client\n" );
entry = GC_ConnectedClientTableGetEntryByHostAddr( state, &state->host_last_recv );
cprintf( "remote host is: %s\n", state->host_last_recv.addr_str );
if ( entry != -1 )
{
//
// the client is already connected
//
cprintf( "ignore, host is already a client\n" );
// GC_NetEventReply_sv2cl_refuse( state, &state->host_last_recv );
return;
}
entry = GC_ConnectedClientTableGetFreeEntry( state );
if ( entry == -1 )
{
//
// the server can't handle more clients
//
cprintf( "ignore, don't accpet more clients\n" );
// GC_NetEventReply_sv2cl_refuse( state, &state->host_last_recv );
return;
}
cc = &state->connected_cl_tbl[entry];
GC_ConnectedClientInit( state, cc, &state->host_last_recv );
cprintf( "server: accept host as client\n" );
}
// =============================================================================
void GC_SendServerOutputToLocalClient( gc_state_t *state )
{
char *sv_out_buf;
int sv_out_size;
sv_out_buf = G_ServerGetOutputBase( &gc_state->sv );
sv_out_size = G_ServerGetOutputSize( &gc_state->sv );
//
// copy server output to local client input
//
G_ClientFillInput( &state->cl, sv_out_buf, sv_out_size );
}
void GC_SendServerOutputToAllConnectedClients( gc_state_t *state )
{
int i, j;
char *sv_out_buf;
int sv_out_size;
static int send_id = 1;
buffer_send_t send;
int num_frag;
sv_out_buf = G_ServerGetOutputBase( &gc_state->sv );
sv_out_size = G_ServerGetOutputSize( &gc_state->sv );
BufferSend_Init( &send, sv_out_buf, sv_out_size, 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_sv2cl_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] );
}
for ( j = 0; j < GC_STATE_MAX_CONNECTED_CLIENTS; j++ )
{
gc_connected_client_t *cc;
cc = &state->connected_cl_tbl[j];
if ( cc->state == connectedClientState_is_joined )
{
GC_NetSendFromLocalPort( gc_state, &cc->host );
}
}
}
}
void GC_InputToBuffer( gc_state_t *state, byte_iter_t *bi )
{
sh_var_t *tmp;
unique_t id;
byte_iter_t frm;
tmp = SHP_GetVar( "gc_id_bind_input" );
id = tmp->ivalue;
if ( id == UNIQUE_INVALIDE )
{
return;
}
// cprintf( "lon: %.2f, lat: %.2f, speed: %.2f\n", gc_state->i_lon, gc_state->i_lat, gc_state->i_speed );
PackFrame_Begin( bi, &frm );
ByteIter_Packi( &frm, id );
ByteIter_Packi( &frm, eventType_server_update_player );
ByteIter_Packf( &frm, gc_state->i_speed );
ByteIter_Packf( &frm, gc_state->i_strafespeed );
ByteIter_Packf( &frm, gc_state->i_lon );
ByteIter_Packf( &frm, gc_state->i_lat );
ByteIter_Packi( &frm, gc_state->i_shoot );
ByteIter_Packi( &frm, gc_state->i_use );
ByteIter_Packi( &frm, gc_state->i_jump );
PackFrame_End( bi, &frm );
}
void GC_SendLocalInputToServer( void )
{
char sv_in_buf[1024];
int sv_in_size;
byte_iter_t bi_in;
if ( gc_state->sv.state != gServerState_is_init )
{
return;
}
if ( gc_state->gc_is_demo )
{
return;
}
if ( gc_state->gc_has_server_connection && gc_state->connected_sv.state != connectedServerState_is_joined )
{
return;
}
ByteIter_Init( &bi_in, sv_in_buf, 1024 );
GC_InputToBuffer( gc_state, &bi_in );
sv_in_size = ByteIter_GetOfs( &bi_in );
G_ServerFillInput( &gc_state->sv, sv_in_buf, sv_in_size );
}
void GC_RunServerFrame()
{
TFUNC_ENTER;
// __named_message( "\n" );
if ( gc_state->sv.state == gServerState_is_init )
{
// char *sv_out_buf;
// int sv_out_size;
//
// remote hosts put their pmod during GC_ConnectedClientRun
// into server input buffer
//
//
// put local pmod into server input buffer
//
#if 0
if ( !gc_state->gc_is_demo )
{
ByteIter_Init( &bi_in, sv_in_buf, 1024 );
GC_InputToBuffer( gc_state, &bi_in );
sv_in_size = ByteIter_GetOfs( &bi_in );
G_ServerFillInput( &gc_state->sv, sv_in_buf, sv_in_size );
}
#endif
GC_SendLocalInputToServer();
G_ServerRunFrame( &gc_state->sv );
/*sv_out_buf = */G_ServerGetOutputBase( &gc_state->sv );
/*sv_out_size = */G_ServerGetOutputSize( &gc_state->sv );
//
// copy server output to local client input
//
GC_SendServerOutputToLocalClient( gc_state );
//
// send server output via connected_cl_tbl[] to all remote hosts
//
// fixme: is this the right place for that
GC_SendServerOutputToAllConnectedClients( gc_state );
// handle server output only once
G_ServerResetOutput( &gc_state->sv );
if ( gc_state->sv.mode == gServerMode_local )
{
}
}
TFUNC_LEAVE;
}
// ====================
// GC_MainLoop
// this one should loop
// forever
// does one loop per
// worldframe
// ====================
// fake!
// ====
void GC_SecondFrame()
{
gc_localseconds++;
SHV_ScrollSOut();
}
int GC_ClassifyFrame()
{
if( GC_CheckWFTimebase()) // time for worldframe
{
ms_wfbegin = SYS_GetMsec();
return GC_ISWORLDFRAME;
} else
return GC_ISRENDERFRAME;
return 0;
}
//
// network
//
/*
==============================
GC_NetSendFromLocalPort
==============================
*/
byte_iter_t * GC_NetSendInit( gc_state_t *state )
{
ByteIter_Init( &state->udp_send_bi, state->udp_send_buf, GC_MAX_NET_UDP_SIZE );
return &state->udp_send_bi;
}
void GC_NetSendFromLocalPort( gc_state_t *state, gc_host_addr_t *host )
{
pos4_t ofs;
// SIM: fixme: most of the called functions work with char * but there are some arithmetics in this functions that might rely on unsigned
unsigned char *buf;
unsigned short chksum;
if ( state->udp_state != gcUdpState_is_init )
__error( "expected gcUdpState_is_init\n" );
ofs = ByteIter_GetOfs( &gc_state->udp_send_bi );
buf = (unsigned char *)ByteIter_GetBase( &gc_state->udp_send_bi );
if ( ofs == 0 )
{
return;
}
//
// calc 16 bit chksum for buffer and append it
//
chksum = BufferChecksum16( ofs, (char *)buf );
// printf( "send chksum: %u\n", chksum );
buf[ofs++] = (chksum&255); // low
buf[ofs++] = (chksum>>8)&255; // high
sendto( state->local_sock, (char *)buf, ofs, 0, (struct sockaddr *)&host->addr, sizeof(struct sockaddr) );
if ( h_sendudp )
{
pos4_t i;
int frag_id, frag_ofs, frag_sum;
// assume buffer fragment
BufferFrag_GetInfo( (char*)&buf[4], &frag_id, &frag_ofs, &frag_sum );
fprintf( h_sendudp, "time: %d, size: %d, buf_id %d, frag_ofs %d, frag_sum %d ", ms_rfbegin, ofs, frag_id, frag_ofs, frag_sum );
for ( i = 0; i < ofs; i++ )
{
if ( (i&15) == 0 )
fprintf( h_sendudp, "\n %.4x: ", i );
fprintf( h_sendudp, "%.2x ", (unsigned char) buf[i] );
}
fprintf( h_sendudp, "\n\n" );
}
}
/*
==============================
GC_NetRecvFromLocalPort
==============================
*/
byte_iter_t * GC_NetRecvFromLocalPort( gc_state_t *state )
{
int size;
socklen_t addr_len;
int ret;
struct timeval tv;
fd_set readfs;
if ( state->udp_state != gcUdpState_is_init )
__error( "expected gcUdpState_is_init\n" );
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO( &readfs );
FD_SET( gc_state->local_sock, &readfs );
ret = select( FD_SETSIZE, &readfs, NULL, NULL, &tv );
/*
if( ret < 0 )
cprintf( "select: %s\n", strerror( errno ));
*/
// cprintf( "select: %d %d\n", ret, errno );
if( ret <= 0 )
{
ByteIter_Init( &state->udp_recv_bi, state->udp_recv_buf, 0 );
return NULL;
}
addr_len = sizeof( struct sockaddr );
size = recvfrom( state->local_sock, state->udp_recv_buf, GC_MAX_NET_UDP_SIZE, 0, (struct sockaddr *) &state->addr_last_recv, &addr_len );
if ( size >= 2 )
{
unsigned short chksum1;
unsigned short chksum2;
unsigned char *ptr;
ptr = (unsigned char *)state->udp_recv_buf;