internal IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
 {
     LazyAsyncResult userAsyncResult = new LazyAsyncResult(this, asyncState, asyncCallback);
     AsyncProtocolRequest asyncRequest = new AsyncProtocolRequest(userAsyncResult);
     this.ProcessWrite(buffer, offset, count, asyncRequest);
     return userAsyncResult;
 }
        private static void ReadCallback(IAsyncResult transportResult)
        {
            if (!(transportResult.AsyncState is FixedSizeReader))
            {
                if (GlobalLog.IsEnabled)
                {
                    GlobalLog.Assert("ReadCallback|State type is wrong, expected FixedSizeReader.");
                }

                Debug.Fail("ReadCallback|State type is wrong, expected FixedSizeReader.");
            }

            if (transportResult.CompletedSynchronously)
            {
                return;
            }

            FixedSizeReader      reader  = (FixedSizeReader)transportResult.AsyncState;
            AsyncProtocolRequest request = reader._request;

            // Async completion.
            try
            {
                int bytes = reader._transportAPM.EndRead(transportResult);

                if (reader.CheckCompletionBeforeNextRead(bytes))
                {
                    return;
                }

                reader.StartReading();
            }
            catch (Exception e)
            {
                if (request.IsUserCompleted)
                {
                    throw;
                }

                request.CompleteWithError(e);
            }
        }
        private static void ReadCallback(IAsyncResult transportResult)
        {
            if (!(transportResult.AsyncState is FixedSizeReader))
            {
                NetEventSource.Fail(null, "State type is wrong, expected FixedSizeReader.");
            }

            if (transportResult.CompletedSynchronously)
            {
                return;
            }

            FixedSizeReader      reader  = (FixedSizeReader)transportResult.AsyncState;
            AsyncProtocolRequest request = reader._request;

            // Async completion.
            try
            {
                int bytes = reader._transport.EndRead(transportResult);

                if (reader.CheckCompletionBeforeNextRead(bytes))
                {
                    return;
                }

                reader.StartReading();
            }
            catch (Exception e)
            {
                if (request.IsUserCompleted)
                {
                    throw;
                }

                request.CompleteUserWithError(e);
            }
        }
