open62541pp 0.19.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
server.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <memory>
5#include <optional>
6#include <string_view>
7#include <utility> // move
8#include <vector>
9
10#include "open62541pp/common.hpp" // NamespaceIndex
11#include "open62541pp/config.hpp"
16#include "open62541pp/span.hpp"
17#include "open62541pp/subscription.hpp" // TODO: remove with Server::createSubscription
18#include "open62541pp/types.hpp"
22
25#include "open62541pp/plugin/log_default.hpp" // LogFunction
27
28namespace opcua {
29
30/* ---------------------------------------- ServerConfig ---------------------------------------- */
31
32template <>
34 static UA_ServerConfig copy(const UA_ServerConfig& config);
35 static UA_ServerConfig move(UA_ServerConfig&& config) noexcept;
36 static void clear(UA_ServerConfig& config) noexcept;
37};
38
39/**
40 * Server configuration.
41 * @see UA_ServerConfig
42 */
43class ServerConfig : public Wrapper<UA_ServerConfig> {
44public:
45 /**
46 * Create server config with default configuration.
47 * Security policies:
48 * - [None](http://opcfoundation.org/UA/SecurityPolicy#None)
49 */
51
52 /**
53 * Create server config with minimal configuration.
54 * Security policies:
55 * - [None](http://opcfoundation.org/UA/SecurityPolicy#None)
56 *
57 * @param port Port number
58 * @param certificate Optional X.509 v3 certificate in `DER` encoded format
59 */
60 explicit ServerConfig(uint16_t port, const ByteString& certificate = {});
61
62#ifdef UA_ENABLE_ENCRYPTION
63 /**
64 * Create server config with encryption enabled (PKI).
65 * Security policies:
66 * - [None](http://opcfoundation.org/UA/SecurityPolicy#None)
67 * - [Basic128Rsa15](http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15)
68 * - [Basic256](http://opcfoundation.org/UA/SecurityPolicy#Basic256)
69 * - [Basic256Sha256](http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256)
70 * - [Aes128_Sha256_RsaOaep](http://opcfoundation.org/UA/SecurityPolicy#Aes128_Sha256_RsaOaep)
71 *
72 * @param port Port number
73 * @param certificate X.509 v3 certificate in `DER` encoded format
74 * @param privateKey Private key in `PEM` encoded format
75 * @param trustList List of trusted certificates in `DER` encoded format
76 * @param issuerList List of issuer certificates (i.e. CAs) in `DER` encoded format
77 * @param revocationList Certificate revocation lists (CRL) in `DER` encoded format
78 *
79 * @see https://reference.opcfoundation.org/Core/Part2/v105/docs/9
80 * @see https://reference.opcfoundation.org/Core/Part4/v105/docs/6.1
81 * @see https://reference.opcfoundation.org/Core/Part6/v105/docs/6.2
82 */
84 uint16_t port,
85 const ByteString& certificate,
86 const ByteString& privateKey,
87 Span<const ByteString> trustList,
88 Span<const ByteString> issuerList,
89 Span<const ByteString> revocationList = {}
90 );
91#endif
92
94 : Wrapper{std::move(native)} {} // NOLINT
95
96 ServerConfig(const ServerConfig&) = delete;
97 ServerConfig(ServerConfig&&) noexcept = default;
98 ServerConfig& operator=(const ServerConfig&) = delete;
99 ServerConfig& operator=(ServerConfig&&) noexcept = default;
100
101 ~ServerConfig() noexcept = default;
102
104
105 void setBuildInfo(BuildInfo buildInfo);
106
107 /// Set application URI, default: `urn:open62541.server.application`.
108 void setApplicationUri(std::string_view uri);
109 /// Set product URI, default: `http://open62541.org`.
110 void setProductUri(std::string_view uri);
111 /// Set application name, default: `open62541-based OPC UA Application`.
112 void setApplicationName(std::string_view name);
113
114 /// Add custom data types.
115 /// All data types provided are automatically considered for decoding of received messages.
116 void addCustomDataTypes(Span<const DataType> types);
117
118 /// Set custom access control.
120 /// Set custom access control (transfer ownership to Server).
121 void setAccessControl(std::unique_ptr<AccessControlBase>&& accessControl);
122};
123
124/* ------------------------------------------- Server ------------------------------------------- */
125
126/**
127 * High-level server class.
128 *
129 * A server is usually created in two steps:
130 * 1. Create and modify a server configuration (ServerConfig)
131 * 2. Create a Server with a ServerConfig (move ownership to the Server instance)
132 *
133 * The server expects that the configuration is not modified during runtime.
134 *
135 * Use the handle() method to get access the underlying UA_Server instance and use the full power
136 * of open62541.
137 *
138 * Don't overwrite the UA_ServerConfig::context pointer! The context pointer is used to store a
139 * pointer to the Server instance (for asWrapper(UA_Server*)) and to get access to the underlying
140 * server context.
141 */
142class Server {
143public:
144 /// Create server with default configuration.
146
147 /// Create server with given configuration (move ownership to server).
148 explicit Server(ServerConfig&& config);
149
150 /// Create server from native instance (move ownership to server).
152
154
155 Server(const Server&) = delete;
156 Server(Server&& other) noexcept;
157 Server& operator=(const Server&) = delete;
158 Server& operator=(Server&& other) noexcept;
159
160 ServerConfig& config() noexcept;
161 const ServerConfig& config() const noexcept;
162
163 [[deprecated("use ServerConfig::addCustomDataTypes via config() or pass config to Server")]]
164 void setCustomDataTypes(Span<const DataType> dataTypes) {
165 config().addCustomDataTypes(dataTypes);
166 }
167
168 /// Get active sessions.
169 std::vector<Session> sessions();
170
171 /// Get all defined namespaces.
172 std::vector<String> namespaceArray();
173
174 /// Register namespace. The new namespace index will be returned.
175 [[nodiscard]] NamespaceIndex registerNamespace(std::string_view uri);
176
177 /// Set value callback for variable node.
179 /// Set value callback for variable node (move ownership to server).
181 const NodeId& id, std::unique_ptr<ValueCallbackBase>&& callback
182 );
183 /// Set data source for variable node.
185 /// Set data source for variable node (move ownership to server).
186 void setVariableNodeDataSource(const NodeId& id, std::unique_ptr<DataSourceBase>&& source);
187
188#ifdef UA_ENABLE_SUBSCRIPTIONS
189 /// Create a (pseudo) subscription to monitor local data changes and events.
190 /// @deprecated Use Subscription constructor
191 [[deprecated("use Subscription constructor")]]
193#endif
194
195 /// Run a single iteration of the server's main loop.
196 /// @return Maximum wait period until next Server::runIterate call (in ms)
197 uint16_t runIterate();
198 /// Run the server's main loop. This method will block until Server::stop is called.
199 void run();
200 /// Stop the server's main loop.
201 void stop();
202 /// Check if the server is running.
203 bool isRunning() const noexcept;
204
205 UA_Server* handle() noexcept;
206 const UA_Server* handle() const noexcept;
207
208private:
209 detail::ServerContext& context() noexcept;
210 const detail::ServerContext& context() const noexcept;
211
212 friend detail::ServerContext& detail::getContext(Server& server) noexcept;
213
214 struct Deleter {
215 void operator()(UA_Server* server) noexcept;
216 };
217
218 std::unique_ptr<detail::ServerContext> context_;
219 std::unique_ptr<UA_Server, Deleter> server_;
220};
221
222/// Convert native UA_Server pointer to its wrapper instance.
223/// The native server must be owned by a Server instance.
224/// @relates Server
225Server* asWrapper(UA_Server* server) noexcept;
226
227/// @relates Server
228inline bool operator==(const Server& lhs, const Server& rhs) noexcept {
229 return (lhs.handle() == rhs.handle());
230}
231
232/// @relates Server
233inline bool operator!=(const Server& lhs, const Server& rhs) noexcept {
234 return !(lhs == rhs);
235}
236
237/* -------------------------------------- Async operations -------------------------------------- */
238
239#if UAPP_HAS_ASYNC_OPERATIONS
240/// Enable or disable async operations for the specified node.
241/// When enabled, operations on the node are queued and must processed by a worker thread using
242/// @ref runAsyncOperation.
243/// @note Only supported for method nodes.
244/// @relates Server
245void useAsyncOperation(Server& server, const NodeId& id, bool enabled);
246
247/// Wraps an async operation request.
249 UA_AsyncOperationType type;
250 const UA_AsyncOperationRequest* request;
251 void* context;
252};
253
254/// Get the next queued async operation if available.
255/// Async operations are queued by nodes with async operations enabled (see @ref useAsyncOperation).
256/// @relates Server
257std::optional<AsyncOperation> getAsyncOperation(Server& server) noexcept;
258
259/// Run the provided async operation.
260/// @relates Server
261void runAsyncOperation(Server& server, const AsyncOperation& operation);
262#endif
263
264} // namespace opcua
Access control base class.
UA_ByteString wrapper class.
Definition types.hpp:537
Data source base class for variable nodes.
Definition nodestore.hpp:60
UA_DataType wrapper class.
Definition datatype.hpp:111
UA_NodeId wrapper class.
Definition types.hpp:641
Server configuration.
Definition server.hpp:43
ServerConfig(uint16_t port, const ByteString &certificate={})
Create server config with minimal configuration.
void setApplicationName(std::string_view name)
Set application name, default: open62541-based OPC UA Application.
ServerConfig()
Create server config with default configuration.
void setLogger(LogFunction func)
ServerConfig(ServerConfig &&) noexcept=default
void setAccessControl(AccessControlBase &accessControl)
Set custom access control.
ServerConfig(const ServerConfig &)=delete
void addCustomDataTypes(Span< const DataType > types)
Add custom data types.
ServerConfig(UA_ServerConfig &&native)
Definition server.hpp:93
void setProductUri(std::string_view uri)
Set product URI, default: http://open62541.org.
void setBuildInfo(BuildInfo buildInfo)
void setApplicationUri(std::string_view uri)
Set application URI, default: urn:open62541.server.application.
ServerConfig(uint16_t port, const ByteString &certificate, const ByteString &privateKey, Span< const ByteString > trustList, Span< const ByteString > issuerList, Span< const ByteString > revocationList={})
Create server config with encryption enabled (PKI).
High-level server class.
Definition server.hpp:142
bool operator==(const Server &lhs, const Server &rhs) noexcept
Definition server.hpp:228
Server(UA_Server *native)
Create server from native instance (move ownership to server).
NamespaceIndex registerNamespace(std::string_view uri)
Register namespace. The new namespace index will be returned.
void stop()
Stop the server's main loop.
void setVariableNodeDataSource(const NodeId &id, DataSourceBase &source)
Set data source for variable node.
Server(Server &&other) noexcept
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.
Subscription< Server > createSubscription() noexcept
Create a (pseudo) subscription to monitor local data changes and events.
Server & operator=(const Server &)=delete
ServerConfig & config() noexcept
void setVariableNodeValueCallback(const NodeId &id, ValueCallbackBase &callback)
Set value callback for variable node.
std::vector< Session > sessions()
Get active sessions.
Server & operator=(Server &&other) noexcept
Server(ServerConfig &&config)
Create server with given configuration (move ownership to server).
bool operator!=(const Server &lhs, const Server &rhs) noexcept
Definition server.hpp:233
Server()
Create server with default configuration.
std::optional< AsyncOperation > getAsyncOperation(Server &server) noexcept
Get the next queued async operation if available.
Server * asWrapper(UA_Server *server) noexcept
Convert native UA_Server pointer to its wrapper instance.
bool isRunning() const noexcept
Check if the server is running.
Server(const Server &)=delete
void setVariableNodeDataSource(const NodeId &id, std::unique_ptr< DataSourceBase > &&source)
Set data source for variable node (move ownership to server).
void runAsyncOperation(Server &server, const AsyncOperation &operation)
Run the provided async operation.
void setVariableNodeValueCallback(const NodeId &id, std::unique_ptr< ValueCallbackBase > &&callback)
Set value callback for variable node (move ownership to server).
void useAsyncOperation(Server &server, const NodeId &id, bool enabled)
Enable or disable async operations for the specified node.
std::vector< String > namespaceArray()
Get all defined namespaces.
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:29
High-level subscription class.
Value callback base class for variable nodes.
Definition nodestore.hpp:18
Template base class to wrap (native) objects.
Definition wrapper.hpp:135
constexpr UA_ServerConfig * handle() noexcept
Return pointer to native object.
Definition wrapper.hpp:221
constexpr const UA_ServerConfig & native() const noexcept
Definition wrapper.hpp:243
UA_BuildInfo wrapper class.
Definition types.hpp:1483
uint16_t NamespaceIndex
Namespace index.
Definition common.hpp:12
std::function< void(LogLevel, LogCategory, std::string_view msg)> LogFunction
Log function.
Wraps an async operation request.
Definition server.hpp:248
const UA_AsyncOperationRequest * request
Definition server.hpp:250
UA_AsyncOperationType type
Definition server.hpp:249
static UA_ServerConfig copy(const UA_ServerConfig &config)
static UA_ServerConfig move(UA_ServerConfig &&config) noexcept
static void clear(UA_ServerConfig &config) noexcept
Default type handler providing standard copy and move operations.
Definition wrapper.hpp:52