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 g_amodes.inc}
22 e_log
, e_fixedbuffer
, ENet
, Classes
;
25 NET_PROTOCOL_VER
= 172;
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;
44 CH_MAX
= CH_UNRELIABLE
; // don't change this
56 NET_DISC_NONE
: enet_uint32
= 0;
57 NET_DISC_PROTOCOL
: enet_uint32
= 1;
58 NET_DISC_VERSION
: enet_uint32
= 2;
59 NET_DISC_FULL
: enet_uint32
= 3;
60 NET_DISC_KICK
: enet_uint32
= 4;
61 NET_DISC_DOWN
: enet_uint32
= 5;
62 NET_DISC_PASSWORD
: enet_uint32
= 6;
63 NET_DISC_TEMPBAN
: enet_uint32
= 7;
64 NET_DISC_BAN
: enet_uint32
= 8;
65 NET_DISC_MAX
: enet_uint32
= 8;
71 BANLIST_FILENAME
= 'banlist.txt';
72 NETDUMP_FILENAME
= 'netdump';
81 RequestedFullUpdate
: Boolean;
84 SendBuf
: array [0..CH_MAX
] of TBuffer
;
90 pTNetClient
= ^TNetClient
;
92 AByte
= array of Byte;
95 NetInitDone
: Boolean = False;
96 NetMode
: Byte = NET_NONE
;
97 NetDump
: Boolean = False;
99 NetServerName
: string = 'Unnamed Server';
100 NetPassword
: string = '';
101 NetPort
: Word = 25666;
103 NetAllowRCON
: Boolean = False;
104 NetRCONPassword
: string = '';
106 NetTimeToUpdate
: Cardinal = 0;
107 NetTimeToReliable
: Cardinal = 0;
108 NetTimeToMaster
: Cardinal = 0;
110 NetHost
: pENetHost
= nil;
111 NetPeer
: pENetPeer
= nil;
113 NetAddr
: ENetAddress
;
115 NetPongAddr
: ENetAddress
;
116 NetPongSock
: ENetSocket
= ENET_SOCKET_NULL
;
118 NetUseMaster
: Boolean = True;
119 NetSlistAddr
: ENetAddress
;
120 NetSlistIP
: string = 'mpms.doom2d.org';
121 NetSlistPort
: Word = 25665;
123 NetClientIP
: string = '127.0.0.1';
124 NetClientPort
: Word = 25666;
126 NetIn
, NetOut
: TBuffer
;
127 NetSend
: array [0..CH_MAX
] of TBuffer
;
129 NetClients
: array of TNetClient
= nil;
130 NetClientCount
: Byte = 0;
131 NetMaxClients
: Byte = 255;
132 NetBannedHosts
: array of TBanRecord
= nil;
134 NetState
: Integer = NET_STATE_NONE
;
136 NetMyID
: Integer = -1;
137 NetPlrUID1
: Integer = -1;
138 NetPlrUID2
: Integer = -1;
140 NetInterpLevel
: Integer = 1;
141 NetUpdateRate
: Cardinal = 0; // as soon as possible
142 NetRelupdRate
: Cardinal = 18; // around two times a second
143 NetMasterRate
: Cardinal = 60000;
145 NetForcePlayerUpdate
: Boolean = False;
146 NetPredictSelf
: Boolean = True;
147 NetGotKeys
: Boolean = False;
149 NetGotEverything
: Boolean = False;
151 NetDumpFile
: TStream
;
153 function g_Net_Init(): Boolean;
154 procedure g_Net_Cleanup();
155 procedure g_Net_Free();
156 procedure g_Net_Flush();
158 function g_Net_Host(IPAddr
: LongWord; Port
: enet_uint16
; MaxClients
: Cardinal = 16): Boolean;
159 procedure g_Net_Host_Die();
160 procedure g_Net_Host_Send(ID
: Integer; Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
161 function g_Net_Host_Update(): enet_size_t
;
162 procedure g_Net_Host_FlushBuffers();
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
;
169 procedure g_Net_Client_FlushBuffers();
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();
198 e_input
, g_nethandler
, g_netmsg
, g_netmaster
, g_player
, g_window
, g_console
,
199 g_main
, g_game
, g_language
, g_weapons
, utils
;
202 { /// SERVICE FUNCTIONS /// }
205 procedure SendBuffer(B
: pTBuffer
; Ch
: Integer; Peer
: pENetPeer
);
210 if Ch
= CH_RELIABLE
then Fl
:= ENET_PACKET_FLAG_RELIABLE
212 if B
^.WritePos
> 0 then
214 P
:= enet_packet_create(Addr(B
^.Data
), B
^.WritePos
, Fl
);
218 enet_host_broadcast(NetHost
, Ch
, P
)
220 enet_peer_send(Peer
, Ch
, P
);
226 function g_Net_FindSlot(): Integer;
235 for I
:= Low(NetClients
) to High(NetClients
) do
237 if NetClients
[I
].Used
then
246 if C
>= NetMaxClients
then
254 if (Length(NetClients
) >= NetMaxClients
) then
258 SetLength(NetClients
, Length(NetClients
) + 1);
259 N
:= High(NetClients
);
265 NetClients
[N
].Used
:= True;
266 NetClients
[N
].ID
:= N
;
267 NetClients
[N
].RequestedFullUpdate
:= False;
268 NetClients
[N
].RCONAuth
:= False;
269 NetClients
[N
].Voted
:= False;
270 NetClients
[N
].Player
:= 0;
271 NetClients
[N
].Peer
:= nil;
272 for I
:= 0 to CH_MAX
do
273 e_Buffer_Clear(Addr(NetClients
[N
].SendBuf
[CH_MAX
]));
279 function g_Net_Init(): Boolean;
286 e_Buffer_Clear(@NetIn
);
287 e_Buffer_Clear(@NetOut
);
288 for I
:= 0 to CH_MAX
do
289 e_Buffer_Clear(@NetSend
[i
]);
290 SetLength(NetClients
, 0);
296 NetAddr
.port
:= 25666;
297 SetLength(NetBannedHosts
, 0);
298 if FileExists(DataDir
+ BANLIST_FILENAME
) then
300 Assign(F
, DataDir
+ BANLIST_FILENAME
);
305 if StrToIp(IPstr
, IP
) then
312 Result
:= (enet_initialize() = 0);
315 procedure g_Net_Flush();
317 if NetMode
= NET_SERVER
then
318 g_Net_Host_FlushBuffers()
320 g_Net_Client_FlushBuffers();
321 enet_host_flush(NetHost
);
324 procedure g_Net_Cleanup();
328 e_Buffer_Clear(@NetIn
);
329 e_Buffer_Clear(@NetOut
);
330 for i
:= 0 to CH_MAX
do
331 e_Buffer_Clear(@NetSend
[i
]);
333 SetLength(NetClients
, 0);
343 NetState
:= NET_STATE_NONE
;
345 NetPongSock
:= ENET_SOCKET_NULL
;
347 NetTimeToMaster
:= 0;
348 NetTimeToUpdate
:= 0;
349 NetTimeToReliable
:= 0;
357 procedure g_Net_Free();
362 NetInitDone
:= False;
366 { /// SERVER FUNCTIONS /// }
369 function g_Net_Host(IPAddr
: LongWord; Port
: enet_uint16
; MaxClients
: Cardinal = 16): Boolean;
371 if NetMode
<> NET_NONE
then
373 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_INGAME
]);
380 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST
], [Port
]));
381 if not NetInitDone
then
383 if (not g_Net_Init()) then
385 g_Console_Add(_lc
[I_NET_MSG_FERROR
] + _lc
[I_NET_ERR_ENET
]);
393 NetAddr
.host
:= IPAddr
;
394 NetAddr
.port
:= Port
;
396 NetHost
:= enet_host_create(@NetAddr
, NET_MAXCLIENTS
, NET_CHANS
, 0, 0);
398 if (NetHost
= nil) then
400 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + Format(_lc
[I_NET_ERR_HOST
], [Port
]));
406 NetPongSock
:= enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM
);
407 if NetPongSock
<> ENET_SOCKET_NULL
then
409 NetPongAddr
.host
:= IPAddr
;
410 NetPongAddr
.port
:= Port
+ 1;
411 if enet_socket_bind(NetPongSock
, @NetPongAddr
) < 0 then
413 enet_socket_destroy(NetPongSock
);
414 NetPongSock
:= ENET_SOCKET_NULL
;
417 enet_socket_set_option(NetPongSock
, ENET_SOCKOPT_NONBLOCK
, 1);
420 NetMode
:= NET_SERVER
;
421 e_Buffer_Clear(@NetOut
);
427 procedure g_Net_Host_Die();
431 if NetMode
<> NET_SERVER
then Exit
;
433 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_DISCALL
]);
434 for I
:= 0 to High(NetClients
) do
435 if NetClients
[I
].Used
then
436 enet_peer_disconnect(NetClients
[I
].Peer
, NET_DISC_DOWN
);
438 while enet_host_service(NetHost
, @NetEvent
, 1000) > 0 do
439 if NetEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
then
440 enet_packet_destroy(NetEvent
.packet
);
442 for I
:= 0 to High(NetClients
) do
443 if NetClients
[I
].Used
then
445 FreeMemory(NetClients
[I
].Peer
^.data
);
446 NetClients
[I
].Peer
^.data
:= nil;
447 enet_peer_reset(NetClients
[I
].Peer
);
448 NetClients
[I
].Peer
:= nil;
449 NetClients
[I
].Used
:= False;
452 if (NetMPeer
<> nil) and (NetMHost
<> nil) then g_Net_Slist_Disconnect
;
453 if NetPongSock
<> ENET_SOCKET_NULL
then
454 enet_socket_destroy(NetPongSock
);
456 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_DIE
]);
457 enet_host_destroy(NetHost
);
462 e_WriteLog('NET: Server stopped', MSG_NOTIFY
);
465 procedure g_Net_Host_FlushBuffers();
470 SendBuffer(@NetSend
[CH_RELIABLE
], CH_RELIABLE
, nil);
471 SendBuffer(@NetSend
[CH_UNRELIABLE
], CH_UNRELIABLE
, nil);
472 // send to individual clients
473 if NetClients
<> nil then
474 for I
:= Low(NetClients
) to High(NetClients
) do
475 with NetClients
[I
] do
477 SendBuffer(@SendBuf
[CH_RELIABLE
], CH_RELIABLE
, Peer
);
478 SendBuffer(@SendBuf
[CH_UNRELIABLE
], CH_UNRELIABLE
, Peer
);
482 procedure g_Net_Host_Send(ID
: Integer; Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
494 if (ID
> High(NetClients
)) or (NetClients
[ID
].Peer
= nil) then
496 e_Buffer_Clear(@NetOut
);
499 B
:= Addr(NetClients
[ID
].SendBuf
[I
]);
503 B
:= Addr(NetSend
[I
]);
506 e_Buffer_Write(B
, @NetOut
);
507 e_Buffer_Clear(@NetOut
);
510 procedure g_Net_Host_CheckPings();
516 Ping
: array [0..9] of Byte;
519 if NetPongSock
= ENET_SOCKET_NULL
then Exit
;
521 Buf
.data
:= Addr(Ping
[0]);
522 Buf
.dataLength
:= 2+8;
526 Len
:= enet_socket_receive(NetPongSock
, @ClAddr
, @Buf
, 1);
527 if Len
< 0 then Exit
;
529 if (Ping
[0] = Ord('D')) and (Ping
[1] = Ord('F')) then
531 ClTime
:= Int64(Addr(Ping
[2])^);
533 e_Buffer_Clear(@NetOut
);
534 e_Buffer_Write(@NetOut
, Byte(Ord('D')));
535 e_Buffer_Write(@NetOut
, Byte(Ord('F')));
536 e_Buffer_Write(@NetOut
, ClTime
);
537 g_Net_Slist_WriteInfo();
539 if gPlayer1
<> nil then Inc(NPl
);
540 if gPlayer2
<> nil then Inc(NPl
);
541 e_Buffer_Write(@NetOut
, NPl
);
542 e_Buffer_Write(@NetOut
, gNumBots
);
544 Buf
.data
:= Addr(NetOut
.Data
[0]);
545 Buf
.dataLength
:= NetOut
.WritePos
;
546 enet_socket_send(NetPongSock
, @ClAddr
, @Buf
, 1);
548 e_Buffer_Clear(@NetOut
);
552 function g_Net_Host_Update(): enet_size_t
;
566 g_Net_Host_CheckPings
;
569 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
571 case (NetEvent
.kind
) of
572 ENET_EVENT_TYPE_CONNECT
:
574 IP
:= IpToStr(NetEvent
.Peer
^.address
.host
);
575 Port
:= NetEvent
.Peer
^.address
.port
;
576 g_Console_Add(_lc
[I_NET_MSG
] +
577 Format(_lc
[I_NET_MSG_HOST_CONN
], [IP
, Port
]));
579 if (NetEvent
.data
<> NET_PROTOCOL_VER
) then
581 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_REJECT
] +
582 _lc
[I_NET_DISC_PROTOCOL
]);
583 NetEvent
.peer
^.data
:= GetMemory(SizeOf(Byte));
584 Byte(NetEvent
.peer
^.data
^) := 255;
585 enet_peer_disconnect(NetEvent
.peer
, NET_DISC_PROTOCOL
);
586 enet_host_flush(NetHost
);
590 ID
:= g_Net_FindSlot();
594 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_HOST_REJECT
] +
595 _lc
[I_NET_DISC_FULL
]);
596 NetEvent
.Peer
^.data
:= GetMemory(SizeOf(Byte));
597 Byte(NetEvent
.peer
^.data
^) := 255;
598 enet_peer_disconnect(NetEvent
.peer
, NET_DISC_FULL
);
599 enet_host_flush(NetHost
);
603 NetClients
[ID
].Peer
:= NetEvent
.peer
;
604 NetClients
[ID
].Peer
^.data
:= GetMemory(SizeOf(Byte));
605 Byte(NetClients
[ID
].Peer
^.data
^) := ID
;
606 NetClients
[ID
].State
:= NET_STATE_AUTH
;
607 NetClients
[ID
].RCONAuth
:= False;
609 enet_peer_timeout(NetEvent
.peer
, ENET_PEER_TIMEOUT_LIMIT
* 2, ENET_PEER_TIMEOUT_MINIMUM
* 2, ENET_PEER_TIMEOUT_MAXIMUM
* 2);
612 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST_ADD
], [ID
]));
615 ENET_EVENT_TYPE_RECEIVE
:
617 ID
:= Byte(NetEvent
.peer
^.data
^);
618 if ID
> High(NetClients
) then Exit
;
619 TC
:= @NetClients
[ID
];
620 g_Net_HostMsgHandler(TC
, NetEvent
.packet
);
623 ENET_EVENT_TYPE_DISCONNECT
:
625 ID
:= Byte(NetEvent
.peer
^.data
^);
626 if ID
> High(NetClients
) then Exit
;
627 TC
:= @NetClients
[ID
];
628 if TC
= nil then Exit
;
630 if not (TC
^.Used
) then Exit
;
632 TP
:= g_Player_Get(TC
^.Player
);
637 TP
.Kill(K_SIMPLEKILL
, 0, HIT_DISCON
);
638 g_Console_Add(Format(_lc
[I_PLAYER_LEAVE
], [TP
.Name
]), True);
639 e_WriteLog('NET: Client ' + TP
.Name
+ ' [' + IntToStr(ID
) + '] disconnected.', MSG_NOTIFY
);
640 g_Player_Remove(TP
.UID
);
644 TC
^.State
:= NET_STATE_NONE
;
647 TC
^.RequestedFullUpdate
:= False;
649 FreeMemory(NetEvent
.peer
^.data
);
650 NetEvent
.peer
^.data
:= nil;
651 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_HOST_DISC
], [ID
]));
654 if NetUseMaster
then g_Net_Slist_Update
;
661 { /// CLIENT FUNCTIONS /// }
664 procedure g_Net_Disconnect(Forced
: Boolean = False);
666 if NetMode
<> NET_CLIENT
then Exit
;
667 if (NetHost
= nil) or (NetPeer
= nil) then Exit
;
671 enet_peer_disconnect(NetPeer
, NET_DISC_NONE
);
673 while (enet_host_service(NetHost
, @NetEvent
, 1500) > 0) do
675 if (NetEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
) then
681 if (NetEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
) then
682 enet_packet_destroy(NetEvent
.packet
);
685 if NetPeer
<> nil then
687 enet_peer_reset(NetPeer
);
693 e_WriteLog('NET: Kicked from server: ' + IntToStr(NetEvent
.data
), MSG_NOTIFY
);
694 if (NetEvent
.data
<= NET_DISC_MAX
) then
695 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_KICK
] +
696 _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + NetEvent
.data
)], True);
699 if NetHost
<> nil then
701 enet_host_destroy(NetHost
);
704 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_CLIENT_DISC
]);
707 e_WriteLog('NET: Disconnected', MSG_NOTIFY
);
710 procedure g_Net_Client_FlushBuffers();
712 SendBuffer(@NetSend
[CH_RELIABLE
], CH_RELIABLE
, NetPeer
);
713 SendBuffer(@NetSend
[CH_UNRELIABLE
], CH_UNRELIABLE
, NetPeer
);
716 procedure g_Net_Client_Send(Reliable
: Boolean; Chan
: Byte = NET_CHAN_GAME
);
724 e_Buffer_Write(@NetSend
[I
], @NetOut
);
725 e_Buffer_Clear(@NetOut
);
728 function g_Net_Client_Update(): enet_size_t
;
731 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
733 case NetEvent
.kind
of
734 ENET_EVENT_TYPE_RECEIVE
:
736 g_Net_ClientMsgHandler(NetEvent
.packet
);
739 ENET_EVENT_TYPE_DISCONNECT
:
741 g_Net_Disconnect(True);
749 function g_Net_Client_UpdateWhileLoading(): enet_size_t
;
752 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
754 case NetEvent
.kind
of
755 ENET_EVENT_TYPE_RECEIVE
:
757 g_Net_ClientLightMsgHandler(NetEvent
.packet
);
760 ENET_EVENT_TYPE_DISCONNECT
:
762 g_Net_Disconnect(True);
770 function g_Net_Connect(IP
: string; Port
: enet_uint16
): Boolean;
774 if NetMode
<> NET_NONE
then
776 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_ERR_INGAME
], True);
783 g_Console_Add(_lc
[I_NET_MSG
] + Format(_lc
[I_NET_MSG_CLIENT_CONN
],
785 if not NetInitDone
then
787 if (not g_Net_Init()) then
789 g_Console_Add(_lc
[I_NET_MSG_FERROR
] + _lc
[I_NET_ERR_ENET
], True);
797 NetHost
:= enet_host_create(nil, 1, NET_CHANS
, 0, 0);
799 if (NetHost
= nil) then
801 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
807 enet_address_set_host(@NetAddr
, PChar(Addr(IP
[1])));
808 NetAddr
.port
:= Port
;
810 NetPeer
:= enet_host_connect(NetHost
, @NetAddr
, NET_CHANS
, NET_PROTOCOL_VER
);
812 if (NetPeer
= nil) then
814 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CLIENT
], True);
815 enet_host_destroy(NetHost
);
824 while (enet_host_service(NetHost
, @NetEvent
, 0) > 0) do
826 if (NetEvent
.kind
= ENET_EVENT_TYPE_CONNECT
) then
828 g_Console_Add(_lc
[I_NET_MSG
] + _lc
[I_NET_MSG_CLIENT_DONE
]);
829 NetMode
:= NET_CLIENT
;
830 e_Buffer_Clear(@NetOut
);
831 enet_peer_timeout(NetPeer
, ENET_PEER_TIMEOUT_LIMIT
* 2, ENET_PEER_TIMEOUT_MINIMUM
* 2, ENET_PEER_TIMEOUT_MAXIMUM
* 2);
833 NetClientPort
:= Port
;
844 if e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(IK_SPACE
) then
848 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_TIMEOUT
], True);
849 if NetPeer
<> nil then enet_peer_reset(NetPeer
);
850 if NetHost
<> nil then
852 enet_host_destroy(NetHost
);
859 function IpToStr(IP
: LongWord): string;
867 Result
:= Result
+ IntToStr(e_Raw_Read_Byte(Ptr
)) + '.';
868 Result
:= Result
+ IntToStr(e_Raw_Read_Byte(Ptr
)) + '.';
869 Result
:= Result
+ IntToStr(e_Raw_Read_Byte(Ptr
)) + '.';
870 Result
:= Result
+ IntToStr(e_Raw_Read_Byte(Ptr
));
874 function StrToIp(IPstr
: string; var IP
: LongWord): Boolean;
878 Result
:= enet_address_set_host(@EAddr
, PChar(@IPstr
[1])) = 0;
882 function g_Net_Client_ByName(Name
: string): pTNetClient
;
888 for a
:= Low(NetClients
) to High(NetClients
) do
889 if (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
891 pl
:= g_Player_Get(NetClients
[a
].Player
);
892 if pl
= nil then continue
;
893 if Copy(LowerCase(pl
.Name
), 1, Length(Name
)) <> LowerCase(Name
) then continue
;
894 if NetClients
[a
].Peer
<> nil then
896 Result
:= @NetClients
[a
];
902 function g_Net_Client_ByPlayer(PID
: Word): pTNetClient
;
907 for a
:= Low(NetClients
) to High(NetClients
) do
908 if (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
909 if NetClients
[a
].Player
= PID
then
911 Result
:= @NetClients
[a
];
916 function g_Net_ClientName_ByID(ID
: Integer): string;
922 if ID
= NET_EVERYONE
then
924 for a
:= Low(NetClients
) to High(NetClients
) do
925 if (NetClients
[a
].ID
= ID
) and (NetClients
[a
].Used
) and (NetClients
[a
].State
= NET_STATE_GAME
) then
927 pl
:= g_Player_Get(NetClients
[a
].Player
);
928 if pl
= nil then Exit
;
933 procedure g_Net_SendData(Data
:AByte
; peer
: pENetPeer
; Reliable
: Boolean; Chan
: Byte = NET_CHAN_DOWNLOAD
);
937 dataLength
: Cardinal;
939 dataLength
:= Length(Data
);
942 F
:= LongWord(ENET_PACKET_FLAG_RELIABLE
)
946 if (peer
<> nil) then
948 P
:= enet_packet_create(@Data
[0], dataLength
, F
);
949 if not Assigned(P
) then Exit
;
950 enet_peer_send(peer
, CH_DOWNLOAD
, P
);
954 P
:= enet_packet_create(@Data
[0], dataLength
, F
);
955 if not Assigned(P
) then Exit
;
956 enet_host_broadcast(NetHost
, CH_DOWNLOAD
, P
);
959 enet_host_flush(NetHost
);
962 function g_Net_Wait_Event(msgId
: Word): TMemoryStream
;
964 downloadEvent
: ENetEvent
;
968 msgStream
: TMemoryStream
;
970 FillChar(downloadEvent
, SizeOf(downloadEvent
), 0);
975 while (enet_host_service(NetHost
, @downloadEvent
, 0) > 0) do
977 if (downloadEvent
.kind
= ENET_EVENT_TYPE_RECEIVE
) and (downloadEvent
.packet
^.dataLength
> 2) then
979 Ptr
:= downloadEvent
.packet
^.data
+ 2; // skip length
982 if (MID
= msgId
) then
984 msgStream
:= TMemoryStream
.Create
;
985 msgStream
.SetSize(downloadEvent
.packet
^.dataLength
- 2);
986 msgStream
.WriteBuffer(Ptr
^, downloadEvent
.packet
^.dataLength
- 2);
987 msgStream
.Seek(0, soFromBeginning
);
990 enet_packet_destroy(downloadEvent
.packet
);
994 enet_packet_destroy(downloadEvent
.packet
);
998 if (downloadEvent
.kind
= ENET_EVENT_TYPE_DISCONNECT
) then
1000 if (downloadEvent
.data
<= NET_DISC_MAX
) then
1001 g_Console_Add(_lc
[I_NET_MSG_ERROR
] + _lc
[I_NET_ERR_CONN
] + ' ' +
1002 _lc
[TStrings_Locale(Cardinal(I_NET_DISC_NONE
) + downloadEvent
.data
)], True);
1012 if e_KeyPressed(IK_ESCAPE
) or e_KeyPressed(IK_SPACE
) then
1015 Result
:= msgStream
;
1018 function g_Net_IsHostBanned(IP
: LongWord; Perm
: Boolean = False): Boolean;
1023 if NetBannedHosts
= nil then
1025 for I
:= 0 to High(NetBannedHosts
) do
1026 if (NetBannedHosts
[I
].IP
= IP
) and ((not Perm
) or (NetBannedHosts
[I
].Perm
)) then
1033 procedure g_Net_BanHost(IP
: LongWord; Perm
: Boolean = True); overload
;
1039 if g_Net_IsHostBanned(IP
, Perm
) then
1043 for I
:= Low(NetBannedHosts
) to High(NetBannedHosts
) do
1044 if NetBannedHosts
[I
].IP
= 0 then
1052 SetLength(NetBannedHosts
, Length(NetBannedHosts
) + 1);
1053 P
:= High(NetBannedHosts
);
1056 NetBannedHosts
[P
].IP
:= IP
;
1057 NetBannedHosts
[P
].Perm
:= Perm
;
1060 procedure g_Net_BanHost(IP
: string; Perm
: Boolean = True); overload
;
1065 b
:= StrToIp(IP
, a
);
1067 g_Net_BanHost(a
, Perm
);
1070 procedure g_Net_UnbanNonPermHosts();
1074 if NetBannedHosts
= nil then
1076 for I
:= Low(NetBannedHosts
) to High(NetBannedHosts
) do
1077 if (NetBannedHosts
[I
].IP
> 0) and not NetBannedHosts
[I
].Perm
then
1079 NetBannedHosts
[I
].IP
:= 0;
1080 NetBannedHosts
[I
].Perm
:= True;
1084 function g_Net_UnbanHost(IP
: string): Boolean; overload
;
1088 Result
:= StrToIp(IP
, a
);
1090 Result
:= g_Net_UnbanHost(a
);
1093 function g_Net_UnbanHost(IP
: LongWord): Boolean; overload
;
1100 if NetBannedHosts
= nil then
1102 for I
:= 0 to High(NetBannedHosts
) do
1103 if NetBannedHosts
[I
].IP
= IP
then
1105 NetBannedHosts
[I
].IP
:= 0;
1106 NetBannedHosts
[I
].Perm
:= True;
1108 // no break here to clear all bans of this host, perm and non-perm
1112 procedure g_Net_SaveBanList();
1117 Assign(F
, DataDir
+ BANLIST_FILENAME
);
1119 if NetBannedHosts
<> nil then
1120 for I
:= 0 to High(NetBannedHosts
) do
1121 if NetBannedHosts
[I
].Perm
and (NetBannedHosts
[I
].IP
> 0) then
1122 Writeln(F
, IpToStr(NetBannedHosts
[I
].IP
));
1126 procedure g_Net_DumpStart();
1128 if NetMode
= NET_SERVER
then
1129 NetDumpFile
:= createDiskFile(NETDUMP_FILENAME
+ '_server')
1131 NetDumpFile
:= createDiskFile(NETDUMP_FILENAME
+ '_client');
1134 procedure g_Net_DumpSendBuffer();
1136 writeInt(NetDumpFile
, gTime
);
1137 writeInt(NetDumpFile
, LongWord(NetOut
.WritePos
));
1138 writeInt(NetDumpFile
, Byte(1));
1139 NetDumpFile
.WriteBuffer(NetOut
.Data
[0], NetOut
.WritePos
);
1142 procedure g_Net_DumpRecvBuffer(Buf
: penet_uint8
; Len
: LongWord);
1144 if (Buf
= nil) or (Len
= 0) then Exit
;
1145 writeInt(NetDumpFile
, gTime
);
1146 writeInt(NetDumpFile
, Len
);
1147 writeInt(NetDumpFile
, Byte(0));
1148 NetDumpFile
.WriteBuffer(Buf
^, Len
);
1151 procedure g_Net_DumpEnd();