/// <summary>
 /// Initializes a new instance of the <see cref="Http2OwinMessageHandler"/> class.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="end"></param>
 /// <param name="transportInfo">The transport information.</param>
 /// <param name="next">The next layer delegate.</param>
 /// <param name="cancel">The cancellation token.</param>
 public Http2OwinMessageHandler(DuplexStream stream, ConnectionEnd end, TransportInformation transportInfo,
                         AppFunc next, CancellationToken cancel)
     : base(stream, end, stream.IsSecure, transportInfo, cancel)
 {
     _next = next;
     stream.OnClose += delegate { Dispose(); };
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="Http2MessageHandler"/> class.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="end">TODO</param>
 /// <param name="isSecure"></param>
 /// <param name="transportInfo">The transport information.</param>
 /// <param name="cancel">The cancel.</param>
 protected Http2MessageHandler(Stream stream, ConnectionEnd end, bool isSecure, 
                                 TransportInformation transportInfo, CancellationToken cancel)
 {
     _isSecure = isSecure;
     _transportInfo = transportInfo;
     _isDisposed = false;
     _cancToken = cancel;
     _stream = stream;
     _end = end;
     _wereFirstSettingsSent = false;
 }
 public Http2ClientMessageHandler(DuplexStream stream, ConnectionEnd end, TransportInformation transportInfo, CancellationToken cancel)
     : base(stream, end, stream.IsSecure, transportInfo, cancel)
 {
     _fileHelper = new FileHelper(ConnectionEnd.Client);
     stream.OnClose += delegate { Dispose(); };
 }
        private TransportInformation GetTransportInfo(SecureSocket incomingClient)
        {
            var localEndPoint = (IPEndPoint)incomingClient.LocalEndPoint;
            var remoteEndPoint = (IPEndPoint)incomingClient.RemoteEndPoint;

            var transportInfo = new TransportInformation
            {
                LocalPort = localEndPoint.Port,
                RemotePort = remoteEndPoint.Port,
            };

            // Side effect of using dual mode sockets, the IPv4 addresses look like 0::ffff:127.0.0.1.
            if (localEndPoint.Address.IsIPv4MappedToIPv6)
            {
                transportInfo.LocalIpAddress = localEndPoint.Address.MapToIPv4().ToString();
            }
            else
            {
                transportInfo.LocalIpAddress = localEndPoint.Address.ToString();
            }

            if (remoteEndPoint.Address.IsIPv4MappedToIPv6)
            {
                transportInfo.RemoteIpAddress = remoteEndPoint.Address.MapToIPv4().ToString();
            }
            else
            {
                transportInfo.RemoteIpAddress = remoteEndPoint.Address.ToString();
            }

            return transportInfo;
        }
        private async void OpenHttp2Session(DuplexStream incomingClientStream, 
                                            TransportInformation transportInformation)
        {
            Http2Logger.LogDebug("Handshake successful");
            using (var messageHandler = new Http2OwinMessageHandler(incomingClientStream, ConnectionEnd.Server, transportInformation, _next, _cancelClientHandling.Token))
            {
                try
                {
                    await messageHandler.StartSessionAsync();
                }
                catch (Exception)
                {
                    Http2Logger.LogError("Client was disconnected");
                }
            }

            GC.Collect();
        }
        private void HandleRequest(DuplexStream incomingClient, string alpnSelectedProtocol, 
                                   TransportInformation transportInformation, bool backToHttp11)
        {
            //Server checks selected protocol and calls http2 or http11 layer
            if (backToHttp11 || alpnSelectedProtocol == Protocols.Http1)
            {
                if (incomingClient.IsSecure)
                    Http2Logger.LogDebug("Ssl chose http11");

                new Http11ProtocolOwinAdapter(incomingClient, _options.Protocol, _next).ProcessRequest();
                return;
            }

            //ALPN selected http2. No need to perform upgrade handshake.
            OpenHttp2Session(incomingClient, transportInformation);
        }