iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
server.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13#ifndef IOX2_SERVER_HPP
14#define IOX2_SERVER_HPP
15
17#include "iox2/bb/expected.hpp"
18#include "iox2/bb/optional.hpp"
19#include "iox2/bb/slice.hpp"
20#include "iox2/port_name.hpp"
21#include "iox2/service_type.hpp"
23
24namespace iox2 {
27template <ServiceType Service,
28 typename RequestPayload,
29 typename RequestHeader,
30 typename ResponsePayload,
31 typename ResponseHeader>
32class Server {
33 public:
34 Server(Server&& rhs) noexcept;
35 auto operator=(Server&& rhs) noexcept -> Server&;
36 ~Server() noexcept;
37
38 Server(const Server&) noexcept = delete;
39 auto operator=(const Server&) noexcept -> Server& = delete;
40
44 auto receive() -> bb::Expected<
47
49 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
50 auto initial_max_slice_len() const -> uint64_t;
51
53 auto id() const -> UniqueServerId;
54
56 auto name() const -> PortNameView;
57
59 auto has_requests() const -> bb::Expected<bool, ConnectionFailure>;
60
61 private:
62 template <ServiceType, typename, typename, typename, typename>
63 friend class PortFactoryServer;
64
65 explicit Server(iox2_server_h handle) noexcept;
66
67 void drop();
68
69 iox2_server_h m_handle = nullptr;
70};
71
72template <ServiceType Service,
73 typename RequestPayload,
74 typename RequestHeader,
75 typename ResponsePayload,
76 typename ResponseHeader>
77inline Server<Service, RequestPayload, RequestHeader, ResponsePayload, ResponseHeader>::Server(Server&& rhs) noexcept {
78 *this = std::move(rhs);
79}
80
81template <ServiceType Service,
82 typename RequestPayload,
83 typename RequestHeader,
84 typename ResponsePayload,
85 typename ResponseHeader>
86inline auto
88 -> Server& {
89 if (this != &rhs) {
90 drop();
91 m_handle = rhs.m_handle;
92 rhs.m_handle = nullptr;
93 }
94
95 return *this;
96}
97
98template <ServiceType Service,
99 typename RequestPayload,
100 typename RequestHeader,
101 typename ResponsePayload,
102 typename ResponseHeader>
106
107template <ServiceType Service,
108 typename RequestPayload,
109 typename RequestHeader,
110 typename ResponsePayload,
111 typename ResponseHeader>
114 ReceiveError> {
115 iox2_active_request_h active_request_handle {};
116 auto result = iox2_server_receive(&m_handle, nullptr, &active_request_handle);
117
118 if (result == IOX2_OK) {
119 if (active_request_handle != nullptr) {
121 active_request_handle);
123 std::move(active_request));
124 }
127 }
128 return bb::err(bb::into<ReceiveError>(result));
129}
130
131template <ServiceType Service,
132 typename RequestPayload,
133 typename RequestHeader,
134 typename ResponsePayload,
135 typename ResponseHeader>
136template <typename T, typename>
137inline auto
139 -> uint64_t {
140 return iox2_server_initial_max_slice_len(&m_handle);
141}
142
143template <ServiceType Service,
144 typename RequestPayload,
145 typename RequestHeader,
146 typename ResponsePayload,
147 typename ResponseHeader>
149 -> UniqueServerId {
150 iox2_unique_server_id_h id_handle = nullptr;
151 iox2_server_id(&m_handle, nullptr, &id_handle);
152 return UniqueServerId { id_handle };
153}
154
155template <ServiceType Service,
156 typename RequestPayload,
157 typename RequestHeader,
158 typename ResponsePayload,
159 typename ResponseHeader>
161 -> PortNameView {
162 const auto* port_name_ptr = iox2_server_name(&m_handle);
163 return PortNameView::from_native_ptr(port_name_ptr);
164}
165
166template <ServiceType Service,
167 typename RequestPayload,
168 typename RequestHeader,
169 typename ResponsePayload,
170 typename ResponseHeader>
172 -> bb::Expected<bool, ConnectionFailure> {
173 bool has_requests_result = false;
174 auto result = iox2_server_has_requests(&m_handle, &has_requests_result);
175
176 if (result == IOX2_OK) {
177 return has_requests_result;
178 }
179
180 return bb::err(bb::into<ConnectionFailure>(result));
181}
182
183template <ServiceType Service,
184 typename RequestPayload,
185 typename RequestHeader,
186 typename ResponsePayload,
187 typename ResponseHeader>
189 iox2_server_h handle) noexcept
190 : m_handle { handle } {
191}
192
193template <ServiceType Service,
194 typename RequestPayload,
195 typename RequestHeader,
196 typename ResponsePayload,
197 typename ResponseHeader>
198inline void Server<Service, RequestPayload, RequestHeader, ResponsePayload, ResponseHeader>::drop() {
199 if (m_handle != nullptr) {
200 iox2_server_drop(m_handle);
201 m_handle = nullptr;
202 }
203}
204} // namespace iox2
205#endif
Non-owning view of a [PortName].
Definition port_name.hpp:25
static auto from_native_ptr(iox2_port_name_ptr m_ptr) -> PortNameView
Creates a [PortNameView] from a native [iox2_port_name_ptr].
Request header used by [MessagingPattern::RequestResponse].
Response header used by [MessagingPattern::RequestResponse].
auto initial_max_slice_len() const -> uint64_t
Returns the maximum initial slice length configured for this [Server].
Definition server.hpp:138
auto has_requests() const -> bb::Expected< bool, ConnectionFailure >
Returns true if the [Server] has [RequestMut]s in its buffer.
Definition server.hpp:171
~Server() noexcept
Definition server.hpp:103
Server(const Server &) noexcept=delete
auto operator=(const Server &) noexcept -> Server &=delete
auto id() const -> UniqueServerId
Returns the [UniqueServerId] of the [Server].
Definition server.hpp:148
Server(Server &&rhs) noexcept
Definition server.hpp:77
auto receive() -> bb::Expected< bb::Optional< ActiveRequest< Service, RequestPayload, RequestHeader, ResponsePayload, ResponseHeader > >, ReceiveError >
Definition server.hpp:112
auto operator=(Server &&rhs) noexcept -> Server &
Definition server.hpp:87
auto name() const -> PortNameView
Returns the [PortNameView] of the [Server].
Definition server.hpp:160
The system-wide unique id of a [Server].
constexpr NulloptT NULLOPT
Definition optional.hpp:28
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
iox2::bb::variation::Optional< T > Optional
Definition optional.hpp:25
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22