-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathserialtcpsrv.go
190 lines (152 loc) · 3.71 KB
/
serialtcpsrv.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
package main
import (
"fmt"
"io"
"net"
"sync"
)
type serialTCPSrvStruct struct {
listener net.Listener
client net.Conn
fromClient chan []byte
toClient chan []byte
clientLoopDeinitNeededChan chan bool
clientLoopDeinitFinishedChan chan bool
deinitNeededChan chan bool
deinitFinishedChan chan bool
clientConnected bool
mutex sync.Mutex
}
var serialTCPSrv serialTCPSrvStruct
func (s *serialTCPSrvStruct) isClientConnected() bool {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.clientConnected
}
func (s *serialTCPSrvStruct) writeLoop(writeLoopDeinitNeededChan, writeLoopDeinitFinishedChan chan bool,
errChan chan error) {
var b []byte
for {
select {
case b = <-s.toClient:
case <-writeLoopDeinitNeededChan:
writeLoopDeinitFinishedChan <- true
return
}
for len(b) > 0 {
written, err := s.client.Write(b)
if err != nil {
errChan <- err
break
}
b = b[written:]
}
}
}
func (s *serialTCPSrvStruct) disconnectClient() {
if s.client != nil {
s.client.Close()
}
}
func (s *serialTCPSrvStruct) deinitClient() {
if s.clientLoopDeinitNeededChan != nil {
s.clientLoopDeinitNeededChan <- true
<-s.clientLoopDeinitFinishedChan
s.clientLoopDeinitNeededChan = nil
s.clientLoopDeinitFinishedChan = nil
}
}
func (s *serialTCPSrvStruct) clientLoop() {
s.mutex.Lock()
s.clientConnected = true
s.mutex.Unlock()
defer func() {
s.mutex.Lock()
s.clientConnected = false
s.mutex.Unlock()
}()
log.Print("client ", s.client.RemoteAddr().String(), " connected")
writeLoopDeinitNeededChan := make(chan bool)
writeLoopDeinitFinishedChan := make(chan bool)
writeErrChan := make(chan error)
go s.writeLoop(writeLoopDeinitNeededChan, writeLoopDeinitFinishedChan, writeErrChan)
defer func() {
s.client.Close()
log.Print("client ", s.client.RemoteAddr().String(), " disconnected")
writeLoopDeinitNeededChan <- true
<-writeLoopDeinitFinishedChan
<-s.clientLoopDeinitNeededChan
s.clientLoopDeinitFinishedChan <- true
}()
for {
b := make([]byte, maxSerialFrameLength)
n, err := s.client.Read(b)
if err != nil {
break
}
select {
case s.fromClient <- b[:n]:
case <-writeErrChan:
return
case <-s.clientLoopDeinitNeededChan:
writeLoopDeinitNeededChan <- true
<-writeLoopDeinitFinishedChan
s.clientLoopDeinitFinishedChan <- true
return
}
}
}
func (s *serialTCPSrvStruct) loop() {
for {
newClient, err := s.listener.Accept()
s.disconnectClient()
s.deinitClient()
s.clientLoopDeinitNeededChan = make(chan bool)
s.clientLoopDeinitFinishedChan = make(chan bool)
if err != nil {
if err != io.EOF {
reportError(err)
}
<-s.deinitNeededChan
s.deinitFinishedChan <- true
return
}
s.client = newClient
go s.clientLoop()
}
}
// We only init the serial port TCP server once, with the first device name we acquire, so apps using the
// serial port TCP server won't have issues with the interface going down while the app is running.
func (s *serialTCPSrvStruct) initIfNeeded() (err error) {
if s.listener != nil {
// Depleting channel which may contain data while the serial connection to the server was offline.
for {
select {
case <-s.fromClient:
default:
return
}
}
}
s.listener, err = net.Listen("tcp", fmt.Sprint(":", serialTCPPort))
if err != nil {
fmt.Println(err)
return
}
log.Print("exposing serial port on tcp port ", serialTCPPort)
s.fromClient = make(chan []byte)
s.toClient = make(chan []byte)
s.deinitNeededChan = make(chan bool)
s.deinitFinishedChan = make(chan bool)
go s.loop()
return
}
func (s *serialTCPSrvStruct) deinit() {
if s.listener != nil {
s.listener.Close()
}
if s.deinitNeededChan != nil {
s.deinitNeededChan <- true
<-s.deinitFinishedChan
}
}