Skip to content

Commit

Permalink
Add Contructor overloads to CoapUdpEndPoint for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Aug 24, 2017
1 parent 4480168 commit 98dad09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
10 changes: 6 additions & 4 deletions CoAPNet.Udp/CoAPNet.Udp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
<PackageId>NZSmartie.CoAPNet.Udp</PackageId>
<Version>0.2.4</Version>
<Version>0.2.5</Version>
<Authors>Roman Vaughan</Authors>
<Company>NZSmartie</Company>
<Description>UDP Socket implementation for NZSmartie.CoAPNet</Description>
Expand All @@ -14,10 +14,12 @@
<RepositoryUrl>https://github.com/NZSmartie/CoAP.Net</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>CoAP IoT sensors devices hardware network protocol udp socket</PackageTags>
<PackageReleaseNotes>v0.2.4
<PackageReleaseNotes>v0.2.5
- Add Contructor overloads to CoapUdpEndPoint for simplicity
v0.2.4
- Implemented a UDP example to use with NZSmartie.CoAPNet</PackageReleaseNotes>
<AssemblyVersion>0.2.4.0</AssemblyVersion>
<FileVersion>0.2.4.0</FileVersion>
<AssemblyVersion>0.2.5.0</AssemblyVersion>
<FileVersion>0.2.5.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
47 changes: 33 additions & 14 deletions CoAPNet.Udp/CoapUdpEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@ public class CoapUdpEndPoint : ICoapEndpoint
public bool IsSecure => false;

public CoapUdpEndPoint(UdpClient udpClient)
:this((IPEndPoint)udpClient.Client.LocalEndPoint)
{
Client = udpClient ?? throw new ArgumentNullException(nameof(udpClient));
_endpoint = (IPEndPoint)Client.Client.LocalEndPoint;

BaseUri = new UriBuilder()
{
Scheme = "coap://",
Host = _endpoint.Address.ToString(),
Port = _endpoint.Port != Coap.Port ? _endpoint.Port : -1
}.Uri;
Client = udpClient;
}

public CoapUdpEndPoint(int port = 0)
: this(new IPEndPoint(IPAddress.Any, port))
{ }

public CoapUdpEndPoint(IPAddress address, int port = 0)
: this(new IPEndPoint(address, port))
{ }

public CoapUdpEndPoint(string ipAddress, int port = 0)
:this(new IPEndPoint(IPAddress.Parse(ipAddress), port))
{ }

public CoapUdpEndPoint(IPEndPoint endpoint)
{
_endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
Expand Down Expand Up @@ -79,7 +84,7 @@ public void Dispose()
public async Task<CoapPacket> ReceiveAsync(CancellationToken token = default (CancellationToken))
{
if (Client == null)
throw new InvalidOperationException();
await BindAsync();

var result = await Client.ReceiveAsync();
return new CoapPacket
Expand All @@ -92,11 +97,25 @@ public void Dispose()
public async Task SendAsync(CoapPacket packet, CancellationToken token = default (CancellationToken))
{
if (Client == null)
throw new InvalidOperationException();
await BindAsync();

var udpDestEndpoint = packet.Endpoint as CoapUdpEndPoint;
if (udpDestEndpoint == null)
throw new ArgumentException();

CoapUdpEndPoint udpDestEndpoint;
switch (packet.Endpoint)
{
case CoapUdpEndPoint udpEndPoint:
udpDestEndpoint = udpEndPoint;
break;
case CoapEndpoint coapEndpoint:
int port = coapEndpoint.BaseUri.Port;
if (port == -1)
port = coapEndpoint.IsSecure ? Coap.PortDTLS : Coap.Port;

udpDestEndpoint = new CoapUdpEndPoint(coapEndpoint.BaseUri.Host, port);
break;
default:
throw new InvalidOperationException($"Unsupported {nameof(CoapPacket)}.{nameof(CoapPacket.Endpoint)} type ({packet.Endpoint.GetType().FullName})");
}

try
{
Expand Down

0 comments on commit 98dad09

Please sign in to comment.