2

I'm using Marc Gravell's Pipelines.Sockets.Unofficial library to get an IDuplexPipe over a socket, but I'm finding a case where I need to modify a message header before flushing. If I call Advance, am I guaranteed it will never flush until I call FlushAsync? It's implied by the types (Advance is synchronous and Flush isn't) but I was hoping someone had a definite answer.

Also, can I sensibly write to a point in the buffer after I've advanced past it?

Julian Birch
  • 2,605
  • 1
  • 21
  • 36
  • 1
    minor note: at this point, you're really talking about the PipeWriter API which is System.IO.Pipelines, not specifically Pipelines.Sockets.Unofficial; happy to help, though! – Marc Gravell Jun 06 '19 at 16:42
  • FWIW: you can usually implement this using leased buffers of some kind, or by rewriting your code to compute headers ahead of time correctly (the latter is preferable but more work, and I understand the issues with things like length headers); I wonder if I could put together some kind of lightweight `IPipeWriter` that is not a true pipe but just a buffer shim, intended for this kind of scenario. Let me know if you think that would be helpful. Heck, I know it would help me :) – Marc Gravell Jun 07 '19 at 07:15
  • Definitely interesting. So the Memory objects would continue to be writable after advance but they'd be invalid after flush? – Julian Birch Jun 07 '19 at 15:01
  • 1
    now? or if I added an extra API? for the latter, that's not quite what I had in mind - but perhaps it would be better if you pinged me with a realistic example of what you'd actually like to be able to do in a perfect world – Marc Gravell Jun 07 '19 at 15:06

1 Answers1

2

No, this is not guaranteed. The Advance method logically commits data to the outbound pipe; once it is committed, if there is an active consumer on the pipe, it can read that value and process it onwards. Just to clarify, the FlushAsync() method specifically does two things that are only tangentially related:

  • it makes sure that a consumer is awakened (if it isn't already)
  • if there is back-pressure, it delays the producer until the consumer has cleared some of the back-pressure

can I sensibly write to a point in the buffer after I've advanced past it?

No

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900