I'm using a System.IO.Pipelines.Pipe in .NET 5 to transfer data from a stream to a reader, and now I'm thinking I'd like to have several independent readers that would all process same stream of data. Is there a variant of a Pipe that would let me have several equal readers?
Alternatively, if I do something like this, will pipe2 use the same segment (i.e. avoid needless copying of data)?
Memory<byte> memory = pipe1.Writer.GetMemory(10240);
int receivedBytesCount = await stream.ReadAsync(memory, token);
pipe1.Writer.Advance(receivedBytesCount);
await pipe2.Writer.WriteAsync(memory);
pipe2.Writer.Advance(receivedBytesCount);
No, Pipes do not support multiple concurrent readers.
You might be able to build a "Tee" Pipe by:
This would be a non-trivial undertaking with plenty of places for things to go wrong. For example: you might need to place a lock around the Sequence as I'm not sure if it supports concurrent reading/writing.