Done UdpClient api docs

This commit is contained in:
Elia Sulimanov 2020-06-17 14:27:11 +03:00
parent 1d169b171b
commit e97a53661e

View file

@ -4,15 +4,32 @@ using System.Collections.Generic;
namespace Cosmos.System.Network
{
/// <summary>
/// UdpClient class. Used to manage the UDP connection to a client.
/// </summary>
public class UdpClient
{
// TODO: Once we support more than just IPv4, we really need to base all the IPv4 classes on abstract classes
// that represent the required functionality, then we can generalize the stack to be independent from IPv4 or IPv6
/// <summary>
/// Datagram class.
/// </summary>
internal class DataGram
{
/// <summary>
/// Data array.
/// </summary>
internal byte[] data;
/// <summary>
/// Source end point.
/// </summary>
internal IPv4.EndPoint source;
/// <summary>
/// Create new inctanse of the <see cref="DataGram"/> class.
/// </summary>
/// <param name="data">Data array.</param>
/// <param name="src">Source end point.</param>
internal DataGram(byte[] data, IPv4.EndPoint src)
{
this.data = data;
@ -20,14 +37,33 @@ namespace Cosmos.System.Network
}
}
/// <summary>
/// Clients dictionary.
/// </summary>
private static TempDictionary<UdpClient> clients;
/// <summary>
/// Local port.
/// </summary>
protected Int32 localPort;
/// <summary>
/// Destination address.
/// </summary>
protected IPv4.Address destination;
/// <summary>
/// Destination port.
/// </summary>
protected Int32 destinationPort;
/// <summary>
/// RX buffer queue.
/// </summary>
private Queue<DataGram> rxBuffer;
/// <summary>
/// Assign clients dictionary.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
static UdpClient()
{
clients = new TempDictionary<UdpClient>();
@ -48,10 +84,21 @@ namespace Cosmos.System.Network
return null;
}
/// <summary>
/// Create new inctanse of the <see cref="UdpClient"/> class.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
/// <exception cref="ArgumentException">Thrown if UdpClient with localPort 0 exists.</exception>
public UdpClient()
:this(0)
{ }
/// <summary>
/// Create new inctanse of the <see cref="UdpClient"/> class.
/// </summary>
/// <param name="localPort">Local port.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
/// <exception cref="ArgumentException">Thrown if localPort already exists.</exception>
public UdpClient(Int32 localPort)
{
this.rxBuffer = new Queue<DataGram>(8);
@ -63,6 +110,13 @@ namespace Cosmos.System.Network
}
}
/// <summary>
/// Create new inctanse of the <see cref="UdpClient"/> class.
/// </summary>
/// <param name="dest">Destination address.</param>
/// <param name="destPort">Destination port.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
/// <exception cref="ArgumentException">Thrown if UdpClient with localPort 0 exists.</exception>
public UdpClient(IPv4.Address dest, Int32 destPort)
: this(0)
{
@ -70,12 +124,25 @@ namespace Cosmos.System.Network
this.destinationPort = destPort;
}
/// <summary>
/// Connect to client.
/// </summary>
/// <param name="dest">Destination address.</param>
/// <param name="destPort">Destination port.</param>
public void Connect(IPv4.Address dest, Int32 destPort)
{
this.destination = dest;
this.destinationPort = destPort;
}
/// <summary>
/// Send data to client.
/// </summary>
/// <param name="data">Data array to send.</param>
/// <exception cref="Exception">Thrown if destination is null or destinationPort is 0.</exception>
/// <exception cref="ArgumentException">Thrown on fatal error (contact support).</exception>
/// <exception cref="OverflowException">Thrown if data array length is greater than Int32.MaxValue.</exception>
/// <exception cref="Sys.IO.IOException">Thrown on IO error.</exception>
public void Send(byte[] data)
{
if ((this.destination == null) ||
@ -87,6 +154,15 @@ namespace Cosmos.System.Network
Send(data, this.destination, this.destinationPort);
}
/// <summary>
/// Send data.
/// </summary>
/// <param name="data">Data array.</param>
/// <param name="dest">Destination address.</param>
/// <param name="destPort">Destination port.</param>
/// <exception cref="ArgumentException">Thrown on fatal error (contact support).</exception>
/// <exception cref="OverflowException">Thrown if data array length is greater than Int32.MaxValue.</exception>
/// <exception cref="Sys.IO.IOException">Thrown on IO error.</exception>
public void Send(byte[] data, IPv4.Address dest, Int32 destPort)
{
IPv4.Address source = IPv4.Config.FindNetwork(dest);
@ -96,6 +172,10 @@ namespace Cosmos.System.Network
IPv4.OutgoingBuffer.AddPacket(packet);
}
/// <summary>
/// Close connection.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
public void Close()
{
if (UdpClient.clients.ContainsKey((UInt32)this.localPort) == true)
@ -104,6 +184,12 @@ namespace Cosmos.System.Network
}
}
/// <summary>
/// Receive data from end point.
/// </summary>
/// <param name="source">Source end point.</param>
/// <returns>byte array value.</returns>
/// <exception cref="InvalidOperationException">Thrown on fatal error (contact support).</exception>
public byte[] Receive(ref IPv4.EndPoint source)
{
if (this.rxBuffer.Count < 1)
@ -119,10 +205,11 @@ namespace Cosmos.System.Network
}
/// <summary>
/// Receive data.
/// Receive data from packet.
/// </summary>
/// <param name="packet">Packet to receive.</param>
/// <exception cref="OverflowException">Thrown on fatal error (contact support).</exception>
/// <exception cref="Sys.IO.IOException">Thrown on IO error.</exception>
internal void receiveData(IPv4.UDPPacket packet)
{
byte[] data = packet.UDP_Data;