del[net]: remove jprotobuf

This commit is contained in:
godotg
2023-12-10 23:13:41 +08:00
parent d0e83c6e1e
commit f1cb975715
28 changed files with 12 additions and 783 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
### protocol
- 考虑用javassist的方式支持protobuf放弃使用jprotobuf(可以省略很多不必要的注解,代码更加简洁),用约定大于配置的方式,还可以原生的和pojo对象结合起来
- 考虑用javassist的方式支持protobuf,用约定大于配置的方式,还可以原生的和pojo对象结合起来
- 通过protobuf的proto文件生成java的pojo源代码,通过动态编译java源代码的方式生成其它语言的源代码
-12
View File
@@ -30,7 +30,6 @@
<!-- Office document parsing -->
<poi.version>5.2.3</poi.version>
<csv.version>1.10.0</csv.version>
<jprotobuf.version>2.4.19</jprotobuf.version>
<!-- maven core plugin -->
<maven-clean-plugin.version>3.3.1</maven-clean-plugin.version>
<maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
@@ -140,17 +139,6 @@
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>jprotobuf</artifactId>
<version>${jprotobuf.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -1,48 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*/
package com.zfoo.net.core.jprotobuf;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.GatewayRouteHandler;
import com.zfoo.net.handler.codec.jprotobuf.JProtobufTcpCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.net.session.Session;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.lang.Nullable;
import java.util.function.BiFunction;
/**
* @author godotg
*/
public class JProtobufGatewayServer extends AbstractServer<SocketChannel> {
private final BiFunction<Session, Object, Boolean> packetFilter;
public JProtobufGatewayServer(HostAndPort host, @Nullable BiFunction<Session, Object, Boolean> packetFilter) {
super(host);
this.packetFilter = packetFilter;
}
@Override
protected void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 180));
channel.pipeline().addLast(new ServerIdleHandler());
channel.pipeline().addLast(new JProtobufTcpCodecHandler());
channel.pipeline().addLast(new GatewayRouteHandler(packetFilter));
}
}
@@ -1,40 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.core.jprotobuf;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.jprotobuf.JProtobufTcpCodecHandler;
import com.zfoo.net.handler.idle.ClientIdleHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
/**
* @author godotg
*/
public class JProtobufTcpClient extends AbstractClient<SocketChannel> {
public JProtobufTcpClient(HostAndPort host) {
super(host);
}
@Override
protected void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 60));
channel.pipeline().addLast(new ClientIdleHandler());
channel.pipeline().addLast(new JProtobufTcpCodecHandler());
channel.pipeline().addLast(new ClientRouteHandler());
}
}
@@ -1,40 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.core.jprotobuf;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.jprotobuf.JProtobufTcpCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
/**
* @author godotg
*/
public class JProtobufTcpServer extends AbstractServer<SocketChannel> {
public JProtobufTcpServer(HostAndPort host) {
super(host);
}
@Override
protected void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 180));
channel.pipeline().addLast(new ServerIdleHandler());
channel.pipeline().addLast(new JProtobufTcpCodecHandler());
channel.pipeline().addLast(new ServerRouteHandler());
}
}
@@ -1,97 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.handler.codec.jprotobuf;
import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.zfoo.net.packet.DecodedPacketInfo;
import com.zfoo.net.packet.EncodedPacketInfo;
import com.zfoo.net.packet.PacketService;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.buffer.ByteBufUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;
import java.io.IOException;
import java.util.List;
/**
* header(4byte) + protocolId(2byte) + packet
* header = body(bytes.length) + protocolId.length(2byte)
*
* @author godotg
*/
public class JProtobufTcpCodecHandler extends ByteToMessageCodec<EncodedPacketInfo> {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws IOException {
// 不够读一个int
if (in.readableBytes() <= PacketService.PACKET_HEAD_LENGTH) {
return;
}
in.markReaderIndex();
var length = in.readInt();
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
if (length < 0 || length > IOUtils.BYTES_PER_MB) {
throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
}
// ByteBuf里的数据太小
if (in.readableBytes() < length) {
in.resetReaderIndex();
return;
}
var sliceByteBuf = in.readSlice(length);
var packetInfo = read(sliceByteBuf);
out.add(packetInfo);
}
@Override
protected void encode(ChannelHandlerContext ctx, EncodedPacketInfo packetInfo, ByteBuf out) throws IOException {
write(out, packetInfo.getPacket(), packetInfo.getAttachment());
}
public static DecodedPacketInfo read(ByteBuf buffer) throws IOException {
var protocolId = ByteBufUtils.readShort(buffer);
var protocolRegistration = ProtocolManager.getProtocol(protocolId);
var protocolClass = protocolRegistration.protocolConstructor().getDeclaringClass();
var protobufCodec = ProtobufProxy.create(protocolClass);
var bytes = ByteBufUtils.readAllBytes(buffer);
var packet = protobufCodec.decode(bytes);
return DecodedPacketInfo.valueOf(packet, null);
}
public void write(ByteBuf buffer, Object packet, Object attachment) throws IOException {
// 写入protobuf协议
@SuppressWarnings("unchecked")
var protobufCodec = (Codec<Object>) ProtobufProxy.create(packet.getClass());
byte[] bytes = protobufCodec.encode(packet);
// header(4byte) + protocolId(2byte)
buffer.writeInt(bytes.length + 2);
var protocolId = ProtocolManager.protocolId(packet.getClass());
// 写入协议号
ByteBufUtils.writeShort(buffer, protocolId);
buffer.writeBytes(bytes);
}
}
@@ -13,8 +13,6 @@
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.anno.Protocol;
import org.slf4j.helpers.FormattingTuple;
@@ -23,15 +21,11 @@ import org.slf4j.helpers.MessageFormatter;
/**
* @author godotg
*/
@ProtobufClass
@Protocol(id = 101)
public class Error {
@Protobuf(order = 1)
private int module;
@Protobuf(order = 2)
private int errorCode;
@Protobuf(order = 3)
private String errorMessage;
@Override
@@ -13,13 +13,11 @@
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@ProtobufClass
@Protocol(id = 102)
public class Heartbeat {
@@ -13,7 +13,6 @@
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.anno.Protocol;
@@ -22,7 +21,6 @@ import com.zfoo.protocol.anno.Protocol;
*
* @author godotg
*/
@ProtobufClass
@Protocol(id = 100)
public class Message {
@@ -13,13 +13,11 @@
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@ProtobufClass
@Protocol(id = 103)
public class Ping {
@@ -13,21 +13,17 @@
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@ProtobufClass
@Protocol(id = 104)
public class Pong {
/**
* 服务器当前的时间戳
*/
@Protobuf(order = 1)
private long time;
public static Pong valueOf(long time) {
@@ -1,36 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.core.jprotobuf.client;
import com.zfoo.net.packet.jprotobuf.JProtobufHelloResponse;
import com.zfoo.net.anno.PacketReceiver;
import com.zfoo.net.session.Session;
import com.zfoo.protocol.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @author godotg
*/
@Component
public class JProtobufTcpClientController {
private static final Logger logger = LoggerFactory.getLogger(JProtobufTcpClientController.class);
@PacketReceiver
public void atJProtobufHelloResponse(Session session, JProtobufHelloResponse ask) {
logger.info("receive [packet:{}] from server", JsonUtils.object2String(ask));
}
}
@@ -1,52 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.core.jprotobuf.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.jprotobuf.JProtobufTcpClient;
import com.zfoo.net.packet.jprotobuf.JProtobufHelloRequest;
import com.zfoo.protocol.util.ThreadUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author godotg
*/
@Ignore
public class JProtobufTcpClientTest {
private static final Logger logger = LoggerFactory.getLogger(JProtobufTcpClientTest.class);
@Test
public void startClient() throws Exception {
var context = new ClassPathXmlApplicationContext("config.xml");
var client = new JProtobufTcpClient(HostAndPort.valueOf("127.0.0.1:9000"));
var session = client.start();
for (int i = 0; i < 1000; i++) {
var ask = new JProtobufHelloRequest();
ask.setMessage("Hello, this is jprotobuf client!");
NetContext.getRouter().send(session, ask);
ThreadUtils.sleep(1000);
}
ThreadUtils.sleep(Long.MAX_VALUE);
}
}
@@ -1,43 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.core.jprotobuf.server;
import com.zfoo.net.NetContext;
import com.zfoo.net.packet.jprotobuf.JProtobufHelloRequest;
import com.zfoo.net.packet.jprotobuf.JProtobufHelloResponse;
import com.zfoo.net.anno.PacketReceiver;
import com.zfoo.net.session.Session;
import com.zfoo.protocol.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @author godotg
*/
@Component
public class JProtobufTcpController {
private static final Logger logger = LoggerFactory.getLogger(JProtobufTcpController.class);
@PacketReceiver
public void atJProtobufHelloRequest(Session session, JProtobufHelloRequest ask) {
logger.info("receive [packet:{}] from client", JsonUtils.object2String(ask));
var answer = new JProtobufHelloResponse();
answer.setMessage("Hello, this is the jprotobuf tcp server!");
NetContext.getRouter().send(session, answer);
}
}
@@ -1,39 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.core.jprotobuf.server;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.jprotobuf.JProtobufTcpServer;
import com.zfoo.protocol.util.ThreadUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author godotg
*/
@Ignore
public class JProtobufTcpServerTest {
@Test
public void startServer() {
var context = new ClassPathXmlApplicationContext("config.xml");
var server = new JProtobufTcpServer(HostAndPort.valueOf("127.0.0.1:9000"));
server.start();
ThreadUtils.sleep(Long.MAX_VALUE);
}
}
@@ -15,7 +15,6 @@ package com.zfoo.net.core.json.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.jprotobuf.server.JProtobufTcpController;
import com.zfoo.net.core.json.JsonWebsocketClient;
import com.zfoo.net.packet.json.JsonHelloRequest;
import com.zfoo.net.packet.json.JsonHelloResponse;
@@ -55,8 +55,6 @@ public class TcpServerTest {
/*
serverPacketController
gatewayProviderController
JProtobufTcpClientController
JProtobufTcpController
providerController
tcpClientController
tcpServerController
@@ -1,40 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.packet.jprotobuf;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@Protocol(id = 1500)
public class JProtobufHelloRequest {
@Protobuf(order = 1)
private String message;
public static JProtobufHelloRequest valueOf(String message) {
var request = new JProtobufHelloRequest();
request.message = message;
return request;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
@@ -1,34 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.packet.jprotobuf;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@Protocol(id = 1501)
public class JProtobufHelloResponse {
@Protobuf(order = 1)
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
-3
View File
@@ -65,9 +65,6 @@
<protocol id="1400" location="com.zfoo.net.packet.websocket.WebsocketHelloRequest"/>
<protocol id="1401" location="com.zfoo.net.packet.websocket.WebsocketHelloResponse"/>
<protocol id="1500" location="com.zfoo.net.packet.jprotobuf.JProtobufHelloRequest"/>
<protocol id="1501" location="com.zfoo.net.packet.jprotobuf.JProtobufHelloResponse"/>
<protocol id="1600" location="com.zfoo.net.packet.json.JsonHelloRequest"/>
<protocol id="1601" location="com.zfoo.net.packet.json.JsonHelloResponse"/>
+2 -11
View File
@@ -175,18 +175,9 @@ your protocol number a little more compactly, so that your protocol number will
### Ⅷ. Use Protobuf in zfoo
- zfoo only provides protobuf in the way of pojo like jprotobuf, but it can generate proto files for clients to use
through pojo objects
- [jprotobuf](https://github.com/jhunters/jprotobuf) you can directly use simple pojo objects without having to
understand proto file operations and syntax
- Generate a proto file for the client to use through the pojo
object, [Generate a proto configuration](src/test/resources/protobuf.xml)
- You can also customize your own build method,
[Use code custom generation to proto](https://github.com/zfoo-project/tank-game-server/tree/main/common/src/main/java/com/zfoo/tank/common/generate)
- zfoo provides a proto file parsing tool to convert the proto file of protobuf into a pojo object for use by zfoo
- Generate pojo objects through proto files for client use, [Parse proto configuration](https://github.com/zfoo-project/zfoo/blob/main/protocol/src/test/java/com/zfoo/protocol/generate/GenerateProtobufTesting.java)
### Ⅸ. The difference between zfoo and Protobuf
+2 -6
View File
@@ -147,14 +147,10 @@ cpu i9900k
### Ⅷ. 在zfoo中使用Protobuf
- zfoo提供了jprotobuf那种pojo的方式使用protobuf,但是可以通过pojo对象生成proto文件给客户端使用
- zfoo提供了proto文件解析工具,将protobuf的proto文件转换为pojo对象给zfoo使用
- [jprotobuf](https://github.com/jhunters/jprotobuf) 针对Java程序开发一套简易类库,可以直接使用简单的pojo对象无需再去了解proto文件操作与语法
- 通过proto文件生成pojo对象给客户端使用,[解析proto配置](https://github.com/zfoo-project/zfoo/blob/main/protocol/src/test/java/com/zfoo/protocol/generate/GenerateProtobufTesting.java)
- 通过pojo对象生成proto文件给客户端使用,[生成proto配置](src/test/resources/protobuf.xml)
- 也可以自定义自己的生成方式,
[使用代码自定义生成proto](https://github.com/zfoo-project/tank-game-server/tree/main/common/src/main/java/com/zfoo/tank/common/generate)
### Ⅸ. zfoo和Protobuf的区别
+7 -21
View File
@@ -101,27 +101,6 @@
<version>${javassist.version}</version>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>jprotobuf</artifactId>
<version>2.4.19</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- fix protobuf warning-->
<!-- Warning:java: 未知的枚举常量 javax.annotation.meta.When.MAYBE 原因: 找不到-->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-core -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
@@ -137,6 +116,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.21.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
@@ -1,72 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.jprotobuf;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.generate.GenerateOperation;
import com.zfoo.protocol.packet.ProtobufObject;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.util.JsonUtils;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author godotg
*/
@Ignore
public class JProtobufTesting {
private static final Map<Integer, String> mapWithInteger = new HashMap<>(Map.of(Integer.MIN_VALUE, "a", -99, "b", 0, "c", 99, "d", Integer.MAX_VALUE, "e"));
@Test
public void deserializerTest() throws IOException {
var simpleTypeCodec = ProtobufProxy.create(ObjectA.class);
var obj = new ObjectA();
obj.a = Integer.MAX_VALUE;
obj.m = mapWithInteger;
// 序列化
byte[] bytes = simpleTypeCodec.encode(obj);
// 反序列化
var newObj = simpleTypeCodec.decode(bytes);
// 反序列化到protobuf
var newProtobufObj = ProtobufObject.ObjectA.parseFrom(bytes);
System.out.println(JsonUtils.object2String(newObj));
}
@Test
public void serializerTest() throws IOException {
// 原生protobuf对象
var protobufObjectB = ProtobufObject.ObjectB.newBuilder().setFlag(false).build();
var protobufObjectA = ProtobufObject.ObjectA.newBuilder()
.setA(Integer.MAX_VALUE)
.putAllM(mapWithInteger)
.setObjectB(protobufObjectB)
.build();
byte[] bytes = protobufObjectA.toByteArray();
var simpleTypeCodec = ProtobufProxy.create(ObjectA.class);
var newObj = simpleTypeCodec.decode(bytes);
System.out.println(JsonUtils.object2String(newObj));
}
}
@@ -1,46 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.jprotobuf;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.zfoo.protocol.anno.Protocol;
import java.util.Map;
/**
* protobuf的测试类型
* 包括了各种复杂的结构,对象,map
*
* @author godotg
*/
@Protocol(id = 102)
public class ObjectA {
// int类型,在protobuf中叫int32
@Protobuf(order = 1)
public int a;
// map类型
// protobuf中的map的key只能为基础类型
@Protobuf(order = 2)
public Map<Integer, String> m;
/**
* 对象类型
*/
@Protobuf(order = 3)
public ObjectB objectB;
}
@@ -1,29 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.jprotobuf;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@Protocol(id = 103)
public class ObjectB {
@Protobuf(order = 1)
public boolean flag;
}
@@ -1,44 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.jprotobuf;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.zfoo.protocol.anno.Protocol;
import java.util.Map;
/**
* ObjectC的测试类型
*
* @author godotg
*/
@Protocol(id = 104)
public class ObjectC {
// int类型,在protobuf中叫int32
@Protobuf(order = 1)
public int a;
// map类型
// protobuf中的map的key只能为基础类型
@Protobuf(order = 2)
public Map<Integer, String> m;
/**
* 对象类型
*/
@Protobuf(order = 3)
public ObjectB objectB;
}
@@ -1,50 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*/
package com.zfoo.protocol.jprotobuf;
import com.google.protobuf.CodedOutputStream;
import com.zfoo.protocol.buffer.ByteBufUtils;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.UnpooledUnsafeHeapByteBuf;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
/**
* @author godotg
*/
@Ignore
public class ProtobufTagTesting {
@Test
public void test() throws IOException {
var buffer = new byte[1024 * 8];
var output = CodedOutputStream.newInstance(buffer);
output.writeSInt32NoTag(Integer.MAX_VALUE);
var length = output.getTotalBytesWritten();
for (int i = 0; i < length; i++) {
System.out.println(buffer[i]);
}
System.out.println("----------------------------------");
var byteBuf = new UnpooledUnsafeHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 1_0000);
ByteBufUtils.writeInt(byteBuf, Integer.MAX_VALUE);
var bytes = ByteBufUtils.readAllBytes(byteBuf);
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
}
}