Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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();
		};
	}
Ejemplo n.º 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();
     };
 }
Ejemplo n.º 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);
         }
     });
 }
Ejemplo n.º 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();
        };
    }
Ejemplo n.º 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();
        };
    }
Ejemplo n.º 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;
	}
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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));
    }
Ejemplo n.º 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();
        }
Ejemplo n.º 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));
    }
Ejemplo n.º 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();
            }
        };
    }
Ejemplo n.º 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();
            }
        };
    }
Ejemplo n.º 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();
        }
Ejemplo n.º 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();
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 24
0
 public static int Write(this IUVStream <ArraySegment <byte> > stream, string text)
 {
     return(stream.Write(Encoding.Default, text));
 }
Ejemplo n.º 25
0
 public static async Task <string> ReadStringAsync(this IUVStream <ArraySegment <byte> > stream)
 {
     return(await ReadStringAsync(stream, Encoding.UTF8));
 }
Ejemplo n.º 26
0
 public static void End(this IUVStream <ArraySegment <byte> > stream, byte[] data, Action <Exception> callback)
 {
     stream.Write(data);
     stream.Shutdown(callback);
 }
Ejemplo n.º 27
0
 public static void End(this IUVStream <ArraySegment <byte> > stream, byte[] data, int index, int count)
 {
     stream.End(data, index, count, null);
 }
Ejemplo n.º 28
0
 public static void Shutdown(this IUVStream <ArraySegment <byte> > stream, Action callback)
 {
     stream.Shutdown((_) => callback());
 }
Ejemplo n.º 29
0
 public static void Shutdown(this IUVStream <ArraySegment <byte> > stream)
 {
     stream.Shutdown(null);
 }
Ejemplo n.º 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));
 }
Ejemplo n.º 31
0
 public static void Pump(this IUVStream readStream, IUVStream writeStream)
 {
     Pump(readStream, writeStream, null);
 }
Ejemplo n.º 32
0
 public static int Write(this IUVStream <ArraySegment <byte> > stream, Encoding enc, string text)
 {
     return(stream.Write(enc, text, null));
 }
Ejemplo n.º 33
0
 public static int Write(this IUVStream <ArraySegment <byte> > stream, string text, Action <Exception> callback)
 {
     return(stream.Write(Encoding.Default, text, callback));
 }
Ejemplo n.º 34
0
 public static void Write(this IUVStream <ArraySegment <byte> > stream, ArraySegment <byte> data)
 {
     stream.Write(data, null);
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 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);
 }