1 (* Copyright (C) Doom 2D: Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE ../shared/a_modes.inc}
21 e_log
, e_msg
, utils
, ENet
, Classes
, md5
, MAPDEF
{$IFDEF USE_MINIUPNPC}, miniupnpc
;{$ELSE};{$ENDIF}
24 NET_PROTOCOL_VER
= 185;
30 NET_CHAN_IMPORTANT
= 1;
33 NET_CHAN_PLAYERPOS
= 4;
35 NET_CHAN_MONSTERPOS
= 6;
36 NET_CHAN_LARGEDATA
= 7;
38 NET_CHAN_DOWNLOAD
= 9;
40 NET_CHAN_DOWNLOAD_EX
= 11;
47 NET_PING_PORT
= $DF2D;
54 NET_DISC_NONE
: enet_uint32
= 0;
55 NET_DISC_PROTOCOL
: enet_uint32
= 1;
56 NET_DISC_VERSION
: enet_uint32
= 2;
57 NET_DISC_FULL
: enet_uint32
= 3;
58 NET_DISC_KICK
: enet_uint32
= 4;
59 NET_DISC_DOWN
: enet_uint32
= 5;
60 NET_DISC_PASSWORD
: enet_uint32
= 6;
61 NET_DISC_TEMPBAN
: enet_uint32
= 7;
62 NET_DISC_BAN
: enet_uint32
= 8;
63 NET_DISC_MAX
: enet_uint32
= 8;
64 NET_DISC_FILE_TIMEOUT
: enet_uint32
= 13;
70 NET_CONNECT_TIMEOUT
= 1000 * 10;
72 BANLIST_FILENAME
= 'banlist.txt';
73 NETDUMP_FILENAME
= 'netdump';
76 TNetMapResourceInfo
= record
77 wadName
: AnsiString; // wad file name, without a path
78 size
: Integer; // wad file size (-1: size and hash are not known)
79 hash
: TMD5Digest
; // wad hash
82 TNetMapResourceInfoArray
= array of TNetMapResourceInfo
;
84 TNetFileTransfer
= record
88 size
: Integer; // file size in bytes
90 lastSentChunk
: Integer;
91 lastAckChunk
: Integer;
92 lastAckTime
: Int64; // msecs; if not "in progress", we're waiting for the first ack
94 diskBuffer
: PChar; // of `chunkSize` bytes
104 RequestedFullUpdate
: Boolean;
105 WaitForFirstSpawn
: Boolean; // set to `true` in server, used to spawn a player on first full state request
108 Transfer
: TNetFileTransfer
; // only one transfer may be active
109 NetOut
: array [0..1] of TMsg
;
115 pTNetClient
= ^TNetClient
;
117 AByte
= array of Byte;
120 NetInitDone
: Boolean = False;
121 NetMode
: Byte = NET_NONE
;
122 NetDump
: Boolean = False;
124 NetServerName
: string = 'Unnamed Server';
125 NetPassword
: string = '';
126 NetPort
: Word = 25666;
128 NetAllowRCON
: Boolean = False;
129 NetRCONPassword
: string = '';
131 NetTimeToUpdate
: Cardinal = 0;
132 NetTimeToReliable
: Cardinal = 0;
133 NetTimeToMaster
: Cardinal = 0;
135 NetHost
: pENetHost
= nil;
136 NetPeer
: pENetPeer
= nil;
138 NetAddr
: ENetAddress
;
140 NetPongAddr
: ENetAddress
;
141 NetPongSock
: ENetSocket
= ENET_SOCKET_NULL
;
143 NetUseMaster
: Boolean = True;
144 NetMasterList
: string = 'mpms.doom2d.org:25665, deadsoftware.ru:25665';
146 NetClientIP
: string = '127.0.0.1';
147 NetClientPort
: Word = 25666;
150 NetBuf
: array [0..1] of TMsg
;
152 NetClients
: array of TNetClient
;
153 NetClientCount
: Byte = 0;
154 NetMaxClients
: Byte = 255;
155 NetBannedHosts
: array of TBanRecord
;
157 NetState
: Integer = NET_STATE_NONE
;
159 NetMyID
: Integer = -1;
160 NetPlrUID1
: Integer = -1;
161 NetPlrUID2
: Integer = -1;
163 NetInterpLevel
: Integer = 1;
164 NetUpdateRate
: Cardinal = 0; // as soon as possible
165 NetRelupdRate
: Cardinal = 18; // around two times a second
166 NetMasterRate
: Cardinal = 60000;
168 NetForcePlayerUpdate
: Boolean = False;
169 NetPredictSelf
: Boolean = True;
170 NetForwardPorts
: Boolean = False;
172 NetGotEverything
: Boolean = False;
173 NetGotKeys
: Boolean = False;
175 {$IFDEF USE_MINIUPNPC}
176 NetPortForwarded
: Word = 0;
177 NetPongForwarded
: Boolean = False;
178 NetIGDControl
: AnsiString;
179 NetIGDService
: TURLStr
;
182 NetPortThread
: TThreadID
= NilThreadId
;
184 NetDumpFile
: TStream
;
186 g_Res_received_map_start
: Integer = 0; // set if we received "map change" event
189 function g_Net_Init(): Boolean;
190 procedure g_Net_Cleanup();
191 procedure g_Net_Free();
192 procedure g_Net_Flush();
194 function g_Net_Host(IPAddr
: LongWord; Port
: enet_uint16
; MaxClients
: Cardinal = 16): Boolean;
195 procedure g_Net_Host_Die();
196 procedure g_Net_Host_Send(ID
: Integer; Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
197 function g_Net_Host_Update(): enet_size_t
;
199 function g_Net_Connect(IP
: string; Port
: enet_uint16
): Boolean;
200 procedure g_Net_Disconnect(Forced
: Boolean = False);
201 procedure g_Net_Client_Send(Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
202 function g_Net_Client_Update(): enet_size_t
;
203 function g_Net_Client_UpdateWhileLoading(): enet_size_t
;
205 function g_Net_Client_ByName(Name
: string): pTNetClient
;
206 function g_Net_Client_ByPlayer(PID
: Word): pTNetClient
;
207 function g_Net_ClientName_ByID(ID
: Integer): string;
209 procedure g_Net_SendData(Data
: AByte
; peer
: pENetPeer
; Reliable
: Boolean; Chan
: Byte = NET_CHAN_DOWNLOAD
);
210 //function g_Net_Wait_Event(msgId: Word): TMemoryStream;
211 //function g_Net_Wait_FileInfo (var tf: TNetFileTransfer; asMap: Boolean; out resList: TStringList): Integer;
213 function IpToStr(IP
: LongWord): string;
214 function StrToIp(IPstr
: string; var IP
: LongWord): Boolean;
216 function g_Net_IsHostBanned(IP
: LongWord; Perm
: Boolean = False): Boolean;
217 procedure g_Net_BanHost(IP
: LongWord; Perm
: Boolean = True); overload
;
218 procedure g_Net_BanHost(IP
: string; Perm
: Boolean = True); overload
;
219 function g_Net_UnbanHost(IP
: string): Boolean; overload
;
220 function g_Net_UnbanHost(IP
: LongWord): Boolean; overload
;
221 procedure g_Net_UnbanNonPermHosts();
222 procedure g_Net_SaveBanList();
224 procedure g_Net_DumpStart();
225 procedure g_Net_DumpSendBuffer();
226 procedure g_Net_DumpRecvBuffer(Buf
: penet_uint8
; Len
: LongWord);
227 procedure g_Net_DumpEnd();
229 function g_Net_ForwardPorts(ForwardPongPort
: Boolean = True): Boolean;
230 procedure g_Net_UnforwardPorts();
232 function g_Net_UserRequestExit
: Boolean;
234 function g_Net_Wait_MapInfo (var tf
: TNetFileTransfer
; var resList
: TNetMapResourceInfoArray
): Integer;
235 function g_Net_RequestResFileInfo (resIndex
: LongInt; out tf
: TNetFileTransfer
): Integer;
236 function g_Net_AbortResTransfer (var tf
: TNetFileTransfer
): Boolean;
237 function g_Net_ReceiveResourceFile (resIndex
: LongInt; var tf
: TNetFileTransfer
; strm
: TStream
): Integer;
239 function g_Net_IsNetworkAvailable (): Boolean;
240 procedure g_Net_InitLowLevel ();
241 procedure g_Net_DeinitLowLevel ();
243 procedure NetServerCVars(P
: SSArray
);
248 // *enet_host_service()*
249 // fuck! https://www.mail-archive.com/enet-discuss@cubik.org/msg00852.html
250 // tl;dr: on shitdows, we can get -1 sometimes, and it is *NOT* a failure.
251 // thank you, enet. let's ignore failures altogether then.
256 g_nethandler
, g_netmsg
, g_netmaster
, g_player
, g_window
, g_console
,
257 g_main
, g_game
, g_language
, g_weapons
, ctypes
, g_system
, g_map
;
260 FILE_CHUNK_SIZE
= 8192;
263 enet_init_success
: Boolean = false;
264 g_Net_DownloadTimeout
: Single;
268 function g_Net_IsNetworkAvailable (): Boolean;
270 result
:= enet_init_success
;
273 procedure g_Net_InitLowLevel ();
275 if enet_init_success
then raise Exception
.Create('wuta?!');
276 enet_init_success
:= (enet_initialize() = 0);
279 procedure g_Net_DeinitLowLevel ();
281 if enet_init_success
then
284 enet_init_success
:= false;
289 //**************************************************************************
293 //**************************************************************************
295 procedure clearNetClientTransfers (var nc
: TNetClient
);
297 nc
.Transfer
.stream
.Free
;
298 nc
.Transfer
.diskName
:= ''; // just in case
299 if (nc
.Transfer
.diskBuffer
<> nil) then FreeMem(nc
.Transfer
.diskBuffer
);
300 nc
.Transfer
.stream
:= nil;
301 nc
.Transfer
.diskBuffer
:= nil;
305 procedure clearNetClient (var nc
: TNetClient
);
307 clearNetClientTransfers(nc
);
311 procedure clearNetClients (clearArray
: Boolean);
315 for f
:= Low(NetClients
) to High(NetClients
) do clearNetClient(NetClients
[f
]);
316 if (clearArray
) then SetLength(NetClients
, 0);
320 function g_Net_UserRequestExit (): Boolean;
322 Result
:= {e_KeyPressed(IK_SPACE) or}
323 e_KeyPressed(IK_ESCAPE
) or
324 e_KeyPressed(VK_ESCAPE
) or
325 e_KeyPressed(JOY0_JUMP
) or
326 e_KeyPressed(JOY1_JUMP
) or
327 e_KeyPressed(JOY2_JUMP
) or
328 e_KeyPressed(JOY3_JUMP
)
332 //**************************************************************************
334 // file transfer declaraions and host packet processor
336 //**************************************************************************
339 // server packet type
340 NTF_SERVER_DONE
= 10; // done with this file
341 NTF_SERVER_FILE_INFO
= 11; // sent after client request
342 NTF_SERVER_CHUNK
= 12; // next chunk; chunk number follows
343 NTF_SERVER_ABORT
= 13; // server abort
344 NTF_SERVER_MAP_INFO
= 14;
346 // client packet type
347 NTF_CLIENT_MAP_REQUEST
= 100; // map file request; also, returns list of additional wads to download
348 NTF_CLIENT_FILE_REQUEST
= 101; // resource file request (by index)
349 NTF_CLIENT_ABORT
= 102; // do not send requested file, or abort current transfer
350 NTF_CLIENT_START
= 103; // start transfer; client may resume download by sending non-zero starting chunk
351 NTF_CLIENT_ACK
= 104; // chunk ack; chunk number follows
354 // disconnect client due to some file transfer error
355 procedure killClientByFT (var nc
: TNetClient
);
357 e_LogWritefln('disconnected client #%d due to file transfer error', [nc
.ID
], TMsgType
.Warning
);
358 enet_peer_disconnect(nc
.Peer
, NET_DISC_FILE_TIMEOUT
);
359 clearNetClientTransfers(nc
);
360 g_Net_Slist_ServerPlayerLeaves();
364 // send file transfer message from server to client
365 function ftransSendServerMsg (var nc
: TNetClient
; var m
: TMsg
): Boolean;
370 if (m
.CurSize
< 1) then exit
;
371 pkt
:= enet_packet_create(m
.Data
, m
.CurSize
, ENET_PACKET_FLAG_RELIABLE
);
372 if not Assigned(pkt
) then begin killClientByFT(nc
); exit
; end;
373 if (enet_peer_send(nc
.Peer
, NET_CHAN_DOWNLOAD_EX
, pkt
) <> 0) then begin killClientByFT(nc
); exit
; end;
378 // send file transfer message from client to server
379 function ftransSendClientMsg (var m
: TMsg
): Boolean;
384 if (m
.CurSize
< 1) then exit
;
385 pkt
:= enet_packet_create(m
.Data
, m
.CurSize
, ENET_PACKET_FLAG_RELIABLE
);
386 if not Assigned(pkt
) then exit
;
387 if (enet_peer_send(NetPeer
, NET_CHAN_DOWNLOAD_EX
, pkt
) <> 0) then exit
;
393 procedure ProcessChunkSend (var nc
: TNetClient
);
395 tf
: ^TNetFileTransfer
;
401 if (tf
.stream
= nil) then exit
;
403 // arbitrary timeout number
404 if (ct
-tf
.lastAckTime
>= 5000) then
409 // check if we need to send something
410 if (not tf
.inProgress
) then exit
; // waiting for the initial ack
411 // ok, we're sending chunks
412 if (tf
.lastAckChunk
<> tf
.lastSentChunk
) then exit
;
413 Inc(tf
.lastSentChunk
);
414 // do it one chunk at a time; client ack will advance our chunk counter
415 chunks
:= (tf
.size
+tf
.chunkSize
-1) div tf
.chunkSize
;
417 if (tf
.lastSentChunk
> chunks
) then
424 if (tf
.lastSentChunk
= chunks
) then
426 // we're done with this file
427 e_LogWritefln('download: client #%d, DONE sending chunks #%d/#%d', [nc
.ID
, tf
.lastSentChunk
, chunks
]);
428 trans_omsg
.Write(Byte(NTF_SERVER_DONE
));
429 clearNetClientTransfers(nc
);
434 trans_omsg
.Write(Byte(NTF_SERVER_CHUNK
));
435 trans_omsg
.Write(LongInt(tf
.lastSentChunk
));
437 rd
:= tf
.size
-(tf
.lastSentChunk
*tf
.chunkSize
);
438 if (rd
> tf
.chunkSize
) then rd
:= tf
.chunkSize
;
439 trans_omsg
.Write(LongInt(rd
));
440 //e_LogWritefln('download: client #%d, sending chunk #%d/#%d (%d bytes)', [nc.ID, tf.lastSentChunk, chunks, rd]);
441 //FIXME: check for errors here
443 tf
.stream
.Seek(tf
.lastSentChunk
*tf
.chunkSize
, soFromBeginning
);
444 tf
.stream
.ReadBuffer(tf
.diskBuffer
^, rd
);
445 trans_omsg
.WriteData(tf
.diskBuffer
, rd
);
452 ftransSendServerMsg(nc
, trans_omsg
);
456 // server file transfer packet processor
457 // received packet is in `NetEvent`
458 procedure ProcessDownloadExPacket ();
465 tf
: ^TNetFileTransfer
;
475 // find client index by peer
476 for f
:= Low(NetClients
) to High(NetClients
) do
478 if (not NetClients
[f
].Used
) then continue
;
479 if (NetClients
[f
].Peer
= NetEvent
.peer
) then
485 //e_LogWritefln('RECEIVE: dlpacket; client=%d (datalen=%u)', [nid, NetEvent.packet^.dataLength]);
487 if (nid
< 0) then exit
; // wtf?!
488 nc
:= @NetClients
[nid
];
490 if (NetEvent
.packet
^.dataLength
= 0) then
496 tf
:= @NetClients
[nid
].Transfer
;
497 tf
.lastAckTime
:= GetTimerMS();
499 cmd
:= Byte(NetEvent
.packet
^.data
^);
500 //e_LogWritefln('RECEIVE: nid=%d; cmd=%u', [nid, cmd]);
502 NTF_CLIENT_FILE_REQUEST
: // file request
504 if (tf
.stream
<> nil) then
509 if (NetEvent
.packet
^.dataLength
< 2) then
514 // new transfer request; build packet
515 if not msg
.Init(NetEvent
.packet
^.data
+1, NetEvent
.packet
^.dataLength
-1, True) then
520 // get resource index
521 ridx
:= msg
.ReadLongInt();
522 if (ridx
< -1) or (ridx
>= length(gExternalResources
)) then
524 e_LogWritefln('Invalid resource index %d', [ridx
], TMsgType
.Warning
);
528 if (ridx
< 0) then fname
:= gGameSettings
.WAD
else fname
:= gExternalResources
[ridx
].diskName
;
529 if (length(fname
) = 0) then
531 e_WriteLog('Invalid filename: '+fname
, TMsgType
.Warning
);
535 tf
.diskName
:= findDiskWad(fname
);
536 if (length(tf
.diskName
) = 0) then
538 e_LogWritefln('NETWORK: file "%s" not found!', [fname
], TMsgType
.Fatal
);
543 //tf.hash := MD5File(tf.diskName);
544 if (ridx
< 0) then tf
.hash
:= gWADHash
else tf
.hash
:= gExternalResources
[ridx
].hash
;
545 // create file stream
546 tf
.diskName
:= findDiskWad(fname
);
548 tf
.stream
:= openDiskFileRO(tf
.diskName
);
552 if (tf
.stream
= nil) then
554 e_WriteLog(Format('NETWORK: file "%s" not found!', [fname
]), TMsgType
.Fatal
);
558 e_LogWritefln('client #%d requested resource #%d (file is `%s` : `%s`)', [nc
.ID
, ridx
, fname
, tf
.diskName
]);
559 tf
.size
:= tf
.stream
.size
;
560 tf
.chunkSize
:= FILE_CHUNK_SIZE
; // arbitrary
561 tf
.lastSentChunk
:= -1;
562 tf
.lastAckChunk
:= -1;
563 tf
.lastAckTime
:= GetTimerMS();
564 tf
.inProgress
:= False; // waiting for the first ACK or for the cancel
565 GetMem(tf
.diskBuffer
, tf
.chunkSize
);
566 // sent file info message
568 trans_omsg
.Write(Byte(NTF_SERVER_FILE_INFO
));
569 trans_omsg
.Write(tf
.hash
);
570 trans_omsg
.Write(tf
.size
);
571 trans_omsg
.Write(tf
.chunkSize
);
572 trans_omsg
.Write(ExtractFileName(fname
));
573 if not ftransSendServerMsg(nc
^, trans_omsg
) then exit
;
575 NTF_CLIENT_ABORT
: // do not send requested file, or abort current transfer
577 e_LogWritefln('client #%d aborted file transfer', [nc
.ID
]);
578 clearNetClientTransfers(nc
^);
580 NTF_CLIENT_START
: // start transfer; client may resume download by sending non-zero starting chunk
582 if not Assigned(tf
.stream
) then
587 if (tf
.lastSentChunk
<> -1) or (tf
.lastAckChunk
<> -1) or (tf
.inProgress
) then
589 // double ack, get lost
593 if (NetEvent
.packet
^.dataLength
< 2) then
599 if not msg
.Init(NetEvent
.packet
^.data
+1, NetEvent
.packet
^.dataLength
-1, True) then
604 chunk
:= msg
.ReadLongInt();
605 if (chunk
< 0) or (chunk
> (tf
.size
+tf
.chunkSize
-1) div tf
.chunkSize
) then
610 e_LogWritefln('client #%d started file transfer from chunk %d', [nc
.ID
, chunk
]);
611 // start sending chunks
612 tf
.inProgress
:= True;
613 tf
.lastSentChunk
:= chunk
-1;
614 tf
.lastAckChunk
:= chunk
-1;
615 ProcessChunkSend(nc
^);
617 NTF_CLIENT_ACK
: // chunk ack; chunk number follows
619 if not Assigned(tf
.stream
) then
624 if (tf
.lastSentChunk
< 0) or (not tf
.inProgress
) then
626 // double ack, get lost
630 if (NetEvent
.packet
^.dataLength
< 2) then
636 if not msg
.Init(NetEvent
.packet
^.data
+1, NetEvent
.packet
^.dataLength
-1, True) then
641 chunk
:= msg
.ReadLongInt();
642 if (chunk
< 0) or (chunk
> (tf
.size
+tf
.chunkSize
-1) div tf
.chunkSize
) then
647 // do it this way, so client may seek, or request retransfers for some reason
648 tf
.lastAckChunk
:= chunk
;
649 tf
.lastSentChunk
:= chunk
;
650 //e_LogWritefln('client #%d acked file transfer chunk %d', [nc.ID, chunk]);
651 ProcessChunkSend(nc
^);
653 NTF_CLIENT_MAP_REQUEST
:
655 e_LogWritefln('client #%d requested map info', [nc
.ID
]);
657 dfn
:= findDiskWad(gGameSettings
.WAD
);
658 if (dfn
= '') then dfn
:= '!wad_not_found!.wad'; //FIXME
659 //md5 := MD5File(dfn);
661 if (not GetDiskFileInfo(dfn
, fi
)) then
663 e_LogWritefln('client #%d requested map info, but i cannot get file info', [nc
.ID
]);
669 st := openDiskFileRO(dfn);
670 if not assigned(st) then exit; //wtf?!
675 trans_omsg
.Write(Byte(NTF_SERVER_MAP_INFO
));
677 trans_omsg
.Write(ExtractFileName(gGameSettings
.WAD
));
679 trans_omsg
.Write(md5
);
681 trans_omsg
.Write(size
);
682 // number of external resources for map
683 trans_omsg
.Write(LongInt(length(gExternalResources
)));
684 // external resource names
685 for f
:= 0 to High(gExternalResources
) do
688 //trans_omsg.Write(ExtractFileName(gExternalResources[f])); // GameDir+'/wads/'+ResList.Strings[i]
690 trans_omsg
.Write('!');
691 trans_omsg
.Write(LongInt(gExternalResources
[f
].size
));
692 trans_omsg
.Write(gExternalResources
[f
].hash
);
693 trans_omsg
.Write(ExtractFileName(gExternalResources
[f
].diskName
));
696 if not ftransSendServerMsg(nc
^, trans_omsg
) then exit
;
707 //**************************************************************************
709 // file transfer crap (both client and server)
711 //**************************************************************************
713 function getNewTimeoutEnd (): Int64;
715 result
:= GetTimerMS();
716 if (g_Net_DownloadTimeout
<= 0) then
718 result
:= result
+1000*60*3; // 3 minutes
722 result
:= result
+trunc(g_Net_DownloadTimeout
*1000);
727 // send map request to server, and wait for "map info" server reply
729 // returns `false` on error or user abort
731 // diskName: map wad file name (without a path)
732 // hash: map wad hash
733 // size: map wad size
734 // chunkSize: set too
735 // resList: list of resource wads
741 // for maps, first `tf.diskName` name will be map wad name, and `tf.hash`/`tf.size` will contain map info
742 function g_Net_Wait_MapInfo (var tf
: TNetFileTransfer
; var resList
: TNetMapResourceInfoArray
): Integer;
748 freePacket
: Boolean = false;
753 ri
: ^TNetMapResourceInfo
;
755 SetLength(resList
, 0);
759 trans_omsg
.Write(Byte(NTF_CLIENT_MAP_REQUEST
));
760 if not ftransSendClientMsg(trans_omsg
) then begin result
:= -1; exit
; end;
762 FillChar(ev
, SizeOf(ev
), 0);
765 ett
:= getNewTimeoutEnd();
767 status
:= enet_host_service(NetHost
, @ev
, 300);
771 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' network error', True);
776 if (status
<= 0) then
782 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' timeout reached', True);
791 ENET_EVENT_TYPE_RECEIVE
:
794 if (ev
.channelID
<> NET_CHAN_DOWNLOAD_EX
) then
796 //e_LogWritefln('g_Net_Wait_MapInfo: skip message from non-transfer channel', []);
798 g_Net_Client_HandlePacket(ev
.packet
, g_Net_ClientLightMsgHandler
);
799 if (g_Res_received_map_start
< 0) then begin result
:= -666; exit
; end;
803 ett
:= getNewTimeoutEnd();
804 if (ev
.packet
.dataLength
< 1) then
806 e_LogWritefln('g_Net_Wait_MapInfo: invalid server packet (no data)', []);
810 Ptr
:= ev
.packet
^.data
;
811 rMsgId
:= Byte(Ptr
^);
812 e_LogWritefln('g_Net_Wait_MapInfo: got message %u from server (dataLength=%u)', [rMsgId
, ev
.packet
^.dataLength
]);
813 if (rMsgId
= NTF_SERVER_FILE_INFO
) then
815 e_LogWritefln('g_Net_Wait_MapInfo: waiting for map info reply, but got file info reply', []);
819 else if (rMsgId
= NTF_SERVER_ABORT
) then
821 e_LogWritefln('g_Net_Wait_MapInfo: server aborted transfer', []);
825 else if (rMsgId
= NTF_SERVER_MAP_INFO
) then
827 e_LogWritefln('g_Net_Wait_MapInfo: creating map info packet...', []);
828 if not msg
.Init(ev
.packet
^.data
+1, ev
.packet
^.dataLength
-1, True) then exit
;
829 e_LogWritefln('g_Net_Wait_MapInfo: parsing map info packet (rd=%d; max=%d)...', [msg
.ReadCount
, msg
.MaxSize
]);
830 SetLength(resList
, 0); // just in case
832 tf
.diskName
:= msg
.ReadString();
833 e_LogWritefln('g_Net_Wait_MapInfo: map wad is `%s`', [tf
.diskName
]);
835 tf
.hash
:= msg
.ReadMD5();
837 tf
.size
:= msg
.ReadLongInt();
838 e_LogWritefln('g_Net_Wait_MapInfo: map wad size is %d', [tf
.size
]);
839 // number of external resources for map
840 rc
:= msg
.ReadLongInt();
841 if (rc
< 0) or (rc
> 1024) then
843 e_LogWritefln('g_Net_Wait_Event: invalid number of map external resources (%d)', [rc
]);
847 e_LogWritefln('g_Net_Wait_MapInfo: map external resource count is %d', [rc
]);
848 SetLength(resList
, rc
);
849 // external resource names
850 for f
:= 0 to rc
-1 do
853 s
:= msg
.ReadString();
854 if (length(s
) = 0) then begin result
:= -1; exit
; end;
858 ri
.size
:= msg
.ReadLongInt();
859 ri
.hash
:= msg
.ReadMD5();
860 ri
.wadName
:= ExtractFileName(msg
.ReadString());
861 if (length(ri
.wadName
) = 0) or (ri
.size
< 0) then begin result
:= -1; exit
; end;
865 // old-style packet, only name
866 ri
.wadName
:= ExtractFileName(s
);
867 if (length(ri
.wadName
) = 0) then begin result
:= -1; exit
; end;
868 ri
.size
:= -1; // unknown
871 e_LogWritefln('g_Net_Wait_MapInfo: got map info', []);
872 Result
:= 0; // success
877 e_LogWritefln('g_Net_Wait_Event: invalid server packet type', []);
883 ENET_EVENT_TYPE_DISCONNECT
:
885 if (ev
.data
<= NET_DISC_MAX
) then
886 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' ' + _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + ev
.data
)], True);
892 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' unknown ENet event ' + IntToStr(Ord(ev
.kind
)), True);
897 if (freePacket
) then begin freePacket
:= false; enet_packet_destroy(ev
.packet
); end;
900 if g_Net_UserRequestExit() then
902 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' user abort', True);
908 if (freePacket
) then enet_packet_destroy(ev
.packet
);
913 // send file request to server, and wait for server reply
915 // returns `false` on error or user abort
917 // diskName (actually, base name)
926 // for maps, first `tf.diskName` name will be map wad name, and `tf.hash`/`tf.size` will contain map info
927 function g_Net_RequestResFileInfo (resIndex
: LongInt; out tf
: TNetFileTransfer
): Integer;
933 freePacket
: Boolean = false;
939 trans_omsg
.Write(Byte(NTF_CLIENT_FILE_REQUEST
));
940 trans_omsg
.Write(resIndex
);
941 if not ftransSendClientMsg(trans_omsg
) then begin result
:= -1; exit
; end;
943 FillChar(ev
, SizeOf(ev
), 0);
946 ett
:= getNewTimeoutEnd();
948 status
:= enet_host_service(NetHost
, @ev
, 300);
952 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' network error', True);
957 if (status
<= 0) then
963 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' timeout reached', True);
972 ENET_EVENT_TYPE_RECEIVE
:
975 if (ev
.channelID
<> NET_CHAN_DOWNLOAD_EX
) then
977 //e_LogWriteln('g_Net_Wait_Event: skip message from non-transfer channel');
979 g_Net_Client_HandlePacket(ev
.packet
, g_Net_ClientLightMsgHandler
);
980 if (g_Res_received_map_start
< 0) then begin result
:= -666; exit
; end;
984 ett
:= getNewTimeoutEnd();
985 if (ev
.packet
.dataLength
< 1) then
987 e_LogWriteln('g_Net_Wait_Event: invalid server packet (no data)');
991 Ptr
:= ev
.packet
^.data
;
992 rMsgId
:= Byte(Ptr
^);
993 e_LogWritefln('received transfer packet with id %d (%u bytes)', [rMsgId
, ev
.packet
^.dataLength
]);
994 if (rMsgId
= NTF_SERVER_FILE_INFO
) then
996 if not msg
.Init(ev
.packet
^.data
+1, ev
.packet
^.dataLength
-1, True) then exit
;
997 tf
.hash
:= msg
.ReadMD5();
998 tf
.size
:= msg
.ReadLongInt();
999 tf
.chunkSize
:= msg
.ReadLongInt();
1000 tf
.diskName
:= ExtractFileName(msg
.readString());
1001 if (tf
.size
< 0) or (tf
.chunkSize
<> FILE_CHUNK_SIZE
) or (length(tf
.diskName
) = 0) then
1003 e_LogWritefln('g_Net_RequestResFileInfo: invalid file info packet', []);
1007 e_LogWritefln('got file info for resource #%d: size=%d; name=%s', [resIndex
, tf
.size
, tf
.diskName
]);
1008 Result
:= 0; // success
1011 else if (rMsgId
= NTF_SERVER_ABORT
) then
1013 e_LogWriteln('g_Net_RequestResFileInfo: server aborted transfer');
1017 else if (rMsgId
= NTF_SERVER_MAP_INFO
) then
1019 e_LogWriteln('g_Net_RequestResFileInfo: waiting for map info reply, but got file info reply');
1025 e_LogWriteln('g_Net_RequestResFileInfo: invalid server packet type');
1031 ENET_EVENT_TYPE_DISCONNECT
:
1033 if (ev
.data
<= NET_DISC_MAX
) then
1034 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' ' + _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + ev
.data
)], True);
1040 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' unknown ENet event ' + IntToStr(Ord(ev
.kind
)), True);
1045 if (freePacket
) then begin freePacket
:= false; enet_packet_destroy(ev
.packet
); end;
1048 if g_Net_UserRequestExit() then
1050 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' user abort', True);
1056 if (freePacket
) then enet_packet_destroy(ev
.packet
);
1061 // call this to cancel file transfer requested by `g_Net_RequestResFileInfo()`
1062 function g_Net_AbortResTransfer (var tf
: TNetFileTransfer
): Boolean;
1065 e_LogWritefln('aborting file transfer...', []);
1068 trans_omsg
.Write(Byte(NTF_CLIENT_ABORT
));
1069 result
:= ftransSendClientMsg(trans_omsg
);
1070 if result
then enet_host_flush(NetHost
);
1074 // call this to start file transfer requested by `g_Net_RequestResFileInfo()`
1076 // returns `false` on error or user abort
1085 // 2 on server abort
1086 // for maps, first `tf.diskName` name will be map wad name, and `tf.hash`/`tf.size` will contain map info
1087 function g_Net_ReceiveResourceFile (resIndex
: LongInt; var tf
: TNetFileTransfer
; strm
: TStream
): Integer;
1093 freePacket
: Boolean = false;
1096 nextChunk
: Integer = 0;
1097 chunkTotal
: Integer;
1104 tf
.resumed
:= false;
1105 e_LogWritefln('file `%s`, size=%d (%d)', [tf
.diskName
, Integer(strm
.size
), tf
.size
], TMsgType
.Notify
);
1106 // check if we should resume downloading
1107 resumed
:= (strm
.size
> tf
.chunkSize
) and (strm
.size
< tf
.size
);
1110 trans_omsg
.Write(Byte(NTF_CLIENT_START
));
1111 if resumed
then chunk
:= strm
.size
div tf
.chunkSize
else chunk
:= 0;
1112 trans_omsg
.Write(LongInt(chunk
));
1113 if not ftransSendClientMsg(trans_omsg
) then begin result
:= -1; exit
; end;
1115 strm
.Seek(chunk
*tf
.chunkSize
, soFromBeginning
);
1116 chunkTotal
:= (tf
.size
+tf
.chunkSize
-1) div tf
.chunkSize
;
1117 e_LogWritefln('receiving file `%s` (%d chunks)', [tf
.diskName
, chunkTotal
], TMsgType
.Notify
);
1118 g_Game_SetLoadingText('downloading "'+ExtractFileName(tf
.diskName
)+'"', chunkTotal
, False);
1119 tf
.resumed
:= resumed
;
1121 if (chunk
> 0) then g_Game_StepLoading(chunk
);
1124 // wait for reply data
1125 FillChar(ev
, SizeOf(ev
), 0);
1127 GetMem(buf
, tf
.chunkSize
);
1129 ett
:= getNewTimeoutEnd();
1131 //stx := -GetTimerMS();
1132 status
:= enet_host_service(NetHost
, @ev
, 300);
1134 if (status < 0) then
1136 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' network error', True);
1141 if (status
<= 0) then
1143 // check for timeout
1147 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' timeout reached', True);
1156 ENET_EVENT_TYPE_RECEIVE
:
1159 if (ev
.channelID
<> NET_CHAN_DOWNLOAD_EX
) then
1161 //e_LogWritefln('g_Net_Wait_Event: skip message from non-transfer channel', []);
1162 freePacket
:= false;
1163 g_Net_Client_HandlePacket(ev
.packet
, g_Net_ClientLightMsgHandler
);
1164 if (g_Res_received_map_start
< 0) then begin result
:= -666; exit
; end;
1168 //stx := stx+GetTimerMS();
1169 //e_LogWritefln('g_Net_ReceiveResourceFile: stx=%d', [Integer(stx)]);
1170 //stx := -GetTimerMS();
1171 ett
:= getNewTimeoutEnd();
1172 if (ev
.packet
.dataLength
< 1) then
1174 e_LogWritefln('g_Net_ReceiveResourceFile: invalid server packet (no data)', []);
1178 Ptr
:= ev
.packet
^.data
;
1179 rMsgId
:= Byte(Ptr
^);
1180 if (rMsgId
= NTF_SERVER_DONE
) then
1182 e_LogWritefln('file transfer complete.', []);
1186 else if (rMsgId
= NTF_SERVER_CHUNK
) then
1188 if not msg
.Init(ev
.packet
^.data
+1, ev
.packet
^.dataLength
-1, True) then exit
;
1189 chunk
:= msg
.ReadLongInt();
1190 csize
:= msg
.ReadLongInt();
1191 if (chunk
<> nextChunk
) then
1193 e_LogWritefln('received chunk %d, but expected chunk %d', [chunk
, nextChunk
]);
1197 if (csize
< 0) or (csize
> tf
.chunkSize
) then
1199 e_LogWritefln('received chunk with size %d, but expected chunk size is %d', [csize
, tf
.chunkSize
]);
1203 //e_LogWritefln('got chunk #%d of #%d (csize=%d)', [chunk, (tf.size+tf.chunkSize-1) div tf.chunkSize, csize]);
1204 msg
.ReadData(buf
, csize
);
1205 strm
.WriteBuffer(buf
^, csize
);
1206 nextChunk
:= chunk
+1;
1207 g_Game_StepLoading();
1210 trans_omsg
.Write(Byte(NTF_CLIENT_ACK
));
1211 trans_omsg
.Write(LongInt(chunk
));
1212 if not ftransSendClientMsg(trans_omsg
) then begin result
:= -1; exit
; end;
1214 else if (rMsgId
= NTF_SERVER_ABORT
) then
1216 e_LogWritefln('g_Net_ReceiveResourceFile: server aborted transfer', []);
1222 e_LogWritefln('g_Net_ReceiveResourceFile: invalid server packet type', []);
1226 //stx := stx+GetTimerMS();
1227 //e_LogWritefln('g_Net_ReceiveResourceFile: process stx=%d', [Integer(stx)]);
1230 ENET_EVENT_TYPE_DISCONNECT
:
1232 if (ev
.data
<= NET_DISC_MAX
) then
1233 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' ' + _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + ev
.data
)], True);
1239 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' unknown ENet event ' + IntToStr(Ord(ev
.kind
)), True);
1244 if (freePacket
) then begin freePacket
:= false; enet_packet_destroy(ev
.packet
); end;
1247 if g_Net_UserRequestExit() then
1249 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' user abort', True);
1256 if (freePacket
) then enet_packet_destroy(ev
.packet
);
1261 //**************************************************************************
1265 //**************************************************************************
1267 function g_Net_FindSlot(): Integer;
1276 for I
:= Low(NetClients
) to High(NetClients
) do
1278 if NetClients
[I
].Used
then
1287 if C
>= NetMaxClients
then
1295 if (Length(NetClients
) >= NetMaxClients
) then
1299 SetLength(NetClients
, Length(NetClients
) + 1);
1300 N
:= High(NetClients
);
1306 NetClients
[N
].Used
:= True;
1307 NetClients
[N
].ID
:= N
;
1308 NetClients
[N
].RequestedFullUpdate
:= False;
1309 NetClients
[N
].WaitForFirstSpawn
:= False;
1310 NetClients
[N
].RCONAuth
:= False;
1311 NetClients
[N
].Voted
:= False;
1312 NetClients
[N
].Player
:= 0;
1313 clearNetClientTransfers(NetClients
[N
]); // just in case
1320 function g_Net_Init(): Boolean;
1329 NetBuf
[NET_UNRELIABLE
].Clear();
1330 NetBuf
[NET_RELIABLE
].Clear();
1331 //SetLength(NetClients, 0);
1332 clearNetClients(true); // clear array
1338 NetAddr
.port
:= 25666;
1339 SetLength(NetBannedHosts
, 0);
1340 path
:= BANLIST_FILENAME
;
1341 if e_FindResource(DataDirs
, path
) = true then
1348 if StrToIp(IPstr
, IP
) then
1352 g_Net_SaveBanList();
1355 //Result := (enet_initialize() = 0);
1356 Result
:= enet_init_success
;
1359 procedure g_Net_Flush();
1363 F
, Chan
: enet_uint32
;
1367 Chan
:= NET_CHAN_GAME
;
1369 if NetMode
= NET_SERVER
then
1370 for T
:= NET_UNRELIABLE
to NET_RELIABLE
do
1372 if NetBuf
[T
].CurSize
> 0 then
1374 P
:= enet_packet_create(NetBuf
[T
].Data
, NetBuf
[T
].CurSize
, F
);
1375 if not Assigned(P
) then continue
;
1376 enet_host_broadcast(NetHost
, Chan
, P
);
1380 for I
:= Low(NetClients
) to High(NetClients
) do
1382 if not NetClients
[I
].Used
then continue
;
1383 if NetClients
[I
].NetOut
[T
].CurSize
<= 0 then continue
;
1384 P
:= enet_packet_create(NetClients
[I
].NetOut
[T
].Data
, NetClients
[I
].NetOut
[T
].CurSize
, F
);
1385 if not Assigned(P
) then continue
;
1386 enet_peer_send(NetClients
[I
].Peer
, Chan
, P
);
1387 NetClients
[I
].NetOut
[T
].Clear();
1390 // next and last iteration is always RELIABLE
1391 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
);
1392 Chan
:= NET_CHAN_IMPORTANT
;
1394 else if NetMode
= NET_CLIENT
then
1395 for T
:= NET_UNRELIABLE
to NET_RELIABLE
do
1397 if NetBuf
[T
].CurSize
> 0 then
1399 P
:= enet_packet_create(NetBuf
[T
].Data
, NetBuf
[T
].CurSize
, F
);
1400 if not Assigned(P
) then continue
;
1401 enet_peer_send(NetPeer
, Chan
, P
);
1404 // next and last iteration is always RELIABLE
1405 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
);
1406 Chan
:= NET_CHAN_IMPORTANT
;
1410 procedure g_Net_Cleanup();
1414 NetBuf
[NET_UNRELIABLE
].Clear();
1415 NetBuf
[NET_RELIABLE
].Clear();
1417 //SetLength(NetClients, 0);
1418 clearNetClients(true); // clear array
1419 NetClientCount
:= 0;
1423 g_Net_Slist_ServerClosed();
1427 NetState
:= NET_STATE_NONE
;
1429 NetPongSock
:= ENET_SOCKET_NULL
;
1431 NetTimeToMaster
:= 0;
1432 NetTimeToUpdate
:= 0;
1433 NetTimeToReliable
:= 0;
1435 NetMode
:= NET_NONE
;
1437 if NetPortThread
<> NilThreadId
then
1438 WaitForThreadTerminate(NetPortThread
, 66666);
1440 NetPortThread
:= NilThreadId
;
1441 g_Net_UnforwardPorts();
1447 procedure g_Net_Free();
1451 //enet_deinitialize();
1452 NetInitDone
:= False;
1456 //**************************************************************************
1460 //**************************************************************************
1462 function ForwardThread(Param
: Pointer): PtrInt
;
1465 if not g_Net_ForwardPorts() then Result
:= -1;
1468 function g_Net_Host(IPAddr
: LongWord; Port
: enet_uint16
; MaxClients
: Cardinal = 16): Boolean;
1470 if NetMode
<> NET_NONE
then
1472 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_INGAME
]);
1479 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST
], [Port
]));
1480 if not NetInitDone
then
1482 if (not g_Net_Init()) then
1484 g_Console_Add(_lc
[I_NET_MSG_FERROR
] + _lc
[I_NET_ERR_ENET
]);
1489 NetInitDone
:= True;
1492 NetAddr
.host
:= IPAddr
;
1493 NetAddr
.port
:= Port
;
1495 NetHost
:= enet_host_create(@NetAddr
, NET_MAXCLIENTS
, NET_CHANS
, 0, 0);
1497 if (NetHost
= nil) then
1499 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + Format(_lc
[I_NET_ERR_HOST
], [Port
]));
1505 if NetForwardPorts
then NetPortThread
:= BeginThread(ForwardThread
);
1507 NetPongSock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
1508 if NetPongSock
<> ENET_SOCKET_NULL
then
1510 NetPongAddr
.host
:= IPAddr
;
1511 NetPongAddr
.port
:= NET_PING_PORT
;
1512 if enet_socket_bind(NetPongSock
, @NetPongAddr
) < 0 then
1514 enet_socket_destroy(NetPongSock
);
1515 NetPongSock
:= ENET_SOCKET_NULL
;
1518 enet_socket_set_option(NetPongSock
, ENET_SOCKOPT_NONBLOCK
, 1);
1521 NetMode
:= NET_SERVER
;
1523 NetBuf
[NET_UNRELIABLE
].Clear();
1524 NetBuf
[NET_RELIABLE
].Clear();
1530 procedure g_Net_Host_Die();
1534 if NetMode
<> NET_SERVER
then Exit
;
1536 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_DISCALL
]);
1537 for I
:= 0 to High(NetClients
) do
1538 if NetClients
[I
].Used
then
1539 enet_peer_disconnect(NetClients
[I
].Peer
, NET_DISC_DOWN
);
1541 while enet_host_service(NetHost
, @NetEvent
, 1000) > 0 do
1542 if NetEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
1543 enet_packet_destroy(NetEvent
.packet
);
1545 for I
:= 0 to High(NetClients
) do
1546 if NetClients
[I
].Used
then
1548 FreeMemory(NetClients
[I
].Peer
^.data
);
1549 NetClients
[I
].Peer
^.data
:= nil;
1550 enet_peer_reset(NetClients
[I
].Peer
);
1551 NetClients
[I
].Peer
:= nil;
1552 NetClients
[I
].Used
:= False;
1553 NetClients
[I
].NetOut
[NET_UNRELIABLE
].Free();
1554 NetClients
[I
].NetOut
[NET_RELIABLE
].Free();
1557 clearNetClients(false); // don't clear array
1558 g_Net_Slist_ServerClosed();
1559 if NetPongSock
<> ENET_SOCKET_NULL
then
1560 enet_socket_destroy(NetPongSock
);
1562 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_DIE
]);
1563 enet_host_destroy(NetHost
);
1565 NetMode
:= NET_NONE
;
1568 e_WriteLog('NET: Server stopped', TMsgType
.Notify
);
1572 procedure g_Net_Host_Send(ID
: Integer; Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
1579 T
:= NET_UNRELIABLE
;
1583 if ID
> High(NetClients
) then Exit
;
1584 if NetClients
[ID
].Peer
= nil then Exit
;
1586 NetClients
[ID
].NetOut
[T
].Write(Integer(NetOut
.CurSize
));
1587 NetClients
[ID
].NetOut
[T
].Write(NetOut
);
1592 NetBuf
[T
].Write(Integer(NetOut
.CurSize
));
1593 NetBuf
[T
].Write(NetOut
);
1596 if NetDump
then g_Net_DumpSendBuffer();
1600 procedure g_Net_Host_CheckPings();
1602 ClAddr
: ENetAddress
;
1606 Ping
: array [0..9] of Byte;
1609 if (NetPongSock
= ENET_SOCKET_NULL
) or (NetHost
= nil) then Exit
;
1611 Buf
.data
:= Addr(Ping
[0]);
1612 Buf
.dataLength
:= 2+8;
1616 Len
:= enet_socket_receive(NetPongSock
, @ClAddr
, @Buf
, 1);
1617 if Len
< 0 then Exit
;
1619 if (Ping
[0] = Ord('D')) and (Ping
[1] = Ord('F')) then
1621 ClTime
:= Int64(Addr(Ping
[2])^);
1624 NetOut
.Write(Byte(Ord('D')));
1625 NetOut
.Write(Byte(Ord('F')));
1626 NetOut
.Write(NetHost
.address
.port
);
1627 NetOut
.Write(ClTime
);
1628 TMasterHost
.writeInfo(NetOut
);
1630 if gPlayer1
<> nil then Inc(NPl
);
1631 if gPlayer2
<> nil then Inc(NPl
);
1633 NetOut
.Write(gNumBots
);
1635 Buf
.data
:= NetOut
.Data
;
1636 Buf
.dataLength
:= NetOut
.CurSize
;
1637 enet_socket_send(NetPongSock
, @ClAddr
, @Buf
, 1);
1644 function g_Net_Host_Update(): enet_size_t
;
1655 if NetUseMaster
then g_Net_Slist_Pulse();
1656 g_Net_Host_CheckPings();
1658 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
1660 case (NetEvent
.kind
) of
1661 ENET_EVENT_TYPE_CONNECT
:
1663 IP
:= IpToStr(NetEvent
.Peer
^.address
.host
);
1664 Port
:= NetEvent
.Peer
^.address
.port
;
1665 g_Console_Add(_lc
[I_NET_MSG
] +
1666 Format(_lc
[I_NET_MSG_HOST_CONN
], [IP
, Port
]));
1668 if (NetEvent
.data
<> NET_PROTOCOL_VER
) then
1670 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_REJECT
] +
1671 _lc
[I_NET_DISC_PROTOCOL
]);
1672 NetEvent
.peer
^.data
:= GetMemory(SizeOf(Byte));
1673 Byte(NetEvent
.peer
^.data
^) := 255;
1674 enet_peer_disconnect(NetEvent
.peer
, NET_DISC_PROTOCOL
);
1675 enet_host_flush(NetHost
);
1679 ID
:= g_Net_FindSlot();
1683 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_REJECT
] +
1684 _lc
[I_NET_DISC_FULL
]);
1685 NetEvent
.Peer
^.data
:= GetMemory(SizeOf(Byte));
1686 Byte(NetEvent
.peer
^.data
^) := 255;
1687 enet_peer_disconnect(NetEvent
.peer
, NET_DISC_FULL
);
1688 enet_host_flush(NetHost
);
1692 NetClients
[ID
].Peer
:= NetEvent
.peer
;
1693 NetClients
[ID
].Peer
^.data
:= GetMemory(SizeOf(Byte));
1694 Byte(NetClients
[ID
].Peer
^.data
^) := ID
;
1695 NetClients
[ID
].State
:= NET_STATE_AUTH
;
1696 NetClients
[ID
].RCONAuth
:= False;
1697 NetClients
[ID
].NetOut
[NET_UNRELIABLE
].Alloc(NET_BUFSIZE
*2);
1698 NetClients
[ID
].NetOut
[NET_RELIABLE
].Alloc(NET_BUFSIZE
*2);
1699 clearNetClientTransfers(NetClients
[ID
]); // just in case
1701 enet_peer_timeout(NetEvent
.peer
, ENET_PEER_TIMEOUT_LIMIT
* 2, ENET_PEER_TIMEOUT_MINIMUM
* 2, ENET_PEER_TIMEOUT_MAXIMUM
* 2);
1703 Inc(NetClientCount
);
1704 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST_ADD
], [ID
]));
1707 ENET_EVENT_TYPE_RECEIVE
:
1709 //e_LogWritefln('RECEIVE: chan=%u', [NetEvent.channelID]);
1710 if (NetEvent
.channelID
= NET_CHAN_DOWNLOAD_EX
) then
1712 ProcessDownloadExPacket();
1716 ID
:= Byte(NetEvent
.peer
^.data
^);
1717 if ID
> High(NetClients
) then Exit
;
1718 TC
:= @NetClients
[ID
];
1720 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
1721 g_Net_Host_HandlePacket(TC
, NetEvent
.packet
, g_Net_HostMsgHandler
);
1725 ENET_EVENT_TYPE_DISCONNECT
:
1727 ID
:= Byte(NetEvent
.peer
^.data
^);
1728 if ID
> High(NetClients
) then Exit
;
1729 clearNetClient(NetClients
[ID
]);
1730 TC
:= @NetClients
[ID
];
1731 if TC
= nil then Exit
;
1733 if not (TC
^.Used
) then Exit
;
1735 TP
:= g_Player_Get(TC
^.Player
);
1740 TP
.Kill(K_SIMPLEKILL
, 0, HIT_DISCON
);
1741 g_Console_Add(Format(_lc
[I_PLAYER_LEAVE
], [TP
.Name
]), True);
1742 e_WriteLog('NET: Client ' + TP
.Name
+ ' [' + IntToStr(ID
) + '] disconnected.', TMsgType
.Notify
);
1743 g_Player_Remove(TP
.UID
);
1747 TC
^.State
:= NET_STATE_NONE
;
1750 TC
^.RequestedFullUpdate
:= False;
1751 TC
^.WaitForFirstSpawn
:= False;
1752 TC
^.NetOut
[NET_UNRELIABLE
].Free();
1753 TC
^.NetOut
[NET_RELIABLE
].Free();
1755 FreeMemory(NetEvent
.peer
^.data
);
1756 NetEvent
.peer
^.data
:= nil;
1757 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST_DISC
], [ID
]));
1758 Dec(NetClientCount
);
1760 if NetUseMaster
then g_Net_Slist_ServerPlayerLeaves();
1767 //**************************************************************************
1771 //**************************************************************************
1773 procedure g_Net_Disconnect(Forced
: Boolean = False);
1775 if NetMode
<> NET_CLIENT
then Exit
;
1776 if (NetHost
= nil) or (NetPeer
= nil) then Exit
;
1780 enet_peer_disconnect(NetPeer
, NET_DISC_NONE
);
1782 while (enet_host_service(NetHost
, @NetEvent
, 1500) > 0) do
1784 if (NetEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
) then
1790 if (NetEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
) then
1791 enet_packet_destroy(NetEvent
.packet
);
1794 if NetPeer
<> nil then
1796 enet_peer_reset(NetPeer
);
1802 e_WriteLog('NET: Kicked from server: ' + IntToStr(NetEvent
.data
), TMsgType
.Notify
);
1803 if (NetEvent
.data
<= NET_DISC_MAX
) then
1804 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_KICK
] +
1805 _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + NetEvent
.data
)], True);
1808 if NetHost
<> nil then
1810 enet_host_destroy(NetHost
);
1813 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_CLIENT_DISC
]);
1816 e_WriteLog('NET: Disconnected', TMsgType
.Notify
);
1819 procedure g_Net_Client_Send(Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
1826 T
:= NET_UNRELIABLE
;
1829 NetBuf
[T
].Write(Integer(NetOut
.CurSize
));
1830 NetBuf
[T
].Write(NetOut
);
1832 if NetDump
then g_Net_DumpSendBuffer();
1834 g_Net_Flush(); // FIXME: for now, send immediately
1837 function g_Net_Client_Update(): enet_size_t
;
1840 while (NetHost
<> nil) and (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
1842 case NetEvent
.kind
of
1843 ENET_EVENT_TYPE_RECEIVE
:
1845 if (NetEvent
.channelID
= NET_CHAN_DOWNLOAD_EX
) then continue
; // ignore all download packets, they're processed by separate code
1846 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
1847 g_Net_Client_HandlePacket(NetEvent
.packet
, g_Net_ClientMsgHandler
);
1850 ENET_EVENT_TYPE_DISCONNECT
:
1852 g_Net_Disconnect(True);
1860 function g_Net_Client_UpdateWhileLoading(): enet_size_t
;
1863 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
1865 case NetEvent
.kind
of
1866 ENET_EVENT_TYPE_RECEIVE
:
1868 if (NetEvent
.channelID
= NET_CHAN_DOWNLOAD_EX
) then continue
; // ignore all download packets, they're processed by separate code
1869 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
1870 g_Net_Client_HandlePacket(NetEvent
.packet
, g_Net_ClientLightMsgHandler
);
1873 ENET_EVENT_TYPE_DISCONNECT
:
1875 g_Net_Disconnect(True);
1884 function g_Net_Connect(IP
: string; Port
: enet_uint16
): Boolean;
1887 TimeoutTime
, T
: Int64;
1889 if NetMode
<> NET_NONE
then
1891 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_ERR_INGAME
], True);
1898 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_CLIENT_CONN
],
1900 if not NetInitDone
then
1902 if (not g_Net_Init()) then
1904 g_Console_Add(_lc
[I_NET_MSG_FERROR
] + _lc
[I_NET_ERR_ENET
], True);
1909 NetInitDone
:= True;
1912 NetHost
:= enet_host_create(nil, 1, NET_CHANS
, 0, 0);
1914 if (NetHost
= nil) then
1916 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
1922 enet_address_set_host(@NetAddr
, PChar(Addr(IP
[1])));
1923 NetAddr
.port
:= Port
;
1925 NetPeer
:= enet_host_connect(NetHost
, @NetAddr
, NET_CHANS
, NET_PROTOCOL_VER
);
1927 if (NetPeer
= nil) then
1929 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
1930 enet_host_destroy(NetHost
);
1936 // предупредить что ждем слишком долго через N секунд
1937 TimeoutTime
:= sys_GetTicks() + NET_CONNECT_TIMEOUT
;
1942 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
1944 if (NetEvent
.kind
= ENET_EVENT_TYPE_CONNECT
) then
1946 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_CLIENT_DONE
]);
1947 NetMode
:= NET_CLIENT
;
1949 enet_peer_timeout(NetPeer
, ENET_PEER_TIMEOUT_LIMIT
* 2, ENET_PEER_TIMEOUT_MINIMUM
* 2, ENET_PEER_TIMEOUT_MAXIMUM
* 2);
1951 NetClientPort
:= Port
;
1958 T
:= sys_GetTicks();
1959 if T
> TimeoutTime
then
1961 TimeoutTime
:= T
+ NET_CONNECT_TIMEOUT
* 100; // одного предупреждения хватит
1962 g_Console_Add(_lc
[I_NET_MSG_TIMEOUT_WARN
], True);
1963 g_Console_Add(Format(_lc
[I_NET_MSG_PORTS
], [Integer(Port
), Integer(NET_PING_PORT
)]), True);
1966 ProcessLoading(true);
1968 if e_KeyPressed(IK_SPACE
) or e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(VK_ESCAPE
) or
1969 e_KeyPressed(JOY0_JUMP
) or e_KeyPressed(JOY1_JUMP
) or e_KeyPressed(JOY2_JUMP
) or e_KeyPressed(JOY3_JUMP
) then
1973 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_TIMEOUT
], True);
1974 g_Console_Add(Format(_lc
[I_NET_MSG_PORTS
], [Integer(Port
), Integer(NET_PING_PORT
)]), True);
1975 if NetPeer
<> nil then enet_peer_reset(NetPeer
);
1976 if NetHost
<> nil then
1978 enet_host_destroy(NetHost
);
1985 function IpToStr(IP
: LongWord): string;
1990 Result
:= IntToStr(PByte(Ptr
+ 0)^) + '.';
1991 Result
:= Result
+ IntToStr(PByte(Ptr
+ 1)^) + '.';
1992 Result
:= Result
+ IntToStr(PByte(Ptr
+ 2)^) + '.';
1993 Result
:= Result
+ IntToStr(PByte(Ptr
+ 3)^);
1996 function StrToIp(IPstr
: string; var IP
: LongWord): Boolean;
2000 Result
:= enet_address_set_host(@EAddr
, PChar(@IPstr
[1])) = 0;
2004 function g_Net_Client_ByName(Name
: string): pTNetClient
;
2010 for a
:= Low(NetClients
) to High(NetClients
) do
2011 if (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
2013 pl
:= g_Player_Get(NetClients
[a
].Player
);
2014 if pl
= nil then continue
;
2015 if Copy(LowerCase(pl
.Name
), 1, Length(Name
)) <> LowerCase(Name
) then continue
;
2016 if NetClients
[a
].Peer
<> nil then
2018 Result
:= @NetClients
[a
];
2024 function g_Net_Client_ByPlayer(PID
: Word): pTNetClient
;
2029 for a
:= Low(NetClients
) to High(NetClients
) do
2030 if (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
2031 if NetClients
[a
].Player
= PID
then
2033 Result
:= @NetClients
[a
];
2038 function g_Net_ClientName_ByID(ID
: Integer): string;
2044 if ID
= NET_EVERYONE
then
2046 for a
:= Low(NetClients
) to High(NetClients
) do
2047 if (NetClients
[a
].ID
= ID
) and (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
2049 pl
:= g_Player_Get(NetClients
[a
].Player
);
2050 if pl
= nil then Exit
;
2055 procedure g_Net_SendData(Data
: AByte
; peer
: pENetPeer
; Reliable
: Boolean; Chan
: Byte = NET_CHAN_DOWNLOAD
);
2059 dataLength
: Cardinal;
2061 dataLength
:= Length(Data
);
2064 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
)
2068 if (peer
<> nil) then
2070 P
:= enet_packet_create(@Data
[0], dataLength
, F
);
2071 if not Assigned(P
) then Exit
;
2072 enet_peer_send(peer
, Chan
, P
);
2076 P
:= enet_packet_create(@Data
[0], dataLength
, F
);
2077 if not Assigned(P
) then Exit
;
2078 enet_host_broadcast(NetHost
, Chan
, P
);
2081 enet_host_flush(NetHost
);
2084 function g_Net_IsHostBanned(IP
: LongWord; Perm
: Boolean = False): Boolean;
2089 if NetBannedHosts
= nil then
2091 for I
:= 0 to High(NetBannedHosts
) do
2092 if (NetBannedHosts
[I
].IP
= IP
) and ((not Perm
) or (NetBannedHosts
[I
].Perm
)) then
2099 procedure g_Net_BanHost(IP
: LongWord; Perm
: Boolean = True); overload
;
2105 if g_Net_IsHostBanned(IP
, Perm
) then
2109 for I
:= Low(NetBannedHosts
) to High(NetBannedHosts
) do
2110 if NetBannedHosts
[I
].IP
= 0 then
2118 SetLength(NetBannedHosts
, Length(NetBannedHosts
) + 1);
2119 P
:= High(NetBannedHosts
);
2122 NetBannedHosts
[P
].IP
:= IP
;
2123 NetBannedHosts
[P
].Perm
:= Perm
;
2126 procedure g_Net_BanHost(IP
: string; Perm
: Boolean = True); overload
;
2131 b
:= StrToIp(IP
, a
);
2133 g_Net_BanHost(a
, Perm
);
2136 procedure g_Net_UnbanNonPermHosts();
2140 if NetBannedHosts
= nil then
2142 for I
:= Low(NetBannedHosts
) to High(NetBannedHosts
) do
2143 if (NetBannedHosts
[I
].IP
> 0) and not NetBannedHosts
[I
].Perm
then
2145 NetBannedHosts
[I
].IP
:= 0;
2146 NetBannedHosts
[I
].Perm
:= True;
2150 function g_Net_UnbanHost(IP
: string): Boolean; overload
;
2154 Result
:= StrToIp(IP
, a
);
2156 Result
:= g_Net_UnbanHost(a
);
2159 function g_Net_UnbanHost(IP
: LongWord): Boolean; overload
;
2166 if NetBannedHosts
= nil then
2168 for I
:= 0 to High(NetBannedHosts
) do
2169 if NetBannedHosts
[I
].IP
= IP
then
2171 NetBannedHosts
[I
].IP
:= 0;
2172 NetBannedHosts
[I
].Perm
:= True;
2174 // no break here to clear all bans of this host, perm and non-perm
2178 procedure g_Net_SaveBanList();
2184 path
:= e_GetWriteableDir(DataDirs
);
2187 path
:= e_CatPath(path
, BANLIST_FILENAME
);
2190 if NetBannedHosts
<> nil then
2191 for I
:= 0 to High(NetBannedHosts
) do
2192 if NetBannedHosts
[I
].Perm
and (NetBannedHosts
[I
].IP
> 0) then
2193 Writeln(F
, IpToStr(NetBannedHosts
[I
].IP
));
2198 procedure g_Net_DumpStart();
2200 if NetMode
= NET_SERVER
then
2201 NetDumpFile
:= e_CreateResource(LogDirs
, NETDUMP_FILENAME
+ '_server')
2203 NetDumpFile
:= e_CreateResource(LogDirs
, NETDUMP_FILENAME
+ '_client');
2206 procedure g_Net_DumpSendBuffer();
2208 writeInt(NetDumpFile
, gTime
);
2209 writeInt(NetDumpFile
, LongWord(NetOut
.CurSize
));
2210 writeInt(NetDumpFile
, Byte(1));
2211 NetDumpFile
.WriteBuffer(NetOut
.Data
^, NetOut
.CurSize
);
2214 procedure g_Net_DumpRecvBuffer(Buf
: penet_uint8
; Len
: LongWord);
2216 if (Buf
= nil) or (Len
= 0) then Exit
;
2217 writeInt(NetDumpFile
, gTime
);
2218 writeInt(NetDumpFile
, Len
);
2219 writeInt(NetDumpFile
, Byte(0));
2220 NetDumpFile
.WriteBuffer(Buf
^, Len
);
2223 procedure g_Net_DumpEnd();
2229 function g_Net_ForwardPorts(ForwardPongPort
: Boolean = True): Boolean;
2230 {$IFDEF USE_MINIUPNPC}
2235 LanAddr
: array [0..255] of Char;
2236 StrPort
: AnsiString;
2241 if NetHost
= nil then
2244 if NetPortForwarded
= NetHost
.address
.port
then
2250 NetPongForwarded
:= False;
2251 NetPortForwarded
:= 0;
2253 DevList
:= upnpDiscover(1000, nil, nil, 0, 0, 2, Addr(Err
));
2254 if DevList
= nil then
2256 conwritefln('port forwarding failed: upnpDiscover() failed: %d', [Err
]);
2260 I
:= UPNP_GetValidIGD(DevList
, @Urls
, @Data
, Addr(LanAddr
[0]), 256);
2264 conwriteln('port forwarding failed: could not find an IGD device on this LAN');
2265 FreeUPNPDevList(DevList
);
2266 FreeUPNPUrls(@Urls
);
2270 StrPort
:= IntToStr(NetHost
.address
.port
);
2271 I
:= UPNP_AddPortMapping(
2272 Urls
.controlURL
, Addr(data
.first
.servicetype
[1]),
2273 PChar(StrPort
), PChar(StrPort
), Addr(LanAddr
[0]), PChar('D2DF'),
2274 PChar('UDP'), nil, PChar('0')
2279 conwritefln('forwarding port %d failed: error %d', [NetHost
.address
.port
, I
]);
2280 FreeUPNPDevList(DevList
);
2281 FreeUPNPUrls(@Urls
);
2285 if ForwardPongPort
then
2287 StrPort
:= IntToStr(NET_PING_PORT
);
2288 I
:= UPNP_AddPortMapping(
2289 Urls
.controlURL
, Addr(data
.first
.servicetype
[1]),
2290 PChar(StrPort
), PChar(StrPort
), Addr(LanAddr
[0]), PChar('D2DF'),
2291 PChar('UDP'), nil, PChar('0')
2296 conwritefln('forwarding port %d failed: error %d', [NET_PING_PORT
, I
]);
2297 NetPongForwarded
:= False;
2301 conwritefln('forwarded port %d successfully', [NET_PING_PORT
]);
2302 NetPongForwarded
:= True;
2306 conwritefln('forwarded port %d successfully', [NetHost
.address
.port
]);
2307 NetIGDControl
:= AnsiString(Urls
.controlURL
);
2308 NetIGDService
:= data
.first
.servicetype
;
2309 NetPortForwarded
:= NetHost
.address
.port
;
2311 FreeUPNPDevList(DevList
);
2312 FreeUPNPUrls(@Urls
);
2321 procedure g_Net_UnforwardPorts();
2322 {$IFDEF USE_MINIUPNPC}
2325 StrPort
: AnsiString;
2327 if NetPortForwarded
= 0 then Exit
;
2329 conwriteln('unforwarding ports...');
2331 StrPort
:= IntToStr(NetPortForwarded
);
2332 I
:= UPNP_DeletePortMapping(
2333 PChar(NetIGDControl
), Addr(NetIGDService
[1]),
2334 PChar(StrPort
), PChar('UDP'), nil
2336 conwritefln(' port %d: %d', [NetPortForwarded
, I
]);
2338 if NetPongForwarded
then
2340 NetPongForwarded
:= False;
2341 StrPort
:= IntToStr(NET_PING_PORT
);
2342 I
:= UPNP_DeletePortMapping(
2343 PChar(NetIGDControl
), Addr(NetIGDService
[1]),
2344 PChar(StrPort
), PChar('UDP'), nil
2346 conwritefln(' port %d: %d', [NET_PING_PORT
, I
]);
2349 NetPortForwarded
:= 0;
2356 procedure NetServerCVars(P
: SSArray
);
2361 cmd
:= LowerCase(P
[0]);
2365 if (Length(P
) > 1) and (Length(P
[1]) > 0) then
2367 NetServerName
:= P
[1];
2368 if Length(NetServerName
) > 64 then
2369 SetLength(NetServerName
, 64);
2370 g_Net_Slist_ServerRenamed();
2372 g_Console_Add(cmd
+ ' "' + NetServerName
+ '"');
2376 if (Length(P
) > 1) and (Length(P
[1]) > 0) then
2378 NetPassword
:= P
[1];
2379 if Length(NetPassword
) > 24 then
2380 SetLength(NetPassword
, 24);
2381 g_Net_Slist_ServerRenamed();
2383 g_Console_Add(cmd
+ ' "' + AnsiLowerCase(NetPassword
) + '"');
2387 if (Length(P
) > 1) then
2389 NetMaxClients
:= nclamp(StrToIntDef(P
[1], NetMaxClients
), 1, NET_MAXCLIENTS
);
2390 if g_Game_IsServer
and g_Game_IsNet
then
2393 for a
:= 0 to High(NetClients
) do
2395 if NetClients
[a
].Used
then
2398 if b
> NetMaxClients
then
2400 s
:= g_Player_Get(NetClients
[a
].Player
).Name
;
2401 enet_peer_disconnect(NetClients
[a
].Peer
, NET_DISC_FULL
);
2402 g_Console_Add(Format(_lc
[I_PLAYER_KICK
], [s
]));
2403 MH_SEND_GameEvent(NET_EV_PLAYER_KICK
, 0, s
);
2407 g_Net_Slist_ServerRenamed();
2410 g_Console_Add(cmd
+ ' ' + IntToStr(NetMaxClients
));
2414 if (Length(P
) > 1) then
2416 NetUseMaster
:= StrToIntDef(P
[1], Byte(NetUseMaster
)) <> 0;
2417 if NetUseMaster
then g_Net_Slist_Public() else g_Net_Slist_Private();
2419 g_Console_Add(cmd
+ ' ' + IntToStr(Byte(NetUseMaster
)));
2423 if (Length(P
) > 1) then
2425 if not g_Game_IsNet
then
2426 NetPort
:= nclamp(StrToIntDef(P
[1], NetPort
), 0, $FFFF)
2428 g_Console_Add(_lc
[I_MSG_NOT_NETGAME
]);
2430 g_Console_Add(cmd
+ ' ' + IntToStr(Ord(NetUseMaster
)));
2436 conRegVar('cl_downloadtimeout', @g_Net_DownloadTimeout
, 0.0, 1000000.0, '', 'timeout in seconds, 0 to disable it');
2437 conRegVar('cl_predictself', @NetPredictSelf
, '', 'predict local player');
2438 conRegVar('cl_forceplayerupdate', @NetForcePlayerUpdate
, '', 'update net players on NET_MSG_PLRPOS');
2439 conRegVar('cl_interp', @NetInterpLevel
, '', 'net player interpolation steps');
2440 conRegVar('cl_last_ip', @NetClientIP
, '', 'address of the last you have connected to');
2441 conRegVar('cl_last_port', @NetClientPort
, '', 'port of the last server you have connected to');
2443 conRegVar('sv_forwardports', @NetForwardPorts
, '', 'forward server port using miniupnpc (requires server restart)');
2444 conRegVar('sv_rcon', @NetAllowRCON
, '', 'enable remote console');
2445 conRegVar('sv_rcon_password', @NetRCONPassword
, '', 'remote console password');
2446 conRegVar('sv_update_interval', @NetUpdateRate
, '', 'unreliable update interval');
2447 conRegVar('sv_reliable_interval', @NetRelupdRate
, '', 'reliable update interval');
2448 conRegVar('sv_master_interval', @NetMasterRate
, '', 'master server update interval');
2450 conRegVar('net_master_list', @NetMasterList
, '', 'list of master servers');
2452 SetLength(NetClients
, 0);
2453 g_Net_DownloadTimeout
:= 60;
2454 NetIn
.Alloc(NET_BUFSIZE
);
2455 NetOut
.Alloc(NET_BUFSIZE
);
2456 NetBuf
[NET_UNRELIABLE
].Alloc(NET_BUFSIZE
*2);
2457 NetBuf
[NET_RELIABLE
].Alloc(NET_BUFSIZE
*2);
2458 trans_omsg
.Alloc(NET_BUFSIZE
);
2462 NetBuf
[NET_UNRELIABLE
].Free();
2463 NetBuf
[NET_RELIABLE
].Free();