Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
	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();
		};
	}
Example #7
0
 public static void ComputeString(HashAlgorithm hashAlgorithm, IUVStream stream, Action<string> callback)
 {
     var hs = new HashStream(hashAlgorithm, stream);
     hs.Complete += () => {
         callback(hs.HashString);
         hs.Dispose();
     };
 }
Example #8
0
 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);
         }
     });
 }
Example #9
0
    public static void ComputeString(HashAlgorithm hashAlgorithm, IUVStream stream, Action <string> callback)
    {
        var hs = new HashStream(hashAlgorithm, stream);

        hs.Complete += () => {
            callback(hs.HashString);
            hs.Dispose();
        };
    }
Example #10
0
    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();
        };
    }
Example #11
0
	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;
	}
Example #12
0
    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);
        }
Example #15
0
        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));
        }
Example #16
0
    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));
    }
Example #17
0
        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();
        }
Example #18
0
    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));
    }
Example #19
0
    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();
            }
        };
    }
Example #20
0
    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();
            }
        };
    }
Example #21
0
        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();
        }
Example #22
0
        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();
        }
Example #23
0
        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);
        }
Example #24
0
 public static int Write(this IUVStream <ArraySegment <byte> > stream, string text)
 {
     return(stream.Write(Encoding.Default, text));
 }
Example #25
0
 public static async Task <string> ReadStringAsync(this IUVStream <ArraySegment <byte> > stream)
 {
     return(await ReadStringAsync(stream, Encoding.UTF8));
 }
Example #26
0
 public static void End(this IUVStream <ArraySegment <byte> > stream, byte[] data, Action <Exception> callback)
 {
     stream.Write(data);
     stream.Shutdown(callback);
 }
Example #27
0
 public static void End(this IUVStream <ArraySegment <byte> > stream, byte[] data, int index, int count)
 {
     stream.End(data, index, count, null);
 }
Example #28
0
 public static void Shutdown(this IUVStream <ArraySegment <byte> > stream, Action callback)
 {
     stream.Shutdown((_) => callback());
 }
Example #29
0
 public static void Shutdown(this IUVStream <ArraySegment <byte> > stream)
 {
     stream.Shutdown(null);
 }
Example #30
0
 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));
 }
Example #31
0
 public static void Pump(this IUVStream readStream, IUVStream writeStream)
 {
     Pump(readStream, writeStream, null);
 }
Example #32
0
 public static int Write(this IUVStream <ArraySegment <byte> > stream, Encoding enc, string text)
 {
     return(stream.Write(enc, text, null));
 }
Example #33
0
 public static int Write(this IUVStream <ArraySegment <byte> > stream, string text, Action <Exception> callback)
 {
     return(stream.Write(Encoding.Default, text, callback));
 }
Example #34
0
 public static void Write(this IUVStream <ArraySegment <byte> > stream, ArraySegment <byte> data)
 {
     stream.Write(data, null);
 }
Example #35
0
 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);
 }
Example #36
0
 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);
 }