open62541++ 0.13.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
Server.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <memory>
5#include <string>
6#include <string_view>
7#include <vector>
8
9#include "open62541pp/Common.h" // NamespaceIndex
10#include "open62541pp/Config.h"
11#include "open62541pp/Logger.h"
12#include "open62541pp/NodeIds.h"
13#include "open62541pp/Span.h"
16
17// forward declaration
18namespace opcua {
19class AccessControlBase;
20class ByteString;
21class DataType;
22class Event;
23template <typename Connection>
24class Node;
25class NodeId;
26class Server;
27class Session;
28struct ValueBackendDataSource;
29struct ValueCallback;
30
31namespace detail {
32struct ServerConnection;
33struct ServerContext;
34} // namespace detail
35
36/* -------------------------------------- Helper functions -------------------------------------- */
37
38namespace detail {
39
41UA_ServerConfig& getConfig(Server& server) noexcept;
42
43ServerConnection* getConnection(UA_Server* server) noexcept;
44ServerConnection& getConnection(Server& server) noexcept;
45
46Server* getWrapper(UA_Server* server) noexcept;
47
49ServerContext& getContext(Server& server) noexcept;
50
51} // namespace detail
52
53/* ------------------------------------------- Server ------------------------------------------- */
54
55/**
56 * High-level server class.
57 *
58 * Exposes the most common functionality. Use the handle() method to get access the underlying
59 * UA_Server instance and use the full power of open6254.
60 */
61class Server {
62public:
63 /**
64 * Create server with default configuration (no encryption).
65 * Security policies:
66 * - [None](http://opcfoundation.org/UA/SecurityPolicy#None)
67 *
68 * @param port Port number
69 * @param certificate Optional X.509 v3 certificate in `DER` encoded format
70 * @param logger Custom log function. If the passed function is empty, the default logger is
71 * used.
72 */
73 explicit Server(uint16_t port = 4840, ByteString certificate = {}, Logger logger = nullptr);
74
75#ifdef UA_ENABLE_ENCRYPTION
76 /**
77 * Create server with encryption enabled (PKI).
78 * Security policies:
79 * - [None](http://opcfoundation.org/UA/SecurityPolicy#None)
80 * - [Basic128Rsa15](http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15)
81 * - [Basic256](http://opcfoundation.org/UA/SecurityPolicy#Basic256)
82 * - [Basic256Sha256](http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256)
83 * - [Aes128_Sha256_RsaOaep](http://opcfoundation.org/UA/SecurityPolicy#Aes128_Sha256_RsaOaep)
84 *
85 * @param port Port number
86 * @param certificate X.509 v3 certificate in `DER` encoded format
87 * @param privateKey Private key in `PEM` encoded format
88 * @param trustList List of trusted certificates in `DER` encoded format
89 * @param issuerList List of issuer certificates (i.e. CAs) in `DER` encoded format
90 * @param revocationList Certificate revocation lists (CRL) in `DER` encoded format
91 *
92 * @see https://reference.opcfoundation.org/Core/Part2/v105/docs/8
93 * @see https://reference.opcfoundation.org/Core/Part4/v105/docs/6.1
94 * @see https://reference.opcfoundation.org/Core/Part6/v105/docs/6.2
95 */
97 uint16_t port,
98 const ByteString& certificate,
99 const ByteString& privateKey,
100 Span<const ByteString> trustList,
101 Span<const ByteString> issuerList,
102 Span<const ByteString> revocationList = {}
103 );
104#endif
105
107
108 Server(const Server&) = delete;
109 Server(Server&&) noexcept = default;
110 Server& operator=(const Server&) = delete;
111 Server& operator=(Server&&) noexcept = default;
112
113 /// Set custom logging function.
114 /// Does nothing if the passed function is empty or a nullptr.
115 void setLogger(Logger logger);
116
117 /// Set custom access control.
118 /// @note Supported since open62541 v1.3
120 /// Set custom access control (transfer ownership to Server).
121 /// @note Supported since open62541 v1.3
122 void setAccessControl(std::unique_ptr<AccessControlBase> accessControl);
123
124 /// Set custom hostname, default: system's host name.
125 void setCustomHostname(std::string_view hostname);
126 /// Set application name, default: `open62541-based OPC UA Application`.
127 void setApplicationName(std::string_view name);
128 /// Set application URI, default: `urn:open62541.server.application`.
129 void setApplicationUri(std::string_view uri);
130 /// Set product URI, default: `http://open62541.org`.
131 void setProductUri(std::string_view uri);
132
133 /// Get active server session.
134 /// @note Supported since open62541 v1.3
135 std::vector<Session> getSessions();
136
137 /// Get all defined namespaces.
138 std::vector<std::string> getNamespaceArray();
139 /// Register namespace. The new namespace index will be returned.
140 [[nodiscard]] NamespaceIndex registerNamespace(std::string_view uri);
141
142 /// Set custom data types.
143 /// All data types provided are automatically considered for decoding of received messages.
144 void setCustomDataTypes(std::vector<DataType> dataTypes);
145
146 /// Set value callbacks to execute before every read and after every write operation.
148 /// Set data source backend for variable node.
150
151#ifdef UA_ENABLE_SUBSCRIPTIONS
152 /// Create a (pseudo) subscription to monitor local data changes and events.
154#endif
155
156#ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
157 /// Create an event object to generate and trigger events.
159#endif
160
161 /// Run a single iteration of the server's main loop.
162 /// @return Maximum wait period until next Server::runIterate call (in ms)
163 uint16_t runIterate();
164 /// Run the server's main loop. This method will block until Server::stop is called.
165 void run();
166 /// Stop the server's main loop.
167 void stop();
168 /// Check if the server is running.
169 bool isRunning() const noexcept;
170
176
177 UA_Server* handle() noexcept;
178 const UA_Server* handle() const noexcept;
179
180private:
181 friend detail::ServerConnection& detail::getConnection(Server& server) noexcept;
182
183 std::unique_ptr<detail::ServerConnection> connection_;
184};
185
186inline bool operator==(const Server& lhs, const Server& rhs) noexcept {
187 return (lhs.handle() == rhs.handle());
188}
189
190inline bool operator!=(const Server& lhs, const Server& rhs) noexcept {
191 return !(lhs == rhs);
192}
193
194} // namespace opcua
Access control base class.
UA_ByteString wrapper class.
Definition Builtin.h:172
UA_DataType wrapper class.
Definition DataType.h:20
Create and trigger events.
Definition Event.h:23
UA_NodeId wrapper class.
Definition NodeId.h:36
High-level node class to access node attribute, browse and populate address space.
Definition Node.h:42
High-level server class.
Definition Server.h:61
void setVariableNodeValueBackend(const NodeId &id, ValueBackendDataSource backend)
Set data source backend for variable node.
std::vector< Session > getSessions()
Get active server session.
Node< Server > getNode(NodeId id)
Node< Server > getObjectsNode()
void setApplicationName(std::string_view name)
Set application name, default: open62541-based OPC UA Application.
void setProductUri(std::string_view uri)
Set product URI, default: http://open62541.org.
NamespaceIndex registerNamespace(std::string_view uri)
Register namespace. The new namespace index will be returned.
void setCustomHostname(std::string_view hostname)
Set custom hostname, default: system's host name.
void stop()
Stop the server's main loop.
void setCustomDataTypes(std::vector< DataType > dataTypes)
Set custom data types.
Server(uint16_t port, const ByteString &certificate, const ByteString &privateKey, Span< const ByteString > trustList, Span< const ByteString > issuerList, Span< const ByteString > revocationList={})
Create server with encryption enabled (PKI).
Server(Server &&) noexcept=default
void run()
Run the server's main loop. This method will block until Server::stop is called.
uint16_t runIterate()
Run a single iteration of the server's main loop.
Node< Server > getRootNode()
Subscription< Server > createSubscription() noexcept
Create a (pseudo) subscription to monitor local data changes and events.
Node< Server > getViewsNode()
Server(uint16_t port=4840, ByteString certificate={}, Logger logger=nullptr)
Create server with default configuration (no encryption).
void setAccessControl(AccessControlBase &accessControl)
Set custom access control.
Event createEvent(const NodeId &eventType=ObjectTypeId::BaseEventType)
Create an event object to generate and trigger events.
Node< Server > getTypesNode()
void setApplicationUri(std::string_view uri)
Set application URI, default: urn:open62541.server.application.
void setVariableNodeValueCallback(const NodeId &id, ValueCallback callback)
Set value callbacks to execute before every read and after every write operation.
friend detail::ServerConnection & detail::getConnection(Server &server) noexcept
bool isRunning() const noexcept
Check if the server is running.
Server(const Server &)=delete
std::vector< std::string > getNamespaceArray()
Get all defined namespaces.
UA_Server * handle() noexcept
void setLogger(Logger logger)
Set custom logging function.
High-level session class to manage client sessions.
Definition Session.h:23
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition Span.h:27
High-level subscription class.
ClientContext * getContext(UA_Client *client) noexcept
UA_ClientConfig * getConfig(UA_Client *client) noexcept
Client * getWrapper(UA_Client *client) noexcept
ClientConnection * getConnection(UA_Client *client) noexcept
std::function< void(LogLevel, LogCategory, std::string_view msg)> Logger
Log function signature.
Definition Logger.h:44
uint16_t NamespaceIndex
Namespace index.
Definition Common.h:11
bool operator!=(const Client &lhs, const Client &rhs) noexcept
Definition Client.h:204
@ DataType
The NodeId of the data type definition for the Value attribute.
Data source backend for variable nodes.
Value callbacks for variable nodes.
Internal storage for Server class.