< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.Framing.SingletonFramingMiddleware
Assembly: CoreWCF.NetFramingBase
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.NetFramingBase/src/CoreWCF/Channels/Framing/SingletonFramingMiddleware.cs
Line coverage
62%
Covered lines: 22
Uncovered lines: 13
Coverable lines: 35
Total lines: 120
Line coverage: 62.8%
Branch coverage
65%
Covered branches: 13
Total branches: 20
Branch coverage: 65%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
OnConnectedAsync()65%202059.37%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.NetFramingBase/src/CoreWCF/Channels/Framing/SingletonFramingMiddleware.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.Threading.Tasks;
 8using CoreWCF.Configuration;
 9using CoreWCF.Runtime;
 10
 11namespace CoreWCF.Channels.Framing
 12{
 13    internal class SingletonFramingMiddleware
 14    {
 15        private readonly HandshakeDelegate _next;
 16
 8317        public SingletonFramingMiddleware(HandshakeDelegate next)
 18        {
 8319            _next = next;
 8320        }
 21
 22        public async Task OnConnectedAsync(FramingConnection connection)
 23        {
 5024            var decoder = new ServerSingletonDecoder(ConnectionOrientedTransportDefaults.MaxViaSize, ConnectionOrientedT
 5025            bool success = false;
 26
 27            try
 28            {
 29                ReadOnlySequence<byte> buffer;
 10030                while (decoder.CurrentState != ServerSingletonDecoder.State.PreUpgradeStart)
 31                {
 5032                    System.IO.Pipelines.ReadResult readResult = await connection.Input.ReadAsync(connection.ChannelIniti
 5033                    buffer = readResult.Buffer;
 34
 5035                    if (readResult.IsCompleted && buffer.IsEmpty)
 36                    {
 37                        // The peer closed the connection before sending the via record. Surface
 38                        // this as a premature EOF; otherwise PipeReader.ReadAsync would keep
 39                        // returning synchronously with an empty buffer and the outer loop would
 40                        // never make progress.
 041                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException())
 42                    }
 43
 26244                    while (buffer.Length > 0)
 45                    {
 26246                        int bytesDecoded = decoder.Decode(buffer);
 26247                        if (bytesDecoded > 0)
 48                        {
 26249                            buffer = buffer.Slice(bytesDecoded);
 50                        }
 51
 26252                        if (decoder.CurrentState == ServerSingletonDecoder.State.PreUpgradeStart)
 53                        {
 54                            // We now know the Via address (which endpoint the client is connecting to).
 55                            // The connection now needs to be handled by the correct endpoint which can
 56                            // handle upgrades etc.
 57                            break; //exit loop
 58                        }
 59                    }
 60
 5061                    connection.Input.AdvanceTo(buffer.Start);
 62                }
 63
 5064                success = true;
 5065            }
 66            catch (CommunicationException exception)
 67            {
 068                DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information);
 069            }
 70            catch (OperationCanceledException exception)
 71            {
 72                //if (TD.ReceiveTimeoutIsEnabled())
 73                //{
 74                //    TD.ReceiveTimeout(exception.Message);
 75                //}
 076                DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information);
 077            }
 78            catch (TimeoutException exception)
 79            {
 80                //if (TD.ReceiveTimeoutIsEnabled())
 81                //{
 82                //    TD.ReceiveTimeout(exception.Message);
 83                //}
 084                DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information);
 085            }
 86            catch (Exception e)
 87            {
 088                if (Fx.IsFatal(e))
 89                {
 090                    throw;
 91                }
 092                if (!TransportExceptionHandler.HandleTransportExceptionHelper(e))
 93                {
 094                    throw;
 95                }
 96                // containment -- all exceptions abort the reader, no additional containment action necessary
 097            }
 98            finally
 99            {
 50100                if (!success)
 101                {
 102                    // TODO: On .NET Framework, this Abort call via a long winding path of plumbing will trigger a new p
 103                    // as this connection establishment handshake has failed. Some back pressure mechanism needs to be i
 104                    // stop extra incoming connection handshakes from being started. Maybe a semaphore which is async wa
 105                    // incoming request and then released on completion of handshake or on an exception. It also closes 
 106                    // so that's all that's happening here for now. Returning and completing the task will cause the con
 0107                    connection.Abort();
 108                }
 109            }
 110
 50111            if (success)
 112            {
 50113                connection.FramingDecoder = decoder;
 50114                await _next(connection);
 115            }
 116            // else:
 117            //   returning will close the connection if it hasn't already been.
 50118        }
 119    }
 120}