Skip to content

Prevent Deref confusion #33

@half2me

Description

@half2me

I can't use the provided tcp stream wrapper since i need to send some custom data immediately after reconnection happens.

I tried to follow the examples and implement the trait:

use std::error::Error;
use std::io;
use std::future::Future;
use std::pin::Pin;
use stubborn_io::tokio::{StubbornIo, UnderlyingIo};
use tokio::net::TcpStream;
use tokio::io::{AsyncWriteExt};
use derived_deref::{Deref, DerefMut};
use tokio::time::{Duration, sleep};

#[derive(Deref, DerefMut)]
struct DurableTCPStream(TcpStream);

impl UnderlyingIo<String> for DurableTCPStream {
    fn establish(addr: String) -> Pin<Box<dyn Future<Output = io::Result<Self>> + Send>> {
        Box::pin(async move {
            let parts: Vec<&str> = addr.split('/').collect();
            println!("connecting to {}", parts[0]);
            let mut stream = TcpStream::connect(parts[0]).await?;

            if parts.len() > 1 {
                // hello message was specified, use it
                println!("sending login: {}", parts[1]);
                stream.write_all(format!("{}\n", parts[1]).as_ref()).await?;
            }
            Ok(DurableTCPStream(stream))
        })
    }
}

type StubbornTCP = StubbornIo<DurableTCPStream, String>;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let mut stream = StubbornTCP::connect(String::from("localhost:9999/logincmd")).await?;

    loop {
        println!("sending msg");
        match stream.write_all(b"hello world!\n").await {
            Ok(_) => (),
            Err(e) => {
                println!("{}", e);
            }
        }
        sleep(Duration::from_millis(1000)).await;
    }
}

When running with the debugger it seems like calling write_all (I've also tried with write) directly calls the underlying poll_write() completely bypassing the retry logic.

What am I missing?

I've put a breakpoint on poll_write but it never gets called.
Screenshot of debugger

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions