From f1cb9757151a6bf85b62ad5e644df34d21017987 Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 10 Dec 2023 23:13:41 +0800 Subject: [PATCH] del[net]: remove jprotobuf --- doc/TODO.md | 2 +- net/pom.xml | 12 --- .../jprotobuf/JProtobufGatewayServer.java | 48 --------- .../core/jprotobuf/JProtobufTcpClient.java | 40 -------- .../core/jprotobuf/JProtobufTcpServer.java | 40 -------- .../jprotobuf/JProtobufTcpCodecHandler.java | 97 ------------------- .../com/zfoo/net/packet/common/Error.java | 6 -- .../com/zfoo/net/packet/common/Heartbeat.java | 2 - .../com/zfoo/net/packet/common/Message.java | 2 - .../java/com/zfoo/net/packet/common/Ping.java | 2 - .../java/com/zfoo/net/packet/common/Pong.java | 4 - .../client/JProtobufTcpClientController.java | 36 ------- .../client/JProtobufTcpClientTest.java | 52 ---------- .../server/JProtobufTcpController.java | 43 -------- .../server/JProtobufTcpServerTest.java | 39 -------- .../json/client/JsonWebsocketClientTest.java | 1 - .../core/tcpAsync/server/TcpServerTest.java | 2 - .../jprotobuf/JProtobufHelloRequest.java | 40 -------- .../jprotobuf/JProtobufHelloResponse.java | 34 ------- net/src/test/resources/protocol.xml | 3 - protocol/README.md | 13 +-- protocol/README_CN.md | 8 +- protocol/pom.xml | 28 ++---- .../protocol/jprotobuf/JProtobufTesting.java | 72 -------------- .../com/zfoo/protocol/jprotobuf/ObjectA.java | 46 --------- .../com/zfoo/protocol/jprotobuf/ObjectB.java | 29 ------ .../com/zfoo/protocol/jprotobuf/ObjectC.java | 44 --------- .../jprotobuf/ProtobufTagTesting.java | 50 ---------- 28 files changed, 12 insertions(+), 783 deletions(-) delete mode 100644 net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufGatewayServer.java delete mode 100644 net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufTcpClient.java delete mode 100644 net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufTcpServer.java delete mode 100644 net/src/main/java/com/zfoo/net/handler/codec/jprotobuf/JProtobufTcpCodecHandler.java delete mode 100644 net/src/test/java/com/zfoo/net/core/jprotobuf/client/JProtobufTcpClientController.java delete mode 100644 net/src/test/java/com/zfoo/net/core/jprotobuf/client/JProtobufTcpClientTest.java delete mode 100644 net/src/test/java/com/zfoo/net/core/jprotobuf/server/JProtobufTcpController.java delete mode 100644 net/src/test/java/com/zfoo/net/core/jprotobuf/server/JProtobufTcpServerTest.java delete mode 100644 net/src/test/java/com/zfoo/net/packet/jprotobuf/JProtobufHelloRequest.java delete mode 100644 net/src/test/java/com/zfoo/net/packet/jprotobuf/JProtobufHelloResponse.java delete mode 100644 protocol/src/test/java/com/zfoo/protocol/jprotobuf/JProtobufTesting.java delete mode 100644 protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectA.java delete mode 100644 protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectB.java delete mode 100644 protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectC.java delete mode 100644 protocol/src/test/java/com/zfoo/protocol/jprotobuf/ProtobufTagTesting.java diff --git a/doc/TODO.md b/doc/TODO.md index 94779982..9886a596 100644 --- a/doc/TODO.md +++ b/doc/TODO.md @@ -1,6 +1,6 @@ ### protocol -- 考虑用javassist的方式支持protobuf,放弃使用jprotobuf(可以省略很多不必要的注解,代码更加简洁),用约定大于配置的方式,还可以原生的和pojo对象结合起来 +- 考虑用javassist的方式支持protobuf,用约定大于配置的方式,还可以原生的和pojo对象结合起来 - 通过protobuf的proto文件生成java的pojo源代码,通过动态编译java源代码的方式生成其它语言的源代码 diff --git a/net/pom.xml b/net/pom.xml index 7ff4524d..1ceb8744 100644 --- a/net/pom.xml +++ b/net/pom.xml @@ -30,7 +30,6 @@ 5.2.3 1.10.0 - 2.4.19 3.3.1 3.3.1 @@ -140,17 +139,6 @@ ${spring.boot.version} provided - - com.baidu - jprotobuf - ${jprotobuf.version} - - - slf4j-api - org.slf4j - - - junit junit diff --git a/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufGatewayServer.java b/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufGatewayServer.java deleted file mode 100644 index 442f07b9..00000000 --- a/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufGatewayServer.java +++ /dev/null @@ -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 { - - private final BiFunction packetFilter; - - public JProtobufGatewayServer(HostAndPort host, @Nullable BiFunction 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)); - } - -} diff --git a/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufTcpClient.java b/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufTcpClient.java deleted file mode 100644 index 01d2e96b..00000000 --- a/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufTcpClient.java +++ /dev/null @@ -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 { - - 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()); - } -} diff --git a/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufTcpServer.java b/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufTcpServer.java deleted file mode 100644 index b74e28c1..00000000 --- a/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufTcpServer.java +++ /dev/null @@ -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 { - - 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()); - } -} diff --git a/net/src/main/java/com/zfoo/net/handler/codec/jprotobuf/JProtobufTcpCodecHandler.java b/net/src/main/java/com/zfoo/net/handler/codec/jprotobuf/JProtobufTcpCodecHandler.java deleted file mode 100644 index 7ed464d2..00000000 --- a/net/src/main/java/com/zfoo/net/handler/codec/jprotobuf/JProtobufTcpCodecHandler.java +++ /dev/null @@ -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 { - - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf in, List 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) 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); - } -} diff --git a/net/src/main/java/com/zfoo/net/packet/common/Error.java b/net/src/main/java/com/zfoo/net/packet/common/Error.java index 31fac1e1..7b4e3ba4 100644 --- a/net/src/main/java/com/zfoo/net/packet/common/Error.java +++ b/net/src/main/java/com/zfoo/net/packet/common/Error.java @@ -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 diff --git a/net/src/main/java/com/zfoo/net/packet/common/Heartbeat.java b/net/src/main/java/com/zfoo/net/packet/common/Heartbeat.java index 6a16022c..120f9828 100644 --- a/net/src/main/java/com/zfoo/net/packet/common/Heartbeat.java +++ b/net/src/main/java/com/zfoo/net/packet/common/Heartbeat.java @@ -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 { diff --git a/net/src/main/java/com/zfoo/net/packet/common/Message.java b/net/src/main/java/com/zfoo/net/packet/common/Message.java index 10b29032..bed3ff38 100644 --- a/net/src/main/java/com/zfoo/net/packet/common/Message.java +++ b/net/src/main/java/com/zfoo/net/packet/common/Message.java @@ -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 { diff --git a/net/src/main/java/com/zfoo/net/packet/common/Ping.java b/net/src/main/java/com/zfoo/net/packet/common/Ping.java index 400e045f..bcf60ddc 100644 --- a/net/src/main/java/com/zfoo/net/packet/common/Ping.java +++ b/net/src/main/java/com/zfoo/net/packet/common/Ping.java @@ -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 { diff --git a/net/src/main/java/com/zfoo/net/packet/common/Pong.java b/net/src/main/java/com/zfoo/net/packet/common/Pong.java index 1790ae58..825927b7 100644 --- a/net/src/main/java/com/zfoo/net/packet/common/Pong.java +++ b/net/src/main/java/com/zfoo/net/packet/common/Pong.java @@ -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) { diff --git a/net/src/test/java/com/zfoo/net/core/jprotobuf/client/JProtobufTcpClientController.java b/net/src/test/java/com/zfoo/net/core/jprotobuf/client/JProtobufTcpClientController.java deleted file mode 100644 index cfa421ca..00000000 --- a/net/src/test/java/com/zfoo/net/core/jprotobuf/client/JProtobufTcpClientController.java +++ /dev/null @@ -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)); - } - -} diff --git a/net/src/test/java/com/zfoo/net/core/jprotobuf/client/JProtobufTcpClientTest.java b/net/src/test/java/com/zfoo/net/core/jprotobuf/client/JProtobufTcpClientTest.java deleted file mode 100644 index 3eed99c1..00000000 --- a/net/src/test/java/com/zfoo/net/core/jprotobuf/client/JProtobufTcpClientTest.java +++ /dev/null @@ -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); - } - -} diff --git a/net/src/test/java/com/zfoo/net/core/jprotobuf/server/JProtobufTcpController.java b/net/src/test/java/com/zfoo/net/core/jprotobuf/server/JProtobufTcpController.java deleted file mode 100644 index a069e723..00000000 --- a/net/src/test/java/com/zfoo/net/core/jprotobuf/server/JProtobufTcpController.java +++ /dev/null @@ -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); - } - -} diff --git a/net/src/test/java/com/zfoo/net/core/jprotobuf/server/JProtobufTcpServerTest.java b/net/src/test/java/com/zfoo/net/core/jprotobuf/server/JProtobufTcpServerTest.java deleted file mode 100644 index 8586ded9..00000000 --- a/net/src/test/java/com/zfoo/net/core/jprotobuf/server/JProtobufTcpServerTest.java +++ /dev/null @@ -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); - } - -} diff --git a/net/src/test/java/com/zfoo/net/core/json/client/JsonWebsocketClientTest.java b/net/src/test/java/com/zfoo/net/core/json/client/JsonWebsocketClientTest.java index ff18ae21..abb48e31 100644 --- a/net/src/test/java/com/zfoo/net/core/json/client/JsonWebsocketClientTest.java +++ b/net/src/test/java/com/zfoo/net/core/json/client/JsonWebsocketClientTest.java @@ -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; diff --git a/net/src/test/java/com/zfoo/net/core/tcpAsync/server/TcpServerTest.java b/net/src/test/java/com/zfoo/net/core/tcpAsync/server/TcpServerTest.java index 9192da39..a41f8a17 100644 --- a/net/src/test/java/com/zfoo/net/core/tcpAsync/server/TcpServerTest.java +++ b/net/src/test/java/com/zfoo/net/core/tcpAsync/server/TcpServerTest.java @@ -55,8 +55,6 @@ public class TcpServerTest { /* serverPacketController gatewayProviderController -JProtobufTcpClientController -JProtobufTcpController providerController tcpClientController tcpServerController diff --git a/net/src/test/java/com/zfoo/net/packet/jprotobuf/JProtobufHelloRequest.java b/net/src/test/java/com/zfoo/net/packet/jprotobuf/JProtobufHelloRequest.java deleted file mode 100644 index ae2bf589..00000000 --- a/net/src/test/java/com/zfoo/net/packet/jprotobuf/JProtobufHelloRequest.java +++ /dev/null @@ -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; - } -} diff --git a/net/src/test/java/com/zfoo/net/packet/jprotobuf/JProtobufHelloResponse.java b/net/src/test/java/com/zfoo/net/packet/jprotobuf/JProtobufHelloResponse.java deleted file mode 100644 index 553b95d7..00000000 --- a/net/src/test/java/com/zfoo/net/packet/jprotobuf/JProtobufHelloResponse.java +++ /dev/null @@ -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; - } -} diff --git a/net/src/test/resources/protocol.xml b/net/src/test/resources/protocol.xml index ec2adfb8..95a34255 100644 --- a/net/src/test/resources/protocol.xml +++ b/net/src/test/resources/protocol.xml @@ -65,9 +65,6 @@ - - - diff --git a/protocol/README.md b/protocol/README.md index 29a57064..9a73227c 100644 --- a/protocol/README.md +++ b/protocol/README.md @@ -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 diff --git a/protocol/README_CN.md b/protocol/README_CN.md index 7b7aa997..7933725d 100644 --- a/protocol/README_CN.md +++ b/protocol/README_CN.md @@ -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的区别 diff --git a/protocol/pom.xml b/protocol/pom.xml index a5100bce..81f583fb 100644 --- a/protocol/pom.xml +++ b/protocol/pom.xml @@ -101,27 +101,6 @@ ${javassist.version} - - com.baidu - jprotobuf - 2.4.19 - test - - - slf4j-api - org.slf4j - - - - - - - - com.google.code.findbugs - jsr305 - 3.0.2 - - org.openjdk.jmh @@ -137,6 +116,13 @@ test + + com.google.protobuf + protobuf-java + 3.21.7 + test + + com.esotericsoftware kryo diff --git a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/JProtobufTesting.java b/protocol/src/test/java/com/zfoo/protocol/jprotobuf/JProtobufTesting.java deleted file mode 100644 index b4db7c62..00000000 --- a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/JProtobufTesting.java +++ /dev/null @@ -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 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)); - } -} diff --git a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectA.java b/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectA.java deleted file mode 100644 index 00ad16da..00000000 --- a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectA.java +++ /dev/null @@ -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 m; - - /** - * 对象类型 - */ - @Protobuf(order = 3) - public ObjectB objectB; - -} diff --git a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectB.java b/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectB.java deleted file mode 100644 index c8a4ee18..00000000 --- a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectB.java +++ /dev/null @@ -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; - -} - diff --git a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectC.java b/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectC.java deleted file mode 100644 index 15e5593f..00000000 --- a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ObjectC.java +++ /dev/null @@ -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 m; - - /** - * 对象类型 - */ - @Protobuf(order = 3) - public ObjectB objectB; - -} diff --git a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ProtobufTagTesting.java b/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ProtobufTagTesting.java deleted file mode 100644 index cb5cbe18..00000000 --- a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/ProtobufTagTesting.java +++ /dev/null @@ -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]); - } - } - -}