| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.IO.Pipelines; |
| | | 8 | | using System.Net.Sockets; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using CoreWCF.Security; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Channels.Framing |
| | | 14 | | { |
| | | 15 | | // TODO: Implement the exception conversion |
| | | 16 | | internal partial class NamedPipeExceptionConvertingDuplexPipe : IDuplexPipe |
| | | 17 | | { |
| | 0 | 18 | | public NamedPipeExceptionConvertingDuplexPipe(IDuplexPipe innerDuplexPipe) |
| | | 19 | | { |
| | 0 | 20 | | Input = new NamedPipeExceptionConvertingPipeReader(innerDuplexPipe.Input); |
| | 0 | 21 | | Output = new NamedPipeExceptionConvertingPipeWriter(innerDuplexPipe.Output); |
| | 0 | 22 | | } |
| | | 23 | | |
| | 0 | 24 | | public PipeReader Input { get; } |
| | 0 | 25 | | public PipeWriter Output { get; } |
| | | 26 | | |
| | | 27 | | private class NamedPipeExceptionConvertingPipeReader : PipeReader |
| | | 28 | | { |
| | | 29 | | private PipeReader _input; |
| | | 30 | | private bool _isComplete; |
| | | 31 | | |
| | 0 | 32 | | public NamedPipeExceptionConvertingPipeReader(PipeReader input) => _input = input; |
| | | 33 | | |
| | 0 | 34 | | public override void AdvanceTo(SequencePosition consumed) => _input.AdvanceTo(consumed); |
| | 0 | 35 | | public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) => _input.AdvanceTo(con |
| | 0 | 36 | | public override void CancelPendingRead() => _input.CancelPendingRead(); |
| | | 37 | | public override void Complete(Exception exception = null) |
| | | 38 | | { |
| | 0 | 39 | | _input.Complete(exception); |
| | 0 | 40 | | _isComplete = true; |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public override async ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default) |
| | | 44 | | { |
| | | 45 | | try |
| | | 46 | | { |
| | 0 | 47 | | if (_isComplete) |
| | | 48 | | { |
| | 0 | 49 | | return new ReadResult(ReadOnlySequence<byte>.Empty, false, true); |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | return await _input.ReadAsync(cancellationToken); |
| | | 53 | | } |
| | 0 | 54 | | catch(SocketException socketException) |
| | | 55 | | { |
| | 0 | 56 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelper( |
| | 0 | 57 | | ConvertReceiveException(socketException), TraceEventType.Error); |
| | | 58 | | } |
| | 0 | 59 | | catch (InvalidOperationException ioe) |
| | | 60 | | { |
| | 0 | 61 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelper( |
| | 0 | 62 | | ConvertTransferException(new SocketException(UnsafeNativeMethods.WSAECONNABORTED), ioe, aborted: |
| | | 63 | | } |
| | 0 | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | public override bool TryRead(out ReadResult result) => _input.TryRead(out result); |
| | | 67 | | |
| | | 68 | | private Exception ConvertReceiveException(SocketException socketException) |
| | | 69 | | { |
| | 0 | 70 | | return ConvertTransferException(socketException, socketException, aborted: false); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | private class NamedPipeExceptionConvertingPipeWriter : PipeWriter |
| | | 75 | | { |
| | | 76 | | private PipeWriter _output; |
| | | 77 | | |
| | 0 | 78 | | public NamedPipeExceptionConvertingPipeWriter(PipeWriter output) => _output = output; |
| | | 79 | | |
| | 0 | 80 | | public override void Advance(int bytes) => _output.Advance(bytes); |
| | 0 | 81 | | public override void CancelPendingFlush() => _output.CancelPendingFlush(); |
| | 0 | 82 | | public override void Complete(Exception exception = null) => _output.Complete(exception); |
| | | 83 | | public override async ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default) |
| | | 84 | | { |
| | | 85 | | try |
| | | 86 | | { |
| | 0 | 87 | | return await _output.FlushAsync(cancellationToken); |
| | | 88 | | } |
| | 0 | 89 | | catch (SocketException socketException) |
| | | 90 | | { |
| | 0 | 91 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelper( |
| | 0 | 92 | | ConvertSendException(socketException), TraceEventType.Error); |
| | | 93 | | } |
| | 0 | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | public override Memory<byte> GetMemory(int sizeHint = 0) => _output.GetMemory(sizeHint); |
| | 0 | 97 | | public override Span<byte> GetSpan(int sizeHint = 0) => _output.GetSpan(sizeHint); |
| | | 98 | | |
| | | 99 | | private Exception ConvertSendException(SocketException socketException) |
| | | 100 | | { |
| | 0 | 101 | | return ConvertTransferException(socketException, socketException, aborted: false); |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private static Exception ConvertTransferException(SocketException socketException, Exception originalException, |
| | | 106 | | { |
| | 0 | 107 | | if (socketException.ErrorCode == UnsafeNativeMethods.ERROR_INVALID_HANDLE) |
| | | 108 | | { |
| | 0 | 109 | | return new CommunicationObjectAbortedException(socketException.Message, socketException); |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | if (socketException.ErrorCode == UnsafeNativeMethods.WSAENETRESET || |
| | 0 | 113 | | socketException.ErrorCode == UnsafeNativeMethods.WSAECONNABORTED || |
| | 0 | 114 | | socketException.ErrorCode == UnsafeNativeMethods.WSAECONNRESET) |
| | | 115 | | { |
| | 0 | 116 | | if (aborted) |
| | | 117 | | { |
| | 0 | 118 | | return new CommunicationObjectAbortedException(SR.TcpLocalConnectionAborted, originalException); |
| | | 119 | | } |
| | | 120 | | else |
| | | 121 | | { |
| | 0 | 122 | | CommunicationException communicationException = new CommunicationException(SR.Format(SR.TcpConnectio |
| | | 123 | | //if (TD.TcpConnectionResetErrorIsEnabled()) |
| | | 124 | | //{ |
| | | 125 | | // if (socketConnection != null) |
| | | 126 | | // { |
| | | 127 | | // int socketId = (socketConnection.socket != null) ? socketConnection.socket.GetHashCode() : |
| | | 128 | | // TD.TcpConnectionResetError(socketId, socketConnection.RemoteEndpointAddress); |
| | | 129 | | // } |
| | | 130 | | //} |
| | | 131 | | //if (DiagnosticUtility.ShouldTrace(exceptionEventType)) |
| | | 132 | | //{ |
| | | 133 | | // TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpConnectionResetError, GetEndpointStri |
| | | 134 | | //} |
| | 0 | 135 | | return communicationException; |
| | | 136 | | } |
| | | 137 | | } |
| | 0 | 138 | | else if (socketException.ErrorCode == UnsafeNativeMethods.WSAETIMEDOUT) |
| | | 139 | | { |
| | 0 | 140 | | TimeoutException timeoutException = new TimeoutException(SR.Format(SR.TcpConnectionTimedOut, "unknown"), |
| | | 141 | | //if (DiagnosticUtility.ShouldTrace(exceptionEventType)) |
| | | 142 | | //{ |
| | | 143 | | // TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpConnectionTimedOut, GetEndpointString(SR. |
| | | 144 | | //} |
| | 0 | 145 | | return timeoutException; |
| | | 146 | | } |
| | | 147 | | else |
| | | 148 | | { |
| | 0 | 149 | | if (aborted) |
| | | 150 | | { |
| | 0 | 151 | | return new CommunicationObjectAbortedException(SR.Format(SR.TcpTransferError, socketException.ErrorC |
| | | 152 | | } |
| | | 153 | | else |
| | | 154 | | { |
| | 0 | 155 | | CommunicationException communicationException = new CommunicationException(SR.Format(SR.TcpTransferE |
| | | 156 | | //if (DiagnosticUtility.ShouldTrace(exceptionEventType)) |
| | | 157 | | //{ |
| | | 158 | | // TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpTransferError, GetEndpointString(SR.T |
| | | 159 | | //} |
| | 0 | 160 | | return communicationException; |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | } |