This commit is contained in:
suyl
2021-07-30 17:22:51 +08:00
parent d1c0a06730
commit 14de70f911
2 changed files with 66 additions and 21 deletions

View File

@@ -99,10 +99,10 @@ var (
regexpQrr = regexp.MustCompile(byteSignQrRight + "(.*?)" + byteSignQrRightE)
regexpSound = regexp.MustCompile(byteSignSound + "(.*?)" + byteSignSoundE)
printMsgChan = make(chan *model.PrintMsg, 1024)
//printMsgChan = make(chan *model.PrintMsg, 1024)
//printMsgCallbackMap = make(map[string]chan string, 1024)
printMsgChanFail = make(chan *model.PrintMsg, 1024)
timeoutChan = make(chan int, 10)
//printMsgChanFail = make(chan *model.PrintMsg, 1024)
timeoutChan = make(chan int, 10)
)
type PrintInfo struct {
@@ -113,8 +113,9 @@ type PrintInfo struct {
//连接的客户端,吧每个客户端都放进来
type TcpClient struct {
Clients map[string]*PrintInfo //放tcp连接的printNo 为key
CallBackMap map[string]chan string //放打印信息回调信息printNo为key
Clients map[string]*PrintInfo //放tcp连接的printNo 为key
MsgMap map[string]chan *model.PrintMsg //放打印信息的printNo为key
CallBackMap map[string]chan string //放打印信息回调信息的printNo为key
*sync.RWMutex
}
@@ -123,20 +124,20 @@ type GetPrintStatus struct {
AppID int
}
//从连接池删除,并关闭连接
func (t *TcpClient) delConn(key string) {
t.Lock()
defer t.Unlock()
if t.Clients[key].C != nil {
t.Clients[key].C.Close()
}
delete(t.Clients, key)
}
//添加到连接池中
func (t *TcpClient) addConn(c net.Conn, key string, status int) {
t.Lock()
defer t.Unlock()
t.Clients[key] = &PrintInfo{
C: c,
Status: status,
@@ -144,6 +145,13 @@ func (t *TcpClient) addConn(c net.Conn, key string, status int) {
}
}
func (t *TcpClient) buildMsgMap(key string) {
t.Lock()
defer t.Unlock()
dataChan := make(chan *model.PrintMsg, 1024)
t.MsgMap[key] = dataChan
}
func (t *TcpClient) buildCallBackMap(key string) {
t.Lock()
defer t.Unlock()
@@ -171,10 +179,29 @@ func (t *TcpClient) getPrintConn(key string) net.Conn {
}
}
func (t *TcpClient) isExistMsg(key string) bool {
t.RLock()
defer t.RUnlock()
if t.MsgMap[key] == nil {
return false
} else {
return true
}
}
func (t *TcpClient) isExistCallback(key string) bool {
t.RLock()
defer t.RUnlock()
if t.CallBackMap[key] == nil {
return false
} else {
return true
}
}
func (t *TcpClient) isExist(key string) bool {
t.RLock()
defer t.RUnlock()
if t.Clients[key] == nil {
return false
} else {
@@ -185,17 +212,35 @@ func (t *TcpClient) isExist(key string) bool {
func (t *TcpClient) setPrintStatus(key string, status int) {
t.Lock()
defer t.Unlock()
if t.isExist(key) {
t.Clients[key].Status = status
t.Clients[key].StatusTime = time.Now()
}
}
func (t *TcpClient) addMsgChan(printMsg *model.PrintMsg) {
t.RUnlock()
defer t.RUnlock()
if !t.isExistMsg(printMsg.PrintNo) {
t.buildMsgMap(printMsg.PrintNo)
}
t.MsgMap[printMsg.PrintNo] <- printMsg
}
func (t *TcpClient) addCallbackChan(key, data string) {
t.RUnlock()
defer t.RUnlock()
if !t.isExistCallback(key) {
t.buildCallBackMap(key)
}
t.CallBackMap[key] <- data
}
func NewTcpClient() *TcpClient {
t := &TcpClient{
Clients: make(map[string]*PrintInfo),
CallBackMap: make(map[string]chan string),
MsgMap: make(map[string]chan *model.PrintMsg),
}
t.RWMutex = new(sync.RWMutex)
return t