bool Parse_Row_Header(MySqlStreamReader reader)
        {
            if (!reader.Ensure(PACKET_HEADER_LENGTH + 1))
            {
                return _needMoreData = true;
            }

            _currentHeader = reader.ReadPacketHeader();
            if (_currentHeader.ContentLength == Packet.MAX_PACKET_LENGTH)
            {
                //need more than 1 packet for row content 
                _parsingState = ResultPacketState.Row_Content;
                return false;
            }
            else if (_currentHeader.ContentLength > Packet.MAX_PACKET_LENGTH)
            {
                throw new NotSupportedException("???");
            }
            byte packetType = reader.PeekByte();
            switch (packetType)
            {
                case ERROR_CODE:
                    _parsingState = ResultPacketState.Error_Content;
                    break;
                case EOF_CODE://0x00 or 0xfe the OK packet header
                    _parsingState = ResultPacketState.Row_EOF;
                    break;
                default:
                    _parsingState = ResultPacketState.Row_Content;
                    break;
            }
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static void Write(MySqlStreamWriter writer, string sql)
        {
            //for those who don't want to alloc an new packet
            //just write it into a stream  
            byte[] buffer = writer.GetEncodeBytes(sql.ToCharArray());
            int totalLen = buffer.Length;
            int packetCount = (totalLen / Packet.MAX_PACKET_LENGTH) + 1;

            if (packetCount <= 1)
            {
                writer.ReserveHeader();
                writer.WriteByte((byte)Command.QUERY);
                //check if we can write data in 1 packet or not 
                writer.WriteBinaryString(buffer);
                var header = new PacketHeader(writer.OnlyPacketContentLength, writer.IncrementPacketNumber());
                writer.WriteHeader(header);
            }
            else
            {
                //we need to split to multiple packet

                int currentPacketContentSize = Packet.MAX_PACKET_LENGTH;
                int pos = 0;

                for (int i = 0; i < packetCount; ++i)
                {
                    //write each packet to stream
                    writer.ReserveHeader();
                    if (i == 0)
                    {
                        //first packet
                        writer.WriteByte((byte)Command.QUERY);
                        writer.WriteBinaryString(buffer, pos, currentPacketContentSize - 1);//remove 1 query cmd
                        pos += (currentPacketContentSize - 1);

                    }
                    else if (i == packetCount - 1)
                    {
                        //last packet
                        currentPacketContentSize = totalLen - pos;
                        writer.WriteBinaryString(buffer, pos, currentPacketContentSize);
                    }
                    else
                    {
                        writer.WriteBinaryString(buffer, pos, currentPacketContentSize);
                        pos += (currentPacketContentSize);
                    }

                    //check if we can write data in 1 packet or not                   
                    var header = new PacketHeader((uint)currentPacketContentSize, writer.IncrementPacketNumber());
                    writer.WriteHeader(header);
                }

            }

        }
 public static PacketHeader Write(MySqlStreamWriter writer, uint stmtId)
 {
     writer.ReserveHeader();
     writer.WriteByte((byte)Command.STMT_RESET);
     writer.WriteUnsigned4(stmtId);
     var _header = new PacketHeader(writer.OnlyPacketContentLength, writer.IncrementPacketNumber());
     writer.WriteHeader(_header);
     return _header;
 }
 public ErrPacket(PacketHeader header) : base(header) { }
 public Packet(PacketHeader header)
 {
     //ensure that we have header before parse packet body 
     this._header = header;
 }
 public static PacketHeader Write(MySqlStreamWriter writer, uint stmtId)
 {
     //for those who don't want to alloc an new packet
     //just write it into a stream
     writer.ReserveHeader();
     writer.WriteByte((byte)Command.STMT_CLOSE);
     writer.WriteUnsigned4(stmtId);
     var h = new PacketHeader(writer.OnlyPacketContentLength, writer.IncrementPacketNumber());
     writer.WriteHeader(h);
     return h;
 }
 public HandshakePacket(PacketHeader header)
     : base(header)
 {
 }
 public static PacketHeader Write(MySqlStreamWriter writer, string sql)
 {
     //for those who don't want to alloc an new packet
     //just write it into a stream
     writer.ReserveHeader();
     writer.WriteByte((byte)Command.STMT_PREPARE);
     writer.WriteString(sql);
     var h = new PacketHeader(writer.OnlyPacketContentLength, writer.IncrementPacketNumber());
     writer.WriteHeader(h);
     return h;
 }
        public void WriteHeader(PacketHeader header)
        {
            //  var packets  = Math.floor(this._buffer.length / MAX_PACKET_LENGTH) + 1;
            //  var buffer   = this._buffer;
            //int maxPacketLength = MAX_PACKET_LENGTH;

            long totalPacketLength = OnlyPacketContentLength + 4;
#if DEBUG
            SharpConnect.Internal.dbugConsole.WriteLine("Current Packet Length = " + totalPacketLength);
#endif
            //TODO: review MAX_PACKET_LENGTH here ****
            //it should be 
            //int packetCount = (int)((totalPacketLength - 4) / _maxAllowedLength) + 1;//-4 bytes of reserve header
            if (totalPacketLength > _serverMaxDataLength)
            {
                throw new Exception("Packet for query is too larger than MAX_ALLOWED_LENGTH");
            }
            if (header.ContentLength > Packet.MAX_PACKET_LENGTH)
            {
                throw new Exception("Packet for query is too larger than MAX_ALLOWED_LENGTH");
            }
            WriteEncodedUnsignedNumber0_3(_headerBuffer, header.ContentLength);
            _headerBuffer[3] = header.PacketNumber;
            _writer.RewindWriteAndJumpBack(_headerBuffer, (int)_startPacketPosition);
        }
 public ClientAuthenticationPacket(PacketHeader header)
     : base(header)
 {
     SetDefaultValues();
 }
        public PacketHeader ReadPacketHeader()
        {
#if DEBUG
            dbugBreakOnMonitorData();
#endif
            _packetHeaderStartAt = _stream.Position;

            PacketHeader header = new PacketHeader(U3(), ReadByte());
#if DEBUG
            if (header.PacketNumber > debugLastPacketNum + 1)
            {
                SharpConnect.Internal.dbugConsole.WriteLine("header.PacketNumber : " + header.PacketNumber + " > lastPacketNumber :" + debugLastPacketNum);
            }
            else if (header.PacketNumber > 0 && header.PacketNumber < debugLastPacketNum && debugLastPacketNum != 2)
            {
                //?
            }
            SharpConnect.Internal.dbugConsole.WriteLine("h>> " + header.PacketNumber + ":" + debugLastPacketNum + ":" + _packetHeaderStartAt);
            debugLastPacketNum = header.PacketNumber;
#endif
            _packetLength = header.ContentLength + 4;
            return header;
        }
 //----------------------------------------------------
 bool Parse_ColumnField_Header(MySqlStreamReader reader)
 {
     if (!reader.Ensure(PACKET_HEADER_LENGTH + 1))
     {
         return _needMoreData = true;
     }
     _currentHeader = reader.ReadPacketHeader();
     byte type = reader.PeekByte();//1
     switch (type)
     {
         case ERROR_CODE:
             _parsingState = PrepareResponseParseState.Error_Content;
             break;
         case EOF_CODE:
             //?
             _parsingState = PrepareResponseParseState.ColumnField_EOF;
             break;
         default:
             _parsingState = PrepareResponseParseState.ColumnField_Content;
             break;
     }
     return false;
 }
 bool Parse_PrepareOk_Header(MySqlStreamReader reader)
 {
     //this is first step ***
     if (!reader.Ensure(PACKET_HEADER_LENGTH + 1))
     {
         return _needMoreData = true;
     }
     _currentHeader = reader.ReadPacketHeader();
     byte type = reader.PeekByte();
     switch (type)
     {
         case ERROR_CODE:
             _parsingState = PrepareResponseParseState.Error_Content;
             break;
         case EOF_CODE:
         case OK_CODE:
             _parsingState = PrepareResponseParseState.OkPrepare_Content;
             break;
         default:
             throw new NotSupportedException("Packet type don't match!!");
     }
     return false;
 }
 //const byte QUIT_CMD = (byte)Command.QUIT;//0x01 
 public ComQuitPacket(PacketHeader header)
     : base(header) { }
 public OkPacket(PacketHeader header, bool protocol41)
     : base(header)
 {
     _protocol41 = protocol41;
 }
 public ComPrepareStatementPacket(PacketHeader header, string sql)
     : base(header)
 {
     _sql = sql;
 }
 public OkPrepareStmtPacket(PacketHeader header)
     : base(header) { }
 public ComExecPrepareStmtPacket(PacketHeader header, uint statementId, MyStructData[] filledValues)
     : base(header)
 {
     _statementId = statementId;
     _prepareValues = filledValues;
 }
 public ResultSetHeaderPacket(PacketHeader header) : base(header) { }
        public static void Write(MySqlStreamWriter writer, uint stmtId, MyStructData[] _prepareValues)
        {
            //for those who don't want to alloc an new packet
            //just write it into a stream
            writer.ReserveHeader();
            writer.WriteByte((byte)Command.STMT_EXECUTE);
            writer.WriteUnsignedNumber(4, stmtId);
            writer.WriteByte((byte)CursorFlags.CURSOR_TYPE_NO_CURSOR);

            writer.WriteUnsignedNumber(4, 1);//iteration-count, always 1
                                             //write NULL-bitmap, length: (num-params+7)/8 
            MyStructData[] fillValues = _prepareValues;
            int paramNum = _prepareValues.Length;
            if (paramNum > 0)
            {
                uint bitmap = 0;
                uint bitValue = 1;
                for (int i = 0; i < paramNum; i++)
                {
                    MySqlDataType dataType = _prepareValues[i].type;
                    if (dataType == MySqlDataType.NULL)
                    {
                        bitmap += bitValue;
                    }
                    bitValue <<= 1; //shift to left 1 bit
                    if (bitValue == 256)
                    {
                        //end of 8 bits
                        //just store data
                        writer.WriteUnsigned1(bitmap);
                        bitmap = 0;
                        bitValue = 1;
                    }
                }
                if (bitValue != 1)
                {
                    writer.WriteUnsigned1(bitmap);
                }
            }
            //new-params-bound - flag
            writer.WriteByte(1);
            //-------------------------------------------------------
            //data types
            for (int i = 0; i < paramNum; i++)
            {
                writer.WriteUnsignedNumber(2, (byte)_prepareValues[i].type);
            }
            //--------------------------------------
            //actual data
            //--------------------------------------
            //stream of data may large than 1 packet
            var tempSingleValueHolder = new TempSingleValueHolder();
            tempSingleValueHolder.headerLenBuffer = new byte[9];
            tempSingleValueHolder.generalContent = new byte[16];
            for (int i = 0; i < paramNum; i++)
            {
                bool isComplete = WriteValueByType(writer, ref _prepareValues[i], ref tempSingleValueHolder);
                var header = new PacketHeader(writer.OnlyPacketContentLength, writer.IncrementPacketNumber());
                writer.WriteHeader(header);
                //--------------------------------------------------------------------------------------------------
                while (!isComplete)
                {
                    //write until complete
                    tempSingleValueHolder.round++;
                    writer.ReserveHeader();
                    isComplete = WriteValueByType(writer, ref _prepareValues[i], ref tempSingleValueHolder);
                    header = new PacketHeader(writer.OnlyPacketContentLength, writer.IncrementPacketNumber());
                    writer.WriteHeader(header);
                }
                //reset
                tempSingleValueHolder.Reset();
            }
            //--------------------------------------

        }
 public DataRowPacket(PacketHeader header, byte[] rowDataBuffer)
     : base(header)
 {
     this._rowDataBuffer = rowDataBuffer;
 }
 public ComStmtResetPacket(PacketHeader header, uint statementId)
     : base(header)
 {
     _statementId = statementId;
 }
 public override void WritePacket(MySqlStreamWriter writer)
 {
     writer.ReserveHeader();//allocate header
     if (protocol41)
     {
         writer.WriteUnsigned4(clientFlags);
         writer.WriteUnsigned4(maxPacketSize);
         writer.WriteUnsigned1(charsetNumber);
         writer.WriteFiller(23);
         writer.WriteNullTerminatedString(user);
         writer.WriteLengthCodedBuffer(scrambleBuff);
         writer.WriteNullTerminatedString(database);
     }
     else
     {
         writer.WriteUnsigned2(clientFlags);
         writer.WriteUnsigned3(maxPacketSize);
         writer.WriteNullTerminatedString(user);
         writer.WriteBuffer(scrambleBuff);
         if (database != null && database.Length > 0)
         {
             writer.WriteFiller(1);
             writer.WriteBuffer(Encoding.ASCII.GetBytes(database));
         }
     }
     _header = new PacketHeader(writer.OnlyPacketContentLength, writer.IncrementPacketNumber());
     writer.WriteHeader(_header);
 }
 public ComStmtSendLongDataPacket(PacketHeader header, uint statement_id, int param_id, MyStructData data)
     : base(header)
 {
     _statement_id = statement_id;
     _param_id = param_id;
     _data = data;
 }
 public ComQueryPacket(PacketHeader header, string sql)
     : base(header)
 {
     _sql = sql;
 }
 public FieldPacket(PacketHeader header, bool protocol41)
     : base(header)
 {
     this.protocol41 = protocol41;
 }
        bool Parse_Field_Header(MySqlStreamReader reader)
        {
            if (!reader.Ensure(PACKET_HEADER_LENGTH + 1)) //check if length is enough to parse 
            {
                return _needMoreData = true;
            }

            _currentHeader = reader.ReadPacketHeader();
            byte packetType = reader.PeekByte();
            switch (packetType)
            {
                case ERROR_CODE:
                    _parsingState = ResultPacketState.Error_Content;
                    break;
                case EOF_CODE:
                case OK_CODE:
                    _parsingState = ResultPacketState.Field_EOF;
                    break;
                default:
                    //next state => field content of this field
                    _parsingState = ResultPacketState.Field_Content;
                    break;
            }
            return false;
        }