Calling SslStream.Read and SslStream.Write concurrently on separate threads

Created at 03 Oct 2015, 18:06
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
YU

YuriyGulyayev

Joined 08.09.2015

Calling SslStream.Read and SslStream.Write concurrently on separate threads
03 Oct 2015, 18:06


Your C# sample calls SslStream.Read and SslStream.Write concurrently on separate threads. Is it safe to do that? Nothing in the SslStream documentation suggests that it is. It only says that SslStream is not guaranteed to be thread safe.

Note that this same thing is documented to be safe for NetworkStream.
 


@YuriyGulyayev
Replies

moneybiz
03 Oct 2015, 19:50

>> SslStream is not guaranteed to be thread safe.

This means if you want to access the object from different threads it's up to you to implement the concurrency handling mechanisms.

Just lock some common object and then Read/Write. When that object is locked only one thread can work on it. The others will wait till the lock is released.

private readonly object _gate = new object();

lock (_gate)
{
   stream.Write('test');
}

 


@moneybiz

YuriyGulyayev
07 Oct 2015, 01:16

RE:

Thank you for your advice, but it can't work for me because stream.Read must run continuously to receive market data and other messages. And once in a while I need to call stream.Write on another thread to send a message, without interrupting the reading.


@YuriyGulyayev

moneybiz
07 Oct 2015, 02:17

You confused me in the begining, actually you don't need to worry about performing read and write simultaneously. It's built-in supported.

"SslStream is not guaranteed to be thread safe" must be for the public members. You don't need to worry about that, you'll be using NetworkStream object.

TcpClient class has a GetStream() method which returns a NetworkStream object.

The documentation says:

Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

 

TcpClient client;

var networkStream  = client.GetStream();
var sslStream = new SslStream(ns, false);

 


@moneybiz

YuriyGulyayev
07 Oct 2015, 18:47

RE:

Unfortunately, the docs doesn't say the same about SslStream. Thread safety of NetworkStream doesn't imply that of SslStream unless SslStream is also designed to be thread safe. And if it is desiged to be thread safe, that is supposed to be reflected in docs. Maybe someone at Microsoft fell asleep at a wrong time...


@YuriyGulyayev

moneybiz
07 Oct 2015, 19:55

How do you create the SslStream?

First you need to establish a tcp connection, then you get the network stream from that connection which you use to create a ssl stream.

I think ssl stream's function is to encrypt messages and send/receive them over a network stream All the network stuff is handled by network stream.

What kind of error message do you receive to claim that it's not thread safe?


@moneybiz

YuriyGulyayev
07 Oct 2015, 20:53

RE:

I do it right and it works. But I am usiure about thread safety.

I'd like Spotware to respond if/why they believe that their sample is thread safe. In particular, if SslStream.Read and SslStream.Write are called concurrently on 2 separate threads.


@YuriyGulyayev