Skip to content
Start free evaluation

pdcomm::opcua::server

Class

In-memory OPC UA server with variables in namespace 2.

class server : public owned

Declared in: #include <pdcomm/opcua/pdcomm_opcua.hpp>

The server serves clients only during poll(); call it regularly from the thread that owns the object. Destroying the server stops it like close().

From examples/opcua/opcua_server.cpp:

auto config = ua::server_options();
config.endpoint = ua::bytes(endpoint);
config.certificate_der = ua::bytes(certificate);
config.private_key_pem = ua::bytes(key);
config.trusted_client_der = ua::bytes(trust);
auto server = ua::server::create(argv[1], config);
std::fill(key.begin(), key.end(), static_cast<unsigned char>(0));  // The SDK copied the identity.
server.add_variable(node_id(1001), "Value", int32_variant(42),
                    PDCOMM_OPCUA_VARIABLE_WRITABLE | PDCOMM_OPCUA_VARIABLE_PUBLIC);
server.add_variable(node_id(1002), "Limit", int32_variant(100), PDCOMM_OPCUA_VARIABLE_PUBLIC);
server.add_variable(node_id(1003), "Protected", int32_variant(7), PDCOMM_OPCUA_VARIABLE_WRITABLE);
const auto address = server.local_address().copy();
// ...
while (!stop_requested &&
       (!seconds || std::chrono::steady_clock::now() - started < std::chrono::seconds(seconds))) {
  server.poll();
  std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
const auto snapshot = server.read_value(node_id(1001)).copy();
// ...
server.close();

Notes

  • Use one server from one thread at a time: a concurrent call throws error with #PDCOMM_OPCUA_BUSY.

Members

add_variable

Method

Adds a Variable node below the Objects folder.

void add_variable(const std::vector<uint8_t>& id, const std::string& name, const std::vector<uint8_t>& value, uint32_t flags = 0)

Declared in: #include <pdcomm/opcua/pdcomm_opcua.hpp>

See pdcomm_opcua_server_add_variable().

Parameters

NameDescription
idEncoded NodeId in namespace 2.
nameUTF-8 BrowseName and DisplayName.
valueEncoded, typed initial Variant.
flagsBitwise OR of ::pdcomm_opcua_variable_flags values; the default 0 makes a read-only variable for authenticated users.

Notes

  • Throws error: if an input is invalid, the NodeId exists or a limit is reached.

close

Method

Closes the listener and aborts every channel.

void close()

Declared in: #include <pdcomm/opcua/pdcomm_opcua.hpp>

Notes

  • Throws error: if the server is already closed or busy.

create

Method

Creates and binds a server.

static server create(const std::string& bind_address, const pdcomm_opcua_server_options& config)

Declared in: #include <pdcomm/opcua/pdcomm_opcua.hpp>

See pdcomm_opcua_server_create(). The identity and the trusted client certificate are copied during the call.

Parameters

NameDescription
bind_addressNumeric socket address such as 127.0.0.1:4840; port 0 picks a free port.
configOptions from server_options() with the endpoint URL set.

Returns

The listening server.

Notes

  • Throws error: if the ABI version differs, an option is invalid, the certificate does not match the application URI or binding fails.

local_address

Method

Returns the socket address the server listens on.

buffer local_address() const

Declared in: #include <pdcomm/opcua/pdcomm_opcua.hpp>

Returns

A buffer with UTF-8 text such as 127.0.0.1:50123.

Notes

  • Throws error: if the server is closed or busy.

poll

Method

Accepts connections and processes requests once, without blocking.

void poll()

Declared in: #include <pdcomm/opcua/pdcomm_opcua.hpp>

Notes

  • Throws error: if the server fails; it is closed afterwards.

read_value

Method

Reads the Value attribute as a local, authenticated user.

buffer read_value(const std::vector<uint8_t>& id) const

Declared in: #include <pdcomm/opcua/pdcomm_opcua.hpp>

Parameters

NameDescription
idEncoded NodeId in namespace 2.

Returns

A buffer with the encoded DataValue; an unknown node yields a DataValue with BadNodeIdUnknown.

Notes

  • Throws error: if the input is invalid or the server is closed or busy.

set_value

Method

Writes the Value attribute as a local, authenticated user.

uint32_t set_value(const std::vector<uint8_t>& id, const std::vector<uint8_t>& value)

Declared in: #include <pdcomm/opcua/pdcomm_opcua.hpp>

Parameters

NameDescription
idEncoded NodeId in namespace 2.
valueEncoded Variant of the variable's type.

Returns

The OPC UA StatusCode of the write, such as Good, BadNotWritable or BadTypeMismatch; a Bad code does not throw.

Notes

  • Throws error: if an input is invalid or the server is closed or busy.