deployed 2.0.4.RELEASE,增加集合类型长度域,升级netty版本

master
剑器近 2021-09-18 18:31:29 +08:00
parent abcf217332
commit b7a938ff4a
4 changed files with 31 additions and 18 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>io.github.yezhihao</groupId> <groupId>io.github.yezhihao</groupId>
<artifactId>protostar</artifactId> <artifactId>protostar</artifactId>
<version>2.0.3.RELEASE</version> <version>2.0.4.RELEASE</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Protostar</name> <name>Protostar</name>
@ -62,7 +62,7 @@
<dependency> <dependency>
<groupId>io.netty</groupId> <groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId> <artifactId>netty-buffer</artifactId>
<version>4.1.66.Final</version> <version>4.1.68.Final</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -65,7 +65,7 @@ public abstract class FieldFactory {
fieldSchema = ConvertSchema.getInstance(field.converter()); fieldSchema = ConvertSchema.getInstance(field.converter());
break; break;
case LIST: case LIST:
fieldSchema = CollectionSchema.getInstance(schema); fieldSchema = CollectionSchema.getInstance(schema, field.lengthSize());
break; break;
case MAP: case MAP:
fieldSchema = ConvertSchema.getInstance(field.converter()); fieldSchema = ConvertSchema.getInstance(field.converter());
@ -85,7 +85,7 @@ public abstract class FieldFactory {
result = new FixedField.Logger(field, f, fieldSchema); result = new FixedField.Logger(field, f, fieldSchema);
} }
} else { } else {
if (field.lengthSize() > 0) { if (field.lengthSize() > 0 && !(fieldSchema instanceof CollectionSchema)) {
result = new DynamicLengthField(field, f, fieldSchema); result = new DynamicLengthField(field, f, fieldSchema);
} else if (field.length() > 0) { } else if (field.length() > 0) {
result = new FixedLengthField(field, f, fieldSchema); result = new FixedLengthField(field, f, fieldSchema);

View File

@ -3,8 +3,6 @@ package io.github.yezhihao.protostar.field;
import io.github.yezhihao.protostar.annotation.Field; import io.github.yezhihao.protostar.annotation.Field;
import io.github.yezhihao.protostar.util.StrUtils; import io.github.yezhihao.protostar.util.StrUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* *
@ -12,7 +10,6 @@ import org.slf4j.LoggerFactory;
* home https://gitee.com/yezhihao/jt808-server * home https://gitee.com/yezhihao/jt808-server
*/ */
public abstract class BasicField<T> implements Comparable<BasicField<T>> { public abstract class BasicField<T> implements Comparable<BasicField<T>> {
protected static Logger log = LoggerFactory.getLogger(BasicField.class.getSimpleName());
protected final int index; protected final int index;
protected final int length; protected final int length;

View File

@ -1,6 +1,7 @@
package io.github.yezhihao.protostar.schema; package io.github.yezhihao.protostar.schema;
import io.github.yezhihao.protostar.Schema; import io.github.yezhihao.protostar.Schema;
import io.github.yezhihao.protostar.util.ByteBufUtils;
import io.github.yezhihao.protostar.util.Cache; import io.github.yezhihao.protostar.util.Cache;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -9,28 +10,42 @@ import java.util.List;
public class CollectionSchema<T> implements Schema<List<T>> { public class CollectionSchema<T> implements Schema<List<T>> {
private static final Cache<Schema, CollectionSchema> CACHE = new Cache<>(); private static final Cache<String, CollectionSchema> CACHE = new Cache<>();
public static CollectionSchema getInstance(Schema schema) { public static CollectionSchema getInstance(Schema schema, int lengthSize) {
return CACHE.get(schema, key -> new CollectionSchema(key)); return CACHE.get(schema.hashCode() + "_" + lengthSize, key -> new CollectionSchema(schema, lengthSize));
} }
private final Schema<T> schema; private final Schema<T> schema;
private CollectionSchema(Schema<T> schema) { private final int lengthSize;
private CollectionSchema(Schema<T> schema, int lengthSize) {
this.schema = schema; this.schema = schema;
this.lengthSize = lengthSize;
} }
@Override @Override
public List<T> readFrom(ByteBuf input) { public List<T> readFrom(ByteBuf input) {
if (!input.isReadable()) if (!input.isReadable())
return null; return null;
List<T> list = new ArrayList<>(); List<T> list;
do { if (lengthSize > 0) {
T obj = schema.readFrom(input); int length = ByteBufUtils.readInt(input, lengthSize);
if (obj == null) break; list = new ArrayList<>(length);
list.add(obj); for (int i = 0; i < length; i++) {
} while (input.isReadable()); T obj = schema.readFrom(input);
if (obj == null) break;
list.add(obj);
}
} else {
list = new ArrayList<>(2);
do {
T obj = schema.readFrom(input);
if (obj == null) break;
list.add(obj);
} while (input.isReadable());
}
return list; return list;
} }
@ -48,7 +63,8 @@ public class CollectionSchema<T> implements Schema<List<T>> {
public void writeTo(ByteBuf output, List<T> list) { public void writeTo(ByteBuf output, List<T> list) {
if (list == null || list.isEmpty()) if (list == null || list.isEmpty())
return; return;
if (lengthSize > 0)
ByteBufUtils.writeInt(output, lengthSize, list.size());
for (T obj : list) { for (T obj : list) {
schema.writeTo(output, obj); schema.writeTo(output, obj);
} }