I’m currently working on an application that has a persistent TLS-wrapped (using native-tls) socket to a server for the duration of the client’s lifetime. I’m able to connect and read/write data just fine in the normal fashion as per the native-tls documentation.
Commns with the server is effectively async. There are not immediate responses from the server. So it’s not a “write some stuff, read some stuff back immediately” kind of scenario. I need to make the writing and reading async, separate from the user’s experience.
My intention was to spawn two threads:
- A “writer” thread, which receives messages on a channel that need to be written to the socket. This serialises the sending of messages without blocking the UI, leaving the UI to handle dealing with the jerk user (me).
- A “reader” thread, which listens on the socket to read data that comes in, when it comes in!
I’ve read through the
TcpStream documentation and seen a few other examples of how one might do this, however, I can’t seem to find any examples of how to do this with native-tls. eg. I can’t seem to clone the stream so that I can write on one thread while reading on another.
Does anyone know how I might go about doing this? The owernship/lifetime stuff is throwing me a bit too in this case. I’d also appreciate some critique on the thought process here. I’m still very new to Rust, so the learnings are appreciated.
Many thanks for your time.