Ice 3.8
C++ API Reference
Loading...
Searching...
No Matches
Connection.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_CONNECTION_H
4#define ICE_CONNECTION_H
5
6#include "Config.h"
7#include "ConnectionF.h"
8#include "EndpointF.h"
9#include "Ice/Identity.h"
10#include "Ice/Proxy.h"
11#include "Ice/ProxyFunctions.h"
12#include "ObjectAdapterF.h"
13
14#include <future>
15#include <map>
16#include <optional>
17
18#if defined(__clang__)
19# pragma clang diagnostic push
20# pragma clang diagnostic ignored "-Wshadow-field-in-constructor"
21#elif defined(__GNUC__)
22# pragma GCC diagnostic push
23# pragma GCC diagnostic ignored "-Wshadow"
24#endif
25
26namespace Ice
27{
28 /// Represents batch compression options when flushing queued batch requests.
29 enum class CompressBatch : std::uint8_t
30 {
31 /// Compress the batch requests.
33
34 /// Don't compress the batch requests.
36
37 /// Compress the batch requests if at least one request was made on a compressed proxy.
39 };
40
41 /// Represents a collection of HTTP headers.
42 using HeaderDict = std::map<std::string, std::string, std::less<>>;
43
44 /// The callback function given to Connection::setCloseCallback.
45 /// @param con The connection that was closed. It's never a nullptr.
46 using CloseCallback = std::function<void(const ConnectionPtr& con)>;
47
48 /// Represents a connection that uses the Ice protocol.
49 /// @headerfile Ice/Ice.h
50 class ICE_API Connection
51 {
52 public:
53 virtual ~Connection();
54
55 /// Aborts this connection.
56 virtual void abort() noexcept = 0;
57
58 /// Starts a graceful closure of this connection once all outstanding invocations have completed.
59 /// @param response A callback that the implementation calls when the connection is closed gracefully.
60 /// @param exception A callback that the implementation calls when the connection closure failed. Its
61 /// `exception_ptr` parameter is always non-null and describes the reason for the failure.
62 /// @remark The response and exception callbacks may be called synchronously (from the calling thread); in
63 /// particular, this occurs when you call `close` on a connection that is already closed. The implementation
64 /// always calls one of the two callbacks once; it never calls both. If closing the connection takes longer than
65 /// the configured close timeout, the connection is aborted with a CloseTimeoutException.
66 virtual void
67 close(std::function<void()> response, std::function<void(std::exception_ptr)> exception) noexcept = 0;
68
69 /// Starts a graceful closure of this connection once all outstanding invocations have completed.
70 /// @return A future that becomes available when the connection is closed.
71 [[nodiscard]] std::future<void> close();
72
73 /// Creates a special proxy (a "fixed proxy") that always uses this connection.
74 /// @tparam Prx The type of the proxy to create.
75 /// @param id The identity of the target object.
76 /// @return A fixed proxy with the provided identity.
77 template<typename Prx = ObjectPrx, std::enable_if_t<std::is_base_of_v<ObjectPrx, Prx>, bool> = true>
78 [[nodiscard]] Prx createProxy(Identity id) const
79 {
80 return uncheckedCast<Prx>(_createProxy(std::move(id)));
81 }
82
83 /// Associates an object adapter with this connection. When a connection receives a request, it dispatches this
84 /// request using its associated object adapter. If the associated object adapter is null, the connection
85 /// rejects any incoming request with an ObjectNotExistException.
86 /// The default object adapter of an incoming connection is the object adapter that created this connection;
87 /// the default object adapter of an outgoing connection is the communicator's default object adapter.
88 /// @param adapter The object adapter to associate with this connection.
89 /// @see Communicator::getDefaultObjectAdapter
90 /// @see #getAdapter
91 virtual void setAdapter(const ObjectAdapterPtr& adapter) = 0;
92
93 /// Gets the object adapter associated with this connection.
94 /// @return The object adapter associated with this connection.
95 /// @see #setAdapter
96 [[nodiscard]] virtual ObjectAdapterPtr getAdapter() const noexcept = 0;
97
98 /// Gets the endpoint from which the connection was created.
99 /// @return The endpoint from which the connection was created.
100 [[nodiscard]] virtual EndpointPtr getEndpoint() const noexcept = 0;
101
102 /// Flushes any pending batch requests for this connection. This corresponds to all batch requests invoked on
103 /// fixed proxies associated with the connection.
104 /// @param compress Specifies whether or not the queued batch requests should be compressed before being sent
105 /// over the wire.
107
108 /// Flushes any pending batch requests for this connection. This corresponds to all batch requests invoked on
109 /// fixed proxies associated with the connection.
110 /// @param compress Specifies whether or not the queued batch requests should be compressed before being sent
111 /// over the wire.
112 /// @param exception The exception callback.
113 /// @param sent The sent callback.
114 /// @return A function that can be called to cancel the invocation locally.
115 virtual std::function<void()> flushBatchRequestsAsync(
116 CompressBatch compress,
117 std::function<void(std::exception_ptr)> exception,
118 std::function<void(bool)> sent = nullptr) = 0;
119
120 /// Flushes any pending batch requests for this connection. This corresponds to all batch requests invoked on
121 /// fixed proxies associated with the connection.
122 /// @param compress Specifies whether or not the queued batch requests should be compressed before being sent
123 /// over the wire.
124 /// @return A future that becomes available when the flush completes.
125 [[nodiscard]] std::future<void> flushBatchRequestsAsync(CompressBatch compress);
126
127 /// Sets a close callback on the connection. The callback is called by the connection when it's closed. The
128 /// callback is called from the Ice thread pool associated with the connection.
129 /// @param callback The close callback object.
130 virtual void setCloseCallback(CloseCallback callback) = 0;
131
132 /// Disables the inactivity check on this connection.
133 virtual void disableInactivityCheck() noexcept = 0;
134
135 /// Returns the connection type. This corresponds to the endpoint type, such as "tcp", "udp", etc.
136 /// @return The type of the connection.
137 [[nodiscard]] virtual const std::string& type() const noexcept = 0;
138
139 /// Returns a description of the connection as human readable text, suitable for logging or error messages.
140 /// @return The description of the connection as human readable text.
141 /// @remark This function remains usable after the connection is closed or aborted.
142 [[nodiscard]] virtual std::string toString() const = 0;
143
144 /// Returns the connection information.
145 /// @return The connection information.
146 [[nodiscard]] virtual ConnectionInfoPtr getInfo() const = 0;
147
148 /// Sets the size of the receive and send buffers.
149 /// @param rcvSize The size of the receive buffer.
150 /// @param sndSize The size of the send buffer.
151 virtual void setBufferSize(int rcvSize, int sndSize) = 0;
152
153 /// Throws an exception that provides the reason for the closure of this connection. For example, this function
154 /// throws CloseConnectionException when the connection was closed gracefully by the peer; it throws
155 /// ConnectionAbortedException when the connection is aborted with #abort. This function does nothing if the
156 /// connection is not yet closed.
157 virtual void throwException() const = 0;
158
159 protected:
160 /// @private
161 [[nodiscard]] virtual ObjectPrx _createProxy(Identity id) const = 0;
162 };
163
164 /// Base class for all connection info classes.
165 /// @headerfile Ice/Ice.h
166 class ICE_API ConnectionInfo
167 {
168 public:
169 virtual ~ConnectionInfo();
170
171 // Deleted to prevent accidental slicing.
172 ConnectionInfo(const ConnectionInfo&) = delete;
173 ConnectionInfo& operator=(const ConnectionInfo&) = delete;
174
175 /// The information of the underlying transport or nullptr if there's no underlying transport.
177
178 /// Indicates whether the connection is an incoming connection.
179 const bool incoming;
180
181 /// The name of the adapter associated with the connection.
182 const std::string adapterName;
183
184 /// The connection ID.
185 const std::string connectionId;
186
187 protected:
188 /// @private
189 explicit ConnectionInfo(ConnectionInfoPtr underlyingInfo)
190 : underlying{std::move(underlyingInfo)},
194 {
195 }
196
197 /// @private
198 ConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
199 : incoming{incoming},
200 adapterName{std::move(adapterName)},
201 connectionId{std::move(connectionId)}
202 {
203 }
204 };
205
206 /// Provides access to the connection details of an IP connection.
207 /// @headerfile Ice/Ice.h
208 class ICE_API IPConnectionInfo : public ConnectionInfo
209 {
210 public:
211 IPConnectionInfo(const IPConnectionInfo&) = delete;
212 IPConnectionInfo& operator=(const IPConnectionInfo&) = delete;
213
214 ~IPConnectionInfo() override;
215
216 /// The local address.
217 const std::string localAddress;
218
219 /// The local port.
220 const int localPort;
221
222 /// The remote address.
223 const std::string remoteAddress;
224
225 /// The remote port.
226 const int remotePort;
227
228 protected:
229 /// @private
230 IPConnectionInfo(
231 bool incoming,
232 std::string adapterName,
233 std::string connectionId,
234 std::string localAddress,
235 int localPort,
236 std::string remoteAddress,
237 int remotePort)
238 : ConnectionInfo{incoming, std::move(adapterName), std::move(connectionId)},
239 localAddress{std::move(localAddress)},
241 remoteAddress{std::move(remoteAddress)},
243 {
244 }
245 };
246
247 /// Provides access to the connection details of a TCP connection.
248 /// @headerfile Ice/Ice.h
249 class ICE_API TCPConnectionInfo final : public IPConnectionInfo
250 {
251 public:
252 ~TCPConnectionInfo() final;
253 TCPConnectionInfo(const TCPConnectionInfo&) = delete;
254 TCPConnectionInfo& operator=(const TCPConnectionInfo&) = delete;
255
256 /// The size of the receive buffer.
257 const int rcvSize;
258
259 /// The size of the send buffer.
260 const int sndSize;
261
262 /// @private
263 TCPConnectionInfo(
264 bool incoming,
265 std::string adapterName,
266 std::string connectionId,
267 std::string localAddress,
268 int localPort,
269 std::string remoteAddress,
270 int remotePort,
271 int rcvSize,
272 int sndSize)
273 : IPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), std::move(localAddress), localPort, std::move(remoteAddress), remotePort},
276 {
277 }
278
279 /// @private
280 TCPConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
281 : TCPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), "", -1, "", -1, 0, 0}
282 {
283 }
284 };
285
286 /// Provides access to the connection details of a UDP connection.
287 /// @headerfile Ice/Ice.h
288 class ICE_API UDPConnectionInfo final : public IPConnectionInfo
289 {
290 public:
291 ~UDPConnectionInfo() final;
292 UDPConnectionInfo(const UDPConnectionInfo&) = delete;
293 UDPConnectionInfo& operator=(const UDPConnectionInfo&) = delete;
294
295 /// The multicast address.
296 const std::string mcastAddress;
297
298 /// The multicast port.
299 const int mcastPort;
300
301 /// The size of the receive buffer.
302 const int rcvSize;
303
304 /// The size of the send buffer.
305 const int sndSize;
306
307 /// @private
308 UDPConnectionInfo(
309 bool incoming,
310 std::string adapterName,
311 std::string connectionId,
312 std::string localAddress,
313 int localPort,
314 std::string remoteAddress,
315 int remotePort,
316 std::string mcastAddress,
317 int mcastPort,
318 int rcvSize,
319 int sndSize)
320 : IPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), std::move(localAddress), localPort, std::move(remoteAddress), remotePort},
321 mcastAddress{std::move(mcastAddress)},
325 {
326 }
327
328 /// @private
329 UDPConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
330 : UDPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), "", -1, "", -1, "", -1, 0, 0}
331 {
332 }
333 };
334
335 /// Provides access to the connection details of a WebSocket connection.
336 /// @headerfile Ice/Ice.h
337 class ICE_API WSConnectionInfo final : public ConnectionInfo
338 {
339 public:
340 ~WSConnectionInfo() final;
341 WSConnectionInfo(const WSConnectionInfo&) = delete;
342 WSConnectionInfo& operator=(const WSConnectionInfo&) = delete;
343
344 /// The headers from the HTTP upgrade request.
346
347 /// @private
349 : ConnectionInfo{std::move(underlying)},
350 headers{std::move(headers)}
351 {
352 }
353 };
354
355 /// Provides access to the connection details of an IAP connection.
356 /// @headerfile Ice/Ice.h
357 class IAPConnectionInfo final : public ConnectionInfo
358 {
359 public:
360 ~IAPConnectionInfo() final;
361 IAPConnectionInfo(const IAPConnectionInfo&) = delete;
362 IAPConnectionInfo& operator=(const IAPConnectionInfo&) = delete;
363
364 /// The accessory name.
365 const std::string name;
366
367 /// The accessory manufacturer.
368 const std::string manufacturer;
369
370 /// The accessory model number.
371 const std::string modelNumber;
372
373 /// The accessory firmware revision.
374 const std::string firmwareRevision;
375
376 /// The accessory hardware revision.
377 const std::string hardwareRevision;
378
379 /// The protocol used by the accessory.
380 const std::string protocol;
381
382 /// @private
383 IAPConnectionInfo(
384 std::string adapterName,
385 std::string connectionId,
386 std::string name,
387 std::string manufacturer,
388 std::string modelNumber,
389 std::string firmwareRevision,
390 std::string hardwareRevision,
391 std::string protocol)
392 : ConnectionInfo{false, std::move(adapterName), std::move(connectionId)},
393 name{std::move(name)},
394 manufacturer{std::move(manufacturer)},
395 modelNumber{std::move(modelNumber)},
398 protocol{std::move(protocol)}
399 {
400 }
401 };
402}
403
404#if defined(__clang__)
405# pragma clang diagnostic pop
406#elif defined(__GNUC__)
407# pragma GCC diagnostic pop
408#endif
409
410#endif
const std::string connectionId
The connection ID.
Definition Connection.h:185
const bool incoming
Indicates whether the connection is an incoming connection.
Definition Connection.h:179
const std::string adapterName
The name of the adapter associated with the connection.
Definition Connection.h:182
const ConnectionInfoPtr underlying
The information of the underlying transport or nullptr if there's no underlying transport.
Definition Connection.h:176
Base class for all connection info classes.
Definition Connection.h:167
virtual std::function< void()> flushBatchRequestsAsync(CompressBatch compress, std::function< void(std::exception_ptr)> exception, std::function< void(bool)> sent=nullptr)=0
Flushes any pending batch requests for this connection.
virtual ObjectAdapterPtr getAdapter() const noexcept=0
Gets the object adapter associated with this connection.
virtual void setAdapter(const ObjectAdapterPtr &adapter)=0
Associates an object adapter with this connection.
virtual void close(std::function< void()> response, std::function< void(std::exception_ptr)> exception) noexcept=0
Starts a graceful closure of this connection once all outstanding invocations have completed.
virtual ConnectionInfoPtr getInfo() const =0
Returns the connection information.
virtual void setBufferSize(int rcvSize, int sndSize)=0
Sets the size of the receive and send buffers.
virtual std::string toString() const =0
Returns a description of the connection as human readable text, suitable for logging or error message...
virtual void setCloseCallback(CloseCallback callback)=0
Sets a close callback on the connection.
Prx createProxy(Identity id) const
Creates a special proxy (a "fixed proxy") that always uses this connection.
Definition Connection.h:78
virtual void disableInactivityCheck() noexcept=0
Disables the inactivity check on this connection.
virtual void abort() noexcept=0
Aborts this connection.
virtual EndpointPtr getEndpoint() const noexcept=0
Gets the endpoint from which the connection was created.
void flushBatchRequests(CompressBatch compress)
Flushes any pending batch requests for this connection.
virtual const std::string & type() const noexcept=0
Returns the connection type.
virtual void throwException() const =0
Throws an exception that provides the reason for the closure of this connection.
Represents a connection that uses the Ice protocol.
Definition Connection.h:51
const std::string manufacturer
The accessory manufacturer.
Definition Connection.h:368
const std::string firmwareRevision
The accessory firmware revision.
Definition Connection.h:374
const std::string protocol
The protocol used by the accessory.
Definition Connection.h:380
const std::string modelNumber
The accessory model number.
Definition Connection.h:371
const std::string name
The accessory name.
Definition Connection.h:365
const std::string hardwareRevision
The accessory hardware revision.
Definition Connection.h:377
const int localPort
The local port.
Definition Connection.h:220
const std::string localAddress
The local address.
Definition Connection.h:217
const int remotePort
The remote port.
Definition Connection.h:226
const std::string remoteAddress
The remote address.
Definition Connection.h:223
The base class for all Ice proxies.
Definition Proxy.h:232
const int rcvSize
The size of the receive buffer.
Definition Connection.h:257
const int sndSize
The size of the send buffer.
Definition Connection.h:260
Provides access to the connection details of a TCP connection.
Definition Connection.h:250
const int rcvSize
The size of the receive buffer.
Definition Connection.h:302
const int mcastPort
The multicast port.
Definition Connection.h:299
const int sndSize
The size of the send buffer.
Definition Connection.h:305
const std::string mcastAddress
The multicast address.
Definition Connection.h:296
Provides access to the connection details of a UDP connection.
Definition Connection.h:289
const HeaderDict headers
The headers from the HTTP upgrade request.
Definition Connection.h:345
std::shared_ptr< ConnectionInfo > ConnectionInfoPtr
A shared pointer to a ConnectionInfo.
Definition ConnectionF.h:21
std::shared_ptr< ObjectAdapter > ObjectAdapterPtr
A shared pointer to an ObjectAdapter.
Prx uncheckedCast(const ObjectPrx &proxy)
Creates a new proxy from an existing proxy.
std::shared_ptr< Endpoint > EndpointPtr
A shared pointer to an Endpoint.
Definition EndpointF.h:20
std::map< std::string, std::string, std::less<> > HeaderDict
Represents a collection of HTTP headers.
Definition Connection.h:42
std::function< void(const ConnectionPtr &con)> CloseCallback
The callback function given to Connection::setCloseCallback.
Definition Connection.h:46
std::shared_ptr< Connection > ConnectionPtr
A shared pointer to a Connection.
Definition ConnectionF.h:18
CompressBatch
Represents batch compression options when flushing queued batch requests.
Definition Connection.h:30
@ BasedOnProxy
Compress the batch requests if at least one request was made on a compressed proxy.
Definition Connection.h:38
@ Yes
Compress the batch requests.
Definition Connection.h:32
@ No
Don't compress the batch requests.
Definition Connection.h:35
The Ice RPC framework.
Definition SampleEvent.h:59
Represents the identity of an Ice object.
Definition Identity.h:40