L2GameClientPacket.java 4.2 KB

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