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)); }); }
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)); }); }
private void OnTcpConnected(IPEndPoint remote, IPEndPoint local) { ImmutableInterlocked.Update(ref ConnectingPeers, p => p.Remove(remote)); if (MaxConnections != -1 && ConnectedPeers.Count >= MaxConnections && !TrustedIpAddresses.Contains(remote.Address)) { Sender.Tell(Tcp.Abort.Instance); return; } ConnectedAddresses.TryGetValue(remote.Address, out int count); if (count >= MaxConnectionsPerAddress) { Sender.Tell(Tcp.Abort.Instance); } else { ConnectedAddresses[remote.Address] = count + 1; IActorRef connection = Context.ActorOf(ProtocolProps(Sender, remote, local), $"connection_{Guid.NewGuid()}"); Context.Watch(connection); Sender.Tell(new Tcp.Register(connection)); ConnectedPeers.TryAdd(connection, remote); } }