mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-07 04:20:44 +00:00
* libs/common: Refactor libs/common 5 - move mathematical functions and types out of `libs/common` to math pkg - move net functions out of `libs/common` to net pkg - move string functions out of `libs/common` to strings pkg - move async functions out of `libs/common` to async pkg - move bit functions out of `libs/common` to bits pkg - move cmap functions out of `libs/common` to cmap pkg - move os functions out of `libs/common` to os pkg Signed-off-by: Marko Baricevic <marbar3778@yahoo.com> * fix testing issues * fix tests closes #41417 woooooooooooooooooo kill the cmn pkg Signed-off-by: Marko Baricevic <marbar3778@yahoo.com> * add changelog entry * fix goimport issues * run gofmt
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package privval
|
|
|
|
import (
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/tendermint/tendermint/crypto"
|
|
tmnet "github.com/tendermint/tendermint/libs/net"
|
|
p2pconn "github.com/tendermint/tendermint/p2p/conn"
|
|
)
|
|
|
|
// 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 = p2pconn.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)
|
|
}
|
|
}
|