-
Notifications
You must be signed in to change notification settings - Fork 1
/
NET_Lib.pas
1308 lines (1136 loc) · 32.1 KB
/
NET_Lib.pas
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
unit NET_Lib;
interface
// Âñ¸ ÷òî çäåñü íàïèñàííî - îñíîâà
// Ïèñàë ðàäè ïðîâåðêè â ëîêàëêå, ò.å. ñîâñåì â ëîêàëêå =)
// êîãäà ñåòè íåò èáî å¸ ó ìåíÿ íà ìîìåíò íàïèñàíèÿ íå áûëî
// Âåñåëî çâó÷èò =) Òåñòèë ñåòüåâîé êîä áåç ñåòè 8)
// Êàê ýòî ïðîâåðèòü?
// Çàïóñêàåì TFK ïèøåì "gowindow" "net_create 25666"
// Òàê ìû ñîçäàëè ñåðâàíò! Âïå÷àòëÿåò? ;)
// Íå çàêðûâàÿ TFK çàïóñêàåì åù¸ îäíó êîïèþ ïðèëîæåíèÿ
// è ïèøåì "gowindow" "net_create" - àëÿ êëèåíò :)
// Çàòåì ñî ñòîðîíû êëèåíòà øë¸ì "net_send Hello Server!"
// Ïåðåêëþ÷àåìñÿ íà îêíî ñåðâåðà è î óæàñ!
// Ìû âèäèì íàøå ñîîáùåíèå! :)
// ===================================================//
// Êðàòêî îáúÿñíþ ôóíêöèè äâèæêà äëÿ ðàáîòû ñ ñåòüþ:
// NET_Init - íå òðîãàòü!
// NET_Free - ñì. ïðåäûäóùèé! ;)
// NET_InitSocket(IP: PChar; Port: WORD): integer;
// Ñîçäàíèå ñîêåòà, òèïà îêíî â ìèð :)
// Ïîêà íå ñîçäàøü - íè÷åãî íå ïîøë¸øü
// Ïðè óêàçàíèè 0 ïîðòà - âûáèðàåòñÿ ëþáîé ñâîáîäíûé
// ïîðò èç äèàïàçîíà 1024..65535
// ×òî õàðàêòåðíî äëÿ êëèåíòà. À âîò ñåðâåðû ó íàñ áóäóò íà ïîñòîÿííîì ïîðòó
// èáî ïðè ïèíãîâàíèè èõ øâ ïàêåòàìè ìû äîëæíû çíàòü ïîðò!
// Ðçóëüòàò > 0
// NET_GetLocalIP: PChar - ëîêàëüíûé IP êîìïüþòåðà
// NET_GetExternalIP: PChar - IP â èíåòå ê ïðèìåðó
// NET_GetHost: PChar - èìÿ íàøåãî êîìïóòåðà
// NET_HostToIP(Host: PChar): PChar - ïåðåâîäèò õîñò â IP
//À òåïåðü ïîÿñíþ ñàì ïðèíöèï ðàáîòû ñ ñåòüþ
// Åñòü áóôåð, â í¸ì íàêàïëèâàþòñÿ äàííûå. Çàòåì
// ïî æåëàíèþ ïðîãðàììèñòà îíè ñêëàäûâàþòñÿ â ïàêåò è îòïðàâëÿþòñÿ.
// Ìàêñèìàëüíûé ðàçìåð ïàêåòà 65507 áàéò
// ñóùåñòâóåò íå ïðîâåðåííàÿ ðåàëèçàöèÿ Anti Packet Lost
// Îáû÷íûå ïàêåòû ìîãóò òåðÿòüñÿ, ïðàâäà, ýòî îòíîñèòñÿ òîëüêî
// ê êîðÿâûì ñåòÿì, òàêèì êàê èíòåðíåò :)
// Âàæíî çàïîìíèòü! Ïðè îòïðàâêå ïðîñòîãî ïàêåòà äâèæîê äîïèñûâàåò â
// çàãîëîâîê 1 áàéò, à ïðè APL öåëàõ 3 áàéòà.
// Òàêæå ñàì çàãîëîâîê UDP ïàêåòà âåñèò 16 áàéò.
// Òàêæå ñóùåñòâåò âòîðîé áóôåð, â êîòîðîì êîïÿòñÿ ïðèøåäøèå ïàêåòû.
// Åñëè èõ âîâðåìÿ íå âûòÿãèâàòü èç áóôåðà òî âíîâü ïðèøåäøèå
// íå íàéäóò ñåáå ìåñòà è ïðîïàäóò íàâñåãäà. :)
// Ïàêåòû âûòàñêèâàþòñÿ ïî íå êó÷åé à îòäåëüíî.
// È åù¸ îäèí íüþàíñèê! Ïîðÿäîê ñëåäîâàíèÿ ïàêåòîâ íå îáÿàòåëüíî
// áóäåò àíàëîãè÷åí ïîðÿäêó îòïðàâêè!
// Åñòü îäíà èíòåðåñíàÿ âåùü: äâèæîê ïîääåðæèâàåò øèðîêîâåùàòåëüíûå ïàêåòû
// ýòî çíà÷èò, ÷òî îòïðàâëåííûé øâ ïàêåò äîéä¸ò äî âñåõ
// êîìïüþòåðîâ â ëîêàëêå. Íî ðîóòåð òàêèå áÿêè íå ïðîïóñêàåò
// òàê ÷òî ëåòàòü òàêîé ïàêåò áóäåò ëèøü â îäíîì ñåãìåíòå ñåòè.
// Íó âðîäå âñ¸... Òåïåðü ôóíêöèè.
// NET_Clear - î÷èñòêà áóôåðà îòïðàâêè
// NET_ClearAPL - î÷èñòêà áóôåðà APL ïàêåòîâ
// NET_Write(Buf: pointer; Count: integer): boolean
// Çåïèñàòü â áóôåð îòïðàâêè íåêîòîðûå äàííûå â êîëè÷åñòâå Count áàéò
// NET_Recv(Buf: pointer; Count: integer; var IP: PChar; var Port: integer): integer
// Ïðî÷èòàòü ñëåäóþùèé ïàêåò èç áóôåðà âõîäÿùèõ :)
// Ðåçóëüòàò > 0
// NET_Send(IP: PChar; Port: WORD; APL: boolean): integer; stdcall; external EngDLL;
// Îòïðàâèòü ïàêåò íà óêàçàííûé IP è Port
// ïðè IP = nil ñîçäà¸òñÿ øâ ïàêåò (BROADCAST)
// Âíèìàíèå! Ïîñëå îòïðàâêè ïàêåòà áóôåð îòïðàâêè íå î÷èùàåòñÿ.
// Íó âîò ñîáñíà è âñ¸ =)
// ß íà 90% óâåðåí, ÷òî îøèáêè â ñåòåâîì êîäå äâèæêà åñòü,
// ò.ê. ïèñàë åãî "âñëåïóþ"!
uses
Windows, SysUtils,
Engine_Reg,
OpenGL,
Func_lib,
Type_Lib,
Graph_lib,
Arena_Lib;
const
NET_MAPBUFSIZE = 1024;
NM_SINFO = 1;
NM_RINFO = 2;
NM_CONNECT = 3;
NM_DISCONNECT = 4;
NM_MSG = 5;
NM_PING = 6;
NM_PONG = 7;
NM_PINGS = 8;
NM_BUSY = 9;
NM_INVITE = 10;
NM_ACCEPT = 11;
NM_JOIN = 12;
NM_DISJOIN = 13;
NM_GAMEINFO = 20;
NM_PLAYERS = 21;
NM_SAY = 22;
NM_ADDPLAYER = 23;
NM_DELPLAYER = 24;
NM_TEAMJOIN = 25;
NM_GAMEMSG = 30;
NM_OBJECTS = 31;
NM_HITS = 32;
NM_RESPAWN = 33;
NM_SHOTOBJ = 34; //ðàêåòû, ãðàíàòû, ïëàçìà, ÁÔÃ
NM_SHOTOBJ_KILL = 35;//ðàêåòà âçîðâàëàñü... è òîìó ïîäîáíîå
NM_FREEOBJ = 36; //âûïàäàþùèå îáúåêòû
NM_SHOT = 37;
NM_CHANGENAME = 51; //îíî æå ÷åéíäæìîäåëü è ðåéëêîëîð è âñ¸ òàêîå :)
//ñîîáùåíèÿ ïðè äèñêîííåêòå
NM_CLIENTLEAVE = 200;
NM_TIMEOUT = 201;
NM_SERVERLEAVE = 202;
NM_KICK = 203;
// ïåðåäà÷à êàðò ïî ñåòè
NM_MAP_GET = 100; // Çàïðîñ íà ñêà÷èâàíèå êàðòû
NM_MAP_SIZE = 101; // Ðàçìåð áóôåðà ïîä ñêà÷èâàåìóþ êàðòó
NM_MAP_BUF = 102; // Î÷åðåäíîé êóñîê êàðòû
NM_MAP_END = 104; // Ñîîáùåíèå îá óñïåøíîì ïðèíÿòèè êàðòû
const
//for INFO
INFO_SERVER = 1;
INFO_PLAYERS = 2;
INFO_OBJS = 4;
INFO_STATS = 8;
INFO_PHYS = 16;
INFO_START = 1;
INFO_LOAD = 30;
const
PINGSTATMAX = 10;
type
TNET_Type = (NT_NONE, NT_SERVER, NT_CLIENT);
TNP_ServerInfo = record
MaxPlayers : Byte;
Players : Byte;
Password : boolean;
MapCRC32 : DWORD;
session : word;
gametype : word;
servertime : word;
warmup : boolean;
warmuparmor : byte;
res1, res2 : word;
stopped : boolean;
end;
TNP_ConnectParams =
record
Version: byte;
NET_ID: integer;
end;
TSpectator =
record
name: string;
ping: word;
end;
TClient = class
constructor Create(IP: string; Port: word; Name_: string);
destructor Destroy; override;
private
FTime, FLastPing : DWORD;
FIP : string;
FPort : Word;
fName : string;
fStatPing: word;
pings: array [0..PINGSTATMAX-1] of word;
//ïî ïîñëåäíèì 10 ïèíãàì îïðåäåëÿåì ñðàäíåå âðåìÿ çàäåðæêè
//êàê ðåàëèçîâàòü âåñà - âåñà áóäóò îò 1.5 äî 0.5
function GetIP: PChar;
public
// äëÿ ñêà÷êè êàðò ïî ñåòè (êàæäîìó êëèåíòó ñâîé áóôåð)
map_buf : PByteArray;
map_size : integer;
///////////////
Ping : DWORD;
pTimer: byte;
players_count: byte;
procedure RecvPing;
procedure SendPing;
procedure SendPong;
procedure SendDisconnect(reason: byte);
procedure Send(APL: boolean);
function map_download: boolean;
property Name: string read fName write fName;
// XProger: íó âîò çà÷åì çäåñü çàïðåùàòü èçìåíåíèå ïèíãà? Âñå ñâîè! ;)))
property LastPing: DWORD read flastping write FLastPing;
procedure ClearPing;
property IP : string read FIP;
property IP_ : PChar read GetIP;
property Port : word read FPort;
function Valid(t: byte): boolean;
property StatPing: word read fStatPing;
end;
//NET MACHINE
// TNet_Exception = class(TException);
TNET_Machine = class
constructor Create;
destructor Destroy;override;
protected
fsocket: boolean;
fNET_ID: integer;
fType : TNet_Type;
ping_time: Cardinal;
timer: cardinal;
btimer: byte;
procedure Check_Socket;
procedure rinfo_recv(IP: string; Port:word; reason: byte = 0);
procedure msg_recv(IP: string; Port:word; len: integer);
procedure players_recv; //ïîëó÷àåò äàííûå îá èãðîêàõ
function players_write(ex_client: TClient): boolean; //ïèøåò äàííûå îá èãðîêàõ â áóôåð çàïèñè
procedure players_sending_end;
procedure say_Recv;
procedure changename_Recv;
function Getspects(ind: integer): TSpectator;virtual;
public
procedure Message_(id: Byte);
function Read(p: pointer; Length: integer): integer;
procedure ReadString(var s: string);
procedure WriteString(s: string);
procedure Disconnect;virtual;
procedure Update_Prev;virtual;
procedure Update_Next;virtual;
procedure game_Prepare;virtual;//prepare to send game messages
procedure game_Send; virtual;//send game messages!!!
procedure msg_Send(IP: string; Port: word; msg: string);
procedure pong_Send(IP: string; Port: word);
procedure sinfo_Send(IP: string; Port: word);
function Gen_ID: integer;
property NET_ID: integer read fNET_ID;
property Type_: TNet_Type read fType;
procedure say_Send(uid: byte; msg: string);virtual;
procedure changename_Send(uid: byte; name, model: string);virtual;
procedure shot_send(uid: byte); virtual;
function spects_Count: integer;virtual;
property spects[ind: integer]: TSpectator read Getspects;
end;
//***
const
NET_BufLen = 4096;
var
NET : TNet_Machine;
var
NET_Buf : PByteArray;
NET_Buf_Seek : integer;
NET_Buf_Len : integer;
NET_servermap: string;
// ÊËÈÅÍÒÛ //
function NET_Cmd(Cmd: ShortString): boolean;
procedure NET_Init;
procedure NET_Free;
procedure NET_Create(multi: boolean);
procedure NET_Update;
procedure NET_Log(msg: string);
function NET_MapDownload(const FileName: string): boolean;
function NET_game: boolean;
implementation
uses
Constants_Lib, Game_Lib, Map_Lib, Player_Lib, weapon_lib, Stat_Lib,
Menu_Lib,
NET_Local_Lib, NET_Client_Lib, NET_Server_Lib;
procedure NET_log(msg: string);
begin
if d_net_log then
Log(msg);
end;
function NET_MapDownload(const FileName: string): boolean;
(***************************************
Êðàòêîå îïèñàíèå âñåãî ýòîãî áåçîáðàçèÿ:
Èìååòñÿ âñåãî 4 ñåòåâûõ ñîîáùåíèÿ
NM_MAP_GET, NM_MAP_SIZE, NM_MAP_BUF è NM_MAP_END
NM_MAP_GET:
-Êëèåíò: NM_MAP_GET <NameLen (1)> <Name>
Ïåðåäà¸ò ñåðâåðó çàïðîñ î íàäîáíîñòè ñêà÷àòü êàðòó <Name>
NM_MAP_SIZE:
-Ñåðâåð: NM_MAP_SIZE <Size (4)>
Îòâåò îò ñåðâåðà íà NM_MAP_GET â êîòîðîì <Size> óêàçûâàåò íà îáùèé îáú¸ì ïåðåäàâàåìûõ äàííûõ
NM_MAP_BUF:
-Ñåðâåð: NM_MAP_BUF <ID (4)> <Data>
Ïàêåò èíäåêñîì ID ñîäåðæàùèé â ñåáå äàííûå <Data>
Ðàçìåð êîòîðûõ = BUF_SIZE, íî äëÿ ïîñëåäíåãî ïàêåòà ìîæåò áûòü ìåíüøå
-Êëèåíò: NM_MAP_BUF <ID (4)>
Åñëè êëèåíò íå ïîëó÷èë êàêîé-ëèáî ïàêåò ïîä íîìåðîì <ID>,
îí ñîîáùàåò îá ýòîì ñåðâåðó, êîòîðûé â ñâîþ î÷åðåäü âûøëåò íåäîñòàþùèé
êóñîê êàðòû ïîä òåì æå íîìåðîì
NM_MAP_END:
-Ñåðâåð: ïîäëî îáðóáàåò êëèåíòó ñêà÷êó êàðòû,
ýòî çíà÷èò, ÷òî ñåðâåð íå õî÷åò äàâàòü (çàïðåùàåò) ñêà÷èâàòü êàðòû
-Êëèåíò: ñîîáùàåò ñåðâåðó îá óñïåøíîì îêîí÷àíèè ïðîöåäóðû ñêà÷êè êàðòû.
Òàéìàóò ðàáîòû ñ ñåðâåðîè ñîñòàâëÿåò TIME_OUT.
Òàéìàóò íà îæèäàíèå î÷åðåäíîãî ïàêåòà îò ñåðâåðà ðàâåí Time_Buf
Ïî îêîí÷àíèè çàãðóçêè, êàðòà ñîõðàíÿåòÿ â "<TFK/TA>/maps/download/"
Âî âðåìÿ ñêà÷èâàíèÿ èãðà âõîäèò â öèêë, â ñâÿçè ñ ÷åì îñòàíàâëèâàåòñÿ îáðàáîòêà
ñîîáùåíèé Windows è ïðèëîæåíèå "ïîäâèñàåò". Â ñâÿçè ñ îðãàíèöàöèåé äâèæêà - ïî
îêîí÷àíèè çàãðóçêè êàðòû ïðèõîäèòñÿ ñáðàñûâàòü èãðîâîé òàéìåð.
Âñÿ ñêà÷êà êàðòû îðãàíèçîâàíà êàê íàäñòðîéêà íàä APL (íó âñÿêîå áûâàåò ;)
Òåîðåòè÷åñêèå ðàñ÷¸òû:
Ïðè ïèíãå = 100 ìñ, áåç ïîòåðü ïàêåòîâ
Êàðòà ðàçìåðîì â 256 êá áóäåò ñêà÷àíà ïðîäïîëîæèòåëüíî çà 7 ñåêóíä :)
Ïðè ðàçìåðå áóôåðà íà êàæäûé ïàêåò BUF_SIZE = 4096 áàéò (ðåãóëèðóåòñÿ ñåðâåðîì)
 íåèñïðàâíîñòÿõ ðàáîòû äàííîé ôóíêöèè âèíèòü XProger'à :)
