This commit is contained in:
suyl
2021-08-03 11:26:45 +08:00
parent b3e86e8e63
commit 8e44870cbf
2 changed files with 16 additions and 11 deletions

View File

@@ -83,6 +83,7 @@ func (t *TcpClient) handleConn(c net.Conn) {
t.addConn(c, printNo, status)
t.buildCallBackMap(printNo)
t.buildMsgMap(printNo)
t.buildTimeoutMap(printNo)
t.HandleTcpMessages(printNo)
t.doPrint(printNo)
if status == printerStatusOnline {
@@ -167,7 +168,7 @@ func (t *TcpClient) HandleTcpMessages(printNo string) {
go func() {
for {
select {
case <-t.TimeoutChan:
case <-t.TimeoutMap[printNo]:
globals.SugarLogger.Debugf("HandleTcpMessages timeout")
return
default:
@@ -196,7 +197,7 @@ func (t *TcpClient) doPrint(key string) (err error) {
go func() {
for {
select {
case <-t.TimeoutChan:
case <-t.TimeoutMap[key]:
globals.SugarLogger.Debugf("doPrint timeout")
return
default:
@@ -233,7 +234,7 @@ func (t *TcpClient) doPrint(key string) (err error) {
dao.UpdateEntity(db, printMsg, "Status", "Comment")
if t.isExist(key) {
t.clear(key)
close(t.TimeoutChan)
close(t.TimeoutMap[key])
}
return
} else {
@@ -243,7 +244,7 @@ func (t *TcpClient) doPrint(key string) (err error) {
//t.delConn(printMsg.PrintNo)
if t.isExist(key) {
t.clear(key)
close(t.TimeoutChan)
close(t.TimeoutMap[key])
}
} else {
globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data))
@@ -267,7 +268,7 @@ func (t *TcpClient) HandleCheckTcpHeart(key string) {
go func() {
for {
select {
case <-t.TimeoutChan:
case <-t.TimeoutMap[key]:
globals.SugarLogger.Debugf("HandleCheckTcpHeart timeout")
return
default:
@@ -277,7 +278,7 @@ func (t *TcpClient) HandleCheckTcpHeart(key string) {
changePrinterStatus(key, printerStatusOffline)
if t.isExist(key) {
t.clear(key)
close(t.TimeoutChan)
close(t.TimeoutMap[key])
return
}
}

View File

@@ -120,7 +120,7 @@ type TcpClient struct {
Clients map[string]*PrintInfo //放tcp连接的printNo 为key
MsgMap map[string]chan *model.PrintMsg //放打印信息的printNo为key
CallBackMap map[string]chan string //放打印信息回调信息的printNo为key
TimeoutChan chan bool //退出channel
TimeoutMap map[string]chan bool //退出channel
*sync.RWMutex
}
@@ -149,9 +149,6 @@ func (t *TcpClient) addConn(c net.Conn, key string, status int) {
Status: status,
StatusTime: time.Now(),
}
if t.TimeoutChan == nil {
t.TimeoutChan = make(chan bool)
}
}
func (t *TcpClient) buildMsgMap(key string) {
@@ -168,6 +165,13 @@ func (t *TcpClient) buildCallBackMap(key string) {
t.CallBackMap[key] = dataChan
}
func (t *TcpClient) buildTimeoutMap(key string) {
t.Lock()
defer t.Unlock()
dataChan := make(chan bool)
t.TimeoutMap[key] = dataChan
}
func (t *TcpClient) getPrintStatus(key string) int {
t.RLock()
defer t.RUnlock()
@@ -283,7 +287,7 @@ func NewTcpClient() *TcpClient {
Clients: make(map[string]*PrintInfo),
CallBackMap: make(map[string]chan string),
MsgMap: make(map[string]chan *model.PrintMsg),
TimeoutChan: make(chan bool),
TimeoutMap: make(map[string]chan bool),
}
t.RWMutex = new(sync.RWMutex)
return t