Beispiel #1
0
        void ParseReceivePacket()
        {
            //TODO: review here, optimized buffer
            _receiveBuffer = new byte[DEFAULT_BUFFER_SIZE];
            var socket  = _conn.socket;
            int receive = socket.Receive(_receiveBuffer);

            if (receive == 0)
            {
                return;
            }

            //---------------------------------------------------
            //TODO: review err handling
            _parser.LoadNewBuffer(_receiveBuffer, receive);
            switch (_receiveBuffer[4])
            {
            case ERROR_CODE:
                LoadError = new ErrPacket();
                LoadError.ParsePacket(_parser);
                break;

            case 0xfe:    //0x00 or 0xfe the OK packet header
            case OK_CODE:
                OkPacket = new OkPacket(_conn.IsProtocol41);
                OkPacket.ParsePacket(_parser);
                break;

            default:
                ParseResultSet();
                break;
            }
        }
Beispiel #2
0
        OkPrepareStmtPacket ParsePrepareResponse()
        {
            _receiveBuffer = new byte[DEFAULT_BUFFER_SIZE];
            var socket  = _conn.socket;
            int receive = socket.Receive(_receiveBuffer);

            if (receive == 0)
            {
                return(null);
            }
            //TODO: review err handling here
            //---------------------------------------------------
            _parser.LoadNewBuffer(_receiveBuffer, receive);
            OkPrepareStmtPacket okPreparePacket = new OkPrepareStmtPacket();

            switch (_receiveBuffer[4])
            {
            case ERROR_CODE:
                LoadError = new ErrPacket();
                LoadError.ParsePacket(_parser);
                okPreparePacket = null;
                break;

            case OK_CODE:
                okPreparePacket.ParsePacket(_parser);
                break;
            }
            return(okPreparePacket);
        }
        public void Connect()
        {
            if (State == ConnectionState.Connected)
            {
                throw new NotSupportedException("already connected");
            }

            var endpoint = new IPEndPoint(IPAddress.Parse(config.host), config.port);

            socket.Connect(endpoint);

            byte[] buffer = new byte[512];
            int    count  = socket.Receive(buffer);

            if (count > 0)
            {
                _writer.Reset();
                _parser.LoadNewBuffer(buffer, count);
                _handshake = new HandshakePacket();
                _handshake.ParsePacket(_parser);
                threadId = _handshake.threadId;

                byte[] token = MakeToken(config.password,
                                         GetScrollbleBuffer(_handshake.scrambleBuff1, _handshake.scrambleBuff2));

                _writer.IncrementPacketNumber();

                //------------------------------------------
                var authPacket = new ClientAuthenticationPacket();
                authPacket.SetValues(config.user, token, config.database, _handshake.protocol41);
                authPacket.WritePacket(_writer);

                byte[] sendBuff    = _writer.ToArray();
                byte[] receiveBuff = new byte[512];
                //-------------------------------------------
                //send data
                int sendNum    = socket.Send(sendBuff);
                int receiveNum = socket.Receive(receiveBuff);

                _parser.LoadNewBuffer(receiveBuff, receiveNum);
                if (receiveBuff[4] == 255)
                {
                    ErrPacket errPacket = new ErrPacket();
                    errPacket.ParsePacket(_parser);
                    return;
                }
                else
                {
                    OkPacket okPacket = new OkPacket(_handshake.protocol41);
                    okPacket.ParsePacket(_parser);
                }
                _writer.Reset();
                GetMaxAllowedPacket();
                _writer.SetMaxAllowedPacket(_maxPacketSize);
            }
        }
Beispiel #4
0
        bool Parse_Error_Content(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength)) //check if length is enough to parse
            {
                return(_needMoreData = true);
            }
            var errPacket = new ErrPacket(_currentHeader);

            errPacket.ParsePacketContent(reader);
            //------------------------
            _parseResult  = new MySqlErrorResult(errPacket);
            _parsingState = ResultPacketState.ShouldEnd;
            return(true);//finished
        }
Beispiel #5
0
        //----------------------------------------------------
        bool ParseErrorPacket(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return(_needMoreData = true);
            }
            var errPacket = new ErrPacket(_currentHeader);

            errPacket.ParsePacketContent(reader);
            //
            _finalResult  = new MySqlErrorResult(errPacket);
            _parsingState = PrepareResponseParseState.ShouldEnd;
            reader.Reset();
            return(true);
        }
