From eb9f2bb1ecb759fb8ac72eb4958a9c1cdd8f989c Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Wed, 8 Mar 2023 14:55:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E8=A7=A3=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/gateway/agent.go | 68 +++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/modules/gateway/agent.go b/modules/gateway/agent.go index 411a0bae5..049b5d86e 100644 --- a/modules/gateway/agent.go +++ b/modules/gateway/agent.go @@ -37,7 +37,7 @@ func newAgent(gateway IGateway, conn *websocket.Conn) *Agent { writeChan: make(chan []byte, 2), closeSignal: make(chan bool), state: 1, - protoMsg: map[string]int64{}, + protoMsg: make(map[string]int64, 0), } agent.wg.Add(2) go agent.readLoop() @@ -97,11 +97,39 @@ locp: code, err = this.secAuth(msg) if err == nil { this.gateway.Debugf("----------2 agent:%s uId:%s MainType:%s SubType:%s ", this.sessionId, this.uId, msg.MainType, msg.SubType) + if err := this.messageDistribution(msg); err != nil { this.gateway.Errorf("messageDistribution err:%v", err) go this.Close() break locp } + if this.uId != "" { + key := this.uId + msg.MainType + msg.SubType // 加锁 + if v, ok := this.protoMsg[key]; ok { + if v != 0 && configure.Now().Unix()-v < 2 { + // 返回错误码 + this.hlock.Lock() + this.protoMsg[key] = configure.Now().Unix() + this.hlock.Unlock() + + data, _ := anypb.New(&pb.NotifyErrorNotifyPush{ + ReqMainType: msg.MainType, + ReqSubType: msg.SubType, + Arg: msg.Data, + Code: pb.ErrorCode_InsufficientPermissions}) + err = this.WriteMsg(&pb.UserMessage{ + MainType: comm.MainTypeNotify, + SubType: comm.SubTypeErrorNotify, + Data: data, + }) + return + } + } + this.hlock.Lock() + this.protoMsg[key] = configure.Now().Unix() + this.hlock.Unlock() + + } } else { this.gateway.Errorf("agent:%s uId:%s 密钥无效 err:%v", this.sessionId, this.uId, err) data, _ := anypb.New(&pb.NotifyErrorNotifyPush{ReqMainType: msg.MainType, ReqSubType: msg.SubType, Code: code, Message: err.Error()}) @@ -114,14 +142,7 @@ locp: break locp } } - if this.uId != "" { - key := this.uId + msg.MainType + msg.SubType - this.hlock.Lock() - if v, ok := this.protoMsg[key]; ok && v != 0 { - v = 0 - } - this.hlock.Unlock() - } + } } this.gateway.Debugf("agent:%s uId:%s readLoop end!", this.sessionId, this.uId) @@ -352,34 +373,15 @@ func (this *Agent) messageDistribution(msg *pb.UserMessage) (err error) { log.Field{Key: "req", Value: fmt.Sprintf("%s:%s %v", req.MainType, req.SubType, req.Message.String())}, log.Field{Key: "reply", Value: reply.String()}, ) + if this.uId != "" { - key := this.uId + msg.MainType + msg.SubType // 加锁 - if v, ok := this.protoMsg[key]; ok { - if v != 0 && configure.Now().Unix()-v < 2 { - // 返回错误码 - this.hlock.Lock() - this.protoMsg[key] = configure.Now().Unix() - this.hlock.Unlock() - - data, _ := anypb.New(&pb.NotifyErrorNotifyPush{ - ReqMainType: msg.MainType, - ReqSubType: msg.SubType, - Arg: msg.Data, - Code: pb.ErrorCode_InsufficientPermissions}) - err = this.WriteMsg(&pb.UserMessage{ - MainType: comm.MainTypeNotify, - SubType: comm.SubTypeErrorNotify, - Data: data, - }) - return - } - } + key := this.uId + msg.MainType + msg.SubType this.hlock.Lock() - this.protoMsg[key] = configure.Now().Unix() + if v, ok := this.protoMsg[key]; ok && v != 0 { // 发送消息 协议解锁 + v = 0 + } this.hlock.Unlock() - } - if reply.Code != pb.ErrorCode_Success { data, _ := anypb.New(&pb.NotifyErrorNotifyPush{ ReqMainType: msg.MainType,