1. package net.xdclass.util;
    2. import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
    3. import io.netty.buffer.ByteBuf;
    4. import io.netty.buffer.Unpooled;
    5. import io.netty.channel.ChannelHandlerContext;
    6. import io.netty.util.CharsetUtil;
    7. import net.xdclass.config.FrameConfig;
    8. import java.util.Arrays;
    9. import java.util.List;
    10. public class Test {
    11. public static void testdecode(ByteBuf in) throws Exception {
    12. System.out.println("进入编码器");
    13. while (true) {
    14. System.out.println("一开始得长度" + in.readableBytes());
    15. if (in.readableBytes() == 0) {
    16. break;
    17. }
    18. int begin_index = in.readerIndex();
    19. //记录一开始得读标识
    20. in.markReaderIndex();//最开始得地方
    21. byte a = in.readByte();
    22. byte[] markbyte = new byte[19];
    23. //读到&=开始校验,只有读到头部才会进行校验
    24. if (a == 36) {
    25. System.out.println("读到$符号,进行头尾部验证");
    26. //先看看长度满不满足要求
    27. if (in.readableBytes() < 19) {
    28. System.out.println("【半包】长度不够,复原至此次原始buf头部,等待后部接收");
    29. in.resetReaderIndex();
    30. break;
    31. } else {
    32. //满足了则看看这个$是否是头部
    33. markbyte[0] = a;
    34. markbyte[1] = in.readByte();
    35. for (int i = 0; i < 17; i++) {
    36. markbyte[i + 2] = in.readByte();
    37. }
    38. if (isHeadOrTail(markbyte).equals("head")) {
    39. System.out.println("返回得in" + in.readableBytes());
    40. System.out.println("检测到头部标识");
    41. //此处进行msg读取
    42. byte[] msg = new byte[100];
    43. int index = 0;
    44. int msg_length=0;
    45. while (true) {
    46. byte b = in.readByte();
    47. //看看是不是$符号
    48. if (b == 36) {
    49. System.out.println("检测到第二个符号");
    50. byte[] tail = new byte[17];
    51. tail[0]=b;
    52. for (int i = 1; i < 17; i++) {
    53. tail[i] = in.readByte();
    54. }
    55. if (isHeadOrTail(tail).equals("tail")){
    56. System.out.println("尾部标识");
    57. }
    58. break;
    59. }
    60. msg[index] = b;
    61. msg_length++;
    62. index++;
    63. }
    64. System.out.println(Arrays.toString(msg));
    65. } else {
    66. System.out.println("不是头部标识,");
    67. in.resetReaderIndex();
    68. //不是头部,因为长度满足且不是头部则这个$符号肯定不是头部。复原至$后面一个字节等待后续读
    69. //前面得逻辑正确得话,大概率是误码了
    70. }
    71. }
    72. }
    73. }
    74. }
    75. public static String isHeadOrTail(byte[] markbyte) {
    76. // System.out.println("判断方法里得in" + in.readableBytes());
    77. System.out.println(Arrays.toString(markbyte));
    78. boolean head = true;
    79. boolean tail = true;
    80. if (!Arrays.equals(markbyte, FrameConfig.head_strb)) {
    81. head = false;
    82. }
    83. if (!Arrays.equals(markbyte, FrameConfig.tail_strb)) {
    84. tail = false;
    85. }
    86. if (head) {
    87. return "head";
    88. } else if (tail) {
    89. return "tail";
    90. } else {
    91. return "no";
    92. }
    93. }
    94. public static void main(String[] args) throws Exception {
    95. ByteBuf byteBuf = Unpooled.copiedBuffer("$===+++start+++===$\" + msg + \"$===+++end+++===$", CharsetUtil.UTF_8);
    96. ByteBuf byteBuf2 = Unpooled.copiedBuffer("$===+++start+++===$", CharsetUtil.UTF_8);
    97. // System.out.println(byteBuf2.readableBytes());
    98. String tail = "$===+++end+++===$";
    99. System.out.println(Arrays.toString(tail.getBytes()));
    100. System.out.println(Arrays.toString("$===+++start+++===$\" + msg + \"$===+++end+++===$".getBytes()));
    101. System.out.println(Arrays.toString(FrameConfig.head_strb));
    102. testdecode(byteBuf);
    103. }
    104. }