github.com/DFWallet/tendermint-cosmos@v0.0.2/privval/socket_dialers.go (about)

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