X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fgame%2Fg_net.pas;h=6d1be31fa920990a39f9ff7f95ce0ab66a437402;hb=62ceb85a85290d53a42a8aa13a5da9a04c4bc80c;hp=27825c263cf738da7301a6454a7b629d9ff8bc10;hpb=ad07d68e5caa4042bf2769099460d37c528e246c;p=d2df-sdl.git diff --git a/src/game/g_net.pas b/src/game/g_net.pas index 27825c2..6d1be31 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; @@ -103,6 +103,7 @@ type Player: Word; RequestedFullUpdate: Boolean; WaitForFirstSpawn: Boolean; // set to `true` in server, used to spawn a player on first full state request + FullUpdateSent: Boolean; RCONAuth: Boolean; Voted: Boolean; Crimes: Integer; @@ -160,9 +161,10 @@ var NetAutoBanLimit: Integer = 5; NetAutoBanPerm: Boolean = True; NetAutoBanWarn: Boolean = False; + NetAutoBanForTimeout: Boolean = False; - NetAuthTimeout: Integer = 15 * 1000; - NetPacketTimeout: Integer = 30 * 1000; + NetAuthTimeout: Integer = 30 * 1000; + NetPacketTimeout: Integer = 60 * 1000; NetState: Integer = NET_STATE_NONE; @@ -206,13 +208,13 @@ procedure g_Net_Flush(); function g_Net_Host(IPAddr: LongWord; Port: enet_uint16; MaxClients: Cardinal = 16): Boolean; procedure g_Net_Host_Die(); procedure g_Net_Host_Send(ID: Integer; Reliable: Boolean; Chan: Byte = NET_CHAN_GAME); -function g_Net_Host_Update(): enet_size_t; +procedure g_Net_Host_Update(); +procedure g_Net_Host_Kick(ID: Integer; Reason: enet_uint32); function g_Net_Connect(IP: string; Port: enet_uint16): Boolean; procedure g_Net_Disconnect(Forced: Boolean = False); procedure g_Net_Client_Send(Reliable: Boolean; Chan: Byte = NET_CHAN_GAME); -function g_Net_Client_Update(): enet_size_t; -function g_Net_Client_UpdateWhileLoading(): enet_size_t; +procedure g_Net_Client_Update(); function g_Net_Client_ByName(Name: string): pTNetClient; function g_Net_Client_ByPlayer(PID: Word): pTNetClient; @@ -371,7 +373,7 @@ const procedure killClientByFT (var nc: TNetClient); begin e_LogWritefln('disconnected client #%d due to file transfer error', [nc.ID], TMsgType.Warning); - enet_peer_disconnect(nc.Peer, NET_DISC_FILE_TIMEOUT); + g_Net_Host_Kick(nc.ID, NET_DISC_FILE_TIMEOUT); clearNetClientTransfers(nc); g_Net_Slist_ServerPlayerLeaves(); end; @@ -918,7 +920,8 @@ begin end; if (freePacket) then begin freePacket := false; enet_packet_destroy(ev.packet); end; end; - ProcessLoading(); + + ProcessLoading(False); if g_Net_UserRequestExit() then begin g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' user abort', True); @@ -1066,7 +1069,8 @@ begin end; if (freePacket) then begin freePacket := false; enet_packet_destroy(ev.packet); end; end; - ProcessLoading(); + + ProcessLoading(False); if g_Net_UserRequestExit() then begin g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' user abort', True); @@ -1265,7 +1269,8 @@ begin end; if (freePacket) then begin freePacket := false; enet_packet_destroy(ev.packet); end; end; - ProcessLoading(); + + ProcessLoading(False); if g_Net_UserRequestExit() then begin g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CONN] + ' user abort', True); @@ -1391,14 +1396,6 @@ begin if NetMode = NET_SERVER then for T := NET_UNRELIABLE to NET_RELIABLE do begin - if NetBuf[T].CurSize > 0 then - begin - P := enet_packet_create(NetBuf[T].Data, NetBuf[T].CurSize, F); - if not Assigned(P) then continue; - enet_host_broadcast(NetHost, Chan, P); - NetBuf[T].Clear(); - end; - for I := Low(NetClients) to High(NetClients) do begin if not NetClients[I].Used then continue; @@ -1614,15 +1611,84 @@ begin end else begin - // write size first - NetBuf[T].Write(Integer(NetOut.CurSize)); - NetBuf[T].Write(NetOut); + for ID := Low(NetClients) to High(NetClients) do + begin + if NetClients[ID].Used then + begin + // write size first + NetClients[ID].NetOut[T].Write(Integer(NetOut.CurSize)); + NetClients[ID].NetOut[T].Write(NetOut); + end; + end; end; if NetDump then g_Net_DumpSendBuffer(); NetOut.Clear(); 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^.FullUpdateSent := 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; + +procedure g_Net_Host_Kick(ID: Integer; Reason: enet_uint32); +var + Peer: pENetPeer; + TC: pTNetClient; +begin + TC := @NetClients[ID]; + if (TC <> nil) and TC^.Used and (TC^.Peer <> nil) then + begin + Peer := TC^.Peer; + g_Net_Host_Disconnect_Client(ID); + enet_peer_disconnect(Peer, Reason); + end; +end; + procedure g_Net_Host_CheckPings(); var ClAddr: ENetAddress; @@ -1678,67 +1744,28 @@ begin 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 + AuthTime := gTime + 1000; // do it every 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 + // client hasn't sent packets in a while; either ban em or kick em + if (NetAutoBanForTimeout) then + begin + g_Net_Penalize(@NetClients[ID], 'message timeout'); + MsgTime := gTime + (NetPacketTimeout div 2) + 500; // wait less for the next check + end + else + begin + e_LogWritefln('NET: client #%u (cid #%u) timed out', [ID, Player]); + g_Net_Host_Disconnect_Client(ID, True); + end; 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; +procedure g_Net_Host_Update(); var IP: string; Port: Word; @@ -1746,7 +1773,6 @@ var TC: pTNetClient; begin IP := ''; - Result := 0; if NetUseMaster then g_Net_Slist_Pulse(); g_Net_Host_CheckPings(); @@ -1769,10 +1795,7 @@ begin _lc[I_NET_DISC_PROTOCOL]); e_WriteLog('NET: Connection request from ' + IP + ' rejected: version mismatch', TMsgType.Notify); - NetEvent.peer^.data := GetMemory(SizeOf(Byte)); - Byte(NetEvent.peer^.data^) := 255; enet_peer_disconnect(NetEvent.peer, NET_DISC_PROTOCOL); - enet_host_flush(NetHost); Exit; end; @@ -1782,10 +1805,7 @@ begin _lc[I_NET_DISC_BAN]); e_WriteLog('NET: Connection request from ' + IP + ' rejected: banned', TMsgType.Notify); - NetEvent.peer^.data := GetMemory(SizeOf(Byte)); - Byte(NetEvent.peer^.data^) := 255; enet_peer_disconnect(NetEvent.Peer, NET_DISC_BAN); - enet_host_flush(NetHost); Exit; end; @@ -1797,10 +1817,7 @@ begin _lc[I_NET_DISC_FULL]); e_WriteLog('NET: Connection request from ' + IP + ' rejected: server full', TMsgType.Notify); - NetEvent.Peer^.data := GetMemory(SizeOf(Byte)); - Byte(NetEvent.peer^.data^) := 255; enet_peer_disconnect(NetEvent.peer, NET_DISC_FULL); - enet_host_flush(NetHost); Exit; end; @@ -1838,6 +1855,8 @@ begin end else begin + if NetEvent.peer^.data = nil then Exit; + ID := Byte(NetEvent.peer^.data^); if ID > High(NetClients) then Exit; TC := @NetClients[ID]; @@ -1852,9 +1871,12 @@ begin ENET_EVENT_TYPE_DISCONNECT: begin - ID := Byte(NetEvent.peer^.data^); - if ID > High(NetClients) then Exit; - g_Net_Host_Disconnect_Client(ID); + if NetEvent.peer^.data <> nil then + begin + ID := Byte(NetEvent.peer^.data^); + if ID > High(NetClients) then Exit; + g_Net_Host_Disconnect_Client(ID); + end; end; end; end; @@ -1931,15 +1953,13 @@ begin g_Net_Flush(); // FIXME: for now, send immediately end; -function g_Net_Client_Update(): enet_size_t; +procedure g_Net_Client_Update(); begin - Result := 0; while (NetHost <> nil) and (enet_host_service(NetHost, @NetEvent, 0) > 0) do begin case NetEvent.kind of ENET_EVENT_TYPE_RECEIVE: begin - if (NetEvent.channelID = NET_CHAN_DOWNLOAD_EX) then continue; // ignore all download packets, they're processed by separate code if NetDump then g_Net_DumpRecvBuffer(NetEvent.packet^.data, NetEvent.packet^.dataLength); g_Net_Client_HandlePacket(NetEvent.packet, g_Net_ClientMsgHandler); end; @@ -1947,37 +1967,12 @@ begin ENET_EVENT_TYPE_DISCONNECT: begin g_Net_Disconnect(True); - Result := 1; Exit; end; end; end end; -function g_Net_Client_UpdateWhileLoading(): enet_size_t; -begin - Result := 0; - while (enet_host_service(NetHost, @NetEvent, 0) > 0) do - begin - case NetEvent.kind of - ENET_EVENT_TYPE_RECEIVE: - begin - if (NetEvent.channelID = NET_CHAN_DOWNLOAD_EX) then continue; // ignore all download packets, they're processed by separate code - if NetDump then g_Net_DumpRecvBuffer(NetEvent.packet^.data, NetEvent.packet^.dataLength); - g_Net_Client_HandlePacket(NetEvent.packet, g_Net_ClientLightMsgHandler); - end; - - ENET_EVENT_TYPE_DISCONNECT: - begin - g_Net_Disconnect(True); - Result := 1; - Exit; - end; - end; - end; - g_Net_Flush(); -end; - function g_Net_Connect(IP: string; Port: enet_uint16): Boolean; var OuterLoop: Boolean; @@ -2060,8 +2055,7 @@ begin g_Console_Add(Format(_lc[I_NET_MSG_PORTS], [Integer(Port), Integer(NET_PING_PORT)]), True); end; - ProcessLoading(true); - + ProcessLoading(True); if e_KeyPressed(IK_SPACE) or e_KeyPressed(IK_ESCAPE) or e_KeyPressed(VK_ESCAPE) or e_KeyPressed(JOY0_JUMP) or e_KeyPressed(JOY1_JUMP) or e_KeyPressed(JOY2_JUMP) or e_KeyPressed(JOY3_JUMP) then OuterLoop := False; @@ -2157,23 +2151,16 @@ var begin dataLength := Length(Data); - if (Reliable) then - F := LongWord(ENET_PACKET_FLAG_RELIABLE) - else - F := 0; + if Reliable + then F := LongWord(ENET_PACKET_FLAG_RELIABLE) + else F := 0; - if (peer <> nil) then - begin - P := enet_packet_create(@Data[0], dataLength, F); - if not Assigned(P) then Exit; - enet_peer_send(peer, Chan, P); - end - else - begin - P := enet_packet_create(@Data[0], dataLength, F); - if not Assigned(P) then Exit; - enet_host_broadcast(NetHost, Chan, P); - end; + P := enet_packet_create(@Data[0], dataLength, F); + if not Assigned(P) then exit; + + if peer <> nil + then enet_peer_send(peer, Chan, P) + else enet_host_broadcast(NetHost, Chan, P); enet_host_flush(NetHost); end; @@ -2319,7 +2306,7 @@ begin begin s := '#' + IntToStr(C^.ID); // can't be arsed g_Net_BanHost(C^.Peer^.address.host, NetAutoBanPerm); - enet_peer_disconnect(C^.Peer, NET_DISC_BAN); + g_Net_Host_Kick(C^.ID, NET_DISC_BAN); g_Console_Add(Format(_lc[I_PLAYER_BAN], [s])); MH_SEND_GameEvent(NET_EV_PLAYER_BAN, 0, s); g_Net_Slist_ServerPlayerLeaves(); @@ -2529,7 +2516,7 @@ begin if b > NetMaxClients then begin s := g_Player_Get(NetClients[a].Player).Name; - enet_peer_disconnect(NetClients[a].Peer, NET_DISC_FULL); + g_Net_Host_Kick(NetClients[a].ID, NET_DISC_FULL); g_Console_Add(Format(_lc[I_PLAYER_KICK], [s])); MH_SEND_GameEvent(NET_EV_PLAYER_KICK, 0, s); end; @@ -2582,6 +2569,7 @@ initialization conRegVar('sv_autoban_threshold', @NetAutoBanLimit, '', 'max crimes before autoban (0 = no autoban)'); 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_autoban_packet_timeout', @NetAutoBanForTimeout, '', 'autoban for packet timeouts'); 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)');