Skip to content

Instantly share code, notes, and snippets.

@loudej
Created January 26, 2012 19:15
Show Gist options
  • Save loudej/1684468 to your computer and use it in GitHub Desktop.
Save loudej/1684468 to your computer and use it in GitHub Desktop.
BodyDelegate
// current delegate
Action /*cancel*/ BodyDelegate(
Action<ArraySegment<byte>, Action, bool> next,
Action<Exception> error,
Action complete);
// node-like delegate
void BodyDelegate(
Action<ArraySegment<byte>, bool> write,
Action<Action, bool> flush,
Action<Exception> end,
CancellationToken cancel);
BodyDelegate WriteAndEndNonBlockingWithCatch()
{
return (write, flush, end, cancel)=>
{
try
{
write(data1);
write(data2);
end(null);
}
catch(Exception ex)
{
end(ex);
}
};
}
BodyDelegate WriteAndEndWithAwaitableBackPressure()
{
return async (write, flush, end, cancel)=>
{
try
{
await WriteAsync(write, data1, flush);
await WriteAsync(write, data2, flush);
end(null);
}
catch(Exception ex)
{
end(ex);
}
};
}
// helper method for async write
Task WriteAsync(Action<ArraySegment<byte>, bool> write, ArraySegment<byte> data, Action<Action, bool> flush)
{
if (write(data))
{
if (flush(()=>{ /* signal incomplete awaitable here */ }))
{
// return incomplete awaitable here
}
}
// return already-completed awaitable here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment