public void OnNextRead(AsyncReadHandler handler)
        {
            var myAction = new MyAction(handler);

            if (Interlocked.CompareExchange(ref readAction, myAction, null) != null)
            {
                throw new InvalidOperationException();
            }
        }
        async Task <int> ReadAsync(byte[] buffer, int offset, int count, string message,
                                   AsyncReadFunc func, AsyncReadHandler handler, CancellationToken cancellationToken)
        {
            Context.LogDebug(4, message);
            try {
                var ret = await handler(buffer, offset, count, func, cancellationToken).ConfigureAwait(false);

                Context.LogDebug(4, "{0} done: {1}", message, ret);
                return(ret);
            } catch (Exception ex) {
                Context.LogDebug(4, "{0} failed: {1}", message, ex);
                throw;
            }
        }
        public override Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            var message = string.Format("{0}.ReadAsync({1},{2})", Name, offset, count);

            AsyncReadFunc    asyncBaseRead    = base.ReadAsync;
            AsyncReadHandler asyncReadHandler = (b, o, c, func, ct) => func(b, o, c, ct);

            var action = Interlocked.Exchange(ref readAction, null);

            if (action?.AsyncRead != null)
            {
                message += " - action";
                return(ReadAsync(buffer, offset, count, message, asyncBaseRead, action.AsyncRead, cancellationToken));
            }

            return(ReadAsync(buffer, offset, count, message, asyncBaseRead, asyncReadHandler, cancellationToken));
        }
        async Task <int> ReadAsync(byte[] buffer, int offset, int count, string message,
                                   AsyncReadFunc func, AsyncReadHandler handler, CancellationToken cancellationToken)
        {
            LogDebug(message);
            try {
                var ret = await handler(buffer, offset, count, func, cancellationToken).ConfigureAwait(false);

                LogDebug($"{message} done: {ret}");
                return(ret);
            } catch (Exception ex) {
                if (IgnoreErrors)
                {
                    return(-1);
                }
                LogDebug($"{message} failed: {ex}");
                throw;
            }
        }
Example #5
0
			public ProcessAsyncReader (Process process, IntPtr handle, bool err_out)
			{
				this.process = process;
				this.handle = handle;
				stream = new FileStream (handle, FileAccess.Read, false);
				this.ReadHandler = new AsyncReadHandler (AddInput);
				this.err_out = err_out;
			}
 public MyAction(AsyncReadHandler handler)
 {
     AsyncRead = handler;
 }