client code
function SaveImageOnServer(imageFile, albumName)
{
let writer = connection.UploadStream("SaveImage", albumName);
for each chunk in imagefile {
try {
writer.WriteAsync(chunk);
}
catch {
connection.sendError(new Exception("whoopsies"));
break;
}
}
writer.SendComplete();
}
server code
private class ImageBuilder : UploadCallbacks
{
// the base class sets up some stuff
// like default error handling behavior
// and a `_parentConnection` field
this._data = new EmptyDataStream();
public OnReceive(chunk) {
_data.concatenate(chunk);
}
public OnComplete(message, albumName) {
_parentConnection.WriteImageToStore(this._data, albumName);
cleanUp();
}
}
// this method gets invoked by the call
public ChannelWriter<stuff> SaveImage(string albumName)
{
var channel = Channel.CreateNewByteChannel();
_ = channel.runWithCallbacks(
new ImageBuilder(parentConnection: this, albumName));
return channel.Writer;
}
private void WriteImageToStore(imageData, albumName) {
<blah blah api>.get(albumName).write(imageData);
}