示例#1
0
        public static IPAddress ToIPAddress(IpAddressInfo address)
        {
            if (address.Equals(IpAddressInfo.Any))
            {
                return(IPAddress.Any);
            }
            if (address.Equals(IpAddressInfo.IPv6Any))
            {
                return(IPAddress.IPv6Any);
            }
            if (address.Equals(IpAddressInfo.Loopback))
            {
                return(IPAddress.Loopback);
            }
            if (address.Equals(IpAddressInfo.IPv6Loopback))
            {
                return(IPAddress.IPv6Loopback);
            }

            return(IPAddress.Parse(address.Address));
        }
        private Task SendMessageIfSocketNotDisposed(byte[] messageData, IpEndPointInfo destination, IpAddressInfo fromLocalIpAddress, CancellationToken cancellationToken)
        {
            var sockets = _sendSockets;

            if (sockets != null)
            {
                sockets = sockets.ToList();

                var tasks = sockets.Where(s => (fromLocalIpAddress == null || fromLocalIpAddress.Equals(s.LocalIPAddress)))
                            .Select(s => SendFromSocket(s, messageData, destination, cancellationToken));
                return(Task.WhenAll(tasks));
            }

            return(Task.CompletedTask);
        }
示例#3
0
        public EndPointListener(HttpListener listener, IpAddressInfo addr, int port, bool secure, ICertificate cert, ILogger logger, ICryptoProvider cryptoProvider, IStreamFactory streamFactory, ISocketFactory socketFactory, IMemoryStreamFactory memoryStreamFactory, ITextEncoding textEncoding)
        {
            this.listener        = listener;
            _logger              = logger;
            _cryptoProvider      = cryptoProvider;
            _streamFactory       = streamFactory;
            _socketFactory       = socketFactory;
            _memoryStreamFactory = memoryStreamFactory;
            _textEncoding        = textEncoding;

            this.secure = secure;
            this.cert   = cert;

            _enableDualMode = addr.Equals(IpAddressInfo.IPv6Any);
            endpoint        = new IpEndPointInfo(addr, port);

            prefixes     = new Dictionary <ListenerPrefix, HttpListener>();
            unregistered = new Dictionary <HttpConnection, HttpConnection>();

            CreateSocket();
        }
        private List <ISocket> GetSendSockets(IpAddressInfo fromLocalIpAddress, IpEndPointInfo destination)
        {
            EnsureSendSocketCreated();

            lock (_SendSocketSynchroniser)
            {
                var sockets = _sendSockets.Where(i => i.LocalIPAddress.AddressFamily == fromLocalIpAddress.AddressFamily);

                // Send from the Any socket and the socket with the matching address
                if (fromLocalIpAddress.AddressFamily == IpAddressFamily.InterNetwork)
                {
                    sockets = sockets.Where(i => i.LocalIPAddress.Equals(IpAddressInfo.Any) || fromLocalIpAddress.Equals(i.LocalIPAddress));

                    // If sending to the loopback address, filter the socket list as well
                    if (destination.IpAddress.Equals(IpAddressInfo.Loopback))
                    {
                        sockets = sockets.Where(i => i.LocalIPAddress.Equals(IpAddressInfo.Any) || i.LocalIPAddress.Equals(IpAddressInfo.Loopback));
                    }
                }
                else if (fromLocalIpAddress.AddressFamily == IpAddressFamily.InterNetworkV6)
                {
                    sockets = sockets.Where(i => i.LocalIPAddress.Equals(IpAddressInfo.IPv6Any) || fromLocalIpAddress.Equals(i.LocalIPAddress));

                    // If sending to the loopback address, filter the socket list as well
                    if (destination.IpAddress.Equals(IpAddressInfo.IPv6Loopback))
                    {
                        sockets = sockets.Where(i => i.LocalIPAddress.Equals(IpAddressInfo.IPv6Any) || i.LocalIPAddress.Equals(IpAddressInfo.IPv6Loopback));
                    }
                }

                return(sockets.ToList());
            }
        }