예제 #1
0
        public void DealMsg(ReceiveMsgData _data)
        {
            if (_data == null)
            {
                return;
            }

            byte[]     _dataBytes   = _data.receiveBytes;
            IPEndPoint receivePoint = _data.receivePoint;
            ByteBuffer _buff        = new ByteBuffer(_dataBytes);
            int        length       = _buff.ReadInt32(); //收到的消息结构实际长度

            if (length > 0)
            {
                if (length <= _data.receiveLength)
                {
                    byte[]      _bytes = _buff.ReadBytes(length);
                    MessageData data   = ProtoBufTools.DeSerialize <MessageData>(_bytes);
                    data.receivePoint = receivePoint;
                    DealMsgSwitch(data);
                }

                //除本次包内容外还有其它数据,有多余的字节,黏包,再处理一次
                int dataLength = _data.receiveLength - 4;
                if (dataLength > length)
                {
                    int leftLength = dataLength - length;
                    _data.receiveBytes  = _buff.ReadBytes(leftLength);
                    _data.receiveLength = leftLength;
                    DealMsg(_data);
                }
            }
        }
예제 #2
0
        public void DealMsg(ReceiveMessageData _data)
        {
            if (_data == null)
            {
                return;
            }
            byte[]     _dataBytes = _data.ReceiveBytes;
            ByteBuffer _buff      = new ByteBuffer(_dataBytes);
            int        length     = _buff.ReadInt32(); //收到的消息结构长度

            byte[] _bytes = _buff.ReadBytes(length);

            int dataLength = _data.ReceiveLength - 4; //去掉包长度的字节,剩下的为内容长度

            if (dataLength > length)                  //有多余的字节,黏包
            {
                int leftLength = dataLength - length;
                _data.ReceiveBytes  = _buff.ReadBytes(leftLength);
                _data.ReceiveLength = leftLength;
                DealMsg(_data);
            }
            else if (dataLength == length)
            {
                MessageData data = ProtoBufTools.DeSerialize <MessageData>(_bytes);
                data.Head.ReceivePoint = _data.ReceivePoint;
                if (DealMsgEvent != null)
                {
                    DealMsgEvent(data);
                }
            }
        }