(ByteBuf byteBuf)
| 76 | } |
| 77 | |
| 78 | public Packet decode(ByteBuf byteBuf) { |
| 79 | //跳过magic number |
| 80 | byteBuf.skipBytes(4); |
| 81 | |
| 82 | //跳过版本号 |
| 83 | byteBuf.skipBytes(1); |
| 84 | |
| 85 | //序列化算法标识 |
| 86 | byte serializeAlgorithm = byteBuf.readByte(); |
| 87 | |
| 88 | //指令 |
| 89 | byte command = byteBuf.readByte(); |
| 90 | |
| 91 | //数据包长度 |
| 92 | int length =byteBuf.readInt(); |
| 93 | |
| 94 | byte[] bytes = new byte[length]; |
| 95 | byteBuf.readBytes(bytes); |
| 96 | Class<? extends Packet> requestType =getRequestType(command); |
| 97 | Serializer serializer = getSerializer(serializeAlgorithm); |
| 98 | |
| 99 | if(requestType != null && serializer != null) { |
| 100 | //解码 |
| 101 | return serializer.deserialize(requestType, bytes); |
| 102 | } else { |
| 103 | System.out.println("指令或者序列化算法不存在!"); |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | private Class<? extends Packet> getRequestType(byte command) { |
| 109 | return packetTypeMap.get(command); |
nothing calls this directly
no test coverage detected