1
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||||
|
"git.rosy.net.cn/jx-print/globals"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -59,8 +60,17 @@ func handleConn(c net.Conn) error {
|
|||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
buffer, n, err := ConnRead(c)
|
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 := c.RemoteAddr().String()
|
||||||
printRemoteAddr = strings.Split(printRemoteAddr, ":")[0]
|
printRemoteAddr = strings.Split(printRemoteAddr, ":")[0]
|
||||||
|
globals.SugarLogger.Debugf("printRemoteAddr2: %s", printRemoteAddr)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
fmt.Println("connection close")
|
fmt.Println("connection close")
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"git.rosy.net.cn/jx-callback/business/model"
|
"git.rosy.net.cn/jx-callback/business/model"
|
||||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||||
"git.rosy.net.cn/jx-callback/globals"
|
"git.rosy.net.cn/jx-callback/globals"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -240,6 +241,11 @@ func (t *TcpClient) getPrintConn(key string) net.Conn {
|
|||||||
return nil
|
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 {
|
func (t *TcpClient) getPrintStatusTime(key string) time.Time {
|
||||||
t.RLock()
|
t.RLock()
|
||||||
@@ -740,6 +746,23 @@ func doPrint(t *TcpClient, key string) (err error) {
|
|||||||
return
|
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 {
|
if _, err = c.Write(data); err != nil {
|
||||||
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
|
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
|
||||||
} else {
|
} else {
|
||||||
@@ -880,3 +903,44 @@ func (t *TcpClient) changePrintMsg(data string, orderNo int64, printNo string) (
|
|||||||
}
|
}
|
||||||
return err
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user