internal Http2Protocol(SecureSocket socket, IStreamStore streamsStore, ProtocolOptions options) { this.options = options; this.streamsStore = streamsStore; this.serializer = new FrameSerializer(this.options); this.builder = new FrameBuilder(); this.socket = socket; this.isServer = true; this.handshakeFinishedEventRaised = new ManualResetEvent(false); }
/// <summary> /// Initializes a new instance of the <see cref="Http2Protocol"/> class. /// </summary> /// <param name="uri">The URI.</param> /// <param name="streamsStore">The streams store.</param> /// <param name="options">Protocol options</param> internal Http2Protocol(Uri uri, IStreamStore streamsStore, ProtocolOptions options) { this.options = options; this.streamsStore = streamsStore; this.serializer = new FrameSerializer(this.options); this.builder = new FrameBuilder(); this.uri = uri; this.isServer = false; this.handshakeFinishedEventRaised = new ManualResetEvent(false); }
/// <summary> /// Initializes a new instance of the <see cref="ProtocolSession"/> class. /// </summary> /// <param name="uri">the URI.</param> /// <param name="isServer">the Server flag.</param> /// <param name="options">Session options.</param> public ProtocolSession(SecureSocket socket, ProtocolOptions options) { this.isFlowControlEnabled = options.IsFlowControl; this.streams = new List <Http2Stream>(); this.closedStreams = new List <Http2Stream>(); this.isServer = true; this.CurrentWindowBalanceToServer = 512; this.CurrentWindowBalanceFromServer = 256; this.protocol = new Http2Protocol(socket, this, options); this.protocol.OnSessionFrame += this.OnSessionFrame; this.protocol.OnClose += this.OnProtocolClose; this.protocol.OnOpen += this.OnProtocolOpen; this.protocol.OnError += this.OnProtocolError; this.protocol.OnPing += this.OnProtocolPing; if (options.UseCompression) { this.Protocol.SetProcessors(new List <IMessageProcessor> { new CompressionProcessor() }); } }
/// <summary> /// Create http2 session. Close if previously opened. /// </summary> /// <param name="uri">The URI</param> /// <param name="option">Options for session</param> /// <returns>The return code.</returns> private static int CreateSession(Uri uri, ProtocolOptions option) { int res = 0; CloseSession(); session = new ProtocolSession(uri, false, option); // URI can still be invalid, missing protocol prefix for example try { session.Open(); session.OnOpen += OnSessionOpened; session.OnClose += OnSessionClosed; session.OnError += OnSessionError; session.OnStreamOpened += OnStreamOpened; session.OnStreamClosed += OnStreamClosed; // wait until session changes its state // for high latency connections we cannot just start using this session sessionMonitorEvent.Reset(); } catch (Exception e) { Http2Logger.LogError("Unable to open session for " + uri + ". " + e.Message); res = 1; } return res; }
private void CreateSession(VirtualSocket socket) { try { Console.WriteLine("New connection"); var options = new ProtocolOptions(); ProtocolSession session = new ProtocolSession(socket, options); session.OnStreamOpened += session_OnStreamOpened; session.OnError += session_OnError; session.Open(); } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// Initializes a new instance of the <see cref="Http2Protocol"/> class. /// </summary> /// <param name="uri">The URI.</param> /// <param name="streamsStore">The streams store.</param> /// <param name="options">Protocol options</param> internal Http2Protocol(Uri uri, IStreamStore streamsStore, ProtocolOptions options) { this.options = options; this.streamsStore = streamsStore; this.serializer = new FrameSerializer(this.options); this.builder = new FrameBuilder(); this.uri = uri; }
/// <summary> /// Processes the specified input data. /// </summary> /// <param name="inputData">The input data.</param> /// <param name="type">The type.</param> /// <param name="optios">Options.</param> /// <param name="flags">Flags.</param> public void Process(ref byte[] inputData, DirectionProcessType type, ProtocolOptions optios, int flags) { inputData = type == DirectionProcessType.Inbound ? Encrypt(inputData) : Decrypt(inputData); }
/// <summary> /// Initializes a new instance of the <see cref="FrameSerializer"/> class. /// </summary> /// <param name="options">The options.</param> public FrameSerializer(ProtocolOptions options) { Option = options; }
/// <summary> /// Processes the specified headers. /// </summary> /// <param name="headers">The headers.</param> /// <param name="type">The type.</param> /// <param name="options">Options.</param> /// <param name="flags">Flags.</param> public void Process(ref byte[] headers, DirectionProcessType type, ProtocolOptions options, int flags) { if (!options.UseCompression) return; byte[] result = null; if (type == DirectionProcessType.Inbound) { Decompress(headers, out result); } else { Compress(headers, out result); } headers = result; }
private SecureSocket CreateSocketByUri(Uri uri) { if (!String.Equals(uri.Scheme, "http", StringComparison.OrdinalIgnoreCase) && !String.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("Unrecognized scheme: " + uri.Scheme); } SecurityOptions options; var extensions = new ExtensionType[] { ExtensionType.Renegotiation, ExtensionType.ALPN }; if (!String.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { options = new SecurityOptions(SecureProtocol.None, null, ConnectionEnd.Client); } else { options = new SecurityOptions(SecureProtocol.Tls1, extensions, ConnectionEnd.Client); } options.Entity = ConnectionEnd.Client; options.CommonName = uri.Host; options.VerificationType = CredentialVerification.None; options.Certificate = Org.Mentalis.Security.Certificates.Certificate.CreateFromCerFile(@"certificate.pfx"); options.Flags = SecurityFlags.Default; options.AllowedAlgorithms = SslAlgorithms.RSA_AES_128_SHA | SslAlgorithms.NULL_COMPRESSION; var s = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options); s.OnClose += SocketOnClose; try { if (String.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { s.OnHandshakeFinish += this.HandshakeFinishedHandler; } s.Connect(new Net.DnsEndPoint(uri.Host, uri.Port)); if (String.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { this.handshakeFinishedEventRaised.WaitOne(60000); if (!s.IsNegotiationCompleted) { throw new Exception("Handshake failed"); } if (!s.Connected) { throw new Exception("Connection was lost!"); } s.OnHandshakeFinish -= this.HandshakeFinishedHandler; this.ApplyProtocolSelectionResults(options.GetSelectedProtocol()); } } catch (Exception ex) { s.Close(); // Emitting protocol error. It will emit session OnError if (OnError != null) { this.OnError(this, new ProtocolErrorEventArgs(ex)); } throw; } return(s); }
/// <summary> /// Initializes a new instance of the <see cref="ProtocolSession"/> class. /// </summary> /// <param name="uri">the URI.</param> /// <param name="isServer">the Server flag.</param> /// <param name="options">Session options.</param> public ProtocolSession(Uri uri, bool isServer, ProtocolOptions options) { this.isFlowControlEnabled = options.IsFlowControl; this.streams = new List<Http2Stream>(); this.closedStreams = new List<Http2Stream>(); this.isServer = isServer; this.CurrentWindowBalanceToServer = 512; this.CurrentWindowBalanceFromServer = 256; this.protocol = new Http2Protocol(uri, this, options); this.protocol.OnSessionFrame += this.OnSessionFrame; this.protocol.OnClose += this.OnProtocolClose; this.protocol.OnOpen += this.OnProtocolOpen; this.protocol.OnError += this.OnProtocolError; this.protocol.OnPing += this.OnProtocolPing; if (options.UseCompression) { this.Protocol.SetProcessors(new List<IMessageProcessor> { new CompressionProcessor() }); } }
private SecureSocket CreateSocketByUri(Uri uri) { if (!String.Equals(uri.Scheme, "http", StringComparison.OrdinalIgnoreCase) && !String.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException("Unrecognized scheme: " + uri.Scheme); SecurityOptions options; var extensions = new ExtensionType[] { ExtensionType.Renegotiation, ExtensionType.ALPN }; if (!String.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { options = new SecurityOptions(SecureProtocol.None, null, ConnectionEnd.Client); } else { options = new SecurityOptions(SecureProtocol.Tls1, extensions, ConnectionEnd.Client); } options.Entity = ConnectionEnd.Client; options.CommonName = uri.Host; options.VerificationType = CredentialVerification.None; options.Certificate = Org.Mentalis.Security.Certificates.Certificate.CreateFromCerFile(@"certificate.pfx"); options.Flags = SecurityFlags.Default; options.AllowedAlgorithms = SslAlgorithms.RSA_AES_128_SHA | SslAlgorithms.NULL_COMPRESSION; var s = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options); s.OnClose += SocketOnClose; try { if (String.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { s.OnHandshakeFinish += this.HandshakeFinishedHandler; } s.Connect(new Net.DnsEndPoint(uri.Host, uri.Port)); if (String.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { this.handshakeFinishedEventRaised.WaitOne(60000); if (!s.IsNegotiationCompleted) { throw new Exception("Handshake failed"); } if (!s.Connected) { throw new Exception("Connection was lost!"); } s.OnHandshakeFinish -= this.HandshakeFinishedHandler; this.ApplyProtocolSelectionResults(options.GetSelectedProtocol()); } } catch (Exception ex) { s.Close(); // Emitting protocol error. It will emit session OnError if (OnError != null) this.OnError(this, new ProtocolErrorEventArgs(ex)); throw; } return s; }