Beispiel #1
0
        public HttpConnection(Socket sock, EndPointListener epl)
#endif
        {
            _sock    = sock;
            _epl     = epl;
            IsSecure = epl.Secure;

#if !NETSTANDARD1_3
            if (!IsSecure)
            {
                Stream = new NetworkStream(sock, false);
            }
            else
            {
                var sslStream = new SslStream(new NetworkStream(sock, false), true);
                sslStream.AuthenticateAsServerAsync(cert).GetAwaiter().GetResult();

                Stream = sslStream;
            }
#else
            Stream = new NetworkStream(sock, false);
#endif
            _timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            Init();
        }
Beispiel #2
0
        internal static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
        {
            if (IPToEndpoints.TryGetValue(ep.Address, out var p))
            {
                if (p.TryRemove(ep.Port, out _) && p.Count == 0)
                {
                    IPToEndpoints.TryRemove(ep.Address, out _);
                }
            }

            epl.Close();
        }
Beispiel #3
0
        private static EndPointListener GetEpListener(string host, int port, HttpListener listener, bool secure = false)
        {
            IPAddress addr;

            if (host == "*")
            {
                addr = IPAddress.Any;
            }
            else if (IPAddress.TryParse(host, out addr) == false)
            {
                try
                {
                    var iphost = new IPHostEntry
                    {
                        HostName    = host,
                        AddressList = Dns.GetHostAddressesAsync(host).Result
                    };

                    addr = iphost.AddressList[0];
                }
                catch
                {
                    addr = IPAddress.Any;
                }
            }

            Dictionary <int, EndPointListener> p;

            if (IPToEndpoints.ContainsKey(addr))
            {
                p = IPToEndpoints[addr];
            }
            else
            {
                p = new Dictionary <int, EndPointListener>();
                IPToEndpoints[addr] = p;
            }

            EndPointListener epl;

            if (p.ContainsKey(port))
            {
                epl = p[port];
            }
            else
            {
                epl     = new EndPointListener(listener, addr, port, secure);
                p[port] = epl;
            }

            return(epl);
        }
Beispiel #4
0
        public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
        {
            lock (IPToEndpoints)
            {
                var p = IPToEndpoints[ep.Address];
                p.Remove(ep.Port);
                if (p.Count == 0)
                {
                    IPToEndpoints.Remove(ep.Address);
                }
            }

            epl.Close();
        }
Beispiel #5
0
 public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
 {
     lock (_ipToEndpoints)
     {
         // Dictionary<int, EndPointListener> p
         Hashtable p = null;
         p = (Hashtable)_ipToEndpoints[ep.Address];
         p.Remove(ep.Port);
         if (p.Count == 0)
         {
             _ipToEndpoints.Remove(ep.Address);
         }
         epl.Close();
     }
 }
Beispiel #6
0
        public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
        {
            lock (IPToEndpoints)
            {
                // Dictionary<int, EndPointListener> p
                var p = (Hashtable)IPToEndpoints[ep.Address];
                p.Remove(ep.Port);
                if (p.Count == 0)
                {
                    IPToEndpoints.Remove(ep.Address);
                }
            }

            epl.CloseAsync().Wait(); // TODO: Is this right?
        }
Beispiel #7
0
        public HttpConnection(Socket sock, EndPointListener epl)
#endif
        {
            _sock = sock;
            _epl  = epl;

#if SSL
            IsSecure = secure;

            if (!secure)
            {
                Stream = new NetworkStream(sock, false);
            }
            else
            {
                _cert = cert;

                ssl_stream = epl.Listener.CreateSslStream(new NetworkStream(sock, false), false, (t, c, ch, e) =>
                {
                    if (c == null)
                    {
                        return(true);
                    }
                    var c2 = c as X509Certificate2;
                    if (c2 == null)
                    {
                        c2 = new X509Certificate2(c.GetRawCertData());
                    }
                    client_cert        = c2;
                    client_cert_errors = new int[] { (int)e };
                    return(true);
                });
                stream = ssl_stream.AuthenticatedStream;
            }
#else
            Stream = new NetworkStream(sock, false);
#endif
            _timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            Init();
        }
Beispiel #8
0
        public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate cert)
        {
            _sock    = sock;
            _epl     = epl;
            IsSecure = secure;
            _cert    = cert;
            if (secure == false)
            {
                _stream = new NetworkStream(sock, false);
            }
            else
            {
#if SSL
                ssl_stream = epl.Listener.CreateSslStream(new NetworkStream(sock, false), false, (t, c, ch, e) =>
                {
                    if (c == null)
                    {
                        return(true);
                    }
                    var c2 = c as X509Certificate2;
                    if (c2 == null)
                    {
                        c2 = new X509Certificate2(c.GetRawCertData());
                    }
                    client_cert        = c2;
                    client_cert_errors = new int[] { (int)e };
                    return(true);
                });
                stream = ssl_stream.AuthenticatedStream;
#else
                throw new Exception("SSL is not supported");
#endif
            }
            _timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            Init();
        }
 public HttpConnection(Socket sock, EndPointListener epl, X509Certificate cert)