< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.Framing.NamedPipeExceptionConvertingDuplexPipe
Assembly: CoreWCF.NetNamedPipe
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.NetNamedPipe/src/CoreWCF/Channels/Framing/NamedPipeExceptionConvertingDuplexPipe.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 53
Coverable lines: 53
Total lines: 165
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
.ctor(...)100%110%
AdvanceTo(...)100%110%
AdvanceTo(...)100%110%
CancelPendingRead()100%110%
Complete(...)100%110%
ReadAsync()0%220%
TryRead(...)100%110%
ConvertReceiveException(...)100%110%
.ctor(...)100%110%
Advance(...)100%110%
CancelPendingFlush()100%110%
Complete(...)100%110%
FlushAsync()100%110%
GetMemory(...)100%110%
GetSpan(...)100%110%
ConvertSendException(...)100%110%
ConvertTransferException(...)0%14140%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.NetNamedPipe/src/CoreWCF/Channels/Framing/NamedPipeExceptionConvertingDuplexPipe.cs

#LineLine coverage
 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
 4using System;
 5using System.Buffers;
 6using System.Diagnostics;
 7using System.IO.Pipelines;
 8using System.Net.Sockets;
 9using System.Threading;
 10using System.Threading.Tasks;
 11using CoreWCF.Security;
 12
 13namespace CoreWCF.Channels.Framing
 14{
 15    // TODO: Implement the exception conversion
 16    internal partial class NamedPipeExceptionConvertingDuplexPipe : IDuplexPipe
 17    {
 018        public NamedPipeExceptionConvertingDuplexPipe(IDuplexPipe innerDuplexPipe)
 19        {
 020            Input = new NamedPipeExceptionConvertingPipeReader(innerDuplexPipe.Input);
 021            Output = new NamedPipeExceptionConvertingPipeWriter(innerDuplexPipe.Output);
 022        }
 23
 024        public PipeReader Input { get; }
 025        public PipeWriter Output { get; }
 26
 27        private class NamedPipeExceptionConvertingPipeReader : PipeReader
 28        {
 29            private PipeReader _input;
 30            private bool _isComplete;
 31
 032            public NamedPipeExceptionConvertingPipeReader(PipeReader input) => _input = input;
 33
 034            public override void AdvanceTo(SequencePosition consumed) => _input.AdvanceTo(consumed);
 035            public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) => _input.AdvanceTo(con
 036            public override void CancelPendingRead() => _input.CancelPendingRead();
 37            public override void Complete(Exception exception = null)
 38            {
 039                _input.Complete(exception);
 040                _isComplete = true;
 041            }
 42
 43            public override async ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default)
 44            {
 45                try
 46                {
 047                    if (_isComplete)
 48                    {
 049                        return new ReadResult(ReadOnlySequence<byte>.Empty, false, true);
 50                    }
 51
 052                    return await _input.ReadAsync(cancellationToken);
 53                }
 054                catch(SocketException socketException)
 55                {
 056                    throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
 057                        ConvertReceiveException(socketException), TraceEventType.Error);
 58                }
 059                catch (InvalidOperationException ioe)
 60                {
 061                    throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
 062                        ConvertTransferException(new SocketException(UnsafeNativeMethods.WSAECONNABORTED), ioe, aborted:
 63                }
 064            }
 65
 066            public override bool TryRead(out ReadResult result) => _input.TryRead(out result);
 67
 68            private Exception ConvertReceiveException(SocketException socketException)
 69            {
 070                return ConvertTransferException(socketException, socketException, aborted: false);
 71            }
 72        }
 73
 74        private class NamedPipeExceptionConvertingPipeWriter : PipeWriter
 75        {
 76            private PipeWriter _output;
 77
 078            public NamedPipeExceptionConvertingPipeWriter(PipeWriter output) => _output = output;
 79
 080            public override void Advance(int bytes) => _output.Advance(bytes);
 081            public override void CancelPendingFlush() => _output.CancelPendingFlush();
 082            public override void Complete(Exception exception = null) => _output.Complete(exception);
 83            public override async ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default)
 84            {
 85                try
 86                {
 087                    return await _output.FlushAsync(cancellationToken);
 88                }
 089                catch (SocketException socketException)
 90                {
 091                    throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
 092                        ConvertSendException(socketException), TraceEventType.Error);
 93                }
 094            }
 95
 096            public override Memory<byte> GetMemory(int sizeHint = 0) => _output.GetMemory(sizeHint);
 097            public override Span<byte> GetSpan(int sizeHint = 0) => _output.GetSpan(sizeHint);
 98
 99            private Exception ConvertSendException(SocketException socketException)
 100            {
 0101                return ConvertTransferException(socketException, socketException, aborted: false);
 102            }
 103        }
 104
 105        private static Exception ConvertTransferException(SocketException socketException, Exception originalException, 
 106        {
 0107            if (socketException.ErrorCode == UnsafeNativeMethods.ERROR_INVALID_HANDLE)
 108            {
 0109                return new CommunicationObjectAbortedException(socketException.Message, socketException);
 110            }
 111
 0112            if (socketException.ErrorCode == UnsafeNativeMethods.WSAENETRESET ||
 0113                socketException.ErrorCode == UnsafeNativeMethods.WSAECONNABORTED ||
 0114                socketException.ErrorCode == UnsafeNativeMethods.WSAECONNRESET)
 115            {
 0116                if (aborted)
 117                {
 0118                    return new CommunicationObjectAbortedException(SR.TcpLocalConnectionAborted, originalException);
 119                }
 120                else
 121                {
 0122                    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                    //}
 0135                    return communicationException;
 136                }
 137            }
 0138            else if (socketException.ErrorCode == UnsafeNativeMethods.WSAETIMEDOUT)
 139            {
 0140                TimeoutException timeoutException = new TimeoutException(SR.Format(SR.TcpConnectionTimedOut, "unknown"),
 141                //if (DiagnosticUtility.ShouldTrace(exceptionEventType))
 142                //{
 143                //    TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpConnectionTimedOut, GetEndpointString(SR.
 144                //}
 0145                return timeoutException;
 146            }
 147            else
 148            {
 0149                if (aborted)
 150                {
 0151                    return new CommunicationObjectAbortedException(SR.Format(SR.TcpTransferError, socketException.ErrorC
 152                }
 153                else
 154                {
 0155                    CommunicationException communicationException = new CommunicationException(SR.Format(SR.TcpTransferE
 156                    //if (DiagnosticUtility.ShouldTrace(exceptionEventType))
 157                    //{
 158                    //    TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpTransferError, GetEndpointString(SR.T
 159                    //}
 0160                    return communicationException;
 161                }
 162            }
 163        }
 164    }
 165}