/// <summary>create pipeline reader from IEnumerable<T></summary> public static IPipeReader CreateFromEnumerable <T>(this PipeFactory pfac, IEnumerable <T> data) where T : struct { return(pfac.CreateReader(null, async(r, w) => { var tmp = new T[1]; foreach (var b in data) { // allocating write buffer var wbuf = w.Alloc(); try { tmp[0] = b; var rspan = new Span <T>(tmp).AsBytes(); wbuf.Ensure(rspan.Length); var span = wbuf.Memory.Span.Slice(0, rspan.Length); rspan.CopyTo(span); wbuf.Advance(rspan.Length); } catch (Exception e) { Console.WriteLine($"error in reader:{e}"); await wbuf.FlushAsync(); // inform exception to next reader w.Complete(e); // when exception is thrown to outside of function, pipe will be broken return; } // must call FlushAsync after Advance await wbuf.FlushAsync(); } // if you forget to call Complete(), following pipeline reader may get stack w.Complete(); })); }
public static IPipeReader CreateFromCommand(this PipeFactory pfac, string filePath, string args , IPipeWriter stderr = null, IPipeReader stdin = null, CancellationToken ct = default(CancellationToken)) { var si = new ProcessStartInfo(filePath, args); si.RedirectStandardOutput = true; si.RedirectStandardError = stderr != null; si.RedirectStandardInput = stdin != null; si.UseShellExecute = false; si.CreateNoWindow = true; return(pfac.CreateReader(null, async(r, w) => { try { using (var proc = new Process()) { proc.StartInfo = si; proc.Start(); var procReader = proc.StandardOutput.BaseStream.AsPipelineReader(); var errReader = stderr != null ? proc.StandardError.BaseStream.AsPipelineReader() : null; await Task.WhenAll( Task.Run(async() => { if (stdin != null) { await stdin.CopyToAsync(proc.StandardInput.BaseStream).ConfigureAwait(false); proc.StandardInput.Dispose(); } }) , Task.Run(async() => { if (stderr != null) { await errReader.CopyToAsync(stderr).ConfigureAwait(false); } }) , Task.Run(() => { try { using (ct.Register(() => proc.Kill())) { proc.WaitForExit(); } } catch (Exception e) { procReader.Complete(e); procReader.CancelPendingRead(); if (errReader != null) { errReader.CancelPendingRead(); } } }) , Task.Run(async() => { await procReader.CopyToAsync(w).ConfigureAwait(false); }) ).ConfigureAwait(false); w.Complete(); } } catch (Exception e) { Console.WriteLine($"{e}"); w.Complete(e); } })); }
public static IPipeReader CreateGZipCompressReader(this PipeFactory factory, IPipeReader reader, CompressionLevel compressionLevel) { var deflater = new WritableDeflateTransform(compressionLevel, ZLibNative.GZip_DefaultWindowBits); return(factory.CreateReader(reader, deflater.Execute)); }
public static IPipeReader CreateGZipDecompressReader(this PipeFactory factory, IPipeReader reader) { var inflater = new ReadableDeflateTransform(ZLibNative.GZip_DefaultWindowBits); return(factory.CreateReader(reader, inflater.Execute)); }
public static IPipeReader DeflateDecompress(this IPipeReader reader, PipeFactory factory) { var inflater = new ReadableDeflateTransform(ZLibNative.Deflate_DefaultWindowBits); return(factory.CreateReader(reader, inflater.Execute)); }
public StreamPipeConnection(PipeFactory factory, Stream stream) { Input = factory.CreateReader(stream); Output = factory.CreateWriter(stream); }