github.com/okex/exchain@v1.8.0/libs/tendermint/privval/socket_dialers.go (about)

     1  package privval
     2  
     3  import (
     4  	"net"
     5  	"time"
     6  
     7  	"github.com/pkg/errors"
     8  
     9  	"github.com/okex/exchain/libs/tendermint/crypto"
    10  	tmnet "github.com/okex/exchain/libs/tendermint/libs/net"
    11  	p2pconn "github.com/okex/exchain/libs/tendermint/p2p/conn"
    12  )
    13  
    14  // Socket errors.
    15  var (
    16  	ErrDialRetryMax = errors.New("dialed maximum retries")
    17  )
    18  
    19  // SocketDialer dials a remote address and returns a net.Conn or an error.
    20  type SocketDialer func() (net.Conn, error)
    21  
    22  // DialTCPFn dials the given tcp addr, using the given timeoutReadWrite and
    23  // privKey for the authenticated encryption handshake.
    24  func DialTCPFn(addr string, timeoutReadWrite time.Duration, privKey crypto.PrivKey) SocketDialer {
    25  	return func() (net.Conn, error) {
    26  		conn, err := tmnet.Connect(addr)
    27  		if err == nil {
    28  			deadline := time.Now().Add(timeoutReadWrite)
    29  			err = conn.SetDeadline(deadline)
    30  		}
    31  		if err == nil {
    32  			conn, err = p2pconn.MakeSecretConnection(conn, privKey)
    33  		}
    34  		return conn, err
    35  	}
    36  }
    37  
    38  // DialUnixFn dials the given unix socket.
    39  func DialUnixFn(addr string) SocketDialer {
    40  	return func() (net.Conn, error) {
    41  		unixAddr := &net.UnixAddr{Name: addr, Net: "unix"}
    42  		return net.DialUnix("unix", nil, unixAddr)
    43  	}
    44  }