Ejemplo n.º 1
0
        /// <summary>
        /// ExecuteCommand does the work of writing the actual command bytes to the writer
        /// We break it out into a function since it is used in several places besides query
        /// </summary>
        /// <param name="cmd">The cmd that we are sending</param>
        /// <param name="bytes">The bytes of the command, can be null</param>
        /// <param name="length">The number of bytes to send</param>
        private void ExecuteCommand(DBCmd cmd, byte[] bytes, int length)
        {
            Debug.Assert(length == 0 || bytes != null);

            try
            {
                stream.StartOutput((ulong)length + 1, true);
                stream.WriteByte((byte)cmd);
                if (length > 0)
                {
                    stream.Write(bytes, 0, length);
                }
                stream.Flush();
            }
            catch (MySqlException ex)
            {
                if (ex.IsFatal)
                {
                    isOpen = false;
                    Close();
                }
                throw;
            }
        }
Ejemplo n.º 2
0
        public override void Open()
        {
            base.Open();

            // connect to one of our specified hosts
            try
            {
#if !CF
                if (Settings.ConnectionProtocol == MySqlConnectionProtocol.SharedMemory)
                {
                    SharedMemoryStream str = new SharedMemoryStream(Settings.SharedMemoryName);
                    str.Open(Settings.ConnectionTimeout);
                    baseStream = str;
                }
                else
                {
#endif
                string pipeName = Settings.PipeName;
                if (Settings.ConnectionProtocol != MySqlConnectionProtocol.NamedPipe)
                {
                    pipeName = null;
                }
                StreamCreator sc = new StreamCreator(Settings.Server, Settings.Port, pipeName);
                baseStream = sc.GetStream(Settings.ConnectionTimeout);
#if !CF
            }
#endif
                if (baseStream == null)
                {
                    throw new Exception();
                }
            }
            catch (Exception ex)
            {
                throw new MySqlException(Resources.UnableToConnectToHost,
                                         (int)MySqlErrorCode.UnableToConnectToHost, ex);
            }

            if (baseStream == null)
            {
                throw new MySqlException("Unable to connect to any of the specified MySQL hosts");
            }

            int maxSinglePacket = 255 * 255 * 255;
            stream = new MySqlStream(baseStream, encoding, false);

            // read off the welcome packet and parse out it's values
            stream.OpenPacket();
            protocol = stream.ReadByte();
            string versionString = stream.ReadString();
            version        = DBVersion.Parse(versionString);
            threadId       = stream.ReadInteger(4);
            encryptionSeed = stream.ReadString();

            if (version.isAtLeast(4, 0, 8))
            {
                maxSinglePacket = (256 * 256 * 256) - 1;
            }

            // read in Server capabilities if they are provided
            serverCaps = 0;
            if (stream.HasMoreData)
            {
                serverCaps = (ClientFlags)stream.ReadInteger(2);
            }
            if (version.isAtLeast(4, 1, 1))
            {
                /* New protocol with 16 bytes to describe server characteristics */
                serverCharSetIndex = stream.ReadInteger(1);

                serverStatus = (ServerStatusFlags)stream.ReadInteger(2);
                stream.SkipBytes(13);
                string seedPart2 = stream.ReadString();
                encryptionSeed += seedPart2;
            }

            // based on our settings, set our connection flags
            SetConnectionFlags();

            stream.StartOutput(0, false);
            stream.WriteInteger((int)connectionFlags,
                                version.isAtLeast(4, 1, 0) ? 4 : 2);

#if !CF
            if (connectionString.UseSSL && (serverCaps & ClientFlags.SSL) != 0)
            {
                stream.Flush();

                StartSSL();

                stream.StartOutput(0, false);
                stream.WriteInteger((int)connectionFlags,
                                    version.isAtLeast(4, 1, 0) ? 4 : 2);
            }
#endif

            stream.WriteInteger(maxSinglePacket,
                                version.isAtLeast(4, 1, 0) ? 4 : 3);

            if (version.isAtLeast(4, 1, 1))
            {
                stream.WriteByte(8);
                stream.Write(new byte[23]);
            }

            Authenticate();

            // if we are using compression, then we use our CompressedStream class
            // to hide the ugliness of managing the compression
            if ((connectionFlags & ClientFlags.COMPRESS) != 0)
            {
                stream = new MySqlStream(baseStream, encoding, true);
            }

            // give our stream the server version we are connected to.
            // We may have some fields that are read differently based
            // on the version of the server we are connected to.
            stream.Version      = version;
            stream.MaxBlockSize = maxSinglePacket;

            isOpen = true;
        }
