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';
75 {$IF DEFINED(FREEBSD) OR DEFINED(DARWIN)}
82 TNetMapResourceInfo
= record
83 wadName
: AnsiString; // wad file name, without a path
84 size
: Integer; // wad file size (-1: size and hash are not known)
85 hash
: TMD5Digest
; // wad hash
88 TNetMapResourceInfoArray
= array of TNetMapResourceInfo
;
90 TNetFileTransfer
= record
94 size
: Integer; // file size in bytes
96 lastSentChunk
: Integer;
97 lastAckChunk
: Integer;
98 lastAckTime
: Int64; // msecs; if not "in progress", we're waiting for the first ack
100 diskBuffer
: PChar; // of `chunkSize` bytes
110 RequestedFullUpdate
: Boolean;
111 WaitForFirstSpawn
: Boolean; // set to `true` in server, used to spawn a player on first full state request
114 Transfer
: TNetFileTransfer
; // only one transfer may be active
115 NetOut
: array [0..1] of TMsg
;
121 pTNetClient
= ^TNetClient
;
123 AByte
= array of Byte;
126 NetInitDone
: Boolean = False;
127 NetMode
: Byte = NET_NONE
;
128 NetDump
: Boolean = False;
130 NetServerName
: string = 'Unnamed Server';
131 NetPassword
: string = '';
132 NetPort
: Word = 25666;
134 NetAllowRCON
: Boolean = False;
135 NetRCONPassword
: string = '';
137 NetTimeToUpdate
: Cardinal = 0;
138 NetTimeToReliable
: Cardinal = 0;
139 NetTimeToMaster
: Cardinal = 0;
141 NetHost
: pENetHost
= nil;
142 NetPeer
: pENetPeer
= nil;
144 NetAddr
: ENetAddress
;
146 NetPongAddr
: ENetAddress
;
147 NetPongSock
: ENetSocket
= ENET_SOCKET_NULL
;
149 NetUseMaster
: Boolean = True;
150 NetMasterList
: string = 'mpms.doom2d.org:25665, deadsoftware.ru:25665';
152 NetClientIP
: string = '127.0.0.1';
153 NetClientPort
: Word = 25666;
156 NetBuf
: array [0..1] of TMsg
;
158 NetClients
: array of TNetClient
;
159 NetClientCount
: Byte = 0;
160 NetMaxClients
: Byte = 255;
161 NetBannedHosts
: array of TBanRecord
;
163 NetState
: Integer = NET_STATE_NONE
;
165 NetMyID
: Integer = -1;
166 NetPlrUID1
: Integer = -1;
167 NetPlrUID2
: Integer = -1;
169 NetInterpLevel
: Integer = 1;
170 NetUpdateRate
: Cardinal = 0; // as soon as possible
171 NetRelupdRate
: Cardinal = 18; // around two times a second
172 NetMasterRate
: Cardinal = 60000;
174 NetForcePlayerUpdate
: Boolean = False;
175 NetPredictSelf
: Boolean = True;
176 NetForwardPorts
: Boolean = False;
178 NetGotEverything
: Boolean = False;
179 NetGotKeys
: Boolean = False;
181 {$IFDEF USE_MINIUPNPC}
182 NetPortForwarded
: Word = 0;
183 NetPongForwarded
: Boolean = False;
184 NetIGDControl
: AnsiString;
185 NetIGDService
: TURLStr
;
188 NetPortThread
: TThreadID
= NilThreadId
;
190 NetDumpFile
: TStream
;
192 g_Res_received_map_start
: Integer = 0; // set if we received "map change" event
195 function g_Net_Init(): Boolean;
196 procedure g_Net_Cleanup();
197 procedure g_Net_Free();
198 procedure g_Net_Flush();
200 function g_Net_Host(IPAddr
: LongWord; Port
: enet_uint16
; MaxClients
: Cardinal = 16): Boolean;
201 procedure g_Net_Host_Die();
202 procedure g_Net_Host_Send(ID
: Integer; Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
203 function g_Net_Host_Update(): enet_size_t
;
205 function g_Net_Connect(IP
: string; Port
: enet_uint16
): Boolean;
206 procedure g_Net_Disconnect(Forced
: Boolean = False);
207 procedure g_Net_Client_Send(Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
208 function g_Net_Client_Update(): enet_size_t
;
209 function g_Net_Client_UpdateWhileLoading(): enet_size_t
;
211 function g_Net_Client_ByName(Name
: string): pTNetClient
;
212 function g_Net_Client_ByPlayer(PID
: Word): pTNetClient
;
213 function g_Net_ClientName_ByID(ID
: Integer): string;
215 procedure g_Net_SendData(Data
: AByte
; peer
: pENetPeer
; Reliable
: Boolean; Chan
: Byte = NET_CHAN_DOWNLOAD
);
216 //function g_Net_Wait_Event(msgId: Word): TMemoryStream;
217 //function g_Net_Wait_FileInfo (var tf: TNetFileTransfer; asMap: Boolean; out resList: TStringList): Integer;
219 function IpToStr(IP
: LongWord): string;
220 function StrToIp(IPstr
: string; var IP
: LongWord): Boolean;
222 function g_Net_IsHostBanned(IP
: LongWord; Perm
: Boolean = False): Boolean;
223 procedure g_Net_BanHost(IP
: LongWord; Perm
: Boolean = True); overload
;
224 procedure g_Net_BanHost(IP
: string; Perm
: Boolean = True); overload
;
225 function g_Net_UnbanHost(IP
: string): Boolean; overload
;
226 function g_Net_UnbanHost(IP
: LongWord): Boolean; overload
;
227 procedure g_Net_UnbanNonPermHosts();
228 procedure g_Net_SaveBanList();
230 procedure g_Net_DumpStart();
231 procedure g_Net_DumpSendBuffer();
232 procedure g_Net_DumpRecvBuffer(Buf
: penet_uint8
; Len
: LongWord);
233 procedure g_Net_DumpEnd();
235 function g_Net_ForwardPorts(ForwardPongPort
: Boolean = True): Boolean;
236 procedure g_Net_UnforwardPorts();
238 function g_Net_UserRequestExit
: Boolean;
240 function g_Net_Wait_MapInfo (var tf
: TNetFileTransfer
; var resList
: TNetMapResourceInfoArray
): Integer;
241 function g_Net_RequestResFileInfo (resIndex
: LongInt; out tf
: TNetFileTransfer
): Integer;
242 function g_Net_AbortResTransfer (var tf
: TNetFileTransfer
): Boolean;
243 function g_Net_ReceiveResourceFile (resIndex
: LongInt; var tf
: TNetFileTransfer
; strm
: TStream
): Integer;
245 function g_Net_IsNetworkAvailable (): Boolean;
246 procedure g_Net_InitLowLevel ();
247 procedure g_Net_DeinitLowLevel ();
249 procedure NetServerCVars(P
: SSArray
);
254 // *enet_host_service()*
255 // fuck! https://www.mail-archive.com/enet-discuss@cubik.org/msg00852.html
256 // tl;dr: on shitdows, we can get -1 sometimes, and it is *NOT* a failure.
257 // thank you, enet. let's ignore failures altogether then.
262 g_nethandler
, g_netmsg
, g_netmaster
, g_player
, g_window
, g_console
,
263 g_main
, g_game
, g_language
, g_weapons
, ctypes
, g_system
, g_map
;
266 FILE_CHUNK_SIZE
= 8192;
269 enet_init_success
: Boolean = false;
270 g_Net_DownloadTimeout
: Single;
274 function g_Net_IsNetworkAvailable (): Boolean;
276 result
:= enet_init_success
;
279 procedure g_Net_InitLowLevel ();
281 if enet_init_success
then raise Exception
.Create('wuta?!');
282 enet_init_success
:= (enet_initialize() = 0);
285 procedure g_Net_DeinitLowLevel ();
287 if enet_init_success
then
290 enet_init_success
:= false;
295 //**************************************************************************
299 //**************************************************************************
301 procedure clearNetClientTransfers (var nc
: TNetClient
);
303 nc
.Transfer
.stream
.Free
;
304 nc
.Transfer
.diskName
:= ''; // just in case
305 if (nc
.Transfer
.diskBuffer
<> nil) then FreeMem(nc
.Transfer
.diskBuffer
);
306 nc
.Transfer
.stream
:= nil;
307 nc
.Transfer
.diskBuffer
:= nil;
311 procedure clearNetClient (var nc
: TNetClient
);
313 clearNetClientTransfers(nc
);
317 procedure clearNetClients (clearArray
: Boolean);
321 for f
:= Low(NetClients
) to High(NetClients
) do clearNetClient(NetClients
[f
]);
322 if (clearArray
) then SetLength(NetClients
, 0);
326 function g_Net_UserRequestExit (): Boolean;
328 Result
:= {e_KeyPressed(IK_SPACE) or}
329 e_KeyPressed(IK_ESCAPE
) or
330 e_KeyPressed(VK_ESCAPE
) or
331 e_KeyPressed(JOY0_JUMP
) or
332 e_KeyPressed(JOY1_JUMP
) or
333 e_KeyPressed(JOY2_JUMP
) or
334 e_KeyPressed(JOY3_JUMP
)
338 //**************************************************************************
340 // file transfer declaraions and host packet processor
342 //**************************************************************************
345 // server packet type
346 NTF_SERVER_DONE
= 10; // done with this file
347 NTF_SERVER_FILE_INFO
= 11; // sent after client request
348 NTF_SERVER_CHUNK
= 12; // next chunk; chunk number follows
349 NTF_SERVER_ABORT
= 13; // server abort
350 NTF_SERVER_MAP_INFO
= 14;
352 // client packet type
353 NTF_CLIENT_MAP_REQUEST
= 100; // map file request; also, returns list of additional wads to download
354 NTF_CLIENT_FILE_REQUEST
= 101; // resource file request (by index)
355 NTF_CLIENT_ABORT
= 102; // do not send requested file, or abort current transfer
356 NTF_CLIENT_START
= 103; // start transfer; client may resume download by sending non-zero starting chunk
357 NTF_CLIENT_ACK
= 104; // chunk ack; chunk number follows
360 // disconnect client due to some file transfer error
361 procedure killClientByFT (var nc
: TNetClient
);
363 e_LogWritefln('disconnected client #%d due to file transfer error', [nc
.ID
], TMsgType
.Warning
);
364 enet_peer_disconnect(nc
.Peer
, NET_DISC_FILE_TIMEOUT
);
365 clearNetClientTransfers(nc
);
366 g_Net_Slist_ServerPlayerLeaves();
370 // send file transfer message from server to client
371 function ftransSendServerMsg (var nc
: TNetClient
; var m
: TMsg
): Boolean;
376 if (m
.CurSize
< 1) then exit
;
377 pkt
:= enet_packet_create(m
.Data
, m
.CurSize
, ENET_PACKET_FLAG_RELIABLE
);
378 if not Assigned(pkt
) then begin killClientByFT(nc
); exit
; end;
379 if (enet_peer_send(nc
.Peer
, NET_CHAN_DOWNLOAD_EX
, pkt
) <> 0) then begin killClientByFT(nc
); exit
; end;
384 // send file transfer message from client to server
385 function ftransSendClientMsg (var m
: TMsg
): Boolean;
390 if (m
.CurSize
< 1) then exit
;
391 pkt
:= enet_packet_create(m
.Data
, m
.CurSize
, ENET_PACKET_FLAG_RELIABLE
);
392 if not Assigned(pkt
) then exit
;
393 if (enet_peer_send(NetPeer
, NET_CHAN_DOWNLOAD_EX
, pkt
) <> 0) then exit
;
399 procedure ProcessChunkSend (var nc
: TNetClient
);
401 tf
: ^TNetFileTransfer
;
407 if (tf
.stream
= nil) then exit
;
409 // arbitrary timeout number
410 if (ct
-tf
.lastAckTime
>= 5000) then
415 // check if we need to send something
416 if (not tf
.inProgress
) then exit
; // waiting for the initial ack
417 // ok, we're sending chunks
418 if (tf
.lastAckChunk
<> tf
.lastSentChunk
) then exit
;
419 Inc(tf
.lastSentChunk
);
420 // do it one chunk at a time; client ack will advance our chunk counter
421 chunks
:= (tf
.size
+tf
.chunkSize
-1) div tf
.chunkSize
;
423 if (tf
.lastSentChunk
> chunks
) then
430 if (tf
.lastSentChunk
= chunks
) then
432 // we're done with this file
433 e_LogWritefln('download: client #%d, DONE sending chunks #%d/#%d', [nc
.ID
, tf
.lastSentChunk
, chunks
]);
434 trans_omsg
.Write(Byte(NTF_SERVER_DONE
));
435 clearNetClientTransfers(nc
);
440 trans_omsg
.Write(Byte(NTF_SERVER_CHUNK
));
441 trans_omsg
.Write(LongInt(tf
.lastSentChunk
));
443 rd
:= tf
.size
-(tf
.lastSentChunk
*tf
.chunkSize
);
444 if (rd
> tf
.chunkSize
) then rd
:= tf
.chunkSize
;
445 trans_omsg
.Write(LongInt(rd
));
446 //e_LogWritefln('download: client #%d, sending chunk #%d/#%d (%d bytes)', [nc.ID, tf.lastSentChunk, chunks, rd]);
447 //FIXME: check for errors here
449 tf
.stream
.Seek(tf
.lastSentChunk
*tf
.chunkSize
, soFromBeginning
);
450 tf
.stream
.ReadBuffer(tf
.diskBuffer
^, rd
);
451 trans_omsg
.WriteData(tf
.diskBuffer
, rd
);
458 ftransSendServerMsg(nc
, trans_omsg
);
462 // server file transfer packet processor
463 // received packet is in `NetEvent`
464 procedure ProcessDownloadExPacket ();
471 tf
: ^TNetFileTransfer
;
481 // find client index by peer
482 for f
:= Low(NetClients
) to High(NetClients
) do
484 if (not NetClients
[f
].Used
) then continue
;
485 if (NetClients
[f
].Peer
= NetEvent
.peer
) then
491 //e_LogWritefln('RECEIVE: dlpacket; client=%d (datalen=%u)', [nid, NetEvent.packet^.dataLength]);
493 if (nid
< 0) then exit
; // wtf?!
494 nc
:= @NetClients
[nid
];
496 if (NetEvent
.packet
^.dataLength
= 0) then
502 tf
:= @NetClients
[nid
].Transfer
;
503 tf
.lastAckTime
:= GetTimerMS();
505 cmd
:= Byte(NetEvent
.packet
^.data
^);
506 //e_LogWritefln('RECEIVE: nid=%d; cmd=%u', [nid, cmd]);
508 NTF_CLIENT_FILE_REQUEST
: // file request
510 if (tf
.stream
<> nil) then
515 if (NetEvent
.packet
^.dataLength
< 2) then
520 // new transfer request; build packet
521 if not msg
.Init(NetEvent
.packet
^.data
+1, NetEvent
.packet
^.dataLength
-1, True) then
526 // get resource index
527 ridx
:= msg
.ReadLongInt();
528 if (ridx
< -1) or (ridx
>= length(gExternalResources
)) then
530 e_LogWritefln('Invalid resource index %d', [ridx
], TMsgType
.Warning
);
534 if (ridx
< 0) then fname
:= gGameSettings
.WAD
else fname
:= gExternalResources
[ridx
].diskName
;
535 if (length(fname
) = 0) then
537 e_WriteLog('Invalid filename: '+fname
, TMsgType
.Warning
);
541 tf
.diskName
:= findDiskWad(fname
);
542 if (length(tf
.diskName
) = 0) then
544 e_LogWritefln('NETWORK: file "%s" not found!', [fname
], TMsgType
.Fatal
);
549 //tf.hash := MD5File(tf.diskName);
550 if (ridx
< 0) then tf
.hash
:= gWADHash
else tf
.hash
:= gExternalResources
[ridx
].hash
;
551 // create file stream
552 tf
.diskName
:= findDiskWad(fname
);
554 tf
.stream
:= openDiskFileRO(tf
.diskName
);
558 if (tf
.stream
= nil) then
560 e_WriteLog(Format('NETWORK: file "%s" not found!', [fname
]), TMsgType
.Fatal
);
564 e_LogWritefln('client #%d requested resource #%d (file is `%s` : `%s`)', [nc
.ID
, ridx
, fname
, tf
.diskName
]);
565 tf
.size
:= tf
.stream
.size
;
566 tf
.chunkSize
:= FILE_CHUNK_SIZE
; // arbitrary
567 tf
.lastSentChunk
:= -1;
568 tf
.lastAckChunk
:= -1;
569 tf
.lastAckTime
:= GetTimerMS();
570 tf
.inProgress
:= False; // waiting for the first ACK or for the cancel
571 GetMem(tf
.diskBuffer
, tf
.chunkSize
);
572 // sent file info message
574 trans_omsg
.Write(Byte(NTF_SERVER_FILE_INFO
));
575 trans_omsg
.Write(tf
.hash
);
576 trans_omsg
.Write(tf
.size
);
577 trans_omsg
.Write(tf
.chunkSize
);
578 trans_omsg
.Write(ExtractFileName(fname
));
579 if not ftransSendServerMsg(nc
^, trans_omsg
) then exit
;
581 NTF_CLIENT_ABORT
: // do not send requested file, or abort current transfer
583 e_LogWritefln('client #%d aborted file transfer', [nc
.ID
]);
584 clearNetClientTransfers(nc
^);
586 NTF_CLIENT_START
: // start transfer; client may resume download by sending non-zero starting chunk
588 if not Assigned(tf
.stream
) then
593 if (tf
.lastSentChunk
<> -1) or (tf
.lastAckChunk
<> -1) or (tf
.inProgress
) then
595 // double ack, get lost
599 if (NetEvent
.packet
^.dataLength
< 2) then
605 if not msg
.Init(NetEvent
.packet
^.data
+1, NetEvent
.packet
^.dataLength
-1, True) then
610 chunk
:= msg
.ReadLongInt();
611 if (chunk
< 0) or (chunk
> (tf
.size
+tf
.chunkSize
-1) div tf
.chunkSize
) then
616 e_LogWritefln('client #%d started file transfer from chunk %d', [nc
.ID
, chunk
]);
617 // start sending chunks
618 tf
.inProgress
:= True;
619 tf
.lastSentChunk
:= chunk
-1;
620 tf
.lastAckChunk
:= chunk
-1;
621 ProcessChunkSend(nc
^);
623 NTF_CLIENT_ACK
: // chunk ack; chunk number follows
625 if not Assigned(tf
.stream
) then
630 if (tf
.lastSentChunk
< 0) or (not tf
.inProgress
) then
632 // double ack, get lost
636 if (NetEvent
.packet
^.dataLength
< 2) then
642 if not msg
.Init(NetEvent
.packet
^.data
+1, NetEvent
.packet
^.dataLength
-1, True) then
647 chunk
:= msg
.ReadLongInt();
648 if (chunk
< 0) or (chunk
> (tf
.size
+tf
.chunkSize
-1) div tf
.chunkSize
) then
653 // do it this way, so client may seek, or request retransfers for some reason
654 tf
.lastAckChunk
:= chunk
;
655 tf
.lastSentChunk
:= chunk
;
656 //e_LogWritefln('client #%d acked file transfer chunk %d', [nc.ID, chunk]);
657 ProcessChunkSend(nc
^);
659 NTF_CLIENT_MAP_REQUEST
:
661 e_LogWritefln('client #%d requested map info', [nc
.ID
]);
663 dfn
:= findDiskWad(gGameSettings
.WAD
);
664 if (dfn
= '') then dfn
:= '!wad_not_found!.wad'; //FIXME
665 //md5 := MD5File(dfn);
667 if (not GetDiskFileInfo(dfn
, fi
)) then
669 e_LogWritefln('client #%d requested map info, but i cannot get file info', [nc
.ID
]);
675 st := openDiskFileRO(dfn);
676 if not assigned(st) then exit; //wtf?!
681 trans_omsg
.Write(Byte(NTF_SERVER_MAP_INFO
));
683 trans_omsg
.Write(ExtractFileName(gGameSettings
.WAD
));
685 trans_omsg
.Write(md5
);
687 trans_omsg
.Write(size
);
688 // number of external resources for map
689 trans_omsg
.Write(LongInt(length(gExternalResources
)));
690 // external resource names
691 for f
:= 0 to High(gExternalResources
) do
694 //trans_omsg.Write(ExtractFileName(gExternalResources[f])); // GameDir+'/wads/'+ResList.Strings[i]
696 trans_omsg
.Write('!');
697 trans_omsg
.Write(LongInt(gExternalResources
[f
].size
));
698 trans_omsg
.Write(gExternalResources
[f
].hash
);
699 trans_omsg
.Write(ExtractFileName(gExternalResources
[f
].diskName
));
702 if not ftransSendServerMsg(nc
^, trans_omsg
) then exit
;
713 //**************************************************************************
715 // file transfer crap (both client and server)
717 //**************************************************************************
719 function getNewTimeoutEnd (): Int64;
721 result
:= GetTimerMS();
722 if (g_Net_DownloadTimeout
<= 0) then
724 result
:= result
+1000*60*3; // 3 minutes
728 result
:= result
+trunc(g_Net_DownloadTimeout
*1000);
733 // send map request to server, and wait for "map info" server reply
735 // returns `false` on error or user abort
737 // diskName: map wad file name (without a path)
738 // hash: map wad hash
739 // size: map wad size
740 // chunkSize: set too
741 // resList: list of resource wads
747 // for maps, first `tf.diskName` name will be map wad name, and `tf.hash`/`tf.size` will contain map info
748 function g_Net_Wait_MapInfo (var tf
: TNetFileTransfer
; var resList
: TNetMapResourceInfoArray
): Integer;
754 freePacket
: Boolean = false;
759 ri
: ^TNetMapResourceInfo
;
761 SetLength(resList
, 0);
765 trans_omsg
.Write(Byte(NTF_CLIENT_MAP_REQUEST
));
766 if not ftransSendClientMsg(trans_omsg
) then begin result
:= -1; exit
; end;
768 FillChar(ev
, SizeOf(ev
), 0);
771 ett
:= getNewTimeoutEnd();
773 status
:= enet_host_service(NetHost
, @ev
, 300);
777 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' network error', True);
782 if (status
<= 0) then
788 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' timeout reached', True);
797 ENET_EVENT_TYPE_RECEIVE
:
800 if (ev
.channelID
<> NET_CHAN_DOWNLOAD_EX
) then
802 //e_LogWritefln('g_Net_Wait_MapInfo: skip message from non-transfer channel', []);
804 g_Net_Client_HandlePacket(ev
.packet
, g_Net_ClientLightMsgHandler
);
805 if (g_Res_received_map_start
< 0) then begin result
:= -666; exit
; end;
809 ett
:= getNewTimeoutEnd();
810 if (ev
.packet
.dataLength
< 1) then
812 e_LogWritefln('g_Net_Wait_MapInfo: invalid server packet (no data)', []);
816 Ptr
:= ev
.packet
^.data
;
817 rMsgId
:= Byte(Ptr
^);
818 e_LogWritefln('g_Net_Wait_MapInfo: got message %u from server (dataLength=%u)', [rMsgId
, ev
.packet
^.dataLength
]);
819 if (rMsgId
= NTF_SERVER_FILE_INFO
) then
821 e_LogWritefln('g_Net_Wait_MapInfo: waiting for map info reply, but got file info reply', []);
825 else if (rMsgId
= NTF_SERVER_ABORT
) then
827 e_LogWritefln('g_Net_Wait_MapInfo: server aborted transfer', []);
831 else if (rMsgId
= NTF_SERVER_MAP_INFO
) then
833 e_LogWritefln('g_Net_Wait_MapInfo: creating map info packet...', []);
834 if not msg
.Init(ev
.packet
^.data
+1, ev
.packet
^.dataLength
-1, True) then exit
;
835 e_LogWritefln('g_Net_Wait_MapInfo: parsing map info packet (rd=%d; max=%d)...', [msg
.ReadCount
, msg
.MaxSize
]);
836 SetLength(resList
, 0); // just in case
838 tf
.diskName
:= msg
.ReadString();
839 e_LogWritefln('g_Net_Wait_MapInfo: map wad is `%s`', [tf
.diskName
]);
841 tf
.hash
:= msg
.ReadMD5();
843 tf
.size
:= msg
.ReadLongInt();
844 e_LogWritefln('g_Net_Wait_MapInfo: map wad size is %d', [tf
.size
]);
845 // number of external resources for map
846 rc
:= msg
.ReadLongInt();
847 if (rc
< 0) or (rc
> 1024) then
849 e_LogWritefln('g_Net_Wait_Event: invalid number of map external resources (%d)', [rc
]);
853 e_LogWritefln('g_Net_Wait_MapInfo: map external resource count is %d', [rc
]);
854 SetLength(resList
, rc
);
855 // external resource names
856 for f
:= 0 to rc
-1 do
859 s
:= msg
.ReadString();
860 if (length(s
) = 0) then begin result
:= -1; exit
; end;
864 ri
.size
:= msg
.ReadLongInt();
865 ri
.hash
:= msg
.ReadMD5();
866 ri
.wadName
:= ExtractFileName(msg
.ReadString());
867 if (length(ri
.wadName
) = 0) or (ri
.size
< 0) then begin result
:= -1; exit
; end;
871 // old-style packet, only name
872 ri
.wadName
:= ExtractFileName(s
);
873 if (length(ri
.wadName
) = 0) then begin result
:= -1; exit
; end;
874 ri
.size
:= -1; // unknown
877 e_LogWritefln('g_Net_Wait_MapInfo: got map info', []);
878 Result
:= 0; // success
883 e_LogWritefln('g_Net_Wait_Event: invalid server packet type', []);
889 ENET_EVENT_TYPE_DISCONNECT
:
891 if (ev
.data
<= NET_DISC_MAX
) then
892 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' ' + _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + ev
.data
)], True);
898 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' unknown ENet event ' + IntToStr(Ord(ev
.kind
)), True);
903 if (freePacket
) then begin freePacket
:= false; enet_packet_destroy(ev
.packet
); end;
906 if g_Net_UserRequestExit() then
908 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' user abort', True);
914 if (freePacket
) then enet_packet_destroy(ev
.packet
);
919 // send file request to server, and wait for server reply
921 // returns `false` on error or user abort
923 // diskName (actually, base name)
932 // for maps, first `tf.diskName` name will be map wad name, and `tf.hash`/`tf.size` will contain map info
933 function g_Net_RequestResFileInfo (resIndex
: LongInt; out tf
: TNetFileTransfer
): Integer;
939 freePacket
: Boolean = false;
945 trans_omsg
.Write(Byte(NTF_CLIENT_FILE_REQUEST
));
946 trans_omsg
.Write(resIndex
);
947 if not ftransSendClientMsg(trans_omsg
) then begin result
:= -1; exit
; end;
949 FillChar(ev
, SizeOf(ev
), 0);
952 ett
:= getNewTimeoutEnd();
954 status
:= enet_host_service(NetHost
, @ev
, 300);
958 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' network error', True);
963 if (status
<= 0) then
969 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' timeout reached', True);
978 ENET_EVENT_TYPE_RECEIVE
:
981 if (ev
.channelID
<> NET_CHAN_DOWNLOAD_EX
) then
983 //e_LogWriteln('g_Net_Wait_Event: skip message from non-transfer channel');
985 g_Net_Client_HandlePacket(ev
.packet
, g_Net_ClientLightMsgHandler
);
986 if (g_Res_received_map_start
< 0) then begin result
:= -666; exit
; end;
990 ett
:= getNewTimeoutEnd();
991 if (ev
.packet
.dataLength
< 1) then
993 e_LogWriteln('g_Net_Wait_Event: invalid server packet (no data)');
997 Ptr
:= ev
.packet
^.data
;
998 rMsgId
:= Byte(Ptr
^);
999 e_LogWritefln('received transfer packet with id %d (%u bytes)', [rMsgId
, ev
.packet
^.dataLength
]);
1000 if (rMsgId
= NTF_SERVER_FILE_INFO
) then
1002 if not msg
.Init(ev
.packet
^.data
+1, ev
.packet
^.dataLength
-1, True) then exit
;
1003 tf
.hash
:= msg
.ReadMD5();
1004 tf
.size
:= msg
.ReadLongInt();
1005 tf
.chunkSize
:= msg
.ReadLongInt();
1006 tf
.diskName
:= ExtractFileName(msg
.readString());
1007 if (tf
.size
< 0) or (tf
.chunkSize
<> FILE_CHUNK_SIZE
) or (length(tf
.diskName
) = 0) then
1009 e_LogWritefln('g_Net_RequestResFileInfo: invalid file info packet', []);
1013 e_LogWritefln('got file info for resource #%d: size=%d; name=%s', [resIndex
, tf
.size
, tf
.diskName
]);
1014 Result
:= 0; // success
1017 else if (rMsgId
= NTF_SERVER_ABORT
) then
1019 e_LogWriteln('g_Net_RequestResFileInfo: server aborted transfer');
1023 else if (rMsgId
= NTF_SERVER_MAP_INFO
) then
1025 e_LogWriteln('g_Net_RequestResFileInfo: waiting for map info reply, but got file info reply');
1031 e_LogWriteln('g_Net_RequestResFileInfo: invalid server packet type');
1037 ENET_EVENT_TYPE_DISCONNECT
:
1039 if (ev
.data
<= NET_DISC_MAX
) then
1040 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' ' + _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + ev
.data
)], True);
1046 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' unknown ENet event ' + IntToStr(Ord(ev
.kind
)), True);
1051 if (freePacket
) then begin freePacket
:= false; enet_packet_destroy(ev
.packet
); end;
1054 if g_Net_UserRequestExit() then
1056 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' user abort', True);
1062 if (freePacket
) then enet_packet_destroy(ev
.packet
);
1067 // call this to cancel file transfer requested by `g_Net_RequestResFileInfo()`
1068 function g_Net_AbortResTransfer (var tf
: TNetFileTransfer
): Boolean;
1071 e_LogWritefln('aborting file transfer...', []);
1074 trans_omsg
.Write(Byte(NTF_CLIENT_ABORT
));
1075 result
:= ftransSendClientMsg(trans_omsg
);
1076 if result
then enet_host_flush(NetHost
);
1080 // call this to start file transfer requested by `g_Net_RequestResFileInfo()`
1082 // returns `false` on error or user abort
1091 // 2 on server abort
1092 // for maps, first `tf.diskName` name will be map wad name, and `tf.hash`/`tf.size` will contain map info
1093 function g_Net_ReceiveResourceFile (resIndex
: LongInt; var tf
: TNetFileTransfer
; strm
: TStream
): Integer;
1099 freePacket
: Boolean = false;
1102 nextChunk
: Integer = 0;
1103 chunkTotal
: Integer;
1110 tf
.resumed
:= false;
1111 e_LogWritefln('file `%s`, size=%d (%d)', [tf
.diskName
, Integer(strm
.size
), tf
.size
], TMsgType
.Notify
);
1112 // check if we should resume downloading
1113 resumed
:= (strm
.size
> tf
.chunkSize
) and (strm
.size
< tf
.size
);
1116 trans_omsg
.Write(Byte(NTF_CLIENT_START
));
1117 if resumed
then chunk
:= strm
.size
div tf
.chunkSize
else chunk
:= 0;
1118 trans_omsg
.Write(LongInt(chunk
));
1119 if not ftransSendClientMsg(trans_omsg
) then begin result
:= -1; exit
; end;
1121 strm
.Seek(chunk
*tf
.chunkSize
, soFromBeginning
);
1122 chunkTotal
:= (tf
.size
+tf
.chunkSize
-1) div tf
.chunkSize
;
1123 e_LogWritefln('receiving file `%s` (%d chunks)', [tf
.diskName
, chunkTotal
], TMsgType
.Notify
);
1124 g_Game_SetLoadingText('downloading "'+ExtractFileName(tf
.diskName
)+'"', chunkTotal
, False);
1125 tf
.resumed
:= resumed
;
1127 if (chunk
> 0) then g_Game_StepLoading(chunk
);
1130 // wait for reply data
1131 FillChar(ev
, SizeOf(ev
), 0);
1133 GetMem(buf
, tf
.chunkSize
);
1135 ett
:= getNewTimeoutEnd();
1137 //stx := -GetTimerMS();
1138 status
:= enet_host_service(NetHost
, @ev
, 300);
1140 if (status < 0) then
1142 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' network error', True);
1147 if (status
<= 0) then
1149 // check for timeout
1153 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' timeout reached', True);
1162 ENET_EVENT_TYPE_RECEIVE
:
1165 if (ev
.channelID
<> NET_CHAN_DOWNLOAD_EX
) then
1167 //e_LogWritefln('g_Net_Wait_Event: skip message from non-transfer channel', []);
1168 freePacket
:= false;
1169 g_Net_Client_HandlePacket(ev
.packet
, g_Net_ClientLightMsgHandler
);
1170 if (g_Res_received_map_start
< 0) then begin result
:= -666; exit
; end;
1174 //stx := stx+GetTimerMS();
1175 //e_LogWritefln('g_Net_ReceiveResourceFile: stx=%d', [Integer(stx)]);
1176 //stx := -GetTimerMS();
1177 ett
:= getNewTimeoutEnd();
1178 if (ev
.packet
.dataLength
< 1) then
1180 e_LogWritefln('g_Net_ReceiveResourceFile: invalid server packet (no data)', []);
1184 Ptr
:= ev
.packet
^.data
;
1185 rMsgId
:= Byte(Ptr
^);
1186 if (rMsgId
= NTF_SERVER_DONE
) then
1188 e_LogWritefln('file transfer complete.', []);
1192 else if (rMsgId
= NTF_SERVER_CHUNK
) then
1194 if not msg
.Init(ev
.packet
^.data
+1, ev
.packet
^.dataLength
-1, True) then exit
;
1195 chunk
:= msg
.ReadLongInt();
1196 csize
:= msg
.ReadLongInt();
1197 if (chunk
<> nextChunk
) then
1199 e_LogWritefln('received chunk %d, but expected chunk %d', [chunk
, nextChunk
]);
1203 if (csize
< 0) or (csize
> tf
.chunkSize
) then
1205 e_LogWritefln('received chunk with size %d, but expected chunk size is %d', [csize
, tf
.chunkSize
]);
1209 //e_LogWritefln('got chunk #%d of #%d (csize=%d)', [chunk, (tf.size+tf.chunkSize-1) div tf.chunkSize, csize]);
1210 msg
.ReadData(buf
, csize
);
1211 strm
.WriteBuffer(buf
^, csize
);
1212 nextChunk
:= chunk
+1;
1213 g_Game_StepLoading();
1216 trans_omsg
.Write(Byte(NTF_CLIENT_ACK
));
1217 trans_omsg
.Write(LongInt(chunk
));
1218 if not ftransSendClientMsg(trans_omsg
) then begin result
:= -1; exit
; end;
1220 else if (rMsgId
= NTF_SERVER_ABORT
) then
1222 e_LogWritefln('g_Net_ReceiveResourceFile: server aborted transfer', []);
1228 e_LogWritefln('g_Net_ReceiveResourceFile: invalid server packet type', []);
1232 //stx := stx+GetTimerMS();
1233 //e_LogWritefln('g_Net_ReceiveResourceFile: process stx=%d', [Integer(stx)]);
1236 ENET_EVENT_TYPE_DISCONNECT
:
1238 if (ev
.data
<= NET_DISC_MAX
) then
1239 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' ' + _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + ev
.data
)], True);
1245 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' unknown ENet event ' + IntToStr(Ord(ev
.kind
)), True);
1250 if (freePacket
) then begin freePacket
:= false; enet_packet_destroy(ev
.packet
); end;
1253 if g_Net_UserRequestExit() then
1255 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' user abort', True);
1262 if (freePacket
) then enet_packet_destroy(ev
.packet
);
1267 //**************************************************************************
1271 //**************************************************************************
1273 function g_Net_FindSlot(): Integer;
1282 for I
:= Low(NetClients
) to High(NetClients
) do
1284 if NetClients
[I
].Used
then
1293 if C
>= NetMaxClients
then
1301 if (Length(NetClients
) >= NetMaxClients
) then
1305 SetLength(NetClients
, Length(NetClients
) + 1);
1306 N
:= High(NetClients
);
1312 NetClients
[N
].Used
:= True;
1313 NetClients
[N
].ID
:= N
;
1314 NetClients
[N
].RequestedFullUpdate
:= False;
1315 NetClients
[N
].WaitForFirstSpawn
:= False;
1316 NetClients
[N
].RCONAuth
:= False;
1317 NetClients
[N
].Voted
:= False;
1318 NetClients
[N
].Player
:= 0;
1319 clearNetClientTransfers(NetClients
[N
]); // just in case
1326 function g_Net_Init(): Boolean;
1335 NetBuf
[NET_UNRELIABLE
].Clear();
1336 NetBuf
[NET_RELIABLE
].Clear();
1337 //SetLength(NetClients, 0);
1338 clearNetClients(true); // clear array
1344 NetAddr
.port
:= 25666;
1345 SetLength(NetBannedHosts
, 0);
1346 path
:= BANLIST_FILENAME
;
1347 if e_FindResource(DataDirs
, path
) = true then
1354 if StrToIp(IPstr
, IP
) then
1358 g_Net_SaveBanList();
1361 //Result := (enet_initialize() = 0);
1362 Result
:= enet_init_success
;
1365 procedure g_Net_Flush();
1369 F
, Chan
: enet_uint32
;
1373 Chan
:= NET_CHAN_GAME
;
1375 if NetMode
= NET_SERVER
then
1376 for T
:= NET_UNRELIABLE
to NET_RELIABLE
do
1378 if NetBuf
[T
].CurSize
> 0 then
1380 P
:= enet_packet_create(NetBuf
[T
].Data
, NetBuf
[T
].CurSize
, F
);
1381 if not Assigned(P
) then continue
;
1382 enet_host_broadcast(NetHost
, Chan
, P
);
1386 for I
:= Low(NetClients
) to High(NetClients
) do
1388 if not NetClients
[I
].Used
then continue
;
1389 if NetClients
[I
].NetOut
[T
].CurSize
<= 0 then continue
;
1390 P
:= enet_packet_create(NetClients
[I
].NetOut
[T
].Data
, NetClients
[I
].NetOut
[T
].CurSize
, F
);
1391 if not Assigned(P
) then continue
;
1392 enet_peer_send(NetClients
[I
].Peer
, Chan
, P
);
1393 NetClients
[I
].NetOut
[T
].Clear();
1396 // next and last iteration is always RELIABLE
1397 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
);
1398 Chan
:= NET_CHAN_IMPORTANT
;
1400 else if NetMode
= NET_CLIENT
then
1401 for T
:= NET_UNRELIABLE
to NET_RELIABLE
do
1403 if NetBuf
[T
].CurSize
> 0 then
1405 P
:= enet_packet_create(NetBuf
[T
].Data
, NetBuf
[T
].CurSize
, F
);
1406 if not Assigned(P
) then continue
;
1407 enet_peer_send(NetPeer
, Chan
, P
);
1410 // next and last iteration is always RELIABLE
1411 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
);
1412 Chan
:= NET_CHAN_IMPORTANT
;
1416 procedure g_Net_Cleanup();
1420 NetBuf
[NET_UNRELIABLE
].Clear();
1421 NetBuf
[NET_RELIABLE
].Clear();
1423 //SetLength(NetClients, 0);
1424 clearNetClients(true); // clear array
1425 NetClientCount
:= 0;
1429 g_Net_Slist_ServerClosed();
1433 NetState
:= NET_STATE_NONE
;
1435 NetPongSock
:= ENET_SOCKET_NULL
;
1437 NetTimeToMaster
:= 0;
1438 NetTimeToUpdate
:= 0;
1439 NetTimeToReliable
:= 0;
1441 NetMode
:= NET_NONE
;
1443 if NetPortThread
<> NilThreadId
then
1444 WaitForThreadTerminate(NetPortThread
, 66666);
1446 NetPortThread
:= NilThreadId
;
1447 g_Net_UnforwardPorts();
1453 procedure g_Net_Free();
1457 //enet_deinitialize();
1458 NetInitDone
:= False;
1462 //**************************************************************************
1466 //**************************************************************************
1468 function ForwardThread(Param
: Pointer): PtrInt
;
1471 if not g_Net_ForwardPorts() then Result
:= -1;
1474 function g_Net_Host(IPAddr
: LongWord; Port
: enet_uint16
; MaxClients
: Cardinal = 16): Boolean;
1476 if NetMode
<> NET_NONE
then
1478 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_INGAME
]);
1485 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST
], [Port
]));
1486 if not NetInitDone
then
1488 if (not g_Net_Init()) then
1490 g_Console_Add(_lc
[I_NET_MSG_FERROR
] + _lc
[I_NET_ERR_ENET
]);
1495 NetInitDone
:= True;
1498 NetAddr
.host
:= IPAddr
;
1499 NetAddr
.port
:= Port
;
1501 if NetForwardPorts
then NetPortThread
:= BeginThread(ForwardThread
);
1503 NetHost
:= enet_host_create(@NetAddr
, NET_MAXCLIENTS
, NET_CHANS
, 0, 0);
1505 if (NetHost
= nil) then
1507 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + Format(_lc
[I_NET_ERR_HOST
], [Port
]));
1513 NetPongSock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
1514 if NetPongSock
<> ENET_SOCKET_NULL
then
1516 NetPongAddr
.host
:= IPAddr
;
1517 NetPongAddr
.port
:= NET_PING_PORT
;
1518 if enet_socket_bind(NetPongSock
, @NetPongAddr
) < 0 then
1520 enet_socket_destroy(NetPongSock
);
1521 NetPongSock
:= ENET_SOCKET_NULL
;
1524 enet_socket_set_option(NetPongSock
, ENET_SOCKOPT_NONBLOCK
, 1);
1527 NetMode
:= NET_SERVER
;
1529 NetBuf
[NET_UNRELIABLE
].Clear();
1530 NetBuf
[NET_RELIABLE
].Clear();
1536 procedure g_Net_Host_Die();
1540 if NetMode
<> NET_SERVER
then Exit
;
1542 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_DISCALL
]);
1543 for I
:= 0 to High(NetClients
) do
1544 if NetClients
[I
].Used
then
1545 enet_peer_disconnect(NetClients
[I
].Peer
, NET_DISC_DOWN
);
1547 while enet_host_service(NetHost
, @NetEvent
, 1000) > 0 do
1548 if NetEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
1549 enet_packet_destroy(NetEvent
.packet
);
1551 for I
:= 0 to High(NetClients
) do
1552 if NetClients
[I
].Used
then
1554 FreeMemory(NetClients
[I
].Peer
^.data
);
1555 NetClients
[I
].Peer
^.data
:= nil;
1556 enet_peer_reset(NetClients
[I
].Peer
);
1557 NetClients
[I
].Peer
:= nil;
1558 NetClients
[I
].Used
:= False;
1559 NetClients
[I
].NetOut
[NET_UNRELIABLE
].Free();
1560 NetClients
[I
].NetOut
[NET_RELIABLE
].Free();
1563 clearNetClients(false); // don't clear array
1564 g_Net_Slist_ServerClosed();
1565 if NetPongSock
<> ENET_SOCKET_NULL
then
1566 enet_socket_destroy(NetPongSock
);
1568 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_DIE
]);
1569 enet_host_destroy(NetHost
);
1571 NetMode
:= NET_NONE
;
1574 e_WriteLog('NET: Server stopped', TMsgType
.Notify
);
1578 procedure g_Net_Host_Send(ID
: Integer; Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
1585 T
:= NET_UNRELIABLE
;
1589 if ID
> High(NetClients
) then Exit
;
1590 if NetClients
[ID
].Peer
= nil then Exit
;
1592 NetClients
[ID
].NetOut
[T
].Write(Integer(NetOut
.CurSize
));
1593 NetClients
[ID
].NetOut
[T
].Write(NetOut
);
1598 NetBuf
[T
].Write(Integer(NetOut
.CurSize
));
1599 NetBuf
[T
].Write(NetOut
);
1602 if NetDump
then g_Net_DumpSendBuffer();
1606 procedure g_Net_Host_CheckPings();
1608 ClAddr
: ENetAddress
;
1612 Ping
: array [0..9] of Byte;
1615 if NetPongSock
= ENET_SOCKET_NULL
then Exit
;
1617 Buf
.data
:= Addr(Ping
[0]);
1618 Buf
.dataLength
:= 2+8;
1622 Len
:= enet_socket_receive(NetPongSock
, @ClAddr
, @Buf
, 1);
1623 if Len
< 0 then Exit
;
1625 if (Ping
[0] = Ord('D')) and (Ping
[1] = Ord('F')) then
1627 ClTime
:= Int64(Addr(Ping
[2])^);
1630 NetOut
.Write(Byte(Ord('D')));
1631 NetOut
.Write(Byte(Ord('F')));
1632 NetOut
.Write(NetPort
);
1633 NetOut
.Write(ClTime
);
1634 TMasterHost
.writeInfo(NetOut
);
1636 if gPlayer1
<> nil then Inc(NPl
);
1637 if gPlayer2
<> nil then Inc(NPl
);
1639 NetOut
.Write(gNumBots
);
1641 Buf
.data
:= NetOut
.Data
;
1642 Buf
.dataLength
:= NetOut
.CurSize
;
1643 enet_socket_send(NetPongSock
, @ClAddr
, @Buf
, 1);
1650 function g_Net_Host_Update(): enet_size_t
;
1661 if NetUseMaster
then g_Net_Slist_Pulse();
1662 g_Net_Host_CheckPings();
1664 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
1666 case (NetEvent
.kind
) of
1667 ENET_EVENT_TYPE_CONNECT
:
1669 IP
:= IpToStr(NetEvent
.Peer
^.address
.host
);
1670 Port
:= NetEvent
.Peer
^.address
.port
;
1671 g_Console_Add(_lc
[I_NET_MSG
] +
1672 Format(_lc
[I_NET_MSG_HOST_CONN
], [IP
, Port
]));
1674 if (NetEvent
.data
<> NET_PROTOCOL_VER
) then
1676 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_REJECT
] +
1677 _lc
[I_NET_DISC_PROTOCOL
]);
1678 NetEvent
.peer
^.data
:= GetMemory(SizeOf(Byte));
1679 Byte(NetEvent
.peer
^.data
^) := 255;
1680 enet_peer_disconnect(NetEvent
.peer
, NET_DISC_PROTOCOL
);
1681 enet_host_flush(NetHost
);
1685 ID
:= g_Net_FindSlot();
1689 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_REJECT
] +
1690 _lc
[I_NET_DISC_FULL
]);
1691 NetEvent
.Peer
^.data
:= GetMemory(SizeOf(Byte));
1692 Byte(NetEvent
.peer
^.data
^) := 255;
1693 enet_peer_disconnect(NetEvent
.peer
, NET_DISC_FULL
);
1694 enet_host_flush(NetHost
);
1698 NetClients
[ID
].Peer
:= NetEvent
.peer
;
1699 NetClients
[ID
].Peer
^.data
:= GetMemory(SizeOf(Byte));
1700 Byte(NetClients
[ID
].Peer
^.data
^) := ID
;
1701 NetClients
[ID
].State
:= NET_STATE_AUTH
;
1702 NetClients
[ID
].RCONAuth
:= False;
1703 NetClients
[ID
].NetOut
[NET_UNRELIABLE
].Alloc(NET_BUFSIZE
*2);
1704 NetClients
[ID
].NetOut
[NET_RELIABLE
].Alloc(NET_BUFSIZE
*2);
1705 clearNetClientTransfers(NetClients
[ID
]); // just in case
1707 enet_peer_timeout(NetEvent
.peer
, ENET_PEER_TIMEOUT_LIMIT
* 2, ENET_PEER_TIMEOUT_MINIMUM
* 2, ENET_PEER_TIMEOUT_MAXIMUM
* 2);
1709 Inc(NetClientCount
);
1710 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST_ADD
], [ID
]));
1713 ENET_EVENT_TYPE_RECEIVE
:
1715 //e_LogWritefln('RECEIVE: chan=%u', [NetEvent.channelID]);
1716 if (NetEvent
.channelID
= NET_CHAN_DOWNLOAD_EX
) then
1718 ProcessDownloadExPacket();
1722 ID
:= Byte(NetEvent
.peer
^.data
^);
1723 if ID
> High(NetClients
) then Exit
;
1724 TC
:= @NetClients
[ID
];
1726 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
1727 g_Net_Host_HandlePacket(TC
, NetEvent
.packet
, g_Net_HostMsgHandler
);
1731 ENET_EVENT_TYPE_DISCONNECT
:
1733 ID
:= Byte(NetEvent
.peer
^.data
^);
1734 if ID
> High(NetClients
) then Exit
;
1735 clearNetClient(NetClients
[ID
]);
1736 TC
:= @NetClients
[ID
];
1737 if TC
= nil then Exit
;
1739 if not (TC
^.Used
) then Exit
;
1741 TP
:= g_Player_Get(TC
^.Player
);
1746 TP
.Kill(K_SIMPLEKILL
, 0, HIT_DISCON
);
1747 g_Console_Add(Format(_lc
[I_PLAYER_LEAVE
], [TP
.Name
]), True);
1748 e_WriteLog('NET: Client ' + TP
.Name
+ ' [' + IntToStr(ID
) + '] disconnected.', TMsgType
.Notify
);
1749 g_Player_Remove(TP
.UID
);
1753 TC
^.State
:= NET_STATE_NONE
;
1756 TC
^.RequestedFullUpdate
:= False;
1757 TC
^.WaitForFirstSpawn
:= False;
1758 TC
^.NetOut
[NET_UNRELIABLE
].Free();
1759 TC
^.NetOut
[NET_RELIABLE
].Free();
1761 FreeMemory(NetEvent
.peer
^.data
);
1762 NetEvent
.peer
^.data
:= nil;
1763 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST_DISC
], [ID
]));
1764 Dec(NetClientCount
);
1766 if NetUseMaster
then g_Net_Slist_ServerPlayerLeaves();
1773 //**************************************************************************
1777 //**************************************************************************
1779 procedure g_Net_Disconnect(Forced
: Boolean = False);
1781 if NetMode
<> NET_CLIENT
then Exit
;
1782 if (NetHost
= nil) or (NetPeer
= nil) then Exit
;
1786 enet_peer_disconnect(NetPeer
, NET_DISC_NONE
);
1788 while (enet_host_service(NetHost
, @NetEvent
, 1500) > 0) do
1790 if (NetEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
) then
1796 if (NetEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
) then
1797 enet_packet_destroy(NetEvent
.packet
);
1800 if NetPeer
<> nil then
1802 enet_peer_reset(NetPeer
);
1808 e_WriteLog('NET: Kicked from server: ' + IntToStr(NetEvent
.data
), TMsgType
.Notify
);
1809 if (NetEvent
.data
<= NET_DISC_MAX
) then
1810 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_KICK
] +
1811 _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + NetEvent
.data
)], True);
1814 if NetHost
<> nil then
1816 enet_host_destroy(NetHost
);
1819 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_CLIENT_DISC
]);
1822 e_WriteLog('NET: Disconnected', TMsgType
.Notify
);
1825 procedure g_Net_Client_Send(Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
1832 T
:= NET_UNRELIABLE
;
1835 NetBuf
[T
].Write(Integer(NetOut
.CurSize
));
1836 NetBuf
[T
].Write(NetOut
);
1838 if NetDump
then g_Net_DumpSendBuffer();
1840 g_Net_Flush(); // FIXME: for now, send immediately
1843 function g_Net_Client_Update(): enet_size_t
;
1846 while (NetHost
<> nil) and (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
1848 case NetEvent
.kind
of
1849 ENET_EVENT_TYPE_RECEIVE
:
1851 if (NetEvent
.channelID
= NET_CHAN_DOWNLOAD_EX
) then continue
; // ignore all download packets, they're processed by separate code
1852 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
1853 g_Net_Client_HandlePacket(NetEvent
.packet
, g_Net_ClientMsgHandler
);
1856 ENET_EVENT_TYPE_DISCONNECT
:
1858 g_Net_Disconnect(True);
1866 function g_Net_Client_UpdateWhileLoading(): enet_size_t
;
1869 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
1871 case NetEvent
.kind
of
1872 ENET_EVENT_TYPE_RECEIVE
:
1874 if (NetEvent
.channelID
= NET_CHAN_DOWNLOAD_EX
) then continue
; // ignore all download packets, they're processed by separate code
1875 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
1876 g_Net_Client_HandlePacket(NetEvent
.packet
, g_Net_ClientLightMsgHandler
);
1879 ENET_EVENT_TYPE_DISCONNECT
:
1881 g_Net_Disconnect(True);
1890 function g_Net_Connect(IP
: string; Port
: enet_uint16
): Boolean;
1893 TimeoutTime
, T
: Int64;
1895 if NetMode
<> NET_NONE
then
1897 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_ERR_INGAME
], True);
1904 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_CLIENT_CONN
],
1906 if not NetInitDone
then
1908 if (not g_Net_Init()) then
1910 g_Console_Add(_lc
[I_NET_MSG_FERROR
] + _lc
[I_NET_ERR_ENET
], True);
1915 NetInitDone
:= True;
1918 NetHost
:= enet_host_create(nil, 1, NET_CHANS
, 0, 0);
1920 if (NetHost
= nil) then
1922 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
1928 enet_address_set_host(@NetAddr
, PChar(Addr(IP
[1])));
1929 NetAddr
.port
:= Port
;
1931 NetPeer
:= enet_host_connect(NetHost
, @NetAddr
, NET_CHANS
, NET_PROTOCOL_VER
);
1933 if (NetPeer
= nil) then
1935 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
1936 enet_host_destroy(NetHost
);
1942 // предупредить что ждем слишком долго через N секунд
1943 TimeoutTime
:= sys_GetTicks() + NET_CONNECT_TIMEOUT
;
1948 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
1950 if (NetEvent
.kind
= ENET_EVENT_TYPE_CONNECT
) then
1952 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_CLIENT_DONE
]);
1953 NetMode
:= NET_CLIENT
;
1955 enet_peer_timeout(NetPeer
, ENET_PEER_TIMEOUT_LIMIT
* 2, ENET_PEER_TIMEOUT_MINIMUM
* 2, ENET_PEER_TIMEOUT_MAXIMUM
* 2);
1957 NetClientPort
:= Port
;
1964 T
:= sys_GetTicks();
1965 if T
> TimeoutTime
then
1967 TimeoutTime
:= T
+ NET_CONNECT_TIMEOUT
* 100; // одного предупреждения хватит
1968 g_Console_Add(_lc
[I_NET_MSG_TIMEOUT_WARN
], True);
1969 g_Console_Add(Format(_lc
[I_NET_MSG_PORTS
], [Integer(Port
), Integer(NET_PING_PORT
)]), True);
1972 ProcessLoading(true);
1974 if e_KeyPressed(IK_SPACE
) or e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(VK_ESCAPE
) or
1975 e_KeyPressed(JOY0_JUMP
) or e_KeyPressed(JOY1_JUMP
) or e_KeyPressed(JOY2_JUMP
) or e_KeyPressed(JOY3_JUMP
) then
1979 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_TIMEOUT
], True);
1980 g_Console_Add(Format(_lc
[I_NET_MSG_PORTS
], [Integer(Port
), Integer(NET_PING_PORT
)]), True);
1981 if NetPeer
<> nil then enet_peer_reset(NetPeer
);
1982 if NetHost
<> nil then
1984 enet_host_destroy(NetHost
);
1991 function IpToStr(IP
: LongWord): string;
1996 Result
:= IntToStr(PByte(Ptr
+ 0)^) + '.';
1997 Result
:= Result
+ IntToStr(PByte(Ptr
+ 1)^) + '.';
1998 Result
:= Result
+ IntToStr(PByte(Ptr
+ 2)^) + '.';
1999 Result
:= Result
+ IntToStr(PByte(Ptr
+ 3)^);
2002 function StrToIp(IPstr
: string; var IP
: LongWord): Boolean;
2006 Result
:= enet_address_set_host(@EAddr
, PChar(@IPstr
[1])) = 0;
2010 function g_Net_Client_ByName(Name
: string): pTNetClient
;
2016 for a
:= Low(NetClients
) to High(NetClients
) do
2017 if (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
2019 pl
:= g_Player_Get(NetClients
[a
].Player
);
2020 if pl
= nil then continue
;
2021 if Copy(LowerCase(pl
.Name
), 1, Length(Name
)) <> LowerCase(Name
) then continue
;
2022 if NetClients
[a
].Peer
<> nil then
2024 Result
:= @NetClients
[a
];
2030 function g_Net_Client_ByPlayer(PID
: Word): pTNetClient
;
2035 for a
:= Low(NetClients
) to High(NetClients
) do
2036 if (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
2037 if NetClients
[a
].Player
= PID
then
2039 Result
:= @NetClients
[a
];
2044 function g_Net_ClientName_ByID(ID
: Integer): string;
2050 if ID
= NET_EVERYONE
then
2052 for a
:= Low(NetClients
) to High(NetClients
) do
2053 if (NetClients
[a
].ID
= ID
) and (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
2055 pl
:= g_Player_Get(NetClients
[a
].Player
);
2056 if pl
= nil then Exit
;
2061 procedure g_Net_SendData(Data
: AByte
; peer
: pENetPeer
; Reliable
: Boolean; Chan
: Byte = NET_CHAN_DOWNLOAD
);
2065 dataLength
: Cardinal;
2067 dataLength
:= Length(Data
);
2070 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
)
2074 if (peer
<> nil) then
2076 P
:= enet_packet_create(@Data
[0], dataLength
, F
);
2077 if not Assigned(P
) then Exit
;
2078 enet_peer_send(peer
, Chan
, P
);
2082 P
:= enet_packet_create(@Data
[0], dataLength
, F
);
2083 if not Assigned(P
) then Exit
;
2084 enet_host_broadcast(NetHost
, Chan
, P
);
2087 enet_host_flush(NetHost
);
2090 function g_Net_IsHostBanned(IP
: LongWord; Perm
: Boolean = False): Boolean;
2095 if NetBannedHosts
= nil then
2097 for I
:= 0 to High(NetBannedHosts
) do
2098 if (NetBannedHosts
[I
].IP
= IP
) and ((not Perm
) or (NetBannedHosts
[I
].Perm
)) then
2105 procedure g_Net_BanHost(IP
: LongWord; Perm
: Boolean = True); overload
;
2111 if g_Net_IsHostBanned(IP
, Perm
) then
2115 for I
:= Low(NetBannedHosts
) to High(NetBannedHosts
) do
2116 if NetBannedHosts
[I
].IP
= 0 then
2124 SetLength(NetBannedHosts
, Length(NetBannedHosts
) + 1);
2125 P
:= High(NetBannedHosts
);
2128 NetBannedHosts
[P
].IP
:= IP
;
2129 NetBannedHosts
[P
].Perm
:= Perm
;
2132 procedure g_Net_BanHost(IP
: string; Perm
: Boolean = True); overload
;
2137 b
:= StrToIp(IP
, a
);
2139 g_Net_BanHost(a
, Perm
);
2142 procedure g_Net_UnbanNonPermHosts();
2146 if NetBannedHosts
= nil then
2148 for I
:= Low(NetBannedHosts
) to High(NetBannedHosts
) do
2149 if (NetBannedHosts
[I
].IP
> 0) and not NetBannedHosts
[I
].Perm
then
2151 NetBannedHosts
[I
].IP
:= 0;
2152 NetBannedHosts
[I
].Perm
:= True;
2156 function g_Net_UnbanHost(IP
: string): Boolean; overload
;
2160 Result
:= StrToIp(IP
, a
);
2162 Result
:= g_Net_UnbanHost(a
);
2165 function g_Net_UnbanHost(IP
: LongWord): Boolean; overload
;
2172 if NetBannedHosts
= nil then
2174 for I
:= 0 to High(NetBannedHosts
) do
2175 if NetBannedHosts
[I
].IP
= IP
then
2177 NetBannedHosts
[I
].IP
:= 0;
2178 NetBannedHosts
[I
].Perm
:= True;
2180 // no break here to clear all bans of this host, perm and non-perm
2184 procedure g_Net_SaveBanList();
2190 path
:= e_GetWriteableDir(DataDirs
);
2193 path
:= e_CatPath(path
, BANLIST_FILENAME
);
2196 if NetBannedHosts
<> nil then
2197 for I
:= 0 to High(NetBannedHosts
) do
2198 if NetBannedHosts
[I
].Perm
and (NetBannedHosts
[I
].IP
> 0) then
2199 Writeln(F
, IpToStr(NetBannedHosts
[I
].IP
));
2204 procedure g_Net_DumpStart();
2206 if NetMode
= NET_SERVER
then
2207 NetDumpFile
:= e_CreateResource(LogDirs
, NETDUMP_FILENAME
+ '_server')
2209 NetDumpFile
:= e_CreateResource(LogDirs
, NETDUMP_FILENAME
+ '_client');
2212 procedure g_Net_DumpSendBuffer();
2214 writeInt(NetDumpFile
, gTime
);
2215 writeInt(NetDumpFile
, LongWord(NetOut
.CurSize
));
2216 writeInt(NetDumpFile
, Byte(1));
2217 NetDumpFile
.WriteBuffer(NetOut
.Data
^, NetOut
.CurSize
);
2220 procedure g_Net_DumpRecvBuffer(Buf
: penet_uint8
; Len
: LongWord);
2222 if (Buf
= nil) or (Len
= 0) then Exit
;
2223 writeInt(NetDumpFile
, gTime
);
2224 writeInt(NetDumpFile
, Len
);
2225 writeInt(NetDumpFile
, Byte(0));
2226 NetDumpFile
.WriteBuffer(Buf
^, Len
);
2229 procedure g_Net_DumpEnd();
2235 function g_Net_ForwardPorts(ForwardPongPort
: Boolean = True): Boolean;
2236 {$IFDEF USE_MINIUPNPC}
2241 LanAddr
: array [0..255] of Char;
2242 StrPort
: AnsiString;
2247 if NetPortForwarded
= NetPort
then
2253 NetPongForwarded
:= False;
2254 NetPortForwarded
:= 0;
2256 DevList
:= upnpDiscover(1000, nil, nil, 0, 0, 2, Addr(Err
));
2257 if DevList
= nil then
2259 conwritefln('port forwarding failed: upnpDiscover() failed: %d', [Err
]);
2263 I
:= UPNP_GetValidIGD(DevList
, @Urls
, @Data
, Addr(LanAddr
[0]), 256);
2267 conwriteln('port forwarding failed: could not find an IGD device on this LAN');
2268 FreeUPNPDevList(DevList
);
2269 FreeUPNPUrls(@Urls
);
2273 StrPort
:= IntToStr(NetPort
);
2274 I
:= UPNP_AddPortMapping(
2275 Urls
.controlURL
, Addr(data
.first
.servicetype
[1]),
2276 PChar(StrPort
), PChar(StrPort
), Addr(LanAddr
[0]), PChar('D2DF'),
2277 PChar('UDP'), nil, PChar('0')
2282 conwritefln('forwarding port %d failed: error %d', [NetPort
, I
]);
2283 FreeUPNPDevList(DevList
);
2284 FreeUPNPUrls(@Urls
);
2288 if ForwardPongPort
then
2290 StrPort
:= IntToStr(NET_PING_PORT
);
2291 I
:= UPNP_AddPortMapping(
2292 Urls
.controlURL
, Addr(data
.first
.servicetype
[1]),
2293 PChar(StrPort
), PChar(StrPort
), Addr(LanAddr
[0]), PChar('D2DF'),
2294 PChar('UDP'), nil, PChar('0')
2299 conwritefln('forwarding port %d failed: error %d', [NET_PING_PORT
, I
]);
2300 NetPongForwarded
:= False;
2304 conwritefln('forwarded port %d successfully', [NET_PING_PORT
]);
2305 NetPongForwarded
:= True;
2309 conwritefln('forwarded port %d successfully', [NetPort
]);
2310 NetIGDControl
:= AnsiString(Urls
.controlURL
);
2311 NetIGDService
:= data
.first
.servicetype
;
2312 NetPortForwarded
:= NetPort
;
2314 FreeUPNPDevList(DevList
);
2315 FreeUPNPUrls(@Urls
);
2324 procedure g_Net_UnforwardPorts();
2325 {$IFDEF USE_MINIUPNPC}
2328 StrPort
: AnsiString;
2330 if NetPortForwarded
= 0 then Exit
;
2332 conwriteln('unforwarding ports...');
2334 StrPort
:= IntToStr(NetPortForwarded
);
2335 I
:= UPNP_DeletePortMapping(
2336 PChar(NetIGDControl
), Addr(NetIGDService
[1]),
2337 PChar(StrPort
), PChar('UDP'), nil
2339 conwritefln(' port %d: %d', [NetPortForwarded
, I
]);
2341 if NetPongForwarded
then
2343 NetPongForwarded
:= False;
2344 StrPort
:= IntToStr(NET_PING_PORT
);
2345 I
:= UPNP_DeletePortMapping(
2346 PChar(NetIGDControl
), Addr(NetIGDService
[1]),
2347 PChar(StrPort
), PChar('UDP'), nil
2349 conwritefln(' port %d: %d', [NET_PING_PORT
, I
]);
2352 NetPortForwarded
:= 0;
2359 procedure NetServerCVars(P
: SSArray
);
2364 cmd
:= LowerCase(P
[0]);
2368 if (Length(P
) > 1) and (Length(P
[1]) > 0) then
2370 NetServerName
:= P
[1];
2371 if Length(NetServerName
) > 64 then
2372 SetLength(NetServerName
, 64);
2373 g_Net_Slist_ServerRenamed();
2375 g_Console_Add(cmd
+ ' "' + NetServerName
+ '"');
2379 if (Length(P
) > 1) and (Length(P
[1]) > 0) then
2381 NetPassword
:= P
[1];
2382 if Length(NetPassword
) > 24 then
2383 SetLength(NetPassword
, 24);
2384 g_Net_Slist_ServerRenamed();
2386 g_Console_Add(cmd
+ ' "' + AnsiLowerCase(NetPassword
) + '"');
2390 if (Length(P
) > 1) then
2392 NetMaxClients
:= nclamp(StrToIntDef(P
[1], NetMaxClients
), 1, NET_MAXCLIENTS
);
2393 if g_Game_IsServer
and g_Game_IsNet
then
2396 for a
:= 0 to High(NetClients
) do
2398 if NetClients
[a
].Used
then
2401 if b
> NetMaxClients
then
2403 s
:= g_Player_Get(NetClients
[a
].Player
).Name
;
2404 enet_peer_disconnect(NetClients
[a
].Peer
, NET_DISC_FULL
);
2405 g_Console_Add(Format(_lc
[I_PLAYER_KICK
], [s
]));
2406 MH_SEND_GameEvent(NET_EV_PLAYER_KICK
, 0, s
);
2410 g_Net_Slist_ServerRenamed();
2413 g_Console_Add(cmd
+ ' ' + IntToStr(NetMaxClients
));
2417 if (Length(P
) > 1) then
2419 NetUseMaster
:= StrToIntDef(P
[1], Byte(NetUseMaster
)) <> 0;
2420 if NetUseMaster
then g_Net_Slist_Public() else g_Net_Slist_Private();
2422 g_Console_Add(cmd
+ ' ' + IntToStr(Byte(NetUseMaster
)));
2426 if (Length(P
) > 1) then
2428 if not g_Game_IsNet
then
2429 NetPort
:= nclamp(StrToIntDef(P
[1], NetPort
), 0, $FFFF)
2431 g_Console_Add(_lc
[I_MSG_NOT_NETGAME
]);
2433 g_Console_Add(cmd
+ ' ' + IntToStr(Ord(NetUseMaster
)));
2439 conRegVar('cl_downloadtimeout', @g_Net_DownloadTimeout
, 0.0, 1000000.0, '', 'timeout in seconds, 0 to disable it');
2440 conRegVar('cl_predictself', @NetPredictSelf
, '', 'predict local player');
2441 conRegVar('cl_forceplayerupdate', @NetForcePlayerUpdate
, '', 'update net players on NET_MSG_PLRPOS');
2442 conRegVar('cl_interp', @NetInterpLevel
, '', 'net player interpolation steps');
2443 conRegVar('cl_last_ip', @NetClientIP
, '', 'address of the last you have connected to');
2444 conRegVar('cl_last_port', @NetClientPort
, '', 'port of the last server you have connected to');
2446 conRegVar('sv_forwardports', @NetForwardPorts
, '', 'forward server port using miniupnpc (requires server restart)');
2447 conRegVar('sv_rcon', @NetAllowRCON
, '', 'enable remote console');
2448 conRegVar('sv_rcon_password', @NetRCONPassword
, '', 'remote console password');
2449 conRegVar('sv_update_interval', @NetUpdateRate
, '', 'unreliable update interval');
2450 conRegVar('sv_reliable_interval', @NetRelupdRate
, '', 'reliable update interval');
2451 conRegVar('sv_master_interval', @NetMasterRate
, '', 'master server update interval');
2453 conRegVar('net_master_list', @NetMasterList
, '', 'list of master servers');
2455 SetLength(NetClients
, 0);
2456 g_Net_DownloadTimeout
:= 60;
2457 NetIn
.Alloc(NET_BUFSIZE
);
2458 NetOut
.Alloc(NET_BUFSIZE
);
2459 NetBuf
[NET_UNRELIABLE
].Alloc(NET_BUFSIZE
*2);
2460 NetBuf
[NET_RELIABLE
].Alloc(NET_BUFSIZE
*2);
2461 trans_omsg
.Alloc(NET_BUFSIZE
);
2465 NetBuf
[NET_UNRELIABLE
].Free();
2466 NetBuf
[NET_RELIABLE
].Free();