Looking for something specific?
  Home
Home
Articles
Page Tag-Cloud
  Software
Software Tag-Cloud
Building from Source
Open Source Definition
All Software
  Popular Tags
Legacy
C Plus Plus
Source Code
Class
Cryptography
  Members
Login
Web-Email
Notable Members
  Official
Our Company
Copyright Information
Software EULA
GPL EULA
LGPL Eula
Pre-Release EULA
Privacy Policy
  Support
Make Contact
 
NTDLS.DatagramMessaging
Downloads   0
User Rating   (Rate)
Last Updated   2/15/2024
License   MIT License
- Download -
View all Releases
Recommended Release
Version   1.3.0
Date   2/15/2024
Status   Stable Stable software is believed to be stable and ready for production use.

This software is open source. You can obtain the latest source code from the GitHub repository or browse the releases for the source code associated with a specific release. If you make any changes which you feel improves this application, please let us know via our Contact Page.

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;
    }
}

Recent Releases:
 1.3.0    1.0.1    1.0.0  

Tags:
 Datagram    Framing    Io    Network    Udp  

No comments currently exists for this software. Why don't you add one?
First Previous Next Last 

 
Copyright © 2024 NetworkDLS.
All rights reserved.
 
Privacy Policy | Our Company | Contact