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

View File

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