Skip to content

Commit

Permalink
protocol add serializerType
Browse files Browse the repository at this point in the history
  • Loading branch information
iamlinhui committed Jun 4, 2022
1 parent bdbe17e commit 5f87f6d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,31 @@

import java.util.List;


/**
* messageLength|messageType|serializerType|metaLength|meta|data
*/
public class MessageDecoder extends MessageToMessageDecoder<ByteBuf> {

private static final SerializerDispatcher SERIALIZER_DISPATCHER = new SerializerDispatcher();

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
Message proxyMessage = new Message();
Message message = new Message();
// 4个字节
proxyMessage.setType(MessageType.getInstance(byteBuf.readInt()));
message.setType(MessageType.getInstance(byteBuf.readInt()));
SerializationType serialization = SerializationType.getInstance(byteBuf.readInt());

int protobufLength = byteBuf.readInt();
if (protobufLength > 0) {
byte[] metaByte = new byte[protobufLength];
int metaByteLength = byteBuf.readInt();
if (metaByteLength > 0) {
byte[] metaByte = new byte[metaByteLength];
byteBuf.readBytes(metaByte);
Meta meta = SERIALIZER_DISPATCHER.deserialize(SerializationType.PROTOSTUFF, metaByte, Meta.class);
proxyMessage.setMeta(meta);
Meta meta = SERIALIZER_DISPATCHER.deserialize(serialization, metaByte, Meta.class);
message.setMeta(meta);
}
if (byteBuf.isReadable()) {
byte[] data = ByteBufUtil.getBytes(byteBuf);
proxyMessage.setData(data);
message.setData(data);
}
list.add(proxyMessage);
list.add(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import cn.promptness.rpt.base.protocol.MessageType;
import cn.promptness.rpt.base.protocol.Meta;
import cn.promptness.rpt.base.serialize.SerializerDispatcher;
import cn.promptness.rpt.base.serialize.api.SerializationType;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.internal.EmptyArrays;


/**
* messageLength|messageType|serializerType|metaLength|meta|data
*/
public class MessageEncoder extends MessageToByteEncoder<Message> {

private static final SerializerDispatcher SERIALIZER_DISPATCHER = new SerializerDispatcher();
Expand All @@ -19,11 +20,12 @@ public class MessageEncoder extends MessageToByteEncoder<Message> {
protected void encode(ChannelHandlerContext channelHandlerContext, Message message, ByteBuf out) throws Exception {
MessageType type = message.getType();
out.writeInt(type.getCode());
out.writeInt(message.getSerialization().getCode());

Meta meta = message.getMeta();
byte[] protobuf = meta == null ? EmptyArrays.EMPTY_BYTES : SERIALIZER_DISPATCHER.serialize(SerializationType.PROTOSTUFF, meta);
out.writeInt(protobuf.length);
out.writeBytes(protobuf);
byte[] metaByte = meta == null ? EmptyArrays.EMPTY_BYTES : SERIALIZER_DISPATCHER.serialize(message.getSerialization(), meta);
out.writeInt(metaByte.length);
out.writeBytes(metaByte);

if (message.getData() != null && message.getData().length > 0) {
out.writeBytes(message.getData());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.promptness.rpt.base.protocol;

import cn.promptness.rpt.base.serialize.api.SerializationType;

/**
* 客户端-服务器自定义通信协议
*/
Expand All @@ -10,6 +12,8 @@ public class Message {
*/
private MessageType type;

private SerializationType serialization = SerializationType.PROTOSTUFF;

/**
* 元数据
*/
Expand Down Expand Up @@ -54,4 +58,11 @@ public void setType(MessageType type) {
this.type = type;
}

public SerializationType getSerialization() {
return serialization;
}

public void setSerialization(SerializationType serialization) {
this.serialization = serialization;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ public int getCode() {
public String getDesc() {
return desc;
}

public static SerializationType getInstance(int code) {
for (SerializationType value : SerializationType.values()) {
if (value.code == code) {
return value;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void execute(ChannelHandlerContext context, Message message) throws Excep
channelFuture.addListener(ChannelFutureListener.CLOSE);
return;
}
logger.info("授权注册成功,客户端使用的秘钥:{}", meta.getClientKey());
ServerChannelCache.getServerChannelMap().put(context.channel().id().asLongText(), context.channel());
context.channel().attr(Constants.CHANNELS).set(new ConcurrentHashMap<>(1024));
context.channel().attr(Constants.Server.DOMAIN).set(domainList);
Expand Down

0 comments on commit 5f87f6d

Please sign in to comment.