ClientStats.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.network;
  20. import com.l2jserver.Config;
  21. public class ClientStats
  22. {
  23. public int processedPackets = 0;
  24. public int droppedPackets = 0;
  25. public int unknownPackets = 0;
  26. public int totalQueueSize = 0;
  27. public int maxQueueSize = 0;
  28. public int totalBursts = 0;
  29. public int maxBurstSize = 0;
  30. public int shortFloods = 0;
  31. public int longFloods = 0;
  32. public int totalQueueOverflows = 0;
  33. public int totalUnderflowExceptions = 0;
  34. private final int[] _packetsInSecond;
  35. private long _packetCountStartTick = 0;
  36. private int _head;
  37. private int _totalCount = 0;
  38. private int _floodsInMin = 0;
  39. private long _floodStartTick = 0;
  40. private int _unknownPacketsInMin = 0;
  41. private long _unknownPacketStartTick = 0;
  42. private int _overflowsInMin = 0;
  43. private long _overflowStartTick = 0;
  44. private int _underflowReadsInMin = 0;
  45. private long _underflowReadStartTick = 0;
  46. private volatile boolean _floodDetected = false;
  47. private volatile boolean _queueOverflowDetected = false;
  48. private final int BUFFER_SIZE;
  49. public ClientStats()
  50. {
  51. BUFFER_SIZE = Config.CLIENT_PACKET_QUEUE_MEASURE_INTERVAL;
  52. _packetsInSecond = new int[BUFFER_SIZE];
  53. _head = BUFFER_SIZE - 1;
  54. }
  55. /**
  56. * @return true if incoming packet need to be dropped
  57. */
  58. protected final boolean dropPacket()
  59. {
  60. final boolean result = _floodDetected || _queueOverflowDetected;
  61. if (result)
  62. {
  63. droppedPackets++;
  64. }
  65. return result;
  66. }
  67. /**
  68. * Later during flood returns true (and send ActionFailed) once per second.
  69. * @param queueSize
  70. * @return true if flood detected first and ActionFailed packet need to be sent.
  71. */
  72. protected final boolean countPacket(int queueSize)
  73. {
  74. processedPackets++;
  75. totalQueueSize += queueSize;
  76. if (maxQueueSize < queueSize)
  77. {
  78. maxQueueSize = queueSize;
  79. }
  80. if (_queueOverflowDetected && (queueSize < 2))
  81. {
  82. _queueOverflowDetected = false;
  83. }
  84. return countPacket();
  85. }
  86. /**
  87. * @return Counts unknown packets and return true if threshold is reached.
  88. */
  89. protected final boolean countUnknownPacket()
  90. {
  91. unknownPackets++;
  92. final long tick = System.currentTimeMillis();
  93. if ((tick - _unknownPacketStartTick) > 60000)
  94. {
  95. _unknownPacketStartTick = tick;
  96. _unknownPacketsInMin = 1;
  97. return false;
  98. }
  99. _unknownPacketsInMin++;
  100. return _unknownPacketsInMin > Config.CLIENT_PACKET_QUEUE_MAX_UNKNOWN_PER_MIN;
  101. }
  102. /**
  103. * @param count - current number of processed packets in burst
  104. * @return burst length and return true if execution of the queue need to be aborted.
  105. */
  106. protected final boolean countBurst(int count)
  107. {
  108. if (count > maxBurstSize)
  109. {
  110. maxBurstSize = count;
  111. }
  112. if (count < Config.CLIENT_PACKET_QUEUE_MAX_BURST_SIZE)
  113. {
  114. return false;
  115. }
  116. totalBursts++;
  117. return true;
  118. }
  119. /**
  120. * @return Counts queue overflows and return true if threshold is reached.
  121. */
  122. protected final boolean countQueueOverflow()
  123. {
  124. _queueOverflowDetected = true;
  125. totalQueueOverflows++;
  126. final long tick = System.currentTimeMillis();
  127. if ((tick - _overflowStartTick) > 60000)
  128. {
  129. _overflowStartTick = tick;
  130. _overflowsInMin = 1;
  131. return false;
  132. }
  133. _overflowsInMin++;
  134. return _overflowsInMin > Config.CLIENT_PACKET_QUEUE_MAX_OVERFLOWS_PER_MIN;
  135. }
  136. /**
  137. * @return Counts underflow exceptions and return true if threshold is reached.
  138. */
  139. protected final boolean countUnderflowException()
  140. {
  141. totalUnderflowExceptions++;
  142. final long tick = System.currentTimeMillis();
  143. if ((tick - _underflowReadStartTick) > 60000)
  144. {
  145. _underflowReadStartTick = tick;
  146. _underflowReadsInMin = 1;
  147. return false;
  148. }
  149. _underflowReadsInMin++;
  150. return _underflowReadsInMin > Config.CLIENT_PACKET_QUEUE_MAX_UNDERFLOWS_PER_MIN;
  151. }
  152. /**
  153. * @return true if maximum number of floods per minute is reached.
  154. */
  155. protected final boolean countFloods()
  156. {
  157. return _floodsInMin > Config.CLIENT_PACKET_QUEUE_MAX_FLOODS_PER_MIN;
  158. }
  159. private final boolean longFloodDetected()
  160. {
  161. return (_totalCount / BUFFER_SIZE) > Config.CLIENT_PACKET_QUEUE_MAX_AVERAGE_PACKETS_PER_SECOND;
  162. }
  163. /**
  164. * Later during flood returns true (and send ActionFailed) once per second.
  165. * @return true if flood detected first and ActionFailed packet need to be sent.
  166. */
  167. private final synchronized boolean countPacket()
  168. {
  169. _totalCount++;
  170. final long tick = System.currentTimeMillis();
  171. if ((tick - _packetCountStartTick) > 1000)
  172. {
  173. _packetCountStartTick = tick;
  174. // clear flag if no more flooding during last seconds
  175. if (_floodDetected && !longFloodDetected() && (_packetsInSecond[_head] < (Config.CLIENT_PACKET_QUEUE_MAX_PACKETS_PER_SECOND / 2)))
  176. {
  177. _floodDetected = false;
  178. }
  179. // wrap head of the buffer around the tail
  180. if (_head <= 0)
  181. {
  182. _head = BUFFER_SIZE;
  183. }
  184. _head--;
  185. _totalCount -= _packetsInSecond[_head];
  186. _packetsInSecond[_head] = 1;
  187. return _floodDetected;
  188. }
  189. final int count = ++_packetsInSecond[_head];
  190. if (!_floodDetected)
  191. {
  192. if (count > Config.CLIENT_PACKET_QUEUE_MAX_PACKETS_PER_SECOND)
  193. {
  194. shortFloods++;
  195. }
  196. else if (longFloodDetected())
  197. {
  198. longFloods++;
  199. }
  200. else
  201. {
  202. return false;
  203. }
  204. _floodDetected = true;
  205. if ((tick - _floodStartTick) > 60000)
  206. {
  207. _floodStartTick = tick;
  208. _floodsInMin = 1;
  209. }
  210. else
  211. {
  212. _floodsInMin++;
  213. }
  214. return true; // Return true only in the beginning of the flood
  215. }
  216. return false;
  217. }
  218. }