Example #1
0
        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="code">请求类型</param>
        /// <param name="postData"></param>
        /// <param name="listener"></param>
        public void SendHttpRequest(RequestCode code, Dictionary <string, object> postData, IHttpCallBackListener listener)
        {
            string url = NetConst.URL_HOST + code.ToString().ToLower();

            url += code.ToString().ToLower();
            StartCoroutine(Post(url, postData, listener));
        }
        public void HandleRequest(RequestCode requestCode, ActionCode actionCode, string data, Client client)
        {
            BaseController controller;

            if (controllerDict.TryGetValue(requestCode, out controller) == false)
            {
                Console.WriteLine("无法得到" + requestCode + "所对应的Controller,无法处理请求");
                return;
            }
            string     methodName = Enum.GetName(typeof(ActionCode), actionCode);
            MethodInfo mi         = controller.GetType().GetMethod(methodName);

            if (mi == null)
            {
                Console.WriteLine("[警告]在Controller[" + controller.GetType() + "]中没有对应的处理方法:[" + methodName + "]");
                return;
            }
            object[] parameters = { data, client, server };
            object   o          = mi.Invoke(controller, parameters);

            Console.WriteLine(requestCode.ToString() + " " + actionCode.ToString());
            if (o == null)
            {
                return;
            }
            server.SendResponse(client, actionCode, o as string);
        }
 /// <summary>
 /// 解析数据
 /// </summary>
 public void ReadMessage(int newDataAmount, Action <RequestCode, ActionCode, string> processDataCallBack)
 {
     startIndex += newDataAmount;
     while (true)
     {
         if (startIndex <= 4)//当存储的数据小于四时说明里面的数据不足,等待下一次补充存储
         {
             return;
         }
         int count = BitConverter.ToInt32(data, 0); //将byte数组中的指定位置的四个字节转为int 转出的数字表示收到的消息字节长度
         if ((startIndex - 4) >= count)             //字节数组中剩余字节大于一次消息传送的字节。也就是字节数组中的数据必须要大于要解析的数据长度
         {
             //string s = Encoding.UTF8.GetString(data, 4, count);//从第四位字节以后开始解析,前四位代表数据长度
             //Console.WriteLine("解析出一条数据" + s);
             RequestCode requestCode = (RequestCode)BitConverter.ToInt32(data, 4);
             Console.WriteLine(requestCode.ToString());
             ActionCode actionCode = (ActionCode)BitConverter.ToInt32(data, 8);
             Console.WriteLine(actionCode.ToString());
             string s = Encoding.UTF8.GetString(data, 12, count - 8);
             Console.WriteLine(s);
             processDataCallBack(requestCode, actionCode, s);              //Message只负责解析消息,解析后的消息发送给服务端处理,由于Message中么有Server的引用,并且为了降低耦合度,发送消息交给客户端链接去做,所以这里回调调用方法。
             Array.Copy(data, count + 4, data, 0, startIndex - 4 - count); //将已经解析过的位置之后的数据移动到首位,移动的数据长度是减去已经解析过的数据。相当于覆盖已经解析过的数据,直到当前读取客户端消息的字节少于等于4也就是字节数组中没有有效数据了。
             startIndex -= (count + 4);
         }
         else
         {
             break;
         }
     }
 }
Example #4
0
        public void valid(string code, RequestCode.Kind expected)
        {
            var rc = new RequestCode(code);

            Assert.Equal(expected, rc.Value);
            Assert.Equal(code, rc.ToString());
        }
Example #5
0
        public void valid(string code, TransactionKind expected)
        {
            var kind = new RequestCode(code);

            Assert.Equal(expected, kind.Value);
            Assert.Equal(expected.ToString(), kind.ToString());
        }
Example #6
0
    private BaseRequest GetRequest(RequestCode requestcode)
    {
        BaseRequest request = requestDic.TryGet(requestcode);

        if (request == null)
        {
            throw new Exception("没有找到对应Request--->" + requestcode.ToString());
        }
        return(request);
    }
