-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Add Stream.CreateConnectedStreams API #43101
Comments
I haven't used them but my understanding is that this is what Pipelines are for? Or you need to pass to API that expects Stream? |
Right, Pipe is basically this and you can use AsStream to get the Stream from the pipe. |
Right, Stream is the lingua-franca of our APIs and is used everywhere. The various places Geoff refers to specifically includes a bunch of places we're testing Streams, e.g. wrapping SslStream around another Stream.
Pipe is one-directional. To use Pipe here, you actually need two of them, then create four streams via AsStream, and then write a custom stream type that wraps two of them and maps the wrapper's reads to one and writes to the other, then creating two instances of those for each end. |
Ah I see this is talking about an IDuplexPipe 😄 |
Yeah, it's basically "new Pipe()" for Streams. And supports unidirectional or bidirectional. |
This API doesn't allow for the scenario where I have a duplex stream and need to read the entire buffer before writing a response -- what do you think about returning a type with those capabilities? |
Because it doesn't have a way to EndWrite, short of Close/Dispose? Yeah, I agree adding something like that would be interesting. It would be nice if we had a common way to do this across NetworkStream, QuicStream, this Stream, etc... |
Do you have an API proposal? |
This needs some play time, but how about I start us off with: abstract class DuplexStream : Stream
{
public abstract void ShutdownWrites();
public static (DuplexStream, DuplexStream) CreateConnectedPair(int maxBufferSize = 16384);
}
class NetworkStream : DuplexStream {}
class QuicStream : DuplexStream {} A better name would be useful as QuicStream might not be duplex. |
I think there are essentially three ways this could be done:
|
I filed a new issue for ShutdownWrite, see linked issue -- please add your comments! |
Background and Motivation
It's often convenient to have a simple in-memory producer/consumer stream. We have a few variations of this in our test code, e.g. VirtualNetwork. You can also achieve this by using a loopback socket or a named pipe; however these approaches are unnecessarily difficult to use and the perf likely isn't great, since it's copying buffers through the OS.
Proposed API
If duplex is true, then both streams can both read and write. If duplex is false, then stream1 is write-only and stream2 is read-only. Alternatively, we could have two separate methods for clarity.
We may want to have a distinguished type for the returned Streams, like ConnectedStream, just in case we ever want to add more APIs to it.
Usage Examples
The text was updated successfully, but these errors were encountered: