Coder Social home page Coder Social logo

genpack's Introduction

GenPack

latest version downloads

GenPack is a library that uses the .NET source generator to automatically generate packets as classes once you define a schema for the packets. It's easy to use and the results are useful.

GenPack also works well with Native AOT. You can take advantage of the benefits of Native AOT.

Simple to use

[GenPackable]
public partial record PeoplePacket
{
    public readonly static PacketSchema Schema = PacketSchemaBuilder.Create()
        .@short("Age", "Age description")
        .@string("Name", "Name description")
        .Build();
}

The following code is automatically generated by the schema information.

    public partial record PeoplePacket : GenPack.IGenPackable
    {
        /// <summary>
        /// Age description
        /// </summary>
        public short Age { get; set; }
        /// <summary>
        /// Name description
        /// </summary>
        public string Name { get; set; } = string.Empty;
        public byte[] ToPacket()
        {
            using var ms = new System.IO.MemoryStream();
            ToPacket(ms);
            return ms.ToArray();
        }
        public void ToPacket(System.IO.Stream stream)
        {
            System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
            writer.Write(Age);
            writer.Write(Name);
        }
        public static PeoplePacket FromPacket(byte[] data)
        {
            using var ms = new System.IO.MemoryStream(data);
            return FromPacket(ms);
        }
        public static PeoplePacket FromPacket(System.IO.Stream stream)
        {
            PeoplePacket result = new PeoplePacket();
            System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);
            int size = 0;
            byte[] buffer = null;
            result.Age = reader.ReadInt16();
            result.Name = reader.ReadString();
            return result;
        }
    }

It's simple to use. You can binary serialize with ToPacket() and deserialize with FromPacket().

var p = new PeoplePacket()
{
    Age = 10,
    Name = "John"
};
var data = p.ToPacket();
var newP = PeoplePacket.FromPacket(data);

Console.WriteLine(newP);
PeoplePacket { Age = 10, Name = John }

How to create a packet schema

Decorate the attribute of class or record with GenPackable. At this point, the target must be given partial. GenPack's packet schema is represented by creating a PacketSchema using the PacketSchemaBuilder.

[GenPackable]
public partial record PeoplePacket
{
    public readonly static PacketSchema Schema = PacketSchemaBuilder.Create()
        .@short("Age", "Age description")
        .@string("Name", "Name description")
        .Build();
}

The format beginning with @ means the schema property to be created. For example, @short("Age", "Age description") gives the Age property the type short and the description Age description. This translates to the following,

        /// <summary>
        /// Age description
        /// </summary>
        public short Age { get; set; }

You can then use the auto-generated properties.

var p = new PeoplePacket()
p.Age = 32;

Schema Properties

Property Description Bits Arguments
@byte byte 8 property name, description
@sbyte signed byte 8 property name, description
@short short int 16 property name, description
@ushort unsigned short int 16 property name, description
@int int 32 property name, description
@uint unsigned int 32 property name, description
@long long int 64 property name, description
@ulong unsigned long int 64 property name, description
@float single float 32 property name, description
@double double float 64 property name, description
@string string N property name, description
@object<type> genpackable object N property name, description
@list<type> variable list N property name, description
@dict<type> variable dictionary N property name, description
@array<type> fixed array N property name, size, description

Tasks

  • Support for Endian, string Encoding.
  • Support for checksums.
  • Support 8-bit, 16-bit, 32-bit, 64-bit, or variable 7-bit sizes for @list and @dict.
  • Add @ver property to allow revision control of packets.
  • Automatically select and deserialize target structures based on packet command(identification code).
  • Generate JSON and gRPC schema with PacketSchema.
  • Process device packets with uncomplicated packet structures.
  • Process structures with complex packets, such as PLCs.
  • Process packets that require speed, such as MemoryPack.

Icon creator: Freepik - Flaticon

genpack's People

Contributors

dimohy avatar

Stargazers

Dima Enns avatar Lee Seung Hu avatar jeonghwan avatar lukewire avatar

Watchers

 avatar

genpack's Issues

string, list, dict 등 가변 길이를 패킷에 저장하는 방식 통일

현재

  • string은 BinaryWriter으로 문자열을 저장할 때 첫번째 패킷에 가변 길이 방식의 사이즈를 저장한다.

0_nnnnnnn
^- 첫번째 비트가 0일 경우 1바이트로 0 ~ 127의 범위를 가진다.

1_nnnnnnn 00000001
^- 첫번째 비트가 1이고 하위 값이 1일경우 128 ~ 255의 범위를 가진다.

1_nnnnnnn 00000010
^- 첫번째 비트가 1이고 하위 값이 2일 경우 256 ~ 383의 범위를 가진다.

  • list, dict의 경우 4바이트 고정 사이즈를 사용한다.

개선 방향

  • 1단계: list, dict 등 가변 길이의 유형의 경우 string과 동일한 규칙으로 사이즈를 저장하도록 한다.
  • 2단계: 이후 스키마 생성 시 옵션을 주어 다양한 방식을 택할 수 있도록 한다. (8비트 고정, 16비트 고정, 32비트 고정, 가변)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.