X-Git-Url: http://deadsoftware.ru/gitweb?p=d2df-sdl.git;a=blobdiff_plain;f=src%2Fgame%2Fg_net.pas;h=edd6024a49af09237130e98d105a0d3d0948efb9;hp=d8fc0046f58eaa49addd1c9d4bf228f78508a985;hb=4204edd3c7df01198a2289af4896be0575fff15c;hpb=8919240d166ec9b0922747a6a02883f49420a0a1 diff --git a/src/game/g_net.pas b/src/game/g_net.pas index d8fc004..edd6024 100644 --- a/src/game/g_net.pas +++ b/src/game/g_net.pas @@ -21,7 +21,7 @@ uses e_log, e_msg, utils, ENet, Classes, md5, MAPDEF{$IFDEF USE_MINIUPNPC}, miniupnpc;{$ELSE};{$ENDIF} const - NET_PROTOCOL_VER = 187; + NET_PROTOCOL_VER = 188; NET_MAXCLIENTS = 24; NET_CHANS = 12; @@ -106,6 +106,8 @@ type RCONAuth: Boolean; Voted: Boolean; Crimes: Integer; + AuthTime: LongWord; + MsgTime: LongWord; Transfer: TNetFileTransfer; // only one transfer may be active NetOut: array [0..1] of TMsg; end; @@ -159,6 +161,9 @@ var NetAutoBanPerm: Boolean = True; NetAutoBanWarn: Boolean = False; + NetAuthTimeout: Integer = 15 * 1000; + NetPacketTimeout: Integer = 30 * 1000; + NetState: Integer = NET_STATE_NONE; NetMyID: Integer = -1; @@ -340,7 +345,6 @@ begin e_KeyPressed(JOY3_JUMP) end; - //************************************************************************** // // file transfer declaraions and host packet processor @@ -505,6 +509,12 @@ begin exit; end; + // don't time out clients during a file transfer + if (NetAuthTimeout > 0) then + nc^.AuthTime := gTime + NetAuthTimeout; + if (NetPacketTimeout > 0) then + nc^.MsgTime := gTime + NetPacketTimeout; + tf := @NetClients[nid].Transfer; tf.lastAckTime := GetTimerMS(); @@ -1564,6 +1574,8 @@ begin NetClients[I].Used := False; NetClients[I].Player := 0; NetClients[I].Crimes := 0; + NetClients[I].AuthTime := 0; + NetClients[I].MsgTime := 0; NetClients[I].NetOut[NET_UNRELIABLE].Free(); NetClients[I].NetOut[NET_RELIABLE].Free(); end; @@ -1654,6 +1666,77 @@ begin end; end; +procedure g_Net_Host_CheckTimeouts(); +var + ID: Integer; +begin + for ID := Low(NetClients) to High(NetClients) do + begin + with NetClients[ID] do + begin + if (Peer = nil) or (State = NET_STATE_NONE) then continue; + if (State = NET_STATE_AUTH) and (AuthTime > 0) and (AuthTime <= gTime) then + begin + g_Net_Penalize(@NetClients[ID], 'auth taking too long'); + AuthTime := gTime + 500; // do it twice a second to give them a chance + end + else if (State = NET_STATE_GAME) and (MsgTime > 0) and (MsgTime <= gTime) then + begin + g_Net_Penalize(@NetClients[ID], 'message timeout'); + AuthTime := gTime + 500; // do it twice a second to give them a chance + end; + end; + end; +end; + +procedure g_Net_Host_Disconnect_Client(ID: Integer; Force: Boolean = False); +var + TP: TPlayer; + TC: pTNetClient; +begin + TC := @NetClients[ID]; + if (TC = nil) then Exit; + clearNetClient(NetClients[ID]); + if not (TC^.Used) then Exit; + + TP := g_Player_Get(TC^.Player); + + if TP <> nil then + begin + TP.Lives := 0; + TP.Kill(K_SIMPLEKILL, 0, HIT_DISCON); + g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [TP.Name]), True); + e_WriteLog('NET: Client ' + TP.Name + ' [' + IntToStr(TC^.ID) + '] disconnected.', TMsgType.Notify); + g_Player_Remove(TP.UID); + end; + + if (TC^.Peer^.data <> nil) then + begin + FreeMemory(TC^.Peer^.data); + TC^.Peer^.data := nil; + end; + + if (Force) then + enet_peer_reset(TC^.Peer); + + TC^.Used := False; + TC^.State := NET_STATE_NONE; + TC^.Peer := nil; + TC^.Player := 0; + TC^.Crimes := 0; + TC^.AuthTime := 0; + TC^.MsgTime := 0; + TC^.RequestedFullUpdate := False; + TC^.WaitForFirstSpawn := False; + TC^.NetOut[NET_UNRELIABLE].Free(); + TC^.NetOut[NET_RELIABLE].Free(); + + g_Console_Add(_lc[I_NET_MSG] + Format(_lc[I_NET_MSG_HOST_DISC], [ID])); + Dec(NetClientCount); + + if NetUseMaster then g_Net_Slist_ServerPlayerLeaves(); +end; + function g_Net_Host_Update(): enet_size_t; var @@ -1661,13 +1744,13 @@ var Port: Word; ID: Integer; TC: pTNetClient; - TP: TPlayer; begin IP := ''; Result := 0; if NetUseMaster then g_Net_Slist_Pulse(); g_Net_Host_CheckPings(); + g_Net_Host_CheckTimeouts(); while (enet_host_service(NetHost, @NetEvent, 0) > 0) do begin @@ -1730,6 +1813,14 @@ begin NetClients[ID].RCONAuth := False; NetClients[ID].NetOut[NET_UNRELIABLE].Alloc(NET_BUFSIZE*2); NetClients[ID].NetOut[NET_RELIABLE].Alloc(NET_BUFSIZE*2); + if (NetAuthTimeout > 0) then + NetClients[ID].AuthTime := gTime + NetAuthTimeout + else + NetClients[ID].AuthTime := 0; + if (NetPacketTimeout > 0) then + NetClients[ID].MsgTime := gTime + NetPacketTimeout + else + NetClients[ID].MsgTime := 0; clearNetClientTransfers(NetClients[ID]); // just in case enet_peer_timeout(NetEvent.peer, ENET_PEER_TIMEOUT_LIMIT * 2, ENET_PEER_TIMEOUT_MINIMUM * 2, ENET_PEER_TIMEOUT_MAXIMUM * 2); @@ -1751,6 +1842,9 @@ begin if ID > High(NetClients) then Exit; TC := @NetClients[ID]; + if (NetPacketTimeout > 0) then + TC^.MsgTime := gTime + NetPacketTimeout; + if NetDump then g_Net_DumpRecvBuffer(NetEvent.packet^.data, NetEvent.packet^.dataLength); g_Net_Host_HandlePacket(TC, NetEvent.packet, g_Net_HostMsgHandler); end; @@ -1760,39 +1854,7 @@ begin begin ID := Byte(NetEvent.peer^.data^); if ID > High(NetClients) then Exit; - clearNetClient(NetClients[ID]); - TC := @NetClients[ID]; - if TC = nil then Exit; - - if not (TC^.Used) then Exit; - - TP := g_Player_Get(TC^.Player); - - if TP <> nil then - begin - TP.Lives := 0; - TP.Kill(K_SIMPLEKILL, 0, HIT_DISCON); - g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [TP.Name]), True); - e_WriteLog('NET: Client ' + TP.Name + ' [' + IntToStr(ID) + '] disconnected.', TMsgType.Notify); - g_Player_Remove(TP.UID); - end; - - TC^.Used := False; - TC^.State := NET_STATE_NONE; - TC^.Peer := nil; - TC^.Player := 0; - TC^.Crimes := 0; - TC^.RequestedFullUpdate := False; - TC^.WaitForFirstSpawn := False; - TC^.NetOut[NET_UNRELIABLE].Free(); - TC^.NetOut[NET_RELIABLE].Free(); - - FreeMemory(NetEvent.peer^.data); - NetEvent.peer^.data := nil; - g_Console_Add(_lc[I_NET_MSG] + Format(_lc[I_NET_MSG_HOST_DISC], [ID])); - Dec(NetClientCount); - - if NetUseMaster then g_Net_Slist_ServerPlayerLeaves(); + g_Net_Host_Disconnect_Client(ID); end; end; end; @@ -2235,10 +2297,19 @@ var s: string; begin e_LogWritefln('NET: client #%u (cid #%u) triggered a penalty (%d/%d): %s', - [C^.ID, C^.Player, C^.Crimes, NetAutoBanLimit, Reason]); + [C^.ID, C^.Player, C^.Crimes + 1, NetAutoBanLimit, Reason]); if (NetAutoBanLimit <= 0) then Exit; + if (C^.Crimes >= NetAutoBanLimit) then + begin + // we have tried asking nicely before, now it is time to die + e_LogWritefln('NET: client #%u (cid #%u) force kicked', + [C^.ID, C^.Player]); + g_Net_Host_Disconnect_Client(C^.ID, True); + Exit; + end; + Inc(C^.Crimes); if (NetAutoBanWarn) then @@ -2512,6 +2583,9 @@ initialization conRegVar('sv_autoban_permanent', @NetAutoBanPerm, '', 'whether autobans are permanent'); conRegVar('sv_autoban_warn', @NetAutoBanWarn, '', 'send warnings to the client when he triggers penalties'); + conRegVar('sv_auth_timeout', @NetAuthTimeout, '', 'number of msec in which connecting clients must complete auth (0 = unlimited)'); + conRegVar('sv_packet_timeout', @NetPacketTimeout, '', 'number of msec the client must idle to be kicked (0 = unlimited)'); + conRegVar('net_master_list', @NetMasterList, '', 'list of master servers'); SetLength(NetClients, 0);