Table of Contents

Class CoapServer

Namespace
Pamoja.Coap
Assembly
Pamoja.Coap.dll

A CoAP server: the end that nodes send their readings to and observe their commands on.

public sealed class CoapServer : IDisposable
Inheritance
CoapServer
Implements
Inherited Members

Remarks

A PUT or POST to a path matching one of its SubscribeAsync(string) filters is answered 2.04 Changed and delivered to ReceiveAsync(), and one to any other path is answered 4.04 Not Found. SendAsync(string, string) sets a resource's state, which a GET reads and every observer is notified of. A send does not wait for a receive that is waiting, so a gateway sends commands while it listens for readings; the other calls on one server run one at a time.

Constructors

CoapServer(string)

Creates a server that will listen on a local address.

public CoapServer(string bind)

Parameters

bind string

The local host:port, such as 0.0.0.0:5683, or port 0 for a free one.

Exceptions

PamojaException

The native server could not be created.

Properties

IsConnected

Gets whether the server holds a bound socket.

public bool IsConnected { get; }

Property Value

bool

LocalPort

Gets the port the server listens on, or null while it is not connected.

public ushort? LocalPort { get; }

Property Value

ushort?

Remarks

This names the port the system chose when the server bound port 0.

Methods

ConnectAsync()

Binds the socket and starts answering requests.

public Task ConnectAsync()

Returns

Task

Exceptions

PamojaException

The address could not be bound.

DisconnectAsync()

Closes the socket, keeping the resources and filters.

public Task DisconnectAsync()

Returns

Task

Exceptions

PamojaException

The native call failed.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Observers(string)

Counts the clients observing a resource.

public int Observers(string path)

Parameters

path string

The resource's path.

Returns

int

How many observers the resource has.

ReceiveAsync()

Waits for the next reading sent to a path the server takes.

public Task<TransportMessage?> ReceiveAsync()

Returns

Task<TransportMessage>

The reading, carrying the path it was sent to.

Exceptions

PamojaException

The server is not connected.

ReceiveAsync(TimeSpan)

Waits a limited time for the next reading sent to a path the server takes.

public Task<TransportMessage?> ReceiveAsync(TimeSpan timeout)

Parameters

timeout TimeSpan

How long to wait.

Returns

Task<TransportMessage>

The reading.

Remarks

When the time runs out nothing is lost: a reading arriving afterwards waits for the next receive.

Exceptions

TimeoutException

No reading arrived in time.

PamojaException

The server is not connected.

SendAsync(string, ReadOnlyMemory<byte>)

Sets a resource's state and notifies every observer of it.

public Task SendAsync(string path, ReadOnlyMemory<byte> payload)

Parameters

path string

The resource's path.

payload ReadOnlyMemory<byte>

Its new state, which a GET now reads.

Returns

Task

Exceptions

PamojaException

The server is not connected.

SendAsync(string, string)

Sets a resource's state to text, such as a command, and notifies its observers.

public Task SendAsync(string path, string text)

Parameters

path string

The resource's path.

text string

Its new state, as UTF-8.

Returns

Task

Exceptions

PamojaException

The server is not connected.

SubscribeAsync(string)

Takes the readings sent to the paths a filter matches.

public Task SubscribeAsync(string filter)

Parameters

filter string

The filter, with + for one level and # for the rest.

Returns

Task

Exceptions

PamojaException

The native call failed.