Example #7
0
    public static byte[] PackData(RequestCode requestData, ActionCode actionCode, string data)
    {
//        Debug.LogWarning("Message:"+requestData);
//        Debug.LogWarning("Message:"+actionCode);
//        Debug.LogWarning("Message:"+data);
//        byte[] requestCodeBytes = BitConverter.GetBytes((int)requestData);
//        Debug.LogWarning(BitConverter.ToString(requestCodeBytes));
//        Debug.LogWarning(Encoding.UTF8.GetBytes(BitConverter.ToString(requestCodeBytes)).Length);
//        byte[] actionCodeBytes = BitConverter.GetBytes((int)actionCode);
//        byte[] dataBytes = Encoding.UTF8.GetBytes(data);
//        int dataAmount = requestCodeBytes.Length + dataBytes.Length + actionCodeBytes.Length;
//        Debug.LogWarning("Length:"+dataAmount);
//        byte[] dataAmountBytes = BitConverter.GetBytes(dataAmount);
        //byte[] newBytes = dataAmountBytes.Concat(requestCodeBytes).ToArray<byte>();//Concat(dataBytes);
        //return newBytes.Concat(dataBytes).ToArray<byte>();
//        return dataAmountBytes.Concat(requestCodeBytes).ToArray<byte>()
//            .Concat(actionCodeBytes).ToArray<byte>()
//            .Concat(dataBytes).ToArray<byte>();
//        string test = "django,12345";
//        byte[] datas = Encoding.UTF8.GetBytes(test);
//
//        return datas;

        //  Debug.LogWarning("packData");
        // Debug.LogWarning("actionCode--------->"+(int)actionCode);
        JsonData jsonData = new JsonData();

        jsonData["requestCode"] = requestData.ToString();
        jsonData["actionCode"]  = actionCode.ToString();
        jsonData["data"]        = data;

        string datas = jsonData.ToJson();

        //  Debug.LogWarning("PackedData------>"+datas);

        byte[] Json = Encoding.UTF8.GetBytes(datas);
        return(Json);

//        byte[] test = dataAmountBytes.Concat(requestCodeBytes).ToArray<byte>()
//            .Concat(actionCodeBytes).ToArray<byte>();


//       Debug.LogWarning("---------"+Encoding.UTF8.GetBytes(BitConverter.ToString(test)).Length);
//        return Encoding.UTF8.GetBytes(BitConverter.ToString(test));
    }
Example #8
0
        /// <summary>
        /// 解析数据或者叫做读取数据
        /// </summary>
        public void ReadMessage(int dataLength, Action <RequestCode, ActionCode, string> processDataCallback)
        {
            tail += dataLength;
            while (true)
            {
                if (tail <= head)
                {
                    return;              //接受数据太小
                }
                //headContent解析出来的大小指的是去除整个协议头部的数据大小
                int headContent = BitConverter.ToInt32(data, 0);
                Console.WriteLine("headContent:" + headContent);
                int re = BitConverter.ToInt32(data, 4);
                Console.WriteLine("re:" + re);
                int ac = BitConverter.ToInt32(data, 8);
                Console.WriteLine("ac:" + ac);

                //判断接收到的数据去掉头部是否满足headContent
                if ((tail - head) >= headContent)
                {
                    RequestCode request = (RequestCode)re;
                    Console.WriteLine("request:" + request.ToString());
                    ActionCode action = (ActionCode)ac;
                    Console.WriteLine("action:" + action.ToString());
                    string s = Encoding.UTF8.GetString(data, head, headContent);
                    Console.WriteLine("s:" + s);

                    //执行回调函数
                    processDataCallback(request, action, s);
                    Console.WriteLine("回调函数已执行!");

                    Array.Copy(data, headContent + head, data, 0, tail - head - headContent);
                    tail -= (headContent + head);
                }
                //此处条件为发生了分包
                else
                {
                    break;
                }
            }
        }