Exemple #4
0
 private static void ReadCallback(IAsyncResult transportResult)
 {
     if (!transportResult.CompletedSynchronously)
     {
         FixedSizeReader      asyncState = (FixedSizeReader)transportResult.AsyncState;
         AsyncProtocolRequest request    = asyncState._Request;
         try
         {
             int bytes = asyncState._Transport.EndRead(transportResult);
             if (!asyncState.CheckCompletionBeforeNextRead(bytes))
             {
                 asyncState.StartReading();
             }
         }
         catch (Exception exception)
         {
             if (request.IsUserCompleted)
             {
                 throw;
             }
             request.CompleteWithError(exception);
         }
     }
 }
        private int StartReading(byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
        {
            int num = 0;
        Label_0002:
            if (asyncRequest != null)
            {
                asyncRequest.SetNextRequest(buffer, offset, count, _ResumeAsyncReadCallback);
            }
            int userResult = this._SslState.CheckEnqueueRead(buffer, offset, count, asyncRequest);
            switch (userResult)
            {
                case 0:
                    return 0;

                case -1:
                    num = this.StartFrameHeader(buffer, offset, count, asyncRequest);
                    if (num == -1)
                    {
                        goto Label_0002;
                    }
                    return num;
            }
            if (asyncRequest != null)
            {
                asyncRequest.CompleteUser(userResult);
            }
            return userResult;
        }
 private int StartFrameBody(int readBytes, byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
 {
     if (readBytes == 0)
     {
         this.DecrementInternalBufferCount(this.InternalBufferCount);
         if (asyncRequest != null)
         {
             asyncRequest.CompleteUser(0);
         }
         return 0;
     }
     readBytes = this._SslState.GetRemainingFrameSize(this.InternalBuffer, readBytes);
     if (readBytes < 0)
     {
         throw new IOException(SR.GetString("net_frame_read_size"));
     }
     this.EnsureInternalBufferSize(this._SslState.HeaderSize, readBytes);
     if (asyncRequest != null)
     {
         asyncRequest.SetNextRequest(this.InternalBuffer, this._SslState.HeaderSize, readBytes, _ReadFrameCallback);
         this._Reader.AsyncReadPacket(asyncRequest);
         if (!asyncRequest.MustCompleteSynchronously)
         {
             return 0;
         }
         readBytes = asyncRequest.Result;
     }
     else
     {
         readBytes = this._Reader.ReadPacket(this.InternalBuffer, this._SslState.HeaderSize, readBytes);
     }
     return this.ProcessFrameBody(readBytes, buffer, offset, count, asyncRequest);
 }
 private static void ReadHeaderCallback(AsyncProtocolRequest asyncRequest)
 {
     try
     {
         _SslStream asyncObject = (_SslStream) asyncRequest.AsyncObject;
         BufferAsyncResult userAsyncResult = (BufferAsyncResult) asyncRequest.UserAsyncResult;
         if (-1 == asyncObject.StartFrameBody(asyncRequest.Result, userAsyncResult.Buffer, userAsyncResult.Offset, userAsyncResult.Count, asyncRequest))
         {
             asyncObject.StartReading(userAsyncResult.Buffer, userAsyncResult.Offset, userAsyncResult.Count, asyncRequest);
         }
     }
     catch (Exception exception)
     {
         if (asyncRequest.IsUserCompleted)
         {
             throw;
         }
         asyncRequest.CompleteWithError(exception);
     }
 }
 private int ProcessReadErrorCode(SecurityStatus errorCode, byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest, byte[] extraBuffer)
 {
     ProtocolToken token = new ProtocolToken(null, errorCode);
     if (token.Renegotiate)
     {
         this._SslState.ReplyOnReAuthentication(extraBuffer);
         return -1;
     }
     if (!token.CloseConnection)
     {
         throw new IOException(SR.GetString("net_io_decrypt"), token.GetException());
     }
     this._SslState.FinishRead(null);
     if (asyncRequest != null)
     {
         asyncRequest.CompleteUser(0);
     }
     return 0;
 }
 private bool CheckEnqueueHandshakeRead(ref byte[] buffer, AsyncProtocolRequest request)
 {
     LazyAsyncResult result = null;
     lock (this)
     {
         if (this._LockReadState == 6)
         {
             return false;
         }
         if (Interlocked.Exchange(ref this._LockReadState, 2) != 4)
         {
             return false;
         }
         if (request != null)
         {
             this._QueuedReadStateRequest = request;
             return true;
         }
         result = new LazyAsyncResult(null, null, null);
         this._QueuedReadStateRequest = result;
     }
     result.InternalWaitForCompletion();
     buffer = (byte[]) result.Result;
     return false;
 }
 internal void ProcessAuthentication(LazyAsyncResult lazyResult)
 {
     if (Interlocked.Exchange(ref this._NestedAuth, 1) == 1)
     {
         throw new InvalidOperationException(SR.GetString("net_io_invalidnestedcall", new object[] { (lazyResult == null) ? "BeginAuthenticate" : "Authenticate", "authenticate" }));
     }
     try
     {
         this.CheckThrow(false);
         AsyncProtocolRequest asyncRequest = null;
         if (lazyResult != null)
         {
             asyncRequest = new AsyncProtocolRequest(lazyResult) {
                 Buffer = null
             };
         }
         this._CachedSession = CachedSessionStatus.Unknown;
         this.ForceAuthentication(this.Context.IsServer, null, asyncRequest);
         if ((lazyResult == null) && Logging.On)
         {
             Logging.PrintInfo(Logging.Web, SR.GetString("net_log_sspi_selected_cipher_suite", new object[] { "ProcessAuthentication", this.SslProtocol, this.CipherAlgorithm, this.CipherStrength, this.HashAlgorithm, this.HashStrength, this.KeyExchangeAlgorithm, this.KeyExchangeStrength }));
         }
     }
     finally
     {
         if ((lazyResult == null) || (this._Exception != null))
         {
             this._NestedAuth = 0;
         }
     }
 }
 private static void PartialFrameCallback(AsyncProtocolRequest asyncRequest)
 {
     SslState asyncObject = (SslState) asyncRequest.AsyncObject;
     try
     {
         asyncObject.StartReadFrame(asyncRequest.Buffer, asyncRequest.Result, asyncRequest);
     }
     catch (Exception exception)
     {
         if (asyncRequest.IsUserCompleted)
         {
             throw;
         }
         asyncObject.FinishHandshake(exception, asyncRequest);
     }
 }
 private void ForceAuthentication(bool receiveFirst, byte[] buffer, AsyncProtocolRequest asyncRequest)
 {
     if (!this.CheckEnqueueHandshake(buffer, asyncRequest))
     {
         this._Framing = Framing.None;
         try
         {
             if (receiveFirst)
             {
                 this.StartReceiveBlob(buffer, asyncRequest);
             }
             else
             {
                 this.StartSendBlob(buffer, (buffer == null) ? 0 : buffer.Length, asyncRequest);
             }
         }
         catch (Exception exception)
         {
             this._Framing = Framing.None;
             this._HandshakeCompleted = false;
             if (this.SetException(exception) == exception)
             {
                 throw;
             }
             throw this._Exception;
         }
         finally
         {
             if (this._Exception != null)
             {
                 this.FinishHandshake(null, null);
             }
         }
     }
 }
 private void FinishHandshake(Exception e, AsyncProtocolRequest asyncRequest)
 {
     try
     {
         lock (this)
         {
             if (e != null)
             {
                 this.SetException(e);
             }
             this.FinishHandshakeRead(0);
             if (Interlocked.CompareExchange(ref this._LockWriteState, 0, 2) == 3)
             {
                 this._LockWriteState = 1;
                 object state = this._QueuedWriteStateRequest;
                 if (state != null)
                 {
                     this._QueuedWriteStateRequest = null;
                     if (state is LazyAsyncResult)
                     {
                         ((LazyAsyncResult) state).InvokeCallback();
                     }
                     else
                     {
                         ThreadPool.QueueUserWorkItem(new WaitCallback(this.CompleteRequestWaitCallback), state);
                     }
                 }
             }
         }
     }
     finally
     {
         if (asyncRequest != null)
         {
             if (e != null)
             {
                 asyncRequest.CompleteWithError(e);
             }
             else
             {
                 asyncRequest.CompleteUser();
             }
         }
     }
 }
 internal bool CheckEnqueueWrite(AsyncProtocolRequest asyncRequest)
 {
     this._QueuedWriteStateRequest = null;
     if (Interlocked.CompareExchange(ref this._LockWriteState, 1, 0) == 2)
     {
         LazyAsyncResult result = null;
         lock (this)
         {
             if (this._LockWriteState == 1)
             {
                 this.CheckThrow(true);
                 return false;
             }
             this._LockWriteState = 3;
             if (asyncRequest != null)
             {
                 this._QueuedWriteStateRequest = asyncRequest;
                 return true;
             }
             result = new LazyAsyncResult(null, null, null);
             this._QueuedWriteStateRequest = result;
         }
         result.InternalWaitForCompletion();
         this.CheckThrow(true);
     }
     return false;
 }
 internal int CheckEnqueueRead(byte[] buffer, int offset, int count, AsyncProtocolRequest request)
 {
     if (Interlocked.CompareExchange(ref this._LockReadState, 4, 0) != 2)
     {
         return this.CheckOldKeyDecryptedData(buffer, offset, count);
     }
     LazyAsyncResult result = null;
     lock (this)
     {
         int num2 = this.CheckOldKeyDecryptedData(buffer, offset, count);
         if (num2 != -1)
         {
             return num2;
         }
         if (this._LockReadState != 2)
         {
             this._LockReadState = 4;
             return -1;
         }
         this._LockReadState = 6;
         if (request != null)
         {
             this._QueuedReadStateRequest = request;
             return 0;
         }
         result = new LazyAsyncResult(null, null, null);
         this._QueuedReadStateRequest = result;
     }
     result.InternalWaitForCompletion();
     lock (this)
     {
         return this.CheckOldKeyDecryptedData(buffer, offset, count);
     }
 }
Exemple #16
0
 //
 // Completes "_Request" with 0 if 0 bytes was requested or legitimate EOF received.
 // Otherwise, reads as directed or completes "_Request" with an Exception or throws.
 //
 public void AsyncReadPacket(AsyncProtocolRequest request)
 {
     _request = request;
     _totalRead = 0;
     StartReading();
 }
 private void ProcessReceivedBlob(byte[] buffer, int count, AsyncProtocolRequest asyncRequest)
 {
     if (count == 0)
     {
         throw new AuthenticationException(SR.GetString("net_auth_eof"), null);
     }
     if (this._PendingReHandshake)
     {
         int offset = 0;
         SecurityStatus errorCode = this.PrivateDecryptData(buffer, ref offset, ref count);
         if (errorCode == SecurityStatus.OK)
         {
             Exception exception = this.EnqueueOldKeyDecryptedData(buffer, offset, count);
             if (exception != null)
             {
                 this.StartSendAuthResetSignal(null, asyncRequest, exception);
                 return;
             }
             this._Framing = Framing.None;
             this.StartReceiveBlob(buffer, asyncRequest);
             return;
         }
         if (errorCode != SecurityStatus.Renegotiate)
         {
             ProtocolToken token = new ProtocolToken(null, errorCode);
             this.StartSendAuthResetSignal(null, asyncRequest, new AuthenticationException(SR.GetString("net_auth_SSPI"), token.GetException()));
             return;
         }
         this._PendingReHandshake = false;
         if (offset != 0)
         {
             Buffer.BlockCopy(buffer, offset, buffer, 0, count);
         }
     }
     this.StartSendBlob(buffer, count, asyncRequest);
 }
 private int ProcessRead(byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
 {
     int num2;
     this.ValidateParameters(buffer, offset, count);
     if (Interlocked.Exchange(ref this._NestedRead, 1) == 1)
     {
         throw new NotSupportedException(SR.GetString("net_io_invalidnestedcall", new object[] { (asyncRequest != null) ? "BeginRead" : "Read", "read" }));
     }
     bool flag = false;
     try
     {
         if (this.InternalBufferCount != 0)
         {
             int num = (this.InternalBufferCount > count) ? count : this.InternalBufferCount;
             if (num != 0)
             {
                 Buffer.BlockCopy(this.InternalBuffer, this.InternalOffset, buffer, offset, num);
                 this.DecrementInternalBufferCount(num);
             }
             if (asyncRequest != null)
             {
                 asyncRequest.CompleteUser(num);
             }
             return num;
         }
         num2 = this.StartReading(buffer, offset, count, asyncRequest);
     }
     catch (Exception exception)
     {
         this._SslState.FinishRead(null);
         flag = true;
         if (exception is IOException)
         {
             throw;
         }
         throw new IOException(SR.GetString("net_io_read"), exception);
     }
     finally
     {
         if ((asyncRequest == null) || flag)
         {
             this._NestedRead = 0;
         }
     }
     return num2;
 }
 private static void ReadFrameCallback(AsyncProtocolRequest asyncRequest)
 {
     SslState asyncObject = (SslState) asyncRequest.AsyncObject;
     try
     {
         if (asyncRequest.Result == 0)
         {
             asyncRequest.Offset = 0;
         }
         asyncObject.ProcessReceivedBlob(asyncRequest.Buffer, asyncRequest.Offset + asyncRequest.Result, asyncRequest);
     }
     catch (Exception exception)
     {
         if (asyncRequest.IsUserCompleted)
         {
             throw;
         }
         asyncObject.FinishHandshake(exception, asyncRequest);
     }
 }
 private void ProcessWrite(byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
 {
     if (this._SslState.LastPayload != null)
     {
         BufferOffsetSize[] buffers = new BufferOffsetSize[] { new BufferOffsetSize(buffer, offset, count, false) };
         if (asyncRequest != null)
         {
             this.ProcessWrite(buffers, new SplitWriteAsyncProtocolRequest(asyncRequest.UserAsyncResult));
         }
         else
         {
             this.ProcessWrite(buffers, null);
         }
     }
     else
     {
         this.ValidateParameters(buffer, offset, count);
         if (Interlocked.Exchange(ref this._NestedWrite, 1) == 1)
         {
             throw new NotSupportedException(SR.GetString("net_io_invalidnestedcall", new object[] { (asyncRequest != null) ? "BeginWrite" : "Write", "write" }));
         }
         bool flag = false;
         try
         {
             this.StartWriting(buffer, offset, count, asyncRequest);
         }
         catch (Exception exception)
         {
             this._SslState.FinishWrite();
             flag = true;
             if (exception is IOException)
             {
                 throw;
             }
             throw new IOException(SR.GetString("net_io_write"), exception);
         }
         finally
         {
             if ((asyncRequest == null) || flag)
             {
                 this._NestedWrite = 0;
             }
         }
     }
 }
 internal void ReplyOnReAuthentication(byte[] buffer)
 {
     lock (this)
     {
         this._LockReadState = 2;
         if (this._PendingReHandshake)
         {
             this.FinishRead(buffer);
             return;
         }
     }
     AsyncProtocolRequest asyncRequest = new AsyncProtocolRequest(new LazyAsyncResult(this, null, new AsyncCallback(this.RehandshakeCompleteCallback))) {
         Buffer = buffer
     };
     this.ForceAuthentication(false, buffer, asyncRequest);
 }
 private static void ResumeAsyncReadCallback(AsyncProtocolRequest request)
 {
     try
     {
         ((_SslStream) request.AsyncObject).StartReading(request.Buffer, request.Offset, request.Count, request);
     }
     catch (Exception exception)
     {
         if (request.IsUserCompleted)
         {
             throw;
         }
         ((_SslStream) request.AsyncObject)._SslState.FinishRead(null);
         request.CompleteWithError(exception);
     }
 }
 private void StartReadFrame(byte[] buffer, int readBytes, AsyncProtocolRequest asyncRequest)
 {
     if (readBytes == 0)
     {
         throw new IOException(SR.GetString("net_auth_eof"));
     }
     if (this._Framing == Framing.None)
     {
         this._Framing = this.DetectFraming(buffer, readBytes);
     }
     int remainingFrameSize = this.GetRemainingFrameSize(buffer, readBytes);
     if (remainingFrameSize < 0)
     {
         throw new IOException(SR.GetString("net_ssl_io_frame"));
     }
     if (remainingFrameSize == 0)
     {
         throw new AuthenticationException(SR.GetString("net_auth_eof"), null);
     }
     buffer = EnsureBufferSize(buffer, readBytes, readBytes + remainingFrameSize);
     if (asyncRequest == null)
     {
         remainingFrameSize = this._Reader.ReadPacket(buffer, readBytes, remainingFrameSize);
     }
     else
     {
         asyncRequest.SetNextRequest(buffer, readBytes, remainingFrameSize, _ReadFrameCallback);
         this._Reader.AsyncReadPacket(asyncRequest);
         if (!asyncRequest.MustCompleteSynchronously)
         {
             return;
         }
         remainingFrameSize = asyncRequest.Result;
         if (remainingFrameSize == 0)
         {
             readBytes = 0;
         }
     }
     this.ProcessReceivedBlob(buffer, readBytes + remainingFrameSize, asyncRequest);
 }
 private static void ResumeAsyncWriteCallback(AsyncProtocolRequest asyncRequest)
 {
     try
     {
         SplitWriteAsyncProtocolRequest request = asyncRequest as SplitWriteAsyncProtocolRequest;
         if (request != null)
         {
             ((_SslStream) asyncRequest.AsyncObject).StartWriting(request.SplitWritesState, request);
         }
         else
         {
             ((_SslStream) asyncRequest.AsyncObject).StartWriting(asyncRequest.Buffer, asyncRequest.Offset, asyncRequest.Count, asyncRequest);
         }
     }
     catch (Exception exception)
     {
         if (asyncRequest.IsUserCompleted)
         {
             throw;
         }
         ((_SslStream) asyncRequest.AsyncObject)._SslState.FinishWrite();
         asyncRequest.CompleteWithError(exception);
     }
 }
 private void StartReceiveBlob(byte[] buffer, AsyncProtocolRequest asyncRequest)
 {
     if (this._PendingReHandshake)
     {
         if (this.CheckEnqueueHandshakeRead(ref buffer, asyncRequest))
         {
             return;
         }
         if (!this._PendingReHandshake)
         {
             this.ProcessReceivedBlob(buffer, buffer.Length, asyncRequest);
             return;
         }
     }
     buffer = EnsureBufferSize(buffer, 0, this.Context.HeaderSize);
     int readBytes = 0;
     if (asyncRequest == null)
     {
         readBytes = this._Reader.ReadPacket(buffer, 0, this.Context.HeaderSize);
     }
     else
     {
         asyncRequest.SetNextRequest(buffer, 0, this.Context.HeaderSize, _PartialFrameCallback);
         this._Reader.AsyncReadPacket(asyncRequest);
         if (!asyncRequest.MustCompleteSynchronously)
         {
             return;
         }
         readBytes = asyncRequest.Result;
     }
     this.StartReadFrame(buffer, readBytes, asyncRequest);
 }
 private int StartFrameHeader(byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
 {
     int readBytes = 0;
     this.EnsureInternalBufferSize(0, this._SslState.HeaderSize);
     if (asyncRequest != null)
     {
         asyncRequest.SetNextRequest(this.InternalBuffer, 0, this._SslState.HeaderSize, _ReadHeaderCallback);
         this._Reader.AsyncReadPacket(asyncRequest);
         if (!asyncRequest.MustCompleteSynchronously)
         {
             return 0;
         }
         readBytes = asyncRequest.Result;
     }
     else
     {
         readBytes = this._Reader.ReadPacket(this.InternalBuffer, 0, this._SslState.HeaderSize);
     }
     return this.StartFrameBody(readBytes, buffer, offset, count, asyncRequest);
 }
 private void StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
 {
     if ((message == null) || (message.Size == 0))
     {
         throw exception;
     }
     if (asyncRequest == null)
     {
         this.InnerStream.Write(message.Payload, 0, message.Size);
     }
     else
     {
         asyncRequest.AsyncState = exception;
         IAsyncResult asyncResult = this.InnerStream.BeginWrite(message.Payload, 0, message.Size, _WriteCallback, asyncRequest);
         if (!asyncResult.CompletedSynchronously)
         {
             return;
         }
         this.InnerStream.EndWrite(asyncResult);
     }
     throw exception;
 }
 private void StartWriting(byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
 {
     if (asyncRequest != null)
     {
         asyncRequest.SetNextRequest(buffer, offset, count, _ResumeAsyncWriteCallback);
     }
     if (count >= 0)
     {
         byte[] outBuffer = null;
         do
         {
             int num2;
             if (this._SslState.CheckEnqueueWrite(asyncRequest))
             {
                 return;
             }
             int num = Math.Min(count, this._SslState.MaxDataSize);
             SecurityStatus errorCode = this._SslState.EncryptData(buffer, offset, num, ref outBuffer, out num2);
             if (errorCode != SecurityStatus.OK)
             {
                 ProtocolToken token = new ProtocolToken(null, errorCode);
                 throw new IOException(SR.GetString("net_io_encrypt"), token.GetException());
             }
             if (asyncRequest != null)
             {
                 asyncRequest.SetNextRequest(buffer, offset + num, count - num, _ResumeAsyncWriteCallback);
                 IAsyncResult asyncResult = this._SslState.InnerStream.BeginWrite(outBuffer, 0, num2, _WriteCallback, asyncRequest);
                 if (!asyncResult.CompletedSynchronously)
                 {
                     return;
                 }
                 this._SslState.InnerStream.EndWrite(asyncResult);
             }
             else
             {
                 this._SslState.InnerStream.Write(outBuffer, 0, num2);
             }
             offset += num;
             count -= num;
             this._SslState.FinishWrite();
         }
         while (count != 0);
     }
     if (asyncRequest != null)
     {
         asyncRequest.CompleteUser();
     }
 }
 private void StartSendBlob(byte[] incoming, int count, AsyncProtocolRequest asyncRequest)
 {
     ProtocolToken message = this.Context.NextMessage(incoming, 0, count);
     this._SecurityStatus = message.Status;
     if (message.Size != 0)
     {
         if (this.Context.IsServer && (this._CachedSession == CachedSessionStatus.Unknown))
         {
             this._CachedSession = (message.Size < 200) ? CachedSessionStatus.IsCached : CachedSessionStatus.IsNotCached;
         }
         if (this._Framing == Framing.Unified)
         {
             this._Framing = this.DetectFraming(message.Payload, message.Payload.Length);
         }
         if (((message.Done && this._ForceBufferingLastHandshakePayload) && ((this.InnerStream.GetType() == typeof(NetworkStream)) && !this._PendingReHandshake)) && !this.CheckWin9xCachedSession())
         {
             this._LastPayload = message.Payload;
         }
         else if (asyncRequest == null)
         {
             this.InnerStream.Write(message.Payload, 0, message.Size);
         }
         else
         {
             asyncRequest.AsyncState = message;
             IAsyncResult asyncResult = this.InnerStream.BeginWrite(message.Payload, 0, message.Size, _WriteCallback, asyncRequest);
             if (!asyncResult.CompletedSynchronously)
             {
                 return;
             }
             this.InnerStream.EndWrite(asyncResult);
         }
     }
     this.CheckCompletionBeforeNextReceive(message, asyncRequest);
 }
Exemple #30
0
 public void AsyncReadPacket(AsyncProtocolRequest request)
 {
     this._Request   = request;
     this._TotalRead = 0;
     this.StartReading();
 }
 private void CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
 {
     if (message.Failed)
     {
         this.StartSendAuthResetSignal(null, asyncRequest, new AuthenticationException(SR.GetString("net_auth_SSPI"), message.GetException()));
     }
     else if (message.Done && !this._PendingReHandshake)
     {
         if (this.CheckWin9xCachedSession())
         {
             this._PendingReHandshake = true;
             this.Win9xSessionRestarted();
             this.ForceAuthentication(false, null, asyncRequest);
         }
         else if (!this.CompleteHandshake())
         {
             this.StartSendAuthResetSignal(null, asyncRequest, new AuthenticationException(SR.GetString("net_ssl_io_cert_validation"), null));
         }
         else
         {
             this.FinishHandshake(null, asyncRequest);
         }
     }
     else
     {
         this.StartReceiveBlob(message.Payload, asyncRequest);
     }
 }
 public void AsyncReadPacket(AsyncProtocolRequest request)
 {
     this._Request = request;
     this._TotalRead = 0;
     this.StartReading();
 }
 private int ProcessFrameBody(int readBytes, byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
 {
     if (readBytes == 0)
     {
         throw new IOException(SR.GetString("net_io_eof"));
     }
     readBytes += this._SslState.HeaderSize;
     int num = 0;
     SecurityStatus errorCode = this._SslState.DecryptData(this.InternalBuffer, ref num, ref readBytes);
     if (errorCode != SecurityStatus.OK)
     {
         byte[] dst = null;
         if (readBytes != 0)
         {
             dst = new byte[readBytes];
             Buffer.BlockCopy(this.InternalBuffer, num, dst, 0, readBytes);
         }
         this.DecrementInternalBufferCount(this.InternalBufferCount);
         return this.ProcessReadErrorCode(errorCode, buffer, offset, count, asyncRequest, dst);
     }
     if ((readBytes == 0) && (count != 0))
     {
         this.DecrementInternalBufferCount(this.InternalBufferCount);
         return -1;
     }
     this.EnsureInternalBufferSize(0, num + readBytes);
     this.DecrementInternalBufferCount(num);
     if (readBytes > count)
     {
         readBytes = count;
     }
     Buffer.BlockCopy(this.InternalBuffer, this.InternalOffset, buffer, offset, readBytes);
     this.DecrementInternalBufferCount(readBytes);
     this._SslState.FinishRead(null);
     if (asyncRequest != null)
     {
         asyncRequest.CompleteUser(readBytes);
     }
     return readBytes;
 }
Exemple #34
0
 //
 // Completes "_Request" with 0 if 0 bytes was requested or legitimate EOF received
 // Otheriwse, reads as directed or completes "_Request" with an Exception or throws right here
 //
 public void AsyncReadPacket(AsyncProtocolRequest request)
 {
     _Request   = request;
     _TotalRead = 0;
     StartReading();
 }
 private bool CheckEnqueueHandshake(byte[] buffer, AsyncProtocolRequest asyncRequest)
 {
     LazyAsyncResult result = null;
     lock (this)
     {
         if (this._LockWriteState == 3)
         {
             return false;
         }
         if (Interlocked.Exchange(ref this._LockWriteState, 2) != 1)
         {
             return false;
         }
         if (asyncRequest != null)
         {
             asyncRequest.Buffer = buffer;
             this._QueuedWriteStateRequest = asyncRequest;
             return true;
         }
         result = new LazyAsyncResult(null, null, null);
         this._QueuedWriteStateRequest = result;
     }
     result.InternalWaitForCompletion();
     return false;
 }