Ejemplo n.º 1
0
        public static T CreateHttpProxy <T>(Type obj, string methodName, List <string> args, short gameId)
        {
            ClientHttpRequsetIoMessage ioMessage = new ClientHttpRequsetIoMessage();

            ioMessage.interfaceName = obj.FullName;
            ioMessage.methodName    = methodName;
            // client.args = JsonConvert.SerializeObject(callMessage.Args);
            if (args != null)
            {
                ioMessage.args = JsonMapper.ToJson(args);
            }
            else
            {
                ioMessage.args = null;
            }
            HttpIoMessage resultMessage = HttpSocketImpl.getInstance().sync(ioMessage, gameId);

            if (resultMessage != null)
            {
                if (resultMessage.resultCode == 200)
                {
                    // object o = JsonConvert.DeserializeObject(resultMessage.msg, returnType);
                    object o = JsonMapper.DeserializeObject(resultMessage.msg, typeof(T));
                    return((T)o);
                }
                else if (resultMessage.resultCode == 404)
                {
                    //异常处理
                    ErrorImpl error     = new ErrorImpl();
                    string[]  strs      = resultMessage.msg.Split(',');
                    string[]  errMsg    = null;
                    int       errorCode = 0;
                    if (strs.Length > 1)
                    {
                        errorCode = int.Parse(strs[0]);
                        errMsg    = new string[strs.Length - 1];
                        for (int i = 1; i < strs.Length; i++)
                        {
                            errMsg[i - 1] = strs[i];
                        }
                    }
                    else if (strs.Length == 1)
                    {
                        errorCode = int.Parse(strs[0]);
                    }
                    error.err(errorCode, errMsg);
                    return(default(T));
                }
                return(default(T));
            }
            else
            {
                return(default(T));
            }
        }
Ejemplo n.º 2
0
        private void RequestHttpServer(string url, ClientHttpRequsetIoMessage body, HttpIoMessage callback, short gameId)
        {
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            //request.ProtocolVersion = HttpVersion.Version11;
            //request.AutomaticDecompression = DecompressionMethods.None;
            //request.Method = "POST";
            //request.ContentType = "application/octet-stream; charset=UTF-8";
            //request.Timeout = 60 * 1000;
            //request.ReadWriteTimeout = 60 * 1000;
            //request.KeepAlive = true;
            //request.Proxy = null;
            //ByteArray byteArray = new ByteArray();
            //byteArray.WriteByte(19);
            //byteArray.WriteShort(1024);
            //byteArray.WriteInt(5);
            //byteArray.WriteShort(gameId);
            //byte[] bytes = ProtostuffUtils.ProtobufSerialize(body);
            //byteArray.WriteInt(bytes.Length);
            //byteArray.WriteBytes(bytes);
            //request.ContentLength = byteArray.Length;
            //request.GetRequestStream().Write(byteArray.Buffer, 0, byteArray.Length);
            //request.GetRequestStream().Close();
            //try
            //{

            //    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //    Debug.Log(response.ContentLength);
            //    Stream stream = response.GetResponseStream();
            //    int length = (int)response.ContentLength;

            //    byte[] data = new byte[length];
            //    int mix = 0;
            //    while (mix < length)
            //    {
            //        int _len = stream.Read(data, mix, data.Length);
            //        mix += _len;
            //    }
            //    ByteArray byteArray1 = new ByteArray();
            //    byteArray.WriteBytes(data);
            //    int dataLength = byteArray.ReadInt();
            //    byte[] resultData = byteArray.ReadBytes();
            //    if (dataLength != resultData.Length)
            //    {
            //        return;
            //    }
            //    try
            //    {
            //        callback = ProtostuffUtils.ProtobufDeserialize<HttpIoMessage>(resultData);
            //    }
            //    catch (Exception e)
            //    {
            //        Debug.LogError(e.StackTrace);
            //    }

            //}
            //catch (Exception e)
            //{
            //    Debug.Log(e);
            //}

            //StartCoroutine(HttpPostsSync(url, body, callback, gameId));
        }
Ejemplo n.º 3
0
        public HttpIoMessage sync(ClientHttpRequsetIoMessage body, short gameId)
        {
            HttpIoMessage resultMessage = HttpPostsSync("http://192.168.1.101:8090/handler", body, gameId);

            return(resultMessage);
        }