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/service_type.hpp"
22
23namespace iox2 {
26template <ServiceType Service,
27 typename RequestPayload,
28 typename RequestHeader,
29 typename ResponsePayload,
30 typename ResponseHeader>
31class Server {
32 public:
33 Server(Server&& rhs) noexcept;
34 auto operator=(Server&& rhs) noexcept -> Server&;
35 ~Server() noexcept;
36
37 Server(const Server&) noexcept = delete;
38 auto operator=(const Server&) noexcept -> Server& = delete;
39
43 auto receive() -> bb::Expected<
46
48 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
49 auto initial_max_slice_len() const -> uint64_t;
50
52 auto id() const -> UniqueServerId;
53
55 auto has_requests() const -> bb::Expected<bool, ConnectionFailure>;
56
57 private:
58 template <ServiceType, typename, typename, typename, typename>
59 friend class PortFactoryServer;
60
61 explicit Server(iox2_server_h handle) noexcept;
62
63 void drop();
64
65 iox2_server_h m_handle = nullptr;
66};
67
68template <ServiceType Service,
69 typename RequestPayload,
70 typename RequestHeader,
71 typename ResponsePayload,
72 typename ResponseHeader>
73inline Server<Service, RequestPayload, RequestHeader, ResponsePayload, ResponseHeader>::Server(Server&& rhs) noexcept {
74 *this = std::move(rhs);
75}
76
77template <ServiceType Service,
78 typename RequestPayload,
79 typename RequestHeader,
80 typename ResponsePayload,
81 typename ResponseHeader>
82inline auto
84 -> Server& {
85 if (this != &rhs) {
86 drop();
87 m_handle = rhs.m_handle;
88 rhs.m_handle = nullptr;
89 }
90
91 return *this;
92}
93
94template <ServiceType Service,
95 typename RequestPayload,
96 typename RequestHeader,
97 typename ResponsePayload,
98 typename ResponseHeader>
102
103template <ServiceType Service,
104 typename RequestPayload,
105 typename RequestHeader,
106 typename ResponsePayload,
107 typename ResponseHeader>
110 ReceiveError> {
111 iox2_active_request_h active_request_handle {};
112 auto result = iox2_server_receive(&m_handle, nullptr, &active_request_handle);
113
114 if (result == IOX2_OK) {
115 if (active_request_handle != nullptr) {
117 active_request_handle);
119 std::move(active_request));
120 }
123 }
124 return bb::err(bb::into<ReceiveError>(result));
125}
126
127template <ServiceType Service,
128 typename RequestPayload,
129 typename RequestHeader,
130 typename ResponsePayload,
131 typename ResponseHeader>
132template <typename T, typename>
133inline auto
135 -> uint64_t {
136 return iox2_server_initial_max_slice_len(&m_handle);
137}
138
139template <ServiceType Service,
140 typename RequestPayload,
141 typename RequestHeader,
142 typename ResponsePayload,
143 typename ResponseHeader>
145 -> UniqueServerId {
146 iox2_unique_server_id_h id_handle = nullptr;
147 iox2_server_id(&m_handle, nullptr, &id_handle);
148 return UniqueServerId { id_handle };
149}
150
151template <ServiceType Service,
152 typename RequestPayload,
153 typename RequestHeader,
154 typename ResponsePayload,
155 typename ResponseHeader>
157 -> bb::Expected<bool, ConnectionFailure> {
158 bool has_requests_result = false;
159 auto result = iox2_server_has_requests(&m_handle, &has_requests_result);
160
161 if (result == IOX2_OK) {
162 return has_requests_result;
163 }
164
165 return bb::err(bb::into<ConnectionFailure>(result));
166}
167
168template <ServiceType Service,
169 typename RequestPayload,
170 typename RequestHeader,
171 typename ResponsePayload,
172 typename ResponseHeader>
174 iox2_server_h handle) noexcept
175 : m_handle { handle } {
176}
177
178template <ServiceType Service,
179 typename RequestPayload,
180 typename RequestHeader,
181 typename ResponsePayload,
182 typename ResponseHeader>
183inline void Server<Service, RequestPayload, RequestHeader, ResponsePayload, ResponseHeader>::drop() {
184 if (m_handle != nullptr) {
185 iox2_server_drop(m_handle);
186 m_handle = nullptr;
187 }
188}
189} // namespace iox2
190#endif
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:134
auto has_requests() const -> bb::Expected< bool, ConnectionFailure >
Returns true if the [Server] has [RequestMut]s in its buffer.
Definition server.hpp:156
~Server() noexcept
Definition server.hpp:99
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:144
Server(Server &&rhs) noexcept
Definition server.hpp:73
auto receive() -> bb::Expected< bb::Optional< ActiveRequest< Service, RequestPayload, RequestHeader, ResponsePayload, ResponseHeader > >, ReceiveError >
Definition server.hpp:108
auto operator=(Server &&rhs) noexcept -> Server &
Definition server.hpp:83
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