Skip to content

Commit

Permalink
Merge pull request #120 from indunet/codec
Browse files Browse the repository at this point in the history
Codec
  • Loading branch information
Deng-Ran authored Oct 27, 2022
2 parents 90f8451 + 54e1484 commit c727d82
Show file tree
Hide file tree
Showing 24 changed files with 169 additions and 262 deletions.
5 changes: 2 additions & 3 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ FastProto是一款能够通过注解自定义协议的二进制序列化 & 反

## *Under Developing*

* 自动类型校验
* 地址冲突检测
* 代码结构 & 性能优化

Expand All @@ -47,7 +46,7 @@ FastProto是一款能够通过注解自定义协议的二进制序列化 & 反
<dependency>
<groupId>org.indunet</groupId>
<artifactId>fastproto</artifactId>
<version>3.7.1</version>
<version>3.8.0</version>
</dependency>
```

Expand Down Expand Up @@ -298,7 +297,7 @@ import org.indunet.fastproto.annotation.scala._

## *基准测试*

* macOS, m1 8 cores, 16gb
* windows 11, i7 11th, 32gb
* openjdk 1.8.0_292
* 二进制数据固定大小60字节,数据对象共包含13个不同类型的字段

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ solve the problem of cross-language and cross-platform data exchange, which is e

## *Under Developing*

* Verification of auto type
* Address conflict detection
* Code structure & performance optimization

Expand All @@ -49,7 +48,7 @@ FastProto is more recommended for the following scenarios:
<dependency>
<groupId>org.indunet</groupId>
<artifactId>fastproto</artifactId>
<version>3.7.1</version>
<version>3.8.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.indunet</groupId>
<artifactId>fastproto</artifactId>
<version>3.7.1</version>
<version>3.8.0</version>
<packaging>jar</packaging>
<name>FastProto</name>
<description>FastProto is a binary serialization and deserialization tool that can customize protocol through annotations.</description>
Expand Down
53 changes: 0 additions & 53 deletions src/main/java/org/indunet/fastproto/CodecFeature.java

This file was deleted.

105 changes: 40 additions & 65 deletions src/main/java/org/indunet/fastproto/FastProto.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,103 +30,78 @@
*/
public class FastProto {
/**
* Convert binary message into object.
* Convert byte array into object.
*
* @param datagram binary message
* @param protocolClass deserialized object
* @return deserialize object instance
* @param bytes byte array
* @param clazz class of deserialized object
* @return deserialize object
*/
public static <T> T parse(@NonNull byte[] datagram, @NonNull Class<T> protocolClass) {
return parse(datagram, protocolClass, CodecFeature.DEFAULT);
}

/**
* Convert binary message into object.
*
* @param datagram binary message
* @param protocolClass deserialized object
* @param codecFeatures codec feature code
* @return deserialize object instance
*/
public static <T> T parse(@NonNull byte[] datagram, @NonNull Class<T> protocolClass, long... codecFeatures) {
val graph = Resolver.resolve(protocolClass);
val codecFeature = CodecFeature.of(codecFeatures);
public static <T> T parse(@NonNull byte[] bytes, @NonNull Class<T> clazz) {
val graph = Resolver.resolve(clazz);
val context = PipelineContext.builder()
.datagram(datagram)
.protocolClass(protocolClass)
.codecFeature(codecFeature)
.bytes(bytes)
.clazz(clazz)
.graph(graph)
.build();

val feature = CodecFeature.of(graph.root());

Pipeline.getDecodeFlow(feature | codecFeature)
Pipeline.getDecodeFlow()
.process(context);

return context.getObject(protocolClass);
}

/**
* Convert object into binary datagram.
*
* @param object serialized object
* @return binary datagram.
*/
public static byte[] toBytes(@NonNull Object object) {
return toBytes(object, CodecFeature.DEFAULT);
return context.getObject(clazz);
}

/**
* Convert object into binary datagram.
* Convert object into byte array.
*
* @param object serialized object
* @param length the length of the datagram.
* @return binary datagram.
* @return byte array
*/
public static byte[] toBytes(@NonNull Object object, int length) {
return toBytes(object, new byte[length], CodecFeature.DEFAULT);
}

public static void toBytes(@NonNull Object object, byte[] buffer) {
toBytes(object, buffer, CodecFeature.DEFAULT);
}

public static byte[] toBytes(@NonNull Object object, long... codecFeatures) {
public static byte[] toBytes(Object object) {
val graph = Resolver.resolve(object.getClass());
val codecFeature = CodecFeature.of(codecFeatures);
val context = PipelineContext.builder()
.object(object)
.protocolClass(object.getClass())
.codecFeature(codecFeature)
.clazz(object.getClass())
.graph(graph)
.build();
val feature = CodecFeature.of(graph.root());

Pipeline.getEncodeFlow(feature | codecFeature)
Pipeline.getEncodeFlow()
.process(context);

return context.getDatagram();
return context.getBytes();
}

public static byte[] toBytes(@NonNull Object object, int length, long... codecFeatures) {
return toBytes(object, new byte[length], codecFeatures);
/**
* Convert object into byte array.
*
* @param object serialized object
* @param length the length of byte array
* @return byte array
*/
public static byte[] toBytes(Object object, int length) {
val bytes = new byte[length];

toBytes(object, bytes);

return bytes;
}

public static byte[] toBytes(@NonNull Object object, byte[] buffer, long... codecFeatures) {
/**
* Convert object into byte array.
*
* @param object serialized object
* @param buffer write result into buffer
* @return byte array
*/
public static void toBytes(Object object, byte[] buffer) {
val graph = Resolver.resolve(object.getClass());
val codecFeature = CodecFeature.of(codecFeatures);
val context = PipelineContext.builder()
.object(object)
.protocolClass(object.getClass())
.codecFeature(codecFeature)
.datagram(buffer)
.clazz(object.getClass())
.bytes(buffer)
.graph(graph)
.build();
val feature = CodecFeature.of(graph.root());

Pipeline.getEncodeFlow(feature | CodecFeature.NON_INFER_LENGTH | codecFeature)
Pipeline.getEncodeFlow()
.process(context);

return context.getDatagram();
}
}
10 changes: 9 additions & 1 deletion src/main/java/org/indunet/fastproto/ProtocolType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import lombok.val;
import org.indunet.fastproto.annotation.*;
import org.indunet.fastproto.exception.ResolveException;

import java.lang.annotation.Annotation;
import java.lang.reflect.Proxy;
Expand Down Expand Up @@ -57,7 +58,14 @@ static <T> T proxy(AutoType autoType, Class<T> dataTypeAnnotationClass) {

if (Arrays.asList("offset", "byteOffset", "bitOffset", "length")
.contains(mth.getName())) {
return ((int[]) mth.invoke(autoType, args))[0];
val ints = (int[]) mth.invoke(autoType, args);

if (ints.length != 0) {
return ints[0];
} else {
throw new ResolveException(
String.format("Autotype lack of property %s", mth.getName()));
}
} else if (Arrays.asList("endian", "charset", "name")
.contains(method.getName())) {
return mth.invoke(autoType, args);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.indunet.fastproto.formula;

import org.indunet.fastproto.exception.FormulaException;
import org.indunet.fastproto.exception.ResolveException;
import org.indunet.fastproto.formula.compiler.JavaStringCompiler;

import java.io.IOException;
Expand All @@ -39,7 +39,7 @@ static FormulaBuilder create(Class inputType, String lambda) {

return (FormulaBuilder) clazz.newInstance();
} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new FormulaException(String.format("Fail compiling lambda expression: %s", lambda), e);
throw new ResolveException(String.format("Fail compiling lambda expression: %s", lambda), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

import lombok.val;
import lombok.var;
import org.indunet.fastproto.exception.FormulaException;
import org.indunet.fastproto.exception.ResolveException;

import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -57,7 +56,7 @@ public Map<String, byte[]> compile(String fileName, String sourceCode) throws IO
Boolean result = task.call();

if (result == null || !result.booleanValue()) {
throw new FormulaException("Fail compiling lambda expression.");
throw new ResolveException("Fail compiling lambda expression.");
}

return manager.getClassBytes();
Expand Down
Loading

0 comments on commit c727d82

Please sign in to comment.