private void WriteReplyHeader(ProtocolHeader header, bool fail) { Action <TransportAsyncCallbackArgs> action; MessagingClientEtwProvider.TraceClient <AmqpTransportListener.TransportHandler, ProtocolHeader>((AmqpTransportListener.TransportHandler source, ProtocolHeader detail) => MessagingClientEtwProvider.Provider.EventWriteAmqpLogOperation(source, TraceOperation.Send, detail), this, header); header.Encode(new ByteBuffer(this.buffer)); this.args.SetBuffer(this.buffer, 0, (int)this.buffer.Length); TransportAsyncCallbackArgs transportAsyncCallbackArg = this.args; if (fail) { action = null; } else { action = AmqpTransportListener.TransportHandler.writeCompleteCallback; } transportAsyncCallbackArg.CompletedCallback = action; this.bufferWriter.WriteBuffer(this.args); if (fail) { this.args.Exception = new NotSupportedException(header.ToString()); this.parent.OnHandleTransportComplete(this.args); } }
public async Task OpenAsync(TimeSpan timeout, bool useWebSocket, X509Certificate2 clientCert) { var hostName = _uri.Host; var tcpSettings = new TcpTransportSettings { Host = hostName, Port = _uri.Port != -1 ? _uri.Port : AmqpConstants.DefaultSecurePort }; TransportSettings = new TlsTransportSettings(tcpSettings) { TargetHost = hostName, Certificate = clientCert }; TransportBase transport; if (useWebSocket) { transport = await CreateClientWebSocketTransportAsync(timeout).ConfigureAwait(false); SaslTransportProvider provider = _amqpSettings.GetTransportProvider <SaslTransportProvider>(); if (provider != null) { _sentHeader = new ProtocolHeader(provider.ProtocolId, provider.DefaultVersion); ByteBuffer buffer = new ByteBuffer(new byte[AmqpConstants.ProtocolHeaderSize]); _sentHeader.Encode(buffer); var args = new TransportAsyncCallbackArgs(); args.SetBuffer(buffer.Buffer, buffer.Offset, buffer.Length); args.CompletedCallback = OnWriteHeaderComplete; args.Transport = transport; transport.WriteAsync(args); _tcs = new TaskCompletionSource <TransportBase>(); transport = await _tcs.Task.ConfigureAwait(false); await transport.OpenAsync(timeout).ConfigureAwait(false); } } else { var tcpInitiator = new AmqpTransportInitiator(_amqpSettings, TransportSettings); transport = await tcpInitiator.ConnectTaskAsync(timeout).ConfigureAwait(false); } AmqpConnection = new AmqpConnection(transport, _amqpSettings, AmqpConnectionSettings); AmqpConnection.Closed += OnConnectionClosed; await AmqpConnection.OpenAsync(timeout).ConfigureAwait(false); _isConnectionClosed = false; }
void WriteReplyHeader(ProtocolHeader header, bool fail) { AmqpTrace.Provider.AmqpLogOperationInformational(this, TraceOperation.Send, header); #if DEBUG header.Trace(true); #endif ByteBuffer byteBuffer = new ByteBuffer(this.buffer); header.Encode(byteBuffer); this.args.SetBuffer(this.buffer, 0, this.buffer.Length); this.args.CompletedCallback = fail ? null : TransportHandler.writeCompleteCallback; this.bufferWriter.WriteBuffer(this.args); if (fail) { this.args.Exception = new NotSupportedException(header.ToString()); this.parent.OnHandleTransportComplete(this.args); } }
public async Task OpenAsync(TimeSpan timeout, bool useWebSocket, X509Certificate2 clientCert, IWebProxy proxy, RemoteCertificateValidationCallback remoteCerificateValidationCallback) { if (Logging.IsEnabled) { Logging.Enter(this, $"{nameof(AmqpClientConnection)}.{nameof(OpenAsync)}"); } var hostName = _uri.Host; var tcpSettings = new TcpTransportSettings { Host = hostName, Port = _uri.Port != -1 ? _uri.Port : AmqpConstants.DefaultSecurePort }; TransportSettings = new TlsTransportSettings(tcpSettings) { TargetHost = hostName, Certificate = clientCert, CertificateValidationCallback = remoteCerificateValidationCallback, }; TransportBase transport; if (useWebSocket) { transport = await CreateClientWebSocketTransportAsync(timeout, proxy).ConfigureAwait(false); SaslTransportProvider provider = _amqpSettings.GetTransportProvider <SaslTransportProvider>(); if (provider != null) { if (Logging.IsEnabled) { Logging.Info(this, $"{nameof(AmqpClientConnection)}.{nameof(OpenAsync)}: Using SaslTransport"); } _sentHeader = new ProtocolHeader(provider.ProtocolId, provider.DefaultVersion); ByteBuffer buffer = new ByteBuffer(new byte[AmqpConstants.ProtocolHeaderSize]); _sentHeader.Encode(buffer); _tcs = new TaskCompletionSource <TransportBase>(); var args = new TransportAsyncCallbackArgs(); args.SetBuffer(buffer.Buffer, buffer.Offset, buffer.Length); args.CompletedCallback = OnWriteHeaderComplete; args.Transport = transport; bool operationPending = transport.WriteAsync(args); if (Logging.IsEnabled) { Logging.Info(this, $"{nameof(AmqpClientConnection)}.{nameof(OpenAsync)}: Sent Protocol Header: {_sentHeader.ToString()} operationPending: {operationPending} completedSynchronously: {args.CompletedSynchronously}"); } if (!operationPending) { args.CompletedCallback(args); } transport = await _tcs.Task.ConfigureAwait(false); await transport.OpenAsync(timeout).ConfigureAwait(false); } } else { var tcpInitiator = new AmqpTransportInitiator(_amqpSettings, TransportSettings); transport = await tcpInitiator.ConnectTaskAsync(timeout).ConfigureAwait(false); } AmqpConnection = new AmqpConnection(transport, _amqpSettings, AmqpConnectionSettings); AmqpConnection.Closed += OnConnectionClosed; await AmqpConnection.OpenAsync(timeout).ConfigureAwait(false); _isConnectionClosed = false; }
private async Task <TransportBase> InitializeTransport(TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, timeout, $"{nameof(InitializeTransport)}"); } TransportBase transport; switch (AmqpTransportSettings.GetTransportType()) { case TransportType.Amqp_WebSocket_Only: transport = await CreateClientWebSocketTransportAsync(timeout).ConfigureAwait(false); SaslTransportProvider provider = AmqpSettings.GetTransportProvider <SaslTransportProvider>(); if (provider != null) { if (Logging.IsEnabled) { Logging.Info(this, $"{nameof(InitializeTransport)}: Using SaslTransport"); } SentProtocolHeader = new ProtocolHeader(provider.ProtocolId, provider.DefaultVersion); ByteBuffer buffer = new ByteBuffer(new byte[AmqpConstants.ProtocolHeaderSize]); SentProtocolHeader.Encode(buffer); TaskCompletionSource = new TaskCompletionSource <TransportBase>(); var args = new TransportAsyncCallbackArgs(); args.SetBuffer(buffer.Buffer, buffer.Offset, buffer.Length); args.CompletedCallback = OnWriteHeaderComplete; args.Transport = transport; bool operationPending = transport.WriteAsync(args); if (Logging.IsEnabled) { Logging.Info(this, $"{nameof(InitializeTransport)}: Sent Protocol Header: {SentProtocolHeader.ToString()} operationPending: {operationPending} completedSynchronously: {args.CompletedSynchronously}"); } if (!operationPending) { args.CompletedCallback(args); } transport = await TaskCompletionSource.Task.ConfigureAwait(false); await transport.OpenAsync(timeout).ConfigureAwait(false); } break; case TransportType.Amqp_Tcp_Only: var amqpTransportInitiator = new AmqpTransportInitiator(AmqpSettings, TlsTransportSettings); transport = await amqpTransportInitiator.ConnectTaskAsync(timeout).ConfigureAwait(false); break; default: throw new InvalidOperationException("AmqpTransportSettings must specify WebSocketOnly or TcpOnly"); } if (Logging.IsEnabled) { Logging.Exit(this, timeout, $"{nameof(InitializeTransport)}"); } return(transport); }