Files
tendermint/privval/socket_dialers.go
Erik Grinaker 2d0fcf498d privval: duplicate SecretConnection from p2p package (#5672)
This is so that the `privval` package will not be affected when we refactor `p2p` (#5670). We will be migrating to gRPC shortly (#4698).
2020-11-16 18:16:10 +00:00

43 lines
1.1 KiB
Go

package privval
import (
"errors"
"net"
"time"
"github.com/tendermint/tendermint/crypto"
tmnet "github.com/tendermint/tendermint/libs/net"
)
// Socket errors.
var (
ErrDialRetryMax = errors.New("dialed maximum retries")
)
// SocketDialer dials a remote address and returns a net.Conn or an error.
type SocketDialer func() (net.Conn, error)
// DialTCPFn dials the given tcp addr, using the given timeoutReadWrite and
// privKey for the authenticated encryption handshake.
func DialTCPFn(addr string, timeoutReadWrite time.Duration, privKey crypto.PrivKey) SocketDialer {
return func() (net.Conn, error) {
conn, err := tmnet.Connect(addr)
if err == nil {
deadline := time.Now().Add(timeoutReadWrite)
err = conn.SetDeadline(deadline)
}
if err == nil {
conn, err = MakeSecretConnection(conn, privKey)
}
return conn, err
}
}
// DialUnixFn dials the given unix socket.
func DialUnixFn(addr string) SocketDialer {
return func() (net.Conn, error) {
unixAddr := &net.UnixAddr{Name: addr, Net: "unix"}
return net.DialUnix("unix", nil, unixAddr)
}
}