Example #1
0
        void ReadStandardOutput()
        {
            #if DEBUG
            if (Log.IsDebugEnabled)
                Log.Debug("Started reading from standard output.");
            #endif
            try
            {
                // Write bytes to a buffer to be delivered via HTTP.
                Stream input = _process.StandardOutput.BaseStream;
                byte[] buffer = new byte[StreamBufferSize];
                int byteCount = 0;
                int i = 0;
                while (_running)
                {
                    int c = input.ReadByte();
                    if (c == -1) break;

                    // Print periodic progress.
                    if (++byteCount % (1024 * 128) == 0)
                    {
            #if DEBUG
                        if (Log.IsDebugEnabled)
                            Log.Debug("Progress: " + Program.GetHumanByteString(byteCount));
            #endif
                    }

                    // Buffering output.
                    buffer[i++] = (byte) c;
                    if (i % StreamBufferSize != 0) continue;
                    AudioData data = new AudioData(buffer, buffer.Length);
                    i = 0; // Restart filling the buffer.
                    OutputQueue.Enqueue(data);
                }

                // Push any extra in buffer onto the queue.
                if (i > 0)
                {
                    AudioData data = new AudioData(buffer, i);
                    OutputQueue.Enqueue(data);
                }

            #if DEBUG
                if (Log.IsDebugEnabled)
                    Log.Debug("Total bytes: " + byteCount + " (" + Program.GetHumanByteString(byteCount) + ")");
            #endif

                // Send zeros for difference between expected size and the actual byte count.
                if (_expectedSize > byteCount)
                {
            #if DEBUG
                    if (Log.IsDebugEnabled)
                        Log.Debug("Byte count is less than expected. Padding with zeros.");
            #endif

                    // Send zeros for difference between expected size and the actual byte count.
                    int diff = _expectedSize - byteCount;
                    byte[] extraBytes = new byte[diff];
                    for (int j = 0; j < diff; j++) extraBytes[j] = 0;
                    AudioData data = new AudioData(extraBytes, diff);
                    OutputQueue.Enqueue(data);
                }
                else if (_expectedSize < byteCount)
                {
            #if DEBUG
                    if (Log.IsErrorEnabled)
                        Log.Error("Byte count is greater than expected size!");
            #endif
                }
                else
                {
            #if DEBUG
                    if (Log.IsDebugEnabled)
                        Log.Debug("Byte count matched expected size.");
            #endif
                }

                OutputQueue.Enqueue(NullAudioData.Instance);
            }
            catch (ThreadAbortException)
            {
            #if DEBUG
                if (Log.IsWarnEnabled)
                    Log.Warn("StandardOutput: Thread aborted.");
            #endif
            }

            #if DEBUG
            if (Log.IsDebugEnabled)
                Log.Debug("Finished reading from standard output.");
            #endif
        }
Example #2
0
        void ReadStandardOutput()
        {
#if DEBUG
            if (Log.IsDebugEnabled)
            {
                Log.Debug("Started reading from standard output.");
            }
#endif
            try
            {
                // Write bytes to a buffer to be delivered via HTTP.
                Stream input     = _process.StandardOutput.BaseStream;
                byte[] buffer    = new byte[StreamBufferSize];
                int    byteCount = 0;
                int    i         = 0;
                while (_running)
                {
                    int c = input.ReadByte();
                    if (c == -1)
                    {
                        break;
                    }

                    // Print periodic progress.
                    if (++byteCount % (1024 * 128) == 0)
                    {
#if DEBUG
                        if (Log.IsDebugEnabled)
                        {
                            Log.Debug("Progress: " + Program.GetHumanByteString(byteCount));
                        }
#endif
                    }

                    // Buffering output.
                    buffer[i++] = (byte)c;
                    if (i % StreamBufferSize != 0)
                    {
                        continue;
                    }
                    AudioData data = new AudioData(buffer, buffer.Length);
                    i = 0; // Restart filling the buffer.
                    OutputQueue.Enqueue(data);
                }

                // Push any extra in buffer onto the queue.
                if (i > 0)
                {
                    AudioData data = new AudioData(buffer, i);
                    OutputQueue.Enqueue(data);
                }

#if DEBUG
                if (Log.IsDebugEnabled)
                {
                    Log.Debug("Total bytes: " + byteCount + " (" + Program.GetHumanByteString(byteCount) + ")");
                }
#endif

                // Send zeros for difference between expected size and the actual byte count.
                if (_expectedSize > byteCount)
                {
#if DEBUG
                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug("Byte count is less than expected. Padding with zeros.");
                    }
#endif

                    // Send zeros for difference between expected size and the actual byte count.
                    int    diff       = _expectedSize - byteCount;
                    byte[] extraBytes = new byte[diff];
                    for (int j = 0; j < diff; j++)
                    {
                        extraBytes[j] = 0;
                    }
                    AudioData data = new AudioData(extraBytes, diff);
                    OutputQueue.Enqueue(data);
                }
                else if (_expectedSize < byteCount)
                {
#if DEBUG
                    if (Log.IsErrorEnabled)
                    {
                        Log.Error("Byte count is greater than expected size!");
                    }
#endif
                }
                else
                {
#if DEBUG
                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug("Byte count matched expected size.");
                    }
#endif
                }

                OutputQueue.Enqueue(NullAudioData.Instance);
            }
            catch (ThreadAbortException)
            {
#if DEBUG
                if (Log.IsWarnEnabled)
                {
                    Log.Warn("StandardOutput: Thread aborted.");
                }
#endif
            }

#if DEBUG
            if (Log.IsDebugEnabled)
            {
                Log.Debug("Finished reading from standard output.");
            }
#endif
        }