9

I'm using NetworkStream.BeginRead to read asynchronously from a Socket.

But it is much faster if you actually wrap the network stream with a BufferedStream.

My question: NetworkStream.BeginRead internally invokes to Socket.BeginReceive and the whole stack of async IO (CompletionPorts on Windows and so on). Does the same happen when BufferedStream is in the middle?

remio
  • 1,242
  • 2
  • 15
  • 36
pablo
  • 6,392
  • 4
  • 42
  • 62
  • Is there anything visible/understandable from Reflector? – remio Mar 18 '12 at 20:57
  • It doesn't implement the async calls (the BufferedStream) so I'm not sure whether somehow it will go to NetworkStream or to the base Stream, which can do async, but afaik not using IOCP – pablo Mar 18 '12 at 21:00

1 Answers1

2

BufferedStream does not support efficient async IO. It uses the default implementation inherited from the Stream class. It will issue synchronous IOs on the thread-pool. So you won't get IO completion ports doing that. You need to do this work yourself. If you are using C# 5 you can nearly reuse BufferedStream's implementation and try to slap async and awaits on it.

usr
  • 168,620
  • 35
  • 240
  • 369
  • What is this based on? Looking at BufferedStream sources it seems that async operations are properly implemented by calling async operations on the underlying stream? – Shay Rojansky Sep 20 '14 at 10:14
  • It's based on .NET 4.0. On 4.5 this is no longer true. Good point. – usr Sep 20 '14 at 15:20