Example #1
0
        public void Compile(bool async = false)
        {
            if (Status == CompileStatus.Compiling)
            {
                return;
            }

            var pct = new Thread(Init)
            {
                Name = "VNCSharpCompiler"
            };

            pct.Start();

            if (async)
            {
                return;
            }

            VitaNexCore.WaitWhile(
                () => Status == CompileStatus.Initializing || Status == CompileStatus.Compiling,
                TimeSpan.FromMinutes(5.0));

            pct.Join();
        }
Example #2
0
        public void Receive(bool decompress, Encoding enc, out string content, out byte[] buffer, out int length)
        {
            content = String.Empty;
            buffer  = _EmptyBuffer;
            length  = 0;

            if (Stream.CanRead)
            {
                VitaNexCore.WaitWhile(() => !Stream.DataAvailable, TimeSpan.FromMilliseconds(1000));

                if (Stream.DataAvailable)
                {
                    buffer = new byte[Client.ReceiveBufferSize];

                    using (var ms = new MemoryStream())
                    {
                        while (Stream.DataAvailable)
                        {
                            length = Stream.Read(buffer, 0, buffer.Length);

                            if (length > 0)
                            {
                                if (ms.Length + length > WebAPI.CSOptions.MaxReceiveBufferSizeBytes)
                                {
                                    throw new InternalBufferOverflowException(
                                              String.Format("Received data exceeded {0:#,0} bytes", WebAPI.CSOptions.MaxReceiveBufferSizeBytes));
                                }

                                ms.SetLength(ms.Length + length);
                                ms.Write(buffer, 0, length);
                            }
                        }

                        buffer = ms.ToArray();
                        length = buffer.Length;
                    }
                }
            }

            WebAPI.CSOptions.ToConsole(
                "Received {0:#,0} bytes ({1:#,0} bytes/read)",
                length,
                Math.Min(length, Client.ReceiveBufferSize));

            if (length > 0)
            {
                if (decompress)
                {
                    Decompress(ref buffer, ref length);
                }

                Decode(enc, buffer, length, out content);
            }
        }
Example #3
0
        public bool ReceiveHeaders(out KeyValueString[] headers)
        {
            headers = _EmptyHeaders;

            var buffer = _EmptyBuffer;
            var length = 0;

            if (Stream.CanRead)
            {
                VitaNexCore.WaitWhile(() => !Stream.DataAvailable, TimeSpan.FromMilliseconds(1000));

                if (Stream.DataAvailable)
                {
                    buffer = new byte[Client.ReceiveBufferSize];

                    using (var ms = new MemoryStream())
                    {
                        int idx = 0, seq = 0;

                        while (Stream.DataAvailable)
                        {
                            var r = Stream.ReadByte();

                            if (r > -1)
                            {
                                if (++length > WebAPI.CSOptions.MaxReceiveBufferSizeBytes)
                                {
                                    throw new InternalBufferOverflowException(
                                              String.Format("Received data exceeded {0:#,0} bytes", WebAPI.CSOptions.MaxReceiveBufferSizeBytes));
                                }

                                var b = (byte)r;

                                buffer[idx++] = b;

                                if (Sequence(b, ref seq) && seq >= 4)
                                {
                                    break;
                                }

                                if (idx >= buffer.Length)
                                {
                                    ms.Write(buffer, 0, idx);
                                    idx = 0;
                                }
                            }
                        }

                        if (idx > 0)
                        {
                            ms.Write(buffer, 0, idx);
                        }

                        buffer = ms.ToArray();
                        length = buffer.Length;
                    }
                }
            }

            WebAPI.CSOptions.ToConsole(
                "Received {0:#,0} bytes ({1:#,0} bytes/read)",
                length,
                Math.Min(length, Client.ReceiveBufferSize));

            if (length <= 0)
            {
                return(false);
            }

            var raw = Encoding.ASCII.GetString(buffer, 0, length);

            if (String.IsNullOrWhiteSpace(raw))
            {
                return(false);
            }

            var h = raw.Split(_Separators, StringSplitOptions.RemoveEmptyEntries);

            if (h.Length == 0)
            {
                return(false);
            }

            headers = ParseHeaders(h).ToArray();

            return(headers.Length > 0);
        }
Example #4
0
        /// <summary>
        ///     Attempts to connect to the specified MySQL Server with the given settings.
        /// </summary>
        /// <param name="retries">Retry connection attempts this many times if the initial connection fails.</param>
        /// <param name="createDB">If a database name is specified, should it try to create it if it doesn't exist?</param>
        /// <returns>True if connection successful</returns>
        public bool Connect(int retries = 0, bool createDB = false)
        {
            if (IsDisposed)
            {
                return(false);
            }

            if (Connected)
            {
                return(true);
            }

            if (Connecting)
            {
                return(false);
            }

            if (!CanConnect())
            {
                return(false);
            }

            var conStr = Credentials.GetConnectionString();

            //MySQL.CSOptions.ToConsole("{0}", conStr);

            var connected = VitaNexCore.TryCatchGet(
                () =>
            {
                if (Instance == null)
                {
                    MySQL.CSOptions.ToConsole("Connection Attempt.");

                    Instance              = new OdbcConnection(conStr);
                    Instance.InfoMessage += OnMessage;

                    Instance.Open();

                    VitaNexCore.WaitWhile(() => Instance.State == ConnectionState.Connecting, TimeSpan.FromSeconds(3));

                    if (Instance.State == ConnectionState.Broken)
                    {
                        Instance.Close();
                    }

                    if (Instance.State == ConnectionState.Open)
                    {
                        MySQL.CSOptions.ToConsole("Connection Successful.");
                        MySQL.Connected(this);

                        if (!String.IsNullOrWhiteSpace(Credentials.Database))
                        {
                            if (createDB)
                            {
                                NonQuery(
                                    @"CREATE DATABASE IF NOT EXISTS `{0}` DEFAULT CHARSET `utf8` DEFAULT COLLATE `utf8_bin`",
                                    Credentials.Database);
                            }

                            Instance.ChangeDatabase(Credentials.Database);
                        }

                        return(true);
                    }
                }

                if (Instance == null)
                {
                    return(false);
                }

                if (Instance.State != ConnectionState.Open)
                {
                    Instance.Close();

                    for (var i = 1; i <= retries; i++)
                    {
                        MySQL.CSOptions.ToConsole("Connection Attempt {0}.", i);

                        Instance.Open();

                        VitaNexCore.WaitWhile(() => Instance.State == ConnectionState.Connecting, TimeSpan.FromSeconds(3));

                        if (Instance.State != ConnectionState.Open)
                        {
                            Instance.Close();
                            continue;
                        }

                        MySQL.CSOptions.ToConsole("Connection Successful.");

                        OnConnected();

                        if (!String.IsNullOrWhiteSpace(Credentials.Database))
                        {
                            if (createDB)
                            {
                                NonQuery(
                                    @"CREATE DATABASE IF NOT EXISTS `{0}` DEFAULT CHARSET `utf8` DEFAULT COLLATE `utf8_bin` DEFAULT ENGINE `INNODB`",
                                    Credentials.Database);
                            }

                            Instance.ChangeDatabase(Credentials.Database);
                        }

                        return(true);
                    }
                }

                if (Instance.State != ConnectionState.Open)
                {
                    Instance.Close();
                }

                return(false);
            },
                MySQL.CSOptions.ToConsole);

            if (!connected)
            {
                MySQL.CSOptions.ToConsole("Connection Failed.");
                Close();
            }

            return(connected);
        }