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)
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 {
b := make([]byte, maxSerialFrameLength)
n, err := s.client.Read(b)
@ -101,7 +111,7 @@ connected:
select {
case s.fromClient <- b[:n]:
case <-writeErrChan:
break connected
return
case <-s.clientLoopDeinitNeededChan:
writeLoopDeinitNeededChan <- true
<-writeLoopDeinitFinishedChan
@ -110,14 +120,6 @@ connected:
return
}
}
log.Print("client ", s.client.RemoteAddr().String(), " disconnected")
writeLoopDeinitNeededChan <- true
<-writeLoopDeinitFinishedChan
<-s.clientLoopDeinitNeededChan
s.clientLoopDeinitFinishedChan <- true
}
func (s *serialTCPSrvStruct) loop() {