Ejemplo n.º 1
0
        private void ProcessData(ConnectControl connect)
        {
            if (connect.bufferCount < sizeof(int) + sizeof(int))
            {
                return;
            }

            //消息长度4个字节,消息类型4个字节
            Array.Copy(connect.buffer, connect.lenBytes, sizeof(int));
            //真实消息长度
            connect.msgLength = BitConverter.ToInt32(connect.lenBytes, 0) - sizeof(int);

            if (connect.bufferCount < connect.msgLength + sizeof(int))
            {
                return;
            }
            try
            {
                ProtobufTool protobuf = proto.Read(connect.buffer);

                //添加到消息集合等待处理
                lock (MessageDistributionControl.Instance.msgList)
                {
                    MessageDistributionControl.Instance.msgList.Add(new ReceiveMessageStruct(connect.id, protobuf));
                }

                //清除已处理的数据
                int count = connect.bufferCount - connect.msgLength - sizeof(int) - sizeof(int);
                Array.Copy(connect.buffer, sizeof(int) + connect.msgLength, connect.buffer, 0, count);
                connect.bufferCount = count;

                if (connect.bufferCount > 0)
                {
                    ProcessData(connect);
                }
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError(e);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 数据处理
        /// </summary>
        private void ProcessData()
        {
            //如果小于长度字节
            if (bufferCount < sizeof(int) + sizeof(int))
            {
                return;
            }

            //消息长度4个字节,消息类型4个字节
            Array.Copy(readBuffer, lenBytes, sizeof(int));

            msgLength = BitConverter.ToInt32(lenBytes, 0) - sizeof(int);

            if (bufferCount < msgLength + sizeof(int))
            {
                return;
            }

            ProtobufTool proto = protobuf.Read(readBuffer);

            Debug.Log("收到消息:" + (CommandID)proto.type);

            lock (messageDistribution.msgList)
            {
                messageDistribution.msgList.Add(new ReceiveMessageStruct(0, proto));
            }

            //清除已处理的消息
            int count = bufferCount - msgLength - sizeof(int) - sizeof(int);

            Array.Copy(readBuffer, sizeof(int) + msgLength, readBuffer, 0, count);
            bufferCount = count;

            if (bufferCount > 0)
            {
                ProcessData();
            }
        }