package com.mogo.telematic;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class MogoMessageEncoder extends MessageToByteEncoder<MogoProtocolMsg> {

    @Override
    protected void encode(ChannelHandlerContext ctx, MogoProtocolMsg msg, ByteBuf out) throws Exception {
        int protocolType = msg.getProtocolType();
        int length = msg.getBodyLength();
        byte[] body = msg.getBody();
        out.writeInt(protocolType);
        out.writeInt(length);
        if (length > 0) {
            out.writeBytes(body);
        }
    }
}