mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-24 12:35:31 +00:00
Done UdpClient api docs
This commit is contained in:
parent
1d169b171b
commit
e97a53661e
1 changed files with 88 additions and 1 deletions
|
|
@ -4,15 +4,32 @@ using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Cosmos.System.Network
|
namespace Cosmos.System.Network
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// UdpClient class. Used to manage the UDP connection to a client.
|
||||||
|
/// </summary>
|
||||||
public class UdpClient
|
public class UdpClient
|
||||||
{
|
{
|
||||||
// TODO: Once we support more than just IPv4, we really need to base all the IPv4 classes on abstract classes
|
// 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
|
// 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
|
internal class DataGram
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Data array.
|
||||||
|
/// </summary>
|
||||||
internal byte[] data;
|
internal byte[] data;
|
||||||
|
/// <summary>
|
||||||
|
/// Source end point.
|
||||||
|
/// </summary>
|
||||||
internal IPv4.EndPoint source;
|
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)
|
internal DataGram(byte[] data, IPv4.EndPoint src)
|
||||||
{
|
{
|
||||||
this.data = data;
|
this.data = data;
|
||||||
|
|
@ -20,14 +37,33 @@ namespace Cosmos.System.Network
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Clients dictionary.
|
||||||
|
/// </summary>
|
||||||
private static TempDictionary<UdpClient> clients;
|
private static TempDictionary<UdpClient> clients;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Local port.
|
||||||
|
/// </summary>
|
||||||
protected Int32 localPort;
|
protected Int32 localPort;
|
||||||
|
/// <summary>
|
||||||
|
/// Destination address.
|
||||||
|
/// </summary>
|
||||||
protected IPv4.Address destination;
|
protected IPv4.Address destination;
|
||||||
|
/// <summary>
|
||||||
|
/// Destination port.
|
||||||
|
/// </summary>
|
||||||
protected Int32 destinationPort;
|
protected Int32 destinationPort;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// RX buffer queue.
|
||||||
|
/// </summary>
|
||||||
private Queue<DataGram> rxBuffer;
|
private Queue<DataGram> rxBuffer;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Assign clients dictionary.
|
||||||
|
/// </summary>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
|
||||||
static UdpClient()
|
static UdpClient()
|
||||||
{
|
{
|
||||||
clients = new TempDictionary<UdpClient>();
|
clients = new TempDictionary<UdpClient>();
|
||||||
|
|
@ -48,10 +84,21 @@ namespace Cosmos.System.Network
|
||||||
return null;
|
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()
|
public UdpClient()
|
||||||
:this(0)
|
: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)
|
public UdpClient(Int32 localPort)
|
||||||
{
|
{
|
||||||
this.rxBuffer = new Queue<DataGram>(8);
|
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)
|
public UdpClient(IPv4.Address dest, Int32 destPort)
|
||||||
: this(0)
|
: this(0)
|
||||||
{
|
{
|
||||||
|
|
@ -70,12 +124,25 @@ namespace Cosmos.System.Network
|
||||||
this.destinationPort = destPort;
|
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)
|
public void Connect(IPv4.Address dest, Int32 destPort)
|
||||||
{
|
{
|
||||||
this.destination = dest;
|
this.destination = dest;
|
||||||
this.destinationPort = destPort;
|
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)
|
public void Send(byte[] data)
|
||||||
{
|
{
|
||||||
if ((this.destination == null) ||
|
if ((this.destination == null) ||
|
||||||
|
|
@ -87,6 +154,15 @@ namespace Cosmos.System.Network
|
||||||
Send(data, this.destination, this.destinationPort);
|
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)
|
public void Send(byte[] data, IPv4.Address dest, Int32 destPort)
|
||||||
{
|
{
|
||||||
IPv4.Address source = IPv4.Config.FindNetwork(dest);
|
IPv4.Address source = IPv4.Config.FindNetwork(dest);
|
||||||
|
|
@ -96,6 +172,10 @@ namespace Cosmos.System.Network
|
||||||
IPv4.OutgoingBuffer.AddPacket(packet);
|
IPv4.OutgoingBuffer.AddPacket(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close connection.
|
||||||
|
/// </summary>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
|
||||||
public void Close()
|
public void Close()
|
||||||
{
|
{
|
||||||
if (UdpClient.clients.ContainsKey((UInt32)this.localPort) == true)
|
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)
|
public byte[] Receive(ref IPv4.EndPoint source)
|
||||||
{
|
{
|
||||||
if (this.rxBuffer.Count < 1)
|
if (this.rxBuffer.Count < 1)
|
||||||
|
|
@ -119,10 +205,11 @@ namespace Cosmos.System.Network
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Receive data.
|
/// Receive data from packet.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="packet">Packet to receive.</param>
|
/// <param name="packet">Packet to receive.</param>
|
||||||
/// <exception cref="OverflowException">Thrown on fatal error (contact support).</exception>
|
/// <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)
|
internal void receiveData(IPv4.UDPPacket packet)
|
||||||
{
|
{
|
||||||
byte[] data = packet.UDP_Data;
|
byte[] data = packet.UDP_Data;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue