ClientStats.java 6.0 KB

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