Beispiel #1
0
        void StoreBuffer(MySqlStreamReader reader, int length)
        {
            if (ms == null)
            {
                ms = new MemoryStream();
                //buffer data to this
            }
            ms.Write(reader.ReadBuffer(length), 0, length);

            //reader.SkipForward(length);
            // throw new NotSupportedException();
            ////TODO: review buffer mx here ****
            //byte[] dataTemp = _mysqlStreamReader.ReadBuffer((int)length);
            //int existingLargeDataBufferLen = (largeDataBuffer == null) ?
            //    0 :
            //    largeDataBuffer.Length;
            //if (existingLargeDataBufferLen > 0)
            //{
            //    //merge ...
            //    byte[] newData = new byte[existingLargeDataBufferLen + dataTemp.Length];
            //    Buffer.BlockCopy(largeDataBuffer, 0, newData, 0, largeDataBuffer.Length);
            //    Buffer.BlockCopy(dataTemp, 0, newData, largeDataBuffer.Length, dataTemp.Length);
            //    largeDataBuffer = newData;
            //}
            //else
            //{
            //    largeDataBuffer = dataTemp;
            //}
        }
        public override void ParsePacketContent(MySqlStreamReader r)
        {

            if (protocol41)
            {
                catalog = r.ReadLengthCodedString();//3,100,101,102,103  (always "def")
                schema = r.ReadLengthCodedString();
                table = r.ReadLengthCodedString();
                orgTable = r.ReadLengthCodedString();
                name = r.ReadLengthCodedString();
                orgName = r.ReadLengthCodedString();

                //next_length (lenenc_int) -- length of the following fields (always 0x0c)  ***
                uint lengthCodedNumber = r.ReadLengthCodedNumber();
                if (lengthCodedNumber != 0x0c)
                {

                    //var err  = new TypeError('Received invalid field length');
                    //err.code = 'PARSER_INVALID_FIELD_LENGTH';
                    //throw err;
#if DEBUG
                    if (lengthCodedNumber == 0)
                    {
                        //error
                        //this package is error packet 
                        //server may send the correct one?
                        dbugFailure = true;
                        return;
                    }
#endif
                    throw new Exception("Received invalid field length");
                }

                charsetNr = r.U2();//2
                maxLengthOfField = r.U4();//4
                columnType = r.ReadByte();//1
                flags = r.U2();//2
                maxShownDecimalDigits = r.ReadByte();
                filler = r.ReadBuffer(2);
                if (filler[0] != 0x0 || filler[1] != 0x0)
                {
                    //var err  = new TypeError('Received invalid filler');
                    //err.code = 'PARSER_INVALID_FILLER';
                    //throw err;
                    throw new Exception("Received invalid filler");
                }
                // parsed flags
                //this.zeroFill = (this.flags & 0x0040 ? true : false);
                zeroFill = ((flags & 0x0040) == 0x0040 ? true : false);
                if (r.ReachedPacketEnd())
                {
                    return;
                }
                //----
                //if command was COM_FIELD_LIST {
                //   lenenc_int length of default- values
                //string[$len]   default values
                //}
                strDefault = r.ReadLengthCodedString();
            }
            else
            {
                table = r.ReadLengthCodedString();
                name = r.ReadLengthCodedString();
                maxLengthOfField = r.ReadUnsigedNumber(r.ReadByte());
                columnType = (int)r.ReadUnsigedNumber(r.ReadByte());
            }
        }
        public override void ParsePacketContent(MySqlStreamReader r)
        {

            if (protocol41)
            {
                clientFlags = r.U4(); //4
                maxPacketSize = r.U4(); //4 
                charsetNumber = r.ReadByte();
                r.ReadFiller(23);
                user = r.ReadNullTerminatedString();
                scrambleBuff = r.ReadLengthCodedBuffer();
                database = r.ReadNullTerminatedString();
            }
            else
            {
                clientFlags = r.U2();//2
                maxPacketSize = r.U3();//3
                user = r.ReadNullTerminatedString();
                scrambleBuff = r.ReadBuffer(8);
                database = r.ReadLengthCodedString();
            }
        }
        public override void ParsePacketContent(MySqlStreamReader r)
        {
            //we already have header ***
            protocolVersion = r.ReadByte();//1
            serverVertion = r.ReadNullTerminatedString();
            threadId = r.U4();//4
            scrambleBuff1 = r.ReadBuffer(8);
            filler1 = r.ReadByte();
            serverCapabilities1 = r.U2();//2
            serverLanguage = r.ReadByte();
            serverStatus = r.U2();//2
            protocol41 = (serverCapabilities1 & (1 << 9)) > 0;
            if (protocol41)
            {
                serverCapabilities2 = r.U2();
                scrambleLength = r.ReadByte();
                filler2 = r.ReadBuffer(10);
                scrambleBuff2 = r.ReadBuffer(12);
                filler3 = r.ReadByte();
            }
            else
            {
                filler2 = r.ReadBuffer(13);
            }

            if (r.ReadPosition == r.CurrentInputLength)
            {
                return;
            }

            pluginData = r.ReadPacketTerminatedString();
            var last = pluginData.Length - 1;
            if (pluginData[last] == '\0')
            {
                pluginData = pluginData.Substring(0, last);
            }
        }
