azure-core-amqp
Loading...
Searching...
No Matches
connection.hpp
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#pragma once
5
6#include "azure/core/amqp/internal/models/amqp_protocol.hpp"
7#include "azure/core/amqp/models/amqp_value.hpp"
8#include "common/async_operation_queue.hpp"
9#include "connection_string_credential.hpp"
10#include "session.hpp"
11
12#include <azure/core/credentials/credentials.hpp>
13
14#include <chrono>
15#include <limits>
16#include <memory>
17#include <string>
18
19namespace Azure { namespace Core { namespace Amqp { namespace _detail {
20 class ConnectionImpl;
21 class ConnectionFactory;
22}}}} // namespace Azure::Core::Amqp::_detail
23
24#if defined(_azure_TESTING_BUILD)
25// Define the test classes dependant on this class here.
26namespace Azure { namespace Core { namespace Amqp { namespace Tests {
27 namespace MessageTests {
28 class AmqpServerMock;
29 class MessageListenerEvents;
30 } // namespace MessageTests
31
32 class TestConnections_ConnectionAttributes_Test;
33 class TestConnections_ConnectionOpenClose_Test;
34 class TestConnections_ConnectionListenClose_Test;
35 class TestSocketListenerEvents;
36 class LinkSocketListenerEvents;
37 class TestLinks_LinkAttachDetach_Test;
38 class TestSessions_MultipleSessionBeginEnd_Test;
39 class TestMessages_SenderOpenClose_Test;
40 class TestMessages_TestLocalhostVsTls_Test;
41 class TestMessages_SenderSendAsync_Test;
42 class TestMessages_SenderOpenClose_Test;
43 class TestMessages_ReceiverOpenClose_Test;
44 class TestMessages_ReceiverReceiveAsync_Test;
45
46}}}} // namespace Azure::Core::Amqp::Tests
47#endif // _azure_TESTING_BUILD
48#if defined(SAMPLES_BUILD)
49namespace LocalServerSample {
50int LocalServerSampleMain();
51} // namespace LocalServerSample
52#endif // SAMPLES_BUILD
53
54namespace Azure { namespace Core { namespace Amqp { namespace _internal {
55 class Session;
56
57 class Error;
58
60 constexpr uint16_t AmqpPort = 5672;
61
63 constexpr uint16_t AmqpTlsPort = 5671;
64
75 enum class ConnectionState
76 {
82 Start,
87 HeaderReceived,
92 HeaderSent,
97 HeaderExchanged,
102 OpenPipe,
107 OcPipe,
112 OpenReceived,
117 OpenSent,
123 ClosePipe,
127 Opened,
134 CloseReceived,
141 CloseSent,
147 Discarding,
152 End,
153
158 Error,
159 };
160
161 std::ostream& operator<<(std::ostream& stream, ConnectionState value);
162
163 class Connection;
164
168 class ConnectionEvents {
169 protected:
170 virtual ~ConnectionEvents() = default;
171
172 public:
179 virtual void OnConnectionStateChanged(
180 Connection const& connection,
181 ConnectionState newState,
182 ConnectionState oldState)
183 = 0;
184
189 virtual void OnIOError(Connection const& connection) = 0;
190 };
191
192 class ConnectionEndpointEvents {
193 protected:
194 virtual ~ConnectionEndpointEvents() = default;
195
196 public:
206 virtual bool OnNewEndpoint(Connection const& connection, Endpoint& endpoint) = 0;
207 };
208
210 struct ConnectionOptions final
211 {
214 std::vector<std::string> AuthenticationScopes;
215
220 std::chrono::milliseconds IdleTimeout{std::chrono::minutes(1)};
221
231 uint32_t MaxFrameSize{(std::numeric_limits<uint32_t>::max)()};
232
238 uint16_t MaxChannelCount{65535};
239
246 Models::AmqpMap Properties;
247
253 uint16_t Port{AmqpTlsPort};
254
269 std::string ContainerId;
270
273 bool EnableTrace{false};
274 };
275
276 class Connection final {
277 public:
286 Connection(
287 std::string const& hostName,
288 std::shared_ptr<Credentials::TokenCredential> credential,
289 ConnectionOptions const& options,
290 ConnectionEvents* eventHandler = nullptr);
291
301 Connection(
302 Network::_internal::Transport const& transport,
303 ConnectionOptions const& options,
304 ConnectionEvents* eventHandler,
305 ConnectionEndpointEvents* endpointEvents);
306
308 ~Connection();
309
319 Session CreateSession(SessionOptions const& options = {}, SessionEvents* eventHandler = nullptr)
320 const;
321
332 Session CreateSession(
333 Endpoint& newEndpoint,
334 SessionOptions const& options = {},
335 SessionEvents* eventHandler = nullptr) const;
336
337 void Poll();
338
339 private:
350 void Open();
351
364 void Listen();
365
379 void Close(
380 std::string const& condition = {},
381 std::string const& description = {},
382 Models::AmqpValue info = {});
383
388 std::string GetHost() const;
389
394 uint16_t GetPort() const;
395
400 uint32_t GetMaxFrameSize() const;
401
406 uint32_t GetRemoteMaxFrameSize() const;
407
412 uint16_t GetMaxChannel() const;
413
418 std::chrono::milliseconds GetIdleTimeout() const;
419
424 Models::AmqpMap GetProperties() const;
425
442 void SetIdleEmptyFrameSendPercentage(double idleTimeoutEmptyFrameSendRatio);
443
444 private:
451 Connection(std::shared_ptr<_detail::ConnectionImpl> impl) : m_impl{impl} {}
452
453 std::shared_ptr<_detail::ConnectionImpl> m_impl;
454 friend class _detail::ConnectionFactory;
455#if _azure_TESTING_BUILD
456 friend class Azure::Core::Amqp::Tests::MessageTests::AmqpServerMock;
457 friend class Azure::Core::Amqp::Tests::MessageTests::MessageListenerEvents;
458 friend class Azure::Core::Amqp::Tests::TestSocketListenerEvents;
459 friend class Azure::Core::Amqp::Tests::LinkSocketListenerEvents;
460 friend class Azure::Core::Amqp::Tests::TestConnections_ConnectionAttributes_Test;
461 friend class Azure::Core::Amqp::Tests::TestConnections_ConnectionOpenClose_Test;
462 friend class Azure::Core::Amqp::Tests::TestConnections_ConnectionListenClose_Test;
463 friend class Azure::Core::Amqp::Tests::TestSessions_MultipleSessionBeginEnd_Test;
464 friend class Azure::Core::Amqp::Tests::TestLinks_LinkAttachDetach_Test;
465 friend class Azure::Core::Amqp::Tests::TestMessages_SenderOpenClose_Test;
466 friend class Azure::Core::Amqp::Tests::TestMessages_TestLocalhostVsTls_Test;
467 friend class Azure::Core::Amqp::Tests::TestMessages_SenderSendAsync_Test;
468 friend class Azure::Core::Amqp::Tests::TestMessages_SenderOpenClose_Test;
469
470#endif // _azure_TESTING_BUILD
471#if SAMPLES_BUILD
472 friend int LocalServerSample::LocalServerSampleMain();
473#endif // SAMPLES_BUILD
474 };
475}}}} // namespace Azure::Core::Amqp::_internal