L2GameClientPacket.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.clientpackets;
  16. import java.nio.BufferUnderflowException;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import org.mmocore.network.ReceivablePacket;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.GameTimeController;
  22. import com.l2jserver.gameserver.network.L2GameClient;
  23. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  24. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  25. /**
  26. * Packets received by the game server from clients
  27. * @author KenM
  28. */
  29. public abstract class L2GameClientPacket extends ReceivablePacket<L2GameClient>
  30. {
  31. private static final Logger _log = Logger.getLogger(L2GameClientPacket.class.getName());
  32. @Override
  33. protected boolean read()
  34. {
  35. //_log.info(this.getType());
  36. try
  37. {
  38. readImpl();
  39. return true;
  40. }
  41. catch (Exception e)
  42. {
  43. _log.log(Level.SEVERE, "Client: " + getClient().toString() + " - Failed reading: " + getType() + " - L2J Server Version: " + Config.SERVER_VERSION + " - DP Revision: " + Config.DATAPACK_VERSION + " ; " + e.getMessage(), e);
  44. if (e instanceof BufferUnderflowException) // only one allowed per client per minute
  45. {
  46. if (GameTimeController.getGameTicks() - getClient().underflowReadStartTick > 600)
  47. {
  48. getClient().underflowReadStartTick = GameTimeController.getGameTicks();
  49. getClient().underflowReadsInMin = 1;
  50. }
  51. else if (++getClient().underflowReadsInMin > 1)
  52. {
  53. getClient().closeNow();
  54. _log.severe("Client " + getClient().toString() + " - Disconnected: Too many buffer underflow exceptions");
  55. }
  56. }
  57. }
  58. return false;
  59. }
  60. protected abstract void readImpl();
  61. @Override
  62. public void run()
  63. {
  64. try
  65. {
  66. // flood protection
  67. if (GameTimeController.getGameTicks() - getClient().packetsSentStartTick > 10)
  68. {
  69. getClient().packetsSentStartTick = GameTimeController.getGameTicks();
  70. getClient().packetsSentInSec = 0;
  71. }
  72. else
  73. {
  74. getClient().packetsSentInSec++;
  75. if (getClient().packetsSentInSec > 12)
  76. {
  77. if (getClient().packetsSentInSec < 100)
  78. sendPacket(ActionFailed.STATIC_PACKET);
  79. return;
  80. }
  81. }
  82. runImpl();
  83. /* Removes onspawn protection - player has faster computer than average
  84. *
  85. * True for these packets:
  86. * AttackRequest
  87. * MoveBackwardToLocation
  88. * RequestActionUse
  89. * RequestMagicSkillUse
  90. *
  91. * it could include pickup and talk too, but less is better
  92. */
  93. if (triggersOnActionRequest() && getClient().getActiveChar() != null)
  94. getClient().getActiveChar().onActionRequest();
  95. cleanUp();
  96. }
  97. catch (Throwable t)
  98. {
  99. _log.log(Level.SEVERE, "Client: " + getClient().toString() + " - Failed running: " + getType() + " - L2J Server Version: " + Config.SERVER_VERSION + " - DP Revision: " + Config.DATAPACK_VERSION + " ; " + t.getMessage(), t);
  100. // in case of EnterWorld error kick player from game
  101. if (this instanceof EnterWorld)
  102. getClient().closeNow();
  103. }
  104. }
  105. protected abstract void runImpl();
  106. protected final void sendPacket(L2GameServerPacket gsp)
  107. {
  108. getClient().sendPacket(gsp);
  109. }
  110. /**
  111. * @return A String with this packet name for debuging purposes
  112. */
  113. public abstract String getType();
  114. /**
  115. * Overriden with true value on some packets that should disable spawn protection
  116. */
  117. protected boolean triggersOnActionRequest()
  118. {
  119. return false;
  120. }
  121. protected void cleanUp()
  122. {}
  123. }