private async Task <IConnection> SendPreambleAsync(IConnection connection, ArraySegment <byte> preamble, TimeSpan timeout) { var timeoutHelper = new TimeoutHelper(timeout); // initialize a new decoder _decoder = new ClientDuplexDecoder(0); byte[] ackBuffer = new byte[1]; if (!await SendLock.WaitAsync(TimeoutHelper.ToMilliseconds(timeout))) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException( SR.Format(SR.CloseTimedOut, timeout), TimeoutHelper.CreateEnterTimedOutException(timeout))); } try { await connection.WriteAsync(preamble.Array, preamble.Offset, preamble.Count, true, timeoutHelper.RemainingTime()); if (_upgrade != null) { StreamUpgradeInitiator upgradeInitiator = _upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via); await upgradeInitiator.OpenAsync(timeoutHelper.RemainingTime()); var connectionWrapper = new OutWrapper <IConnection>(); connectionWrapper.Value = connection; bool upgradeInitiated = await ConnectionUpgradeHelper.InitiateUpgradeAsync(upgradeInitiator, connectionWrapper, _decoder, this, timeoutHelper.RemainingTime()); connection = connectionWrapper.Value; if (!upgradeInitiated) { await ConnectionUpgradeHelper.DecodeFramingFaultAsync(_decoder, connection, this.Via, MessageEncoder.ContentType, timeoutHelper.RemainingTime()); } SetRemoteSecurity(upgradeInitiator); await upgradeInitiator.CloseAsync(timeoutHelper.RemainingTime()); await connection.WriteAsync(ClientDuplexEncoder.PreambleEndBytes, 0, ClientDuplexEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime()); } int ackBytesRead = await connection.ReadAsync(ackBuffer, 0, ackBuffer.Length, timeoutHelper.RemainingTime()); if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, _decoder, Via)) { await ConnectionUpgradeHelper.DecodeFramingFaultAsync(_decoder, connection, Via, MessageEncoder.ContentType, timeoutHelper.RemainingTime()); } return(connection); } finally { SendLock.Release(); } }
internal async Task <IConnection> SendPreambleAsync(IConnection connection, TimeoutHelper timeoutHelper, ClientFramingDecoder decoder) { await connection.WriteAsync(Preamble, 0, Preamble.Length, true, timeoutHelper.RemainingTime()); if (_upgrade != null) { StreamUpgradeInitiator upgradeInitiator = _upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via); await upgradeInitiator.OpenAsync(timeoutHelper.RemainingTime()); var connectionWrapper = new OutWrapper <IConnection>(); connectionWrapper.Value = connection; bool upgradeInitiated = await ConnectionUpgradeHelper.InitiateUpgradeAsync(upgradeInitiator, connectionWrapper, decoder, this, timeoutHelper.RemainingTime()); connection = connectionWrapper.Value; if (!upgradeInitiated) { await ConnectionUpgradeHelper.DecodeFramingFaultAsync(decoder, connection, this.Via, _messageEncoder.ContentType, timeoutHelper.RemainingTime()); } await upgradeInitiator.CloseAsync(timeoutHelper.RemainingTime()); await connection.WriteAsync(ClientSingletonEncoder.PreambleEndBytes, 0, ClientSingletonEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime()); } byte[] ackBuffer = new byte[1]; int ackBytesRead = await connection.ReadAsync(ackBuffer, 0, ackBuffer.Length, timeoutHelper.RemainingTime()); if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, decoder, Via)) { await ConnectionUpgradeHelper.DecodeFramingFaultAsync(decoder, connection, Via, _messageEncoder.ContentType, timeoutHelper.RemainingTime()); } return(connection); }