diff --git a/business/jxstore/event/event_tcp.go b/business/jxstore/event/event_tcp.go index a79f499c9..0c3fbc9f5 100644 --- a/business/jxstore/event/event_tcp.go +++ b/business/jxstore/event/event_tcp.go @@ -17,11 +17,12 @@ import ( ) var Poll *Pool +var PrintPool map[string]*PrintPoolMap func init() { Poll = NewPool(500) Poll.Start() - + PrintPool = make(map[string]*PrintPoolMap, 10000) } //入口 @@ -73,7 +74,6 @@ func handleConn(c net.Conn, t *TcpClient) error { } defer c.Close() for { - var printNo string //打印机编号 buffer, n, err := ConnRead(c) if err != nil { if err == io.EOF { @@ -99,10 +99,24 @@ func handleConn(c net.Conn, t *TcpClient) error { c.Write([]byte("ok")) return err } - //证明是心跳 + + var printNo string //打印机编号 + var heartbeat bool = false if strings.Contains(data, heartText) || strings.Contains(data, heartTextNew) { printNoData, _ := hex.DecodeString(data[len(heartText) : len(data)-8]) printNo = string(printNoData) + heartbeat = true + } else if strings.Contains(data, printText) || strings.Contains(data, printTextNew) { //打印回调 + _, printNo = getCallbackMsgInfo(data) + heartbeat = true + } + + obj, ok := PrintPool[printNo] + + //证明是心跳 + if heartbeat { + //printNoData, _ := hex.DecodeString(data[len(heartText) : len(data)-8]) + //printNo = string(printNoData) status := printStatus2JxStatus(data[len(data)-8 : len(data)-6]) //如果没在连接池里 //1、加到连接池中,不同的打印机no开不同的goroutine @@ -132,14 +146,12 @@ func handleConn(c net.Conn, t *TcpClient) error { t.setPrintStatusTime(printNo) } //状态不一致再更新状态(可能缺纸了,过热了等) - //if t.getPrintStatus(printNo) != status { t.setPrintStatus(printNo, status) changePrinterStatus(printNo, status) - //} - } else if strings.Contains(data, printText) || strings.Contains(data, printTextNew) { //打印回调 + } else if heartbeat { //打印回调 //打印消息发送后,打印机会回调该条打印消息的状态(打印成功or失败,失败原因..) //将回调的信息放到回调channel中,打印成功后再打印下一条消息 - _, printNo = getCallbackMsgInfo(data) + //_, printNo = getCallbackMsgInfo(data) //更新打印机心跳时间(打印机本身不会在打印的同时,或回调的同时发心跳消息,会导致心跳判断超时,这里更新一下) t.setPrintStatusTime(printNo) t.addCallbackChan(printNo, data) @@ -462,7 +474,7 @@ func doPrint(t *TcpClient, key string) (err error) { //检测心跳 func HandleCheckTcpHeart(t *TcpClient, key string) { - go func() { + fn := func() { for { if t.TimeoutMap[key] == true { statusTime := t.getPrintStatusTime(key) @@ -472,7 +484,6 @@ func HandleCheckTcpHeart(t *TcpClient, key string) { globals.SugarLogger.Debugf("超过一分十秒没有心跳的打印机[%s]", key) changePrinterStatus(key, printerStatusOffline) delete(t.TimeoutMap, key) - globals.SugarLogger.Debugf("t := %s", utils.Format4Output(t.Clients[key].StatusTime, false)) } } } else { @@ -480,8 +491,32 @@ func HandleCheckTcpHeart(t *TcpClient, key string) { close(t.MsgMap[key]) close(t.CallBackMap[key]) t.delConn(key) + t = nil return } } - }() + } + Poll.AddJob(fn) + //go func() { + // for { + // if t.TimeoutMap[key] == true { + // statusTime := t.getPrintStatusTime(key) + // if !utils.IsTimeZero(statusTime) { + // //1分钟内没心跳判断打印机掉线了 + // if time.Now().Sub(statusTime) > time.Second*70 { + // globals.SugarLogger.Debugf("超过一分十秒没有心跳的打印机[%s]", key) + // changePrinterStatus(key, printerStatusOffline) + // delete(t.TimeoutMap, key) + // globals.SugarLogger.Debugf("t := %s", utils.Format4Output(t.Clients[key].StatusTime, false)) + // } + // } + // } else { + // t.getClients(key).C.Close() + // close(t.MsgMap[key]) + // close(t.CallBackMap[key]) + // t.delConn(key) + // return + // } + // } + //}() } diff --git a/business/jxstore/event/event_tcp_utils.go b/business/jxstore/event/event_tcp_utils.go index d692ac193..632067cfe 100644 --- a/business/jxstore/event/event_tcp_utils.go +++ b/business/jxstore/event/event_tcp_utils.go @@ -125,13 +125,17 @@ type PrintInfo struct { StatusTime time.Time } +type PrintPoolMap struct { + *sync.RWMutex + PrintObj *TcpClient +} + //连接的客户端,吧每个客户端都放进来 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 - //TimeoutMap map[string]chan bool //退出channel - TimeoutMap map[string]bool //退出channel + TimeoutMap map[string]bool //退出channel *sync.RWMutex } diff --git a/business/jxstore/event/print_test.go b/business/jxstore/event/print_test.go index 9ffdc9b6e..90c388acd 100644 --- a/business/jxstore/event/print_test.go +++ b/business/jxstore/event/print_test.go @@ -104,3 +104,14 @@ func TestNewPool(t *testing.T) { pool.Stop() } + +func TestCC(t *testing.T) { + var aa map[string]string + + aa = make(map[string]string, 10) + fmt.Println(aa) + aa["1"] = "2" + c, d := aa["1"] + fmt.Println(c) + fmt.Println(d) +}