FFMpegCore/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs
Malte Rosenbjerg c3b5cd997e Cleanup
Former-commit-id: 72366d573a
2020-05-09 17:53:03 +02:00

51 lines
1.5 KiB
C#

using System;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
using FFMpegCore.Pipes;
namespace FFMpegCore.Arguments
{
public abstract class PipeArgument : IInputArgument, IOutputArgument
{
private string PipeName { get; }
public string PipePath => PipeHelpers.GetPipePath(PipeName);
protected NamedPipeServerStream Pipe { get; private set; } = null!;
private readonly PipeDirection _direction;
protected PipeArgument(PipeDirection direction)
{
PipeName = PipeHelpers.GetUnqiuePipeName();
_direction = direction;
}
public void Pre()
{
if (Pipe != null)
throw new InvalidOperationException("Pipe already has been opened");
Pipe = new NamedPipeServerStream(PipeName, _direction, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
}
public void Post()
{
Pipe?.Dispose();
Pipe = null!;
}
public async Task During(CancellationToken? cancellationToken = null)
{
await ProcessDataAsync(cancellationToken ?? CancellationToken.None)
.ContinueWith(task =>
{
Post();
if (task.Exception != null)
throw task.Exception;
}).ConfigureAwait(false);
}
public abstract Task ProcessDataAsync(CancellationToken token);
public abstract string Text { get; }
}
}