Ejemplo n.º 3
0
        public override void Open()
        {
            base.Open();

            // connect to one of our specified hosts
            try
            {
#if !CF
                if (Settings.ConnectionProtocol == MySqlConnectionProtocol.SharedMemory)
                {
                    SharedMemoryStream str = new SharedMemoryStream(Settings.SharedMemoryName);
                    str.Open(Settings.ConnectionTimeout);
                    baseStream = str;
                }
                else
                {
#endif
                    string pipeName = Settings.PipeName;
                    if (Settings.ConnectionProtocol != MySqlConnectionProtocol.NamedPipe)
                        pipeName = null;
                    StreamCreator sc = new StreamCreator(Settings.Server, Settings.Port, pipeName);
                    baseStream = sc.GetStream(Settings.ConnectionTimeout);
#if !CF
                }
#endif
                if (baseStream == null)
                    throw new Exception();
            }
            catch (Exception ex)
            {
                throw new MySqlException(Resources.UnableToConnectToHost, 
                    (int) MySqlErrorCode.UnableToConnectToHost, ex);
            }

            if (baseStream == null)
                throw new MySqlException("Unable to connect to any of the specified MySQL hosts");

            int maxSinglePacket = 255*255*255;
            stream = new MySqlStream(baseStream, encoding, false);

            // read off the welcome packet and parse out it's values
            stream.OpenPacket();
            protocol = stream.ReadByte();
            string versionString = stream.ReadString();
            version = DBVersion.Parse(versionString);
            threadId = stream.ReadInteger(4);
            encryptionSeed = stream.ReadString();

            if (version.isAtLeast(4, 0, 8))
                maxSinglePacket = (256*256*256) - 1;

            // read in Server capabilities if they are provided
            serverCaps = 0;
            if (stream.HasMoreData)
                serverCaps = (ClientFlags) stream.ReadInteger(2);
            if (version.isAtLeast(4, 1, 1))
            {
                /* New protocol with 16 bytes to describe server characteristics */
                serverCharSetIndex = stream.ReadInteger(1);

                serverStatus = (ServerStatusFlags) stream.ReadInteger(2);
                stream.SkipBytes(13);
                string seedPart2 = stream.ReadString();
                encryptionSeed += seedPart2;
            }

            // based on our settings, set our connection flags
            SetConnectionFlags();

            stream.StartOutput(0, false);
            stream.WriteInteger((int) connectionFlags,
                                version.isAtLeast(4, 1, 0) ? 4 : 2);

#if !CF
            if (connectionString.UseSSL && (serverCaps & ClientFlags.SSL) != 0)
            {
                stream.Flush();

                StartSSL();

                stream.StartOutput(0, false);
                stream.WriteInteger((int) connectionFlags,
                                    version.isAtLeast(4, 1, 0) ? 4 : 2);
            }
#endif

            stream.WriteInteger(maxSinglePacket,
                                version.isAtLeast(4, 1, 0) ? 4 : 3);

            if (version.isAtLeast(4, 1, 1))
            {
                stream.WriteByte(8);
                stream.Write(new byte[23]);
            }

            Authenticate();

            // if we are using compression, then we use our CompressedStream class
            // to hide the ugliness of managing the compression
            if ((connectionFlags & ClientFlags.COMPRESS) != 0)
                stream = new MySqlStream(baseStream, encoding, true);

            // give our stream the server version we are connected to.  
            // We may have some fields that are read differently based 
            // on the version of the server we are connected to.
            stream.Version = version;
            stream.MaxBlockSize = maxSinglePacket;

            isOpen = true;
        }