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 /// Returns the connection type. This corresponds to the endpoint type, such as "tcp", "udp", etc.
133 /// @return The type of the connection.
134 [[nodiscard]] virtual const std::string& type() const noexcept = 0;
135
136 /// Returns a description of the connection as human readable text, suitable for logging or error messages.
137 /// @return The description of the connection as human readable text.
138 /// @remark This function remains usable after the connection is closed or aborted.
139 [[nodiscard]] virtual std::string toString() const = 0;
140
141 /// Returns the connection information.
142 /// @return The connection information.
143 [[nodiscard]] virtual ConnectionInfoPtr getInfo() const = 0;
144
145 /// Sets the size of the receive and send buffers.
146 /// @param rcvSize The size of the receive buffer.
147 /// @param sndSize The size of the send buffer.
148 virtual void setBufferSize(int rcvSize, int sndSize) = 0;
149
150 /// Throws an exception that provides the reason for the closure of this connection. For example, this function
151 /// throw CloseConnectionException when the connection was closed gracefully by the peer; it throws
152 /// ConnectionAbortedException when the connection is aborted with #abort. This function does nothing if the
153 /// connection is not yet closed.
154 virtual void throwException() const = 0;
155
156 protected:
157 /// @private
158 [[nodiscard]] virtual ObjectPrx _createProxy(Identity id) const = 0;
159 };
160
161 /// Base class for all connection info classes.
162 /// @headerfile Ice/Ice.h
163 class ICE_API ConnectionInfo
164 {
165 public:
166 virtual ~ConnectionInfo();
167
168 // Deleted to prevent accidental slicing.
169 ConnectionInfo(const ConnectionInfo&) = delete;
170 ConnectionInfo& operator=(const ConnectionInfo&) = delete;
171
172 /// The information of the underlying transport or nullptr if there's no underlying transport.
174
175 /// Indicates whether the connection is an incoming connection.
176 const bool incoming;
177
178 /// The name of the adapter associated with the connection.
179 const std::string adapterName;
180
181 /// The connection ID.
182 const std::string connectionId;
183
184 protected:
185 /// @private
186 explicit ConnectionInfo(ConnectionInfoPtr underlyingInfo)
187 : underlying{std::move(underlyingInfo)},
191 {
192 }
193
194 /// @private
195 ConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
196 : incoming{incoming},
197 adapterName{std::move(adapterName)},
198 connectionId{std::move(connectionId)}
199 {
200 }
201 };
202
203 /// Provides access to the connection details of an IP connection.
204 /// @headerfile Ice/Ice.h
205 class ICE_API IPConnectionInfo : public ConnectionInfo
206 {
207 public:
208 IPConnectionInfo(const IPConnectionInfo&) = delete;
209 IPConnectionInfo& operator=(const IPConnectionInfo&) = delete;
210
211 ~IPConnectionInfo() override;
212
213 /// The local address.
214 const std::string localAddress;
215
216 /// The local port.
217 const int localPort;
218
219 /// The remote address.
220 const std::string remoteAddress;
221
222 /// The remote port.
223 const int remotePort;
224
225 protected:
226 /// @private
227 IPConnectionInfo(
228 bool incoming,
229 std::string adapterName,
230 std::string connectionId,
231 std::string localAddress,
232 int localPort,
233 std::string remoteAddress,
234 int remotePort)
235 : ConnectionInfo{incoming, std::move(adapterName), std::move(connectionId)},
236 localAddress{std::move(localAddress)},
238 remoteAddress{std::move(remoteAddress)},
240 {
241 }
242 };
243
244 /// Provides access to the connection details of a TCP connection.
245 /// @headerfile Ice/Ice.h
246 class ICE_API TCPConnectionInfo final : public IPConnectionInfo
247 {
248 public:
249 ~TCPConnectionInfo() final;
250 TCPConnectionInfo(const TCPConnectionInfo&) = delete;
251 TCPConnectionInfo& operator=(const TCPConnectionInfo&) = delete;
252
253 /// The size of the receive buffer.
254 const int rcvSize;
255
256 /// The size of the send buffer.
257 const int sndSize;
258
259 /// @private
260 TCPConnectionInfo(
261 bool incoming,
262 std::string adapterName,
263 std::string connectionId,
264 std::string localAddress,
265 int localPort,
266 std::string remoteAddress,
267 int remotePort,
268 int rcvSize,
269 int sndSize)
270 : IPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), std::move(localAddress), localPort, std::move(remoteAddress), remotePort},
273 {
274 }
275
276 /// @private
277 TCPConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
278 : TCPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), "", -1, "", -1, 0, 0}
279 {
280 }
281 };
282
283 /// Provides access to the connection details of a UDP connection.
284 /// @headerfile Ice/Ice.h
285 class ICE_API UDPConnectionInfo final : public IPConnectionInfo
286 {
287 public:
288 ~UDPConnectionInfo() final;
289 UDPConnectionInfo(const UDPConnectionInfo&) = delete;
290 UDPConnectionInfo& operator=(const UDPConnectionInfo&) = delete;
291
292 /// The multicast address.
293 const std::string mcastAddress;
294
295 /// The multicast port.
296 const int mcastPort;
297
298 /// The size of the receive buffer.
299 const int rcvSize;
300
301 /// The size of the send buffer.
302 const int sndSize;
303
304 /// @private
305 UDPConnectionInfo(
306 bool incoming,
307 std::string adapterName,
308 std::string connectionId,
309 std::string localAddress,
310 int localPort,
311 std::string remoteAddress,
312 int remotePort,
313 std::string mcastAddress,
314 int mcastPort,
315 int rcvSize,
316 int sndSize)
317 : IPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), std::move(localAddress), localPort, std::move(remoteAddress), remotePort},
318 mcastAddress{std::move(mcastAddress)},
322 {
323 }
324
325 /// @private
326 UDPConnectionInfo(bool incoming, std::string adapterName, std::string connectionId)
327 : UDPConnectionInfo{incoming, std::move(adapterName), std::move(connectionId), "", -1, "", -1, "", -1, 0, 0}
328 {
329 }
330 };
331
332 /// Provides access to the connection details of a WebSocket connection.
333 /// @headerfile Ice/Ice.h
334 class ICE_API WSConnectionInfo final : public ConnectionInfo
335 {
336 public:
337 ~WSConnectionInfo() final;
338 WSConnectionInfo(const WSConnectionInfo&) = delete;
339 WSConnectionInfo& operator=(const WSConnectionInfo&) = delete;
340
341 /// The headers from the HTTP upgrade request.
343
344 /// @private
346 : ConnectionInfo{std::move(underlying)},
347 headers{std::move(headers)}
348 {
349 }
350 };
351
352 /// Provides access to the connection details of an IAP connection.
353 /// @headerfile Ice/Ice.h
354 class IAPConnectionInfo final : public ConnectionInfo
355 {
356 public:
357 ~IAPConnectionInfo() final;
358 IAPConnectionInfo(const IAPConnectionInfo&) = delete;
359 IAPConnectionInfo& operator=(const IAPConnectionInfo&) = delete;
360
361 /// The accessory name.
362 const std::string name;
363
364 /// The accessory manufacturer.
365 const std::string manufacturer;
366
367 /// The accessory model number.
368 const std::string modelNumber;
369
370 /// The accessory firmware revision.
371 const std::string firmwareRevision;
372
373 /// The accessory hardware revision.
374 const std::string hardwareRevision;
375
376 /// The protocol used by the accessory.
377 const std::string protocol;
378
379 /// @private
380 IAPConnectionInfo(
381 std::string adapterName,
382 std::string connectionId,
383 std::string name,
384 std::string manufacturer,
385 std::string modelNumber,
386 std::string firmwareRevision,
387 std::string hardwareRevision,
388 std::string protocol)
389 : ConnectionInfo{false, std::move(adapterName), std::move(connectionId)},
390 name{std::move(name)},
391 manufacturer{std::move(manufacturer)},
392 modelNumber{std::move(modelNumber)},
395 protocol{std::move(protocol)}
396 {
397 }
398 };
399}
400
401#if defined(__clang__)
402# pragma clang diagnostic pop
403#elif defined(__GNUC__)
404# pragma GCC diagnostic pop
405#endif
406
407#endif
const std::string connectionId
The connection ID.
Definition Connection.h:182
const bool incoming
Indicates whether the connection is an incoming connection.
Definition Connection.h:176
const std::string adapterName
The name of the adapter associated with the connection.
Definition Connection.h:179
const ConnectionInfoPtr underlying
The information of the underlying transport or nullptr if there's no underlying transport.
Definition Connection.h:173
Base class for all connection info classes.
Definition Connection.h:164
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 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:365
const std::string firmwareRevision
The accessory firmware revision.
Definition Connection.h:371
const std::string protocol
The protocol used by the accessory.
Definition Connection.h:377
const std::string modelNumber
The accessory model number.
Definition Connection.h:368
const std::string name
The accessory name.
Definition Connection.h:362
const std::string hardwareRevision
The accessory hardware revision.
Definition Connection.h:374
const int localPort
The local port.
Definition Connection.h:217
const std::string localAddress
The local address.
Definition Connection.h:214
const int remotePort
The remote port.
Definition Connection.h:223
const std::string remoteAddress
The remote address.
Definition Connection.h:220
The base class for all Ice proxies.
Definition Proxy.h:232
const int rcvSize
The size of the receive buffer.
Definition Connection.h:254
const int sndSize
The size of the send buffer.
Definition Connection.h:257
Provides access to the connection details of a TCP connection.
Definition Connection.h:247
const int rcvSize
The size of the receive buffer.
Definition Connection.h:299
const int mcastPort
The multicast port.
Definition Connection.h:296
const int sndSize
The size of the send buffer.
Definition Connection.h:302
const std::string mcastAddress
The multicast address.
Definition Connection.h:293
Provides access to the connection details of a UDP connection.
Definition Connection.h:286
const HeaderDict headers
The headers from the HTTP upgrade request.
Definition Connection.h:342
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