L2GameClientPacket.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.Logger;
  18. import org.mmocore.network.ReceivablePacket;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.GameTimeController;
  21. import com.l2jserver.gameserver.network.L2GameClient;
  22. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  23. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  24. /**
  25. * Packets received by the game server from clients
  26. * @author KenM
  27. */
  28. public abstract class L2GameClientPacket extends ReceivablePacket<L2GameClient>
  29. {
  30. private static final Logger _log = Logger.getLogger(L2GameClientPacket.class.getName());
  31. @Override
  32. protected boolean read()
  33. {
  34. //_log.info(this.getType());
  35. try
  36. {
  37. readImpl();
  38. return true;
  39. }
  40. catch (Exception e)
  41. {
  42. _log.severe("Client: "+getClient().toString()+" - Failed reading: "+getType()+" - L2J Server Version: "+Config.SERVER_VERSION+" - DP Revision: "+Config.DATAPACK_VERSION);
  43. e.printStackTrace();
  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.severe("Client: "+getClient().toString()+" - Failed running: "+getType()+" - L2J Server Version: "+Config.SERVER_VERSION+" - DP Revision: "+Config.DATAPACK_VERSION);
  100. t.printStackTrace();
  101. // in case of EnterWorld error kick player from game
  102. if (this instanceof EnterWorld)
  103. getClient().closeNow();
  104. }
  105. }
  106. protected abstract void runImpl();
  107. protected final void sendPacket(L2GameServerPacket gsp)
  108. {
  109. getClient().sendPacket(gsp);
  110. }
  111. /**
  112. * @return A String with this packet name for debuging purposes
  113. */
  114. public abstract String getType();
  115. /**
  116. * Overriden with true value on some packets that should disable spawn protection
  117. */
  118. protected boolean triggersOnActionRequest()
  119. {
  120. return false;
  121. }
  122. protected void cleanUp()
  123. {}
  124. }