// // private static void ReadCallback(AsyncProtocolRequest asyncRequest) { // Async ONLY completion try { NegotiateStream negoStream = (NegotiateStream)asyncRequest.AsyncObject; BufferAsyncResult bufferResult = (BufferAsyncResult)asyncRequest.UserAsyncResult; // This is not a hack, just optimization to avoid an additional callback. // if ((object)asyncRequest.Buffer == (object)negoStream._ReadHeader) { negoStream.StartFrameBody(asyncRequest.Result, bufferResult.Buffer, bufferResult.Offset, bufferResult.Count, asyncRequest); } else { if (-1 == negoStream.ProcessFrameBody(asyncRequest.Result, bufferResult.Buffer, bufferResult.Offset, bufferResult.Count, asyncRequest)) { // in case we decrypted 0 bytes start another reading. negoStream.StartReading(bufferResult.Buffer, bufferResult.Offset, bufferResult.Count, asyncRequest); } } } catch (Exception e) { if (asyncRequest.IsUserCompleted) { // This will throw on a worker thread. throw; } asyncRequest.CompleteWithError(e); } }
// // internal int EndRead(IAsyncResult asyncResult) { if (asyncResult == null) { throw new ArgumentNullException("asyncResult"); } BufferAsyncResult bufferResult = asyncResult as BufferAsyncResult; if (bufferResult == null) { throw new ArgumentException(SR.GetString(SR.net_io_async_result, asyncResult.GetType().FullName), "asyncResult"); } if (Interlocked.Exchange(ref _NestedRead, 0) == 0) { throw new InvalidOperationException(SR.GetString(SR.net_io_invalidendcall, "EndRead")); } // No "artificial" timeouts implemented so far, InnerStream controls timeout. bufferResult.InternalWaitForCompletion(); if (bufferResult.Result is Exception) { if (bufferResult.Result is IOException) { throw (Exception)bufferResult.Result; } throw new IOException(SR.GetString(SR.net_io_read), (Exception)bufferResult.Result); } return((int)bufferResult.Result); }
internal int EndRead(IAsyncResult asyncResult) { if (asyncResult == null) { throw new ArgumentNullException("asyncResult"); } BufferAsyncResult result = asyncResult as BufferAsyncResult; if (result == null) { throw new ArgumentException(SR.GetString("net_io_async_result", new object[] { asyncResult.GetType().FullName }), "asyncResult"); } if (Interlocked.Exchange(ref this._NestedRead, 0) == 0) { throw new InvalidOperationException(SR.GetString("net_io_invalidendcall", new object[] { "EndRead" })); } result.InternalWaitForCompletion(); if (!(result.Result is Exception)) { return((int)result.Result); } if (result.Result is IOException) { throw ((Exception)result.Result); } throw new IOException(SR.GetString("net_io_write"), (Exception)result.Result); }
public override void EndWrite(IAsyncResult asyncResult) { this._NegoState.CheckThrow(true); if (!this._NegoState.CanGetSecureStream) { base.InnerStream.EndWrite(asyncResult); } else { if (asyncResult == null) { throw new ArgumentNullException("asyncResult"); } BufferAsyncResult result = asyncResult as BufferAsyncResult; if (result == null) { throw new ArgumentException(SR.GetString("net_io_async_result", new object[] { asyncResult.GetType().FullName }), "asyncResult"); } if (Interlocked.Exchange(ref this._NestedWrite, 0) == 0) { throw new InvalidOperationException(SR.GetString("net_io_invalidendcall", new object[] { "EndWrite" })); } result.InternalWaitForCompletion(); if (result.Result is Exception) { if (result.Result is IOException) { throw ((Exception)result.Result); } throw new IOException(SR.GetString("net_io_write"), (Exception)result.Result); } } }
// // internal IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) { BufferAsyncResult bufferResult = new BufferAsyncResult(this, buffer, offset, count, asyncState, asyncCallback); AsyncProtocolRequest asyncRequest = new AsyncProtocolRequest(bufferResult); ProcessRead(buffer, offset, count, asyncRequest ); return bufferResult; }
internal IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) { var bufferResult = new BufferAsyncResult(this, buffer, offset, count, asyncState, asyncCallback); ProcessRead(buffer, offset, count, bufferResult); return(bufferResult); }
internal IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) { BufferAsyncResult userAsyncResult = new BufferAsyncResult(this, buffer, offset, count, asyncState, asyncCallback); AsyncProtocolRequest asyncRequest = new AsyncProtocolRequest(userAsyncResult); this.ProcessRead(buffer, offset, count, asyncRequest); return(userAsyncResult); }
// // Combined sync/async read method. For sync request asyncRequest==null. // private int ProcessRead(byte[] buffer, int offset, int count, BufferAsyncResult asyncResult) { ValidateParameters(buffer, offset, count); if (Interlocked.Exchange(ref _nestedRead, 1) == 1) { throw new NotSupportedException(SR.Format(SR.net_io_invalidnestedcall, (asyncResult != null? "BeginRead":"Read"), "read")); } // If this is an async operation, get the AsyncProtocolRequest to use. // We do this only after we verify we're the sole write operation in flight. AsyncProtocolRequest asyncRequest = GetOrCreateProtocolRequest(ref _readProtocolRequest, asyncResult); bool failed = false; try { int copyBytes; if (InternalBufferCount != 0) { copyBytes = InternalBufferCount > count ? count : InternalBufferCount; if (copyBytes != 0) { Buffer.BlockCopy(InternalBuffer, InternalOffset, buffer, offset, copyBytes); SkipBytes(copyBytes); } asyncRequest?.CompleteUser(copyBytes); return(copyBytes); } return(StartReading(buffer, offset, count, asyncRequest)); } catch (Exception e) { _sslState.FinishRead(null); failed = true; if (e is IOException) { throw; } throw new IOException(SR.net_io_read, e); } finally { if (asyncRequest == null || failed) { _nestedRead = 0; } } }
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) { this._NegoState.CheckThrow(true); if (!this._NegoState.CanGetSecureStream) { return(base.InnerStream.BeginWrite(buffer, offset, count, asyncCallback, asyncState)); } BufferAsyncResult userAsyncResult = new BufferAsyncResult(this, buffer, offset, count, true, asyncState, asyncCallback); AsyncProtocolRequest asyncRequest = new AsyncProtocolRequest(userAsyncResult); this.ProcessWrite(buffer, offset, count, asyncRequest); return(userAsyncResult); }
public override int EndRead(IAsyncResult asyncResult) { #if DEBUG using (DebugThreadTracking.SetThreadKind(ThreadKinds.User)) { #endif _negoState.CheckThrow(true); if (!_negoState.CanGetSecureStream) { return(TaskToApm.End <int>(asyncResult)); } if (asyncResult == null) { throw new ArgumentNullException(nameof(asyncResult)); } BufferAsyncResult bufferResult = asyncResult as BufferAsyncResult; if (bufferResult == null) { throw new ArgumentException(SR.Format(SR.net_io_async_result, asyncResult.GetType().FullName), nameof(asyncResult)); } if (Interlocked.Exchange(ref _NestedRead, 0) == 0) { throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, "EndRead")); } // No "artificial" timeouts implemented so far, InnerStream controls timeout. bufferResult.InternalWaitForCompletion(); if (bufferResult.Result is Exception e) { if (e is IOException) { ExceptionDispatchInfo.Throw(e); } throw new IOException(SR.net_io_read, e); } return(bufferResult.Int32Result); #if DEBUG } #endif }
private int EndRead(IAsyncResult asyncResult) { #if DEBUG using (GlobalLog.SetThreadKind(ThreadKinds.User)) { #endif _negoState.CheckThrow(true); if (!_negoState.CanGetSecureStream) { return(InnerStreamAPM.EndRead(asyncResult)); } if (asyncResult == null) { throw new ArgumentNullException("asyncResult"); } BufferAsyncResult bufferResult = asyncResult as BufferAsyncResult; if (bufferResult == null) { throw new ArgumentException(SR.Format(SR.net_io_async_result, asyncResult.GetType().FullName), "asyncResult"); } if (Interlocked.Exchange(ref _NestedRead, 0) == 0) { throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, "EndRead")); } // No "artificial" timeouts implemented so far, InnerStream controls timeout. bufferResult.InternalWaitForCompletion(); if (bufferResult.Result is Exception) { if (bufferResult.Result is IOException) { throw (Exception)bufferResult.Result; } throw new IOException(SR.net_io_read, (Exception)bufferResult.Result); } return((int)bufferResult.Result); #if DEBUG } #endif }
public override void EndWrite(IAsyncResult asyncResult) { #if DEBUG using (DebugThreadTracking.SetThreadKind(ThreadKinds.User)) { #endif _negoState.CheckThrow(true); if (!_negoState.CanGetSecureStream) { InnerStream.EndWrite(asyncResult); return; } if (asyncResult == null) { throw new ArgumentNullException(nameof(asyncResult)); } BufferAsyncResult bufferResult = asyncResult as BufferAsyncResult; if (bufferResult == null) { throw new ArgumentException(SR.Format(SR.net_io_async_result, asyncResult.GetType().FullName), nameof(asyncResult)); } if (Interlocked.Exchange(ref _NestedWrite, 0) == 0) { throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, "EndWrite")); } // No "artificial" timeouts implemented so far, InnerStream controls timeout. bufferResult.InternalWaitForCompletion(); if (bufferResult.Result is Exception) { if (bufferResult.Result is IOException) { throw (Exception)bufferResult.Result; } throw new IOException(SR.net_io_write, (Exception)bufferResult.Result); } #if DEBUG } #endif }
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); } }
public /*override*/ IAsyncResult BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state) { if (!this.BufferedAsync) { return(IntBeginRead(buffer, offset, count, callback, state)); } AsyncRequest lRequest = new AsyncRequest(); lRequest.AsyncBuffer = buffer; lRequest.AsyncOffset = offset; lRequest.AsyncCount = count; lRequest.AsyncRest = count; lRequest.AsyncCallback = callback; lRequest.AsyncState = state; try { if (fBuffer != null && fBufferEnd - fBufferStart > 0) { IAsyncResult lAr = new BufferAsyncResult(buffer, offset, count, lRequest); IntReadCallback(lAr); return(lAr); } IntBeginRead(buffer, offset, count, IntReadCallback, lRequest); } catch (ObjectDisposedException) // disconnect from this side { TriggerAsyncDisconnect(); throw; } catch (SocketException) // disconnect { TriggerAsyncDisconnect(); throw; } return(lRequest); }
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) { #if DEBUG using (GlobalLog.SetThreadKind(ThreadKinds.User | ThreadKinds.Async)) { #endif _NegoState.CheckThrow(true); if (!_NegoState.CanGetSecureStream) { return(InnerStream.BeginRead(buffer, offset, count, asyncCallback, asyncState)); } BufferAsyncResult bufferResult = new BufferAsyncResult(this, buffer, offset, count, asyncState, asyncCallback); AsyncProtocolRequest asyncRequest = new AsyncProtocolRequest(bufferResult); ProcessRead(buffer, offset, count, asyncRequest); return(bufferResult); #if DEBUG } #endif }
// // private static void ReadFrameCallback(AsyncProtocolRequest asyncRequest) { // Async ONLY completion try { _SslStream sslStream = (_SslStream)asyncRequest.AsyncObject; BufferAsyncResult bufferResult = (BufferAsyncResult)asyncRequest.UserAsyncResult; if (-1 == sslStream.ProcessFrameBody(asyncRequest.Result, bufferResult.Buffer, bufferResult.Offset, bufferResult.Count, asyncRequest)) { // in case we decrypted 0 bytes start another reading. sslStream.StartReading(bufferResult.Buffer, bufferResult.Offset, bufferResult.Count, asyncRequest); } } catch (Exception e) { if (asyncRequest.IsUserCompleted) { // This will throw on a worker thread. throw; } asyncRequest.CompleteWithError(e); } }
// // public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback?asyncCallback, object?asyncState) { #if DEBUG using (DebugThreadTracking.SetThreadKind(ThreadKinds.User | ThreadKinds.Async)) { #endif _negoState.CheckThrow(true); if (!_negoState.CanGetSecureStream) { return(TaskToApm.Begin(InnerStream.WriteAsync(buffer, offset, count), asyncCallback, asyncState)); } BufferAsyncResult bufferResult = new BufferAsyncResult(this, buffer, offset, count, asyncState, asyncCallback); AsyncProtocolRequest asyncRequest = new AsyncProtocolRequest(bufferResult); ProcessWrite(buffer, offset, count, asyncRequest); return(bufferResult); #if DEBUG } #endif }
// // Combined sync/async read method. For sync request asyncRequest==null. // private int ProcessRead(byte[] buffer, int offset, int count, BufferAsyncResult asyncResult) { ValidateParameters(buffer, offset, count); if (Interlocked.Exchange(ref _nestedRead, 1) == 1) { throw new NotSupportedException(SR.Format(SR.net_io_invalidnestedcall, (asyncResult!=null? "BeginRead":"Read"), "read")); } // If this is an async operation, get the AsyncProtocolRequest to use. // We do this only after we verify we're the sole write operation in flight. AsyncProtocolRequest asyncRequest = GetOrCreateProtocolRequest(ref _readProtocolRequest, asyncResult); bool failed = false; try { int copyBytes; if (InternalBufferCount != 0) { copyBytes = InternalBufferCount > count ? count : InternalBufferCount; if (copyBytes != 0) { Buffer.BlockCopy(InternalBuffer, InternalOffset, buffer, offset, copyBytes); SkipBytes(copyBytes); } asyncRequest?.CompleteUser(copyBytes); return copyBytes; } return StartReading(buffer, offset, count, asyncRequest); } catch (Exception e) { _sslState.FinishRead(null); failed = true; if (e is IOException) { throw; } throw new IOException(SR.net_io_read, e); } finally { if (asyncRequest == null || failed) { _nestedRead = 0; } } }
// // private IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) { #if DEBUG using (GlobalLog.SetThreadKind(ThreadKinds.User | ThreadKinds.Async)) { #endif _negoState.CheckThrow(true); if (!_negoState.CanGetSecureStream) { return InnerStream.BeginWrite(buffer, offset, count, asyncCallback, asyncState); } BufferAsyncResult bufferResult = new BufferAsyncResult(this, buffer, offset, count, true, asyncState, asyncCallback); AsyncProtocolRequest asyncRequest = new AsyncProtocolRequest(bufferResult); ProcessWrite(buffer, offset, count, asyncRequest); return bufferResult; #if DEBUG } #endif }
private void IntReadCallback(IAsyncResult ar) { AsyncRequest lRequest = (AsyncRequest)ar.AsyncState; Int32 lCount; try { BufferAsyncResult lBufferResult = ar as BufferAsyncResult; if (lBufferResult != null) { if (fBufferEnd - fBufferStart > lBufferResult.Count) { for (Int32 i = 0; i < lBufferResult.Count; i++) { lBufferResult.Buffer[lBufferResult.Offset + i] = fBuffer[i + fBufferStart]; } fBufferStart += lBufferResult.Count; lCount = lBufferResult.Count; } else { Int32 lSize = fBufferEnd - fBufferStart; for (Int32 i = 0; i < lSize; i++) { lBufferResult.Buffer[lBufferResult.Offset + i] = fBuffer[i + fBufferStart]; } fBufferStart = 0; fBufferEnd = 0; lCount = lSize; } } else { lCount = IntEndRead(ar); } } catch (ObjectDisposedException) // disconnect from this side { TriggerAsyncDisconnect(); return; } catch (SocketException) // disconnect { TriggerAsyncDisconnect(); return; } catch (ArgumentException) // disconnect { return; } if (lCount == 0) { TriggerAsyncDisconnect(); return; } lRequest.AsyncRest = lRequest.AsyncRest - lCount; if (lRequest.AsyncRest > 0) { lRequest.AsyncOffset = lRequest.AsyncOffset + lCount; try { TriggerAsyncHaveIncompleteData(); IntBeginRead(lRequest.AsyncBuffer, lRequest.AsyncOffset, lRequest.AsyncRest, IntReadCallback, lRequest); } catch (ObjectDisposedException) // disconnect from this side { TriggerAsyncDisconnect(); } catch (SocketException) // disconnect { TriggerAsyncDisconnect(); } } else { lRequest.AsyncCallback(lRequest); } }