MyProtocolDecoder.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.js.kbt.socket;
  2. import java.nio.ByteOrder;
  3. import java.nio.charset.Charset;
  4. import io.netty.buffer.ByteBuf;
  5. import io.netty.channel.ChannelHandlerContext;
  6. import io.netty.handler.codec.DecoderException;
  7. import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
  8. public class MyProtocolDecoder extends LengthFieldBasedFrameDecoder {
  9. private static final String HEXES = "0123456789ABCDEF";
  10. private static final int HEADER_SIZE = 4;
  11. /**
  12. *
  13. * @param maxFrameLength 帧的最大长度
  14. * @param lengthFieldOffset length字段偏移的地址
  15. * @param lengthFieldLength length字段所占的字节长
  16. * @param lengthAdjustment 修改帧数据长度字段中定义的值,可以为负数 因为有时候我们习惯把头部记入长度,若为负数,则说明要推后多少个字段
  17. * @param initialBytesToStrip 解析时候跳过多少个长度
  18. * @param failFast 为true,当frame长度超过maxFrameLength时立即报TooLongFrameException异常,为false,读取完整个帧再报异
  19. */
  20. public MyProtocolDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
  21. super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
  22. }
  23. protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) {
  24. buf = buf.order(order);
  25. long frameLength;
  26. byte b1 = buf.getByte(offset);
  27. byte b2 = buf.getByte(offset + 1);
  28. StringBuilder hex = new StringBuilder();
  29. String s1 = hex.append(HEXES.charAt((b2 & 0xF0) >> 4)).append(HEXES.charAt((b2 & 0x0F)))
  30. .append(HEXES.charAt((b1 & 0xF0) >> 4)).append(HEXES.charAt((b1 & 0x0F))).toString();
  31. frameLength = Integer.parseInt(s1, 16) - 4;//去掉头部的FAAF0000四位
  32. System.out.println("getUnadjustedFrameLength解析长度:"+frameLength);
  33. return frameLength;
  34. }
  35. @Override
  36. protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
  37. System.out.println("解析长度decode:");
  38. //在这里调用父类的方法,实现指得到想要的部分,我在这里全部都要,也可以只要body部分
  39. in = (ByteBuf) super.decode(ctx,in);
  40. if(in == null){
  41. return null;
  42. }
  43. if(in.readableBytes()<HEADER_SIZE){
  44. throw new Exception("字节数不足");
  45. }
  46. // byte fa = in.readByte();
  47. // byte af = in.readByte();
  48. // byte b1 = in.readByte();
  49. // byte b2 = in.readByte();
  50. // //读取length字段
  51. // StringBuilder hex = new StringBuilder();
  52. // String s1 = hex.append(HEXES.charAt((b2 & 0xF0) >> 4)).append(HEXES.charAt((b2 & 0x0F)))
  53. // .append(HEXES.charAt((b1 & 0xF0) >> 4)).append(HEXES.charAt((b1 & 0x0F))).toString();
  54. //
  55. // int length = Integer.parseInt(s1, 16);
  56. // System.out.println("解析长度:"+length);
  57. // if(in.readableBytes()!=length){
  58. // throw new Exception("标记的长度不符合实际长度");
  59. // }
  60. //读取body
  61. byte []bytes = new byte[in.readableBytes()];
  62. in.readBytes(bytes);
  63. return bytes;
  64. }
  65. }