Skip to content

Instantly share code, notes, and snippets.

@DylanDmitri
Last active May 29, 2018 19:29
Show Gist options
  • Save DylanDmitri/79de433a4366064a9ee473cad8c60637 to your computer and use it in GitHub Desktop.
Save DylanDmitri/79de433a4366064a9ee473cad8c60637 to your computer and use it in GitHub Desktop.

Pseudocode ideas

for client -> server streaming


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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment