예제 #1
0
  private void tickProcessor() throws Exception {
    this.lastMeasure = System.currentTimeMillis();
    while (!this.shutdown) {
      long start = System.currentTimeMillis();
      int max = 5000;
      while (max > 0 && this.receivePacket()) {
        --max;
      }
      while (this.receiveStream()) ;

      long time = System.currentTimeMillis() - start;
      if (time < 50) {
        try {
          Thread.sleep(50 - time);
        } catch (InterruptedException e) {
          // ignore
        }
      }
      this.tick();
    }
  }
예제 #2
0
 public void blockAddress(String address, int timeout) {
   long finalTime = System.currentTimeMillis() + timeout * 1000;
   if (!this.block.containsKey(address) || timeout == -1) {
     if (timeout == -1) {
       finalTime = Long.MAX_VALUE;
     } else {
       this.getLogger().notice("Blocked " + address + " for " + timeout + " seconds");
     }
     this.block.put(address, finalTime);
   } else if (this.block.get(address) < finalTime) {
     this.block.put(address, finalTime);
   }
 }
예제 #3
0
  private void tick() throws Exception {
    long time = System.currentTimeMillis();
    for (Session session : new ArrayList<>(this.sessions.values())) {
      session.update(time);
    }

    for (String address : this.ipSec.keySet()) {
      int count = this.ipSec.get(address);
      if (count >= this.packetLimit) {
        this.blockAddress(address);
      }
    }
    this.ipSec.clear();

    if ((this.ticks & 0b1111) == 0) {
      double diff = Math.max(50d, (double) time - this.lastMeasure);
      this.streamOption("bandwidth", this.sendBytes / diff + ";" + this.receiveBytes / diff);
      this.lastMeasure = time;
      this.sendBytes = 0;
      this.receiveBytes = 0;

      if (!this.block.isEmpty()) {
        long now = System.currentTimeMillis();
        for (String address : new ArrayList<>(this.block.keySet())) {
          long timeout = this.block.get(address);
          if (timeout <= now) {
            this.block.remove(address);
            this.getLogger().notice("Unblocked " + address);
          } else {
            break;
          }
        }
      }
    }

    ++this.ticks;
  }