server/client/socket_builders.go

80 lines
1.6 KiB
Go
Raw Normal View History

2017-11-30 09:13:58 +00:00
package client
import (
"crypto/tls"
"net"
"time"
"git.loafle.net/commons_go/logging"
cs "git.loafle.net/commons_go/server"
2017-11-30 09:36:59 +00:00
cuc "git.loafle.net/commons_go/util/context"
2017-11-30 09:13:58 +00:00
)
type SocketBuilders struct {
Network string
Address string
TLSConfig *tls.Config
KeepAlive time.Duration
Timeout time.Duration
LocalAddress net.Addr
}
func (sb *SocketBuilders) SocketContext(parent cuc.Context) SocketContext {
return newSocketContext(parent)
}
func (sb *SocketBuilders) SocketHandler() SocketHandler {
return NewSocketHandler()
}
func (sb *SocketBuilders) GetNetwork() string {
2017-11-30 09:36:59 +00:00
return sb.Network
2017-11-30 09:13:58 +00:00
}
func (sb *SocketBuilders) GetAddress() string {
return sb.Address
}
func (sb *SocketBuilders) GetTLSConfig() *tls.Config {
return sb.TLSConfig
}
2017-11-30 09:57:09 +00:00
func (sb *SocketBuilders) Dial(dialer *net.Dialer, network, address string) (net.Conn, error) {
if nil == sb.TLSConfig {
return dialer.Dial(network, address)
}
return tls.DialWithDialer(dialer, network, address, sb.TLSConfig)
}
2017-11-30 09:13:58 +00:00
func (sb *SocketBuilders) GetKeepAlive() time.Duration {
return sb.KeepAlive
}
func (sb *SocketBuilders) GetTimeout() time.Duration {
return sb.Timeout
}
2017-11-30 09:36:59 +00:00
func (sb *SocketBuilders) GetLocalAddress() net.Addr {
2017-11-30 09:13:58 +00:00
return sb.LocalAddress
}
func (sb *SocketBuilders) Validate() {
if "" == sb.Network {
logging.Logger().Panic("Client Socket: Network must be specified")
}
if "" == sb.Address {
logging.Logger().Panic("Client Socket: Address must be specified")
}
if 0 >= sb.KeepAlive {
sb.KeepAlive = cs.DefaultKeepAlive
}
if 0 >= sb.Timeout {
sb.Timeout = cs.DefaultConnectTimeout
}
}