// SPDX-License-Identifier: AGPL-3.0-only
/**
* Digital Voice Modem - Fixed Network Equipment Core Library
* AGPLv3 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / Fixed Network Equipment Core Library
* @license AGPLv3 License (https://opensource.org/licenses/AGPL-3.0)
*
* Copyright (C) 2022 Bryan Biedenkapp, N2PLL
*
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace fnecore
{
///
/// Structure representing a raw UDP packet frame.
///
/// "Frame" is used loosely here...
public struct UdpFrame
{
///
///
///
public IPEndPoint Endpoint;
///
///
///
public byte[] Message;
} // public struct UDPFrame
///
/// Base class from which all UDP classes are derived.
///
public abstract class UdpBase
{
protected UdpClient client;
/*
** Methods
*/
///
/// Initializes a new instance of the class.
///
protected UdpBase()
{
client = new UdpClient();
}
///
///
///
///
public async Task Receive()
{
UdpReceiveResult res = await client.ReceiveAsync();
return new UdpFrame()
{
Message = res.Buffer,
Endpoint = res.RemoteEndPoint
};
}
} // public abstract class UDPBase
///
/// Class implementing a UDP listener (server).
///
public class UdpListener : UdpBase
{
private IPEndPoint listen;
/*
** Properties
*/
///
/// Gets the for this .
///
public IPEndPoint EndPoint => listen;
/*
** Methods
*/
///
/// Initializes a new instance of the class.
///
///
///
public UdpListener(string address, int port) : this(new IPEndPoint(IPAddress.Parse(address), port))
{
/* stub */
}
///
/// Initializes a new instance of the class.
///
///
public UdpListener(IPEndPoint endpoint)
{
listen = endpoint;
client = new UdpClient(listen);
}
///
///
///
///
public void Send(UdpFrame frame)
{
client.Send(frame.Message, frame.Message.Length, frame.Endpoint);
}
///
///
///
///
public async Task SendAsync(UdpFrame frame)
{
return await client.SendAsync(frame.Message, frame.Message.Length, frame.Endpoint);
}
} // public class UdpListener : UdpBase
///
///
///
public class UdpReceiver : UdpBase
{
private IPEndPoint endpoint;
/*
** Properties
*/
///
/// Gets the for this .
///
public IPEndPoint EndPoint => endpoint;
/*
** Methods
*/
///
/// Initializes a new instance of the class.
///
public UdpReceiver()
{
/* stub */
}
///
///
///
///
///
///
public void Connect(string hostName, int port)
{
try
{
try
{
endpoint = new IPEndPoint(IPAddress.Parse(hostName), port);
}
catch
{
IPHostEntry entry = Dns.GetHostEntry(hostName);
if (entry.AddressList.Length > 0)
{
IPAddress address = entry.AddressList[0];
endpoint = new IPEndPoint(address, port);
}
}
}
catch
{
return;
}
client.Connect(endpoint.Address.ToString(), endpoint.Port);
}
///
///
///
///
///
public void Connect(IPEndPoint endpoint)
{
UdpReceiver recv = new UdpReceiver();
this.endpoint = endpoint;
client.Connect(endpoint.Address.ToString(), endpoint.Port);
}
///
///
///
///
public void Send(UdpFrame frame)
{
client.Send(frame.Message, frame.Message.Length);
}
} // public class UdpReceiver : UdpBase
} // namespace fnecore