pub fn connect(&self, addr: &SocketAddr) -> Result<Arc<Peer>, Error> {
		if Peer::is_denied(&self.config, &addr) {
			debug!(
				LOGGER,
				"connect_peer: peer {} denied, not connecting.", addr
			);
			return Err(Error::ConnectionClose);
		}

		// check ip and port to see if we are trying to connect to ourselves
		// todo: this can't detect all cases of PeerWithSelf, for example config.host is '0.0.0.0'
		//
		if self.config.port == addr.port()
			&& (addr.ip().is_loopback() || addr.ip() == self.config.host)
		{
			return Err(Error::PeerWithSelf);
		}

		if let Some(p) = self.peers.get_connected_peer(addr) {
			// if we're already connected to the addr, just return the peer
			trace!(LOGGER, "connect_peer: already connected {}", addr);
			return Ok(p);
		}

		trace!(
			LOGGER,
			"connect_peer: on {}:{}. connecting to {}",
			self.config.host,
			self.config.port,
			addr
		);
		match TcpStream::connect_timeout(addr, Duration::from_secs(10)) {
			Ok(mut stream) => {
				let addr = SocketAddr::new(self.config.host, self.config.port);
				let total_diff = self.peers.total_difficulty();

				let mut peer = Peer::connect(
					&mut stream,
					self.capabilities,
					total_diff,
					addr,
					&self.handshake,
					self.peers.clone(),
				)?;
				peer.start(stream);
				let peer = Arc::new(peer);
				self.peers.add_connected(peer.clone())?;
				Ok(peer)
			}
			Err(e) => {
				debug!(
					LOGGER,
					"connect_peer: on {}:{}. Could not connect to {}: {:?}",
					self.config.host,
					self.config.port,
					addr,
					e
				);
				Err(Error::Connection(e))
			}
		}
	}
/// Asks the server to connect to a new peer. Directly returns the peer if
/// we're already connected to the provided address.
Asks the server to connect to a new peer. Directly returns the peer if
we're already connected to the provided address.
Asks the server to connect to a new peer . Directly returns the peer if we ' re already connected to the provided address .
pub fn connect(&self, addr: &SocketAddr) -> Result<Arc<Peer>, Error> {
		if Peer::is_denied(&self.config, &addr) {
			debug!(
				LOGGER,
				"connect_peer: peer {} denied, not connecting.", addr
			);
			return Err(Error::ConnectionClose);
		}
		// check ip and port to see if we are trying to connect to ourselves
		// todo: this can't detect all cases of PeerWithSelf, for example config.host is '0.0.0.0'
		//
		if self.config.port == addr.port()
			&& (addr.ip().is_loopback() || addr.ip() == self.config.host)
		{
			return Err(Error::PeerWithSelf);
		}
		if let Some(p) = self.peers.get_connected_peer(addr) {
			// if we're already connected to the addr, just return the peer
			trace!(LOGGER, "connect_peer: already connected {}", addr);
			return Ok(p);
		}
		trace!(
			LOGGER,
			"connect_peer: on {}:{}. connecting to {}",
			self.config.host,
			self.config.port,
			addr
		);
		match TcpStream::connect_timeout(addr, Duration::from_secs(10)) {
			Ok(mut stream) => {
				let addr = SocketAddr::new(self.config.host, self.config.port);
				let total_diff = self.peers.total_difficulty();
				let mut peer = Peer::connect(
					&mut stream,
					self.capabilities,
					total_diff,
					addr,
					&self.handshake,
					self.peers.clone(),
				)?;
				peer.start(stream);
				let peer = Arc::new(peer);
				self.peers.add_connected(peer.clone())?;
				Ok(peer)
			}
			Err(e) => {
				debug!(
					LOGGER,
					"connect_peer: on {}:{}. Could not connect to {}: {:?}",
					self.config.host,
					self.config.port,
					addr,
					e
				);
				Err(Error::Connection(e))
			}
		}
	}
pub fn connect ( & self , addr : & SocketAddr ) -> Result < Arc < Peer > , Error > { if Peer :: is_denied ( & self . config , & addr ) { debug ! ( LOGGER " " addr ) ; return Err ( Error :: ConnectionClose ) ; } // check ip and port to see if we are trying to connect to ourselves // todo: this can't detect all cases of PeerWithSelf, for example config.host is '0.0.0.0' // if self . config . port == addr . port ( ) && ( addr . ip ( ) . is_loopback ( ) || addr . ip ( ) == self . config . host ) { return Err ( Error :: PeerWithSelf ) ; } if let Some ( p ) = self . peers . get_connected_peer ( addr ) { // if we're already connected to the addr, just return the peer trace ! ( LOGGER " " addr ) ; return Ok ( p ) ; } trace ! ( LOGGER " " self config host self config port addr ) ; match TcpStream :: connect_timeout ( addr , Duration :: from_secs ( 10 ) ) { Ok ( mut stream ) => { let addr = SocketAddr :: new ( self . config . host , self . config . port ) ; let total_diff = self . peers . total_difficulty ( ) ; let mut peer = Peer :: connect ( & mut stream , self . capabilities , total_diff , addr , & self . handshake , self . peers . clone ( ) , ) ? ; peer . start ( stream ) ; let peer = Arc :: new ( peer ) ; self . peers . add_connected ( peer . clone ( ) ) ? ; Ok ( peer ) } Err ( e ) => { debug ! ( LOGGER " " self config host self config port addr e ) ; Err ( Error :: Connection ( e ) ) } } }
Asks the server to connect to a new peer.
Asks the server to connect to a new peer .
&self
addr
&SocketAddr