< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.Framing.NetTcpExceptionConvertingDuplexPipe
Assembly: CoreWCF.NetTcp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.NetTcp/src/CoreWCF/Channels/Framing/NetTcpExceptionConvertingDuplexPipe.cs
Line coverage
56%
Covered lines: 31
Uncovered lines: 24
Coverable lines: 55
Total lines: 216
Line coverage: 56.3%
Branch coverage
38%
Covered branches: 7
Total branches: 18
Branch coverage: 38.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
AdvanceTo(...)100%11100%
AdvanceTo(...)100%11100%
CancelPendingRead()100%110%
Complete(...)100%11100%
ReadAsync()25%4442.85%
TryRead(...)100%11100%
ConvertReceiveException(...)100%110%
.ctor(...)100%11100%
Advance(...)100%11100%
CancelPendingFlush()100%110%
Complete(...)100%11100%
FlushAsync()100%1140%
GetMemory(...)100%110%
GetSpan(...)100%11100%
ConvertSendException(...)100%110%
ConvertTransferException(...)42.85%141450%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.NetTcp/src/CoreWCF/Channels/Framing/NetTcpExceptionConvertingDuplexPipe.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.Diagnostics;
 6using System.IO.Pipelines;
 7using System.Net.Sockets;
 8using System.Threading;
 9using System.Threading.Tasks;
 10using Microsoft.AspNetCore.Connections;
 11
 12namespace CoreWCF.Channels.Framing
 13{
 14    internal class NetTcpExceptionConvertingDuplexPipe : IDuplexPipe
 15    {
 7916        public NetTcpExceptionConvertingDuplexPipe(IDuplexPipe innerDuplexPipe)
 17        {
 7918            Input = new NetTcpExceptionConvertingPipeReader(innerDuplexPipe.Input);
 7919            Output = new NetTcpExceptionConvertingPipeWriter(innerDuplexPipe.Output);
 7920        }
 21
 125022        public PipeReader Input { get; }
 201723        public PipeWriter Output { get; }
 24
 25        private class NetTcpExceptionConvertingPipeReader : PipeReader
 26        {
 27            private PipeReader _input;
 28
 15829            public NetTcpExceptionConvertingPipeReader(PipeReader input) => _input = input;
 30
 59531            public override void AdvanceTo(SequencePosition consumed) => _input.AdvanceTo(consumed);
 13432            public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) => _input.AdvanceTo(con
 033            public override void CancelPendingRead() => _input.CancelPendingRead();
 7634            public override void Complete(Exception exception = null) => _input.Complete(exception);
 35            public override async ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default)
 36            {
 37                try
 38                {
 76539                    return await _input.ReadAsync(cancellationToken);
 40                }
 041                catch(SocketException socketException)
 42                {
 043                    throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
 044                        ConvertReceiveException(socketException), TraceEventType.Error);
 45                }
 346                catch(ConnectionResetException cre)
 47                {
 348                    if (cre.InnerException is SocketException socketException)
 49                    {
 350                        throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
 351                            ConvertTransferException(socketException, cre, false), TraceEventType.Error);
 52                    }
 053                    var exception = DiagnosticUtility.ExceptionUtility.ThrowHelper(
 054                        cre, TraceEventType.Error);
 055                    if (exception == cre)
 56                    {
 57                        // If we returned the same exception as was caught, rethrow to preserve the stack trace
 058                        throw;
 59                    }
 60                    else
 61                    {
 062                        throw exception;
 63                    }
 64                }
 68465            }
 66
 5567            public override bool TryRead(out ReadResult result) => _input.TryRead(out result);
 68
 69            private Exception ConvertReceiveException(SocketException socketException)
 70            {
 071                return ConvertTransferException(socketException, socketException, aborted: false);
 72            }
 73        }
 74
 75        private class NetTcpExceptionConvertingPipeWriter : PipeWriter
 76        {
 77            private PipeWriter _output;
 78
 15879            public NetTcpExceptionConvertingPipeWriter(PipeWriter output) => _output = output;
 80
 114881            public override void Advance(int bytes) => _output.Advance(bytes);
 082            public override void CancelPendingFlush() => _output.CancelPendingFlush();
 7783            public override void Complete(Exception exception = null) => _output.Complete(exception);
 84            public override async ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default)
 85            {
 86                try
 87                {
 200088                    return await _output.FlushAsync(cancellationToken);
 89                }
 090                catch (SocketException socketException)
 91                {
 092                    throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
 093                        ConvertSendException(socketException), TraceEventType.Error);
 94                }
 200095            }
 96
 097            public override Memory<byte> GetMemory(int sizeHint = 0) => _output.GetMemory(sizeHint);
 114898            public override Span<byte> GetSpan(int sizeHint = 0) => _output.GetSpan(sizeHint);
 99
 100            private Exception ConvertSendException(SocketException socketException)
 101            {
 0102                return ConvertTransferException(socketException, socketException, aborted: false);
 103            }
 104        }
 105
 106        private static Exception ConvertTransferException(SocketException socketException, Exception originalException, 
 107        {
 3108            if (socketException.ErrorCode == UnsafeNativeMethods.ERROR_INVALID_HANDLE)
 109            {
 0110                return new CommunicationObjectAbortedException(socketException.Message, socketException);
 111            }
 112
 3113            if (socketException.ErrorCode == UnsafeNativeMethods.WSAENETRESET ||
 3114                socketException.ErrorCode == UnsafeNativeMethods.WSAECONNABORTED ||
 3115                socketException.ErrorCode == UnsafeNativeMethods.WSAECONNRESET)
 116            {
 0117                if (aborted)
 118                {
 0119                    return new CommunicationObjectAbortedException(SR.TcpLocalConnectionAborted, originalException);
 120                }
 121                else
 122                {
 0123                    CommunicationException communicationException = new CommunicationException(SR.Format(SR.TcpConnectio
 124                    //if (TD.TcpConnectionResetErrorIsEnabled())
 125                    //{
 126                    //    if (socketConnection != null)
 127                    //    {
 128                    //        int socketId = (socketConnection.socket != null) ? socketConnection.socket.GetHashCode() :
 129                    //        TD.TcpConnectionResetError(socketId, socketConnection.RemoteEndpointAddress);
 130                    //    }
 131                    //}
 132                    //if (DiagnosticUtility.ShouldTrace(exceptionEventType))
 133                    //{
 134                    //    TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpConnectionResetError, GetEndpointStri
 135                    //}
 0136                    return communicationException;
 137                }
 138            }
 3139            else if (socketException.ErrorCode == UnsafeNativeMethods.WSAETIMEDOUT)
 140            {
 0141                TimeoutException timeoutException = new TimeoutException(SR.Format(SR.TcpConnectionTimedOut, "unknown"),
 142                //if (DiagnosticUtility.ShouldTrace(exceptionEventType))
 143                //{
 144                //    TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpConnectionTimedOut, GetEndpointString(SR.
 145                //}
 0146                return timeoutException;
 147            }
 148            else
 149            {
 3150                if (aborted)
 151                {
 0152                    return new CommunicationObjectAbortedException(SR.Format(SR.TcpTransferError, socketException.ErrorC
 153                }
 154                else
 155                {
 3156                    CommunicationException communicationException = new CommunicationException(SR.Format(SR.TcpTransferE
 157                    //if (DiagnosticUtility.ShouldTrace(exceptionEventType))
 158                    //{
 159                    //    TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpTransferError, GetEndpointString(SR.T
 160                    //}
 3161                    return communicationException;
 162                }
 163            }
 164        }
 165
 166        internal static class UnsafeNativeMethods
 167        {
 168            public const int ERROR_SUCCESS = 0;
 169            public const int ERROR_FILE_NOT_FOUND = 2;
 170            public const int ERROR_ACCESS_DENIED = 5;
 171            public const int ERROR_INVALID_HANDLE = 6;
 172            public const int ERROR_NOT_ENOUGH_MEMORY = 8;
 173            public const int ERROR_OUTOFMEMORY = 14;
 174            public const int ERROR_SHARING_VIOLATION = 32;
 175            public const int ERROR_NETNAME_DELETED = 64;
 176            public const int ERROR_INVALID_PARAMETER = 87;
 177            public const int ERROR_BROKEN_PIPE = 109;
 178            public const int ERROR_ALREADY_EXISTS = 183;
 179            public const int ERROR_PIPE_BUSY = 231;
 180            public const int ERROR_NO_DATA = 232;
 181            public const int ERROR_MORE_DATA = 234;
 182            public const int WAIT_TIMEOUT = 258;
 183            public const int ERROR_PIPE_CONNECTED = 535;
 184            public const int ERROR_OPERATION_ABORTED = 995;
 185            public const int ERROR_IO_PENDING = 997;
 186            public const int ERROR_SERVICE_ALREADY_RUNNING = 1056;
 187            public const int ERROR_SERVICE_DISABLED = 1058;
 188            public const int ERROR_NO_TRACKING_SERVICE = 1172;
 189            public const int ERROR_ALLOTTED_SPACE_EXCEEDED = 1344;
 190            public const int ERROR_NO_SYSTEM_RESOURCES = 1450;
 191
 192            // When querying for the token length
 193            private const int ERROR_INSUFFICIENT_BUFFER = 122;
 194
 195            public const int STATUS_PENDING = 0x103;
 196
 197            // socket errors
 198            public const int WSAACCESS = 10013;
 199            public const int WSAEMFILE = 10024;
 200            public const int WSAEMSGSIZE = 10040;
 201            public const int WSAEADDRINUSE = 10048;
 202            public const int WSAEADDRNOTAVAIL = 10049;
 203            public const int WSAENETDOWN = 10050;
 204            public const int WSAENETUNREACH = 10051;
 205            public const int WSAENETRESET = 10052;
 206            public const int WSAECONNABORTED = 10053;
 207            public const int WSAECONNRESET = 10054;
 208            public const int WSAENOBUFS = 10055;
 209            public const int WSAESHUTDOWN = 10058;
 210            public const int WSAETIMEDOUT = 10060;
 211            public const int WSAECONNREFUSED = 10061;
 212            public const int WSAEHOSTDOWN = 10064;
 213            public const int WSAEHOSTUNREACH = 10065;
 214        }
 215    }
 216}