package com.pwt.netty.handler.codec.vehicle2;

import com.pwt.netty.common.vehicle2.Vehicle2Message;
import com.pwt.netty.handler.codec.exceptions.ProtocolException;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class Vehicle2ProtocolEncoder extends MessageToByteEncoder<Vehicle2Message> {

    @Override
    protected void encode(ChannelHandlerContext ctx, Vehicle2Message msg, ByteBuf out) throws Exception {
        // 使用小端格式
        msg.encode(out);
        if(out.writerIndex() != msg.getProtocolLength()) {
            throw new ProtocolException("encode frame error: length value not equals actual data length");
        }
    }
}
