open62541pp 0.16.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"
18#include "open62541pp/span.hpp"
20#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/8
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 /// Set custom access control.
110 /// Set custom access control (transfer ownership to Server).
111 void setAccessControl(std::unique_ptr<AccessControlBase>&& accessControl);
112};
113
114/* ------------------------------------------- Server ------------------------------------------- */
115
116/**
117 * High-level server class.
118 *
119 * A server is usually created in two steps:
120 * 1. Create and modify a server configuration (ServerConfig)
121 * 2. Create a Server with a ServerConfig (move ownership to the Server instance)
122 *
123 * The server expects that the configuration is not modified during runtime.
124 *
125 * Use the handle() method to get access the underlying UA_Server instance and use the full power
126 * of open62541.
127 *
128 * Don't overwrite the UA_ServerConfig::context pointer! The context pointer is used to store a
129 * pointer to the Server instance (for asWrapper(UA_Server*)) and to get access to the underlying
130 * server context.
131 */
132class Server {
133public:
134 /// Create server with default configuration.
136
137 /// Create server with given configuration (move ownership to server).
139
140 /// @copydoc ServerConfig::ServerConfig(uint16_t, const ByteString&)
141 [[deprecated("use ServerConfig constructor and construct Server with ServerConfig")]]
142 explicit Server(uint16_t port, const ByteString& certificate = {})
143 : Server(ServerConfig(port, certificate)) {}
144
145#ifdef UA_ENABLE_ENCRYPTION
146 /// @copydoc ServerConfig::ServerConfig(
147 /// uint16_t,
148 /// const ByteString&,
149 /// const ByteString&,
150 /// Span<const ByteString>,
151 /// Span<const ByteString>,
152 /// Span<const ByteString>
153 /// )
154 [[deprecated("use ServerConfig constructor and construct Server with ServerConfig")]]
156 uint16_t port,
157 const ByteString& certificate,
158 const ByteString& privateKey,
159 Span<const ByteString> trustList,
160 Span<const ByteString> issuerList,
161 Span<const ByteString> revocationList = {}
162 )
163 : Server(ServerConfig(port, certificate, privateKey, trustList, issuerList, revocationList)
164 ) {}
165#endif
166
168
169 Server(const Server&) = delete;
170 Server(Server&& other) noexcept;
171 Server& operator=(const Server&) = delete;
172 Server& operator=(Server&& other) noexcept;
173
174 ServerConfig& config() noexcept;
175 const ServerConfig& config() const noexcept;
176
177 [[deprecated("use ServerConfig::setLogger via config() or pass config to Server")]]
178 void setLogger(LogFunction logger) {
179 config().setLogger(std::move(logger));
180 }
181
182 [[deprecated("use ServerConfig::setApplicationUri via config() or pass config to Server")]]
183 void setApplicationUri(std::string_view uri) {
185 }
186
187 [[deprecated("use ServerConfig::setProductUri via config() or pass config to Server")]]
188 void setProductUri(std::string_view uri) {
189 config().setProductUri(uri);
190 }
191
192 [[deprecated("use ServerConfig::setApplicationName via config() or pass config to Server")]]
193 void setApplicationName(std::string_view name) {
195 }
196
197 /// Set custom hostname, default: system's host name.
198 [[deprecated("not supported since open62541 v1.4")]]
199 void setCustomHostname(std::string_view hostname);
200
201 [[deprecated("use ServerConfig::setAccessControl via config() or pass config to Server")]]
202 void setAccessControl(AccessControlBase& accessControl) {
203 config().setAccessControl(accessControl);
204 }
205
206 [[deprecated("use ServerConfig::setAccessControl via config() or pass config to Server")]]
207 void setAccessControl(std::unique_ptr<AccessControlBase>&& accessControl) {
208 config().setAccessControl(std::move(accessControl));
209 }
210
211 /// Set custom data types.
212 /// All data types provided are automatically considered for decoding of received messages.
214
215 /// Get active server session.
216 std::vector<Session> getSessions();
217
218 /// Get all defined namespaces.
219 std::vector<std::string> getNamespaceArray();
220 /// Register namespace. The new namespace index will be returned.
221 [[nodiscard]] NamespaceIndex registerNamespace(std::string_view uri);
222
223 /// Set value callbacks to execute before every read and after every write operation.
225 /// Set data source backend for variable node.
227
228#ifdef UA_ENABLE_SUBSCRIPTIONS
229 /// Create a (pseudo) subscription to monitor local data changes and events.
231#endif
232
233#ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
234 /// Create an event object to generate and trigger events.
235 [[deprecated("use Event constructor")]]
237#endif
238
239 /// Run a single iteration of the server's main loop.
240 /// @return Maximum wait period until next Server::runIterate call (in ms)
241 uint16_t runIterate();
242 /// Run the server's main loop. This method will block until Server::stop is called.
243 void run();
244 /// Stop the server's main loop.
245 void stop();
246 /// Check if the server is running.
247 bool isRunning() const noexcept;
248
249 [[deprecated("use Node constructor")]] Node<Server> getNode(NodeId id);
250 [[deprecated("use Node constructor")]] Node<Server> getRootNode();
251 [[deprecated("use Node constructor")]] Node<Server> getObjectsNode();
252 [[deprecated("use Node constructor")]] Node<Server> getTypesNode();
253 [[deprecated("use Node constructor")]] Node<Server> getViewsNode();
254
255 UA_Server* handle() noexcept;
256 const UA_Server* handle() const noexcept;
257
258private:
259 detail::ServerContext& context() noexcept;
260 const detail::ServerContext& context() const noexcept;
261
262 friend detail::ServerContext& detail::getContext(Server& server) noexcept;
263
264 struct Deleter {
265 void operator()(UA_Server* server) noexcept;
266 };
267
268 std::unique_ptr<detail::ServerContext> context_;
269 std::unique_ptr<UA_Server, Deleter> server_;
270};
271
272/// Convert native UA_Server pointer to its wrapper instance.
273/// The native server must be owned by a Server instance.
274Server* asWrapper(UA_Server* server) noexcept;
275
276inline bool operator==(const Server& lhs, const Server& rhs) noexcept {
277 return (lhs.handle() == rhs.handle());
278}
279
280inline bool operator!=(const Server& lhs, const Server& rhs) noexcept {
281 return !(lhs == rhs);
282}
283
284} // namespace opcua
Access control base class.
UA_BuildInfo wrapper class.
UA_ByteString wrapper class.
Definition types.hpp:490
Create and trigger events.
Definition event.hpp:19
UA_NodeId wrapper class.
Definition types.hpp:590
High-level node class to access node attribute, browse and populate address space.
Definition server.hpp:30
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
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:132
void setCustomDataTypes(Span< const DataType > dataTypes)
Set custom data types.
void setVariableNodeValueBackend(const NodeId &id, ValueBackendDataSource backend)
Set data source backend for variable node.
Server(uint16_t port, const ByteString &certificate={})
Create server config with minimal configuration.
Definition server.hpp:142
std::vector< Session > getSessions()
Get active server session.
Node< Server > getNode(NodeId id)
Node< Server > getObjectsNode()
void setApplicationName(std::string_view name)
Definition server.hpp:193
void setProductUri(std::string_view uri)
Definition server.hpp:188
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.
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:155
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:202
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:183
void setVariableNodeValueCallback(const NodeId &id, ValueCallback callback)
Set value callbacks to execute before every read and after every write operation.
Server & operator=(Server &&other) noexcept
Server(ServerConfig &&config)
Create server with given configuration (move ownership to server).
void setLogger(LogFunction logger)
Definition server.hpp:178
void setAccessControl(std::unique_ptr< AccessControlBase > &&accessControl)
Definition server.hpp:207
friend detail::ServerContext & detail::getContext(Server &server) noexcept
Server()
Create server with default configuration.
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
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:26
High-level subscription class.
Template base class to wrap native objects.
Definition wrapper.hpp:32
constexpr const UA_ServerConfig & native() const noexcept
Definition wrapper.hpp:75
Client * asWrapper(UA_Client *client) noexcept
Convert native UA_Client pointer to its wrapper instance.
std::function< void(LogLevel, LogCategory, std::string_view msg)> LogFunction
Log function.
uint16_t NamespaceIndex
Namespace index.
Definition common.hpp:12
bool operator!=(const Client &lhs, const Client &rhs) noexcept
Definition client.hpp:295
bool operator==(const Client &lhs, const Client &rhs) noexcept
Definition client.hpp:291
Data source backend for variable nodes.
Definition nodestore.hpp:45
Value callbacks for variable nodes.
Definition nodestore.hpp:16