예제 #1
0
        public HttpIoMessage HttpPostsSync(string url, ClientHttpRequsetIoMessage body, short gameId)
        {
            Debug.Log("发送" + body.interfaceName + "::" + body.methodName + "====" + body.args);
            var       request   = new UnityWebRequest(url, "POST");
            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.timeout         = 5 * 1000;
            request.uploadHandler   = new UploadHandlerRaw(byteArray.Buffer);
            request.downloadHandler = new DownloadHandlerBuffer();
            request.SetRequestHeader("Content-Type", "application/octet-stream");
            request.SendWebRequest();
            while (true)
            {
                if (request.isDone)
                {
                    break;
                }
                if (request.isHttpError)
                {
                    break;
                }
                if (request.isNetworkError)
                {
                    break;
                }
            }
            if (request.responseCode == 200)
            {
                ByteArray byteArray1 = new ByteArray();
                byteArray1.WriteBytes(request.downloadHandler.data);
                int    dataLength = byteArray1.ReadInt();
                byte[] resultData = byteArray1.ReadBytes();
                if (dataLength != resultData.Length)
                {
                    //yield return null;
                }
                try
                {
                    return(ProtostuffUtils.ProtobufDeserialize <HttpIoMessage>(resultData));
                }
                catch (Exception e)
                {
                    Debug.LogError(e.StackTrace);
                }
            }
            return(null);
        }
예제 #2
0
        private void onData()
        {
            //消息长度小于数据基础长度说明包没完整
            if (ioBuffer.Length < BASE_LENGTH)
            {
                isRead = false;
                return;
            }

            //读取定义的消息长度
            while (true)
            {
                int datazie = ioBuffer.ReadInt();
                if (datazie == -777888)
                {
                    break;
                }
            }
            short cmd       = ioBuffer.ReadShort();
            short gameId    = ioBuffer.ReadShort();
            short srcGameId = ioBuffer.ReadShort();
            int   length    = ioBuffer.ReadInt();

            if (ioBuffer.Length < length + BASE_LENGTH)
            {
                //还原指针
                ioBuffer.Postion = 0;
                isRead           = false;
                //数据不全 还原指针等待下一数据包(分包)
                return;
            }
            ByteArray ioData = new ByteArray();

            ioData.WriteBytes(ioBuffer.Buffer, BASE_LENGTH, length);
            ioBuffer.Postion += length;
            byte[] buf = new byte[length];
            buf = ioData.ReadBytes();
            ClientTcpIoMessage clientTcpIoMessage = ProtostuffUtils.ProtobufDeserialize <ClientTcpIoMessage>(buf);

            Debug.Log("收到:" + clientTcpIoMessage.interfaceName + "::" + clientTcpIoMessage.methodName + "====" + clientTcpIoMessage.args);
            IoMessageHandler.getInstance().push(clientTcpIoMessage);
            ByteArray bytes = new ByteArray();

            bytes.WriteBytes(ioBuffer.Buffer, ioBuffer.Postion, ioBuffer.Length - ioBuffer.Postion);
            ioBuffer = bytes;
            onData();
        }
예제 #3
0
        public override void async(IoMessage ioMessage)
        {
            byte[]    buf = ProtostuffUtils.ProtobufSerialize(ioMessage);
            ByteArray arr = new ByteArray();

            arr.WriteInt(-777888);//包头
            arr.WriteShort(dataSrc);
            arr.WriteShort((short)buf.Length);
            arr.WriteBytes(buf);
            try
            {
                socket.Send(arr.Buffer);
            }
            catch (Exception)
            {
                this.close();
                Debug.Log("网络错误");
            }
        }
예제 #4
0
        public void async(ClientTcpIoMessage ioMessage, short gameId, short cmd)
        {
            Debug.Log("发送" + ioMessage.interfaceName + "::" + ioMessage.methodName + "====" + ioMessage.args);
            byte[]    buf = ProtostuffUtils.ProtobufSerialize(ioMessage);
            ByteArray arr = new ByteArray();

            arr.WriteInt(-777888);
            arr.WriteShort(cmd);
            arr.WriteShort(gameId);
            arr.WriteShort(-100);
            arr.WriteInt(buf.Length);
            arr.WriteBytes(buf);
            try
            {
                socket.Send(arr.Buffer);
            }
            catch (Exception e)
            {
                this.close();
                Debug.Log("网络错误");
                Debug.LogException(e);
            }
        }
예제 #5
0
        public override object sync(IoMessage ioMessage)
        {
            var request = new UnityWebRequest("http://127.0.0.1:8888/game/handle", "POST");

            byte[]    buf = ProtostuffUtils.ProtobufSerialize(ioMessage);
            ByteArray arr = new ByteArray();

            arr.WriteInt(-777888);//包头
            arr.WriteShort(1);
            arr.WriteShort((short)buf.Length);
            arr.WriteBytes(buf);
            request.uploadHandler   = new UploadHandlerRaw(arr.Buffer);
            request.downloadHandler = new DownloadHandlerBuffer();
            request.SetRequestHeader("Content-Type", "application/octet-stream");
            request.SendWebRequest();
            Debug.Log("发送完成");
            if (request.responseCode == 200)
            {
                ByteArray byteArray = new ByteArray();
                byteArray.WriteBytes(request.downloadHandler.data);
                int head = byteArray.ReadInt();
                if (head == -777888)
                {
                    short  dataSrcRespone = byteArray.ReadShort(); //数据类型 1,全是基础数据类型 2,PB类型
                    short  resultCode     = byteArray.ReadShort(); //成功失败码
                    short  length         = byteArray.ReadShort();
                    byte[] result         = byteArray.ReadBytes();
                    if (resultCode == 404)
                    {
                        IoMessageBaseTypeImpl errorMessage = ProtostuffUtils.ProtobufDeserialize <IoMessageBaseTypeImpl>(result);
                        string errMsg = (string)errorMessage.getData();
                        IError error  = new ErrorImpl();

                        string[] str = errMsg.Split('$');
                        string[] msg = new string[] { "" };
                        if (str.Length <= 0)
                        {
                            error.err(202, msg);
                            throw new NotImplementedException("远程调用异常");
                        }
                        else if (str.Length > 1)
                        {
                            msg = new string[str.Length - 1];
                            for (int i = 0; i < str.Length; i++)
                            {
                                msg[i] = str[i + 1];
                            }
                        }
                        error.err(short.Parse(str[0]), msg);
                        throw new NotImplementedException("远程调用异常");
                    }
                    switch (dataSrcRespone)
                    {
                    case (short)DataSrcEnum.BaseType:
                        Debug.Log("马上返回");
                        return(ProtostuffUtils.ProtobufDeserialize <IoMessageBaseTypeImpl>(result));

                    case (short)DataSrcEnum.PBType:
                        return(ProtostuffUtils.ProtobufDeserialize <IoMessagePBTypeImpl>(result));
                    }
                }
            }
            return(null);
        }