1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.Runtime;
using System.ServiceModel;
using System.ServiceModel.Security.Tokens;
sealed class MsmqOutputChannel : TransportOutputChannel
{
MsmqQueue msmqQueue;
MsmqTransactionMode transactionMode;
readonly byte[] preamble; // cached .NET framing singleton preamble
SynchronizedDisposablePool<MsmqOutputMessage<IOutputChannel>> outputMessages;
MsmqChannelFactory<IOutputChannel> factory;
SecurityTokenProviderContainer certificateTokenProvider;
public MsmqOutputChannel(MsmqChannelFactory<IOutputChannel> factory, EndpointAddress to, Uri via, bool manualAddressing)
: base(factory, to, via, manualAddressing, factory.MessageVersion)
{
// construct the .NET framing preamble used for every message
byte[] modeBytes = ClientSingletonSizedEncoder.ModeBytes;
EncodedVia encodedVia = new EncodedVia(this.Via.AbsoluteUri);
EncodedContentType encodedContentType = EncodedContentType.Create(factory.MessageEncoderFactory.Encoder.ContentType);
this.preamble = DiagnosticUtility.Utility.AllocateByteArray(modeBytes.Length + ClientSingletonSizedEncoder.CalcStartSize(encodedVia, encodedContentType));
Buffer.BlockCopy(modeBytes, 0, this.preamble, 0, modeBytes.Length);
ClientSingletonSizedEncoder.EncodeStart(this.preamble, modeBytes.Length, encodedVia, encodedContentType);
this.outputMessages = new SynchronizedDisposablePool<MsmqOutputMessage<IOutputChannel>>(factory.MaxPoolSize);
if (factory.IsMsmqX509SecurityConfigured)
{
this.certificateTokenProvider = factory.CreateX509TokenProvider(to, via);
}
this.factory = factory;
}
void CloseQueue()
{
this.outputMessages.Dispose();
if (null != this.msmqQueue)
this.msmqQueue.Dispose();
this.msmqQueue = null;
}
void OnCloseCore(bool isAborting, TimeSpan timeout)
{
this.CloseQueue();
this.outputMessages.Dispose();
if (factory.IsMsmqX509SecurityConfigured)
{
if (isAborting)
this.certificateTokenProvider.Abort();
else
this.certificateTokenProvider.Close(timeout);
}
}
protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
{
OnCloseCore(false, timeout);
return new CompletedAsyncResult(callback, state);
}
protected override void OnEndClose(IAsyncResult result)
{
CompletedAsyncResult.End(result);
}
protected override void OnClose(TimeSpan timeout)
{
OnCloseCore(false, timeout);
}
protected override void OnAbort()
{
OnCloseCore(true, TimeSpan.Zero);
}
void OnOpenCore(TimeSpan timeout)
{
OpenQueue();
if (factory.IsMsmqX509SecurityConfigured)
{
this.certificateTokenProvider.Open(timeout);
}
}
void OpenQueue()
{
try
{
this.msmqQueue = new MsmqQueue(this.factory.AddressTranslator.UriToFormatName(this.RemoteAddress.Uri),
UnsafeNativeMethods.MQ_SEND_ACCESS);
}
catch (MsmqException ex)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
}
if (this.factory.ExactlyOnce)
{
this.transactionMode = MsmqTransactionMode.CurrentOrSingle;
}
else
{
this.transactionMode = MsmqTransactionMode.None;
}
}
protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
{
OnOpenCore(timeout);
return new CompletedAsyncResult(callback, state);
}
protected override void OnEndOpen(IAsyncResult result)
{
CompletedAsyncResult.End(result);
}
protected override void OnOpen(TimeSpan timeout)
{
OnOpenCore(timeout);
}
protected override IAsyncResult OnBeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state)
{
OnSend(message, timeout);
return new CompletedAsyncResult(callback, state);
}
protected override void OnEndSend(IAsyncResult result)
{
CompletedAsyncResult.End(result);
}
protected override void OnSend(Message message, TimeSpan timeout)
{
// serialize the indigo message to byte array and copy the .NET framing preamble
ArraySegment<byte> messageData = this.factory.MessageEncoderFactory.Encoder.WriteMessage(
message, int.MaxValue, this.factory.BufferManager, preamble.Length);
Buffer.BlockCopy(preamble, 0, messageData.Array, messageData.Offset - preamble.Length, preamble.Length);
byte[] buffer = messageData.Array;
int offset = messageData.Offset - preamble.Length;
int size = messageData.Count + preamble.Length;
MsmqOutputMessage<IOutputChannel> msmqMessage = this.outputMessages.Take();
if (msmqMessage == null)
{
msmqMessage = new MsmqOutputMessage<IOutputChannel>(this.factory, size, this.RemoteAddress);
MsmqDiagnostics.PoolFull(this.factory.MaxPoolSize);
}
try
{
msmqMessage.ApplyCertificateIfNeeded(this.certificateTokenProvider, this.factory.MsmqTransportSecurity.MsmqAuthenticationMode, timeout);
msmqMessage.Body.EnsureBufferLength(size);
msmqMessage.Body.BufferLength = size;
Buffer.BlockCopy(buffer, offset, msmqMessage.Body.Buffer, 0, size);
this.factory.BufferManager.ReturnBuffer(buffer);
bool lockHeld = false;
try
{
Msmq.EnterXPSendLock(out lockHeld, this.factory.MsmqTransportSecurity.MsmqProtectionLevel);
this.msmqQueue.Send(msmqMessage, this.transactionMode);
MsmqDiagnostics.DatagramSent(msmqMessage.MessageId, message);
}
catch (MsmqException ex)
{
if (ex.FaultSender)
this.Fault();
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
}
finally
{
if (lockHeld)
{
Msmq.LeaveXPSendLock();
}
}
}
finally
{
if (!this.outputMessages.Return(msmqMessage))
{
msmqMessage.Dispose();
}
}
}
}
}
|