public static int Write(this IUVStream <ArraySegment <byte> > stream, Encoding enc, string text, Action <Exception> callback) { var bytes = enc.GetBytes(text); stream.Write(bytes, callback); return(bytes.Length); }
public static int End(this IUVStream <ArraySegment <byte> > stream, Encoding encoding, string text, Action <Exception> callback) { int size = stream.Write(encoding, text); stream.Shutdown(callback); return(size); }
public static Task <ArraySegment <byte>?> ReadAsync(this IUVStream stream) { var tcs = new TaskCompletionSource <ArraySegment <byte>?>(); Action <Exception> error = (e) => tcs.SetException(e); Action <ArraySegment <byte> > data = (buffer) => tcs.SetResult(buffer); Action end = () => tcs.SetResult(null); try { stream.Error += error; stream.Complete += end; stream.Data += data; stream.Resume(); } catch (ArgumentException) { tcs.SetResult(null); } catch (Exception e) { tcs.SetException(e); } return(tcs.Task.ContinueWith((bb) => { stream.Pause(); stream.Error -= error; stream.Complete -= end; stream.Data -= data; return bb.Result; }, stream.Loop.Scheduler)); }
public static Task <TData?> ReadStructAsync <TData>(this IUVStream <TData> stream) where TData : struct { var tcs = new TaskCompletionSource <TData?>(); 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); }
public static int Write(this IUVStream stream, Encoding enc, string text, Action <bool> callback) { var bytes = enc.GetBytes(text); stream.Write(bytes, callback); return(bytes.Length); }
public static void Compute(HashAlgorithm hashAlgorithm, IUVStream<ArraySegment<byte>> stream, Action<byte[]> callback) { var hs = new HashStream(hashAlgorithm, stream); hs.Complete += () => { callback(hs.Hash); hs.Dispose(); }; }
public static void ComputeString(HashAlgorithm hashAlgorithm, IUVStream stream, Action<string> callback) { var hs = new HashStream(hashAlgorithm, stream); hs.Complete += () => { callback(hs.HashString); hs.Dispose(); }; }
public static void Pump <T>(this IUVStream <T> readStream, IUVStream <T> writeStream, Action <Exception> callback) { readStream.Pump(writeStream, (ex1, ex2) => { if (callback != null) { callback(ex1 ?? ex2); } }); }
public static void ComputeString(HashAlgorithm hashAlgorithm, IUVStream stream, Action <string> callback) { var hs = new HashStream(hashAlgorithm, stream); hs.Complete += () => { callback(hs.HashString); hs.Dispose(); }; }
public static void Compute(HashAlgorithm hashAlgorithm, IUVStream <ArraySegment <byte> > stream, Action <byte[]> callback) { var hs = new HashStream(hashAlgorithm, stream); hs.Complete += () => { callback(hs.Hash); hs.Dispose(); }; }
public static async Task<byte[]> Compute(IUVStream<ArraySegment<byte>> stream) { var hashAlgorithm = SHA1Managed.Create(); ArraySegment<byte>? data; while ((data = await stream.ReadStructAsync()).HasValue) { hashAlgorithm.TransformBlock(data.Value); } hashAlgorithm.TransformFinalBlock(); return hashAlgorithm.Hash; }
public static async Task <byte[]> Compute(IUVStream <ArraySegment <byte> > stream) { var hashAlgorithm = SHA1Managed.Create(); ArraySegment <byte>?data; while ((data = await stream.ReadStructAsync()).HasValue) { hashAlgorithm.TransformBlock(data.Value); } hashAlgorithm.TransformFinalBlock(); return(hashAlgorithm.Hash); }
public static Task ShutdownAsync(this IUVStream stream) { var tcs = new TaskCompletionSource <object>(); try { stream.Shutdown(() => { tcs.SetResult(null); }); } catch (Exception e) { tcs.SetException(e); } return(tcs.Task); }
public static Task WriteAsync(this IUVStream stream, byte[] data, int index, int count) { var tcs = new TaskCompletionSource <object>(); try { stream.Write(data, index, count, (_) => { tcs.SetResult(null); }); } catch (Exception e) { tcs.SetException(e); } return(tcs.Task); }
public static async Task <string> ReadStringAsync(this IUVStream <ArraySegment <byte> > stream, Encoding encoding) { if (encoding == null) { throw new ArgumentException("encoding"); } var buffer = await stream.ReadStructAsync(); if (!buffer.HasValue) { return(null); } return(encoding.GetString(buffer.Value)); }
public static async Task <string> ReadStringAsync(this IUVStream stream, Encoding encoding) { if (encoding == null) { throw new ArgumentException("encoding"); } var buffer = await stream.ReadAsync(); if (buffer == null) { return(null); } return(encoding.GetString(buffer.Buffer, buffer.Start, buffer.Length)); }
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 async Task <string> ReadStringAsync(this IUVStream stream, Encoding encoding) { if (encoding == null) { throw new ArgumentException("encoding"); } var buffer = await stream.ReadAsync(); if (!buffer.HasValue) { return(null); } var b = buffer.Value; return(encoding.GetString(b.Array, b.Offset, b.Count)); }
public HashStream(HashAlgorithm algorithm, IUVStream stream) { HashAlgorithm = algorithm; Stream = stream; Stream.Data += ((b) => { HashAlgorithm.TransformBlock(b.Array, b.Offset, b.Count, null, 0); }); Stream.Complete += () => { HashAlgorithm.TransformFinalBlock(new byte[] { }, 0, 0); Hash = HashAlgorithm.Hash; if (Complete != null) { Complete(); } }; }
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 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?.Invoke(e, null); Action <TData> data = (val) => finish?.Invoke(null, val); Action end = () => finish?.Invoke(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); }
public static int Write(this IUVStream <ArraySegment <byte> > stream, string text) { return(stream.Write(Encoding.Default, text)); }
public static async Task <string> ReadStringAsync(this IUVStream <ArraySegment <byte> > stream) { return(await ReadStringAsync(stream, Encoding.UTF8)); }
public static void End(this IUVStream <ArraySegment <byte> > stream, byte[] data, Action <Exception> callback) { stream.Write(data); stream.Shutdown(callback); }
public static void End(this IUVStream <ArraySegment <byte> > stream, byte[] data, int index, int count) { stream.End(data, index, count, null); }
public static void Shutdown(this IUVStream <ArraySegment <byte> > stream, Action callback) { stream.Shutdown((_) => callback()); }
public static void Shutdown(this IUVStream <ArraySegment <byte> > stream) { stream.Shutdown(null); }
public static void Read(this IUVStream <ArraySegment <byte> > stream, Encoding enc, Action <string> callback) { stream.Data += (data) => callback(enc.GetString(data.Array, data.Offset, data.Count)); }
public static void Pump(this IUVStream readStream, IUVStream writeStream) { Pump(readStream, writeStream, null); }
public static int Write(this IUVStream <ArraySegment <byte> > stream, Encoding enc, string text) { return(stream.Write(enc, text, null)); }
public static int Write(this IUVStream <ArraySegment <byte> > stream, string text, Action <Exception> callback) { return(stream.Write(Encoding.Default, text, callback)); }
public static void Write(this IUVStream <ArraySegment <byte> > stream, ArraySegment <byte> data) { stream.Write(data, null); }
public static void Write(this IUVStream <ArraySegment <byte> > stream, byte[] data, Action <Exception> callback) { Ensure.ArgumentNotNull(data, "data"); stream.Write(data, 0, data.Length, callback); }
public static void Write(this IUVStream <ArraySegment <byte> > stream, byte[] data, int index, int count, Action <Exception> callback) { stream.Write(new ArraySegment <byte>(data, index, count), callback); }