NTDLS.DatagramMessaging 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package NTDLS.DatagramMessaging --version 1.3.0                
NuGet\Install-Package NTDLS.DatagramMessaging -Version 1.3.0                
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.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NTDLS.DatagramMessaging --version 1.3.0                
#r "nuget: NTDLS.DatagramMessaging, 1.3.0"                
#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.3.0

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

NTDLS.DatagramMessaging

📦 Be sure to check out the NuGet pacakge: 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 adds compression.

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 OnNotificationReceived event.

static void Main()
{
    var udpManager = new DmMessenger(1234);

    udpManager.OnNotificationReceived += UdpManager_OnNotificationReceived;
}

private static void UdpManager_OnNotificationReceived(DmContext context, IDmNotification payload)
{
    if (payload is MyFirstUDPPacket myFirstUDPPacket)
    {
        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 udpManager = new DmMessenger(1234);

        udpManager.AddHandler(new HandlePackets());
    }

    private class HandlePackets : IDmMessageHandler
    {
        public static void ProcessFrameNotificationCallback(DmContext context, MyFirstUDPPacket payload)
        {
            Console.WriteLine($"{payload.Message}->{payload.UID}->{payload.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 dmMessenger = new DmMessenger();

    int packetNumber = 0;

    while (true)
    {
        dmMessenger.WriteMessage("127.0.0.1", 1234,
            new MyFirstUDPPacket($"Packet#:{packetNumber++} "));

        Thread.Sleep(100);
    }
}

Supporting Code:

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

public class MyFirstUDPPacket: IDmNotification
{
    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 net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
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.3.1 257 2/19/2024
1.3.0 235 2/15/2024
1.2.0 233 2/15/2024

Added convention based message handling.