public static void Pump <T>(this IUVStream <T> readStream, IUVStream <T> writeStream, Action <Exception> callback) { bool pending = false; bool done = false; Action <Exception> call = null; Action complete = () => call(null); call = (ex) => { if (done) { return; } readStream.Error -= call; readStream.Complete -= complete; done = true; if (callback != null) { callback(ex); } }; readStream.Data += ((data) => { writeStream.Write(data, null); if (writeStream.WriteQueueSize > 0) { pending = true; readStream.Pause(); } }); writeStream.Drain += () => { if (pending) { pending = false; readStream.Resume(); } }; readStream.Error += call; readStream.Complete += complete; readStream.Resume(); }
public static void Pump(this IUVStream readStream, IUVStream writeStream, Action <Exception, Exception> callback) { bool pending = false; bool done = false; Action <Exception, Exception> call = (ex1, ex2) => { if (done) { return; } done = true; if (callback != null) { callback(ex1, ex2); } }; readStream.Data += ((bb) => { writeStream.Write(bb.Array, bb.Offset, bb.Count); if (writeStream.WriteQueueSize > 0) { pending = true; readStream.Pause(); } }); writeStream.Drain += () => { if (pending) { pending = false; readStream.Resume(); } }; readStream.Complete += () => { writeStream.Shutdown(() => call(null, null)); }; readStream.Error += (ex) => call(ex, null); readStream.Error += (ex) => call(null, ex); readStream.Resume(); }
public static Task <TData?> ReadStructAsync <TData>(this IUVStream <TData> stream) where TData : struct { var tcs = new TaskCompletionSource <TData?>(); #if TASK_STATUS HelperFunctions.SetStatus(tcs.Task, TaskStatus.Running); #endif Action <Exception, TData?> finish = null; Action <Exception> error = (e) => finish(e, null); Action <TData> data = (val) => finish(null, val); Action end = () => finish(null, null); finish = HelperFunctions.Finish(tcs, () => { stream.Pause(); stream.Error -= error; stream.Complete -= end; stream.Data -= data; }); try { stream.Error += error; stream.Complete += end; stream.Data += data; stream.Resume(); } catch (Exception e) { finish(e, null); } return(tcs.Task); }