server/client/socket_builders.go

92 lines
1.8 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 {
2017-12-05 07:21:32 +00:00
SocketHandler SocketHandler
Network string
Address string
TLSConfig *tls.Config
2017-11-30 09:13:58 +00:00
2017-12-01 03:42:03 +00:00
HandshakeTimeout time.Duration
KeepAlive time.Duration
2017-11-30 09:13:58 +00:00
LocalAddress net.Addr
}
func (sb *SocketBuilders) SocketContext(parent cuc.Context) SocketContext {
return newSocketContext(parent)
}
2017-12-05 07:21:32 +00:00
func (sb *SocketBuilders) GetSocketHandler() SocketHandler {
return sb.SocketHandler
2017-11-30 09:13:58 +00:00
}
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-12-01 03:42:03 +00:00
func (sb *SocketBuilders) Dial(network, address string) (net.Conn, error) {
var deadline time.Time
if 0 != sb.HandshakeTimeout {
deadline = time.Now().Add(sb.HandshakeTimeout)
2017-11-30 09:57:09 +00:00
}
2017-12-01 03:42:03 +00:00
d := &net.Dialer{
KeepAlive: sb.KeepAlive,
Deadline: deadline,
LocalAddr: sb.LocalAddress,
}
return d.Dial(network, address)
2017-11-30 09:57:09 +00:00
}
2017-11-30 09:13:58 +00:00
func (sb *SocketBuilders) GetKeepAlive() time.Duration {
return sb.KeepAlive
}
2017-12-01 03:42:03 +00:00
func (sb *SocketBuilders) GetHandshakeTimeout() time.Duration {
return sb.HandshakeTimeout
2017-11-30 09:13:58 +00:00
}
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() {
2017-12-05 07:21:32 +00:00
if nil == sb.SocketHandler {
logging.Logger().Panic("Client Socket: SocketHandler must be specified")
}
2017-11-30 09:13:58 +00:00
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
}
2017-12-01 03:42:03 +00:00
if 0 >= sb.HandshakeTimeout {
sb.HandshakeTimeout = cs.DefaultHandshakeTimeout
2017-11-30 09:13:58 +00:00
}
}