コード例 #1
0
        protected void ConnectToPeer(IPEndPoint endPoint, bool isTrusted = false)
        {
            endPoint = endPoint.Unmap();
            if (endPoint.Port == ListenerTcpPort && localAddresses.Contains(endPoint.Address))
            {
                return;
            }

            if (isTrusted)
            {
                TrustedIpAddresses.Add(endPoint.Address);
            }
            if (ConnectedAddresses.TryGetValue(endPoint.Address, out int count) && count >= MaxConnectionsPerAddress)
            {
                return;
            }
            if (ConnectedPeers.Values.Contains(endPoint))
            {
                return;
            }
            ImmutableInterlocked.Update(ref ConnectingPeers, p =>
            {
                if ((p.Count >= ConnectingMax && !isTrusted) || p.Contains(endPoint))
                {
                    return(p);
                }
                tcp_manager.Tell(new Tcp.Connect(endPoint));
                return(p.Add(endPoint));
            });
        }
コード例 #2
0
        protected void ConnectToPeer(IPEndPoint endPoint, bool isTrusted = false)
        {
            endPoint = endPoint.Unmap();
            // If the address is the same, the ListenerTcpPort should be different, otherwise, return
            if (endPoint.Port == ListenerTcpPort && localAddresses.Contains(endPoint.Address))
            {
                return;
            }

            if (isTrusted)
            {
                TrustedIpAddresses.Add(endPoint.Address);
            }
            // If connections with the peer greater than or equal to MaxConnectionsPerAddress, return.
            if (ConnectedAddresses.TryGetValue(endPoint.Address, out int count) && count >= MaxConnectionsPerAddress)
            {
                return;
            }
            if (ConnectedPeers.Values.Contains(endPoint))
            {
                return;
            }
            ImmutableInterlocked.Update(ref ConnectingPeers, p =>
            {
                if ((p.Count >= ConnectingMax && !isTrusted) || p.Contains(endPoint))
                {
                    return(p);
                }
                tcp_manager.Tell(new Tcp.Connect(endPoint));
                return(p.Add(endPoint));
            });
        }