Skip to main content

GenPack by dimohy

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: GenPack

Packet generation and serialization/deserialization library using the .NET Source Generator

Author: dimohy

NuGet: https://www.nuget.org/packages/GenPack/

You can find more details at https://github.com/dimohy/GenPack

Source : https://github.com/dimohy/GenPack

Original Readme

note

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

PropertyDescriptionBitsArguments
@bytebyte8property name, description
@sbytesigned byte8property name, description
@shortshort int16property name, description
@ushortunsigned short int16property name, description
@intint32property name, description
@uintunsigned int32property name, description
@longlong int64property name, description
@ulongunsigned long int64property name, description
@floatsingle float32property name, description
@doubledouble float64property name, description
@stringstringNproperty name, description
@object\<type>genpackable objectNproperty name, description
@list\<type>variable listNproperty name, description
@dict\<type>variable dictionaryNproperty name, description
@array\<type>fixed arrayNproperty 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

About

note

Generating Binary Serialization and properties for class

How to use

Example ( source csproj, source files )

This is the CSharp Project that references GenPack

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GenPack" Version=" 0.9.0-preview1" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

#pragma warning disable CS0219
namespace SerializerDemo
{
public partial record Person : GenPack.IGenPackable
{
/// <summary>
/// Age description
/// </summary>
public short Id { 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(Id);
writer.Write(Name);
}
public static SerializerDemo.Person FromPacket(byte[] data)
{
using var ms = new System.IO.MemoryStream(data);
return FromPacket(ms);
}
public static SerializerDemo.Person FromPacket(System.IO.Stream stream)
{
SerializerDemo.Person result = new SerializerDemo.Person();
System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);
int size = 0;
byte[] buffer = null;
result.Id = reader.ReadInt16();
result.Name = reader.ReadString();
return result;
}
}
}

Usefull

Download Example (.NET C# )

Share GenPack

https://ignatandrei.github.io/RSCG_Examples/v2/docs/GenPack

In the same category (Serializer) - 4 other generators

jsonConverterSourceGenerator

JsonPolymorphicGenerator

ProtobufSourceGenerator

System.Text.Json