public static void DecodeFramingFault(ClientFramingDecoder decoder, IConnection connection, Uri via, string contentType, ref TimeoutHelper timeoutHelper) { ValidateReadingFaultString(decoder); int offset = 0; byte[] buffer = DiagnosticUtility.Utility.AllocateByteArray(0x100); int size = connection.Read(buffer, offset, buffer.Length, timeoutHelper.RemainingTime()); while (size > 0) { int num3 = decoder.Decode(buffer, offset, size); offset += num3; size -= num3; if (decoder.CurrentState == ClientFramingDecoderState.Fault) { ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime()); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(FaultStringDecoder.GetFaultException(decoder.Fault, via.ToString(), contentType)); } if (decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString) { throw Fx.AssertAndThrow("invalid framing client state machine"); } if (size == 0) { offset = 0; size = connection.Read(buffer, offset, buffer.Length, timeoutHelper.RemainingTime()); } } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException()); }
private void CompleteReadFaultData() { int offset = 0; int size = this.connection.EndRead(); while (size > 0) { int num3 = this.decoder.Decode(this.connection.AsyncReadBuffer, offset, size); offset += num3; size -= num3; if (this.decoder.CurrentState == ClientFramingDecoderState.Fault) { ConnectionUtilities.CloseNoThrow(this.connection, this.timeoutHelper.RemainingTime()); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(FaultStringDecoder.GetFaultException(this.decoder.Fault, this.via.ToString(), this.contentType)); } if (this.decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString) { throw Fx.AssertAndThrow("invalid framing client state machine"); } if (size == 0) { offset = 0; if (this.connection.BeginRead(0, Math.Min(0x100, this.connection.AsyncReadBufferSize), this.timeoutHelper.RemainingTime(), onReadFaultData, this) == AsyncReadResult.Queued) { return; } size = this.connection.EndRead(); } } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.decoder.CreatePrematureEOFException()); }
public static async Task DecodeFramingFaultAsync(ClientFramingDecoder decoder, IConnection connection, Uri via, string contentType, TimeSpan timeout) { var timeoutHelper = new TimeoutHelper(timeout); ValidateReadingFaultString(decoder); var tcs = new TaskCompletionSource <bool>(); var result = connection.BeginRead(0, Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize), timeoutHelper.RemainingTime(), FramingDuplexSessionChannel.OnIoComplete, tcs); if (result == AsyncCompletionResult.Completed) { tcs.TrySetResult(true); } await tcs.Task; int offset = 0; int size = connection.EndRead(); while (size > 0) { int bytesDecoded = decoder.Decode(connection.AsyncReadBuffer, offset, size); offset += bytesDecoded; size -= bytesDecoded; if (decoder.CurrentState == ClientFramingDecoderState.Fault) { ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime()); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( FaultStringDecoder.GetFaultException(decoder.Fault, via.ToString(), contentType)); } else { if (decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString) { throw new Exception("invalid framing client state machine"); } if (size == 0) { offset = 0; tcs = new TaskCompletionSource <bool>(); result = connection.BeginRead(0, Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize), timeoutHelper.RemainingTime(), FramingDuplexSessionChannel.OnIoComplete, tcs); if (result == AsyncCompletionResult.Completed) { tcs.TrySetResult(true); } await tcs.Task; size = connection.EndRead(); } } } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException()); }
public static async Task DecodeFramingFaultAsync(ClientFramingDecoder decoder, IConnection connection, Uri via, string contentType, TimeSpan timeout) { var timeoutHelper = new TimeoutHelper(timeout); ValidateReadingFaultString(decoder); int size = await connection.ReadAsync(0, Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize), timeoutHelper.RemainingTime()); int offset = 0; while (size > 0) { int bytesDecoded = decoder.Decode(connection.AsyncReadBuffer, offset, size); offset += bytesDecoded; size -= bytesDecoded; if (decoder.CurrentState == ClientFramingDecoderState.Fault) { ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime()); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( FaultStringDecoder.GetFaultException(decoder.Fault, via.ToString(), contentType)); } else { if (decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString) { throw new Exception("invalid framing client state machine"); } if (size == 0) { offset = 0; size = await connection.ReadAsync(0, Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize), timeoutHelper.RemainingTime()); } } } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException()); }
internal static void SendFault(IConnection connection, string faultString, byte[] drainBuffer, TimeSpan sendTimeout, int maxRead) { if (TD.ConnectionReaderSendFaultIsEnabled()) { TD.ConnectionReaderSendFault(faultString); } EncodedFault encodedFault = new EncodedFault(faultString); TimeoutHelper timeoutHelper = new TimeoutHelper(sendTimeout); try { connection.Write(encodedFault.EncodedBytes, 0, encodedFault.EncodedBytes.Length, true, timeoutHelper.RemainingTime()); connection.Shutdown(timeoutHelper.RemainingTime()); } catch (CommunicationException e) { DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); connection.Abort(); return; } catch (TimeoutException e) { if (TD.SendTimeoutIsEnabled()) { TD.SendTimeout(e.Message); } DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); connection.Abort(); return; } // make sure we read until EOF or a quota is hit int read = 0; int readTotal = 0; for (;;) { try { read = connection.Read(drainBuffer, 0, drainBuffer.Length, timeoutHelper.RemainingTime()); } catch (CommunicationException e) { DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); connection.Abort(); return; } catch (TimeoutException e) { if (TD.SendTimeoutIsEnabled()) { TD.SendTimeout(e.Message); } DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); connection.Abort(); return; } if (read == 0) { break; } readTotal += read; if (readTotal > maxRead || timeoutHelper.RemainingTime() <= TimeSpan.Zero) { connection.Abort(); return; } } ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime()); }
internal static void SendFault(IConnection connection, string faultString, byte[] drainBuffer, TimeSpan sendTimeout, int maxRead) { EncodedFault fault = new EncodedFault(faultString); TimeoutHelper helper = new TimeoutHelper(sendTimeout); try { connection.Write(fault.EncodedBytes, 0, fault.EncodedBytes.Length, true, helper.RemainingTime()); connection.Shutdown(helper.RemainingTime()); } catch (CommunicationException exception) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information); } connection.Abort(); return; } catch (TimeoutException exception2) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception2, TraceEventType.Information); } connection.Abort(); return; } int num = 0; int num2 = 0; do { try { num = connection.Read(drainBuffer, 0, drainBuffer.Length, helper.RemainingTime()); } catch (CommunicationException exception3) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception3, TraceEventType.Information); } connection.Abort(); return; } catch (TimeoutException exception4) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception4, TraceEventType.Information); } connection.Abort(); return; } if (num == 0) { ConnectionUtilities.CloseNoThrow(connection, helper.RemainingTime()); return; } num2 += num; }while ((num2 <= maxRead) && (helper.RemainingTime() > TimeSpan.Zero)); connection.Abort(); }