This commit is contained in:
suyl
2021-08-02 14:35:34 +08:00
parent 18f9feb2de
commit 882d8eee59
2 changed files with 37 additions and 18 deletions

View File

@@ -22,7 +22,6 @@ func ListenTcp() {
return return
} }
globals.SugarLogger.Debugf("begin listenTcp port 8000......") globals.SugarLogger.Debugf("begin listenTcp port 8000......")
go t.HandleCheckTcpHeart()
for { for {
c, err := l.Accept() c, err := l.Accept()
if err != nil { if err != nil {
@@ -83,6 +82,7 @@ func (t *TcpClient) handleConn(c net.Conn) {
} }
changePrinterStatus(printNo, status) changePrinterStatus(printNo, status)
} }
t.HandleCheckTcpHeart(printNo)
if t.getPrintStatus(printNo) != status { if t.getPrintStatus(printNo) != status {
t.setPrintStatus(printNo, status) t.setPrintStatus(printNo, status)
changePrinterStatus(printNo, status) changePrinterStatus(printNo, status)
@@ -219,23 +219,32 @@ func (t *TcpClient) doPrint(key string) (err error) {
return err return err
} }
func (t *TcpClient) HandleCheckTcpHeart() { func (t *TcpClient) HandleCheckTcpHeart(key string) {
go func() {
for { for {
keys := []string{} if time.Now().Sub(t.getPrintStatusTime(key)) > time.Minute+time.Second {
t.RLock() changePrinterStatus(key, printerStatusOffline)
for k, v := range t.Clients { if t.isExist(key) {
if time.Now().Sub(v.StatusTime) > time.Minute+time.Second { t.clear(key)
v.Status = printerStatusOffline return
keys = append(keys, k)
}
}
t.RUnlock()
globals.SugarLogger.Debugf("HandleCheckTcpHeart del keys: %v", keys)
for _, v := range keys {
changePrinterStatus(v, printerStatusOffline)
if t.isExist(v) {
t.clear(v)
} }
} }
//keys := []string{}
//t.RLock()
//for k, v := range t.Clients {
// if time.Now().Sub(v.StatusTime) > time.Minute+time.Second {
// v.Status = printerStatusOffline
// keys = append(keys, k)
// }
//}
//t.RUnlock()
//globals.SugarLogger.Debugf("HandleCheckTcpHeart del keys: %v", keys)
//for _, v := range keys {
// changePrinterStatus(v, printerStatusOffline)
// if t.isExist(v) {
// t.clear(v)
// }
//}
} }
}()
} }

View File

@@ -184,6 +184,16 @@ func (t *TcpClient) getPrintConn(key string) net.Conn {
} }
} }
func (t *TcpClient) getPrintStatusTime(key string) time.Time {
t.RLock()
defer t.RUnlock()
if t.Clients[key] != nil {
return t.Clients[key].StatusTime
} else {
return utils.ZeroTimeValue
}
}
func (t *TcpClient) isExistMsg(key string) bool { func (t *TcpClient) isExistMsg(key string) bool {
t.RLock() t.RLock()
defer t.RUnlock() defer t.RUnlock()