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)); }
int Read_internal(byte[] buffer, int offset, int size, string message, SyncReadFunc func) { Context.LogDebug(4, message); try { int ret = func(buffer, offset, size); Context.LogDebug(4, "{0} done: {1}", message, ret); return(ret); } catch (Exception ex) { Context.LogDebug(4, "{0} failed: {1}", message, ex); throw; } }
int Read_internal(byte[] buffer, int offset, int size, string message, SyncReadFunc func) { LogDebug(message); try { int ret = func(buffer, offset, size); LogDebug($"{message} done: {ret}"); return(ret); } catch (Exception ex) { if (IgnoreErrors) { return(-1); } LogDebug($"{message} failed: {ex}"); throw; } }
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)); }