Ice 3.9
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 for 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. The response and
66 /// exception callbacks must not throw any exception.
67 virtual void
68 close(std::function<void()> response, std::function<void(std::exception_ptr)> exception) noexcept = 0;
69
70 /// Starts a graceful closure of this connection once all outstanding invocations have completed.
71 /// @return A future that becomes available when the connection is closed.
72 [[nodiscard]] std::future<void> close();
73
74 /// Creates a special proxy (a "fixed proxy") that always uses this connection.
75 /// @tparam Prx The type of the proxy to create.
76 /// @param id The identity of the target object.
77 /// @return A fixed proxy with the provided identity.
78 /// @throws CommunicatorDestroyedException Thrown when the communicator has been destroyed.
79 template<typename Prx = ObjectPrx, std::enable_if_t<std::is_base_of_v<ObjectPrx, Prx>, bool> = true>
80 [[nodiscard]] Prx createProxy(Identity id) const
81 {
82 return uncheckedCast<Prx>(_createProxy(std::move(id)));
83 }
84
85 /// Associates an object adapter with this connection. When a connection receives a request, it dispatches this
86 /// request using its associated object adapter. If the associated object adapter is null, the connection
87 /// rejects any incoming request with an ObjectNotExistException.
88 /// The default object adapter of an incoming connection is the object adapter that created this connection;
89 /// the default object adapter of an outgoing connection is the communicator's default object adapter.
90 /// @param adapter The object adapter to associate with this connection.
91 /// @throws std::logic_error Thrown when this connection is an incoming connection: you can only call this
92 /// function on outgoing (client) connections.
93 /// @see Communicator::getDefaultObjectAdapter
94 /// @see #getAdapter
95 virtual void setAdapter(const ObjectAdapterPtr& adapter) = 0;
96
97 /// Gets the object adapter associated with this connection.
98 /// @return The object adapter associated with this connection.
99 /// @see #setAdapter
100 [[nodiscard]] virtual ObjectAdapterPtr getAdapter() const noexcept = 0;
101
102 /// Gets the endpoint from which the connection was created.
103 /// @return The endpoint from which the connection was created.
104 [[nodiscard]] virtual EndpointPtr getEndpoint() const noexcept = 0;
105
106 /// Flushes any pending batch requests for this connection.
107 /// This means all batch requests invoked on fixed proxies associated with the connection.
108 /// @param compress Specifies whether or not the queued batch requests should be compressed before being sent
109 /// over the wire.
110 /// @throws LocalException Thrown when the flush fails. For example, this function throws
111 /// CommunicatorDestroyedException when the communicator has been destroyed.
113
114 /// Flushes any pending batch requests for this connection.
115 /// This means all batch requests invoked on fixed proxies associated with the connection.
116 /// @param compress Specifies whether or not the queued batch requests should be compressed before being sent
117 /// over the wire.
118 /// @param exception The exception callback. The Ice runtime calls this function from an Ice thread pool
119 /// thread. If you set InitializationData::executor, the executor determines the thread that executes this
120 /// function.
121 /// @param sent The sent callback. The Ice runtime calls this function when the batch requests are accepted by
122 /// the transport. When the batch requests are accepted synchronously, the Ice runtime calls this function from
123 /// the current thread and passes `true` as argument. When the batch requests are accepted asynchronously, the
124 /// Ice runtime calls this function from an Ice thread pool thread and passes `false` as argument. If you set
125 /// InitializationData::executor, the executor determines the thread that executes this function in the
126 /// asynchronous case.
127 /// @return A function that can be called to cancel the invocation locally.
128 /// @throws CommunicatorDestroyedException Thrown synchronously when the communicator has been destroyed.
129 virtual std::function<void()> flushBatchRequestsAsync(
130 CompressBatch compress,
131 std::function<void(std::exception_ptr)> exception,
132 std::function<void(bool)> sent = nullptr) = 0;
133
134 /// Flushes any pending batch requests for this connection. This corresponds to all batch requests invoked on
135 /// fixed proxies associated with the connection.
136 /// @param compress Specifies whether or not the queued batch requests should be compressed before being sent
137 /// over the wire.
138 /// @return A future that becomes available when the flush completes.
139 /// @throws CommunicatorDestroyedException Thrown synchronously when the communicator has been destroyed.
140 [[nodiscard]] std::future<void> flushBatchRequestsAsync(CompressBatch compress);
141
142 /// Sets a close callback on the connection. The callback is called by the connection when it's closed.
143 /// The callback is called from the Ice thread pool associated with the connection.
144 /// @param callback The close callback object.
145 virtual void setCloseCallback(CloseCallback callback) = 0;
146
147 /// Disables the inactivity check on this connection.
148 virtual void disableInactivityCheck() noexcept = 0;
149
150 /// Returns the connection type. This corresponds to the endpoint type, such as "tcp", "udp", etc.
151 /// @return The type of the connection.
152 [[nodiscard]] virtual const std::string& type() const noexcept = 0;
153
154 /// Returns a description of the connection as human readable text, suitable for logging or error messages.
155 /// @return The description of the connection as human readable text.
156 /// @remark This function remains usable after the connection is closed or aborted.
157 [[nodiscard]] virtual std::string toString() const = 0;
158
159 /// Returns the connection information.
160 /// @return The connection information.
161 [[nodiscard]] virtual ConnectionInfoPtr getInfo() const = 0;
162
163 /// Sets the size of the receive and send buffers.
164 /// @param rcvSize The size of the receive buffer.
165 /// @param sndSize The size of the send buffer.
166 virtual void setBufferSize(int rcvSize, int sndSize) = 0;
167
168 /// Throws the exception that provides the reason for the closure of this connection. Does nothing if the
169 /// connection is not yet closing or closed.
170 /// @throws CloseConnectionException Thrown when the connection was closed gracefully by the peer.
171 /// @throws ConnectionAbortedException Thrown when the connection was aborted, for example with #abort.
172 virtual void throwException() const = 0;
173
174 protected:
175 /// @private
176 [[nodiscard]] virtual ObjectPrx _createProxy(Identity id) const = 0;
177 };
178
179 /// Base class for all connection info classes.
180 /// @headerfile Ice/Ice.h
181 class ICE_API ConnectionInfo
182 {
183 public:
184 virtual ~ConnectionInfo();
185
186 // Deleted to prevent accidental slicing.
187 ConnectionInfo(const ConnectionInfo&) = delete;
188 ConnectionInfo& operator=(const ConnectionInfo&) = delete;
189
190 /// The information of the underlying transport or nullptr if there's no underlying transport.
192
193 /// Indicates whether the connection is an incoming connection.
194 const bool incoming;
195
196 /// The name of the adapter associated with the connection.
197 const std::string adapterName;
198
199 /// The connection ID.
200 const std::string connectionId;
201
202 protected:
203 /// @private
204 explicit ConnectionInfo(ConnectionInfoPtr underlyingInfo)
205 : underlying{std::move(underlyingInfo)},
209 {
210 }
211
212 /// @private
213 ConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
214 : incoming{incoming},
215 adapterName{std::move(adapterName)},
216 connectionId{std::move(connectionId)}
217 {
218 }
219 };
220
221 /// Provides access to the connection details of an IP connection.
222 /// @headerfile Ice/Ice.h
223 class ICE_API IPConnectionInfo : public ConnectionInfo
224 {
225 public:
226 IPConnectionInfo(const IPConnectionInfo&) = delete;
227 IPConnectionInfo& operator=(const IPConnectionInfo&) = delete;
228
229 ~IPConnectionInfo() override;
230
231 /// The local address.
232 const std::string localAddress;
233
234 /// The local port.
235 const int localPort;
236
237 /// The remote address.
238 const std::string remoteAddress;
239
240 /// The remote port.
241 const int remotePort;
242
243 protected:
244 /// @private
245 IPConnectionInfo(
246 bool incoming,
247 std::string adapterName,
248 std::string connectionId,
249 std::string localAddress,
250 int localPort,
251 std::string remoteAddress,
252 int remotePort)
253 : ConnectionInfo{incoming, std::move(adapterName), std::move(connectionId)},
254 localAddress{std::move(localAddress)},
256 remoteAddress{std::move(remoteAddress)},
258 {
259 }
260 };
261
262 /// Provides access to the connection details of a TCP connection.
263 /// @headerfile Ice/Ice.h
264 class ICE_API TCPConnectionInfo final : public IPConnectionInfo
265 {
266 public:
267 ~TCPConnectionInfo() final;
268 TCPConnectionInfo(const TCPConnectionInfo&) = delete;
269 TCPConnectionInfo& operator=(const TCPConnectionInfo&) = delete;
270
271 /// The size of the receive buffer.
272 const int rcvSize;
273
274 /// The size of the send buffer.
275 const int sndSize;
276
277 /// @private
278 TCPConnectionInfo(
279 bool incoming,
280 std::string adapterName,
281 std::string connectionId,
282 std::string localAddress,
283 int localPort,
284 std::string remoteAddress,
285 int remotePort,
286 int rcvSize,
287 int sndSize)
288 : IPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), std::move(localAddress), localPort, std::move(remoteAddress), remotePort},
291 {
292 }
293
294 /// @private
295 TCPConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
296 : TCPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), "", -1, "", -1, 0, 0}
297 {
298 }
299 };
300
301 /// Provides access to the connection details of a UDP connection.
302 /// @headerfile Ice/Ice.h
303 class ICE_API UDPConnectionInfo final : public IPConnectionInfo
304 {
305 public:
306 ~UDPConnectionInfo() final;
307 UDPConnectionInfo(const UDPConnectionInfo&) = delete;
308 UDPConnectionInfo& operator=(const UDPConnectionInfo&) = delete;
309
310 /// The multicast address.
311 const std::string mcastAddress;
312
313 /// The multicast port.
314 const int mcastPort;
315
316 /// The size of the receive buffer.
317 const int rcvSize;
318
319 /// The size of the send buffer.
320 const int sndSize;
321
322 /// @private
323 UDPConnectionInfo(
324 bool incoming,
325 std::string adapterName,
326 std::string connectionId,
327 std::string localAddress,
328 int localPort,
329 std::string remoteAddress,
330 int remotePort,
331 std::string mcastAddress,
332 int mcastPort,
333 int rcvSize,
334 int sndSize)
335 : IPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), std::move(localAddress), localPort, std::move(remoteAddress), remotePort},
336 mcastAddress{std::move(mcastAddress)},
340 {
341 }
342
343 /// @private
344 UDPConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
345 : UDPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), "", -1, "", -1, "", -1, 0, 0}
346 {
347 }
348 };
349
350 /// Provides access to the connection details of a WebSocket connection.
351 /// @headerfile Ice/Ice.h
352 class ICE_API WSConnectionInfo final : public ConnectionInfo
353 {
354 public:
355 ~WSConnectionInfo() final;
356 WSConnectionInfo(const WSConnectionInfo&) = delete;
357 WSConnectionInfo& operator=(const WSConnectionInfo&) = delete;
358
359 /// The HTTP headers from the WebSocket upgrade handshake: the request headers for an incoming connection, and
360 /// the response headers for an outgoing connection.
362
363 /// @private
365 : ConnectionInfo{std::move(underlying)},
366 headers{std::move(headers)}
367 {
368 }
369 };
370
371 /// Provides access to the connection details of an IAP connection.
372 /// @headerfile Ice/Ice.h
373 class IAPConnectionInfo final : public ConnectionInfo
374 {
375 public:
376 ~IAPConnectionInfo() final;
377 IAPConnectionInfo(const IAPConnectionInfo&) = delete;
378 IAPConnectionInfo& operator=(const IAPConnectionInfo&) = delete;
379
380 /// The accessory name.
381 const std::string name;
382
383 /// The accessory manufacturer.
384 const std::string manufacturer;
385
386 /// The accessory model number.
387 const std::string modelNumber;
388
389 /// The accessory firmware revision.
390 const std::string firmwareRevision;
391
392 /// The accessory hardware revision.
393 const std::string hardwareRevision;
394
395 /// The protocol used by the accessory.
396 const std::string protocol;
397
398 /// @private
399 IAPConnectionInfo(
400 std::string adapterName,
401 std::string connectionId,
402 std::string name,
403 std::string manufacturer,
404 std::string modelNumber,
405 std::string firmwareRevision,
406 std::string hardwareRevision,
407 std::string protocol)
408 : ConnectionInfo{false, std::move(adapterName), std::move(connectionId)},
409 name{std::move(name)},
410 manufacturer{std::move(manufacturer)},
411 modelNumber{std::move(modelNumber)},
414 protocol{std::move(protocol)}
415 {
416 }
417 };
418}
419
420#if defined(__clang__)
421# pragma clang diagnostic pop
422#elif defined(__GNUC__)
423# pragma GCC diagnostic pop
424#endif
425
426#endif
const std::string connectionId
The connection ID.
Definition Connection.h:200
const bool incoming
Indicates whether the connection is an incoming connection.
Definition Connection.h:194
const std::string adapterName
The name of the adapter associated with the connection.
Definition Connection.h:197
const ConnectionInfoPtr underlying
The information of the underlying transport or nullptr if there's no underlying transport.
Definition Connection.h:191
Base class for all connection info classes.
Definition Connection.h:182
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:80
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 the 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:384
const std::string firmwareRevision
The accessory firmware revision.
Definition Connection.h:390
const std::string protocol
The protocol used by the accessory.
Definition Connection.h:396
const std::string modelNumber
The accessory model number.
Definition Connection.h:387
const std::string name
The accessory name.
Definition Connection.h:381
const std::string hardwareRevision
The accessory hardware revision.
Definition Connection.h:393
const int localPort
The local port.
Definition Connection.h:235
const std::string localAddress
The local address.
Definition Connection.h:232
const int remotePort
The remote port.
Definition Connection.h:241
const std::string remoteAddress
The remote address.
Definition Connection.h:238
The base class for all Ice proxies.
Definition Proxy.h:265
const int rcvSize
The size of the receive buffer.
Definition Connection.h:272
const int sndSize
The size of the send buffer.
Definition Connection.h:275
Provides access to the connection details of a TCP connection.
Definition Connection.h:265
const int rcvSize
The size of the receive buffer.
Definition Connection.h:317
const int mcastPort
The multicast port.
Definition Connection.h:314
const int sndSize
The size of the send buffer.
Definition Connection.h:320
const std::string mcastAddress
The multicast address.
Definition Connection.h:311
Provides access to the connection details of a UDP connection.
Definition Connection.h:304
const HeaderDict headers
The HTTP headers from the WebSocket upgrade handshake: the request headers for an incoming connection...
Definition Connection.h:361
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 for 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:60
Represents the identity of an Ice object.
Definition Identity.h:41