-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
284 lines (247 loc) · 8.9 KB
/
client.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package modbus
import (
"context"
"errors"
"github.com/aldas/go-modbus-client/packet"
"io"
"net"
"os"
"strings"
"sync"
"time"
)
const (
// tcpPacketMaxLen is maximum length in bytes that valid Modbus TCP packet can be
//
// Quote from MODBUS Application Protocol Specification V1.1b3:
// The size of the MODBUS PDU is limited by the size constraint inherited from the first
// MODBUS implementation on Serial Line network (max. RS485 ADU = 256 bytes).
// Therefore:
// MODBUS PDU for serial line communication = 256 - Server address (1 byte) - CRC (2bytes) = 253 bytes.
// Consequently:
// RS232 / RS485 ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256 bytes.
// TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes.
tcpPacketMaxLen = 7 + 253 // 2 trans id + 2 proto + 2 pdu len + 1 unit id + 253 max data len
rtuPacketMaxLen = 256 // 1 unit id + 253 max data len + 2 crc
defaultWriteTimeout = 1 * time.Second
defaultReadTimeout = 2 * time.Second
defaultConnectTimeout = 1 * time.Second
)
// ErrPacketTooLong is error indicating that modbus server sent amount of data that is bigger than any modbus packet could be
var ErrPacketTooLong = ClientError{Err: errors.New("received more bytes than valid Modbus packet size can be")}
// ErrClientNotConnected is error indicating that Client has not yet connected to the modbus server
var ErrClientNotConnected = ClientError{Err: errors.New("client is not connected")}
// Client provides mechanisms to send requests to modbus server over network connection
type Client struct {
timeNow func() time.Time
// writeTimeout is total amount of time writing the request can take after client returns error
writeTimeout time.Duration
// readTimeout is total amount of time reading the response can take before client returns error
readTimeout time.Duration
dialContextFunc func(ctx context.Context, address string) (net.Conn, error)
asProtocolErrorFunc func(data []byte) error
parseResponseFunc func(data []byte) (packet.Response, error)
mu sync.RWMutex
address string
conn net.Conn
hooks ClientHooks
}
// ClientHooks allows to log bytes send/received by client.
// NB: Do not modify given slice - it is not a copy.
type ClientHooks interface {
BeforeWrite(toWrite []byte)
AfterEachRead(received []byte, n int, err error)
BeforeParse(received []byte)
}
// ClientConfig is configuration for Client
type ClientConfig struct {
// WriteTimeout is total amount of time writing the request can take after client returns error
WriteTimeout time.Duration
// ReadTimeout is total amount of time reading the response can take before client returns error
ReadTimeout time.Duration
DialContextFunc func(ctx context.Context, address string) (net.Conn, error)
AsProtocolErrorFunc func(data []byte) error
ParseResponseFunc func(data []byte) (packet.Response, error)
Hooks ClientHooks
}
func defaultClient(conf ClientConfig) *Client {
c := &Client{
timeNow: time.Now,
writeTimeout: defaultWriteTimeout,
readTimeout: defaultReadTimeout,
dialContextFunc: dialContext,
// TCP is our default protocol
asProtocolErrorFunc: packet.AsTCPErrorPacket,
parseResponseFunc: packet.ParseTCPResponse,
}
if conf.WriteTimeout > 0 {
c.writeTimeout = conf.WriteTimeout
}
if conf.ReadTimeout > 0 {
c.readTimeout = conf.ReadTimeout
}
if conf.DialContextFunc != nil {
c.dialContextFunc = conf.DialContextFunc
}
if conf.AsProtocolErrorFunc != nil {
c.asProtocolErrorFunc = conf.AsProtocolErrorFunc
}
if conf.ParseResponseFunc != nil {
c.parseResponseFunc = conf.ParseResponseFunc
}
if conf.Hooks != nil {
c.hooks = conf.Hooks
}
return c
}
// NewTCPClient creates new instance of Modbus Client for Modbus TCP protocol
func NewTCPClient() *Client {
return NewTCPClientWithConfig(ClientConfig{})
}
// NewTCPClientWithConfig creates new instance of Modbus Client for Modbus TCP protocol with given configuration options
func NewTCPClientWithConfig(conf ClientConfig) *Client {
client := defaultClient(conf)
client.asProtocolErrorFunc = packet.AsTCPErrorPacket
client.parseResponseFunc = packet.ParseTCPResponse
return client
}
// NewRTUClient creates new instance of Modbus Client for Modbus RTU protocol
func NewRTUClient() *Client {
return NewRTUClientWithConfig(ClientConfig{})
}
// NewRTUClientWithConfig creates new instance of Modbus Client for Modbus RTU protocol with given configuration options
func NewRTUClientWithConfig(conf ClientConfig) *Client {
client := defaultClient(conf)
client.asProtocolErrorFunc = packet.AsRTUErrorPacket
client.parseResponseFunc = packet.ParseRTUResponseWithCRC
return client
}
// NewClient creates new instance of Modbus Client with given configuration options
func NewClient(conf ClientConfig) *Client {
return defaultClient(conf)
}
// Connect opens network connection to Client to server. Context lifetime is only meant for this call.
// ctx is to be used for to cancel connection attempt.
func (c *Client) Connect(ctx context.Context, address string) error {
c.mu.Lock()
defer c.mu.Unlock()
conn, err := c.dialContextFunc(ctx, address)
if err != nil {
return err
}
c.conn = conn
c.address = address
return nil
}
func dialContext(ctx context.Context, address string) (net.Conn, error) {
dialer := &net.Dialer{
// Timeout is the maximum amount of time a dial will wait for a connect to complete.
Timeout: defaultConnectTimeout,
// KeepAlive specifies the interval between keep-alive probes for an active network connection.
KeepAlive: 15 * time.Second,
}
network, addr := addressExtractor(address)
return dialer.DialContext(ctx, network, addr)
}
func addressExtractor(address string) (string, string) {
network, addr, ok := strings.Cut(address, "://")
if !ok {
return "tcp", address
}
return network, addr
}
// Close closes network connection to Modbus server
func (c *Client) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.conn == nil {
return nil
}
return c.conn.Close()
}
// ClientError indicates errors returned by Client that network related and are possibly retryable
type ClientError struct {
Err error
}
// Error returns contained error message
func (e *ClientError) Error() string { return e.Err.Error() }
// Unwrap allows unwrapping errors with errors.Is and errors.As
func (e *ClientError) Unwrap() error { return e.Err }
// Do sends given Modbus request to modbus server and returns parsed Response.
// ctx is to be used for to cancel connection attempt.
// On modbus exception nil is returned as response and error wraps value of type packet.ErrorResponseTCP or packet.ErrorResponseRTU
// User errors.Is and errors.As to check if error wraps packet.ErrorResponseTCP or packet.ErrorResponseRTU
func (c *Client) Do(ctx context.Context, req packet.Request) (packet.Response, error) {
c.mu.Lock()
defer c.mu.Unlock()
if req == nil {
return nil, errors.New("request can not be nil")
}
if c.conn == nil {
return nil, &ErrClientNotConnected
}
resp, err := c.do(ctx, req.Bytes(), req.ExpectedResponseLength())
if err != nil {
return nil, err
}
if c.hooks != nil {
c.hooks.BeforeParse(resp)
}
return c.parseResponseFunc(resp)
}
func (c *Client) do(ctx context.Context, data []byte, expectedLen int) ([]byte, error) {
if err := c.conn.SetWriteDeadline(c.timeNow().Add(c.writeTimeout)); err != nil {
return nil, err
}
if c.hooks != nil {
c.hooks.BeforeWrite(data)
}
if _, err := c.conn.Write(data); err != nil {
return nil, &ClientError{Err: err}
}
// make buffer a little bit bigger than would be valid to see problems when somehow more bytes are sent
const maxBytes = tcpPacketMaxLen + 10
received := [maxBytes]byte{}
total := 0
readTimeout := time.After(c.readTimeout)
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-readTimeout:
return nil, &ClientError{Err: errors.New("total read timeout exceeded")}
default:
}
_ = c.conn.SetReadDeadline(c.timeNow().Add(500 * time.Microsecond)) // max 0.5ms block time for read per iteration
n, err := c.conn.Read(received[total:maxBytes])
if c.hooks != nil {
c.hooks.AfterEachRead(received[total:total+n], n, err)
}
// on read errors we do not return immediately as for:
// os.ErrDeadlineExceeded - we set new deadline on next iteration
// io.EOF - we check if read + received is enough to form complete packet
if err != nil && !(errors.Is(err, os.ErrDeadlineExceeded) || errors.Is(err, io.EOF)) {
return nil, &ClientError{Err: err}
}
total += n
if total > tcpPacketMaxLen {
return nil, &ErrPacketTooLong
}
// check if we have exactly the error packet. Error packets are shorter than regulars packets
if errPacket := c.asProtocolErrorFunc(received[0:total]); errPacket != nil {
return nil, &ClientError{Err: errPacket}
}
if total >= expectedLen {
break
}
if errors.Is(err, io.EOF) {
break
}
}
if total == 0 {
return nil, &ClientError{Err: errors.New("no bytes received")}
}
result := make([]byte, total)
copy(result, received[:total])
return result, nil
}