open62541pp 0.18.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 <string>
6#include <string_view>
7#include <utility> // move
8#include <vector>
9
10#include "open62541pp/common.hpp" // NamespaceIndex
11#include "open62541pp/config.hpp"
15#include "open62541pp/event.hpp"
17#include "open62541pp/span.hpp"
18#include "open62541pp/subscription.hpp" // TODO: remove with Server::createSubscription
19#include "open62541pp/types.hpp"
22
25#include "open62541pp/plugin/log_default.hpp" // LogFunction
27
28namespace opcua {
29template <typename Connection>
30class Node;
31
32/* ---------------------------------------- ServerConfig ---------------------------------------- */
33
34/**
35 * Server configuration.
36 * @see UA_ServerConfig
37 */
38class ServerConfig : public Wrapper<UA_ServerConfig> {
39public:
40 /**
41 * Create server config with default configuration.
42 * Security policies:
43 * - [None](http://opcfoundation.org/UA/SecurityPolicy#None)
44 */
46
47 /**
48 * Create server config with minimal configuration.
49 * Security policies:
50 * - [None](http://opcfoundation.org/UA/SecurityPolicy#None)
51 *
52 * @param port Port number
53 * @param certificate Optional X.509 v3 certificate in `DER` encoded format
54 */
55 explicit ServerConfig(uint16_t port, const ByteString& certificate = {});
56
57#ifdef UA_ENABLE_ENCRYPTION
58 /**
59 * Create server config with encryption enabled (PKI).
60 * Security policies:
61 * - [None](http://opcfoundation.org/UA/SecurityPolicy#None)
62 * - [Basic128Rsa15](http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15)
63 * - [Basic256](http://opcfoundation.org/UA/SecurityPolicy#Basic256)
64 * - [Basic256Sha256](http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256)
65 * - [Aes128_Sha256_RsaOaep](http://opcfoundation.org/UA/SecurityPolicy#Aes128_Sha256_RsaOaep)
66 *
67 * @param port Port number
68 * @param certificate X.509 v3 certificate in `DER` encoded format
69 * @param privateKey Private key in `PEM` encoded format
70 * @param trustList List of trusted certificates in `DER` encoded format
71 * @param issuerList List of issuer certificates (i.e. CAs) in `DER` encoded format
72 * @param revocationList Certificate revocation lists (CRL) in `DER` encoded format
73 *
74 * @see https://reference.opcfoundation.org/Core/Part2/v105/docs/9
75 * @see https://reference.opcfoundation.org/Core/Part4/v105/docs/6.1
76 * @see https://reference.opcfoundation.org/Core/Part6/v105/docs/6.2
77 */
79 uint16_t port,
80 const ByteString& certificate,
81 const ByteString& privateKey,
82 Span<const ByteString> trustList,
83 Span<const ByteString> issuerList,
84 Span<const ByteString> revocationList = {}
85 );
86#endif
87
89
91
92 ServerConfig(const ServerConfig&) = delete;
93 ServerConfig(ServerConfig&& other) noexcept;
96
98
99 void setBuildInfo(BuildInfo buildInfo);
100
101 /// Set application URI, default: `urn:open62541.server.application`.
102 void setApplicationUri(std::string_view uri);
103 /// Set product URI, default: `http://open62541.org`.
104 void setProductUri(std::string_view uri);
105 /// Set application name, default: `open62541-based OPC UA Application`.
106 void setApplicationName(std::string_view name);
107
108 /// Add custom data types.
109 /// All data types provided are automatically considered for decoding of received messages.
111
112 /// Set custom access control.
114 /// Set custom access control (transfer ownership to Server).
115 void setAccessControl(std::unique_ptr<AccessControlBase>&& accessControl);
116};
117
118/* ------------------------------------------- Server ------------------------------------------- */
119
120/**
121 * High-level server class.
122 *
123 * A server is usually created in two steps:
124 * 1. Create and modify a server configuration (ServerConfig)
125 * 2. Create a Server with a ServerConfig (move ownership to the Server instance)
126 *
127 * The server expects that the configuration is not modified during runtime.
128 *
129 * Use the handle() method to get access the underlying UA_Server instance and use the full power
130 * of open62541.
131 *
132 * Don't overwrite the UA_ServerConfig::context pointer! The context pointer is used to store a
133 * pointer to the Server instance (for asWrapper(UA_Server*)) and to get access to the underlying
134 * server context.
135 */
136class Server {
137public:
138 /// Create server with default configuration.
140
141 /// Create server with given configuration (move ownership to server).
143
144 /// @copydoc ServerConfig::ServerConfig(uint16_t, const ByteString&)
145 [[deprecated("use ServerConfig constructor and construct Server with ServerConfig")]]
146 explicit Server(uint16_t port, const ByteString& certificate = {})
147 : Server(ServerConfig(port, certificate)) {}
148
149#ifdef UA_ENABLE_ENCRYPTION
150 /// @copydoc ServerConfig::ServerConfig(
151 /// uint16_t,
152 /// const ByteString&,
153 /// const ByteString&,
154 /// Span<const ByteString>,
155 /// Span<const ByteString>,
156 /// Span<const ByteString>
157 /// )
158 [[deprecated("use ServerConfig constructor and construct Server with ServerConfig")]]
160 uint16_t port,
161 const ByteString& certificate,
162 const ByteString& privateKey,
163 Span<const ByteString> trustList,
164 Span<const ByteString> issuerList,
165 Span<const ByteString> revocationList = {}
166 )
167 : Server(ServerConfig(port, certificate, privateKey, trustList, issuerList, revocationList)
168 ) {}
169#endif
170
171 /// Create server from native instance (move ownership to server).
172 explicit Server(UA_Server* native);
173
175
176 Server(const Server&) = delete;
177 Server(Server&& other) noexcept;
178 Server& operator=(const Server&) = delete;
179 Server& operator=(Server&& other) noexcept;
180
181 ServerConfig& config() noexcept;
182 const ServerConfig& config() const noexcept;
183
184 [[deprecated("use ServerConfig::setLogger via config() or pass config to Server")]]
185 void setLogger(LogFunction logger) {
186 config().setLogger(std::move(logger));
187 }
188
189 [[deprecated("use ServerConfig::setApplicationUri via config() or pass config to Server")]]
190 void setApplicationUri(std::string_view uri) {
192 }
193
194 [[deprecated("use ServerConfig::setProductUri via config() or pass config to Server")]]
195 void setProductUri(std::string_view uri) {
196 config().setProductUri(uri);
197 }
198
199 [[deprecated("use ServerConfig::setApplicationName via config() or pass config to Server")]]
200 void setApplicationName(std::string_view name) {
202 }
203
204 /// Set custom hostname, default: system's host name.
205 [[deprecated("not supported since open62541 v1.4")]]
206 void setCustomHostname(std::string_view hostname);
207
208 [[deprecated("use ServerConfig::setAccessControl via config() or pass config to Server")]]
209 void setAccessControl(AccessControlBase& accessControl) {
210 config().setAccessControl(accessControl);
211 }
212
213 [[deprecated("use ServerConfig::setAccessControl via config() or pass config to Server")]]
214 void setAccessControl(std::unique_ptr<AccessControlBase>&& accessControl) {
215 config().setAccessControl(std::move(accessControl));
216 }
217
218 [[deprecated("use ServerConfig::addCustomDataTypes via config() or pass config to Server")]]
220 config().addCustomDataTypes(dataTypes);
221 }
222
223 /// Get active sessions.
224 std::vector<Session> sessions();
225
226 /// @deprecated Use sessions() instead
227 [[deprecated("use sessions() instead")]]
228 std::vector<Session> getSessions() {
229 return sessions();
230 }
231
232 /// Get all defined namespaces.
233 std::vector<std::string> namespaceArray();
234
235 /// @deprecated Use namespaceArray() instead
236 [[deprecated("use namespaceArray() instead")]]
237 std::vector<std::string> getNamespaceArray() {
238 return namespaceArray();
239 }
240
241 /// Register namespace. The new namespace index will be returned.
242 [[nodiscard]] NamespaceIndex registerNamespace(std::string_view uri);
243
244 /// Set value callback for variable node.
246 /// Set value callback for variable node (move ownership to server).
247 void setVariableNodeValueCallback(const NodeId& id, std::unique_ptr<ValueCallbackBase>&& callback);
248 /// Set data source for variable node.
250 /// Set data source for variable node (move ownership to server).
251 void setVariableNodeDataSource(const NodeId& id, std::unique_ptr<DataSourceBase>&& source);
252
253#ifdef UA_ENABLE_SUBSCRIPTIONS
254 /// Create a (pseudo) subscription to monitor local data changes and events.
255 /// @deprecated Use Subscription constructor
256 [[deprecated("use Subscription constructor")]]
258#endif
259
260#ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
261 /// Create an event object to generate and trigger events.
262 [[deprecated("use Event constructor")]]
263 Event createEvent(const NodeId& eventType = ObjectTypeId::BaseEventType);
264#endif
265
266 /// Run a single iteration of the server's main loop.
267 /// @return Maximum wait period until next Server::runIterate call (in ms)
268 uint16_t runIterate();
269 /// Run the server's main loop. This method will block until Server::stop is called.
270 void run();
271 /// Stop the server's main loop.
272 void stop();
273 /// Check if the server is running.
274 bool isRunning() const noexcept;
275
276 [[deprecated("use Node constructor")]] Node<Server> getNode(NodeId id);
277 [[deprecated("use Node constructor")]] Node<Server> getRootNode();
278 [[deprecated("use Node constructor")]] Node<Server> getObjectsNode();
279 [[deprecated("use Node constructor")]] Node<Server> getTypesNode();
280 [[deprecated("use Node constructor")]] Node<Server> getViewsNode();
281
282 UA_Server* handle() noexcept;
283 const UA_Server* handle() const noexcept;
284
285private:
286 detail::ServerContext& context() noexcept;
287 const detail::ServerContext& context() const noexcept;
288
289 friend detail::ServerContext& detail::getContext(Server& server) noexcept;
290
291 struct Deleter {
292 void operator()(UA_Server* server) noexcept;
293 };
294
295 std::unique_ptr<detail::ServerContext> context_;
296 std::unique_ptr<UA_Server, Deleter> server_;
297};
298
299/// Convert native UA_Server pointer to its wrapper instance.
300/// The native server must be owned by a Server instance.
301/// @relates Server
302Server* asWrapper(UA_Server* server) noexcept;
303
304/// @relates Server
305inline bool operator==(const Server& lhs, const Server& rhs) noexcept {
306 return (lhs.handle() == rhs.handle());
307}
308
309/// @relates Server
310inline bool operator!=(const Server& lhs, const Server& rhs) noexcept {
311 return !(lhs == rhs);
312}
313
314} // namespace opcua
Access control base class.
UA_ByteString wrapper class.
Definition types.hpp:553
Data source base class for variable nodes.
Definition nodestore.hpp:60
Create and trigger events.
Definition event.hpp:19
UA_NodeId wrapper class.
Definition types.hpp:665
High-level node class to access node attribute, browse and populate address space.
Definition node.hpp:45
Server configuration.
Definition server.hpp:38
void setAccessControl(std::unique_ptr< AccessControlBase > &&accessControl)
Set custom access control (transfer ownership to Server).
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)
void setAccessControl(AccessControlBase &accessControl)
Set custom access control.
ServerConfig & operator=(ServerConfig &&other) noexcept
ServerConfig(ServerConfig &&other) noexcept
ServerConfig(const ServerConfig &)=delete
ServerConfig & operator=(const ServerConfig &)=delete
void addCustomDataTypes(Span< const DataType > types)
Add custom data types.
ServerConfig(UA_ServerConfig &&native)
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:136
void setCustomDataTypes(Span< const DataType > dataTypes)
Definition server.hpp:219
bool operator==(const Server &lhs, const Server &rhs) noexcept
Definition server.hpp:305
Server(uint16_t port, const ByteString &certificate={})
Create server config with minimal configuration.
Definition server.hpp:146
std::vector< Session > getSessions()
Definition server.hpp:228
Node< Server > getNode(NodeId id)
Node< Server > getObjectsNode()
void setApplicationName(std::string_view name)
Definition server.hpp:200
Server(UA_Server *native)
Create server from native instance (move ownership to server).
void setProductUri(std::string_view uri)
Definition server.hpp:195
std::vector< std::string > namespaceArray()
Get all defined namespaces.
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 setVariableNodeDataSource(const NodeId &id, DataSourceBase &source)
Set data source for variable node.
Server(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).
Definition server.hpp:159
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.
Node< Server > getRootNode()
Subscription< Server > createSubscription() noexcept
Create a (pseudo) subscription to monitor local data changes and events.
Node< Server > getViewsNode()
Server & operator=(const Server &)=delete
ServerConfig & config() noexcept
void setAccessControl(AccessControlBase &accessControl)
Definition server.hpp:209
void setVariableNodeValueCallback(const NodeId &id, ValueCallbackBase &callback)
Set value callback for variable node.
std::vector< Session > sessions()
Get active sessions.
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)
Definition server.hpp:190
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:310
void setLogger(LogFunction logger)
Definition server.hpp:185
void setAccessControl(std::unique_ptr< AccessControlBase > &&accessControl)
Definition server.hpp:214
friend detail::ServerContext & detail::getContext(Server &server) noexcept
Server()
Create server with default configuration.
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).
std::vector< std::string > getNamespaceArray()
Definition server.hpp:237
void setVariableNodeValueCallback(const NodeId &id, std::unique_ptr< ValueCallbackBase > &&callback)
Set value callback for variable node (move ownership to server).
UA_Server * handle() noexcept
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:30
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:33
constexpr const UA_ServerConfig & native() const noexcept
Definition wrapper.hpp:102
UA_BuildInfo wrapper class.
Definition types.hpp:1549
uint16_t NamespaceIndex
Namespace index.
Definition common.hpp:12
std::function< void(LogLevel, LogCategory, std::string_view msg)> LogFunction
Log function.