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

            Context.LogDebug(4, message);

            AsyncWriteFunc asyncBaseWrite = (b, o, s, _) => Task.Factory.FromAsync(
                (ca, st) => base.BeginWrite(b, o, s, ca, st),
                (result) => base.EndWrite(result), null);

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

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

            message += " - action";

            AsyncWriteFunc writeFunc = (b, o, s, ct) => action.AsyncWrite(b, o, s, asyncBaseWrite, ct);

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

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

            SyncWriteFunc syncWrite         = (b, o, s) => base.Write(b, o, s);
            SyncWriteFunc originalSyncWrite = syncWrite;

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

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

                AsyncWriteFunc asyncBaseWrite = (b, o, s, _) => Task.Factory.FromAsync(
                    (callback, state) => originalSyncWrite.BeginInvoke(b, o, s, callback, state),
                    (result) => originalSyncWrite.EndInvoke(result), null);

                syncWrite = (b, o, s) => action.AsyncWrite(b, o, s, asyncBaseWrite, CancellationToken.None).Wait();
            }

            Write_internal(buffer, offset, size, message, syncWrite);
        }
        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            var message = $"{Name}.BeginWrite({offset},{size})";

            LogDebug(message);

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

            AsyncWriteFunc asyncBaseWrite = (b, o, s, _) => Task.Factory.FromAsync(
                (ca, st) => base.BeginWrite(b, o, s, ca, st),
                (result) => base.EndWrite(result), null);

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

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

            message += " - action";

            AsyncWriteFunc writeFunc = (b, o, s, ct) => action.AsyncWrite(b, o, s, asyncBaseWrite, ct);

            try {
                var writeTask = writeFunc(buffer, offset, size, CancellationToken.None);
                LogDebug($"{message} got task: {writeTask.Status}");
                return(TaskToApm.Begin(writeTask, callback, state));
            } catch (Exception ex) {
                LogDebug($"{message} failed: {ex}");
                throw;
            }
        }
        async Task WriteAsync(byte[] buffer, int offset, int count, string message,
                              AsyncWriteFunc func, AsyncWriteHandler handler, CancellationToken cancellationToken)
        {
            Context.LogDebug(4, message);
            try {
                await handler(buffer, offset, count, func, cancellationToken).ConfigureAwait(false);

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

            AsyncWriteFunc    asyncBaseWrite    = base.WriteAsync;
            AsyncWriteHandler asyncWriteHandler = (b, o, c, func, ct) => func(b, o, c, ct);

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

            if (action?.AsyncWrite != null)
            {
                message += " - action";
                return(WriteAsync(buffer, offset, count, message, asyncBaseWrite, action.AsyncWrite, cancellationToken));
            }

            return(WriteAsync(buffer, offset, count, message, asyncBaseWrite, asyncWriteHandler, cancellationToken));
        }
        async Task WriteAsync(byte[] buffer, int offset, int count, string message,
                              AsyncWriteFunc func, AsyncWriteHandler handler, CancellationToken cancellationToken)
        {
            LogDebug(message);
            try {
                await handler(buffer, offset, count, func, cancellationToken).ConfigureAwait(false);

                LogDebug($"{message} done");
            } catch (Exception ex) {
                if (IgnoreErrors)
                {
                    return;
                }
                LogDebug($"{message} failed: {ex}");
                throw;
            }
        }
        public override void Write(byte[] buffer, int offset, int size)
        {
            var message = string.Format("{0}.Write({1},{2})", Name, offset, size);

            SyncWriteFunc syncWrite         = (b, o, s) => base.Write(b, o, s);
            SyncWriteFunc originalSyncWrite = syncWrite;

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

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

                AsyncWriteFunc asyncBaseWrite = (b, o, s, _) => Task.Factory.FromAsync(
                    (callback, state) => originalSyncWrite.BeginInvoke(b, o, s, callback, state),
                    (result) => originalSyncWrite.EndInvoke(result), null);

                syncWrite = (b, o, s) => action.AsyncWrite(b, o, s, asyncBaseWrite, CancellationToken.None).Wait();
            }

            Write_internal(buffer, offset, size, message, syncWrite);
        }