92 lines
1.9 KiB
Go
92 lines
1.9 KiB
Go
package client
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net"
|
|
"time"
|
|
|
|
"git.loafle.net/commons_go/logging"
|
|
cs "git.loafle.net/commons_go/server"
|
|
cuc "git.loafle.net/commons_go/util/context"
|
|
)
|
|
|
|
type SocketBuilders struct {
|
|
SocketHandler SocketHandler
|
|
Network string
|
|
Address string
|
|
TLSConfig *tls.Config
|
|
|
|
HandshakeTimeout time.Duration
|
|
KeepAlive time.Duration
|
|
|
|
LocalAddress net.Addr
|
|
}
|
|
|
|
func (sb *SocketBuilders) SocketContext(parent cuc.Context) SocketContext {
|
|
return newSocketContext(parent)
|
|
}
|
|
|
|
func (sb *SocketBuilders) GetSocketHandler() SocketHandler {
|
|
return sb.SocketHandler
|
|
}
|
|
|
|
func (sb *SocketBuilders) GetNetwork() string {
|
|
return sb.Network
|
|
}
|
|
|
|
func (sb *SocketBuilders) GetAddress() string {
|
|
return sb.Address
|
|
}
|
|
|
|
func (sb *SocketBuilders) GetTLSConfig() *tls.Config {
|
|
return sb.TLSConfig
|
|
}
|
|
|
|
func (sb *SocketBuilders) Dial(network, address string) (net.Conn, error) {
|
|
var deadline time.Time
|
|
if 0 != sb.HandshakeTimeout {
|
|
deadline = time.Now().Add(sb.HandshakeTimeout)
|
|
}
|
|
|
|
d := &net.Dialer{
|
|
KeepAlive: sb.KeepAlive,
|
|
Deadline: deadline,
|
|
LocalAddr: sb.LocalAddress,
|
|
}
|
|
|
|
return d.Dial(network, address)
|
|
}
|
|
|
|
func (sb *SocketBuilders) GetKeepAlive() time.Duration {
|
|
return sb.KeepAlive
|
|
}
|
|
|
|
func (sb *SocketBuilders) GetHandshakeTimeout() time.Duration {
|
|
return sb.HandshakeTimeout
|
|
}
|
|
|
|
func (sb *SocketBuilders) GetLocalAddress() net.Addr {
|
|
return sb.LocalAddress
|
|
}
|
|
|
|
func (sb *SocketBuilders) Validate() {
|
|
if nil == sb.SocketHandler {
|
|
logging.Logger().Panicf("Client Socket: SocketHandler must be specified")
|
|
}
|
|
|
|
if "" == sb.Network {
|
|
logging.Logger().Panicf("Client Socket: Network must be specified")
|
|
}
|
|
if "" == sb.Address {
|
|
logging.Logger().Panicf("Client Socket: Address must be specified")
|
|
}
|
|
|
|
if 0 >= sb.KeepAlive {
|
|
sb.KeepAlive = cs.DefaultKeepAlive
|
|
}
|
|
if 0 >= sb.HandshakeTimeout {
|
|
sb.HandshakeTimeout = cs.DefaultHandshakeTimeout
|
|
}
|
|
|
|
}
|