***************************************)
const
TIME_OUT : cardinal = 5000;
TIME_BUF = 2000;
BUF_IDLE = 3000; // âðåìÿ îòâåä¸ííîå íà ïîëó÷åíèå çàïðîøåííîãî ïàêåòà
BUF_MAX = 5; // Ìàêñèìàëüíîå êîë-âî ïîäàâàåìûõ çàïðîñîâ
var
timeout : DWORD;
buf_map : PByteArray;
buf_count : integer;
buf_out : integer; // êîë-âî ïîäàíûõ çàïðîñîâ
buf_stat : array of DWORD; // âðåìÿ ñ ìîìåíòà îòïðàâêè çàïðîñà ïàêåòà
map_size : integer; // êîë-âî áàéò âûäåëåíîå íà áóôåð êàðòû
msg : byte;
// àäðåñ ñåðâåðà
IP : string;
Port : integer;
// àäðåñ âõîäÿùåãî ñîîáùåíèÿ
incIP : PChar;
incPort : integer;
// Âðåìåííûå ïåðåìåííûå :)
i, j : integer;
F : File;
progress : integer;
procedure DrawProgress;
var
pos : single;
begin
inc(progress);
pos := progress * 500 / buf_count;
glClear(GL_COLOR_BUFFER_BIT);
xglTex_Disable;
glBegin(GL_QUADS);
glColor3f(1, 1, 0);
glVertex2f(70, 260);
glVertex2f(70, 220);
glColor3f(1, 0, 0);
glVertex2f(70 + pos, 220);
glVertex2f(70 + pos, 260);
glEnd;
glColor3f(1, 1, 1);
glBegin(GL_LINE_STRIP);
glVertex2f(70, 260);
glVertex2f(70, 220);
glVertex2f(570, 220);
glVertex2f(570, 260);
glVertex2f(70, 260);
glEnd;
xglSwap;
end;
begin
Result := false;
if FileName = '' then Exit; // êòî âàñ òàì çíàåò... ;)
// äëÿ îòðèñîâêè ïðîãðåññáàðà
glViewport(0, 0, xglWidth, xglHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluOrtho2D(0, 640, 480, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
xglAlphaBlend(0);
Log('^3Try to download map');
buf_count := 0;
buf_stat := nil; // ñòàòèñòèêà ïðèñëàííûõ áóôåðîâ (ïðèø¸ë èëè íåò)
buf_map := nil;
map_size := 0;
try // ýòîò try - òîæå debug :)
// XProger: áëèí, ýòè 2 ñòðîêè âûëåòàþò ñ îøèáêîé!
// ãäå âîîáùå IP Port ñåðâåðà õðàíèòñÿ???
//IP := NET_Server.client[0].IP;
//Port := NET_Server.client[0].Port;
//Neoff: òû - êëèåíò! net_server òóò âîîáùå íè ïðè ÷åì!
IP := NET_Client.serv.IP;
Port := NET_Client.serv.Port;
progress := 0;
// Ïîñûëàåì çàïðîñ íà ñêà÷êó êàðòû
NET.Message_(NM_Map_Get);
NET.WriteString(FileName);
NET_Client.Serv.Send(true);
TimeOut := GetTickCount; // çàñåêàåì âðåìÿ
// ïîêà íå ïðåâûøåí êðèòè÷åñêèé ëèìèò âðåìåíè îæèäàíèÿ îòâåòà
while GetTickCount - TimeOut < Time_Out do
begin
if GetAsyncKeyState(VK_ESCAPE) <> 0 then break;
Engine_Reg.NET_Update;
NET_Buf_seek:=0;
NET_Buf_len := NET_Recv(net_buf, NET_BUFLEN, incIP, incPort);
i:=NET_Buf_len;
NET.Read(@msg, 1);
// ïðèíèìàåì ïàêåòû ïàêåò òîëüêî ñ ñåðâåðà, îñòàëüíûå âûêèäûâàåì íà ñâàëêó (íå äî íèõ ñåé÷àñ)
if (i > 0) and (Port = incPort) and (IP = StrPas(incIP)) then
begin
if map_size = 0 then // âíÿòíîãî îòâåòà îò ñåðâåðà íå ïîñòóïàëî
case msg of
// Óðÿ! Ñåðâåð ïîäàë ïðèçíàêè æèçíè, è õî÷åò ïîäåëèòüñÿ ñ íàìè...
NM_MAP_SIZE : begin
NET.Read(@map_size, 4); // Size êàðòû
GetMem(buf_map, map_size); // âûäåëÿåì ïàìÿòü ïîä áóôåð êàðòû
// âû÷èñëÿåì êîë-âî ïàêåòîâ
buf_count := (map_size - 1) div NET_MAPBUFSIZE + 1;
// ñîçäà¸ì áóôåð ñòàòèñòèêè
SetLength(buf_stat, buf_count);
FillChar(buf_stat[0], buf_count * 4, 1);
////////////// debug
Log('Map size: ' + IntToStr(map_size));
end;
NM_MAP_END : break; // ×òî-òî íå ñðîñëîñü :(
end
else
case msg of
// â i ó íàñ õðàíèòñÿ êîë-âî ïðî÷èòàíûõ áàéò
NM_MAP_BUF : begin // òî, ÷òî íàäî!
NET.Read(@j, 4); // ÷èòàåì ID ïàêåòà
if (buf_stat[j] > 0) and (j >= 0) and (j < buf_count) then
begin
buf_stat[j] := 0; // ýòè äàííûå íàì áîëüøå íå íóæíû
NET.Read(pointer(integer(buf_map)+j * NET_MAPBUFSIZE) , i - 5);
DrawProgress;
end;
end;
NM_MAP_END : break; // ÷òîá òåáÿ... :(
end;
TimeOut := GetTickCount; // ò.ê. ïðèøëîñ ñîîáùåíèå îò ñåðâåðà
end;
// ñ÷èòàåì êîë-âî äåéñòâóþùèõ çàïðîñîâ
buf_out := 0;
for j := 0 to buf_count - 1 do
if buf_stat[j] > 1 then // çàïðàøèâàåòñÿ
if GetTickCount - buf_stat[j] > BUF_IDLE then // ïðåâûøåí èíòåðâàë çàïðîñà (APL ÏÐÎÏÀË!!!)
buf_stat[j] := 1 // òàêîå ìîæåò ñëó÷èòüñÿ ïðè net_apl_trys ïîòåðÿõ ïîäðÿä, ÷òî ìàëîâåðîÿòíî ;)
else
inc(buf_out);
// ðàññûëêà çàïðîñîâ
if buf_out < BUF_MAX then // åñòü ìåñòî äëÿ î÷åðåäíîãî çàïðîñà
for j := 0 to buf_count - 1 do
if buf_stat[j] = 1 then // îæèäàåò çàïðîñà
begin
buf_stat[j] := GetTickCount;
// ôîðìèðóåì ïàêåò çàïðîñà
NET.Message_(NM_MAP_BUF);
NET_Write(@j, 4);
NET_Client.Serv.Send(true);
inc(buf_out);
if buf_out = BUF_MAX then // ìåñò íåò! :)
break;
end;
// Åñëè íåò çàïðîñîâ - çíà÷èò âñ¸ óæå ïîëó÷åíî
if (buf_out = 0) and (map_size>0) then
begin
// ãîâîðèì ñïàñèáî ñåðâåðó ;)
NET.Message_(NM_MAP_END);
NET_Client.Serv.Send(true);
Result := true;
break;
end;
end;
if Result then
begin
Log('^3Saving downloaded map to ^7"' + ExtractFileName(FileName) + '^7"');
CreateDir(Engine_ModDir + 'maps\download');
try
AssignFile(F, Engine_ModDir + 'maps\download\' + ExtractFileName(FileName)+'.tm');
Rewrite(F, 1);
BlockWrite(F, buf_map^, map_size);
CloseFile(F);
except
Log('^1Can''t save map ^7"maps\download\' + ExtractFileName(FileName) + '^7"')
end;
end
else
Log('^1Can''t download map ^7"' + ExtractFileName(FileName) + '^7"');
except
Log('aaaa');
end;
// ÷èñòèìñÿ
buf_stat := nil;
if buf_map <> nil then
FreeMem(buf_map);
// ñáðîñòèòü î÷åðåäü Update â äâèæêå
Engine_FlushTimer;
end;
function NET_game: boolean;
begin
Result:=NET.Type_<>NT_NONE;
end;
procedure NET_Init;
begin
GetMem(NET_Buf, NET_BufLen);
net_Local:=TLocal_Machine.Create;
NET:=net_Local;
Console_CmdReg('net_search', @NET_Cmd);
Console_CmdReg('net_send', @NET_Cmd);
Console_CmdReg('net_info', @NET_Cmd);
Console_CmdReg('net_invite', @NET_Cmd);
Console_CmdReg('net_connect', @NET_Cmd);
Console_CmdReg('net_disconnect', @NET_Cmd); //èìèòèðóåò ping timeout :)
Console_CmdReg('net_reconnect', @NET_Cmd);
Console_CmdReg('net_join', @NET_Cmd);
Console_CmdReg('net_p2join', @NET_Cmd);
Console_CmdReg('net_disjoin', @NET_Cmd);
Console_CmdReg('net_showclients', @NET_Cmd);
Console_CmdReg('net_kick', @NET_Cmd);
Console_CmdReg('arena_address', @NET_Cmd);
end;
procedure NET_Free;
begin
FreeMem(NET_Buf);
NET.Free;
end;
procedure NET_Create(multi: boolean);
begin
if multi then
NET_Log('^3NET_Create Multiplayer')
else NET_Log('^3NET_Create Hotseat');
if multi then
begin
if (NET.Type_<>NT_Server) then
begin
NET_ClearAPL;
NET_Server:=TServer_Machine.Create;
Map.session_number:=0;
Arena_Ping;
end
end
else
if (NET.Type_<>NT_NONE) then
NET_Local:=TLocal_Machine.Create;
end;
procedure NET_Update;
begin
NET.Update_Prev;
end;
function NET_Cmd(Cmd: ShortString): boolean;
var
par : array [1..3] of string;
i : integer;
str, str_ : string;
b: boolean;
begin
Result := true;
str := Func_Lib.LowerCase(trim(cmd));
str_ := str;
for i := 1 to 2 do
par[i] := StrSpace(str);
par[3]:=str;
if par[1] = 'net_search' then
begin
i := StrToInt(par[2]);
if i = 0 then
i := 25666;
NET.Message_(NM_SINFO);
NET_Send(nil, i, false);
NET.Ping_Time := GetTickCount;
Exit;
end;
if par[1] = 'net_info' then
begin
if par[2]<>'' then
begin
if pos(':', par[2]) <> 0 then
begin
str := Copy(par[2], 1, pos(':', par[2]) - 1);
str_ := Copy(par[2], pos(':', par[2]) + 1, Length(par[2]));
end
else
begin
str := par[2];
str_ := '25666';
end;
i := StrToInt(str_);
NET.sinfo_Send(str, i);
end;
Exit;
end;
if par[1] = 'net_invite' then
begin
if (par[2]<>'') and (NET.Type_=NT_SERVER) then
begin
if pos(':', par[2]) <> 0 then
begin
str := Copy(par[2], 1, pos(':', par[2]) - 1);
str_ := Copy(par[2], pos(':', par[2]) + 1, Length(par[2]));
end
else
begin
str := par[2];
str_ := '25666';
end;
i := StrToInt(str_);
NET_Server.Invite_Send(str, i);
Log('^1Invite send to '+str+':'+inttostr(i));
end else if (NET.Type_<>NT_SERVER) then
begin
Log('^1Only server can send Invite!');
end;
Exit;
end;
if par[1] = 'net_send' then
begin
if par[3] <> '' then
begin
if pos(':', par[2]) <> 0 then
begin
str := Copy(par[2], 1, pos(':', par[2]) - 1);
str_ := Copy(par[2], pos(':', par[2]) + 1, Length(par[2]));
end
else
begin
str := par[2];
str_ := '25666';
end;
i := StrToInt(str_);
NET.msg_Send(str, i, par[3]);
end;
Exit;
end;
if par[1] = 'net_connect' then
begin
if par[2] <> '' then
begin
if pos(':', par[2]) <> 0 then
begin
str := Copy(par[2], 1, pos(':', par[2]) - 1);
str_ := Copy(par[2], pos(':', par[2]) + 1, Length(par[2]));
end
else
begin
str := par[2];
str_ := '25666';
end;
i := StrToInt(str_);
if NET.Type_=NT_NONE then
begin
net_Local.Connect(str, i);
end
else
begin
//log
end;
end;
Exit;
end;
if par[1] = 'net_disconnect' then
begin
if NET.Type_=NT_CLIENT then
begin
NET_Local:=TLocal_Machine.Create;
Map.ClearAll;
Log('^2Disconnected');
end;
Result:=true;
Exit;
end;
if par[1] = 'net_reconnect' then
begin
if NET.Type_=NT_CLIENT then
begin
b:=net_debug_disconnect;
net_debug_disconnect:=true;
str:=NET_Client.Serv.IP;
i:=NET_Client.Serv.Port;
NET_Local:=TLocal_Machine.Create;
Map.ClearAll;
net_Local.Connect(str, i);
net_debug_disconnect:=b;
end;
Result:=true;
Exit;
end;
if par[1] = 'net_join' then
begin
if NET.Type_=NT_CLIENT then
begin
if Map.pl_find(-1, C_PLAYER_P1) then
Log('^2You are already in game')
else NET_Client.join_send(C_PLAYER_p1, p1name, p1model);
end else Log('^2This command only for Client machine');
Exit;
end;
if par[1] = 'net_p2join' then
begin
if NET.Type_=NT_CLIENT then
begin
if Map.pl_find(-1, C_PLAYER_P2) then
Log('^2You are already in game')
else NET_Client.join_send(C_PLAYER_p2, p2name, p2model);
end else Log('^2This command only for Client machine');
Exit;
end;
if par[1] = 'net_disjoin' then
begin
if NET.Type_=NT_CLIENT then
begin
NET_Client.disjoin_send(C_PLAYER_ALL);
end else Log('^2This command only for Client machine');
Exit;
end;
if par[1] = 'arena_address' then
begin
if trim(par[2]) = '' then
Log('^3arena_address is ^7"^b' + arena_address + '^n^7"')
else
begin
arena_address := trim(Copy(cmd, Length(par[1]) + 1, Length(cmd)));
cfgProc('arena_address ' + arena_address);
Log('^2arena_address changed to ^7"^b' + arena_address + '^n^7"');
end;
Exit;
end;
if par[1] = 'net_showclients' then
begin
Result:=true;
if NET.Type_=NT_SERVER then
with NET_Server do
begin
Log(' ^3 server has ^7'+IntToStr(client_count)+' ^3clients');
for i:=0 to client_Count-1 do
begin
Log('^3 '+inttostr(i+1)+': '+client[i].Name+' has ^1'+inttostr(client[i].players_count)+'^3 players');
if Map.pl_find_NET(-1, client[i]) then
repeat
Log('^3 player - ^7'+Map.pl_current.Name);
until not Map.pl_find_NETnext(-1, client[i]);
end;
end else Log('^2Server-side command');
Exit;
end;
if par[1] = 'net_kick' then
begin
Result:=true;
if NET.Type_=NT_SERVER then
with NET_Server do
begin
if (par[2]='') then
Log('^3 net_kick CLIENT_ID (client_id see in net_showclients command')
else
begin
i:=StrToInt(par[2]);
if (i>0) and (i<=client_count) then
begin
NET_Server.client_Delete(i-1, NM_KICK);
Log(' ^2Client kicked');
end else Log('^2 Wrong parameter');
end;
end else Log('^2Server-side command');
Exit;
end;
Result := false;
end;
{ TClient }
procedure TClient.ClearPing;
begin
FillChar(pings, sizeof(pings), 0);
fStatPing:=0;
fTime:=GetTickCount;
fLastPing:=GetTickCount;
end;
constructor TClient.Create(IP: string; Port: word; Name_: string);
begin
FIP := IP;
FPort := Port;
fName := Name_;
ClearPing;
map_buf := nil;
map_size := 0;
end;
destructor TClient.Destroy;
begin
if map_buf <> nil then
FreeMem(map_buf);
end;
function TClient.GetIP: PChar;
begin
Result:=PChar(FIP);
end;
function TClient.map_download: boolean;
begin
Result:=map_buf<>nil;
end;
procedure TClient.RecvPing;
var
i: integer;
begin
Ping := GetTickCount - FTime;
FTime := GetTickCount;
FLastPing:=GetTickCount;
Move(pings[1], pings[0], sizeof(pings)-sizeof(word));
pings[high(pings)]:=Ping;
fStatPing:=0;
for i:=low(pings) to high(pings) do
fStatPing:=fStatPing+round( (i / (high(pings)+1) + 1/2 )* Pings[i]);
if pings[0]>0 then
fStatPing:=fStatPing div (high(pings)+1);
end;
procedure TClient.Send(APL: boolean);
begin
NET_Send(PChar(fIP), fPort, APL);
end;
procedure TClient.SendDisconnect(reason: byte);
begin
if reason<200 then reason:=NM_CLIENTLEAVE;
NET.Message_(NM_Disconnect);
NET_Write(@reason, 1);
NET_Send(PChar(IP), Port, true);
end;
procedure TClient.SendPing;
begin
FTime:=GetTickCount;
NET.Message_(NM_PING);
Send(false);
end;
procedure TClient.SendPong;
begin
NET.Message_(NM_PONG);
Send(false);
end;
function TClient.Valid(t: byte): boolean;
var
t1, t2: smallint;
begin
Result:=false;
t1:=pTimer;t2:=t;
t1:=t2-t1;
while t1>128 do t1:=t1-256;
if (t1>0) or (t1<-32) then
begin
pTimer:=t;
Result:=true;
end;
end;
{ TNET_Machine }
constructor TNET_Machine.Create;
begin
Gen_ID;
timer:=0;
if NET<>nil then
NET.Free;
NET:=Self;
end;
destructor TNET_Machine.Destroy;
begin
NET_ClearAPL;
//abstract
end;
function TNET_Machine.Gen_ID: integer;
begin
fNET_ID:=random(maxint);
Result:=fNET_ID;
end;
procedure TNET_Machine.msg_Send(IP: string; Port: word; msg: string);
begin
Check_Socket;
Message_(NM_MSG);
if length(msg)<255 then
begin
NET_Write(@msg[1], length(msg));
NET_Send(PChar(IP), Port, true);
end;
end;
procedure TNET_Machine.game_Prepare;
begin
//abstract
end;
procedure TNET_Machine.game_Send;
begin
//abstract
end;
procedure TNET_Machine.Update_Next;