forked from SpigotMC/BungeeCord
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from SpigotMC/master
[pull] master from SpigotMC:master
- Loading branch information
Showing
5 changed files
with
94 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package net.md_5.bungee.util; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class PacketLimiter | ||
{ | ||
|
||
// max amount of packets allowed per second | ||
private final int limit; | ||
// max amount of data allowed per second | ||
private final int dataLimit; | ||
|
||
@Getter | ||
private int counter; | ||
@Getter | ||
private int dataCounter; | ||
private long nextSecond; | ||
|
||
/** | ||
* Counts the received packet amount and size. | ||
* | ||
* @param size size of the packet | ||
* @return return false if the player should be kicked | ||
*/ | ||
public boolean incrementAndCheck(int size) | ||
{ | ||
counter++; | ||
dataCounter += size; | ||
|
||
if ( ( limit > 0 && counter > limit ) || ( dataLimit > 0 && dataCounter > dataLimit ) ) | ||
{ | ||
long now = System.currentTimeMillis(); | ||
if ( nextSecond > now ) | ||
{ | ||
return false; | ||
} | ||
nextSecond = now + 1000; | ||
counter = 0; | ||
dataCounter = 0; | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters