pdcomm::opcua::server
Class
In-memory OPC UA server with variables in namespace 2.
class server : public ownedThe 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)See pdcomm_opcua_server_add_variable().
Parameters
| Name | Description |
|---|---|
id | Encoded NodeId in namespace 2. |
name | UTF-8 BrowseName and DisplayName. |
value | Encoded, typed initial Variant. |
flags | Bitwise 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()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)See pdcomm_opcua_server_create(). The identity and the trusted client certificate are copied during the call.
Parameters
| Name | Description |
|---|---|
bind_address | Numeric socket address such as 127.0.0.1:4840; port 0 picks a free port. |
config | Options 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() constReturns
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()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) constParameters
| Name | Description |
|---|---|
id | Encoded 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)Parameters
| Name | Description |
|---|---|
id | Encoded NodeId in namespace 2. |
value | Encoded 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.