예제 #1
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            var message = string.Format("{0}.BeginRead({1},{2})", Name, offset, size);

            Context.LogDebug(4, message);

            AsyncReadFunc asyncBaseRead = (b, o, s, _) => Task.Factory.FromAsync(
                (ca, st) => base.BeginRead(b, o, s, ca, st),
                (result) => base.EndRead(result), null);

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

            if (action?.AsyncRead == null)
            {
                return(base.BeginRead(buffer, offset, size, callback, state));
            }

            message += " - action";

            AsyncReadFunc readFunc = (b, o, s, ct) => action.AsyncRead(b, o, s, asyncBaseRead, ct);

            try {
                Context.LogDebug(4, message);
                var readTask = readFunc(buffer, offset, size, CancellationToken.None);
                Context.LogDebug(4, "{0} got task: {1}", message, readTask.Status);
                return(TaskToApm.Begin(readTask, callback, state));
            } catch (Exception ex) {
                Context.LogDebug(4, "{0} failed: {1}", message, ex);
                throw;
            }
        }
예제 #2
0
        public override int Read(byte[] buffer, int offset, int size)
        {
            var message = $"{Name}.Read({offset},{size})";

            if (RequireAsync)
            {
                throw Context.AssertFail($"{message}: async API required.");
            }

            SyncReadFunc syncRead         = (b, o, s) => base.Read(b, o, s);
            SyncReadFunc originalSyncRead = syncRead;

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

            if (action?.AsyncRead != null)
            {
                message += " - action";

                AsyncReadFunc asyncBaseRead = (b, o, s, _) => Task.Factory.FromAsync(
                    (callback, state) => originalSyncRead.BeginInvoke(b, o, s, callback, state),
                    (result) => originalSyncRead.EndInvoke(result), null);

                syncRead = (b, o, s) => action.AsyncRead(b, o, s, asyncBaseRead, CancellationToken.None).Result;
            }

            return(Read_internal(buffer, offset, size, message, syncRead));
        }
예제 #3
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            var message = $"{Name}.BeginRead({offset},{size})";

            LogDebug(message);

            if (RequireAsync && !AllowBeginEndAsync)
            {
                throw Context.AssertFail($"{message}: async API required.");
            }

            AsyncReadFunc asyncBaseRead = (b, o, s, _) => Task.Factory.FromAsync(
                (ca, st) => base.BeginRead(b, o, s, ca, st),
                (result) => base.EndRead(result), null);

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

            if (action?.AsyncRead == null)
            {
                return(base.BeginRead(buffer, offset, size, callback, state));
            }

            message += " - action";

            AsyncReadFunc readFunc = (b, o, s, ct) => action.AsyncRead(b, o, s, asyncBaseRead, ct);

            try {
                var readTask = readFunc(buffer, offset, size, CancellationToken.None);
                LogDebug($"{message} got task: {readTask.Status}");
                return(TaskToApm.Begin(readTask, callback, state));
            } catch (Exception ex) {
                LogDebug($"{message} failed: {ex}");
                throw;
            }
        }
예제 #4
0
        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;
            }
        }
예제 #5
0
        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));
        }
예제 #6
0
        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;
            }
        }
예제 #7
0
        public override int Read(byte[] buffer, int offset, int size)
        {
            var message = string.Format("{0}.Read({1},{2})", Name, offset, size);

            SyncReadFunc syncRead         = (b, o, s) => base.Read(b, o, s);
            SyncReadFunc originalSyncRead = syncRead;

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

            if (action?.AsyncRead != null)
            {
                message += " - action";

                AsyncReadFunc asyncBaseRead = (b, o, s, _) => Task.Factory.FromAsync(
                    (callback, state) => originalSyncRead.BeginInvoke(b, o, s, callback, state),
                    (result) => originalSyncRead.EndInvoke(result), null);

                syncRead = (b, o, s) => action.AsyncRead(b, o, s, asyncBaseRead, CancellationToken.None).Result;
            }

            return(Read_internal(buffer, offset, size, message, syncRead));
        }