How to read all available data from WinRT StreamSocket and start up input stream?

I want to read all the data that is currently waiting on a socket before writing new data to it. The reading methods in WinRT are asynchronous, so I can’t just whileuntil the socket is empty. Since I want to drop data on the socket, I do not want to use a reader, but I directly read data from the IInputStreamsocket.

Something like this pseudo code:

function emptySocket(inputStream, timeout, buffer) {
  if (!buffer) {
    buffer = new Windows.Storage.Streams.Buffer(4096);
  }
  WinJS.Promise.timeout(timeout, inputStream.readAsync(buffer, 4096, Windows.Storage.Streams.InputStreamOptions.partial)
  .then(function(buffer) {
    if (buffer.length === 0) return;
    emptySocket(inputStream, timeout, buffer);
  }), function (error) {
    return;
  });
}

The timeout interrupts the wait operation when there is nothing else on the socket. Windows.Storage.Streams.InputStreamOptions.partialdoesn't seem to return if nothing is in the socket.

btw: What is the difference between Windows.Storage.Streams.InputStreamOptions.partialand Windows.Storage.Streams.InputStreamOptions.readAhead?

+1
source share

All Articles