Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dimohy/GenPack
Packet generation and serialization/deserialization library using the .NET Source Generator
https://github.com/dimohy/GenPack
csharp-sourcegenerator
Last synced: 24 days ago
JSON representation
Packet generation and serialization/deserialization library using the .NET Source Generator
- Host: GitHub
- URL: https://github.com/dimohy/GenPack
- Owner: dimohy
- License: mit
- Created: 2024-03-29T03:28:15.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-04-12T00:54:04.000Z (8 months ago)
- Last Synced: 2024-11-13T00:43:47.274Z (30 days ago)
- Topics: csharp-sourcegenerator
- Language: C#
- Homepage:
- Size: 91.8 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- RSCG_Examples - https://github.com/dimohy/GenPack
README
# GenPack
[![latest version](https://img.shields.io/nuget/v/GenPack)](https://www.nuget.org/packages/GenPack)
[![downloads](https://img.shields.io/nuget/dt/GenPack)](https://www.nuget.org/packages/GenPack)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
```csharp
[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.
```csharp
public partial record PeoplePacket : GenPack.IGenPackable
{
///
/// Age description
///
public short Age { get; set; }
///
/// Name description
///
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()`.
```csharp
var p = new PeoplePacket()
{
Age = 10,
Name = "John"
};
var data = p.ToPacket();
var newP = PeoplePacket.FromPacket(data);Console.WriteLine(newP);
``````shell
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`.```csharp
[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,```csharp
///
/// Age description
///
public short Age { get; set; }
```
You can then use the auto-generated properties.```csharp
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\ | genpackable object | N | property name, description |
| @list\ | variable list | N | property name, description |
| @dict\ | variable dictionary | N | property name, description |
| @array\ | 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](https://www.flaticon.com/kr/free-icon/blocks_2021305)