mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-21 11:35:12 +08:00
update 电单车协议
This commit is contained in:
@@ -10,8 +10,8 @@ public class ClientChannelInitializer extends ChannelInitializer<SocketChannel>
|
||||
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
||||
ChannelPipeline pipeline = socketChannel.pipeline();
|
||||
|
||||
pipeline.addLast(new MessageEncode());
|
||||
pipeline.addLast(new MessageDecode());
|
||||
// pipeline.addLast(new MessageEncode());
|
||||
// pipeline.addLast(new MessageDecode());
|
||||
|
||||
pipeline.addLast(new RpcResponseHandler());
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.jsowell.pile.rpc;
|
||||
|
||||
import com.jsowell.common.protocol.Message;
|
||||
import com.jsowell.common.protocol.MessageConstant;
|
||||
import com.jsowell.common.util.bean.SerializationUtil;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MessageDecode extends ByteToMessageDecoder {
|
||||
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
|
||||
|
||||
// 由于数据包的前4个字节用于记录总数据大小,如果数据不够4个字节,不进行读
|
||||
if(byteBuf.readableBytes() < 4) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 标记开始读的位置
|
||||
byteBuf.markReaderIndex();
|
||||
|
||||
// 前四个字节记录了数据大小
|
||||
int dataSize = byteBuf.readInt();
|
||||
|
||||
// 查看剩余可读字节是否足够,如果不是,重置读取位置,等待下一次解析
|
||||
if(byteBuf.readableBytes() < dataSize) {
|
||||
byteBuf.resetReaderIndex();
|
||||
return;
|
||||
}
|
||||
|
||||
// 读取消息类型
|
||||
byte messageType = byteBuf.readByte();
|
||||
// 读取数据, 数组大小需要剔除1个字节的消息类型
|
||||
byte[] data = new byte[dataSize -1];
|
||||
|
||||
byteBuf.readBytes(data);
|
||||
|
||||
Message message = SerializationUtil.deserialize(MessageConstant.getMessageClass(messageType), data);
|
||||
|
||||
list.add(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.jsowell.pile.rpc;
|
||||
|
||||
import com.jsowell.common.protocol.Message;
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,8 +10,8 @@ public class ServerChannelInitializer extends ChannelInitializer<SocketChannel>
|
||||
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
||||
ChannelPipeline pipeline = socketChannel.pipeline();
|
||||
|
||||
pipeline.addLast(new MessageEncode());
|
||||
pipeline.addLast(new MessageDecode());
|
||||
// pipeline.addLast(new MessageEncode());
|
||||
// pipeline.addLast(new MessageDecode());
|
||||
|
||||
pipeline.addLast(new RpcRequestHandler());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user