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, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
22 e_log
, e_msg
, ENet
, Classes
, MAPDEF
{$IFDEF USE_MINIUPNPC}, miniupnpc
;{$ELSE};{$ENDIF}
25 NET_PROTOCOL_VER
= 173;
31 NET_CHAN_IMPORTANT
= 1;
34 NET_CHAN_PLAYERPOS
= 4;
36 NET_CHAN_MONSTERPOS
= 6;
37 NET_CHAN_LARGEDATA
= 7;
39 NET_CHAN_DOWNLOAD
= 9;
50 NET_DISC_NONE
: enet_uint32
= 0;
51 NET_DISC_PROTOCOL
: enet_uint32
= 1;
52 NET_DISC_VERSION
: enet_uint32
= 2;
53 NET_DISC_FULL
: enet_uint32
= 3;
54 NET_DISC_KICK
: enet_uint32
= 4;
55 NET_DISC_DOWN
: enet_uint32
= 5;
56 NET_DISC_PASSWORD
: enet_uint32
= 6;
57 NET_DISC_TEMPBAN
: enet_uint32
= 7;
58 NET_DISC_BAN
: enet_uint32
= 8;
59 NET_DISC_MAX
: enet_uint32
= 8;
65 BANLIST_FILENAME
= 'banlist.txt';
66 NETDUMP_FILENAME
= 'netdump';
75 RequestedFullUpdate
: Boolean;
83 pTNetClient
= ^TNetClient
;
85 AByte
= array of Byte;
88 NetInitDone
: Boolean = False;
89 NetMode
: Byte = NET_NONE
;
90 NetDump
: Boolean = False;
92 NetServerName
: string = 'Unnamed Server';
93 NetPassword
: string = '';
94 NetPort
: Word = 25666;
96 NetAllowRCON
: Boolean = False;
97 NetRCONPassword
: string = '';
99 NetTimeToUpdate
: Cardinal = 0;
100 NetTimeToReliable
: Cardinal = 0;
101 NetTimeToMaster
: Cardinal = 0;
103 NetHost
: pENetHost
= nil;
104 NetPeer
: pENetPeer
= nil;
106 NetAddr
: ENetAddress
;
108 NetPongAddr
: ENetAddress
;
109 NetPongSock
: ENetSocket
= ENET_SOCKET_NULL
;
111 NetUseMaster
: Boolean = True;
112 NetSlistAddr
: ENetAddress
;
113 NetSlistIP
: string = 'mpms.doom2d.org';
114 NetSlistPort
: Word = 25665;
116 NetClientIP
: string = '127.0.0.1';
117 NetClientPort
: Word = 25666;
121 NetClients
: array of TNetClient
;
122 NetClientCount
: Byte = 0;
123 NetMaxClients
: Byte = 255;
124 NetBannedHosts
: array of TBanRecord
;
126 NetState
: Integer = NET_STATE_NONE
;
128 NetMyID
: Integer = -1;
129 NetPlrUID1
: Integer = -1;
130 NetPlrUID2
: Integer = -1;
132 NetInterpLevel
: Integer = 1;
133 NetUpdateRate
: Cardinal = 0; // as soon as possible
134 NetRelupdRate
: Cardinal = 18; // around two times a second
135 NetMasterRate
: Cardinal = 60000;
137 NetForcePlayerUpdate
: Boolean = False;
138 NetPredictSelf
: Boolean = True;
139 NetForwardPorts
: Boolean = False;
141 NetGotEverything
: Boolean = False;
142 NetGotKeys
: Boolean = False;
144 {$IFDEF USE_MINIUPNPC}
145 NetPortForwarded
: Word = 0;
146 NetPongForwarded
: Boolean = False;
147 NetIGDControl
: AnsiString;
148 NetIGDService
: TURLStr
;
149 NetPortThread
: TThreadID
= 0;
152 NetDumpFile
: TStream
;
154 function g_Net_Init(): Boolean;
155 procedure g_Net_Cleanup();
156 procedure g_Net_Free();
157 procedure g_Net_Flush();
159 function g_Net_Host(IPAddr
: LongWord; Port
: enet_uint16
; MaxClients
: Cardinal = 16): Boolean;
160 procedure g_Net_Host_Die();
161 procedure g_Net_Host_Send(ID
: Integer; Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
162 function g_Net_Host_Update(): enet_size_t
;
164 function g_Net_Connect(IP
: string; Port
: enet_uint16
): Boolean;
165 procedure g_Net_Disconnect(Forced
: Boolean = False);
166 procedure g_Net_Client_Send(Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
167 function g_Net_Client_Update(): enet_size_t
;
168 function g_Net_Client_UpdateWhileLoading(): enet_size_t
;
170 function g_Net_Client_ByName(Name
: string): pTNetClient
;
171 function g_Net_Client_ByPlayer(PID
: Word): pTNetClient
;
172 function g_Net_ClientName_ByID(ID
: Integer): string;
174 procedure g_Net_SendData(Data
:AByte
; peer
: pENetPeer
; Reliable
: Boolean; Chan
: Byte = NET_CHAN_DOWNLOAD
);
175 function g_Net_Wait_Event(msgId
: Word): TMemoryStream
;
177 function IpToStr(IP
: LongWord): string;
178 function StrToIp(IPstr
: string; var IP
: LongWord): Boolean;
180 function g_Net_IsHostBanned(IP
: LongWord; Perm
: Boolean = False): Boolean;
181 procedure g_Net_BanHost(IP
: LongWord; Perm
: Boolean = True); overload
;
182 procedure g_Net_BanHost(IP
: string; Perm
: Boolean = True); overload
;
183 function g_Net_UnbanHost(IP
: string): Boolean; overload
;
184 function g_Net_UnbanHost(IP
: LongWord): Boolean; overload
;
185 procedure g_Net_UnbanNonPermHosts();
186 procedure g_Net_SaveBanList();
188 procedure g_Net_DumpStart();
189 procedure g_Net_DumpSendBuffer();
190 procedure g_Net_DumpRecvBuffer(Buf
: penet_uint8
; Len
: LongWord);
191 procedure g_Net_DumpEnd();
193 function g_Net_ForwardPorts(ForwardPongPort
: Boolean = True): Boolean;
194 procedure g_Net_UnforwardPorts();
200 e_input
, g_nethandler
, g_netmsg
, g_netmaster
, g_player
, g_window
, g_console
,
201 g_main
, g_game
, g_language
, g_weapons
, utils
;
204 { /// SERVICE FUNCTIONS /// }
207 function g_Net_FindSlot(): Integer;
216 for I
:= Low(NetClients
) to High(NetClients
) do
218 if NetClients
[I
].Used
then
227 if C
>= NetMaxClients
then
235 if (Length(NetClients
) >= NetMaxClients
) then
239 SetLength(NetClients
, Length(NetClients
) + 1);
240 N
:= High(NetClients
);
246 NetClients
[N
].Used
:= True;
247 NetClients
[N
].ID
:= N
;
248 NetClients
[N
].RequestedFullUpdate
:= False;
249 NetClients
[N
].RCONAuth
:= False;
250 NetClients
[N
].Voted
:= False;
251 NetClients
[N
].Player
:= 0;
257 function g_Net_Init(): Boolean;
265 SetLength(NetClients
, 0);
271 NetAddr
.port
:= 25666;
272 SetLength(NetBannedHosts
, 0);
273 if FileExists(DataDir
+ BANLIST_FILENAME
) then
275 Assign(F
, DataDir
+ BANLIST_FILENAME
);
280 if StrToIp(IPstr
, IP
) then
287 Result
:= (enet_initialize() = 0);
290 procedure g_Net_Flush();
292 enet_host_flush(NetHost
);
295 procedure g_Net_Cleanup();
300 SetLength(NetClients
, 0);
310 NetState
:= NET_STATE_NONE
;
312 NetPongSock
:= ENET_SOCKET_NULL
;
314 NetTimeToMaster
:= 0;
315 NetTimeToUpdate
:= 0;
316 NetTimeToReliable
:= 0;
320 if NetPortThread
<> 0 then
321 WaitForThreadTerminate(NetPortThread
, 66666);
324 g_Net_UnforwardPorts();
330 procedure g_Net_Free();
335 NetInitDone
:= False;
339 { /// SERVER FUNCTIONS /// }
342 function ForwardThread(Param
: Pointer): PtrInt
;
345 if not g_Net_ForwardPorts() then Result
:= -1;
348 function g_Net_Host(IPAddr
: LongWord; Port
: enet_uint16
; MaxClients
: Cardinal = 16): Boolean;
350 if NetMode
<> NET_NONE
then
352 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_INGAME
]);
359 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST
], [Port
]));
360 if not NetInitDone
then
362 if (not g_Net_Init()) then
364 g_Console_Add(_lc
[I_NET_MSG_FERROR
] + _lc
[I_NET_ERR_ENET
]);
372 NetAddr
.host
:= IPAddr
;
373 NetAddr
.port
:= Port
;
375 if NetForwardPorts
then NetPortThread
:= BeginThread(ForwardThread
);
377 NetHost
:= enet_host_create(@NetAddr
, NET_MAXCLIENTS
, NET_CHANS
, 0, 0);
379 if (NetHost
= nil) then
381 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + Format(_lc
[I_NET_ERR_HOST
], [Port
]));
387 NetPongSock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
388 if NetPongSock
<> ENET_SOCKET_NULL
then
390 NetPongAddr
.host
:= IPAddr
;
391 NetPongAddr
.port
:= Port
+ 1;
392 if enet_socket_bind(NetPongSock
, @NetPongAddr
) < 0 then
394 enet_socket_destroy(NetPongSock
);
395 NetPongSock
:= ENET_SOCKET_NULL
;
398 enet_socket_set_option(NetPongSock
, ENET_SOCKOPT_NONBLOCK
, 1);
401 NetMode
:= NET_SERVER
;
408 procedure g_Net_Host_Die();
412 if NetMode
<> NET_SERVER
then Exit
;
414 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_DISCALL
]);
415 for I
:= 0 to High(NetClients
) do
416 if NetClients
[I
].Used
then
417 enet_peer_disconnect(NetClients
[I
].Peer
, NET_DISC_DOWN
);
419 while enet_host_service(NetHost
, @NetEvent
, 1000) > 0 do
420 if NetEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
421 enet_packet_destroy(NetEvent
.packet
);
423 for I
:= 0 to High(NetClients
) do
424 if NetClients
[I
].Used
then
426 FreeMemory(NetClients
[I
].Peer
^.data
);
427 NetClients
[I
].Peer
^.data
:= nil;
428 enet_peer_reset(NetClients
[I
].Peer
);
429 NetClients
[I
].Peer
:= nil;
430 NetClients
[I
].Used
:= False;
433 if (NetMPeer
<> nil) and (NetMHost
<> nil) then g_Net_Slist_Disconnect
;
434 if NetPongSock
<> ENET_SOCKET_NULL
then
435 enet_socket_destroy(NetPongSock
);
437 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_DIE
]);
438 enet_host_destroy(NetHost
);
443 e_WriteLog('NET: Server stopped', TMsgType
.Notify
);
447 procedure g_Net_Host_Send(ID
: Integer; Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
453 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
)
459 if ID
> High(NetClients
) then Exit
;
460 if NetClients
[ID
].Peer
= nil then Exit
;
462 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, F
);
463 if not Assigned(P
) then Exit
;
465 enet_peer_send(NetClients
[ID
].Peer
, Chan
, P
);
469 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, F
);
470 if not Assigned(P
) then Exit
;
472 enet_host_broadcast(NetHost
, Chan
, P
);
475 if NetDump
then g_Net_DumpSendBuffer();
480 procedure g_Net_Host_CheckPings();
486 Ping
: array [0..9] of Byte;
489 if NetPongSock
= ENET_SOCKET_NULL
then Exit
;
491 Buf
.data
:= Addr(Ping
[0]);
492 Buf
.dataLength
:= 2+8;
496 Len
:= enet_socket_receive(NetPongSock
, @ClAddr
, @Buf
, 1);
497 if Len
< 0 then Exit
;
499 if (Ping
[0] = Ord('D')) and (Ping
[1] = Ord('F')) then
501 ClTime
:= Int64(Addr(Ping
[2])^);
504 NetOut
.Write(Byte(Ord('D')));
505 NetOut
.Write(Byte(Ord('F')));
506 NetOut
.Write(ClTime
);
507 g_Net_Slist_WriteInfo();
509 if gPlayer1
<> nil then Inc(NPl
);
510 if gPlayer2
<> nil then Inc(NPl
);
512 NetOut
.Write(gNumBots
);
514 Buf
.data
:= NetOut
.Data
;
515 Buf
.dataLength
:= NetOut
.CurSize
;
516 enet_socket_send(NetPongSock
, @ClAddr
, @Buf
, 1);
522 function g_Net_Host_Update(): enet_size_t
;
536 g_Net_Host_CheckPings
;
539 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
541 case (NetEvent
.kind
) of
542 ENET_EVENT_TYPE_CONNECT
:
544 IP
:= IpToStr(NetEvent
.Peer
^.address
.host
);
545 Port
:= NetEvent
.Peer
^.address
.port
;
546 g_Console_Add(_lc
[I_NET_MSG
] +
547 Format(_lc
[I_NET_MSG_HOST_CONN
], [IP
, Port
]));
549 if (NetEvent
.data
<> NET_PROTOCOL_VER
) then
551 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_REJECT
] +
552 _lc
[I_NET_DISC_PROTOCOL
]);
553 NetEvent
.peer
^.data
:= GetMemory(SizeOf(Byte));
554 Byte(NetEvent
.peer
^.data
^) := 255;
555 enet_peer_disconnect(NetEvent
.peer
, NET_DISC_PROTOCOL
);
556 enet_host_flush(NetHost
);
560 ID
:= g_Net_FindSlot();
564 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_REJECT
] +
565 _lc
[I_NET_DISC_FULL
]);
566 NetEvent
.Peer
^.data
:= GetMemory(SizeOf(Byte));
567 Byte(NetEvent
.peer
^.data
^) := 255;
568 enet_peer_disconnect(NetEvent
.peer
, NET_DISC_FULL
);
569 enet_host_flush(NetHost
);
573 NetClients
[ID
].Peer
:= NetEvent
.peer
;
574 NetClients
[ID
].Peer
^.data
:= GetMemory(SizeOf(Byte));
575 Byte(NetClients
[ID
].Peer
^.data
^) := ID
;
576 NetClients
[ID
].State
:= NET_STATE_AUTH
;
577 NetClients
[ID
].RCONAuth
:= False;
579 enet_peer_timeout(NetEvent
.peer
, ENET_PEER_TIMEOUT_LIMIT
* 2, ENET_PEER_TIMEOUT_MINIMUM
* 2, ENET_PEER_TIMEOUT_MAXIMUM
* 2);
582 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST_ADD
], [ID
]));
585 ENET_EVENT_TYPE_RECEIVE
:
587 ID
:= Byte(NetEvent
.peer
^.data
^);
588 if ID
> High(NetClients
) then Exit
;
589 TC
:= @NetClients
[ID
];
591 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
592 g_Net_HostMsgHandler(TC
, NetEvent
.packet
);
595 ENET_EVENT_TYPE_DISCONNECT
:
597 ID
:= Byte(NetEvent
.peer
^.data
^);
598 if ID
> High(NetClients
) then Exit
;
599 TC
:= @NetClients
[ID
];
600 if TC
= nil then Exit
;
602 if not (TC
^.Used
) then Exit
;
604 TP
:= g_Player_Get(TC
^.Player
);
609 TP
.Kill(K_SIMPLEKILL
, 0, HIT_DISCON
);
610 g_Console_Add(Format(_lc
[I_PLAYER_LEAVE
], [TP
.Name
]), True);
611 e_WriteLog('NET: Client ' + TP
.Name
+ ' [' + IntToStr(ID
) + '] disconnected.', TMsgType
.Notify
);
612 g_Player_Remove(TP
.UID
);
616 TC
^.State
:= NET_STATE_NONE
;
619 TC
^.RequestedFullUpdate
:= False;
621 FreeMemory(NetEvent
.peer
^.data
);
622 NetEvent
.peer
^.data
:= nil;
623 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST_DISC
], [ID
]));
626 if NetUseMaster
then g_Net_Slist_Update
;
633 { /// CLIENT FUNCTIONS /// }
636 procedure g_Net_Disconnect(Forced
: Boolean = False);
638 if NetMode
<> NET_CLIENT
then Exit
;
639 if (NetHost
= nil) or (NetPeer
= nil) then Exit
;
643 enet_peer_disconnect(NetPeer
, NET_DISC_NONE
);
645 while (enet_host_service(NetHost
, @NetEvent
, 1500) > 0) do
647 if (NetEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
) then
653 if (NetEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
) then
654 enet_packet_destroy(NetEvent
.packet
);
657 if NetPeer
<> nil then
659 enet_peer_reset(NetPeer
);
665 e_WriteLog('NET: Kicked from server: ' + IntToStr(NetEvent
.data
), TMsgType
.Notify
);
666 if (NetEvent
.data
<= NET_DISC_MAX
) then
667 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_KICK
] +
668 _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + NetEvent
.data
)], True);
671 if NetHost
<> nil then
673 enet_host_destroy(NetHost
);
676 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_CLIENT_DISC
]);
679 e_WriteLog('NET: Disconnected', TMsgType
.Notify
);
682 procedure g_Net_Client_Send(Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
688 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
)
692 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, F
);
693 if not Assigned(P
) then Exit
;
695 enet_peer_send(NetPeer
, Chan
, P
);
696 if NetDump
then g_Net_DumpSendBuffer();
701 function g_Net_Client_Update(): enet_size_t
;
704 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
706 case NetEvent
.kind
of
707 ENET_EVENT_TYPE_RECEIVE
:
709 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
710 g_Net_ClientMsgHandler(NetEvent
.packet
);
713 ENET_EVENT_TYPE_DISCONNECT
:
715 g_Net_Disconnect(True);
723 function g_Net_Client_UpdateWhileLoading(): enet_size_t
;
726 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
728 case NetEvent
.kind
of
729 ENET_EVENT_TYPE_RECEIVE
:
731 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
732 g_Net_ClientLightMsgHandler(NetEvent
.packet
);
735 ENET_EVENT_TYPE_DISCONNECT
:
737 g_Net_Disconnect(True);
746 function g_Net_Connect(IP
: string; Port
: enet_uint16
): Boolean;
750 if NetMode
<> NET_NONE
then
752 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_ERR_INGAME
], True);
759 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_CLIENT_CONN
],
761 if not NetInitDone
then
763 if (not g_Net_Init()) then
765 g_Console_Add(_lc
[I_NET_MSG_FERROR
] + _lc
[I_NET_ERR_ENET
], True);
773 NetHost
:= enet_host_create(nil, 1, NET_CHANS
, 0, 0);
775 if (NetHost
= nil) then
777 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
783 enet_address_set_host(@NetAddr
, PChar(Addr(IP
[1])));
784 NetAddr
.port
:= Port
;
786 NetPeer
:= enet_host_connect(NetHost
, @NetAddr
, NET_CHANS
, NET_PROTOCOL_VER
);
788 if (NetPeer
= nil) then
790 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
791 enet_host_destroy(NetHost
);
800 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
802 if (NetEvent
.kind
= ENET_EVENT_TYPE_CONNECT
) then
804 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_CLIENT_DONE
]);
805 NetMode
:= NET_CLIENT
;
807 enet_peer_timeout(NetPeer
, ENET_PEER_TIMEOUT_LIMIT
* 2, ENET_PEER_TIMEOUT_MINIMUM
* 2, ENET_PEER_TIMEOUT_MAXIMUM
* 2);
809 NetClientPort
:= Port
;
816 ProcessLoading(true);
818 if e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(IK_SPACE
) then
822 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_TIMEOUT
], True);
823 if NetPeer
<> nil then enet_peer_reset(NetPeer
);
824 if NetHost
<> nil then
826 enet_host_destroy(NetHost
);
833 function IpToStr(IP
: LongWord): string;
838 Result
:= IntToStr(PByte(Ptr
+ 0)^) + '.';
839 Result
:= Result
+ IntToStr(PByte(Ptr
+ 1)^) + '.';
840 Result
:= Result
+ IntToStr(PByte(Ptr
+ 2)^) + '.';
841 Result
:= Result
+ IntToStr(PByte(Ptr
+ 3)^);
844 function StrToIp(IPstr
: string; var IP
: LongWord): Boolean;
848 Result
:= enet_address_set_host(@EAddr
, PChar(@IPstr
[1])) = 0;
852 function g_Net_Client_ByName(Name
: string): pTNetClient
;
858 for a
:= Low(NetClients
) to High(NetClients
) do
859 if (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
861 pl
:= g_Player_Get(NetClients
[a
].Player
);
862 if pl
= nil then continue
;
863 if Copy(LowerCase(pl
.Name
), 1, Length(Name
)) <> LowerCase(Name
) then continue
;
864 if NetClients
[a
].Peer
<> nil then
866 Result
:= @NetClients
[a
];
872 function g_Net_Client_ByPlayer(PID
: Word): pTNetClient
;
877 for a
:= Low(NetClients
) to High(NetClients
) do
878 if (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
879 if NetClients
[a
].Player
= PID
then
881 Result
:= @NetClients
[a
];
886 function g_Net_ClientName_ByID(ID
: Integer): string;
892 if ID
= NET_EVERYONE
then
894 for a
:= Low(NetClients
) to High(NetClients
) do
895 if (NetClients
[a
].ID
= ID
) and (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
897 pl
:= g_Player_Get(NetClients
[a
].Player
);
898 if pl
= nil then Exit
;
903 procedure g_Net_SendData(Data
:AByte
; peer
: pENetPeer
; Reliable
: Boolean; Chan
: Byte = NET_CHAN_DOWNLOAD
);
907 dataLength
: Cardinal;
909 dataLength
:= Length(Data
);
912 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
)
916 if (peer
<> nil) then
918 P
:= enet_packet_create(@Data
[0], dataLength
, F
);
919 if not Assigned(P
) then Exit
;
920 enet_peer_send(peer
, Chan
, P
);
924 P
:= enet_packet_create(@Data
[0], dataLength
, F
);
925 if not Assigned(P
) then Exit
;
926 enet_host_broadcast(NetHost
, Chan
, P
);
929 enet_host_flush(NetHost
);
932 function g_Net_Wait_Event(msgId
: Word): TMemoryStream
;
934 downloadEvent
: ENetEvent
;
938 msgStream
: TMemoryStream
;
940 FillChar(downloadEvent
, SizeOf(downloadEvent
), 0);
945 while (enet_host_service(NetHost
, @downloadEvent
, 0) > 0) do
947 if (downloadEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
) then
949 Ptr
:= downloadEvent
.packet
^.data
;
953 if (MID
= msgId
) then
955 msgStream
:= TMemoryStream
.Create
;
956 msgStream
.SetSize(downloadEvent
.packet
^.dataLength
);
957 msgStream
.WriteBuffer(Ptr
^, downloadEvent
.packet
^.dataLength
);
958 msgStream
.Seek(0, soFromBeginning
);
961 enet_packet_destroy(downloadEvent
.packet
);
965 enet_packet_destroy(downloadEvent
.packet
);
969 if (downloadEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
) then
971 if (downloadEvent
.data
<= NET_DISC_MAX
) then
972 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' ' +
973 _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + downloadEvent
.data
)], True);
979 ProcessLoading(true);
981 if e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(IK_SPACE
) then
987 function g_Net_IsHostBanned(IP
: LongWord; Perm
: Boolean = False): Boolean;
992 if NetBannedHosts
= nil then
994 for I
:= 0 to High(NetBannedHosts
) do
995 if (NetBannedHosts
[I
].IP
= IP
) and ((not Perm
) or (NetBannedHosts
[I
].Perm
)) then
1002 procedure g_Net_BanHost(IP
: LongWord; Perm
: Boolean = True); overload
;
1008 if g_Net_IsHostBanned(IP
, Perm
) then
1012 for I
:= Low(NetBannedHosts
) to High(NetBannedHosts
) do
1013 if NetBannedHosts
[I
].IP
= 0 then
1021 SetLength(NetBannedHosts
, Length(NetBannedHosts
) + 1);
1022 P
:= High(NetBannedHosts
);
1025 NetBannedHosts
[P
].IP
:= IP
;
1026 NetBannedHosts
[P
].Perm
:= Perm
;
1029 procedure g_Net_BanHost(IP
: string; Perm
: Boolean = True); overload
;
1034 b
:= StrToIp(IP
, a
);
1036 g_Net_BanHost(a
, Perm
);
1039 procedure g_Net_UnbanNonPermHosts();
1043 if NetBannedHosts
= nil then
1045 for I
:= Low(NetBannedHosts
) to High(NetBannedHosts
) do
1046 if (NetBannedHosts
[I
].IP
> 0) and not NetBannedHosts
[I
].Perm
then
1048 NetBannedHosts
[I
].IP
:= 0;
1049 NetBannedHosts
[I
].Perm
:= True;
1053 function g_Net_UnbanHost(IP
: string): Boolean; overload
;
1057 Result
:= StrToIp(IP
, a
);
1059 Result
:= g_Net_UnbanHost(a
);
1062 function g_Net_UnbanHost(IP
: LongWord): Boolean; overload
;
1069 if NetBannedHosts
= nil then
1071 for I
:= 0 to High(NetBannedHosts
) do
1072 if NetBannedHosts
[I
].IP
= IP
then
1074 NetBannedHosts
[I
].IP
:= 0;
1075 NetBannedHosts
[I
].Perm
:= True;
1077 // no break here to clear all bans of this host, perm and non-perm
1081 procedure g_Net_SaveBanList();
1086 Assign(F
, DataDir
+ BANLIST_FILENAME
);
1088 if NetBannedHosts
<> nil then
1089 for I
:= 0 to High(NetBannedHosts
) do
1090 if NetBannedHosts
[I
].Perm
and (NetBannedHosts
[I
].IP
> 0) then
1091 Writeln(F
, IpToStr(NetBannedHosts
[I
].IP
));
1095 procedure g_Net_DumpStart();
1097 if NetMode
= NET_SERVER
then
1098 NetDumpFile
:= createDiskFile(NETDUMP_FILENAME
+ '_server')
1100 NetDumpFile
:= createDiskFile(NETDUMP_FILENAME
+ '_client');
1103 procedure g_Net_DumpSendBuffer();
1105 writeInt(NetDumpFile
, gTime
);
1106 writeInt(NetDumpFile
, LongWord(NetOut
.CurSize
));
1107 writeInt(NetDumpFile
, Byte(1));
1108 NetDumpFile
.WriteBuffer(NetOut
.Data
^, NetOut
.CurSize
);
1111 procedure g_Net_DumpRecvBuffer(Buf
: penet_uint8
; Len
: LongWord);
1113 if (Buf
= nil) or (Len
= 0) then Exit
;
1114 writeInt(NetDumpFile
, gTime
);
1115 writeInt(NetDumpFile
, Len
);
1116 writeInt(NetDumpFile
, Byte(0));
1117 NetDumpFile
.WriteBuffer(Buf
^, Len
);
1120 procedure g_Net_DumpEnd();
1126 function g_Net_ForwardPorts(ForwardPongPort
: Boolean = True): Boolean;
1127 {$IFDEF USE_MINIUPNPC}
1132 LanAddr
: array [0..255] of Char;
1133 StrPort
: AnsiString;
1138 if NetPortForwarded
= NetPort
then
1144 NetPongForwarded
:= False;
1145 NetPortForwarded
:= 0;
1147 DevList
:= upnpDiscover(1000, nil, nil, 0, 0, Addr(Err
));
1148 if DevList
= nil then
1150 conwritefln('port forwarding failed: upnpDiscover() failed: %d', [Err
]);
1154 I
:= UPNP_GetValidIGD(DevList
, @Urls
, @Data
, Addr(LanAddr
[0]), 256);
1158 conwriteln('port forwarding failed: could not find an IGD device on this LAN');
1159 FreeUPNPDevList(DevList
);
1160 FreeUPNPUrls(@Urls
);
1164 StrPort
:= IntToStr(NetPort
);
1165 I
:= UPNP_AddPortMapping(
1166 Urls
.controlURL
, Addr(data
.first
.servicetype
[1]),
1167 PChar(StrPort
), PChar(StrPort
), Addr(LanAddr
[0]), PChar('D2DF'),
1168 PChar('UDP'), nil, PChar('0')
1173 conwritefln('forwarding port %d failed: error %d', [NetPort
, I
]);
1174 FreeUPNPDevList(DevList
);
1175 FreeUPNPUrls(@Urls
);
1179 if ForwardPongPort
then
1181 StrPort
:= IntToStr(NetPort
+ 1);
1182 I
:= UPNP_AddPortMapping(
1183 Urls
.controlURL
, Addr(data
.first
.servicetype
[1]),
1184 PChar(StrPort
), PChar(StrPort
), Addr(LanAddr
[0]), PChar('D2DF'),
1185 PChar('UDP'), nil, PChar('0')
1190 conwritefln('forwarding port %d failed: error %d', [NetPort
+ 1, I
]);
1191 NetPongForwarded
:= False;
1195 conwritefln('forwarded port %d successfully', [NetPort
+ 1]);
1196 NetPongForwarded
:= True;
1200 conwritefln('forwarded port %d successfully', [NetPort
]);
1201 NetIGDControl
:= AnsiString(Urls
.controlURL
);
1202 NetIGDService
:= data
.first
.servicetype
;
1203 NetPortForwarded
:= NetPort
;
1205 FreeUPNPDevList(DevList
);
1206 FreeUPNPUrls(@Urls
);
1215 procedure g_Net_UnforwardPorts();
1216 {$IFDEF USE_MINIUPNPC}
1219 StrPort
: AnsiString;
1221 if NetPortForwarded
= 0 then Exit
;
1223 conwriteln('unforwarding ports...');
1225 StrPort
:= IntToStr(NetPortForwarded
);
1226 I
:= UPNP_DeletePortMapping(
1227 PChar(NetIGDControl
), Addr(NetIGDService
[1]),
1228 PChar(StrPort
), PChar('UDP'), nil
1230 conwritefln(' port %d: %d', [NetPortForwarded
, I
]);
1232 if NetPongForwarded
then
1234 NetPongForwarded
:= False;
1235 StrPort
:= IntToStr(NetPortForwarded
+ 1);
1236 I
:= UPNP_DeletePortMapping(
1237 PChar(NetIGDControl
), Addr(NetIGDService
[1]),
1238 PChar(StrPort
), PChar('UDP'), nil
1240 conwritefln(' port %d: %d', [NetPortForwarded
+ 1, I
]);
1243 NetPortForwarded
:= 0;
1252 NetIn
.Alloc(NET_BUFSIZE
);
1253 NetOut
.Alloc(NET_BUFSIZE
);