This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
client_tcp.go
351 lines (310 loc) · 9.75 KB
/
client_tcp.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package modbus
import (
"encoding/binary"
"fmt"
"io"
"net"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/goburrow/serial"
)
const (
// TCPDefaultTimeout TCP Default timeout
TCPDefaultTimeout = 1 * time.Second
// TCPDefaultAutoReconnect TCP Default auto reconnect count
TCPDefaultAutoReconnect = 1
)
// TCPClientProvider implements ClientProvider interface.
type TCPClientProvider struct {
logger
address string
mu sync.Mutex
// TCP connection
conn net.Conn
// Connect & Read timeout
timeout time.Duration
// For synchronization between messages of server & client
transactionID uint32
// request
*pool
}
// check TCPClientProvider implements the interface ClientProvider underlying method
var _ ClientProvider = (*TCPClientProvider)(nil)
// request pool, all TCP client use this pool
var tcpPool = newPool(tcpAduMaxSize)
// NewTCPClientProvider allocates a new TCPClientProvider.
func NewTCPClientProvider(address string, opts ...ClientProviderOption) *TCPClientProvider {
p := &TCPClientProvider{
address: address,
timeout: TCPDefaultTimeout,
pool: tcpPool,
logger: newLogger("modbusTCPMaster =>"),
}
for _, opt := range opts {
opt(p)
}
return p
}
// encode modbus application protocol header & pdu to TCP frame,return adu.
// ---- MBAP header ----
// Transaction identifier: 2 bytes
// Protocol identifier: 2 bytes
// Length: 2 bytes
// Unit identifier: 1 byte
// ---- data Unit ----
// Function code: 1 byte
// Data: n bytes
func (sf *protocolFrame) encodeTCPFrame(tid uint16, slaveID byte,
pdu ProtocolDataUnit) (protocolTCPHeader, []byte, error) {
length := tcpHeaderMbapSize + 1 + len(pdu.Data)
if length > tcpAduMaxSize {
return protocolTCPHeader{}, nil, fmt.Errorf("modbus: length of data '%v' must not be bigger than '%v'",
length, tcpAduMaxSize)
}
head := protocolTCPHeader{
tid,
tcpProtocolIdentifier,
uint16(2 + len(pdu.Data)), // sizeof(SlaveId) + sizeof(FuncCode) + Data
slaveID,
}
// fill adu buffer
adu := sf.adu[0:length]
binary.BigEndian.PutUint16(adu, head.transactionID) // MBAP Transaction identifier
binary.BigEndian.PutUint16(adu[2:], head.protocolID) // MBAP Protocol identifier
binary.BigEndian.PutUint16(adu[4:], head.length) // MBAP Length
adu[6] = head.slaveID // MBAP Unit identifier
adu[tcpHeaderMbapSize] = pdu.FuncCode // PDU funcCode
copy(adu[tcpHeaderMbapSize+1:], pdu.Data) // PDU data
return head, adu, nil
}
// decode extracts tcpHeader & PDU from TCP frame:
// ---- MBAP header ----
// Transaction identifier: 2 bytes
// Protocol identifier: 2 bytes
// Length: 2 bytes
// Unit identifier: 1 byte
// ---- data Unit ----
// Function : 1 byte
// Data : 0 up to 252 bytes
func decodeTCPFrame(adu []byte) (protocolTCPHeader, []byte, error) {
if len(adu) < tcpAduMinSize { // Minimum size (including MBAP, funcCode)
return protocolTCPHeader{}, nil, fmt.Errorf("modbus: response length '%v' does not meet minimum '%v'",
len(adu), tcpAduMinSize)
}
// Read length value in the header
head := protocolTCPHeader{
transactionID: binary.BigEndian.Uint16(adu),
protocolID: binary.BigEndian.Uint16(adu[2:]),
length: binary.BigEndian.Uint16(adu[4:]),
slaveID: adu[6],
}
pduLength := len(adu) - tcpHeaderMbapSize
if pduLength != int(head.length-1) {
return head, nil, fmt.Errorf("modbus: length in response '%v' does not match pdu data length '%v'",
head.length-1, pduLength)
}
// The first byte after header is function code
return head, adu[tcpHeaderMbapSize:], nil
}
// verify confirms valid data
func verifyTCPFrame(reqHead, rspHead protocolTCPHeader, reqPDU, rspPDU ProtocolDataUnit) error {
switch {
case rspHead.transactionID != reqHead.transactionID: // Check transaction ID
return fmt.Errorf("modbus: response transaction id '%v' does not match request '%v'",
rspHead.transactionID, reqHead.transactionID)
case rspHead.protocolID != reqHead.protocolID: // Check protocol ID
return fmt.Errorf("modbus: response protocol id '%v' does not match request '%v'",
rspHead.protocolID, reqHead.protocolID)
case rspHead.slaveID != reqHead.slaveID: // Check slaveID same
return fmt.Errorf("modbus: response unit id '%v' does not match request '%v'",
rspHead.slaveID, reqHead.slaveID)
case rspPDU.FuncCode != reqPDU.FuncCode: // Check correct function code returned (exception)
return responseError(rspPDU)
case rspPDU.Data == nil || len(rspPDU.Data) == 0: // check Empty response
return fmt.Errorf("modbus: response data is empty")
}
return nil
}
// Send the request to tcp and get the response
func (sf *TCPClientProvider) Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error) {
var response ProtocolDataUnit
frame := sf.pool.get()
defer sf.pool.put(frame)
// add transaction id
tid := uint16(atomic.AddUint32(&sf.transactionID, 1))
head, aduRequest, err := frame.encodeTCPFrame(tid, slaveID, request)
if err != nil {
return response, err
}
aduResponse, err := sf.SendRawFrame(aduRequest)
if err != nil {
return response, err
}
rspHead, pdu, err := decodeTCPFrame(aduResponse)
if err != nil {
return response, err
}
response = ProtocolDataUnit{pdu[0], pdu[1:]}
err = verifyTCPFrame(head, rspHead, request, response)
return response, err
}
// SendPdu send pdu request to the remote server
func (sf *TCPClientProvider) SendPdu(slaveID byte, pduRequest []byte) ([]byte, error) {
if len(pduRequest) < pduMinSize || len(pduRequest) > pduMaxSize {
return nil, fmt.Errorf("modbus: rspPdu size '%v' must not be between '%v' and '%v'",
len(pduRequest), pduMinSize, pduMaxSize)
}
frame := sf.pool.get()
defer sf.pool.put(frame)
// add transaction id
tid := uint16(atomic.AddUint32(&sf.transactionID, 1))
request := ProtocolDataUnit{pduRequest[0], pduRequest[1:]}
head, aduRequest, err := frame.encodeTCPFrame(tid, slaveID, request)
if err != nil {
return nil, err
}
aduResponse, err := sf.SendRawFrame(aduRequest)
if err != nil {
return nil, err
}
rspHead, rspPdu, err := decodeTCPFrame(aduResponse)
if err != nil {
return nil, err
}
response := ProtocolDataUnit{rspPdu[0], rspPdu[1:]}
if err = verifyTCPFrame(head, rspHead, request, response); err != nil {
return nil, err
}
// rspPdu pass tcpMBAP head
return rspPdu, nil
}
// SendRawFrame send raw adu request frame
func (sf *TCPClientProvider) SendRawFrame(aduRequest []byte) (aduResponse []byte, err error) {
sf.mu.Lock()
defer sf.mu.Unlock()
if err = sf.connect(); err != nil {
return
}
// Send data
sf.Debugf("sending [% x]", aduRequest)
// Set write and read timeout
var timeout time.Time
if sf.timeout > 0 {
timeout = time.Now().Add(sf.timeout)
}
if err = sf.conn.SetDeadline(timeout); err != nil {
return nil, err
}
if _, err = sf.conn.Write(aduRequest); err != nil {
return
}
// Read header first
var data [tcpAduMaxSize]byte
var cnt int
if sf.timeout > 0 {
timeout = time.Now().Add(sf.timeout)
}
if err = sf.conn.SetDeadline(timeout); err != nil {
return nil, err
}
if cnt, err = io.ReadFull(sf.conn, data[:tcpHeaderMbapSize]); err != nil {
if e, ok := err.(net.Error); (ok && !e.Temporary() && !e.Timeout()) ||
(err != io.EOF && err == io.ErrClosedPipe) ||
strings.Contains(err.Error(), "use of closed network connection") ||
(cnt == 0 && err == io.EOF) {
sf.close()
}
return
}
// Read length, ignore transaction & protocol id (4 bytes)
length := int(binary.BigEndian.Uint16(data[4:]))
switch {
case length <= 0:
_ = sf.flush(data[:])
err = fmt.Errorf("modbus: length in response header '%v' must not be zero", length)
return
case length > (tcpAduMaxSize - (tcpHeaderMbapSize - 1)):
_ = sf.flush(data[:])
err = fmt.Errorf("modbus: length in response header '%v' must not greater than '%v'",
length, tcpAduMaxSize-tcpHeaderMbapSize+1)
return
}
if sf.timeout > 0 {
timeout = time.Now().Add(sf.timeout)
}
if err = sf.conn.SetDeadline(timeout); err != nil {
return nil, err
}
// Skip unit id
length += tcpHeaderMbapSize - 1
if _, err = io.ReadFull(sf.conn, data[tcpHeaderMbapSize:length]); err != nil {
return
}
aduResponse = data[:length]
sf.Debugf("received [% x]", aduResponse)
return aduResponse, nil
}
// Connect establishes a new connection to the address in Address.
// Connect and Close are exported so that multiple requests can be done with one session
func (sf *TCPClientProvider) Connect() error {
sf.mu.Lock()
err := sf.connect()
sf.mu.Unlock()
return err
}
// Caller must hold the mutex before calling this method.
func (sf *TCPClientProvider) connect() error {
if sf.conn == nil {
dialer := &net.Dialer{Timeout: sf.timeout}
conn, err := dialer.Dial("tcp", sf.address)
if err != nil {
return err
}
sf.conn = conn
}
return nil
}
// IsConnected returns a bool signifying whether
// the client is connected or not.
func (sf *TCPClientProvider) IsConnected() bool {
sf.mu.Lock()
b := sf.conn != nil
sf.mu.Unlock()
return b
}
// Caller must hold the mutex before calling this method.
func (sf *TCPClientProvider) close() (err error) {
if sf.conn != nil {
err = sf.conn.Close()
sf.conn = nil
}
return
}
// Close closes current connection.
func (sf *TCPClientProvider) Close() (err error) {
sf.mu.Lock()
err = sf.close()
sf.mu.Unlock()
return
}
func (sf *TCPClientProvider) setSerialConfig(serial.Config) {}
func (sf *TCPClientProvider) setTCPTimeout(t time.Duration) {
sf.timeout = t
}
// flush flushes pending data in the connection,
// returns io.EOF if connection is closed.
func (sf *TCPClientProvider) flush(b []byte) (err error) {
if err = sf.conn.SetReadDeadline(time.Now()); err != nil {
return
}
// timeout setting will be reset when reading
if _, err = sf.conn.Read(b); err != nil {
// Ignore timeout error
if netError, ok := err.(net.Error); ok && netError.Timeout() {
err = nil
}
}
return
}