devlooped / WebSocketPipe

System.IO.Pipelines API adapter for System.Net.WebSockets

Home Page:https://clarius.org/WebSocketPipe

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simplify threading aspects of outgoing PipeWriter

kzu opened this issue · comments

We currently use a different Pipe for the outgoing bytes, which means we need to account for two threads active simultaneously, one for reading and one for writing (since we write to its Output and then need another loop for reading from the output pipe for ultimate sending via the websocket.

This introduces subtle and non-obvious dependencies between read/write, which are hard to document and easy to trip over. A much better approach would be to expose a PipeWriter that simply interfaces directly with the underlying websocket, so that writing to it causes an immediate SendAsync over the websocket of a fully completed message (that is, with EndOfMessage=true). This seems like the most common and useful path, and avoids multiple issues that are otherwise quite non-trivial to resolve.

This means the RunAsync is basically independent of writing to the output too, since the former only deals with the input/incoming side of the websocket exclusively. It's up to the user to spin up a separate thread for that purpose or not, at that point.