开发者

SslStream asynchronous methods always return an IAsyncResult where CompletedSynchronously is True. Why?

开发者 https://www.devze.com 2023-03-23 02:12 出处:网络
I use TcpClient BeginConnect to create a socket connection to a TCP server.That returns an IAsyncResult where CompletedSynchronously is false which is what I would expect at least most of the time.I w

I use TcpClient BeginConnect to create a socket connection to a TCP server. That returns an IAsyncResult where CompletedSynchronously is false which is what I would expect at least most of the time. I wrap the sockets NetworkStream in an SslStream call BeginAuthenticateAsClient which returns an IAsyncResult where CompletedSynchronously is true. This really surprised me since this method makes multiple round trips to the server to do the handshake. Every BeginWrite & BeginRead I do from that point on returns an IAsyncResult where CompletedSynchronously is true.

I suspect that the implementation of SslStream does not support async IO. I was going to confirm this by going to the server without the SSL wrapper but unfortunately that server requires SSL so I couldn't.

If SslStream does not support async io I plan to wrap the SslStream around an in memory stream and then pass that along to NetworkStream BeginRead/BeginWrite. Unfortunately I won't be able to do the hands开发者_运维百科hake async though.

Does anybody know if SslStream in fact does not support async IO? Is there a workaround to get it to?

Thanks.

dan


I broke the SslStream apart using Reflector and I can confirm that, at the very least, BeginAuthenticateAsClient, BeginWrite, and BeginRead all complete synchronously as you have observed. The callback method, however, is pushed onto the ThreadPool so that will come in asynchronously, but the actual read or write is already finished before BeginWrite or BeginRead return.

The odd thing about this is that it appears that SslStream gets everything going in asynchronous mode, but then waits for the read or write to complete before returning. Why does it do this? I have no idea!

It does not appear that there is any way to change this particular behavior of SslStream.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号