diff options
Diffstat (limited to 'src/net/packetlimiter.cpp')
-rw-r--r-- | src/net/packetlimiter.cpp | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/src/net/packetlimiter.cpp b/src/net/packetlimiter.cpp index 2859ff205..cb9c7e19c 100644 --- a/src/net/packetlimiter.cpp +++ b/src/net/packetlimiter.cpp @@ -1,9 +1,9 @@ /* - * The ManaPlus Client + * The ManaVerse Client * Copyright (C) 2011-2020 The ManaPlus Developers - * Copyright (C) 2020-2023 The ManaVerse Developers + * Copyright (C) 2020-2025 The ManaVerse Developers * - * This file is part of The ManaPlus Client. + * This file is part of The ManaVerse Client. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,23 +33,41 @@ #include "debug.h" +// Client-side packet limiter +// The time limits here are all in units of 10ms (SDL timer tick). +// See MILLISECONDS_IN_A_TICK. + struct PacketLimit final { A_DEFAULT_COPY(PacketLimit) + // timer tick of the first packet in a burst int lastTime; + // number of basic ticks in one packet burst int timeLimit; + // number of packets inside an active packet burst int cnt; + // max number of packets allowed inside a packet burst int cntLimit; }; PacketLimit mPacketLimits[CAST_SIZE(PacketType::PACKET_SIZE) + 1]; +// remember to increment this if you (massively) change the above, +// so that the limits file on user's system is properly discarded. +const int PACKET_LIMIT_FILE_VERSION = 4; void PacketLimiter::initPacketLimiter() { // here i setting packet limits. but current server is broken, // and this limits may not help. + // see also: server-side logic in: + // TMWA: src/map/clif.cpp (clif_check_packet_flood) + // Herc: src/map/clif.c (clif_parse) + // src/common/socket.c (do_sockets(func_parse)) + // (herc has no per-packet limits, just a per-connection + // fairness system.) + mPacketLimits[CAST_SIZE( PacketType::PACKET_CHAT)].timeLimit = 10 + 5; mPacketLimits[CAST_SIZE( @@ -200,7 +218,7 @@ void PacketLimiter::initPacketLimiter() return; } - const int ver = atoi(line); + const int fileVersion = atoi(line); for (int f = 0; f < CAST_S32(PacketType::PACKET_SIZE); @@ -209,7 +227,7 @@ void PacketLimiter::initPacketLimiter() if (!inPacketFile.getline(line, 100)) break; - if (!(ver == 1 && + if (!(fileVersion == 1 && (static_cast<PacketTypeT>(f) == PacketType::PACKET_DROP || static_cast<PacketTypeT>(f) == PacketType::PACKET_NPC_NEXT))) @@ -218,7 +236,7 @@ void PacketLimiter::initPacketLimiter() } } inPacketFile.close(); - if (ver < 5) + if (fileVersion < PACKET_LIMIT_FILE_VERSION) writePacketLimits(packetLimitsName); } } @@ -235,7 +253,7 @@ void PacketLimiter::writePacketLimits(const std::string &packetLimitsName) outPacketFile.close(); return; } - outPacketFile << "4" << std::endl; + outPacketFile << PACKET_LIMIT_FILE_VERSION << std::endl; for (int f = 0; f < CAST_S32(PacketType::PACKET_SIZE); f ++) { outPacketFile << toString(mPacketLimits[f].timeLimit) @@ -265,10 +283,9 @@ bool PacketLimiter::checkPackets(const PacketTypeT type) const int cntLimit = limit.cntLimit; if (lastTime > tick_time) - { + { // integer wrap-around check // instance()->mPacketLimits[type].lastTime = time; // instance()->mPacketLimits[type].cnt = 0; - return true; } else if (lastTime + timeLimit > time) @@ -305,7 +322,7 @@ bool PacketLimiter::limitPackets(const PacketTypeT type) const int cntLimit = pack.cntLimit; if (lastTime > tick_time) - { + { // integer wrap-around check pack.lastTime = time; pack.cnt = 0; return true; |