2
0

GameServerBasePacket.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.gameserverpackets;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import com.l2jserver.gameserver.TaskPriority;
  19. /**
  20. * @author -Wooden-
  21. *
  22. */
  23. public abstract class GameServerBasePacket
  24. {
  25. private ByteArrayOutputStream _bao;
  26. protected GameServerBasePacket()
  27. {
  28. _bao = new ByteArrayOutputStream();
  29. }
  30. protected void writeD(int value)
  31. {
  32. _bao.write(value &0xff);
  33. _bao.write(value >> 8 &0xff);
  34. _bao.write(value >> 16 &0xff);
  35. _bao.write(value >> 24 &0xff);
  36. }
  37. protected void writeH(int value)
  38. {
  39. _bao.write(value &0xff);
  40. _bao.write(value >> 8 &0xff);
  41. }
  42. protected void writeC(int value)
  43. {
  44. _bao.write(value &0xff);
  45. }
  46. protected void writeF(double org)
  47. {
  48. long value = Double.doubleToRawLongBits(org);
  49. _bao.write((int)(value &0xff));
  50. _bao.write((int)(value >> 8 &0xff));
  51. _bao.write((int)(value >> 16 &0xff));
  52. _bao.write((int)(value >> 24 &0xff));
  53. _bao.write((int)(value >> 32 &0xff));
  54. _bao.write((int)(value >> 40 &0xff));
  55. _bao.write((int)(value >> 48 &0xff));
  56. _bao.write((int)(value >> 56 &0xff));
  57. }
  58. protected void writeS(String text)
  59. {
  60. try
  61. {
  62. if (text != null)
  63. {
  64. _bao.write(text.getBytes("UTF-16LE"));
  65. }
  66. }
  67. catch (Exception e)
  68. {
  69. e.printStackTrace();
  70. }
  71. _bao.write(0);
  72. _bao.write(0);
  73. }
  74. protected void writeB(byte[] array)
  75. {
  76. try
  77. {
  78. _bao.write(array);
  79. }
  80. catch (IOException e)
  81. {
  82. // TODO Auto-generated catch block
  83. e.printStackTrace();
  84. }
  85. }
  86. public int getLength()
  87. {
  88. return _bao.size()+2;
  89. }
  90. public byte[] getBytes()
  91. {
  92. writeD(0x00); // reserve for checksum
  93. int padding = _bao.size() % 8;
  94. if (padding != 0)
  95. {
  96. for (int i = padding; i<8;i++)
  97. {
  98. writeC(0x00);
  99. }
  100. }
  101. return _bao.toByteArray();
  102. }
  103. public TaskPriority getPriority() { return TaskPriority.PR_HIGH; }
  104. public abstract byte[] getContent() throws IOException;
  105. }