Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to wrap NetworkStream with BufferedStream for async reading?

Tags:

c#

asyncsocket

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?

like image 233
pablo Avatar asked Mar 17 '12 01:03

pablo


1 Answers

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.

like image 116
usr Avatar answered Oct 22 '22 15:10

usr