forked from thinkgos/go-iecp5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
618 lines (539 loc) · 16 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// Copyright 2020 thinkgos (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 (
"context"
"errors"
"io"
"math/rand"
"net"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/jiguorui/go-iecp5/asdu"
"github.com/jiguorui/go-iecp5/clog"
)
const (
inactive = iota
active
)
// Client is an IEC104 master
type Client struct {
option ClientOption
conn net.Conn
handler ClientHandlerInterface
// channel
rcvASDU chan []byte // for received asdu
sendASDU chan []byte // for send asdu
rcvRaw chan []byte // for recvLoop raw cs104 frame
sendRaw chan []byte // for sendLoop raw cs104 frame
// I帧的发送与接收序号
seqNoSend uint16 // sequence number of next outbound I-frame
ackNoSend uint16 // outbound sequence number yet to be confirmed
seqNoRcv uint16 // sequence number of next inbound I-frame
ackNoRcv uint16 // inbound sequence number yet to be confirmed
// maps sendTime I-frames to their respective sequence number
pending []seqPending
startDtActiveSendSince atomic.Value // 当发送startDtActive时,等待确认回复的超时间隔
stopDtActiveSendSince atomic.Value // 当发起stopDtActive时,等待确认回复的超时
// 连接状态
status uint32
rwMux sync.RWMutex
isActive uint32
// 其他
clog.Clog
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
closeCancel context.CancelFunc
onConnect func(c *Client)
onConnectionLost func(c *Client)
}
// NewClient returns an IEC104 master,default config and default asdu.ParamsWide params
func NewClient(handler ClientHandlerInterface, o *ClientOption) *Client {
return &Client{
option: *o,
handler: handler,
rcvASDU: make(chan []byte, o.config.RecvUnAckLimitW<<4),
sendASDU: make(chan []byte, o.config.SendUnAckLimitK<<4),
rcvRaw: make(chan []byte, o.config.RecvUnAckLimitW<<5),
sendRaw: make(chan []byte, o.config.SendUnAckLimitK<<5), // may not block!
Clog: clog.NewLogger("cs104 client => "),
onConnect: func(*Client) {},
onConnectionLost: func(*Client) {},
}
}
// SetOnConnectHandler set on connect handler
func (sf *Client) SetOnConnectHandler(f func(c *Client)) *Client {
if f != nil {
sf.onConnect = f
}
return sf
}
// SetConnectionLostHandler set connection lost handler
func (sf *Client) SetConnectionLostHandler(f func(c *Client)) *Client {
if f != nil {
sf.onConnectionLost = f
}
return sf
}
// Start start the server,and return quickly,if it nil,the server will disconnected background,other failed
func (sf *Client) Start() error {
if sf.option.server == nil {
return errors.New("empty remote server")
}
go sf.running()
return nil
}
// Connect is
func (sf *Client) running() {
var ctx context.Context
sf.rwMux.Lock()
if !atomic.CompareAndSwapUint32(&sf.status, initial, disconnected) {
sf.rwMux.Unlock()
return
}
ctx, sf.closeCancel = context.WithCancel(context.Background())
sf.rwMux.Unlock()
defer sf.setConnectStatus(initial)
for {
select {
case <-ctx.Done():
return
default:
}
sf.Debug("connecting server %+v", sf.option.server)
conn, err := openConnection(sf.option.server, sf.option.TLSConfig, sf.option.config.ConnectTimeout0)
if err != nil {
sf.Error("connect failed, %v", err)
if !sf.option.autoReconnect {
return
}
time.Sleep(sf.option.reconnectInterval)
continue
}
sf.Debug("connect success")
sf.conn = conn
sf.run(ctx)
sf.Debug("disconnected server %+v", sf.option.server)
select {
case <-ctx.Done():
return
default:
// 随机500ms-1s的重试,避免快速重试造成服务器许多无效连接
time.Sleep(time.Millisecond * time.Duration(500+rand.Intn(500)))
}
}
}
func (sf *Client) recvLoop() {
sf.Debug("recvLoop started")
defer func() {
sf.cancel()
sf.wg.Done()
sf.Debug("recvLoop stopped")
}()
for {
rawData := make([]byte, APDUSizeMax)
for rdCnt, length := 0, 2; rdCnt < length; {
byteCount, err := io.ReadFull(sf.conn, rawData[rdCnt:length])
if err != nil {
// See: https://github.com/golang/go/issues/4373
if err != io.EOF && err != io.ErrClosedPipe ||
strings.Contains(err.Error(), "use of closed network connection") {
sf.Error("receive failed, %v", err)
return
}
if e, ok := err.(net.Error); ok && !e.Temporary() {
sf.Error("receive failed, %v", err)
return
}
if rdCnt == 0 && err == io.EOF {
sf.Error("remote connect closed, %v", err)
return
}
}
rdCnt += byteCount
if rdCnt == 0 {
continue
} else if rdCnt == 1 {
if rawData[0] != startFrame {
rdCnt = 0
continue
}
} else {
if rawData[0] != startFrame {
rdCnt, length = 0, 2
continue
}
length = int(rawData[1]) + 2
if length < APCICtlFiledSize+2 || length > APDUSizeMax {
rdCnt, length = 0, 2
continue
}
if rdCnt == length {
apdu := rawData[:length]
sf.Debug("RX Raw[% x]", apdu)
sf.rcvRaw <- apdu
}
}
}
}
}
func (sf *Client) sendLoop() {
sf.Debug("sendLoop started")
defer func() {
sf.cancel()
sf.wg.Done()
sf.Debug("sendLoop stopped")
}()
for {
select {
case <-sf.ctx.Done():
return
case apdu := <-sf.sendRaw:
sf.Debug("TX Raw[% x]", apdu)
for wrCnt := 0; len(apdu) > wrCnt; {
byteCount, err := sf.conn.Write(apdu[wrCnt:])
if err != nil {
// See: https://github.com/golang/go/issues/4373
if err != io.EOF && err != io.ErrClosedPipe ||
strings.Contains(err.Error(), "use of closed network connection") {
sf.Error("sendRaw failed, %v", err)
return
}
if e, ok := err.(net.Error); !ok || !e.Temporary() {
sf.Error("sendRaw failed, %v", err)
return
}
// temporary error may be recoverable
}
wrCnt += byteCount
}
}
}
}
// run is the big fat state machine.
func (sf *Client) run(ctx context.Context) {
sf.Debug("run started!")
// before any thing make sure init
sf.cleanUp()
sf.ctx, sf.cancel = context.WithCancel(ctx)
sf.setConnectStatus(connected)
sf.wg.Add(3)
go sf.recvLoop()
go sf.sendLoop()
go sf.handlerLoop()
var checkTicker = time.NewTicker(timeoutResolution)
// transmission timestamps for timeout calculation
var willNotTimeout = time.Now().Add(time.Hour * 24 * 365 * 100)
var unAckRcvSince = willNotTimeout
var idleTimeout3Sine = time.Now() // 空闲间隔发起testFrAlive
var testFrAliveSendSince = willNotTimeout // 当发起testFrAlive时,等待确认回复的超时间隔
sf.startDtActiveSendSince.Store(willNotTimeout)
sf.stopDtActiveSendSince.Store(willNotTimeout)
sendSFrame := func(rcvSN uint16) {
sf.Debug("TX sFrame %v", sAPCI{rcvSN})
sf.sendRaw <- newSFrame(rcvSN)
}
sendIFrame := func(asdu1 []byte) {
seqNo := sf.seqNoSend
iframe, err := newIFrame(seqNo, sf.seqNoRcv, asdu1)
if err != nil {
return
}
sf.ackNoRcv = sf.seqNoRcv
sf.seqNoSend = (seqNo + 1) & 32767
sf.pending = append(sf.pending, seqPending{seqNo & 32767, time.Now()})
sf.Debug("TX iFrame %v", iAPCI{seqNo, sf.seqNoRcv})
sf.sendRaw <- iframe
}
defer func() {
// default: STOPDT, when connected establish and not enable "data transfer" yet
atomic.StoreUint32(&sf.isActive, inactive)
sf.setConnectStatus(disconnected)
checkTicker.Stop()
_ = sf.conn.Close() // 连锁引发cancel
sf.wg.Wait()
sf.onConnectionLost(sf)
sf.Debug("run stopped!")
}()
sf.onConnect(sf)
for {
if atomic.LoadUint32(&sf.isActive) == active && seqNoCount(sf.ackNoSend, sf.seqNoSend) <= sf.option.config.SendUnAckLimitK {
select {
case o := <-sf.sendASDU:
sendIFrame(o)
idleTimeout3Sine = time.Now()
continue
case <-sf.ctx.Done():
return
default: // make no block
}
}
select {
case <-sf.ctx.Done():
return
case now := <-checkTicker.C:
// check all timeouts
if now.Sub(testFrAliveSendSince) >= sf.option.config.SendUnAckTimeout1 ||
now.Sub(sf.startDtActiveSendSince.Load().(time.Time)) >= sf.option.config.SendUnAckTimeout1 ||
now.Sub(sf.stopDtActiveSendSince.Load().(time.Time)) >= sf.option.config.SendUnAckTimeout1 {
sf.Error("test frame alive confirm timeout t₁")
return
}
// check oldest unacknowledged outbound
if sf.ackNoSend != sf.seqNoSend &&
//now.Sub(sf.peek()) >= sf.SendUnAckTimeout1 {
now.Sub(sf.pending[0].sendTime) >= sf.option.config.SendUnAckTimeout1 {
sf.ackNoSend++
sf.Error("fatal transmission timeout t₁")
return
}
// 确定最早发送的i-Frame是否超时,超时则回复sFrame
if sf.ackNoRcv != sf.seqNoRcv &&
(now.Sub(unAckRcvSince) >= sf.option.config.RecvUnAckTimeout2 ||
now.Sub(idleTimeout3Sine) >= timeoutResolution) {
sendSFrame(sf.seqNoRcv)
sf.ackNoRcv = sf.seqNoRcv
}
// 空闲时间到,发送TestFrActive帧,保活
if now.Sub(idleTimeout3Sine) >= sf.option.config.IdleTimeout3 {
sf.sendUFrame(uTestFrActive)
testFrAliveSendSince = time.Now()
idleTimeout3Sine = testFrAliveSendSince
}
case apdu := <-sf.rcvRaw:
idleTimeout3Sine = time.Now() // 每收到一个i帧,S帧,U帧, 重置空闲定时器, t3
apci, asduVal := parse(apdu)
switch head := apci.(type) {
case sAPCI:
sf.Debug("RX sFrame %v", head)
if !sf.updateAckNoOut(head.rcvSN) {
sf.Error("fatal incoming acknowledge either earlier than previous or later than sendTime")
return
}
case iAPCI:
sf.Debug("RX iFrame %v", head)
if atomic.LoadUint32(&sf.isActive) == inactive {
sf.Warn("station not active")
break // not active, discard apdu
}
if !sf.updateAckNoOut(head.rcvSN) || head.sendSN != sf.seqNoRcv {
sf.Error("fatal incoming acknowledge either earlier than previous or later than sendTime")
return
}
sf.rcvASDU <- asduVal
if sf.ackNoRcv == sf.seqNoRcv { // first unacked
unAckRcvSince = time.Now()
}
sf.seqNoRcv = (sf.seqNoRcv + 1) & 32767
if seqNoCount(sf.ackNoRcv, sf.seqNoRcv) >= sf.option.config.RecvUnAckLimitW {
sendSFrame(sf.seqNoRcv)
sf.ackNoRcv = sf.seqNoRcv
}
case uAPCI:
sf.Debug("RX uFrame %v", head)
switch head.function {
//case uStartDtActive:
// sf.sendUFrame(uStartDtConfirm)
// atomic.StoreUint32(&sf.isActive, active)
case uStartDtConfirm:
atomic.StoreUint32(&sf.isActive, active)
sf.startDtActiveSendSince.Store(willNotTimeout)
//case uStopDtActive:
// sf.sendUFrame(uStopDtConfirm)
// atomic.StoreUint32(&sf.isActive, inactive)
case uStopDtConfirm:
atomic.StoreUint32(&sf.isActive, inactive)
sf.stopDtActiveSendSince.Store(willNotTimeout)
case uTestFrActive:
sf.sendUFrame(uTestFrConfirm)
case uTestFrConfirm:
testFrAliveSendSince = willNotTimeout
default:
sf.Error("illegal U-Frame functions[0x%02x] ignored", head.function)
}
}
}
}
}
func (sf *Client) handlerLoop() {
sf.Debug("handlerLoop started")
defer func() {
sf.wg.Done()
sf.Debug("handlerLoop stopped")
}()
for {
select {
case <-sf.ctx.Done():
return
case rawAsdu := <-sf.rcvASDU:
asduPack := asdu.NewEmptyASDU(&sf.option.params)
if err := asduPack.UnmarshalBinary(rawAsdu); err != nil {
sf.Warn("asdu UnmarshalBinary failed,%+v", err)
continue
}
if err := sf.clientHandler(asduPack); err != nil {
sf.Warn("Falied handling I frame, error: %v", err)
}
}
}
}
func (sf *Client) setConnectStatus(status uint32) {
sf.rwMux.Lock()
atomic.StoreUint32(&sf.status, status)
sf.rwMux.Unlock()
}
func (sf *Client) connectStatus() uint32 {
sf.rwMux.RLock()
status := atomic.LoadUint32(&sf.status)
sf.rwMux.RUnlock()
return status
}
func (sf *Client) cleanUp() {
sf.ackNoRcv = 0
sf.ackNoSend = 0
sf.seqNoRcv = 0
sf.seqNoSend = 0
sf.pending = nil
// clear sending chan buffer
loop:
for {
select {
case <-sf.sendRaw:
case <-sf.rcvRaw:
case <-sf.rcvASDU:
case <-sf.sendASDU:
default:
break loop
}
}
}
func (sf *Client) sendUFrame(which byte) {
sf.Debug("TX uFrame %v", uAPCI{which})
sf.sendRaw <- newUFrame(which)
}
func (sf *Client) updateAckNoOut(ackNo uint16) (ok bool) {
if ackNo == sf.ackNoSend {
return true
}
// new acks validate, ack 不能在 req seq 前面,出错
if seqNoCount(sf.ackNoSend, sf.seqNoSend) < seqNoCount(ackNo, sf.seqNoSend) {
return false
}
// confirm reception
for i, v := range sf.pending {
if v.seq == (ackNo - 1) {
sf.pending = sf.pending[i+1:]
break
}
}
sf.ackNoSend = ackNo
return true
}
// IsConnected get server session connected state
func (sf *Client) IsConnected() bool {
return sf.connectStatus() == connected
}
// clientHandler hand response handler
func (sf *Client) clientHandler(asduPack *asdu.ASDU) error {
defer func() {
if err := recover(); err != nil {
sf.Critical("client handler %+v", err)
}
}()
sf.Debug("ASDU %+v", asduPack)
switch asduPack.Identifier.Type {
case asdu.C_IC_NA_1: // InterrogationCmd
return sf.handler.InterrogationHandler(sf, asduPack)
case asdu.C_CI_NA_1: // CounterInterrogationCmd
return sf.handler.CounterInterrogationHandler(sf, asduPack)
case asdu.C_RD_NA_1: // ReadCmd
return sf.handler.ReadHandler(sf, asduPack)
case asdu.C_CS_NA_1: // ClockSynchronizationCmd
return sf.handler.ClockSyncHandler(sf, asduPack)
case asdu.C_TS_NA_1: // TestCommand
return sf.handler.TestCommandHandler(sf, asduPack)
case asdu.C_RP_NA_1: // ResetProcessCmd
return sf.handler.ResetProcessHandler(sf, asduPack)
case asdu.C_CD_NA_1: // DelayAcquireCommand
return sf.handler.DelayAcquisitionHandler(sf, asduPack)
}
return sf.handler.ASDUHandler(sf, asduPack)
}
// Params returns params of client
func (sf *Client) Params() *asdu.Params {
return &sf.option.params
}
// Send send asdu
func (sf *Client) Send(a *asdu.ASDU) error {
if !sf.IsConnected() {
return ErrUseClosedConnection
}
if atomic.LoadUint32(&sf.isActive) == inactive {
return ErrNotActive
}
data, err := a.MarshalBinary()
if err != nil {
return err
}
select {
case sf.sendASDU <- data:
default:
return ErrBufferFulled
}
return nil
}
// UnderlyingConn returns underlying conn of client
func (sf *Client) UnderlyingConn() net.Conn {
return sf.conn
}
// Close close all
func (sf *Client) Close() error {
sf.rwMux.Lock()
if sf.closeCancel != nil {
sf.closeCancel()
}
sf.rwMux.Unlock()
return nil
}
// SendStartDt start data transmission on this connection
func (sf *Client) SendStartDt() {
sf.startDtActiveSendSince.Store(time.Now())
sf.sendUFrame(uStartDtActive)
}
// SendStopDt stop data transmission on this connection
func (sf *Client) SendStopDt() {
sf.stopDtActiveSendSince.Store(time.Now())
sf.sendUFrame(uStopDtActive)
}
//InterrogationCmd wrap asdu.InterrogationCmd
func (sf *Client) InterrogationCmd(coa asdu.CauseOfTransmission, ca asdu.CommonAddr, qoi asdu.QualifierOfInterrogation) error {
return asdu.InterrogationCmd(sf, coa, ca, qoi)
}
// CounterInterrogationCmd wrap asdu.CounterInterrogationCmd
func (sf *Client) CounterInterrogationCmd(coa asdu.CauseOfTransmission, ca asdu.CommonAddr, qcc asdu.QualifierCountCall) error {
return asdu.CounterInterrogationCmd(sf, coa, ca, qcc)
}
// ReadCmd wrap asdu.ReadCmd
func (sf *Client) ReadCmd(coa asdu.CauseOfTransmission, ca asdu.CommonAddr, ioa asdu.InfoObjAddr) error {
return asdu.ReadCmd(sf, coa, ca, ioa)
}
// ClockSynchronizationCmd wrap asdu.ClockSynchronizationCmd
func (sf *Client) ClockSynchronizationCmd(coa asdu.CauseOfTransmission, ca asdu.CommonAddr, t time.Time) error {
return asdu.ClockSynchronizationCmd(sf, coa, ca, t)
}
// ResetProcessCmd wrap asdu.ResetProcessCmd
func (sf *Client) ResetProcessCmd(coa asdu.CauseOfTransmission, ca asdu.CommonAddr, qrp asdu.QualifierOfResetProcessCmd) error {
return asdu.ResetProcessCmd(sf, coa, ca, qrp)
}
// DelayAcquireCommand wrap asdu.DelayAcquireCommand
func (sf *Client) DelayAcquireCommand(coa asdu.CauseOfTransmission, ca asdu.CommonAddr, msec uint16) error {
return asdu.DelayAcquireCommand(sf, coa, ca, msec)
}
// TestCommand wrap asdu.TestCommand
func (sf *Client) TestCommand(coa asdu.CauseOfTransmission, ca asdu.CommonAddr) error {
return asdu.TestCommand(sf, coa, ca)
}