private static HandshakeDelegate BuildHandshakeDelegateForDispatcher(IServiceDispatcher dispatcher) { var be = dispatcher.Binding.CreateBindingElements(); var mebe = be.Find <MessageEncodingBindingElement>(); MessageEncoderFactory mefact = mebe.CreateMessageEncoderFactory(); var tbe = be.Find <TransportBindingElement>(); int maxReceivedMessageSize = (int)Math.Min(tbe.MaxReceivedMessageSize, int.MaxValue); var bufferManager = BufferManager.CreateBufferManager(tbe.MaxBufferPoolSize, maxReceivedMessageSize); var upgradeBindingElements = (from element in be where element is StreamUpgradeBindingElement select element).Cast <StreamUpgradeBindingElement>().ToList(); StreamUpgradeProvider streamUpgradeProvider = null; ISecurityCapabilities securityCapabilities = null; if (upgradeBindingElements.Count > 1) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.MultipleStreamUpgradeProvidersInParameters)); } // TODO: Limit NamedPipes to prevent it using SslStreamSecurityUpgradeProvider else if ((upgradeBindingElements.Count == 1) /*&& this.SupportsUpgrade(upgradeBindingElements[0])*/) { var bindingContext = new BindingContext(new CustomBinding(dispatcher.Binding), new BindingParameterCollection()); streamUpgradeProvider = upgradeBindingElements[0].BuildServerStreamUpgradeProvider(bindingContext); streamUpgradeProvider.OpenAsync().GetAwaiter().GetResult(); securityCapabilities = upgradeBindingElements[0].GetProperty <ISecurityCapabilities>(bindingContext); } return((connection) => { connection.MessageEncoderFactory = mefact; connection.StreamUpgradeAcceptor = streamUpgradeProvider?.CreateUpgradeAcceptor(); connection.SecurityCapabilities = securityCapabilities; connection.ServiceDispatcher = dispatcher; connection.BufferManager = bufferManager; return Task.CompletedTask; }); }
public ServerFramingDuplexSessionChannel(Microsoft.ServiceBus.Channels.ConnectionOrientedTransportChannelListener channelListener, Microsoft.ServiceBus.Channels.ServerSessionPreambleConnectionReader preambleReader, EndpointAddress localAddress, bool exposeConnectionProperty, Microsoft.ServiceBus.Channels.ConnectionDemuxer connectionDemuxer) : base(channelListener, localAddress, preambleReader.Via, exposeConnectionProperty) { this.channelListener = channelListener; this.connectionDemuxer = connectionDemuxer; base.Connection = preambleReader.Connection; this.decoder = preambleReader.Decoder; this.connectionBuffer = preambleReader.connectionBuffer; this.offset = preambleReader.BufferOffset; this.size = preambleReader.BufferSize; this.rawConnection = preambleReader.RawConnection; StreamUpgradeProvider upgrade = channelListener.Upgrade; if (upgrade != null) { this.upgradeAcceptor = upgrade.CreateUpgradeAcceptor(); } MessagingClientEtwProvider.Provider.RelayChannelConnectionTransfer(base.Activity, base.Connection.Activity); }
public Microsoft.ServiceBus.Channels.IConnection CompletePreamble(TimeSpan timeout) { Microsoft.ServiceBus.Common.TimeoutHelper timeoutHelper = new Microsoft.ServiceBus.Common.TimeoutHelper(timeout); if (!this.transportSettings.MessageEncoderFactory.Encoder.IsContentTypeSupported(this.decoder.ContentType)) { this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/ContentTypeInvalid", ref timeoutHelper); ExceptionUtility exceptionUtility = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility; string contentTypeMismatch = Resources.ContentTypeMismatch; object[] contentType = new object[] { this.decoder.ContentType, this.transportSettings.MessageEncoderFactory.Encoder.ContentType }; throw exceptionUtility.ThrowHelperError(new ProtocolException(Microsoft.ServiceBus.SR.GetString(contentTypeMismatch, contentType))); } StreamUpgradeAcceptor streamUpgradeAcceptor = null; StreamUpgradeProvider upgrade = this.transportSettings.Upgrade; if (upgrade != null) { streamUpgradeAcceptor = upgrade.CreateUpgradeAcceptor(); } Microsoft.ServiceBus.Channels.IConnection connection = base.Connection; while (true) { if (this.size == 0) { this.offset = 0; this.size = connection.Read(this.connectionBuffer, 0, (int)this.connectionBuffer.Length, timeoutHelper.RemainingTime()); if (this.size == 0) { break; } } do { int num = this.decoder.Decode(this.connectionBuffer, this.offset, this.size); if (num > 0) { Microsoft.ServiceBus.Channels.ServerSingletonPreambleConnectionReader serverSingletonPreambleConnectionReader = this; serverSingletonPreambleConnectionReader.offset = serverSingletonPreambleConnectionReader.offset + num; Microsoft.ServiceBus.Channels.ServerSingletonPreambleConnectionReader serverSingletonPreambleConnectionReader1 = this; serverSingletonPreambleConnectionReader1.size = serverSingletonPreambleConnectionReader1.size - num; } switch (this.decoder.CurrentState) { case Microsoft.ServiceBus.Channels.ServerSingletonDecoder.State.UpgradeRequest: { if (streamUpgradeAcceptor == null) { this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/UpgradeInvalid", ref timeoutHelper); ExceptionUtility exceptionUtility1 = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility; string upgradeRequestToNonupgradableService = Resources.UpgradeRequestToNonupgradableService; object[] objArray = new object[] { this.decoder.Upgrade }; throw exceptionUtility1.ThrowHelperError(new ProtocolException(Microsoft.ServiceBus.SR.GetString(upgradeRequestToNonupgradableService, objArray))); } if (!streamUpgradeAcceptor.CanUpgrade(this.decoder.Upgrade)) { this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/UpgradeInvalid", ref timeoutHelper); ExceptionUtility exceptionUtility2 = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility; string upgradeProtocolNotSupported = Resources.UpgradeProtocolNotSupported; object[] upgrade1 = new object[] { this.decoder.Upgrade }; throw exceptionUtility2.ThrowHelperError(new ProtocolException(Microsoft.ServiceBus.SR.GetString(upgradeProtocolNotSupported, upgrade1))); } connection.Write(Microsoft.ServiceBus.Channels.ServerSingletonEncoder.UpgradeResponseBytes, 0, (int)Microsoft.ServiceBus.Channels.ServerSingletonEncoder.UpgradeResponseBytes.Length, true, timeoutHelper.RemainingTime()); Microsoft.ServiceBus.Channels.IConnection preReadConnection = connection; if (this.size > 0) { preReadConnection = new Microsoft.ServiceBus.Channels.PreReadConnection(preReadConnection, this.connectionBuffer, this.offset, this.size); } try { connection = Microsoft.ServiceBus.Channels.InitialServerConnectionReader.UpgradeConnection(preReadConnection, streamUpgradeAcceptor, this.transportSettings); this.connectionBuffer = connection.AsyncReadBuffer; continue; } catch (Exception exception1) { Exception exception = exception1; if (Fx.IsFatal(exception)) { throw; } this.WriteAuditFailure(streamUpgradeAcceptor as StreamSecurityUpgradeAcceptor, exception); throw; } break; } case Microsoft.ServiceBus.Channels.ServerSingletonDecoder.State.Start: { this.SetupSecurityIfNecessary(streamUpgradeAcceptor); connection.Write(Microsoft.ServiceBus.Channels.ServerSessionEncoder.AckResponseBytes, 0, (int)Microsoft.ServiceBus.Channels.ServerSessionEncoder.AckResponseBytes.Length, true, timeoutHelper.RemainingTime()); return(connection); } default: { continue; } } }while (this.size != 0); } throw Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.decoder.CreatePrematureEOFException()); }