0xDECAFBAD
0xDECAFBAD

Reputation: 637

System.IO.Pipelines: Receiving continuous messages

I'm fairly new to the concept of Pipes and I like to implement a client which receives messages from a server. The server pushes these messages automatically using a fairly constant delay of X seconds. I do understand the basics of Pipes and their usage-scenarios - also, there is a great tutorial on the MS Dev-Blog. The following question directly refers to this tutorial: I am pretty sure I can use one pipe and the two tasks for handling all messages sent from the server until the connection closes, but there are two break statements within the loop in the tutorial, where the second statement confuses me regarding my assumption to handle all messages using the two tasks - I understand the first condition

int bytesRead = await socket.ReceiveAsync(memory, SocketFlags.None);
if (bytesRead == 0) { break; }

but the second one completely confuses me:

FlushResult result = await writer.FlushAsync();
if (result.IsCompleted) { break; }

What exactly happens here? In which scenarios is result.IsCompleted true?

Upvotes: 1

Views: 649

Answers (1)

Patrick Beynio
Patrick Beynio

Reputation: 858

as the documentation states:
IsCompleted gets a value that indicates the reader is no longer reading data written to the PipeWriter.

So this should be a test if the flush and PipeReader.ReadAsync succeed.

Like it's described in your linked blog post:

When the call to PipeReader.ReadAsync() returns, we get a ReadResult which contains 2 important pieces of information, the data that was read in the form of ReadOnlySequence and a bool IsCompleted that lets the reader know if the writer is done writing (EOF).

just the other way around for the PipeWriter instead of the PipeReader. This seems to be needed since they run in parallel.

Upvotes: 2

Related Questions