Created
January 26, 2012 19:15
-
-
Save loudej/1684468 to your computer and use it in GitHub Desktop.
BodyDelegate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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