Close the TCP serial server client connection when there's an error

This commit is contained in:
Nonoo 2020-11-08 16:57:22 +01:00
parent f64d9018f6
commit 3a1b74ce2d

View file

@ -90,7 +90,17 @@ func (s *serialTCPSrvStruct) clientLoop() {
writeErrChan := make(chan error) writeErrChan := make(chan error)
go s.writeLoop(writeLoopDeinitNeededChan, writeLoopDeinitFinishedChan, writeErrChan) go s.writeLoop(writeLoopDeinitNeededChan, writeLoopDeinitFinishedChan, writeErrChan)
connected: defer func() {
s.client.Close()
log.Print("client ", s.client.RemoteAddr().String(), " disconnected")
writeLoopDeinitNeededChan <- true
<-writeLoopDeinitFinishedChan
<-s.clientLoopDeinitNeededChan
s.clientLoopDeinitFinishedChan <- true
}()
for { for {
b := make([]byte, maxSerialFrameLength) b := make([]byte, maxSerialFrameLength)
n, err := s.client.Read(b) n, err := s.client.Read(b)
@ -101,7 +111,7 @@ connected:
select { select {
case s.fromClient <- b[:n]: case s.fromClient <- b[:n]:
case <-writeErrChan: case <-writeErrChan:
break connected return
case <-s.clientLoopDeinitNeededChan: case <-s.clientLoopDeinitNeededChan:
writeLoopDeinitNeededChan <- true writeLoopDeinitNeededChan <- true
<-writeLoopDeinitFinishedChan <-writeLoopDeinitFinishedChan
@ -110,14 +120,6 @@ connected:
return return
} }
} }
log.Print("client ", s.client.RemoteAddr().String(), " disconnected")
writeLoopDeinitNeededChan <- true
<-writeLoopDeinitFinishedChan
<-s.clientLoopDeinitNeededChan
s.clientLoopDeinitFinishedChan <- true
} }
func (s *serialTCPSrvStruct) loop() { func (s *serialTCPSrvStruct) loop() {