Пример #1
0
        private void ReadData()
        {
            Byte[] data = GameProtocol.Encoder.Decode(ref receiveCache);

            if (data != null)
            {
                Protocol protocol = ProtocolHelper.ConvertBytesToProtocol(data);
                receiveCallBack(protocol, this);
                ReadData();
            }
            else
            {
                isReceiving = false;
            }
        }
Пример #2
0
        static void SerailizingTestCase()
        {
            LoginProtocol loginProtocol = new LoginProtocol()
            {
                userId = "abc", userPassword = "******"
            };

            Byte[] bytes = ProtocolHelper.ConvertProtocolToBytes(loginProtocol);

            var result = ProtocolHelper.ConvertBytesToProtocol(bytes);

            if (result != null)
            {
                Debug.Log("序列化成功");
            }
            else
            {
                Debug.Log("序列化失败");
            }
        }
Пример #3
0
    private void ListenForData()
    {
        try
        {
            socketConnection = new TcpClient();
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);
            socketConnection.Connect(ip);

            while (socketConnection.Connected)
            {
                using (NetworkStream stream = socketConnection.GetStream())
                {
                    int length;
                    while ((length = stream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        Byte[] incomingData = new Byte[length];
                        Array.Copy(buffer, 0, incomingData, 0, length);
                        Byte[]   decodingData = GameProtocol.Encoder.Decode(incomingData);
                        Protocol protocol     = ProtocolHelper.ConvertBytesToProtocol(decodingData);
                        if (protocol != null)
                        {
                            Debug.Log("收到协议类型" + protocol.GetType().ToString());
                            MainThreadDispatcher.Instance().Enqueue(InvokeCallbackInMainThread(protocol));
                        }
                        else
                        {
                            Debug.Log("收到协议反序列化失败");
                        }
                    }
                }
            }
        }
        catch (SocketException socketException)
        {
            Debug.Log("套接字异常:" + socketException.ToString());
        }
    }