package com.snowcattle.game.service.net.message.encoder;
import com.snowcattle.game.service.net.message.AbstractNetProtoBufMessage;
import com.snowcattle.game.service.net.message.NetMessageBody;
import com.snowcattle.game.service.net.message.NetMessageHead;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
/**
* Created by jiangwenping on 17/2/8.
*/
public class NetProtoBufTcpMessageEncoderFactory implements INetProtoBufTcpMessageEncoderFactory {
@Override
public ByteBuf createByteBuf(AbstractNetProtoBufMessage netMessage) throws Exception {
ByteBuf byteBuf = Unpooled.buffer(256);
//编写head
NetMessageHead netMessageHead = netMessage.getNetMessageHead();
byteBuf.writeShort(netMessageHead.getHead());
//长度
byteBuf.writeInt(0);
//设置内容
byteBuf.writeByte(netMessageHead.getVersion());
byteBuf.writeShort(netMessageHead.getCmd());
byteBuf.writeInt(netMessageHead.getSerial());
//编写body
netMessage.encodeNetProtoBufMessageBody();
NetMessageBody netMessageBody = netMessage.getNetMessageBody();
byteBuf.writeBytes(netMessageBody.getBytes());
//重新设置长度
int skip = 6;
int length = byteBuf.readableBytes() - skip;
byteBuf.setInt(2, length);
byteBuf.slice();
return byteBuf;
}
}