0

I've found a library named tokio-serde which will send and receive data using tokio tcp socket and serialize and deserialize that using serde json.

There is two examples which will show how you can create a server and a client using this lib.

In this example you can see how to send data using serialized object.

serialized
        .send(json!({...}));

https://github.com/carllerche/tokio-serde/blob/master/examples/client.rs

So I've created a server app and stored serialized objects in a vector to send them messages later. But for any reason, these connections can be disconnected and peer client may not be available.

Here my problem is how can I detect when a client is disconnected, using serialized object ?

I've searched through documentation but couldn't find any solution.

Any help is really appreciated. Thank you so much.

Amoo Hesam
  • 461
  • 3
  • 13
  • 1
    In general you'd get an error when reading or writing to the stream if the other side has disconnected. Tokio-serde looks to just defer to the underlying error type, which will be [`std::io::Error`](https://doc.rust-lang.org/std/io/struct.Error.html#method.kind). Beyond that, you can probe it using [`.ready()`](https://docs.rs/tokio/1.3.0/tokio/net/struct.TcpStream.html#method.ready) on the `TcpStream` and detect any errors that way. – kmdreko Mar 18 '21 at 06:09

1 Answers1

1

As per @kmdreko comment. I tried to access underlying TcpSocket and detect the disconnect state using that.

Something like this would help to find if client is disconnected:

if serialized.get_ref().get_ref().as_ref().ready(Interest::WRITABLE).await.is_err() { 
    // Connection error
}

I know there is lots of get_ref() calls, but this is what I found for now.

Amoo Hesam
  • 461
  • 3
  • 13