コード例 #1
0
        public static void ShowConsole()
        {
            var handle = GetConsoleWindow();

            var outputRedirected = IsHandleRedirected((IntPtr)StdOutConsoleHandle);

            // If we are outputting somewhere unexpected, add an additional console window
            if (outputRedirected)
            {
                var originalStream = Console.OpenStandardOutput();

                // Free before trying to allocate
                FreeConsole();
                AllocConsole();

                var outputStream = Console.OpenStandardOutput();
                if (originalStream != null)
                {
                    outputStream = new DualStream(originalStream, outputStream);
                }

                TextWriter writer = new StreamWriter(outputStream)
                {
                    AutoFlush = true
                };
                Console.SetOut(writer);
            }
            else if (handle != IntPtr.Zero)
            {
                const int SW_SHOW = 5;
                ShowWindow(handle, SW_SHOW);
            }
        }
コード例 #2
0
        public static void ShowConsole()
        {
            var handle = GetConsoleWindow();

            var outputRedirected = IsHandleRedirected((IntPtr)StdOutConsoleHandle);

            if (handle == IntPtr.Zero || outputRedirected)
            {
                Stream originalStream = null;
                if (outputRedirected)
                {
                    originalStream = Console.OpenStandardOutput();
                }

                AllocConsole();

                Stream outputStream = Console.OpenStandardOutput();
                if (originalStream != null)
                {
                    outputStream = new DualStream(originalStream, outputStream);
                }

                TextWriter writer = new StreamWriter(outputStream)
                {
                    AutoFlush = true
                };
                Console.SetOut(writer);
            }
            else
            {
                const int SW_SHOW = 5;
                ShowWindow(handle, SW_SHOW);
            }
        }
コード例 #3
0
        public void TestDoubleStream()
        {
            byte[] inputBytes  = new byte[] { 6, 8, 4, 22, 11 };
            byte[] outputBytes = new byte[] { 66, 88, 101 };

            MemoryStream inputStream  = new MemoryStream(inputBytes);
            MemoryStream outputStream = new MemoryStream();
            DualStream   testStream   = new DualStream();

            testStream.InputStream  = inputStream;
            testStream.OutputStream = outputStream;

            foreach (byte value in inputBytes)
            {
                Assert.IsTrue(testStream.ReadByte() == value);
            }

            foreach (byte value in outputBytes)
            {
                testStream.WriteByte(value);
            }

            Assert.IsTrue(outputStream.ToArray().SequenceEqual(outputBytes));
            Assert.IsTrue(testStream.Read(inputBytes, 0, 5) == 0);
        }
コード例 #4
0
ファイル: ConsoleLogListener.cs プロジェクト: psowinski/xenko
        public static void ShowConsole()
        {
            var handle = GetConsoleWindow();

            var outputRedirected = IsHandleRedirected((IntPtr)StdOutConsoleHandle);

            // If we are outputting somewhere unexpected, add an additional console window
            if (outputRedirected)
            {
                var originalStream = Console.OpenStandardOutput();

                // Free before trying to allocate
                FreeConsole();
                AllocConsole();

                var outputStream = Console.OpenStandardOutput();
                if (originalStream != null)
                {
                    outputStream = new DualStream(originalStream, outputStream);
                }

                TextWriter writer = new StreamWriter(outputStream) { AutoFlush = true };
                Console.SetOut(writer);
            }
            else if (handle != IntPtr.Zero)
            {
                const int SW_SHOW = 5;
                ShowWindow(handle, SW_SHOW);
            }
        }
コード例 #5
0
ファイル: StreamRequest.cs プロジェクト: klincheR/NadekoBot
        private async Task BufferSong() {
            //start feeding the buffer
            var p = Process.Start(new ProcessStartInfo {
                FileName = "ffmpeg",
                Arguments = $"-i {Url} -f s16le -ar 48000 -ac 2 pipe:1",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = false,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden,
            });
            int attempt = 0;
            while (true) {
                int magickBuffer = 1;
                //wait for the read pos to catch up with write pos
                while (buffer.writePos - buffer.readPos > 1.MB() && State != StreamState.Completed) {
                    prebufferingComplete = true;
                    await Task.Delay(150);
                }

                if (State == StreamState.Completed) {
                    try {
                        p.CancelOutputRead();
                        p.Close();
                    } catch (Exception) { }
                    Console.WriteLine("Buffering canceled, stream is completed.");
                    return;
                }

                if (buffer.readPos > 1.MiB() && buffer.writePos > 1.MiB()) { // if buffer is over 5 MiB, create new one
                    var skip = 1.MB(); //remove only 5 MB, just in case
                    var newBuffer = new DualStream();

                    lock (_bufferLock) {
                        byte[] data = buffer.ToArray().Skip(skip).ToArray();
                        var newReadPos = buffer.readPos - skip;
                        var newPos = buffer.Position - skip;
                        buffer = newBuffer;
                        buffer.Write(data, 0, data.Length);
                        buffer.readPos = newReadPos;
                        buffer.Position = newPos;
                    }
                }             

                var buf = new byte[1024];
                int read = 0;
                read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, 1024);
                //Console.WriteLine($"Read: {read}");
                if (read == 0) {
                    if (attempt == 5) {
                        try {
                            p.CancelOutputRead();
                            p.Close();
                        } catch (Exception) { }

                        Console.WriteLine($"Didn't read anything from the stream for {attempt} attempts. {buffer.Length/1.MB()}MB length");
                        return;
                    } else {
                        ++attempt;
                        await Task.Delay(20);
                    }
                } else {
                    attempt = 0;
                    await buffer.WriteAsync(buf, 0, read);
                }
            }
        }
コード例 #6
0
ファイル: StreamRequest.cs プロジェクト: klincheR/NadekoBot
 public MusicStreamer(StreamRequest parent, string directUrl) {
     this.parent = parent;
     this.buffer = new DualStream();
     this.Url = directUrl;
     State = StreamState.Queued;
 }
コード例 #7
0
        public static void ShowConsole()
        {
            var handle = GetConsoleWindow();

            var outputRedirected = IsHandleRedirected((IntPtr)StdOutConsoleHandle);

            if (handle == IntPtr.Zero || outputRedirected)
            {
                Stream originalStream = null;
                if (outputRedirected)
                {
                    originalStream = Console.OpenStandardOutput();
                }

                AllocConsole();

                Stream outputStream = Console.OpenStandardOutput();
                if (originalStream != null)
                {
                    outputStream = new DualStream(originalStream, outputStream);
                }

                TextWriter writer = new StreamWriter(outputStream) { AutoFlush = true };
                Console.SetOut(writer);
            }
            else
            {
                const int SW_SHOW = 5;
                ShowWindow(handle, SW_SHOW);
            }
        }