add protobuf serializer, spi extension
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-02-09 17:36:11 +08:00
parent 9d72433199
commit 5db4dbeba5
37 changed files with 1123 additions and 343 deletions

View File

@@ -45,6 +45,7 @@ public class Constants {
* 编码解码 byte 数组固定长度
*/
public static int DECODER_FRAMELENGTH = 100;
private Constants() {
}

View File

@@ -5,7 +5,6 @@ import io.netty.channel.ChannelFutureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.SocketAddress;
/**
@@ -40,31 +39,4 @@ public class NettyUtil {
logger.info("closeChannel: close the connection to remote address[{}] result: {}",
addrRemote, future.isSuccess()));
}
public static byte[] toBytes(Object obj) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
// if (bytes.length > Constants.DECODER_FRAMELENGTH) {
// logger.error("bytes length should not bigger than {}", Constants.DECODER_FRAMELENGTH);
// return null;
// } else if (bytes.length < Constants.DECODER_FRAMELENGTH) {
// byte[] result = new byte[Constants.DECODER_FRAMELENGTH];
//
// // 如果长度不足,填充
// System.arraycopy(bytes, 0, result, 0, bytes.length);
// return result;
// }
return bos.toByteArray();
}
public static Object toObject(byte[] bts) throws IOException, ClassNotFoundException {
ByteArrayInputStream bis = new ByteArrayInputStream(bts);
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}
}

View File

@@ -1,83 +0,0 @@
package io.github.ehlxr.did.common;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author ehlxr
*/
public class SdkProto implements Serializable {
private static final AtomicInteger REQUEST_ID = new AtomicInteger(0);
private static final long serialVersionUID = 8986093068588745760L;
/**
* 请求的ID
*/
private int rqid;
/**
* 全局的 ID
*/
private long did;
public SdkProto() {
rqid = REQUEST_ID.incrementAndGet();
did = 0;
}
public SdkProto(int rqid, long did) {
this.rqid = rqid;
this.did = did;
}
public static SdkProtoBuilder newBuilder() {
return new SdkProtoBuilder();
}
public int getRqid() {
return rqid;
}
public void setRqid(int rqid) {
if (rqid > 0) {
this.rqid = rqid;
}
}
public long getDid() {
return did;
}
public void setDid(long did) {
this.did = did;
}
@Override
public String toString() {
return JsonUtils.obj2String(this);
}
public static final class SdkProtoBuilder {
private int rqid;
private long did;
private SdkProtoBuilder() {
}
public SdkProtoBuilder rqid(int rqid) {
this.rqid = rqid;
return this;
}
public SdkProtoBuilder did(long did) {
this.did = did;
return this;
}
public SdkProto build() {
SdkProto sdkProto = new SdkProto();
sdkProto.setRqid(rqid);
sdkProto.setDid(did);
return sdkProto;
}
}
}

View File

@@ -1,95 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.did.netty;
import io.github.ehlxr.did.common.NettyUtil;
import io.github.ehlxr.did.common.Try;
/**
* @author ehlxr
* @since 2021-02-08 22:07.
*/
public class MyProtocolBean {
// 类型(系统编号 0xA 表示A系统0xB 表示B系统
private byte type;
// 信息标志 0xA 表示心跳包 0xB 表示超时包 0xC 业务信息包
private byte flag;
// 内容长度
private int length;
// 内容
private byte[] content;
public MyProtocolBean(byte type, byte flag, int length, byte[] content) {
this.type = type;
this.flag = flag;
this.length = length;
this.content = content;
}
public byte getType() {
return type;
}
public void setType(byte type) {
this.type = type;
}
public byte getFlag() {
return flag;
}
public void setFlag(byte flag) {
this.flag = flag;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
@Override
public String toString() {
return "MyProtocolBean{" +
"type=" + type +
", flag=" + flag +
", length=" + length +
", content=" + Try.of(NettyUtil::toObject).apply(content).get() +
'}';
}
}

View File

@@ -1,79 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.did.netty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
/**
* @author ehlxr
* @since 2021-02-08 22:09.
*/
public class MyProtocolDecoder extends LengthFieldBasedFrameDecoder {
private static final int HEADER_SIZE = 6;
/**
* @param maxFrameLength 帧的最大长度
* @param lengthFieldOffset length字段偏移的地址
* @param lengthFieldLength length字段所占的字节长
* @param lengthAdjustment 修改帧数据长度字段中定义的值,可以为负数 因为有时候我们习惯把头部记入长度,若为负数,则说明要推后多少个字段
* @param initialBytesToStrip 解析时候跳过多少个长度
* @param failFast 为true当frame长度超过maxFrameLength时立即报TooLongFrameException异常为false读取完整个帧再报异
*/
public MyProtocolDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength,
int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
//在这里调用父类的方法,实现指得到想要的部分,我在这里全部都要,也可以只要body部分
in = (ByteBuf) super.decode(ctx, in);
if (in == null) {
return null;
}
if (in.readableBytes() < HEADER_SIZE) {
throw new Exception("字节数不足");
}
//读取type字段
byte type = in.readByte();
//读取flag字段
byte flag = in.readByte();
//读取length字段
int length = in.readInt();
if (in.readableBytes() != length) {
throw new Exception("标记的长度不符合实际长度");
}
//读取body
byte[] bytes = new byte[in.readableBytes()];
in.readBytes(bytes);
return new MyProtocolBean(type, flag, length, bytes);
}
}

View File

@@ -1,48 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.did.netty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
/**
* @author ehlxr
* @since 2021-02-08 22:12.
*/
public class MyProtocolEncoder extends MessageToByteEncoder<MyProtocolBean> {
@Override
protected void encode(ChannelHandlerContext ctx, MyProtocolBean msg, ByteBuf out) throws Exception {
if (msg == null) {
throw new Exception("msg is null");
}
out.writeByte(msg.getType());
out.writeByte(msg.getFlag());
out.writeInt(msg.getLength());
out.writeBytes(msg.getContent());
}
}