コード例 #1
0
        public override bool recv(MessageReaderBase reader, byte[] buffer, UInt32 rpos, UInt32 len)
        {
            if (!_blowfish.isGood())
            {
                Dbg.ERROR_MSG("BlowfishFilter::recv: Dropping packet, due to invalid filter");
                return(false);
            }

            if (_packet.length() == 0 && len >= MIN_PACKET_SIZE && BitConverter.ToUInt16(buffer, (int)rpos) - 1 == len - 3)
            {
                int packLen = BitConverter.ToUInt16(buffer, (int)rpos) - 1;
                int padSize = buffer[rpos + 2];

                decrypt(buffer, (int)(rpos + 3), (int)(len - 3));

                int length = packLen - padSize;
                if (reader != null)
                {
                    reader.process(buffer, rpos + 3, (UInt32)length);
                }

                return(true);
            }

            _packet.append(buffer, rpos, len);

            while (_packet.length() > 0)
            {
                UInt32 currLen = 0;
                int    oldwpos = 0;
                if (_packLen <= 0)
                {
                    // 如果满足一个最小包则尝试解包, 否则缓存这个包待与下一个包合并然后解包
                    if (_packet.length() >= MIN_PACKET_SIZE)
                    {
                        _packLen = _packet.readUint16();
                        _padSize = _packet.readUint8();

                        _packLen -= 1;

                        if (_packet.length() > _packLen)
                        {
                            currLen      = (UInt32)(_packet.rpos + _packLen);
                            oldwpos      = _packet.wpos;
                            _packet.wpos = (int)currLen;
                        }
                        else if (_packet.length() < _packLen)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    // 如果上一次有做过解包行为但包还没有完整则继续处理
                    // 如果包是完整的下面流程会解密, 如果有多余的内容需要将其剪裁出来待与下一个包合并
                    if (_packet.length() > _packLen)
                    {
                        currLen      = (UInt32)(_packet.rpos + _packLen);
                        oldwpos      = _packet.wpos;
                        _packet.wpos = (int)currLen;
                    }
                    else if (_packet.length() < _packLen)
                    {
                        return(false);
                    }
                }

                decrypt(_packet);
                _packet.wpos -= _padSize;

                // 上面的流程能保证wpos之后不会有多余的包
                // 如果有多余的包数据会放在_recvStream
                if (reader != null)
                {
                    reader.process(_packet.data(), (UInt32)_packet.rpos, _packet.length());
                }

                if (currLen > 0)
                {
                    _packet.rpos = (int)currLen;
                    _packet.wpos = oldwpos;
                }
                else
                {
                    _packet.clear();
                }

                _packLen = 0;
                _padSize = 0;
            }

            return(true);
        }
コード例 #2
0
 public abstract bool recv(MessageReaderBase reader, byte[] buffer, UInt32 rpos, UInt32 len);