Example #9
0
        /// <summary>
        /// 处理请求-通过RequestCode找到Controller,ActionCode找到具体执行的方法
        /// </summary>
        /// <param name="requestCode">请求枚举</param>
        /// <param name="actionCode">方法枚举</param>
        /// <param name="data">数据</param>
        /// <param name="runThisRequestClient">发起这个请求的客户端</param>
        public void HandleRequest(RequestCode requestCode, ActionCode actionCode, string data, Client runThisRequestClient)
        {
            BaseController controller;
            bool           isSuccessful = _allControllerDic.TryGetValue(requestCode, out controller);

            if (!isSuccessful)
            {
                //报错
                Console.WriteLine("未从allControllerDic字典中找到RequestCode:" + requestCode.ToString() + "对应的Controller");
                return;
            }
            else
            {
                //找到了就调用Controller中ActionCode对应的方法
            }
            //将ActionCode枚举类型转化为字符串-不用ToString,因为这种方式效率不高
            string methodName = Enum.GetName(typeof(ActionCode), actionCode);
            //利用反射机制-ActionCode就是Controller中方法的名字,通过方法名字来访问方法
            //得到函数信息
            MethodInfo methodInfo = controller.GetType().GetMethod(methodName);

            if (methodInfo == null)
            {
                Console.WriteLine("未找到类" + controller.ToString() + "对应的方法" + methodName);
                return;
            }
            else
            {
                //执行这个函数-要指定哪个对象,要参数可以传参数[为一个Object
                object[] parameters  = new object[] { data, runThisRequestClient, server };
                object   responseObj = methodInfo.Invoke(controller, parameters);
                //Console.WriteLine("");

                //判断obj,看需不需要返回客户端数据
                if (responseObj != null && (responseObj as string) != "")
                {
                    //不为空且不为空串时-返回给客户端数据[响应客户端]
                    server.SendResponseToClient(runThisRequestClient, actionCode, responseObj as string);
                }
            }
        }
Example #10
0
        /// <summary>
        /// 接口返回Json数据 统一处理
        /// </summary>
        /// <returns></returns>
        protected string RetrnJson <T>(WebServiceResponse <T> objDataEt)
        {
            dynamic     d    = new System.Dynamic.ExpandoObject();
            RequestCode code = RequestCode.成功;
            string      Msg  = "";

            if (objDataEt != null)
            {
                if (!objDataEt.Successful)
                {
                    // 属于 接口供应商的错误代码
                    switch (objDataEt.ErrCode)
                    {
                    case APIErrCode.用户认证失败:
                        code = RequestCode.API_用户认证失败;
                        break;

                    case APIErrCode.内部程序出错:
                        code = RequestCode.API_内部程序出错;
                        break;

                    case APIErrCode.订单号已存在:
                        code = RequestCode.API_订单号已存在;
                        break;

                    case APIErrCode.价格不正确:
                        code = RequestCode.API_价格不正确;
                        break;

                    case APIErrCode.API新错误代码:
                        code = RequestCode.API_新错误代码;
                        Msg  = objDataEt.ErrMsg;
                        break;

                    default:
                        code = RequestCode.API_新错误代码;
                        Msg  = string.Format("API_新错误代码:{0}", objDataEt.ErrMsg);
                        break;
                    }
                }
                else
                {
                    if (objDataEt.ResponseEt == null)
                    {
                        code = RequestCode.无数据返回;
                    }
                }
            }
            else
            {
                code = RequestCode.序代码出错;
            }

            d.Code = (int)code;
            if (string.IsNullOrEmpty(Msg))
            {
                d.Msg = code.ToString();
            }

            if (objDataEt != null)
            {
                d.DataModel = objDataEt.ResponseEt;
            }
            else
            {
                d.DataModel = null;
            }

            return(JsonUtil.GetJsonByObj(d));
        }
Example #11
0
 private void OnProcessMessage(RequestCode requestCode, ActionCode actionCode, string data)
 {
     Console.WriteLine("收到消息" + "," + requestCode.ToString() + "," + actionCode.ToString() + "," + data);
     server.HandleRequest(requestCode, actionCode, data, this);
 }