Ejemplo n.º 1
0
        // 接收响应并处理
        public void HandleMessage(byte[] body)
        {
            try {
                var rsp = Pb.DecodeRsp(body);
                var seq = rsp.RspWrap1.Seq;

                SendQueueValue val = null;
                SendQueue.TryGetValue(seq + "", out val);

                var callback = val?.response;

                if (val == null)
                {
                    return;
                }
                // 处理错误码,并拦截 value.response

                EventUpload.PushRequestEvent(new ReqEventParam {
                    RqCmd = val.Cmd, RqSq = rsp.RspWrap1.Seq, RqCd = rsp.RspWrap1.ErrCode, Time = Convert.ToInt64((DateTime.Now - val.Time).TotalMilliseconds)
                });

                // 心跳不拦截
                if (val.Cmd != (int)ProtoCmd.ECmdHeartBeatReq && HandleErrCode(rsp.RspWrap1))
                {
                    return;
                }

                callback?.Invoke(rsp);
                return;
            } catch (Exception e) {
                Debugger.Log(e.ToString());
            }
        }
Ejemplo n.º 2
0
        // 发送成功 Callback
        private static void HandleSendSuccess(string seq)
        {
            SendQueueValue val = null;

            SendQueue.TryGetValue(seq + "", out val);
            if (seq == "" || val == null)
            {
                return;
            }
            val.sendSuccess();
        }
Ejemplo n.º 3
0
        // 发送失败 Callback
        private void HandleSendFail(string seq, int code)
        {
            SendQueueValue val = null;

            SendQueue.TryGetValue(seq + "", out val);
            if (val == null)
            {
                return;
            }

            // 处理 wssocket 帧长度超过 856B
            if (code == ErrCode.EcRelayDataExceedLimited || DateTime.Now.Subtract(val.Time).TotalMilliseconds > Config.ResendTimeout)
            {
                var sendCode = UserStatus.GetErrCode() != 0 ? UserStatus.GetErrCode() : code;
                val.sendFail(sendCode, null);
                return;
            }
            switch (code)
            {
            case (int)QAppProtoErrCode.EcSdkUninit:
                // 没有初始化
                val.sendFail(code, null);
                break;

            case (int)QAppProtoErrCode.EcSdkNoLogin:
                // 没登录
                Socket.Emit("autoAuth", new SocketEvent());
                return;

            case (int)QAppProtoErrCode.EcSdkNoCheckLogin:
            {
                // 没checklogin
                Socket.Emit("autoAuth", new SocketEvent());
                return;
            }
            }
            return;
        }
Ejemplo n.º 4
0
        // 发送消息请求
        public string SendRequest(ByteString body, int subcmd, NetResponseCallback response, Action <ResponseEvent> callback, string cmd, string seq)
        {
            if (seq.Length == 0)
            {
                seq = Guid.NewGuid().ToString();
                var sendQueueVal = new SendQueueValue {
                    Time         = DateTime.Now,
                    IsSocketSend = false,
                    Cmd          = (int)subcmd,
                    resend       = () => this.SendRequest(body, subcmd, response, callback, cmd, seq),
                    response     = msg => {
                        response(true, msg, callback);
                        DeleteSendQueue(seq);
                    }
                };
                sendQueueVal.sendSuccess = () => {
                    // if(Socket.Id == 1) Debugger.Log("handle send success {0}", seq);
                    sendQueueVal.IsSocketSend = true;
                };
                sendQueueVal.remove = () => {
                    DeleteSendQueue(seq);
                };
                sendQueueVal.sendFail = (errCode, errMsg) => {
                    var errMessage = "消息发送失败," + errMsg + "[" + errCode + "]";
                    var rspWrap1   = new ClientSendServerRspWrap1 {
                        Seq     = seq,
                        ErrCode = errCode,
                        ErrMsg  = errMessage
                    };
                    response(false, new DecodeRspResult {
                        RspWrap1 = rspWrap1,
                    }, callback);
                    DeleteSendQueue(seq);
                };
                AddSendQueue(seq, sendQueueVal);
            }

            // PB request = new PB();

            var qAppRequest = new ClientSendServerReqWrap1 {
                Version   = RequestHeader.Version,
                AppName   = RequestHeader.AppName,
                ClientIp  = RequestHeader.ClientIp,
                ServiceIp = RequestHeader.ServiceIp,
                Business  = RequestHeader.Business,
                AuthKey   = RequestHeader.AuthKey,
                AuthType  = RequestHeader.AuthType,
                AuthIp    = RequestHeader.AuthIp,
                GameId    = RequestHeader.GameId,
                Uid       = RequestHeader.Uid,
                PlayerId  = RequestHeader.PlayerId,
                Cmd       = cmd,
                Seq       = seq
            };
            var accessReq = new ClientSendServerReqWrap2();

            accessReq.Cmd = (ProtoCmd)subcmd;
            var data = Pb.EncodeReq(qAppRequest, accessReq, body);

            if (data.Length > _maxDataLength)
            {
                SendQueueValue val = null;
                SendQueue.TryGetValue(seq + "", out val);
                var timer = new Timer();
                timer.SetTimeout(() => {
                    if (val != null)
                    {
                        val.sendFail((int)QAppProtoErrCode.EcSdkSendFail, "数据长度超限");
                    }
                }, 0);
                return(seq);
            }

            var reqData = BuildData(data);

            return(this.Send(reqData, seq, (ProtoCmd)subcmd));
        }
Ejemplo n.º 5
0
 // 向请求队列中添加记录
 protected void AddSendQueue(string seq, SendQueueValue value)
 {
     SendQueue.TryAdd(seq, value);
     this._queue.TryAdd(seq, null);
 }
Ejemplo n.º 6
0
 // 向请求队列中添加记录
 protected void AddSendQueue(string seq, SendQueueValue value)
 {
     // Debugger.Log("Add Send queue.");
     SendQueue.Add(seq, value);
     this._queue.Add(seq);
 }