Beispiel #5
0
        bool Parse_Row_Content(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return(_needMoreData = true);
            }
            if (_currentHeader.ContentLength > Packet.MAX_PACKET_LENGTH)
            {
                throw new NotSupportedException("???");
            }
            else if (_currentHeader.ContentLength >= Packet.MAX_PACKET_LENGTH)
            {
                //can't complete in this round
                //so store data into temp extra large buffer
                //and set isLargeData= true
                StoreBuffer(reader, (int)_currentHeader.ContentLength);
                isLargeData = true;
                //we still in the row content state
                _parsingState = ResultPacketState.Row_Header;
                return(false);
            }
            //--------------------------------

            if (_generateResultMode)
            {
                //this is normal mode (opposite to JustFlushOutMode)
                //in this mode we parse packet content
                //and add it to the output rows
                //----------------------------------
                //in  this version row buffer len must < int.MaxLength
                if (_currentHeader.ContentLength > int.MaxValue)
                {
                    throw new NotSupportedException("not support this length");
                }
                //------------------------------------
                if (isLargeData)
                {
                    if (ms == null)
                    {                                      //it should not be null here
                        throw new NotSupportedException(); //?
                    }
                    ms.Write(reader.ReadBuffer((int)_currentHeader.ContentLength), 0,
                             (int)_currentHeader.ContentLength);
                    _rows.Add(new DataRowPacket(_currentHeader, ms.ToArray()));

#if NET20
                    ms.Close();
#endif
                    ms.Dispose();
                    ms = null;

                    isLargeData = false; //reset
                }
                else
                {
                    _rows.Add(new DataRowPacket(_currentHeader,
                                                reader.ReadBuffer((int)_currentHeader.ContentLength)));
                }
            }
            else
            {
                //just flush data***
                //not create data row
                if (_currentHeader.ContentLength > int.MaxValue)
                {
                    throw new Exception("not support content length> int.MaxValue");
                }
                reader.SkipForward((int)_currentHeader.ContentLength);
            }
            //-----------------------------------------------------------------------
            //after this row, next state = next row header
            _parsingState = ResultPacketState.Row_Header;
            return(false);
        }
        void StoreBuffer(MySqlStreamReader reader, int length)
        {
            if (ms == null)
            {
                ms = new MemoryStream();
                //buffer data to this 
            }
            ms.Write(reader.ReadBuffer(length), 0, length);

            //reader.SkipForward(length);
            // throw new NotSupportedException();
            ////TODO: review buffer mx here ****
            //byte[] dataTemp = _mysqlStreamReader.ReadBuffer((int)length);
            //int existingLargeDataBufferLen = (largeDataBuffer == null) ?
            //    0 :
            //    largeDataBuffer.Length;
            //if (existingLargeDataBufferLen > 0)
            //{
            //    //merge ...
            //    byte[] newData = new byte[existingLargeDataBufferLen + dataTemp.Length];
            //    Buffer.BlockCopy(largeDataBuffer, 0, newData, 0, largeDataBuffer.Length);
            //    Buffer.BlockCopy(dataTemp, 0, newData, largeDataBuffer.Length, dataTemp.Length);
            //    largeDataBuffer = newData;
            //}
            //else
            //{
            //    largeDataBuffer = dataTemp;
            //}
        }
        bool Parse_Row_Content(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return _needMoreData = true;
            }
            if (_currentHeader.ContentLength > Packet.MAX_PACKET_LENGTH)
            {
                throw new NotSupportedException("???");
            }
            else if (_currentHeader.ContentLength >= Packet.MAX_PACKET_LENGTH)
            {
                //can't complete in this round 
                //so store data into temp extra large buffer 
                //and set isLargeData= true
                StoreBuffer(reader, (int)_currentHeader.ContentLength);
                isLargeData = true;
                //we still in the row content state
                _parsingState = ResultPacketState.Row_Header;
                return false;
            }
            //--------------------------------       

            if (_generateResultMode)
            {
                //this is normal mode (opposite to JustFlushOutMode)
                //in this mode we parse packet content 
                //and add it to the output rows 
                //----------------------------------
                //in  this version row buffer len must < int.MaxLength
                if (_currentHeader.ContentLength > int.MaxValue)
                {
                    throw new NotSupportedException("not support this length");
                }
                //------------------------------------  
                if (isLargeData)
                {
                    if (ms == null)
                    {   //it should not be null here
                        throw new NotSupportedException();//?   
                    }
                    ms.Write(reader.ReadBuffer((int)_currentHeader.ContentLength), 0,
                        (int)_currentHeader.ContentLength);
                    _rows.Add(new DataRowPacket(_currentHeader, ms.ToArray()));

                    ms.Close();
                    ms.Dispose();
                    ms = null;

                    isLargeData = false; //reset
                }
                else
                {
                    _rows.Add(new DataRowPacket(_currentHeader,
                    reader.ReadBuffer((int)_currentHeader.ContentLength)));
                }

            }
            else
            {
                //just flush data*** 
                //not create data row
                if (_currentHeader.ContentLength > int.MaxValue)
                {
                    throw new Exception("not support content length> int.MaxValue");
                }
                reader.SkipForward((int)_currentHeader.ContentLength);
            }
            //-----------------------------------------------------------------------
            //after this row, next state = next row header
            _parsingState = ResultPacketState.Row_Header;
            return false;
        }