mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-14 10:19:04 +00:00
del[net]: remove jprotobuf
This commit is contained in:
1 parent
d0e83c6e1e
commit
f1cb975715
28 files changed
+12
-783
No files matched your search
+2
-11
@@ -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
|
||||
|
||||
|
||||
@@ -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
@@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user