mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-22 20:15:06 +08:00
rpc
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.jsowell.netty.rpc;
|
||||
|
||||
import com.jsowell.common.util.bean.SerializationUtil;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
public class MessageEncode extends MessageToByteEncoder<Message> {
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext channelHandlerContext, Message message, ByteBuf byteBuf) throws Exception {
|
||||
// 将对象进行序列化
|
||||
byte[] data = SerializationUtil.serialize(message);
|
||||
|
||||
// 写数据长度,前4个字节用于记录数据总长度(对象 + 类型(1个字节))
|
||||
byteBuf.writeInt(data.length + 1);
|
||||
// 写记录消息类型,用于反序列选择类的类型
|
||||
byteBuf.writeByte(message.getMessageType());
|
||||
// 写对象
|
||||
byteBuf.writeBytes(data);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user