package com.snowcattle.game.service.net.message.encoder;
import com.snowcattle.game.service.net.message.AbstractNetMessage;
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 jwp on 2017/1/24.
* 网络消息编码
*/
public class NetMessageEncoderFactory implements INetMessageEncoderFactory{
public ByteBuf createByteBuf(AbstractNetMessage abstractNetMessage){
ByteBuf byteBuf = Unpooled.buffer(256);
//编写head
NetMessageHead netMessageHead = abstractNetMessage.getNetMessageHead();
byteBuf.writeShort(netMessageHead.getHead());
//长度
byteBuf.writeInt(0);
byteBuf.writeByte(netMessageHead.getVersion());
byteBuf.writeShort(netMessageHead.getCmd());
byteBuf.writeInt(netMessageHead.getSerial());
//编写body
NetMessageBody netMessageBody = abstractNetMessage.getNetMessageBody();
byteBuf.writeBytes(netMessageBody.getBytes());
//重新设置长度
// int skip = (2+4);
int skip = 6;
int length = byteBuf.readableBytes() - skip;
byteBuf.setInt(2, length);
byteBuf.slice();
return byteBuf;
}
}