ClientStats.java 6.2 KB

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