private void GetConnectNumber(int timeOut)
        {
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            IntPtr         handle         = SharedMemoryStream.OpenEvent(1048578u, false, this.memoryName + "_CONNECT_REQUEST");

            autoResetEvent.Handle = (handle);
            AutoResetEvent autoResetEvent2 = new AutoResetEvent(false);

            handle = SharedMemoryStream.OpenEvent(1048578u, false, this.memoryName + "_CONNECT_ANSWER");
            autoResetEvent2.Handle = (handle);
            IntPtr hFileMappingObject = SharedMemoryStream.OpenFileMapping(2u, false, this.memoryName + "_CONNECT_DATA");
            IntPtr intPtr             = SharedMemoryStream.MapViewOfFile(hFileMappingObject, 2u, 0u, 0u, (IntPtr)4);

            if (!autoResetEvent.Set())
            {
                throw new MySqlException("Failed to open shared memory connection ");
            }
            autoResetEvent2.WaitOne(timeOut * 1000, false);
            this.connectNumber = Marshal.ReadInt32(intPtr);
        }
        private void SetupEvents()
        {
            string text = this.memoryName + "_" + this.connectNumber;

            this.dataMap     = SharedMemoryStream.OpenFileMapping(2u, false, text + "_DATA");
            this.dataView    = SharedMemoryStream.MapViewOfFile(this.dataMap, 2u, 0u, 0u, (IntPtr)16004);
            this.serverWrote = new AutoResetEvent(false);
            IntPtr handle = SharedMemoryStream.OpenEvent(1048578u, false, text + "_SERVER_WROTE");

            this.serverWrote.Handle = handle;
            this.serverRead         = new AutoResetEvent(false);
            handle = SharedMemoryStream.OpenEvent(1048578u, false, text + "_SERVER_READ");
            this.serverRead.Handle = handle;
            this.clientWrote       = new AutoResetEvent(false);
            handle = SharedMemoryStream.OpenEvent(1048578u, false, text + "_CLIENT_WROTE");
            this.clientWrote.Handle = handle;
            this.clientRead         = new AutoResetEvent(false);
            handle = SharedMemoryStream.OpenEvent(1048578u, false, text + "_CLIENT_READ");
            this.clientRead.Handle = handle;
            this.serverRead.Set();
        }
        public bool IsClosed()
        {
            bool result;

            try
            {
                this.dataView = SharedMemoryStream.MapViewOfFile(this.dataMap, 2u, 0u, 0u, (IntPtr)16004);
                if (this.dataView == IntPtr.Zero)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (Exception)
            {
                result = true;
            }
            return(result);
        }
Esempio n. 4
0
        public override void Open()
        {
            base.Open();

            // connect to one of our specified hosts
            Stream stream;

            try
            {
                if (Settings.Protocol == ConnectionProtocol.SharedMemory)
                {
                    SharedMemoryStream str = new SharedMemoryStream(Settings.SharedMemoryName);
                    str.Open(Settings.ConnectionTimeout);
                    stream = str;
                }
                else
                {
                    string pipeName = Settings.PipeName;
                    if (Settings.Protocol != ConnectionProtocol.NamedPipe)
                    {
                        pipeName = null;
                    }
                    StreamCreator sc = new StreamCreator(Settings.Server, Settings.Port, pipeName);
                    stream = sc.GetStream(Settings.ConnectionTimeout);
                }
            }
            catch (Exception ex)
            {
                throw new MySqlException("Unable to connect to any of the specified MySQL hosts", ex);
            }


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

            reader          = new PacketReader(new BufferedStream(stream), this);
            writer          = new PacketWriter(new BufferedStream(stream), this);
            writer.Encoding = encoding;

            // read off the welcome packet and parse out it's values
            reader.OpenPacket();
            protocol = reader.ReadByte();
            string versionString = reader.ReadString();

            version        = DBVersion.Parse(versionString);
            threadId       = (int)reader.ReadInteger(4);
            encryptionSeed = reader.ReadString();

            // starting with 4.0.8, maxSinglePacket should be 0xffffff
            if (version.isAtLeast(4, 0, 8))
            {
                MaxSinglePacket = (256 * 256 * 256) - 1;
            }

            // read in Server capabilities if they are provided
            serverCaps = 0;
            if (reader.HasMoreData)
            {
                serverCaps = (ClientFlags)reader.ReadInteger(2);
            }

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

            writer.StartPacket(0);
            writer.WriteInteger((int)connectionFlags, version.isAtLeast(4, 1, 0) ? 4 : 2);
            writer.WriteInteger(MaxSinglePacket, version.isAtLeast(4, 1, 0) ? 4 : 3);

            // 4.1.1 included some new server status info
            if (version.isAtLeast(4, 1, 1))
            {
                /* New protocol with 16 bytes to describe server characteristics */
                serverLanguage = reader.ReadInteger(1);
                serverStatus   = (ServerStatusFlags)reader.ReadInteger(2);
                reader.Skip(13);

                string seedPart2 = reader.ReadString();
                encryptionSeed += seedPart2;

                writer.WriteByte(8);
                writer.Write(new byte[23], 0, 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 CompressedStream( stream, MaxSinglePacket );
                writer          = new PacketWriter(new CompressedStream(new BufferedStream(stream)), this);
                writer.Encoding = encoding;
                reader          = new PacketReader(new CompressedStream(new BufferedStream(stream)), this);
            }

            isOpen = true;
        }
Esempio n. 5
0
        public override void Open()
        {
            base.Open();
            Stream stream;

            try
            {
                if (base.Settings.Protocol == ConnectionProtocol.SharedMemory)
                {
                    SharedMemoryStream sharedMemoryStream = new SharedMemoryStream(base.Settings.SharedMemoryName);
                    sharedMemoryStream.Open(base.Settings.ConnectionTimeout);
                    stream = sharedMemoryStream;
                }
                else
                {
                    string pipeName = base.Settings.PipeName;
                    if (base.Settings.Protocol != ConnectionProtocol.NamedPipe)
                    {
                        pipeName = null;
                    }
                    StreamCreator streamCreator = new StreamCreator(base.Settings.Server, base.Settings.Port, pipeName);
                    stream = streamCreator.GetStream((uint)base.Settings.ConnectionTimeout);
                }
                if (stream == null)
                {
                    throw new Exception();
                }
            }
            catch (Exception inner)
            {
                throw new MySqlException(Resources.UnableToConnectToHost, 1042, inner);
            }
            if (stream == null)
            {
                throw new MySqlException("Unable to connect to any of the specified MySQL hosts");
            }
            this.reader          = new PacketReader(new BufferedStream(stream), this);
            this.writer          = new PacketWriter(new BufferedStream(stream), this);
            this.writer.Encoding = this.encoding;
            this.reader.OpenPacket();
            this.protocol = this.reader.ReadByte();
            string versionString = this.reader.ReadString();

            this.version        = DBVersion.Parse(versionString);
            this.threadId       = this.reader.ReadInteger(4);
            this.encryptionSeed = this.reader.ReadString();
            if (this.version.isAtLeast(4, 0, 8))
            {
                this.MaxSinglePacket = 16777215;
            }
            this.serverCaps = (ClientFlags)0;
            if (this.reader.HasMoreData)
            {
                this.serverCaps = (ClientFlags)this.reader.ReadInteger(2);
            }
            this.SetConnectionFlags();
            this.writer.StartPacket(0L);
            this.writer.WriteInteger((long)this.connectionFlags, this.version.isAtLeast(4, 1, 0) ? 4 : 2);
            this.writer.WriteInteger((long)this.MaxSinglePacket, this.version.isAtLeast(4, 1, 0) ? 4 : 3);
            if (this.version.isAtLeast(4, 1, 1))
            {
                this.serverLanguage = this.reader.ReadInteger(1);
                this.serverStatus   = (ServerStatusFlags)this.reader.ReadInteger(2);
                this.reader.Skip(13L);
                string text = this.reader.ReadString();
                this.encryptionSeed += text;
                this.writer.WriteByte(8);
                this.writer.Write(new byte[23], 0, 23);
            }
            this.Authenticate();
            if ((this.connectionFlags & ClientFlags.COMPRESS) != (ClientFlags)0)
            {
                this.writer          = new PacketWriter(new BufferedStream(new CompressedStream(stream)), this);
                this.writer.Encoding = this.encoding;
                this.reader          = new PacketReader(new BufferedStream(new CompressedStream(stream)), this);
            }
            this.isOpen = true;
        }
Esempio n. 6
0
        public void 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,
                                                     Settings.Keepalive, this.Version);
#if !CF
                MySqlSecurityPermission.CreatePermissionSet(false).Assert();
#endif
                baseStream = sc.GetStream(Settings.ConnectionTimeout);
#if !CF
            }
