mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-06-01 16:12:49 +00:00
ref[protocol]: Serialization and deserialization do not have to be forced to inherit IPacket
This commit is contained in:
@@ -151,7 +151,7 @@ public class PacketService implements IPacketService {
|
||||
// 解析包的附加包
|
||||
var hasAttachment = ByteBufUtils.tryReadBoolean(buffer);
|
||||
var attachment = hasAttachment ? ((IAttachment) ProtocolManager.read(buffer)) : null;
|
||||
return DecodedPacketInfo.valueOf(packet, attachment);
|
||||
return DecodedPacketInfo.valueOf((IPacket) packet, attachment);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ProtocolManager {
|
||||
/**
|
||||
* serialize the packet into the buffer
|
||||
*/
|
||||
public static void write(ByteBuf buffer, IPacket packet) {
|
||||
public static void write(ByteBuf buffer, Object packet) {
|
||||
var protocolId = protocolId(packet.getClass());
|
||||
// write the protocolId
|
||||
ByteBufUtils.writeShort(buffer, protocolId);
|
||||
@@ -67,8 +67,8 @@ public class ProtocolManager {
|
||||
/**
|
||||
* deserialization a packet from the buffer
|
||||
*/
|
||||
public static IPacket read(ByteBuf buffer) {
|
||||
return (IPacket) protocols[ByteBufUtils.readShort(buffer)].read(buffer);
|
||||
public static Object read(ByteBuf buffer) {
|
||||
return protocols[ByteBufUtils.readShort(buffer)].read(buffer);
|
||||
}
|
||||
|
||||
public static IProtocolRegistration getProtocol(short protocolId) {
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
package com.zfoo.protocol.buffer;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.collection.*;
|
||||
import com.zfoo.protocol.registration.IProtocolRegistration;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
@@ -397,13 +396,13 @@ public abstract class ByteBufUtils {
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
//---------------------------------以下方法会被字节码生成的代码调用--------------------------------------
|
||||
public static boolean writePacketFlag(ByteBuf byteBuf, IPacket packet) {
|
||||
public static boolean writePacketFlag(ByteBuf byteBuf, Object packet) {
|
||||
boolean flag = packet == null;
|
||||
byteBuf.writeBoolean(!flag);
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static void writePacketCollection(ByteBuf byteBuf, Collection<? extends IPacket> collection, IProtocolRegistration protocolRegistration) {
|
||||
public static void writePacketCollection(ByteBuf byteBuf, Collection<?> collection, IProtocolRegistration protocolRegistration) {
|
||||
if (collection == null) {
|
||||
byteBuf.writeByte(0);
|
||||
return;
|
||||
@@ -414,28 +413,28 @@ public abstract class ByteBufUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void writePacketList(ByteBuf byteBuf, List<? extends IPacket> list, IProtocolRegistration protocolRegistration) {
|
||||
public static void writePacketList(ByteBuf byteBuf, List<?> list, IProtocolRegistration protocolRegistration) {
|
||||
writePacketCollection(byteBuf, list, protocolRegistration);
|
||||
}
|
||||
|
||||
public static List<IPacket> readPacketList(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
public static List<?> readPacketList(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
var length = readInt(byteBuf);
|
||||
List<IPacket> list = CollectionUtils.newList(length);
|
||||
List<Object> list = CollectionUtils.newList(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
list.add((IPacket) protocolRegistration.read(byteBuf));
|
||||
list.add(protocolRegistration.read(byteBuf));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void writePacketSet(ByteBuf byteBuf, Set<? extends IPacket> list, IProtocolRegistration protocolRegistration) {
|
||||
writePacketCollection(byteBuf, list, protocolRegistration);
|
||||
public static void writePacketSet(ByteBuf byteBuf, Set<?> set, IProtocolRegistration protocolRegistration) {
|
||||
writePacketCollection(byteBuf, set, protocolRegistration);
|
||||
}
|
||||
|
||||
public static Set<IPacket> readPacketSet(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
public static Set<?> readPacketSet(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
var length = readInt(byteBuf);
|
||||
Set<IPacket> set = CollectionUtils.newSet(length);
|
||||
Set<Object> set = CollectionUtils.newSet(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
set.add((IPacket) protocolRegistration.read(byteBuf));
|
||||
set.add(protocolRegistration.read(byteBuf));
|
||||
}
|
||||
return set;
|
||||
}
|
||||
@@ -503,7 +502,7 @@ public abstract class ByteBufUtils {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void writeIntPacketMap(ByteBuf byteBuf, Map<Integer, ? extends IPacket> map, IProtocolRegistration protocolRegistration) {
|
||||
public static void writeIntPacketMap(ByteBuf byteBuf, Map<Integer, ?> map, IProtocolRegistration protocolRegistration) {
|
||||
if (map == null) {
|
||||
byteBuf.writeByte(0);
|
||||
return;
|
||||
@@ -515,11 +514,11 @@ public abstract class ByteBufUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<Integer, IPacket> readIntPacketMap(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
public static Map<Integer, ?> readIntPacketMap(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
var length = readInt(byteBuf);
|
||||
var map = new IntObjectHashMap<IPacket>(CollectionUtils.comfortableCapacity(length));
|
||||
var map = new IntObjectHashMap<>(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
map.put(readInt(byteBuf), (IPacket) protocolRegistration.read(byteBuf));
|
||||
map.put(readInt(byteBuf), protocolRegistration.read(byteBuf));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
@@ -587,7 +586,7 @@ public abstract class ByteBufUtils {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void writeLongPacketMap(ByteBuf byteBuf, Map<Long, ? extends IPacket> map, IProtocolRegistration protocolRegistration) {
|
||||
public static void writeLongPacketMap(ByteBuf byteBuf, Map<Long, ?> map, IProtocolRegistration protocolRegistration) {
|
||||
if (map == null) {
|
||||
byteBuf.writeByte(0);
|
||||
return;
|
||||
@@ -599,11 +598,11 @@ public abstract class ByteBufUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<Long, IPacket> readLongPacketMap(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
public static Map<Long, ?> readLongPacketMap(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
var length = readInt(byteBuf);
|
||||
var map = new LongObjectHashMap<IPacket>(CollectionUtils.comfortableCapacity(length));
|
||||
var map = new LongObjectHashMap<>(CollectionUtils.comfortableCapacity(length));
|
||||
for (var i = 0; i < length; i++) {
|
||||
map.put(readLong(byteBuf), (IPacket) protocolRegistration.read(byteBuf));
|
||||
map.put(readLong(byteBuf), protocolRegistration.read(byteBuf));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
@@ -671,7 +670,7 @@ public abstract class ByteBufUtils {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void writeStringPacketMap(ByteBuf byteBuf, Map<String, ? extends IPacket> map, IProtocolRegistration protocolRegistration) {
|
||||
public static void writeStringPacketMap(ByteBuf byteBuf, Map<String, ?> map, IProtocolRegistration protocolRegistration) {
|
||||
if (map == null) {
|
||||
byteBuf.writeByte(0);
|
||||
return;
|
||||
@@ -683,11 +682,11 @@ public abstract class ByteBufUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, IPacket> readStringPacketMap(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
public static Map<String, ?> readStringPacketMap(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) {
|
||||
var length = readInt(byteBuf);
|
||||
Map<String, IPacket> map = CollectionUtils.newMap(length);
|
||||
Map<String, Object> map = CollectionUtils.newMap(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
map.put(readString(byteBuf), (IPacket) protocolRegistration.read(byteBuf));
|
||||
map.put(readString(byteBuf), protocolRegistration.read(byteBuf));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
@@ -1334,7 +1333,7 @@ public abstract class ByteBufUtils {
|
||||
return chars;
|
||||
}
|
||||
|
||||
public static <T extends IPacket> void writePacketArray(ByteBuf byteBuf, T[] array, IProtocolRegistration protocolRegistration) {
|
||||
public static <T> void writePacketArray(ByteBuf byteBuf, T[] array, IProtocolRegistration protocolRegistration) {
|
||||
if (array == null) {
|
||||
byteBuf.writeByte(0);
|
||||
return;
|
||||
|
||||
+1
-2
@@ -12,7 +12,6 @@
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
@@ -23,7 +22,7 @@ import java.util.function.BiConsumer;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class ConcurrentFileChannelHeapMap<V extends IPacket> implements LpMap<V>, Closeable {
|
||||
public class ConcurrentFileChannelHeapMap<V> implements LpMap<V>, Closeable {
|
||||
|
||||
private final ReentrantLock fileChannelLock = new ReentrantLock();
|
||||
|
||||
|
||||
+1
-3
@@ -12,8 +12,6 @@
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
@@ -23,7 +21,7 @@ import java.util.function.BiConsumer;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class ConcurrentFileChannelMap<V extends IPacket> implements LpMap<V>, Closeable {
|
||||
public class ConcurrentFileChannelMap<V> implements LpMap<V>, Closeable {
|
||||
|
||||
|
||||
private final FileChannelMap<V> fileChannelMap;
|
||||
|
||||
@@ -12,8 +12,6 @@
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import java.util.concurrent.ConcurrentNavigableMap;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
@@ -23,7 +21,7 @@ import java.util.function.BiConsumer;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class ConcurrentHeapMap<V extends IPacket> implements LpMap<V> {
|
||||
public class ConcurrentHeapMap<V> implements LpMap<V> {
|
||||
|
||||
private final ConcurrentNavigableMap<Long, V> map = new ConcurrentSkipListMap<>();
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
@@ -22,7 +21,7 @@ import java.util.function.BiConsumer;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class FileChannelHeapMap<V extends IPacket> implements LpMap<V>, Closeable {
|
||||
public class FileChannelHeapMap<V> implements LpMap<V>, Closeable {
|
||||
|
||||
private final FileChannelMap<V> fileChannelMap;
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import com.zfoo.protocol.exception.RunException;
|
||||
import com.zfoo.protocol.registration.IProtocolRegistration;
|
||||
@@ -37,7 +36,7 @@ import java.util.function.BiConsumer;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class FileChannelMap<V extends IPacket> implements LpMap<V>, Closeable {
|
||||
public class FileChannelMap<V> implements LpMap<V>, Closeable {
|
||||
|
||||
private final File dbFile;
|
||||
protected RandomAccessFile dbFileRandomAccess;
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import com.zfoo.protocol.buffer.ByteBufUtils;
|
||||
import com.zfoo.protocol.registration.IProtocolRegistration;
|
||||
@@ -34,7 +33,7 @@ import java.util.function.BiConsumer;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class FileHeapMap<V extends IPacket> implements LpMap<V> {
|
||||
public class FileHeapMap<V> implements LpMap<V> {
|
||||
|
||||
private final File dbFile;
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import io.netty.util.collection.LongObjectHashMap;
|
||||
|
||||
import java.util.function.BiConsumer;
|
||||
@@ -21,7 +20,7 @@ import java.util.function.BiConsumer;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class HeapMap<V extends IPacket> implements LpMap<V> {
|
||||
public class HeapMap<V> implements LpMap<V> {
|
||||
|
||||
protected LongObjectHashMap<V> map;
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.exception.RunException;
|
||||
|
||||
import java.util.function.BiConsumer;
|
||||
@@ -24,7 +23,7 @@ import java.util.function.BiConsumer;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public interface LpMap<V extends IPacket> {
|
||||
public interface LpMap<V> {
|
||||
|
||||
/**
|
||||
* @param packet the previous value associated with key, or null if there was no mapping for key.
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
package com.zfoo.protocol.registration;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.anno.Compatible;
|
||||
import com.zfoo.protocol.buffer.ByteBufUtils;
|
||||
import com.zfoo.protocol.collection.ArrayUtils;
|
||||
@@ -51,12 +50,11 @@ public abstract class EnhanceUtils {
|
||||
public static String byteBufUtilsWriteInt0 = byteBufUtils + ".writeInt($1, 0);";
|
||||
|
||||
static {
|
||||
var classArray = new Class<?>[]{IPacket.class, IProtocolRegistration.class, IFieldRegistration.class, ByteBuf.class};
|
||||
var classArray = new Class<?>[]{IProtocolRegistration.class, IFieldRegistration.class, ByteBuf.class};
|
||||
|
||||
var classPool = ClassPool.getDefault();
|
||||
|
||||
// 导入需要的包
|
||||
classPool.importPackage(IPacket.class.getCanonicalName());
|
||||
classPool.importPackage(ByteBufUtils.class.getCanonicalName());
|
||||
classPool.importPackage(CollectionUtils.class.getCanonicalName());
|
||||
classPool.importPackage(ArrayUtils.class.getCanonicalName());
|
||||
@@ -159,7 +157,7 @@ public abstract class EnhanceUtils {
|
||||
moduleMethod.setBody("{return " + registration.module() + ";}");
|
||||
enhanceClazz.addMethod(moduleMethod);
|
||||
|
||||
CtMethod writeMethod = new CtMethod(classPool.get(void.class.getCanonicalName()), "write", classPool.get(new String[]{ByteBuf.class.getCanonicalName(), IPacket.class.getCanonicalName()}), enhanceClazz);
|
||||
CtMethod writeMethod = new CtMethod(classPool.get(void.class.getCanonicalName()), "write", classPool.get(new String[]{ByteBuf.class.getCanonicalName(), Object.class.getCanonicalName()}), enhanceClazz);
|
||||
writeMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
|
||||
writeMethod.setBody(writeMethodBody(registration));
|
||||
enhanceClazz.addMethod(writeMethod);
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
package com.zfoo.protocol.registration;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
@@ -33,7 +32,7 @@ public interface IProtocolRegistration {
|
||||
/**
|
||||
* 序列化
|
||||
*/
|
||||
void write(ByteBuf buffer, IPacket packet);
|
||||
void write(ByteBuf buffer, Object packet);
|
||||
|
||||
/**
|
||||
* 反序列化
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
package com.zfoo.protocol.registration;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import com.zfoo.protocol.anno.Compatible;
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
@@ -181,7 +180,7 @@ public class ProtocolAnalysis {
|
||||
for (var protocolDefinition : moduleDefinition.getProtocols()) {
|
||||
protocolDefinitionMap.put(protocolDefinition.getLocation(), protocolDefinition.isEnhance());
|
||||
protocolNameMap.put(protocolDefinition.getLocation(), protocolDefinition.getId());
|
||||
var packetClazzList = scanClassList(protocolDefinition.getLocation());
|
||||
var packetClazzList = Set.of(ClassUtils.forName(protocolDefinition.getLocation()));
|
||||
clazzSet.addAll(packetClazzList);
|
||||
for (Class<?> clazz : packetClazzList) {
|
||||
var previous = classModuleDefinitionMap.put(clazz, module.getId());
|
||||
@@ -200,7 +199,8 @@ public class ProtocolAnalysis {
|
||||
var enhanceList = new ArrayList<IProtocolRegistration>();
|
||||
for (var moduleDefinition : xmlProtocols.getModules()) {
|
||||
var module = modules[moduleDefinition.getId()];
|
||||
var packetClazzList = moduleDefinitionClassMap.get(moduleDefinition.getId());;
|
||||
var packetClazzList = moduleDefinitionClassMap.get(moduleDefinition.getId());
|
||||
;
|
||||
if (CollectionUtils.isEmpty(packetClazzList)) {
|
||||
continue;
|
||||
}
|
||||
@@ -228,37 +228,7 @@ public class ProtocolAnalysis {
|
||||
} catch (Exception e) {
|
||||
throw new RunException("[{}]包扫描类异常", packageName, e);
|
||||
}
|
||||
return scanClassList(clazzSet);
|
||||
}
|
||||
|
||||
public static Set<Class<?>> scanClassList(String className) {
|
||||
var clazzSet = new HashSet<Class<?>>();
|
||||
try {
|
||||
Class<?> clazz = Class.forName(className);
|
||||
if (!IPacket.class.isAssignableFrom(clazz) || clazz.isInterface()) {
|
||||
return clazzSet;
|
||||
}
|
||||
clazzSet.add(clazz);
|
||||
} catch (Exception e) {
|
||||
clazzSet.addAll(scanPackageList(className));
|
||||
}
|
||||
return clazzSet;
|
||||
}
|
||||
|
||||
public static Set<Class<?>> scanClassList(Set<String> classList) {
|
||||
var clazzSet = new HashSet<Class<?>>();
|
||||
for (var className : classList) {
|
||||
try {
|
||||
Class<?> clazz = Class.forName(className);
|
||||
if (!IPacket.class.isAssignableFrom(clazz) || clazz.isInterface()) {
|
||||
continue;
|
||||
}
|
||||
clazzSet.add(clazz);
|
||||
} catch (Exception e) {
|
||||
clazzSet.addAll(scanPackageList(className));
|
||||
}
|
||||
}
|
||||
return clazzSet;
|
||||
return clazzSet.stream().map(it -> ClassUtils.forName(it)).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private static void enhance(GenerateOperation generateOperation, List<IProtocolRegistration> enhanceList) {
|
||||
@@ -582,12 +552,9 @@ public class ProtocolAnalysis {
|
||||
public static short getProtocolIdAndCheckClass(Class<?> clazz) {
|
||||
// 是否为一个简单的javabean
|
||||
ReflectionUtils.assertIsPojoClass(clazz);
|
||||
// 是否实现了IPacket接口
|
||||
AssertionUtils.isTrue(IPacket.class.isAssignableFrom(clazz), "[class:{}]没有实现接口[IPacket:{}]", clazz.getCanonicalName(), IPacket.class.getCanonicalName());
|
||||
// 不能是泛型类
|
||||
AssertionUtils.isTrue(ArrayUtils.isEmpty(clazz.getTypeParameters()), "[class:{}]不能是泛型类", clazz.getCanonicalName());
|
||||
|
||||
|
||||
// 必须要有一个空的构造器
|
||||
Constructor<?> constructor = ReflectionUtils.publicEmptyConstructor(clazz);
|
||||
|
||||
@@ -647,7 +614,7 @@ public class ProtocolAnalysis {
|
||||
|
||||
|
||||
//拓扑排序检查循环协议
|
||||
if(subProtocolIdMap.isEmpty()){
|
||||
if (subProtocolIdMap.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
//先判断自循环引用
|
||||
@@ -660,52 +627,44 @@ public class ProtocolAnalysis {
|
||||
}
|
||||
}
|
||||
//入度
|
||||
var inDegree=new HashMap<Short,Integer>();
|
||||
var inDegree = new HashMap<Short, Integer>();
|
||||
//初始化入度
|
||||
for(var protocolEntry : subProtocolIdMap.entrySet())
|
||||
{
|
||||
var protocolId=protocolEntry.getKey();
|
||||
inDegree.put(protocolId,inDegree.getOrDefault(protocolId,0));
|
||||
for (var protocolEntry : subProtocolIdMap.entrySet()) {
|
||||
var protocolId = protocolEntry.getKey();
|
||||
inDegree.put(protocolId, inDegree.getOrDefault(protocolId, 0));
|
||||
var subProtocolSet = protocolEntry.getValue();
|
||||
for(var subProtocolId:subProtocolSet)
|
||||
{
|
||||
inDegree.put(subProtocolId,inDegree.getOrDefault(subProtocolId,0)+1);
|
||||
for (var subProtocolId : subProtocolSet) {
|
||||
inDegree.put(subProtocolId, inDegree.getOrDefault(subProtocolId, 0) + 1);
|
||||
}
|
||||
}
|
||||
var queue=new LinkedList<Short>();
|
||||
for(var protocolEntry:inDegree.entrySet())
|
||||
{
|
||||
var protocolInDegree=protocolEntry.getValue();
|
||||
if(protocolInDegree==0)
|
||||
{
|
||||
var queue = new LinkedList<Short>();
|
||||
for (var protocolEntry : inDegree.entrySet()) {
|
||||
var protocolInDegree = protocolEntry.getValue();
|
||||
if (protocolInDegree == 0) {
|
||||
queue.offer(protocolEntry.getKey());
|
||||
}
|
||||
}
|
||||
while(!queue.isEmpty())
|
||||
{
|
||||
var protocolId=queue.poll();
|
||||
if(subProtocolIdMap.containsKey(protocolId)){
|
||||
for(var subProtocolId:subProtocolIdMap.get(protocolId))
|
||||
{
|
||||
inDegree.put(subProtocolId,inDegree.get(subProtocolId)-1);
|
||||
if(inDegree.get(subProtocolId)==0)
|
||||
{
|
||||
while (!queue.isEmpty()) {
|
||||
var protocolId = queue.poll();
|
||||
if (subProtocolIdMap.containsKey(protocolId)) {
|
||||
for (var subProtocolId : subProtocolIdMap.get(protocolId)) {
|
||||
inDegree.put(subProtocolId, inDegree.get(subProtocolId) - 1);
|
||||
if (inDegree.get(subProtocolId) == 0) {
|
||||
queue.offer(subProtocolId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var circularReferenceProtocols=new ArrayList<String>();
|
||||
var circularReferenceProtocols = new ArrayList<String>();
|
||||
//入度不为0的表示存在循环引用的协议
|
||||
for(var protocolEntry:inDegree.entrySet())
|
||||
{
|
||||
if(protocolEntry.getValue()>0){
|
||||
for (var protocolEntry : inDegree.entrySet()) {
|
||||
if (protocolEntry.getValue() > 0) {
|
||||
circularReferenceProtocols.add(protocols[protocolEntry.getKey()].protocolConstructor().getDeclaringClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
//抛出所有存在循环引用的协议类名
|
||||
if(circularReferenceProtocols.size()>0){
|
||||
throw new RunException("[class:{}]中存在循环引用",StringUtils.joinWith(",",circularReferenceProtocols.toArray()));
|
||||
if (circularReferenceProtocols.size() > 0) {
|
||||
throw new RunException("[class:{}]中存在循环引用", StringUtils.joinWith(",", circularReferenceProtocols.toArray()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
package com.zfoo.protocol.registration;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.anno.Compatible;
|
||||
import com.zfoo.protocol.buffer.ByteBufUtils;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
@@ -30,7 +29,6 @@ import java.lang.reflect.Field;
|
||||
*/
|
||||
public class ProtocolRegistration implements IProtocolRegistration {
|
||||
|
||||
|
||||
private short id;
|
||||
private byte module;
|
||||
private Constructor<?> constructor;
|
||||
@@ -62,7 +60,7 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
|
||||
|
||||
@Override
|
||||
public void write(ByteBuf buffer, IPacket packet) {
|
||||
public void write(ByteBuf buffer, Object packet) {
|
||||
if (packet == null) {
|
||||
ByteBufUtils.writeBoolean(buffer, false);
|
||||
return;
|
||||
|
||||
+1
-2
@@ -13,7 +13,6 @@
|
||||
|
||||
package com.zfoo.protocol.serializer.cpp;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
@@ -44,7 +43,7 @@ public class CppObjectProtocolSerializer implements ICppSerializer {
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
if (IPacket.class.isAssignableFrom(field.getType())) {
|
||||
if (Object.class.isAssignableFrom(field.getType())) {
|
||||
builder.append(StringUtils.format("buffer.writePacket({}, {});", objectStr, objectProtocolField.getProtocolId()))
|
||||
.append(LS);
|
||||
} else {
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
package com.zfoo.protocol.serializer.cpp;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.anno.Compatible;
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.generate.GenerateOperation;
|
||||
@@ -250,7 +249,7 @@ public abstract class GenerateCppUtils {
|
||||
var field = fields[i];
|
||||
var fieldRegistration = fieldRegistrations[i];
|
||||
var serializer = cppSerializer(fieldRegistration.serializer());
|
||||
if (IPacket.class.isAssignableFrom(field.getType())) {
|
||||
if (Object.class.isAssignableFrom(field.getType())) {
|
||||
serializer.writeObject(cppBuilder, "&message->" + field.getName(), 3, field, fieldRegistration);
|
||||
} else {
|
||||
serializer.writeObject(cppBuilder, "message->" + field.getName(), 3, field, fieldRegistration);
|
||||
@@ -275,7 +274,7 @@ public abstract class GenerateCppUtils {
|
||||
|
||||
var readObject = cppSerializer(fieldRegistration.serializer()).readObject(cppBuilder, 3, field, fieldRegistration);
|
||||
cppBuilder.append(TAB + TAB + TAB);
|
||||
if (IPacket.class.isAssignableFrom(field.getType())) {
|
||||
if (Object.class.isAssignableFrom(field.getType())) {
|
||||
cppBuilder.append(StringUtils.format("packet->{} = *{};", field.getName(), readObject));
|
||||
} else {
|
||||
cppBuilder.append(StringUtils.format("packet->{} = {};", field.getName(), readObject));
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ public class EnhanceObjectProtocolSerializer implements IEnhanceSerializer {
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, Field field, IFieldRegistration fieldRegistration) {
|
||||
var objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
builder.append(StringUtils.format("{}.write($1, (IPacket){});", EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId()), objectStr));
|
||||
builder.append(StringUtils.format("{}.write($1,{});", EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId()), objectStr));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -31,7 +31,7 @@ public interface IEnhanceSerializer {
|
||||
}
|
||||
|
||||
/**
|
||||
* IProtocolRegistration.write(ByteBuf buffer, IPacket packet);
|
||||
* IProtocolRegistration.write(ByteBuf buffer, Object packet);
|
||||
* $1=buffer
|
||||
* $2=packet
|
||||
*/
|
||||
|
||||
+1
-2
@@ -13,7 +13,6 @@
|
||||
|
||||
package com.zfoo.protocol.serializer.reflect;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import com.zfoo.protocol.registration.IProtocolRegistration;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
@@ -38,7 +37,7 @@ public class ObjectProtocolSerializer implements ISerializer {
|
||||
public void writeObject(ByteBuf buffer, Object object, IFieldRegistration fieldRegistration) {
|
||||
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
IProtocolRegistration protocol = ProtocolManager.getProtocol(objectProtocolField.getProtocolId());
|
||||
protocol.write(buffer, (IPacket) object);
|
||||
protocol.write(buffer, object);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -41,6 +41,15 @@ public abstract class ClassUtils {
|
||||
|
||||
public final static String JAR_URL_SEPARATOR = "!/";
|
||||
|
||||
|
||||
public static Class<?> forName(String className) {
|
||||
try {
|
||||
return Class.forName(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描指定包下的class文件
|
||||
*
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
package com.zfoo.protocol.buffer.model;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
/**
|
||||
@@ -20,7 +20,7 @@ import com.zfoo.protocol.anno.Protocol;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 1000)
|
||||
public class BigPacket implements IPacket {
|
||||
public class BigPacket {
|
||||
|
||||
public int[] a = new int[10_0000];
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
package com.zfoo.protocol.buffer.model;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -24,7 +24,7 @@ import java.util.Objects;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 2)
|
||||
public class MyObjectA implements IPacket {
|
||||
public class MyObjectA {
|
||||
|
||||
public int a;
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
package com.zfoo.protocol.buffer.model;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
import java.util.Objects;
|
||||
@@ -23,7 +23,7 @@ import java.util.Objects;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 3)
|
||||
public class MyObjectB implements IPacket {
|
||||
public class MyObjectB {
|
||||
|
||||
public boolean a;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap.model;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
import java.util.Objects;
|
||||
@@ -22,7 +22,7 @@ import java.util.Objects;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 1)
|
||||
public class MyPacket implements IPacket {
|
||||
public class MyPacket {
|
||||
|
||||
private int a;
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.zfoo.protocol.field.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BytesObject implements IPacket {
|
||||
public class BytesObject {
|
||||
private byte[] a;
|
||||
|
||||
private byte[] b;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.zfoo.protocol.field.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class FloatObject implements IPacket {
|
||||
public class FloatObject {
|
||||
private float a;
|
||||
|
||||
private float b;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.zfoo.protocol.field.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class InnerObject implements IPacket {
|
||||
public class InnerObject {
|
||||
private int x;
|
||||
|
||||
public int getX() {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.zfoo.protocol.field.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class InnerObjectObject implements IPacket {
|
||||
public class InnerObjectObject {
|
||||
private InnerObject a;
|
||||
|
||||
private InnerObject b;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.zfoo.protocol.field.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class IntObject implements IPacket {
|
||||
public class IntObject {
|
||||
private int a;
|
||||
|
||||
private int b;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.zfoo.protocol.field.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class IntegerObject implements IPacket {
|
||||
public class IntegerObject {
|
||||
private Integer a;
|
||||
|
||||
private Integer b;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.zfoo.protocol.field.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ListIntegerObject implements IPacket {
|
||||
public class ListIntegerObject {
|
||||
private List<Integer> a;
|
||||
|
||||
private List<Integer> b;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.zfoo.protocol.field.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class MapObject implements IPacket {
|
||||
public class MapObject {
|
||||
private Map<Integer, String> a;
|
||||
|
||||
private Map<Integer, String> b;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.zfoo.protocol.field.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public class SetObject implements IPacket {
|
||||
public class SetObject {
|
||||
private Set<Integer> a;
|
||||
|
||||
private Set<Integer> b;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.zfoo.protocol.field.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class StringObject implements IPacket {
|
||||
public class StringObject {
|
||||
private String a;
|
||||
|
||||
private String b;
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
package com.zfoo.protocol.jprotobuf;
|
||||
|
||||
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -27,7 +26,7 @@ import java.util.Map;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 102)
|
||||
public class ObjectA implements IPacket {
|
||||
public class ObjectA {
|
||||
|
||||
|
||||
// int类型,在protobuf中叫int32
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
package com.zfoo.protocol.jprotobuf;
|
||||
|
||||
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
/**
|
||||
@@ -22,7 +21,7 @@ import com.zfoo.protocol.anno.Protocol;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 103)
|
||||
public class ObjectB implements IPacket {
|
||||
public class ObjectB {
|
||||
|
||||
@Protobuf(order = 1)
|
||||
public boolean flag;
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
package com.zfoo.protocol.jprotobuf;
|
||||
|
||||
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -26,7 +25,7 @@ import java.util.Map;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 104)
|
||||
public class ObjectC implements IPacket {
|
||||
public class ObjectC {
|
||||
|
||||
// int类型,在protobuf中叫int32
|
||||
@Protobuf(order = 1)
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
package com.zfoo.protocol.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import com.zfoo.protocol.anno.Compatible;
|
||||
import com.zfoo.protocol.anno.Note;
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
@@ -21,7 +21,7 @@ import com.zfoo.protocol.anno.Protocol;
|
||||
import java.util.*;
|
||||
|
||||
@Protocol(id = 100, note = "复杂的对象,包括了各种复杂的结构,数组,List,Set,Map")
|
||||
public class ComplexObject implements IPacket {
|
||||
public class ComplexObject {
|
||||
|
||||
|
||||
@Note("byte类型,最简单的整形")
|
||||
|
||||
@@ -12,10 +12,10 @@
|
||||
|
||||
package com.zfoo.protocol.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
public class EmptyObject implements IPacket {
|
||||
public class EmptyObject {
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
package com.zfoo.protocol.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
import java.util.List;
|
||||
@@ -25,7 +25,7 @@ import java.util.Set;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 101)
|
||||
public class NormalObject implements IPacket {
|
||||
public class NormalObject {
|
||||
|
||||
private byte a;
|
||||
private byte[] aaa;
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
package com.zfoo.protocol.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -24,7 +24,7 @@ import java.util.Objects;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 102)
|
||||
public class ObjectA implements IPacket {
|
||||
public class ObjectA {
|
||||
|
||||
private int a;
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
package com.zfoo.protocol.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
import java.util.Objects;
|
||||
@@ -23,7 +23,7 @@ import java.util.Objects;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 103)
|
||||
public class ObjectB implements IPacket {
|
||||
public class ObjectB {
|
||||
|
||||
private boolean flag;
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
package com.zfoo.protocol.packet;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import com.zfoo.protocol.anno.Protocol;
|
||||
|
||||
/**
|
||||
@@ -21,7 +21,7 @@ import com.zfoo.protocol.anno.Protocol;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Protocol(id = 104)
|
||||
public class SimpleObject implements IPacket {
|
||||
public class SimpleObject {
|
||||
|
||||
private int c;
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ package com.zfoo.protocol.packet;
|
||||
|
||||
import com.esotericsoftware.kryo.io.Input;
|
||||
import com.esotericsoftware.kryo.io.Output;
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
@@ -33,7 +32,7 @@ import static com.zfoo.protocol.SpeedTest.*;
|
||||
/**
|
||||
* 主要来测试极端大的对象序列化和反序列化情况,极端大的对象指的是字段多,对象大,方法大
|
||||
*/
|
||||
public class VeryBigObject implements IPacket {
|
||||
public class VeryBigObject {
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user