azure-core-amqp
Loading...
Searching...
No Matches
message_receiver.hpp
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#pragma once
5
6#include "azure/core/amqp/internal/amqp_settle_mode.hpp"
7#include "azure/core/amqp/internal/models/amqp_error.hpp"
8#include "azure/core/amqp/models/amqp_message.hpp"
9#include "azure/core/amqp/models/amqp_value.hpp"
10#include "claims_based_security.hpp"
11#include "common/async_operation_queue.hpp"
12#include "connection_string_credential.hpp"
13#include "link.hpp"
14#include "session.hpp"
15
16#include <azure/core/credentials/credentials.hpp>
17#include <azure/core/nullable.hpp>
18
19#include <vector>
20
21namespace Azure { namespace Core { namespace Amqp { namespace _detail {
22 class MessageReceiverImpl;
23 class MessageReceiverFactory;
24}}}} // namespace Azure::Core::Amqp::_detail
25
26#if defined(_azure_TESTING_BUILD)
27namespace Azure { namespace Core { namespace Amqp { namespace Tests { namespace MessageTests {
28 class MockServiceEndpoint;
29}}}}} // namespace Azure::Core::Amqp::Tests::MessageTests
30#endif
31
32namespace Azure { namespace Core { namespace Amqp { namespace _internal {
33 enum class MessageReceiverState
34 {
35 Invalid,
36 Idle,
37 Opening,
38 Open,
39 Closing,
40 Error,
41 };
42 std::ostream& operator<<(std::ostream& stream, _internal::MessageReceiverState state);
43
44 class MessageReceiver;
45
46 struct MessageReceiverOptions final
47 {
57 std::string Name;
58
68 ReceiverSettleMode SettleMode{ReceiverSettleMode::First};
69
71 Models::_internal::MessageTarget MessageTarget;
72
74 Nullable<uint32_t> InitialDeliveryCount;
75
77 Nullable<uint64_t> MaxMessageSize;
78
82 uint32_t MaxLinkCredit{};
83
85 Models::AmqpMap Properties;
86
88 bool EnableTrace{false};
89
91 bool AuthenticationRequired{true};
92 };
93
94 class MessageReceiverEvents {
95 protected:
96 ~MessageReceiverEvents() = default;
97
98 public:
99 virtual void OnMessageReceiverStateChanged(
100 MessageReceiver const& receiver,
101 MessageReceiverState newState,
102 MessageReceiverState oldState)
103 = 0;
104 virtual Models::AmqpValue OnMessageReceived(
105 MessageReceiver const& receiver,
106 std::shared_ptr<Models::AmqpMessage> const& message)
107 = 0;
108 virtual void OnMessageReceiverDisconnected(
109 MessageReceiver const& receiver,
110 Models::_internal::AmqpError const& error)
111 = 0;
112 };
113
136 class MessageReceiver final {
137 public:
138 ~MessageReceiver() noexcept;
139
140 MessageReceiver(MessageReceiver const&) = default;
141 MessageReceiver& operator=(MessageReceiver const&) = default;
142 MessageReceiver(MessageReceiver&&) = default;
143 MessageReceiver& operator=(MessageReceiver&&) = default;
144
149 void Open(Context const& context = {});
150
154 void Close(Context const& context = {});
155
160 std::string GetLinkName() const;
161
166 std::string GetSourceName() const;
167
174 std::pair<std::shared_ptr<const Models::AmqpMessage>, Models::_internal::AmqpError>
175 WaitForIncomingMessage(Context const& context = {});
176
182 std::pair<std::shared_ptr<const Models::AmqpMessage>, Models::_internal::AmqpError>
183 TryWaitForIncomingMessage();
184
185 private:
186 MessageReceiver(std::shared_ptr<_detail::MessageReceiverImpl> impl) : m_impl{impl} {}
187 friend class _detail::MessageReceiverFactory;
188 std::shared_ptr<_detail::MessageReceiverImpl> m_impl;
189
190#if _azure_TESTING_BUILD
191 friend class Azure::Core::Amqp::Tests::MessageTests::MockServiceEndpoint;
192
193 // There is a deadlock associated with the link polling if it is enabled from an AMQP event
194 // callback. To work around this, link polling is disabled when creating a message receiver from
195 // an existing link endpoint. This method should be called to enable it at a time when it is
196 // safer to enable link polling.
197
198 // This is a test hook and should not be used outside of test code.
199 void EnableLinkPolling();
200#endif
201 };
202}}}} // namespace Azure::Core::Amqp::_internal