github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/privval/socket_dialers.go (about)

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