| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Diagnostics; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using Amqp; |
| | 11 | | using Azure.Amqp; |
| | 12 | | using Core; |
| | 13 | | using Primitives; |
| | 14 | |
|
| | 15 | | internal class MessageSession : MessageReceiver, IMessageSession |
| | 16 | | { |
| | 17 | | private readonly ServiceBusDiagnosticSource diagnosticSource; |
| | 18 | |
|
| | 19 | | public MessageSession( |
| | 20 | | string entityPath, |
| | 21 | | MessagingEntityType? entityType, |
| | 22 | | ReceiveMode receiveMode, |
| | 23 | | ServiceBusConnection serviceBusConnection, |
| | 24 | | ICbsTokenProvider cbsTokenProvider, |
| | 25 | | RetryPolicy retryPolicy, |
| | 26 | | int prefetchCount = Constants.DefaultClientPrefetchCount, |
| | 27 | | string sessionId = null, |
| | 28 | | bool isSessionReceiver = false) |
| 0 | 29 | | : base(entityPath, entityType, receiveMode, serviceBusConnection, cbsTokenProvider, retryPolicy, prefetchCou |
| | 30 | | { |
| 0 | 31 | | this.diagnosticSource = new ServiceBusDiagnosticSource(entityPath, serviceBusConnection.Endpoint); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Gets the time that the session identified by <see cref="SessionId"/> is locked until for this client. |
| | 36 | | /// </summary> |
| | 37 | | public DateTime LockedUntilUtc |
| | 38 | | { |
| 0 | 39 | | get => this.LockedUntilUtcInternal; |
| 0 | 40 | | internal set => this.LockedUntilUtcInternal = value; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets the SessionId. |
| | 45 | | /// </summary> |
| 0 | 46 | | public string SessionId => this.SessionIdInternal; |
| | 47 | |
|
| | 48 | | public Task<byte[]> GetStateAsync() |
| | 49 | | { |
| 0 | 50 | | this.ThrowIfClosed(); |
| 0 | 51 | | return ServiceBusDiagnosticSource.IsEnabled() ? this.OnGetStateInstrumentedAsync() : this.OnGetStateAsync(); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | public Task SetStateAsync(byte[] sessionState) |
| | 55 | | { |
| 0 | 56 | | this.ThrowIfClosed(); |
| 0 | 57 | | return ServiceBusDiagnosticSource.IsEnabled() ? this.OnSetStateInstrumentedAsync(sessionState) : this.OnSetS |
| | 58 | | } |
| | 59 | |
|
| | 60 | | public Task RenewSessionLockAsync() |
| | 61 | | { |
| 0 | 62 | | this.ThrowIfClosed(); |
| 0 | 63 | | return ServiceBusDiagnosticSource.IsEnabled() ? this.OnRenewSessionLockInstrumentedAsync() : this.OnRenewSes |
| | 64 | | } |
| | 65 | |
|
| | 66 | | protected override void OnMessageHandler(MessageHandlerOptions registerHandlerOptions, Func<Message, Cancellatio |
| | 67 | | { |
| 0 | 68 | | throw new InvalidOperationException($"{nameof(RegisterMessageHandler)} is not supported for Sessions."); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | protected override Task<DateTime> OnRenewLockAsync(string lockToken) |
| | 72 | | { |
| 0 | 73 | | throw new InvalidOperationException($"{nameof(RenewLockAsync)} is not supported for Session. Use {nameof(Ren |
| | 74 | | } |
| | 75 | |
|
| | 76 | | protected async Task<byte[]> OnGetStateAsync() |
| | 77 | | { |
| | 78 | | try |
| | 79 | | { |
| 0 | 80 | | var amqpRequestMessage = AmqpRequestMessage.CreateRequest(ManagementConstants.Operations.GetSessionState |
| | 81 | |
|
| 0 | 82 | | if (this.ReceiveLinkManager.TryGetOpenedObject(out var receiveLink)) |
| | 83 | | { |
| 0 | 84 | | amqpRequestMessage.AmqpMessage.ApplicationProperties.Map[ManagementConstants.Request.AssociatedLinkN |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | amqpRequestMessage.Map[ManagementConstants.Properties.SessionId] = this.SessionIdInternal; |
| | 88 | |
|
| 0 | 89 | | var amqpResponseMessage = await this.ExecuteRequestResponseAsync(amqpRequestMessage).ConfigureAwait(fals |
| | 90 | |
|
| 0 | 91 | | byte[] sessionState = null; |
| 0 | 92 | | if (amqpResponseMessage.StatusCode == AmqpResponseStatusCode.OK) |
| | 93 | | { |
| 0 | 94 | | if (amqpResponseMessage.Map[ManagementConstants.Properties.SessionState] != null) |
| | 95 | | { |
| 0 | 96 | | sessionState = amqpResponseMessage.GetValue<ArraySegment<byte>>(ManagementConstants.Properties.S |
| | 97 | | } |
| | 98 | | } |
| | 99 | | else |
| | 100 | | { |
| 0 | 101 | | throw amqpResponseMessage.ToMessagingContractException(); |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | return sessionState; |
| | 105 | | } |
| | 106 | | catch (Exception exception) |
| | 107 | | { |
| 0 | 108 | | throw AmqpExceptionHelper.GetClientException(exception); |
| | 109 | | } |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | protected async Task OnSetStateAsync(byte[] sessionState) |
| | 113 | | { |
| | 114 | | try |
| | 115 | | { |
| 0 | 116 | | var amqpRequestMessage = AmqpRequestMessage.CreateRequest(ManagementConstants.Operations.SetSessionState |
| | 117 | |
|
| 0 | 118 | | if (this.ReceiveLinkManager.TryGetOpenedObject(out var receiveLink)) |
| | 119 | | { |
| 0 | 120 | | amqpRequestMessage.AmqpMessage.ApplicationProperties.Map[ManagementConstants.Request.AssociatedLinkN |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | amqpRequestMessage.Map[ManagementConstants.Properties.SessionId] = this.SessionIdInternal; |
| | 124 | |
|
| 0 | 125 | | if (sessionState != null) |
| | 126 | | { |
| 0 | 127 | | var value = new ArraySegment<byte>(sessionState); |
| 0 | 128 | | amqpRequestMessage.Map[ManagementConstants.Properties.SessionState] = value; |
| | 129 | | } |
| | 130 | | else |
| | 131 | | { |
| 0 | 132 | | amqpRequestMessage.Map[ManagementConstants.Properties.SessionState] = null; |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | var amqpResponseMessage = await this.ExecuteRequestResponseAsync(amqpRequestMessage).ConfigureAwait(fals |
| 0 | 136 | | if (amqpResponseMessage.StatusCode != AmqpResponseStatusCode.OK) |
| | 137 | | { |
| 0 | 138 | | throw amqpResponseMessage.ToMessagingContractException(); |
| | 139 | | } |
| 0 | 140 | | } |
| | 141 | | catch (Exception exception) |
| | 142 | | { |
| 0 | 143 | | throw AmqpExceptionHelper.GetClientException(exception); |
| | 144 | | } |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | protected async Task OnRenewSessionLockAsync() |
| | 148 | | { |
| | 149 | | try |
| | 150 | | { |
| 0 | 151 | | var amqpRequestMessage = AmqpRequestMessage.CreateRequest(ManagementConstants.Operations.RenewSessionLoc |
| | 152 | |
|
| 0 | 153 | | if (this.ReceiveLinkManager.TryGetOpenedObject(out var receiveLink)) |
| | 154 | | { |
| 0 | 155 | | amqpRequestMessage.AmqpMessage.ApplicationProperties.Map[ManagementConstants.Request.AssociatedLinkN |
| | 156 | | } |
| | 157 | |
|
| 0 | 158 | | amqpRequestMessage.Map[ManagementConstants.Properties.SessionId] = this.SessionIdInternal; |
| | 159 | |
|
| 0 | 160 | | var amqpResponseMessage = await this.ExecuteRequestResponseAsync(amqpRequestMessage).ConfigureAwait(fals |
| | 161 | |
|
| 0 | 162 | | if (amqpResponseMessage.StatusCode == AmqpResponseStatusCode.OK) |
| | 163 | | { |
| 0 | 164 | | this.LockedUntilUtcInternal = amqpResponseMessage.GetValue<DateTime>(ManagementConstants.Properties. |
| | 165 | | } |
| | 166 | | else |
| | 167 | | { |
| 0 | 168 | | throw amqpResponseMessage.ToMessagingContractException(); |
| | 169 | | } |
| 0 | 170 | | } |
| | 171 | | catch (Exception exception) |
| | 172 | | { |
| 0 | 173 | | throw AmqpExceptionHelper.GetClientException(exception); |
| | 174 | | } |
| 0 | 175 | | } |
| | 176 | |
|
| | 177 | | /// <summary> |
| | 178 | | /// Throw an OperationCanceledException if the object is Closing. |
| | 179 | | /// </summary> |
| | 180 | | protected override void ThrowIfClosed() |
| | 181 | | { |
| 0 | 182 | | if (this.IsClosedOrClosing) |
| | 183 | | { |
| 0 | 184 | | throw new ObjectDisposedException($"MessageSession with Id '{this.ClientId}' has already been closed. Pl |
| | 185 | | } |
| 0 | 186 | | } |
| | 187 | |
|
| | 188 | | private async Task<byte[]> OnGetStateInstrumentedAsync() |
| | 189 | | { |
| 0 | 190 | | Activity activity = this.diagnosticSource.GetSessionStateStart(this.SessionId); |
| 0 | 191 | | Task<byte[]> getStateTask = null; |
| 0 | 192 | | byte[] state = null; |
| | 193 | |
|
| | 194 | | try |
| | 195 | | { |
| 0 | 196 | | getStateTask = this.OnGetStateAsync(); |
| 0 | 197 | | state = await getStateTask.ConfigureAwait(false); |
| 0 | 198 | | return state; |
| | 199 | | } |
| 0 | 200 | | catch (Exception ex) |
| | 201 | | { |
| 0 | 202 | | this.diagnosticSource.ReportException(ex); |
| 0 | 203 | | throw; |
| | 204 | | } |
| | 205 | | finally |
| | 206 | | { |
| 0 | 207 | | this.diagnosticSource.GetSessionStateStop(activity, this.SessionId, state, getStateTask?.Status); |
| | 208 | | } |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | private async Task OnSetStateInstrumentedAsync(byte[] sessionState) |
| | 212 | | { |
| 0 | 213 | | Activity activity = this.diagnosticSource.SetSessionStateStart(this.SessionId, sessionState); |
| 0 | 214 | | Task setStateTask = null; |
| | 215 | |
|
| | 216 | | try |
| | 217 | | { |
| 0 | 218 | | setStateTask = this.OnSetStateAsync(sessionState); |
| 0 | 219 | | await setStateTask.ConfigureAwait(false); |
| 0 | 220 | | } |
| 0 | 221 | | catch (Exception ex) |
| | 222 | | { |
| 0 | 223 | | this.diagnosticSource.ReportException(ex); |
| 0 | 224 | | throw; |
| | 225 | | } |
| | 226 | | finally |
| | 227 | | { |
| 0 | 228 | | this.diagnosticSource.SetSessionStateStop(activity, sessionState, this.SessionId, setStateTask?.Status); |
| | 229 | | } |
| 0 | 230 | | } |
| | 231 | |
|
| | 232 | | private async Task OnRenewSessionLockInstrumentedAsync() |
| | 233 | | { |
| 0 | 234 | | Activity activity = this.diagnosticSource.RenewSessionLockStart(this.SessionId); |
| 0 | 235 | | Task renewTask = null; |
| | 236 | |
|
| | 237 | | try |
| | 238 | | { |
| 0 | 239 | | renewTask = this.OnRenewSessionLockAsync(); |
| 0 | 240 | | await renewTask.ConfigureAwait(false); |
| 0 | 241 | | } |
| 0 | 242 | | catch (Exception ex) |
| | 243 | | { |
| 0 | 244 | | this.diagnosticSource.ReportException(ex); |
| 0 | 245 | | throw; |
| | 246 | | } |
| | 247 | | finally |
| | 248 | | { |
| 0 | 249 | | this.diagnosticSource.RenewSessionLockStop(activity, this.SessionId, renewTask?.Status); |
| | 250 | | } |
| 0 | 251 | | } |
| | 252 | |
|
| | 253 | | } |
| | 254 | | } |