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
;
151 NetPortThread
: TThreadID
= 0;
153 NetDumpFile
: TStream
;
155 function g_Net_Init(): Boolean;
156 procedure g_Net_Cleanup();
157 procedure g_Net_Free();
158 procedure g_Net_Flush();
160 function g_Net_Host(IPAddr
: LongWord; Port
: enet_uint16
; MaxClients
: Cardinal = 16): Boolean;
161 procedure g_Net_Host_Die();
162 procedure g_Net_Host_Send(ID
: Integer; Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
163 function g_Net_Host_Update(): enet_size_t
;
165 function g_Net_Connect(IP
: string; Port
: enet_uint16
): Boolean;
166 procedure g_Net_Disconnect(Forced
: Boolean = False);
167 procedure g_Net_Client_Send(Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
168 function g_Net_Client_Update(): enet_size_t
;
169 function g_Net_Client_UpdateWhileLoading(): enet_size_t
;
171 function g_Net_Client_ByName(Name
: string): pTNetClient
;
172 function g_Net_Client_ByPlayer(PID
: Word): pTNetClient
;
173 function g_Net_ClientName_ByID(ID
: Integer): string;
175 procedure g_Net_SendData(Data
:AByte
; peer
: pENetPeer
; Reliable
: Boolean; Chan
: Byte = NET_CHAN_DOWNLOAD
);
176 function g_Net_Wait_Event(msgId
: Word): TMemoryStream
;
178 function IpToStr(IP
: LongWord): string;
179 function StrToIp(IPstr
: string; var IP
: LongWord): Boolean;
181 function g_Net_IsHostBanned(IP
: LongWord; Perm
: Boolean = False): Boolean;
182 procedure g_Net_BanHost(IP
: LongWord; Perm
: Boolean = True); overload
;
183 procedure g_Net_BanHost(IP
: string; Perm
: Boolean = True); overload
;
184 function g_Net_UnbanHost(IP
: string): Boolean; overload
;
185 function g_Net_UnbanHost(IP
: LongWord): Boolean; overload
;
186 procedure g_Net_UnbanNonPermHosts();
187 procedure g_Net_SaveBanList();
189 procedure g_Net_DumpStart();
190 procedure g_Net_DumpSendBuffer();
191 procedure g_Net_DumpRecvBuffer(Buf
: penet_uint8
; Len
: LongWord);
192 procedure g_Net_DumpEnd();
194 function g_Net_ForwardPorts(ForwardPongPort
: Boolean = True): Boolean;
195 procedure g_Net_UnforwardPorts();
201 e_input
, g_nethandler
, g_netmsg
, g_netmaster
, g_player
, g_window
, g_console
,
202 g_main
, g_game
, g_language
, g_weapons
, utils
;
205 { /// SERVICE FUNCTIONS /// }
208 function g_Net_FindSlot(): Integer;
217 for I
:= Low(NetClients
) to High(NetClients
) do
219 if NetClients
[I
].Used
then
228 if C
>= NetMaxClients
then
236 if (Length(NetClients
) >= NetMaxClients
) then
240 SetLength(NetClients
, Length(NetClients
) + 1);
241 N
:= High(NetClients
);
247 NetClients
[N
].Used
:= True;
248 NetClients
[N
].ID
:= N
;
249 NetClients
[N
].RequestedFullUpdate
:= False;
250 NetClients
[N
].RCONAuth
:= False;
251 NetClients
[N
].Voted
:= False;
252 NetClients
[N
].Player
:= 0;
258 function g_Net_Init(): Boolean;
266 SetLength(NetClients
, 0);
272 NetAddr
.port
:= 25666;
273 SetLength(NetBannedHosts
, 0);
274 if FileExists(DataDir
+ BANLIST_FILENAME
) then
276 Assign(F
, DataDir
+ BANLIST_FILENAME
);
281 if StrToIp(IPstr
, IP
) then
288 Result
:= (enet_initialize() = 0);
291 procedure g_Net_Flush();
293 enet_host_flush(NetHost
);
296 procedure g_Net_Cleanup();
301 SetLength(NetClients
, 0);
311 NetState
:= NET_STATE_NONE
;
313 NetPongSock
:= ENET_SOCKET_NULL
;
315 NetTimeToMaster
:= 0;
316 NetTimeToUpdate
:= 0;
317 NetTimeToReliable
:= 0;
321 if NetPortThread
<> 0 then
322 WaitForThreadTerminate(NetPortThread
, 66666);
325 g_Net_UnforwardPorts();
331 procedure g_Net_Free();
336 NetInitDone
:= False;
340 { /// SERVER FUNCTIONS /// }
343 function ForwardThread(Param
: Pointer): PtrInt
;
346 if not g_Net_ForwardPorts() then Result
:= -1;
349 function g_Net_Host(IPAddr
: LongWord; Port
: enet_uint16
; MaxClients
: Cardinal = 16): Boolean;
351 if NetMode
<> NET_NONE
then
353 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_INGAME
]);
360 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST
], [Port
]));
361 if not NetInitDone
then
363 if (not g_Net_Init()) then
365 g_Console_Add(_lc
[I_NET_MSG_FERROR
] + _lc
[I_NET_ERR_ENET
]);
373 NetAddr
.host
:= IPAddr
;
374 NetAddr
.port
:= Port
;
376 if NetForwardPorts
then NetPortThread
:= BeginThread(ForwardThread
);
378 NetHost
:= enet_host_create(@NetAddr
, NET_MAXCLIENTS
, NET_CHANS
, 0, 0);
380 if (NetHost
= nil) then
382 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + Format(_lc
[I_NET_ERR_HOST
], [Port
]));
388 NetPongSock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
389 if NetPongSock
<> ENET_SOCKET_NULL
then
391 NetPongAddr
.host
:= IPAddr
;
392 NetPongAddr
.port
:= Port
+ 1;
393 if enet_socket_bind(NetPongSock
, @NetPongAddr
) < 0 then
395 enet_socket_destroy(NetPongSock
);
396 NetPongSock
:= ENET_SOCKET_NULL
;
399 enet_socket_set_option(NetPongSock
, ENET_SOCKOPT_NONBLOCK
, 1);
402 NetMode
:= NET_SERVER
;
409 procedure g_Net_Host_Die();
413 if NetMode
<> NET_SERVER
then Exit
;
415 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_DISCALL
]);
416 for I
:= 0 to High(NetClients
) do
417 if NetClients
[I
].Used
then
418 enet_peer_disconnect(NetClients
[I
].Peer
, NET_DISC_DOWN
);
420 while enet_host_service(NetHost
, @NetEvent
, 1000) > 0 do
421 if NetEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
422 enet_packet_destroy(NetEvent
.packet
);
424 for I
:= 0 to High(NetClients
) do
425 if NetClients
[I
].Used
then
427 FreeMemory(NetClients
[I
].Peer
^.data
);
428 NetClients
[I
].Peer
^.data
:= nil;
429 enet_peer_reset(NetClients
[I
].Peer
);
430 NetClients
[I
].Peer
:= nil;
431 NetClients
[I
].Used
:= False;
434 if (NetMPeer
<> nil) and (NetMHost
<> nil) then g_Net_Slist_Disconnect
;
435 if NetPongSock
<> ENET_SOCKET_NULL
then
436 enet_socket_destroy(NetPongSock
);
438 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_DIE
]);
439 enet_host_destroy(NetHost
);
444 e_WriteLog('NET: Server stopped', TMsgType
.Notify
);
448 procedure g_Net_Host_Send(ID
: Integer; Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
454 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
)
460 if ID
> High(NetClients
) then Exit
;
461 if NetClients
[ID
].Peer
= nil then Exit
;
463 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, F
);
464 if not Assigned(P
) then Exit
;
466 enet_peer_send(NetClients
[ID
].Peer
, Chan
, P
);
470 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, F
);
471 if not Assigned(P
) then Exit
;
473 enet_host_broadcast(NetHost
, Chan
, P
);
476 if NetDump
then g_Net_DumpSendBuffer();
481 procedure g_Net_Host_CheckPings();
487 Ping
: array [0..9] of Byte;
490 if NetPongSock
= ENET_SOCKET_NULL
then Exit
;
492 Buf
.data
:= Addr(Ping
[0]);
493 Buf
.dataLength
:= 2+8;
497 Len
:= enet_socket_receive(NetPongSock
, @ClAddr
, @Buf
, 1);
498 if Len
< 0 then Exit
;
500 if (Ping
[0] = Ord('D')) and (Ping
[1] = Ord('F')) then
502 ClTime
:= Int64(Addr(Ping
[2])^);
505 NetOut
.Write(Byte(Ord('D')));
506 NetOut
.Write(Byte(Ord('F')));
507 NetOut
.Write(ClTime
);
508 g_Net_Slist_WriteInfo();
510 if gPlayer1
<> nil then Inc(NPl
);
511 if gPlayer2
<> nil then Inc(NPl
);
513 NetOut
.Write(gNumBots
);
515 Buf
.data
:= NetOut
.Data
;
516 Buf
.dataLength
:= NetOut
.CurSize
;
517 enet_socket_send(NetPongSock
, @ClAddr
, @Buf
, 1);
523 function g_Net_Host_Update(): enet_size_t
;
537 g_Net_Host_CheckPings
;
540 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
542 case (NetEvent
.kind
) of
543 ENET_EVENT_TYPE_CONNECT
:
545 IP
:= IpToStr(NetEvent
.Peer
^.address
.host
);
546 Port
:= NetEvent
.Peer
^.address
.port
;
547 g_Console_Add(_lc
[I_NET_MSG
] +
548 Format(_lc
[I_NET_MSG_HOST_CONN
], [IP
, Port
]));
550 if (NetEvent
.data
<> NET_PROTOCOL_VER
) then
552 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_REJECT
] +
553 _lc
[I_NET_DISC_PROTOCOL
]);
554 NetEvent
.peer
^.data
:= GetMemory(SizeOf(Byte));
555 Byte(NetEvent
.peer
^.data
^) := 255;
556 enet_peer_disconnect(NetEvent
.peer
, NET_DISC_PROTOCOL
);
557 enet_host_flush(NetHost
);
561 ID
:= g_Net_FindSlot();
565 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_REJECT
] +
566 _lc
[I_NET_DISC_FULL
]);
567 NetEvent
.Peer
^.data
:= GetMemory(SizeOf(Byte));
568 Byte(NetEvent
.peer
^.data
^) := 255;
569 enet_peer_disconnect(NetEvent
.peer
, NET_DISC_FULL
);
570 enet_host_flush(NetHost
);
574 NetClients
[ID
].Peer
:= NetEvent
.peer
;
575 NetClients
[ID
].Peer
^.data
:= GetMemory(SizeOf(Byte));
576 Byte(NetClients
[ID
].Peer
^.data
^) := ID
;
577 NetClients
[ID
].State
:= NET_STATE_AUTH
;
578 NetClients
[ID
].RCONAuth
:= False;
580 enet_peer_timeout(NetEvent
.peer
, ENET_PEER_TIMEOUT_LIMIT
* 2, ENET_PEER_TIMEOUT_MINIMUM
* 2, ENET_PEER_TIMEOUT_MAXIMUM
* 2);
583 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST_ADD
], [ID
]));
586 ENET_EVENT_TYPE_RECEIVE
:
588 ID
:= Byte(NetEvent
.peer
^.data
^);
589 if ID
> High(NetClients
) then Exit
;
590 TC
:= @NetClients
[ID
];
592 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
593 g_Net_HostMsgHandler(TC
, NetEvent
.packet
);
596 ENET_EVENT_TYPE_DISCONNECT
:
598 ID
:= Byte(NetEvent
.peer
^.data
^);
599 if ID
> High(NetClients
) then Exit
;
600 TC
:= @NetClients
[ID
];
601 if TC
= nil then Exit
;
603 if not (TC
^.Used
) then Exit
;
605 TP
:= g_Player_Get(TC
^.Player
);
610 TP
.Kill(K_SIMPLEKILL
, 0, HIT_DISCON
);
611 g_Console_Add(Format(_lc
[I_PLAYER_LEAVE
], [TP
.Name
]), True);
612 e_WriteLog('NET: Client ' + TP
.Name
+ ' [' + IntToStr(ID
) + '] disconnected.', TMsgType
.Notify
);
613 g_Player_Remove(TP
.UID
);
617 TC
^.State
:= NET_STATE_NONE
;
620 TC
^.RequestedFullUpdate
:= False;
622 FreeMemory(NetEvent
.peer
^.data
);
623 NetEvent
.peer
^.data
:= nil;
624 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST_DISC
], [ID
]));
627 if NetUseMaster
then g_Net_Slist_Update
;
634 { /// CLIENT FUNCTIONS /// }
637 procedure g_Net_Disconnect(Forced
: Boolean = False);
639 if NetMode
<> NET_CLIENT
then Exit
;
640 if (NetHost
= nil) or (NetPeer
= nil) then Exit
;
644 enet_peer_disconnect(NetPeer
, NET_DISC_NONE
);
646 while (enet_host_service(NetHost
, @NetEvent
, 1500) > 0) do
648 if (NetEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
) then
654 if (NetEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
) then
655 enet_packet_destroy(NetEvent
.packet
);
658 if NetPeer
<> nil then
660 enet_peer_reset(NetPeer
);
666 e_WriteLog('NET: Kicked from server: ' + IntToStr(NetEvent
.data
), TMsgType
.Notify
);
667 if (NetEvent
.data
<= NET_DISC_MAX
) then
668 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_KICK
] +
669 _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + NetEvent
.data
)], True);
672 if NetHost
<> nil then
674 enet_host_destroy(NetHost
);
677 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_CLIENT_DISC
]);
680 e_WriteLog('NET: Disconnected', TMsgType
.Notify
);
683 procedure g_Net_Client_Send(Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
689 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
)
693 P
:= enet_packet_create(NetOut
.Data
, NetOut
.CurSize
, F
);
694 if not Assigned(P
) then Exit
;
696 enet_peer_send(NetPeer
, Chan
, P
);
697 if NetDump
then g_Net_DumpSendBuffer();
702 function g_Net_Client_Update(): enet_size_t
;
705 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
707 case NetEvent
.kind
of
708 ENET_EVENT_TYPE_RECEIVE
:
710 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
711 g_Net_ClientMsgHandler(NetEvent
.packet
);
714 ENET_EVENT_TYPE_DISCONNECT
:
716 g_Net_Disconnect(True);
724 function g_Net_Client_UpdateWhileLoading(): enet_size_t
;
727 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
729 case NetEvent
.kind
of
730 ENET_EVENT_TYPE_RECEIVE
:
732 if NetDump
then g_Net_DumpRecvBuffer(NetEvent
.packet
^.data
, NetEvent
.packet
^.dataLength
);
733 g_Net_ClientLightMsgHandler(NetEvent
.packet
);
736 ENET_EVENT_TYPE_DISCONNECT
:
738 g_Net_Disconnect(True);
747 function g_Net_Connect(IP
: string; Port
: enet_uint16
): Boolean;
751 if NetMode
<> NET_NONE
then
753 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_ERR_INGAME
], True);
760 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_CLIENT_CONN
],
762 if not NetInitDone
then
764 if (not g_Net_Init()) then
766 g_Console_Add(_lc
[I_NET_MSG_FERROR
] + _lc
[I_NET_ERR_ENET
], True);
774 NetHost
:= enet_host_create(nil, 1, NET_CHANS
, 0, 0);
776 if (NetHost
= nil) then
778 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
784 enet_address_set_host(@NetAddr
, PChar(Addr(IP
[1])));
785 NetAddr
.port
:= Port
;
787 NetPeer
:= enet_host_connect(NetHost
, @NetAddr
, NET_CHANS
, NET_PROTOCOL_VER
);
789 if (NetPeer
= nil) then
791 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
792 enet_host_destroy(NetHost
);
801 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
803 if (NetEvent
.kind
= ENET_EVENT_TYPE_CONNECT
) then
805 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_CLIENT_DONE
]);
806 NetMode
:= NET_CLIENT
;
808 enet_peer_timeout(NetPeer
, ENET_PEER_TIMEOUT_LIMIT
* 2, ENET_PEER_TIMEOUT_MINIMUM
* 2, ENET_PEER_TIMEOUT_MAXIMUM
* 2);
810 NetClientPort
:= Port
;
817 ProcessLoading(true);
819 if e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(IK_SPACE
) then
823 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_TIMEOUT
], True);
824 if NetPeer
<> nil then enet_peer_reset(NetPeer
);
825 if NetHost
<> nil then
827 enet_host_destroy(NetHost
);
834 function IpToStr(IP
: LongWord): string;
839 Result
:= IntToStr(PByte(Ptr
+ 0)^) + '.';
840 Result
:= Result
+ IntToStr(PByte(Ptr
+ 1)^) + '.';
841 Result
:= Result
+ IntToStr(PByte(Ptr
+ 2)^) + '.';
842 Result
:= Result
+ IntToStr(PByte(Ptr
+ 3)^);
845 function StrToIp(IPstr
: string; var IP
: LongWord): Boolean;
849 Result
:= enet_address_set_host(@EAddr
, PChar(@IPstr
[1])) = 0;
853 function g_Net_Client_ByName(Name
: string): pTNetClient
;
859 for a
:= Low(NetClients
) to High(NetClients
) do
860 if (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
862 pl
:= g_Player_Get(NetClients
[a
].Player
);
863 if pl
= nil then continue
;
864 if Copy(LowerCase(pl
.Name
), 1, Length(Name
)) <> LowerCase(Name
) then continue
;
865 if NetClients
[a
].Peer
<> nil then
867 Result
:= @NetClients
[a
];
873 function g_Net_Client_ByPlayer(PID
: Word): pTNetClient
;
878 for a
:= Low(NetClients
) to High(NetClients
) do
879 if (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
880 if NetClients
[a
].Player
= PID
then
882 Result
:= @NetClients
[a
];
887 function g_Net_ClientName_ByID(ID
: Integer): string;
893 if ID
= NET_EVERYONE
then
895 for a
:= Low(NetClients
) to High(NetClients
) do
896 if (NetClients
[a
].ID
= ID
) and (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
898 pl
:= g_Player_Get(NetClients
[a
].Player
);
899 if pl
= nil then Exit
;
904 procedure g_Net_SendData(Data
:AByte
; peer
: pENetPeer
; Reliable
: Boolean; Chan
: Byte = NET_CHAN_DOWNLOAD
);
908 dataLength
: Cardinal;
910 dataLength
:= Length(Data
);
913 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
)
917 if (peer
<> nil) then
919 P
:= enet_packet_create(@Data
[0], dataLength
, F
);
920 if not Assigned(P
) then Exit
;
921 enet_peer_send(peer
, Chan
, P
);
925 P
:= enet_packet_create(@Data
[0], dataLength
, F
);
926 if not Assigned(P
) then Exit
;
927 enet_host_broadcast(NetHost
, Chan
, P
);
930 enet_host_flush(NetHost
);
933 function g_Net_Wait_Event(msgId
: Word): TMemoryStream
;
935 downloadEvent
: ENetEvent
;
939 msgStream
: TMemoryStream
;
941 FillChar(downloadEvent
, SizeOf(downloadEvent
), 0);
946 while (enet_host_service(NetHost
, @downloadEvent
, 0) > 0) do
948 if (downloadEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
) then
950 Ptr
:= downloadEvent
.packet
^.data
;
954 if (MID
= msgId
) then
956 msgStream
:= TMemoryStream
.Create
;
957 msgStream
.SetSize(downloadEvent
.packet
^.dataLength
);
958 msgStream
.WriteBuffer(Ptr
^, downloadEvent
.packet
^.dataLength
);
959 msgStream
.Seek(0, soFromBeginning
);
962 enet_packet_destroy(downloadEvent
.packet
);
966 enet_packet_destroy(downloadEvent
.packet
);
970 if (downloadEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
) then
972 if (downloadEvent
.data
<= NET_DISC_MAX
) then
973 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' ' +
974 _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + downloadEvent
.data
)], True);
980 ProcessLoading(true);
982 if e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(IK_SPACE
) then
988 function g_Net_IsHostBanned(IP
: LongWord; Perm
: Boolean = False): Boolean;
993 if NetBannedHosts
= nil then
995 for I
:= 0 to High(NetBannedHosts
) do
996 if (NetBannedHosts
[I
].IP
= IP
) and ((not Perm
) or (NetBannedHosts
[I
].Perm
)) then
1003 procedure g_Net_BanHost(IP
: LongWord; Perm
: Boolean = True); overload
;
1009 if g_Net_IsHostBanned(IP
, Perm
) then
1013 for I
:= Low(NetBannedHosts
) to High(NetBannedHosts
) do
1014 if NetBannedHosts
[I
].IP
= 0 then
1022 SetLength(NetBannedHosts
, Length(NetBannedHosts
) + 1);
1023 P
:= High(NetBannedHosts
);
1026 NetBannedHosts
[P
].IP
:= IP
;
1027 NetBannedHosts
[P
].Perm
:= Perm
;
1030 procedure g_Net_BanHost(IP
: string; Perm
: Boolean = True); overload
;
1035 b
:= StrToIp(IP
, a
);
1037 g_Net_BanHost(a
, Perm
);
1040 procedure g_Net_UnbanNonPermHosts();
1044 if NetBannedHosts
= nil then
1046 for I
:= Low(NetBannedHosts
) to High(NetBannedHosts
) do
1047 if (NetBannedHosts
[I
].IP
> 0) and not NetBannedHosts
[I
].Perm
then
1049 NetBannedHosts
[I
].IP
:= 0;
1050 NetBannedHosts
[I
].Perm
:= True;
1054 function g_Net_UnbanHost(IP
: string): Boolean; overload
;
1058 Result
:= StrToIp(IP
, a
);
1060 Result
:= g_Net_UnbanHost(a
);
1063 function g_Net_UnbanHost(IP
: LongWord): Boolean; overload
;
1070 if NetBannedHosts
= nil then
1072 for I
:= 0 to High(NetBannedHosts
) do
1073 if NetBannedHosts
[I
].IP
= IP
then
1075 NetBannedHosts
[I
].IP
:= 0;
1076 NetBannedHosts
[I
].Perm
:= True;
1078 // no break here to clear all bans of this host, perm and non-perm
1082 procedure g_Net_SaveBanList();
1087 Assign(F
, DataDir
+ BANLIST_FILENAME
);
1089 if NetBannedHosts
<> nil then
1090 for I
:= 0 to High(NetBannedHosts
) do
1091 if NetBannedHosts
[I
].Perm
and (NetBannedHosts
[I
].IP
> 0) then
1092 Writeln(F
, IpToStr(NetBannedHosts
[I
].IP
));
1096 procedure g_Net_DumpStart();
1098 if NetMode
= NET_SERVER
then
1099 NetDumpFile
:= createDiskFile(NETDUMP_FILENAME
+ '_server')
1101 NetDumpFile
:= createDiskFile(NETDUMP_FILENAME
+ '_client');
1104 procedure g_Net_DumpSendBuffer();
1106 writeInt(NetDumpFile
, gTime
);
1107 writeInt(NetDumpFile
, LongWord(NetOut
.CurSize
));
1108 writeInt(NetDumpFile
, Byte(1));
1109 NetDumpFile
.WriteBuffer(NetOut
.Data
^, NetOut
.CurSize
);
1112 procedure g_Net_DumpRecvBuffer(Buf
: penet_uint8
; Len
: LongWord);
1114 if (Buf
= nil) or (Len
= 0) then Exit
;
1115 writeInt(NetDumpFile
, gTime
);
1116 writeInt(NetDumpFile
, Len
);
1117 writeInt(NetDumpFile
, Byte(0));
1118 NetDumpFile
.WriteBuffer(Buf
^, Len
);
1121 procedure g_Net_DumpEnd();
1127 function g_Net_ForwardPorts(ForwardPongPort
: Boolean = True): Boolean;
1128 {$IFDEF USE_MINIUPNPC}
1133 LanAddr
: array [0..255] of Char;
1134 StrPort
: AnsiString;
1139 if NetPortForwarded
= NetPort
then
1145 NetPongForwarded
:= False;
1146 NetPortForwarded
:= 0;
1148 DevList
:= upnpDiscover(1000, nil, nil, 0, 0, 2, Addr(Err
));
1149 if DevList
= nil then
1151 conwritefln('port forwarding failed: upnpDiscover() failed: %d', [Err
]);
1155 I
:= UPNP_GetValidIGD(DevList
, @Urls
, @Data
, Addr(LanAddr
[0]), 256);
1159 conwriteln('port forwarding failed: could not find an IGD device on this LAN');
1160 FreeUPNPDevList(DevList
);
1161 FreeUPNPUrls(@Urls
);
1165 StrPort
:= IntToStr(NetPort
);
1166 I
:= UPNP_AddPortMapping(
1167 Urls
.controlURL
, Addr(data
.first
.servicetype
[1]),
1168 PChar(StrPort
), PChar(StrPort
), Addr(LanAddr
[0]), PChar('D2DF'),
1169 PChar('UDP'), nil, PChar('0')
1174 conwritefln('forwarding port %d failed: error %d', [NetPort
, I
]);
1175 FreeUPNPDevList(DevList
);
1176 FreeUPNPUrls(@Urls
);
1180 if ForwardPongPort
then
1182 StrPort
:= IntToStr(NetPort
+ 1);
1183 I
:= UPNP_AddPortMapping(
1184 Urls
.controlURL
, Addr(data
.first
.servicetype
[1]),
1185 PChar(StrPort
), PChar(StrPort
), Addr(LanAddr
[0]), PChar('D2DF'),
1186 PChar('UDP'), nil, PChar('0')
1191 conwritefln('forwarding port %d failed: error %d', [NetPort
+ 1, I
]);
1192 NetPongForwarded
:= False;
1196 conwritefln('forwarded port %d successfully', [NetPort
+ 1]);
1197 NetPongForwarded
:= True;
1201 conwritefln('forwarded port %d successfully', [NetPort
]);
1202 NetIGDControl
:= AnsiString(Urls
.controlURL
);
1203 NetIGDService
:= data
.first
.servicetype
;
1204 NetPortForwarded
:= NetPort
;
1206 FreeUPNPDevList(DevList
);
1207 FreeUPNPUrls(@Urls
);
1216 procedure g_Net_UnforwardPorts();
1217 {$IFDEF USE_MINIUPNPC}
1220 StrPort
: AnsiString;
1222 if NetPortForwarded
= 0 then Exit
;
1224 conwriteln('unforwarding ports...');
1226 StrPort
:= IntToStr(NetPortForwarded
);
1227 I
:= UPNP_DeletePortMapping(
1228 PChar(NetIGDControl
), Addr(NetIGDService
[1]),
1229 PChar(StrPort
), PChar('UDP'), nil
1231 conwritefln(' port %d: %d', [NetPortForwarded
, I
]);
1233 if NetPongForwarded
then
1235 NetPongForwarded
:= False;
1236 StrPort
:= IntToStr(NetPortForwarded
+ 1);
1237 I
:= UPNP_DeletePortMapping(
1238 PChar(NetIGDControl
), Addr(NetIGDService
[1]),
1239 PChar(StrPort
), PChar('UDP'), nil
1241 conwritefln(' port %d: %d', [NetPortForwarded
+ 1, I
]);
1244 NetPortForwarded
:= 0;
1253 NetIn
.Alloc(NET_BUFSIZE
);
1254 NetOut
.Alloc(NET_BUFSIZE
);