コード例 #1
0
        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);
            }
        }
コード例 #2
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);
            });
        }
コード例 #3
0
 /// <summary>
 /// open connection, +/- blocking
 /// </summary>
 /// <param name="nextAction"></param>
 public void Connect(Action nextAction = null)
 {
     if (State == ConnectionState.Connected)
     {
         throw new NotSupportedException("already connected");
     }
     _mysqlParserMx.UseConnectionParser();
     this._workingState = WorkingState.Rest;
     //--------------
     var endpoint = new IPEndPoint(IPAddress.Parse(config.host), config.port);
     socket.Connect(endpoint);
     this._workingState = WorkingState.Rest;
     //--------------
     //**start listen after connect
     InitWait();
     StartReceive(mysql_result =>
     {
         //when complete1
         //create handshake packet and send back
         var handshakeResult = mysql_result as MySqlHandshakeResult;
         if (handshakeResult == null)
         {
             //error
             throw new Exception("err1");
         }
         HandshakePacket handshake_packet = handshakeResult.packet;
         this.threadId = handshake_packet.threadId;
         byte[] token = MakeToken(config.password,
            GetScrollbleBuffer(handshake_packet.scrambleBuff1, handshake_packet.scrambleBuff2));
         _writer.IncrementPacketNumber();
         //----------------------------
         //send authen packet to the server
         var authPacket = new ClientAuthenticationPacket(new PacketHeader());
         authPacket.SetValues(config.user, token, config.database, isProtocol41 = handshake_packet.protocol41);
         authPacket.WritePacket(_writer);
         byte[] sendBuff = _writer.ToArray();
         _writer.Reset();
         //------------------------------------
         //switch to result packet parser  
         _mysqlParserMx.SetProtocol41(isProtocol41);
         _mysqlParserMx.UseResultParser();
         //------------------------------------
         StartSend(sendBuff, 0, sendBuff.Length, () =>
         {
             StartReceive(mysql_result2 =>
             {
                 var ok = mysql_result2 as MySqlOkResult;
                 if (ok != null)
                 {
                     this._workingState = WorkingState.Rest;
                 }
                 else
                 {
                     //TODO: review here
                     //error  
                     _workingState = WorkingState.Error;
                 }
                 //set max allow of the server ***
                 //todo set max allow packet***
                 UnWait();
                 if (nextAction != null)
                 {
                     nextAction();
                 }
             });
         });
     });
     if (nextAction == null)
     {
         //block ....
         Wait();
     }
     else
     {
         UnWait();
     }
 }
コード例 #4
0
        /// <summary>
        /// open connection, +/- blocking
        /// </summary>
        /// <param name="nextAction"></param>
        public void Connect(Action nextAction = null)
        {
            if (State == ConnectionState.Connected)
            {
                throw new NotSupportedException("already connected");
            }
            _mysqlParserMx.UseConnectionParser();
            this._workingState = WorkingState.Rest;
            //--------------
            var endpoint = new IPEndPoint(IPAddress.Parse(config.host), config.port);

            socket.Connect(endpoint);
            this._workingState = WorkingState.Rest;
            //--------------
            //**start listen after connect
            InitWait();
            StartReceive(mysql_result =>
            {
                //when complete1
                //create handshake packet and send back
                var handshakeResult = mysql_result as MySqlHandshakeResult;
                if (handshakeResult == null)
                {
                    //error
                    throw new Exception("err1");
                }
                HandshakePacket handshake_packet = handshakeResult.packet;
                this.threadId = handshake_packet.threadId;

                // https://dev.mysql.com/doc/internals/en/sha256.html

                byte[] token = string.IsNullOrEmpty(config.password) ?
                               /*1*/ new byte[0] : //Empty passwords are not hashed, but sent as empty string.

                               /* or 2*/ MakeToken(config.password, GetScrollbleBuffer(
                                                       handshake_packet.scrambleBuff1,
                                                       handshake_packet.scrambleBuff2));

                _writer.IncrementPacketNumber();
                //----------------------------
                //send authen packet to the server
                var authPacket = new ClientAuthenticationPacket(new PacketHeader());
                authPacket.SetValues(config.user, token, config.database, isProtocol41 = handshake_packet.protocol41);
                authPacket.WritePacket(_writer);
                byte[] sendBuff = _writer.ToArray();
                _writer.Reset();
                //------------------------------------
                //switch to result packet parser
                _mysqlParserMx.SetProtocol41(isProtocol41);
                _mysqlParserMx.UseResultParser();
                //------------------------------------
                StartSend(sendBuff, 0, sendBuff.Length, () =>
                {
                    StartReceive(mysql_result2 =>
                    {
                        var ok = mysql_result2 as MySqlOkResult;
                        if (ok != null)
                        {
                            this._workingState = WorkingState.Rest;
                        }
                        else
                        {
                            //TODO: review here
                            //error
                            _workingState = WorkingState.Error;
                        }
                        //set max allow of the server ***
                        //todo set max allow packet***
                        UnWait();
                        if (nextAction != null)
                        {
                            nextAction();
                        }
                    });
                });
            });
            if (nextAction == null)
            {
                //block ....
                Wait();
            }
            else
            {
                UnWait();
            }
        }