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); }
void Write_internal(byte[] buffer, int offset, int size, string message, SyncWriteFunc func) { Context.LogDebug(4, message); try { func(buffer, offset, size); Context.LogDebug(4, "{0} done", message); } catch (Exception ex) { Context.LogDebug(4, "{0} failed: {1}", message, ex); throw; } }
void Write_internal(byte[] buffer, int offset, int size, string message, SyncWriteFunc func) { LogDebug(message); try { func(buffer, offset, size); 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); }