Innovenergy_trunk/csharp/Lib/Utils/Net/UdpExtensions.cs

29 lines
882 B
C#

using System.Net;
using System.Net.Sockets;
namespace InnovEnergy.Lib.Utils.Net;
public static class UdpExtensions
{
public static Int32 SendDatagram(this UdpClient udpClient, Byte[] data, IPEndPoint endPoint)
{
return udpClient.Send(data, data.Length, endPoint);
}
public static Int32 SendDatagram(this UdpClient udpClient, UdpDatagram datagram)
{
var payload = datagram.Payload;
var data = payload as Byte[] ?? payload.ToArray(payload.Count);
return udpClient.Send(data, data.Length, datagram.EndPoint);
}
public static UdpDatagram ReadDatagram(this UdpClient udpClient)
{
var endPoint = new IPEndPoint(0, 0);
var datagram = udpClient.Receive(ref endPoint);
return new UdpDatagram(datagram, endPoint);
}
}
public record UdpDatagram(IReadOnlyList<Byte> Payload, IPEndPoint EndPoint);