Beispiel #6
0
        public bool ReadRow()
        {
            if (_tableHeader == null)
            {
                return(_hasSomeRow = false);
            }

            switch (_receiveBuffer[_parser.Position + 4])
            {
            case ERROR_CODE:
            {
                LoadError = new ErrPacket();
                LoadError.ParsePacket(_parser);
                return(_hasSomeRow = false);
            }

            case EOF_CODE:
            {
                EofPacket rowDataEof = ParseEOF();

                return(_hasSomeRow = false);
            }

            default:
            {
                if (_prepareContext != null)
                {
                    _lastPrepareRow.ReuseSlots();
                    _lastPrepareRow.ParsePacketHeader(_parser);

                    _receiveBuffer = CheckLimit(_lastPrepareRow.GetPacketLength(), _receiveBuffer, DEFAULT_BUFFER_SIZE);
                    _lastPrepareRow.ParsePacket(_parser);
                    CheckBeforeParseHeader(_receiveBuffer);
                }
                else
                {
                    _lastRow.ReuseSlots();
                    _lastRow.ParsePacketHeader(_parser);

                    _receiveBuffer = CheckLimit(_lastRow.GetPacketLength(), _receiveBuffer, DEFAULT_BUFFER_SIZE);
                    _lastRow.ParsePacket(_parser);
                    CheckBeforeParseHeader(_receiveBuffer);
                }
                return(_hasSomeRow = true);
            }
            }
        }
 public MySqlErrorResult(ErrPacket errPacket)
 {
     this.errPacket = errPacket;
     this.IsError   = true;
 }
Beispiel #8
0
        public void ConnectAsync(Action connHandler)
        {
            //1. socket
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _saea.SetBuffer(_sockBuffer, 0, _sockBuffer.Length);

            //2. buffer
            _connSession       = new MySqlConnectionSession(_saea, 1024, 1024);
            _saea.UserToken    = _connSession;
            _saea.AcceptSocket = socket;

            var endPoint = new IPEndPoint(IPAddress.Parse(config.host), config.port);

            //first connect
            socket.Connect(endPoint);
            _connSession.StartReceive(recv =>
            {
                //TODO: review here, don't copy,
                //we should use shared sockBuffer

                byte[] buffer = new byte[512];
                int count     = recv.BytesTransferred;
                recv.CopyTo(0, buffer, 0, recv.BytesTransferred);
                _parser.LoadNewBuffer(buffer, count);
                _handshake = new HandshakePacket();
                _handshake.ParsePacket(_parser);
                this.threadId = _handshake.threadId;

                byte[] token = MakeToken(config.password,
                                         GetScrollbleBuffer(_handshake.scrambleBuff1, _handshake.scrambleBuff2));

                _writer.Reset();
                _writer.IncrementPacketNumber();
                //------------------------------------------
                var authPacket = new ClientAuthenticationPacket();
                authPacket.SetValues(config.user, token, config.database, _handshake.protocol41);
                authPacket.WritePacket(_writer);

                //send
                //do authen
                //handle
                recv._recvAction = () =>
                {
                    byte[] sendBuff    = _writer.ToArray();
                    byte[] receiveBuff = new byte[512];
                    //-------------------------------------------

                    //send data
                    int sendNum    = socket.Send(sendBuff);
                    int receiveNum = socket.Receive(receiveBuff);

                    _parser.LoadNewBuffer(receiveBuff, receiveNum);
                    if (receiveBuff[4] == 255)
                    {
                        ErrPacket errPacket = new ErrPacket();
                        errPacket.ParsePacket(_parser);
                    }
                    else
                    {
                        OkPacket okPacket = new OkPacket(_handshake.protocol41);
                        okPacket.ParsePacket(_parser);
                    }
                    _writer.Reset();
                    GetMaxAllowedPacket();
                    if (_maxPacketSize > 0)
                    {
                        _writer.SetMaxAllowedPacket(_maxPacketSize);
                    }

                    if (connHandler != null)
                    {
                        connHandler();
                    }
                };
                return(EndReceiveState.Complete);
            });
        }
 public MySqlErrorResult(ErrPacket errPacket)
 {
     this.errPacket = errPacket;
     this.IsError = true;
 }
 //----------------------------------------------------
 bool ParseErrorPacket(MySqlStreamReader reader)
 {
     if (!reader.Ensure(_currentHeader.ContentLength))
     {
         return _needMoreData = true;
     }
     var errPacket = new ErrPacket(_currentHeader);
     errPacket.ParsePacketContent(reader);
     // 
     _finalResult = new MySqlErrorResult(errPacket);
     _parsingState = PrepareResponseParseState.ShouldEnd;
     reader.Reset();
     return true;
 }
 bool Parse_Error_Content(MySqlStreamReader reader)
 {
     if (!reader.Ensure(_currentHeader.ContentLength)) //check if length is enough to parse 
     {
         return _needMoreData = true;
     }
     var errPacket = new ErrPacket(_currentHeader);
     errPacket.ParsePacketContent(reader);
     //------------------------
     _parseResult = new MySqlErrorResult(errPacket);
     _parsingState = ResultPacketState.ShouldEnd;
     return true;//finished
 }