From 3cb53546f882aa93b6688bc211c8ed95b574bad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=AE=97=E6=A5=A0?= Date: Tue, 12 May 2026 16:30:39 +0800 Subject: [PATCH] 1 --- business/jxstore/event/event_tcp.go | 10 ++++ business/jxstore/event/event_tcp_utils.go | 64 +++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/business/jxstore/event/event_tcp.go b/business/jxstore/event/event_tcp.go index 925298a7e..0c7e745c0 100644 --- a/business/jxstore/event/event_tcp.go +++ b/business/jxstore/event/event_tcp.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "git.rosy.net.cn/jx-callback/business/model/dao" + "git.rosy.net.cn/jx-print/globals" "io" "net" "strings" @@ -59,8 +60,17 @@ func handleConn(c net.Conn) error { } for { buffer, n, err := ConnRead(c) + + remoteAddr := c.RemoteAddr().(*net.TCPAddr) + remoteIP := remoteAddr.IP.String() // 打印机IP + remotePort := remoteAddr.Port // 打印机端口 + globals.SugarLogger.Debugf("remoteIP1: %s", remoteIP) + globals.SugarLogger.Debugf("remotePort1: %d", remotePort) + printRemoteAddr := c.RemoteAddr().String() printRemoteAddr = strings.Split(printRemoteAddr, ":")[0] + globals.SugarLogger.Debugf("printRemoteAddr2: %s", printRemoteAddr) + if err != nil { if err == io.EOF { fmt.Println("connection close") diff --git a/business/jxstore/event/event_tcp_utils.go b/business/jxstore/event/event_tcp_utils.go index 25bdc8f43..8956fcf46 100644 --- a/business/jxstore/event/event_tcp_utils.go +++ b/business/jxstore/event/event_tcp_utils.go @@ -8,6 +8,7 @@ import ( "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/globals" + "log" "net" "regexp" "strconv" @@ -240,6 +241,11 @@ func (t *TcpClient) getPrintConn(key string) net.Conn { return nil } } +func (t *TcpClient) setPrintConn(key string, c net.Conn) { + t.RLock() + defer t.RUnlock() + t.Clients[key].C = c +} func (t *TcpClient) getPrintStatusTime(key string) time.Time { t.RLock() @@ -740,6 +746,23 @@ func doPrint(t *TcpClient, key string) (err error) { return } + // 链接已经关闭了 + if isClosed(c) { + remoteAddr := c.RemoteAddr().(*net.TCPAddr) + remoteIP := remoteAddr.IP.String() // 打印机IP + remotePort := remoteAddr.Port // 打印机端口 + globals.SugarLogger.Debugf("remoteIP3: %s", remoteIP) + globals.SugarLogger.Debugf("remotePort3: %d", remotePort) + + printRemoteAddr := c.RemoteAddr().String() + printRemoteAddr = strings.Split(printRemoteAddr, ":")[0] + globals.SugarLogger.Debugf("printRemoteAddr3: %s", printRemoteAddr) + + c = reconnectPrinter(c) + if c != nil { + t.setPrintConn(printMsg.PrintNo, c) + } + } if _, err = c.Write(data); err != nil { globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err) } else { @@ -880,3 +903,44 @@ func (t *TcpClient) changePrintMsg(data string, orderNo int64, printNo string) ( } return err } + +// 工具函数:判断连接是否关闭 +func isClosed(conn net.Conn) bool { + buffer := make([]byte, 0) + // 设置读取超时=极短时间,不阻塞 + conn.SetReadDeadline(time.Now().Add(1 * time.Millisecond)) + _, err := conn.Read(buffer) + conn.SetReadDeadline(time.Time{}) // 重置超时 + + if err != nil { + if netErr, ok := err.(net.Error); ok && netErr.Timeout() { + return false // 只是超时,连接正常 + } + } + return true // 连接已关闭 +} + +var mu sync.Mutex + +// 自动重连 +func reconnectPrinter(printerConn net.Conn) net.Conn { + mu.Lock() + defer mu.Unlock() + + if printerConn != nil { + printerConn.Close() + } + + // 重试 5 次 + for i := 0; i < 5; i++ { + conn, err := net.DialTimeout("tcp", "打印机IP:端口", 3*time.Second) + if err == nil { + printerConn = conn + log.Println("打印机重连成功") + return printerConn + } + time.Sleep(2 * time.Second) + } + log.Println("打印机重连失败") + return nil +}