跳至主要內容

扁平化写法示例

hylexus大约 1 分钟示例编码解码

扁平化写法示例

提示

  • 在阅读本文之前,建议先阅读 协议格式说明
  • 所谓的 扁平化写法 就是不用单独封装 HeaderBody,而是直接将 HeaderBody 的数据写在同一个实体类中

解码

实体类定义

Rust 命名风格
@Setter
@Getter
@ToString
public class RustStyleDebugEntity01ForDecode {
    //////////////////////// header
    // 固定为 0x80901234
    @Preset.RustStyle.i32
    private int magicNumber;

    // 主版本号 无符号数 1字节
    @Preset.RustStyle.u8
    private short majorVersion;

    // 次版本号 无符号数 1字节
    @Preset.RustStyle.u8
    private short minorVersion;

    // 消息类型 无符号数 2字节
    @Preset.RustStyle.u16
    private int msgType;

    // 消息体长度 无符号数 2字节
    @Preset.RustStyle.u16
    private int msgBodyLength;

    //////////////////////// body
    // 下一个字段(username)长度 无符号数 2字节
    @Preset.RustStyle.u16
    private int usernameLength;

    // 用户名 String, "UTF-8"
    @Preset.RustStyle.str(lengthExpression = "getUsernameLength()")
    private String username;

    // 下一个字段(password)长度 无符号数 2字节
    @Preset.RustStyle.u16
    private int passwordLength;

    // 密码 String, "GBK"
    @Preset.RustStyle.str(charset = "GBK", lengthExpression = "getPasswordLength()")
    private String password;

    // 生日 String[8], "yyyyMMdd", "UTF-8" 8字节
    @Preset.RustStyle.str(length = 8)
    private String birthday;

    // 手机号 BCD_8421[6] 6字节(12个数字)
    @Preset.RustStyle.str(charset = "bcd_8421", length = 6)
    private String phoneNumber;

    // 年龄 无符号数 2字节
    @Preset.RustStyle.u16
    private int age;

    // 状态 有符号数 2字节
    @Preset.RustStyle.i16
    private short status;
}

反序列化

public class EntityDecodeTest {

    @Test
    void testDecode() {
        final EntityCodec entityCodec = new EntityCodec();

        // buffer 中存储的是要反序列化的数据(这里写死用来演示)
        final String hexString = "8090123401020001003d001678747265616d2d636f6465632ee794a8e688b7e5908d001178747265616d2d636f6465632ec3dcc2eb3230323130323033013911112222270fff9c";
        final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer().writeBytes(XtreamBytes.decodeHex(hexString));

        try {
            final RustStyleDebugEntity01ForDecode entity = entityCodec.decode(RustStyleDebugEntity01ForDecode.class, buffer);
            System.out.println(entity);
        } finally {
            buffer.release();
        }
    }
}











 


 



编码

实体类定义

Rust 命名风格
@Setter
@Getter
@ToString
public class RustStyleDebugEntity01ForEncode {
    //////////////////////// header
    // 固定为 0x80901234
    @Preset.RustStyle.i32
    private int magicNumber = 0x80901234;

    // 主版本号 无符号数 1字节
    @Preset.RustStyle.u8
    private short majorVersion;
    // 次版本号 无符号数 1字节

    @Preset.RustStyle.u8
    private short minorVersion;

    // 消息类型 无符号数 2字节
    @Preset.RustStyle.u16
    private int msgType;

    // 消息体长度 无符号数 2字节
    @Preset.RustStyle.u16
    private int msgBodyLength;

    //////////////////////// body
    // 下一个字段长度 无符号数 2字节
    @Preset.RustStyle.u16
    private int usernameLength;

    // 用户名 String, "UTF-8"
    @Preset.RustStyle.str
    private String username;

    // 下一个字段长度 无符号数 2字节
    @Preset.RustStyle.u16
    private int passwordLength;

    // 密码 String, "GBK"
    @Preset.RustStyle.str(charset = "GBK")
    private String password;

    // 生日 String[8], "yyyyMMdd", "UTF-8"
    @Preset.RustStyle.str
    private String birthday;

    // 手机号 BCD_8421[6] "GBK"
    @Preset.RustStyle.str(charset = "bcd_8421")
    private String phoneNumber;

    // 年龄 无符号数 2字节
    @Preset.RustStyle.u16
    private int age;

    // 状态 有符号数 2字节
    @Preset.RustStyle.i16
    private short status;
}

序列化

public class EntityEncodeTest {

    @Test
    void testEncode() {
        final EntityCodec entityCodec = new EntityCodec();

        final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        try {
            final RustStyleDebugEntity01ForEncode instance = new RustStyleDebugEntity01ForEncode();
            // 省略属性赋值
            // instance.setXxx(someValue);
            // ...
            instance.setMajorVersion((short) 1);

            // 将 instance 的数据序列化到 buffer 中
            entityCodec.encode(instance, buffer);
            // 使用 buffer
            System.out.println(ByteBufUtil.hexDump(buffer));
        } finally {
            buffer.release();
        }
    }
}