#endif
            }
            catch (System.Security.SecurityException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new MySqlException(Resources.UnableToConnectToHost,
                                         (int)MySqlErrorCode.UnableToConnectToHost, ex);
            }
            if (baseStream == null)
            {
                throw new MySqlException(Resources.UnableToConnectToHost,
                                         (int)MySqlErrorCode.UnableToConnectToHost);
            }

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

            stream.ResetTimeout((int)Settings.ConnectionTimeout * 1000);

            // read off the welcome packet and parse out it's values
            packet = stream.ReadPacket();
            int protocol         = packet.ReadByte();
            string versionString = packet.ReadString();
            version = DBVersion.Parse(versionString);
            if (!version.isAtLeast(5, 0, 0))
            {
                throw new NotSupportedException(Resources.ServerTooOld);
            }
            threadId       = packet.ReadInteger(4);
            encryptionSeed = packet.ReadString();

            maxSinglePacket = (256 * 256 * 256) - 1;

            // read in Server capabilities if they are provided
            ClientFlags serverCaps = 0;
            if (packet.HasMoreData)
            {
                serverCaps = (ClientFlags)packet.ReadInteger(2);
            }

            /* New protocol with 16 bytes to describe server characteristics */
            owner.ConnectionCharSetIndex = (int)packet.ReadByte();

            serverStatus = (ServerStatusFlags)packet.ReadInteger(2);

            // Since 5.5, high bits of server caps are stored after status.
            // Previously, it was part of reserved always 0x00 13-byte filler.
            uint serverCapsHigh = (uint)packet.ReadInteger(2);
            serverCaps |= (ClientFlags)(serverCapsHigh << 16);

            packet.Position += 11;
            string seedPart2 = packet.ReadString();
            encryptionSeed += seedPart2;

            string authenticationMethod = "";
            if ((serverCaps & ClientFlags.PLUGIN_AUTH) != 0)
            {
                authenticationMethod = packet.ReadString();
            }

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

            packet.Clear();
            packet.WriteInteger((int)connectionFlags, 4);

#if !CF
            if ((serverCaps & ClientFlags.SSL) == 0)
            {
                if ((Settings.SslMode != MySqlSslMode.None) &&
                    (Settings.SslMode != MySqlSslMode.Preferred))
                {
                    // Client requires SSL connections.
                    string message = String.Format(Resources.NoServerSSLSupport,
                                                   Settings.Server);
                    throw new MySqlException(message);
                }
            }
            else if (Settings.SslMode != MySqlSslMode.None)
            {
                stream.SendPacket(packet);
                StartSSL();
                packet.Clear();
                packet.WriteInteger((int)connectionFlags, 4);
            }
#endif

            packet.WriteInteger(maxSinglePacket, 4);
            packet.WriteByte(8);
            packet.Write(new byte[23]);

            Authenticate(false);

            // 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.
            packet.Version      = version;
            stream.MaxBlockSize = maxSinglePacket;
        }
Esempio n. 7
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;
        }
 public override void Close()
 {
     SharedMemoryStream.UnmapViewOfFile(this.dataView);
     SharedMemoryStream.CloseHandle(this.dataMap);
 }
 public override void Flush()
 {
     SharedMemoryStream.FlushViewOfFile(this.dataView, 0u);
 }