Esempio 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());
            }
        }
Esempio n. 2
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));
        }