Skip to content

Commit

Permalink
use helper to init dialer
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Nov 11, 2021
1 parent ec3d407 commit 1fdba80
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
48 changes: 18 additions & 30 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ func RequestTimeout(t time.Duration) Option {
// Dialer sets the uacp.Dialer to establish the connection to the server.
func Dialer(d *uacp.Dialer) Option {
return func(cfg *Config) {
initDialer(cfg)
cfg.dialer = d
}
}
Expand All @@ -474,64 +475,51 @@ func Dialer(d *uacp.Dialer) Option {
// Defaults to DefaultDialTimeout. Set to zero for no timeout.
func DialTimeout(d time.Duration) Option {
return func(cfg *Config) {
if cfg.dialer == nil {
cfg.dialer = &uacp.Dialer{}
}
if cfg.dialer.Dialer == nil {
cfg.dialer.Dialer = &net.Dialer{}
}
initDialer(cfg)
cfg.dialer.Dialer.Timeout = d
}
}

// MaxMessageSize sets the maximum message size for the UACP handshake.
func MaxMessageSize(n uint32) Option {
return func(cfg *Config) {
if cfg.dialer == nil {
cfg.dialer = &uacp.Dialer{}
}
if cfg.dialer.ClientACK == nil {
cfg.dialer.ClientACK = uacp.DefaultClientACK
}
initDialer(cfg)
cfg.dialer.ClientACK.MaxMessageSize = n
}
}

// MaxChunkCount sets the maximum chunk count for the UACP handshake.
func MaxChunkCount(n uint32) Option {
return func(cfg *Config) {
if cfg.dialer == nil {
cfg.dialer = &uacp.Dialer{}
}
if cfg.dialer.ClientACK == nil {
cfg.dialer.ClientACK = uacp.DefaultClientACK
}
initDialer(cfg)
cfg.dialer.ClientACK.MaxChunkCount = n
}
}

// ReceiveBufferSize sets the receive buffer size for the UACP handshake.
func ReceiveBufferSize(n uint32) Option {
return func(cfg *Config) {
if cfg.dialer == nil {
cfg.dialer = &uacp.Dialer{}
}
if cfg.dialer.ClientACK == nil {
cfg.dialer.ClientACK = uacp.DefaultClientACK
}
initDialer(cfg)
cfg.dialer.ClientACK.ReceiveBufSize = n
}
}

// SendBufferSize sets the send buffer size for the UACP handshake.
func SendBufferSize(n uint32) Option {
return func(cfg *Config) {
if cfg.dialer == nil {
cfg.dialer = &uacp.Dialer{}
}
if cfg.dialer.ClientACK == nil {
cfg.dialer.ClientACK = uacp.DefaultClientACK
}
initDialer(cfg)
cfg.dialer.ClientACK.SendBufSize = n
}
}

func initDialer(cfg *Config) {
if cfg.dialer == nil {
cfg.dialer = &uacp.Dialer{}
}
if cfg.dialer.Dialer == nil {
cfg.dialer.Dialer = &net.Dialer{}
}
if cfg.dialer.ClientACK == nil {
cfg.dialer.ClientACK = uacp.DefaultClientACK
}
}
45 changes: 36 additions & 9 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,19 +666,34 @@ func TestOptions(t *testing.T) {
},
{
name: `Dialer()`,
opt: Dialer(&uacp.Dialer{}),
opt: Dialer(&uacp.Dialer{
Dialer: &net.Dialer{Timeout: 3 * time.Second},
ClientACK: &uacp.Acknowledge{
MaxMessageSize: 1,
MaxChunkCount: 2,
SendBufSize: 3,
ReceiveBufSize: 4,
},
}),
cfg: &Config{
dialer: &uacp.Dialer{},
dialer: &uacp.Dialer{
Dialer: &net.Dialer{Timeout: 3 * time.Second},
ClientACK: &uacp.Acknowledge{
MaxMessageSize: 1,
MaxChunkCount: 2,
SendBufSize: 3,
ReceiveBufSize: 4,
},
},
},
},
{
name: `DialTimeout(5s)`,
opt: DialTimeout(5 * time.Second),
cfg: &Config{
dialer: &uacp.Dialer{
Dialer: &net.Dialer{
Timeout: 5 * time.Second,
},
Dialer: &net.Dialer{Timeout: 5 * time.Second},
ClientACK: uacp.DefaultClientACK,
},
},
},
Expand All @@ -687,7 +702,10 @@ func TestOptions(t *testing.T) {
opt: MaxMessageSize(5),
cfg: &Config{
dialer: func() *uacp.Dialer {
d := &uacp.Dialer{ClientACK: uacp.DefaultClientACK}
d := &uacp.Dialer{
Dialer: &net.Dialer{},
ClientACK: uacp.DefaultClientACK,
}
d.ClientACK.MaxMessageSize = 5
return d
}(),
Expand All @@ -698,7 +716,10 @@ func TestOptions(t *testing.T) {
opt: MaxChunkCount(5),
cfg: &Config{
dialer: func() *uacp.Dialer {
d := &uacp.Dialer{ClientACK: uacp.DefaultClientACK}
d := &uacp.Dialer{
Dialer: &net.Dialer{},
ClientACK: uacp.DefaultClientACK,
}
d.ClientACK.MaxChunkCount = 5
return d
}(),
Expand All @@ -709,7 +730,10 @@ func TestOptions(t *testing.T) {
opt: ReceiveBufferSize(5),
cfg: &Config{
dialer: func() *uacp.Dialer {
d := &uacp.Dialer{ClientACK: uacp.DefaultClientACK}
d := &uacp.Dialer{
Dialer: &net.Dialer{},
ClientACK: uacp.DefaultClientACK,
}
d.ClientACK.ReceiveBufSize = 5
return d
}(),
Expand All @@ -720,7 +744,10 @@ func TestOptions(t *testing.T) {
opt: SendBufferSize(5),
cfg: &Config{
dialer: func() *uacp.Dialer {
d := &uacp.Dialer{ClientACK: uacp.DefaultClientACK}
d := &uacp.Dialer{
Dialer: &net.Dialer{},
ClientACK: uacp.DefaultClientACK,
}
d.ClientACK.SendBufSize = 5
return d
}(),
Expand Down

0 comments on commit 1fdba80

Please sign in to comment.