PDComm.OpcUa.Server
Class
An OPC UA server with an in-memory address space, driven by the caller.
public sealed class Server : IDisposableThe server creates no background task and no callbacks: call Server.Poll regularly to accept connections and serve requests. It exposes basic variables only, added with Server.AddVariable below the Objects folder; there is no external data provider, event, history or PubSub API. Anonymous sessions are supported; user name and X.509 user authentication are not.
Calls on one server are serialized by an internal lock. Server.Close stops accepting connections and aborts open channels without a protocol shutdown notification; Server.Dispose does the same if needed and releases the server. Buffers returned by the server stay valid afterwards.
The first use of the API checks that the native library implements OPC UA ABI 0.2. If not, the call throws TypeInitializationException whose inner exception is PlatformNotSupportedException.
using var server = Server.Create(bindAddress, new ServerOptions
{
Endpoint = endpointUrl, CertificateDer = Load(certificatePath), PrivateKeyPem = key,
TrustedClientDer = Load(clientCertificatePath),
});
server.AddVariable(UaBinary.NodeId(1001), "Value", UaBinary.Int32Variant(42), VariableFlags.Writable | VariableFlags.Public);
using (var address = server.LocalAddress())
Console.WriteLine($"Listening on {Encoding.UTF8.GetString(address.Copy())}");
while (!stop.IsSet)
{
server.Poll();
stop.Wait(1);
}
server.Close();Members
AddVariable
Method
Adds a variable below the Objects folder.
public void AddVariable(byte[] nodeId, string name, byte[] variant, VariableFlags flags = VariableFlags.None)Parameters
| Name | Description |
|---|---|
nodeId | The UA Binary encoding of the variable's NodeId, with namespace index 2 (urn:pdcomm:opcua:nodes); at most 4096 bytes. |
name | The browse and display name. |
variant | The UA Binary encoding of the initial value as a typed Variant (not a Null Variant); the data type and value rank are inferred from it. At most 4 MiB. |
flags | The access options; by default read-only for authenticated sessions. |
Notes
- Throws
ObjectDisposedException: The server has been disposed. - Throws
ArgumentNullException:nodeId,nameorvariantisnull. - Throws
OpcUaException: An encoding is invalid, the NodeId is in another namespace or already exists, or the address space limits (64 MiB of values, 8192 references below a parent) would be exceeded.
Close
Method
Stops accepting connections and aborts open channels.
public void Close()Clients receive no protocol shutdown notification.
Notes
- Throws
ObjectDisposedException: The server has been disposed. - Throws
OpcUaException: The native call failed.
Create
Method
Creates a server and binds its listening socket.
public static Server Create(string bindAddress, ServerOptions options)Parameters
| Name | Description |
|---|---|
bindAddress | The numeric socket address to bind, for example 0.0.0.0:4840 or 127.0.0.1:0; separate from the advertised ServerOptions.Endpoint URL. |
options | The endpoint, identity, trust and capacity options. |
Returns
A new server, which the caller owns and must dispose.
Notes
- Throws
ArgumentNullException:bindAddressoroptionsisnull. - Throws
OpcUaException: The options are invalid, the socket cannot be bound, or no valid license grants theopcua-servermodule (OpcUaException.LicenseRequired; nothing is bound). - Throws
TypeInitializationException: The native library is missing or does not implement OPC UA ABI 0.2 (the inner exception says which).
Dispose
Method
Releases the server, aborting open channels if it is still running.
public void Dispose()A call running on another thread keeps the native server alive until it returns. Calling this method more than once has no effect.
LocalAddress
Method
Gets the socket address the server is bound to, for example to learn the port chosen for port 0.
public BinaryBuffer LocalAddress()Returns
A new buffer, which the caller must dispose, holding the UTF-8 address without a terminator.
Notes
- Throws
ObjectDisposedException: The server has been disposed. - Throws
OpcUaException: The server is closed or the native call failed.
Poll
Method
Accepts connections and serves pending requests without waiting for new data.
public void Poll()Notes
- Throws
ObjectDisposedException: The server has been disposed. - Throws
OpcUaException: The server is closed or failed.
ReadValue
Method
Reads the current value of a variable from the application.
public BinaryBuffer ReadValue(byte[] nodeId)Parameters
| Name | Description |
|---|---|
nodeId | The UA Binary encoding of the variable's NodeId. |
Returns
A new buffer, which the caller must dispose, holding the UA Binary DataValue. An unknown node yields a DataValue with status BadNodeIdUnknown, not an exception.
Notes
- Throws
ObjectDisposedException: The server has been disposed. - Throws
ArgumentNullException:nodeIdisnull. - Throws
OpcUaException: The NodeId encoding is invalid or the native call failed.
SetValue
Method
Sets the value of a variable from the application.
public uint SetValue(byte[] nodeId, byte[] variant)Parameters
| Name | Description |
|---|---|
nodeId | The UA Binary encoding of the variable's NodeId. |
variant | The UA Binary encoding of the new value as a Variant. |
Returns
The OPC UA status code of the write, for example 0 (Good) or a Bad code when the variable is unknown, not writable, or the value has the wrong type. The same writability and type checks apply as for a client write.
Notes
- Throws
ObjectDisposedException: The server has been disposed. - Throws
ArgumentNullException:nodeIdorvariantisnull. - Throws
OpcUaException: An encoding is invalid or the native call failed.