ClientStats.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. droppedPackets++;
  59. return result;
  60. }
  61. /**
  62. * Later during flood returns true (and send ActionFailed) once per second.
  63. * @param queueSize
  64. * @return true if flood detected first and ActionFailed packet need to be sent.
  65. */
  66. protected final boolean countPacket(int queueSize)
  67. {
  68. processedPackets++;
  69. totalQueueSize += queueSize;
  70. if (maxQueueSize < queueSize)
  71. maxQueueSize = queueSize;
  72. if (_queueOverflowDetected && queueSize < 2)
  73. _queueOverflowDetected = false;
  74. return countPacket();
  75. }
  76. /**
  77. * @return Counts unknown packets and return true if threshold is reached.
  78. */
  79. protected final boolean countUnknownPacket()
  80. {
  81. unknownPackets++;
  82. final long tick = System.currentTimeMillis();
  83. if (tick - _unknownPacketStartTick > 60000)
  84. {
  85. _unknownPacketStartTick = tick;
  86. _unknownPacketsInMin = 1;
  87. return false;
  88. }
  89. _unknownPacketsInMin++;
  90. return _unknownPacketsInMin > Config.CLIENT_PACKET_QUEUE_MAX_UNKNOWN_PER_MIN;
  91. }
  92. /**
  93. * @param count - current number of processed packets in burst
  94. * @return burst length and return true if execution of the queue need to be aborted.
  95. */
  96. protected final boolean countBurst(int count)
  97. {
  98. if (count > maxBurstSize)
  99. maxBurstSize = count;
  100. if (count < Config.CLIENT_PACKET_QUEUE_MAX_BURST_SIZE)
  101. return false;
  102. totalBursts++;
  103. return true;
  104. }
  105. /**
  106. * @return Counts queue overflows and return true if threshold is reached.
  107. */
  108. protected final boolean countQueueOverflow()
  109. {
  110. _queueOverflowDetected = true;
  111. totalQueueOverflows++;
  112. final long tick = System.currentTimeMillis();
  113. if (tick - _overflowStartTick > 60000)
  114. {
  115. _overflowStartTick = tick;
  116. _overflowsInMin = 1;
  117. return false;
  118. }
  119. _overflowsInMin++;
  120. return _overflowsInMin > Config.CLIENT_PACKET_QUEUE_MAX_OVERFLOWS_PER_MIN;
  121. }
  122. /**
  123. * @return Counts underflow exceptions and return true if threshold is reached.
  124. */
  125. protected final boolean countUnderflowException()
  126. {
  127. totalUnderflowExceptions++;
  128. final long tick = System.currentTimeMillis();
  129. if (tick - _underflowReadStartTick > 60000)
  130. {
  131. _underflowReadStartTick = tick;
  132. _underflowReadsInMin = 1;
  133. return false;
  134. }
  135. _underflowReadsInMin++;
  136. return _underflowReadsInMin > Config.CLIENT_PACKET_QUEUE_MAX_UNDERFLOWS_PER_MIN;
  137. }
  138. /**
  139. * @return true if maximum number of floods per minute is reached.
  140. */
  141. protected final boolean countFloods()
  142. {
  143. return _floodsInMin > Config.CLIENT_PACKET_QUEUE_MAX_FLOODS_PER_MIN;
  144. }
  145. private final boolean longFloodDetected()
  146. {
  147. return (_totalCount / BUFFER_SIZE) > Config.CLIENT_PACKET_QUEUE_MAX_AVERAGE_PACKETS_PER_SECOND;
  148. }
  149. /**
  150. * Later during flood returns true (and send ActionFailed) once per second.
  151. * @return true if flood detected first and ActionFailed packet need to be sent.
  152. */
  153. private final synchronized boolean countPacket()
  154. {
  155. _totalCount++;
  156. final long tick = System.currentTimeMillis();
  157. if (tick - _packetCountStartTick > 1000)
  158. {
  159. _packetCountStartTick = tick;
  160. // clear flag if no more flooding during last seconds
  161. if (_floodDetected
  162. && !longFloodDetected()
  163. && _packetsInSecond[_head] < Config.CLIENT_PACKET_QUEUE_MAX_PACKETS_PER_SECOND / 2)
  164. _floodDetected = false;
  165. // wrap head of the buffer around the tail
  166. if (_head <= 0)
  167. _head = BUFFER_SIZE;
  168. _head--;
  169. _totalCount -= _packetsInSecond[_head];
  170. _packetsInSecond[_head] = 1;
  171. return _floodDetected;
  172. }
  173. final int count = ++_packetsInSecond[_head];
  174. if (!_floodDetected)
  175. {
  176. if (count > Config.CLIENT_PACKET_QUEUE_MAX_PACKETS_PER_SECOND)
  177. shortFloods++;
  178. else if (longFloodDetected())
  179. longFloods++;
  180. else
  181. return false;
  182. _floodDetected = true;
  183. if (tick - _floodStartTick > 60000)
  184. {
  185. _floodStartTick = tick;
  186. _floodsInMin = 1;
  187. }
  188. else
  189. _floodsInMin++;
  190. return true; // Return true only in the beginning of the flood
  191. }
  192. return false;
  193. }
  194. }