Пример #1
0
        private void ReceiveProtobufMessageThread()
        {
            while (true)
            {
                if (!this.clientSocket.Connected)
                {
                    //与服务器断开连接跳出循环
                    //Console.WriteLine("Failed to clientSocket server.");
                    UnityEngine.Debug.Log("<-----------this.clientSocket.Connected-->flase------------------>");
                    this.clientSocket.Close();
                    break;
                }

                int msglength = 0;
                try
                {
                    if (this.clientSocket.Poll(-1, SelectMode.SelectRead) || this.clientSocket.Poll(-1, SelectMode.SelectError))
                    {
                        if (!this.clientSocket.Connected)
                        {
                            //与服务器断开连接跳出循环
                            // Console.WriteLine("Failed to clientSocket server.");
                            this.clientSocket.Close();

                            break;
                        }
                        //接受数据保存至bytes当中
                        byte[] headerBytes = new byte[9];
                        //Receive方法中会一直等待服务端回发消息
                        //如果没有回发会一直在这里等着。
                        int i = this.clientSocket.Receive(headerBytes);
                        if (i <= 0)
                        {
                            clientSocket.Close();
                            break;
                        }


                        ProtoBufPackageHeader header = new ProtoBufPackageHeader();
                        int iHeaderLen = header.ReturnHeaderLen();
                        header.ReadFrom(headerBytes, 0);
                        msglength = header.MessageLength - iHeaderLen;

                        if (0 == msglength)
                        {
                            ResponsePing();
                            continue;
                        }
                    }
                    if (this.clientSocket.Poll(-1, SelectMode.SelectRead) || this.clientSocket.Poll(-1, SelectMode.SelectError))
                    {
                        byte[] msgbytes = new byte[msglength];
                        int    i        = clientSocket.Receive(msgbytes);
                        if (i <= 0)
                        {
                            clientSocket.Close();
                            break;
                        }
                        PrintRecvHexData(msgbytes);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Failed to clientSocket error" + e.ToString());
                    clientSocket.Close();
                    break;
                }
            }
        }
Пример #2
0
        private void Read()
        {
            int msglength  = 0;
            int msgNameLen = 0;

            while (true)
            {
                try
                {
                    if (this.State != TCPSocketLayer.States.Connected)
                    {
                        UnityEngine.Debug.Log("Recive Thread DisConnect-->");
                        break;
                    }

                    if (this.socketPollSleep > 0)
                    {
                        TCPSocketLayer.Sleep(this.socketPollSleep);
                    }
                    if (this.connection.Client.Poll(-1, SelectMode.SelectRead) || this.connection.Client.Poll(-1, SelectMode.SelectError))
                    {
                        byte[] headerBytes = new byte[9];
                        int    i           = this.connection.Client.Receive(headerBytes);
                        if (i <= 0)
                        {
                            this.Disconnect();
                            break;
                        }

                        ProtoBufPackageHeader header = new ProtoBufPackageHeader();
                        int iHeaderLen = header.ReturnHeaderLen();
                        header.ReadFrom(headerBytes, 0);
                        msglength  = header.MessageLength - iHeaderLen;
                        msgNameLen = header.MessageTypeLength;
                        if (0 == msglength)
                        {
                            ResponsePing();
                            continue;
                        }
                    }
                    if (this.connection.Client.Poll(-1, SelectMode.SelectRead) || this.connection.Client.Poll(-1, SelectMode.SelectError))
                    {
                        byte[] msgbytes = new byte[msglength];
                        int    i        = this.connection.Client.Receive(msgbytes);
                        if (i <= 0)
                        {
                            this.Disconnect();
                            break;
                        }
                        ByteArray byteArray = new ByteArray();
                        byteArray.WriteBytes(msgbytes);
                        UnityEngine.Debug.Log("TCP Socket 收到的字节:  " + DefaultObjectDumpFormatter.HexDump(byteArray));
                        this.HandleBinaryProtoBufData(byteArray.Bytes, byteArray.Bytes.Length, msgNameLen);
                    }
                }
                catch (Exception ex)
                {
                    this.HandleError("General error reading data from socket: " + ex.Message + " " + ex.StackTrace);
                    break;
                }
            }
        }