| | 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.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | |
|
| | 10 | | internal sealed class SessionPumpHost |
| | 11 | | { |
| | 12 | | readonly object syncLock; |
| | 13 | | SessionReceivePump sessionReceivePump; |
| | 14 | | CancellationTokenSource sessionPumpCancellationTokenSource; |
| | 15 | | readonly Uri endpoint; |
| | 16 | |
|
| 0 | 17 | | public SessionPumpHost(string clientId, ReceiveMode receiveMode, ISessionClient sessionClient, Uri endpoint) |
| | 18 | | { |
| 0 | 19 | | this.syncLock = new object(); |
| 0 | 20 | | this.ClientId = clientId; |
| 0 | 21 | | this.ReceiveMode = receiveMode; |
| 0 | 22 | | this.SessionClient = sessionClient; |
| 0 | 23 | | this.endpoint = endpoint; |
| 0 | 24 | | } |
| | 25 | |
|
| 0 | 26 | | ReceiveMode ReceiveMode { get; } |
| | 27 | |
|
| 0 | 28 | | ISessionClient SessionClient { get; } |
| | 29 | |
|
| 0 | 30 | | string ClientId { get; } |
| | 31 | |
|
| | 32 | | public void Close() |
| | 33 | | { |
| 0 | 34 | | if (this.sessionReceivePump != null) |
| | 35 | | { |
| 0 | 36 | | this.sessionPumpCancellationTokenSource?.Cancel(); |
| 0 | 37 | | this.sessionPumpCancellationTokenSource?.Dispose(); |
| 0 | 38 | | this.sessionReceivePump = null; |
| | 39 | | } |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public void OnSessionHandler( |
| | 43 | | Func<IMessageSession, Message, CancellationToken, Task> callback, |
| | 44 | | SessionHandlerOptions sessionHandlerOptions) |
| | 45 | | { |
| 0 | 46 | | MessagingEventSource.Log.RegisterOnSessionHandlerStart(this.ClientId, sessionHandlerOptions); |
| | 47 | |
|
| 0 | 48 | | lock (this.syncLock) |
| | 49 | | { |
| 0 | 50 | | if (this.sessionReceivePump != null) |
| | 51 | | { |
| 0 | 52 | | throw new InvalidOperationException(Resources.SessionHandlerAlreadyRegistered); |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | this.sessionPumpCancellationTokenSource = new CancellationTokenSource(); |
| 0 | 56 | | this.sessionReceivePump = new SessionReceivePump( |
| 0 | 57 | | this.ClientId, |
| 0 | 58 | | this.SessionClient, |
| 0 | 59 | | this.ReceiveMode, |
| 0 | 60 | | sessionHandlerOptions, |
| 0 | 61 | | callback, |
| 0 | 62 | | this.endpoint, |
| 0 | 63 | | this.sessionPumpCancellationTokenSource.Token); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | try |
| | 67 | | { |
| 0 | 68 | | this.sessionReceivePump.StartPump(); |
| 0 | 69 | | } |
| 0 | 70 | | catch (Exception exception) |
| | 71 | | { |
| 0 | 72 | | MessagingEventSource.Log.RegisterOnSessionHandlerException(this.ClientId, exception); |
| 0 | 73 | | if (this.sessionReceivePump != null) |
| | 74 | | { |
| 0 | 75 | | this.sessionPumpCancellationTokenSource.Cancel(); |
| 0 | 76 | | this.sessionPumpCancellationTokenSource.Dispose(); |
| 0 | 77 | | this.sessionReceivePump = null; |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | throw; |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | MessagingEventSource.Log.RegisterOnSessionHandlerStop(this.ClientId); |
| 0 | 84 | | } |
| | 85 | | } |
| | 86 | | } |