forked from thinkgos/go-iecp5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientOption.go
95 lines (84 loc) · 2.38 KB
/
clientOption.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2020 iotopo (thinkgo@aliyun.com). All rights reserved.
// Use of this source code is governed by a version 3 of the GNU General
// Public License, license that can be found in the LICENSE file.
package cs104
import (
"crypto/tls"
"net/url"
"strings"
"time"
"github.com/iotopo/go-iecp5/asdu"
)
// ClientOption 客户端配置
type ClientOption struct {
config Config
params asdu.Params
server *url.URL // 连接的服务器端
autoReconnect bool // 是否启动重连
reconnectInterval time.Duration // 重连间隔时间
TLSConfig *tls.Config // tls配置
}
// NewOption with default config and default asdu.ParamsWide params
func NewOption() *ClientOption {
return &ClientOption{
DefaultConfig(),
*asdu.ParamsWide,
nil,
true,
DefaultReconnectInterval,
nil,
}
}
// SetConfig set config if config is valid it will use DefaultConfig()
func (sf *ClientOption) SetConfig(cfg Config) *ClientOption {
if err := cfg.Valid(); err != nil {
sf.config = DefaultConfig()
} else {
sf.config = cfg
}
return sf
}
// SetParams set asdu params if params is valid it will use asdu.ParamsWide
func (sf *ClientOption) SetParams(p *asdu.Params) *ClientOption {
if err := p.Valid(); err != nil {
sf.params = *asdu.ParamsWide
} else {
sf.params = *p
}
return sf
}
// SetReconnectInterval set tcp reconnect the host interval when connect failed after try
func (sf *ClientOption) SetReconnectInterval(t time.Duration) *ClientOption {
if t > 0 {
sf.reconnectInterval = t
}
return sf
}
// SetAutoReconnect enable auto reconnect
func (sf *ClientOption) SetAutoReconnect(b bool) *ClientOption {
sf.autoReconnect = b
return sf
}
// SetTLSConfig set tls config
func (sf *ClientOption) SetTLSConfig(t *tls.Config) *ClientOption {
sf.TLSConfig = t
return sf
}
// AddRemoteServer adds a broker URI to the list of brokers to be used.
// The format should be scheme://host:port
// Default values for hostname is "127.0.0.1", for schema is "tcp://".
// An example broker URI would look like: tcp://foobar.com:1204
func (sf *ClientOption) AddRemoteServer(server string) error {
if len(server) > 0 && server[0] == ':' {
server = "127.0.0.1" + server
}
if !strings.Contains(server, "://") {
server = "tcp://" + server
}
remoteURL, err := url.Parse(server)
if err != nil {
return err
}
sf.server = remoteURL
return nil
}