NTDLS.DatagramMessaging 1.7.1

dotnet add package NTDLS.DatagramMessaging --version 1.7.1                
NuGet\Install-Package NTDLS.DatagramMessaging -Version 1.7.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NTDLS.DatagramMessaging" Version="1.7.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NTDLS.DatagramMessaging --version 1.7.1                
#r "nuget: NTDLS.DatagramMessaging, 1.7.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NTDLS.DatagramMessaging as a Cake Addin
#addin nuget:?package=NTDLS.DatagramMessaging&version=1.7.1

// Install NTDLS.DatagramMessaging as a Cake Tool
#tool nuget:?package=NTDLS.DatagramMessaging&version=1.7.1                

NTDLS.DatagramMessaging

📦 Be sure to check out the NuGet package: https://www.nuget.org/packages/NTDLS.DatagramMessaging

NTDLS.DatagramMessaging is a set of classes and extensions methods that allow you to send/receive UDP packets with ease. It handles corruption checks, concatenation, fragmentation, serialization and compression with optional overloads.

UDP Sever (Event based):

Here we are instantiating a DmMessenger and giving it a listen port. This will cause the manager to go into listen mode. Any received messages will handled by the OnDatagramReceived event.

static void Main()
{
    var dmServer = new DmServer(1234);

    //Typically a good idea to hook the exception handler.
    //dmServer.OnException += DmServer_OnException;

    dmServer.OnDatagramReceived += UdpManager_OnDatagramReceived;

    Console.ReadLine();

    dmServer.Stop();
}

private static void UdpManager_OnDatagramReceived(DmContext context, IDmDatagram datagram)
{
    if (datagram is MyFirstUDPPacket myFirstUDPPacket)
    {
        context.Dispatch(myFirstUDPPacket); //Echo the datagram back to the sender.
        Console.WriteLine($"{myFirstUDPPacket.Message}->{myFirstUDPPacket.UID}->{myFirstUDPPacket.TimeStamp}");
    }
}

UDP Sever (Convention based):

Here we are instantiating a DmMessenger and giving it a listen port. This will cause the manager to go into listen mode. Any received messages will handled by the class HandlePackets which was suppled to the UDP messenger by a call to AddHandler().

static void Main()
{
    var dmServer = new DmServer(1234);

    //Typically a good idea to hook the exception handler.
    //dmServer.OnException += DmServer_OnException;

    dmServer.AddHandler(new HandlePackets());

    Console.ReadLine();

    dmServer.Stop();
}

private class HandlePackets : IDmMessageHandler
{
    private class HandlePackets : IDmMessageHandler
    {
        public static void DatagramHandler(DmContext context, MyFirstUDPPacket datagram)
        {
            context.Dispatch(datagram); //Echo the datagram back to the sender.
            Console.WriteLine($"{datagram.Message}->{datagram.UID}->{datagram.TimeStamp}");
        }
    }
}

UDP Client:

Here we are instantiating a DmMessenger without a a listen port. This means that this this manager is in write-only mode. We are going to loop and send frames containing serialized MyFirstUDPPacket.

static void Main()
{
    var dmClient = new DmClient("127.0.0.1", 1234);

    //Typically a good idea to hook the exception handler.
    //dmClient.OnException += DmServer_OnException;

    int packetNumber = 0;

    while (true)
    {
        dmClient.Dispatch(new MyFirstUDPPacket($"Packet#:{packetNumber++} "));
        Thread.Sleep(10);
    }

    Console.ReadLine();

    dmClient.Stop();
}

Supporting Code:

The class that we are going to be serializing and deserializing in the examples.

public class MyFirstUDPPacket: IDmDatagram
{
    public DateTime TimeStamp { get; set; } = DateTime.UtcNow;
    public Guid UID { get; set; } = Guid.NewGuid();
    public string Message { get; set; } = string.Empty;

    public MyFirstUDPPacket()
    {
    }

    public MyFirstUDPPacket(string message)
    {
        Message = message;
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.7.1 0 3/11/2025
1.7.0 0 3/11/2025
1.6.2 48 3/10/2025
1.6.1 48 3/10/2025
1.6.0 50 3/10/2025
1.5.2 45 3/10/2025
1.5.1 40 3/10/2025
1.5.0 33 3/10/2025
1.4.0 162 3/8/2025
1.3.7 84 3/2/2025
1.3.6 72 1/8/2025
1.3.5 99 1/4/2025
1.3.4 110 1/4/2025
1.3.3 111 12/31/2024
1.3.2 103 12/3/2024
1.3.1 277 2/19/2024
1.3.0 251 2/15/2024
1.2.0 248 2/15/2024

Refactoring.