Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any struct/trait in quiche that implement trait std::io::Read/Write? #984

Open
tubzby opened this issue Jun 25, 2021 · 1 comment
Labels
question Further information is requested

Comments

@tubzby
Copy link

tubzby commented Jun 25, 2021

I have a TCP server running in production that I'm now trying to migrate to QUIC.

The client/server example is some kind of complicated, I think it will be much helpful if quiche::Connection(or anything else) implement trait std::io::Read/Write, so anyone having some knowledge of TCPStream can easily adopt quiche in their own program.

I know I can wrap it myself, but it will be much convenient if it is right there.

Thanks!

@ghedo ghedo added the question Further information is requested label Jul 20, 2021
@ljluestc
Copy link

use quiche::Connection;
use std::io::{self, Read, Write};

// A wrapper to adapt `quiche::Connection` to `std::io::Read`.
pub struct QuicheRead<'a> {
    conn: &'a mut Connection,
}

impl<'a> QuicheRead<'a> {
    pub fn new(conn: &'a mut Connection) -> Self {
        QuicheRead { conn }
    }
}

impl<'a> Read for QuicheRead<'a> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        // Adapt the quiche connection to read data
        // This is a simplified example, in practice, you might need to handle more cases.
        match self.conn.recv(buf) {
            Ok(len) => Ok(len),
            Err(_) => Err(io::Error::new(io::ErrorKind::Other, "Read error")),
        }
    }
}

// A wrapper to adapt `quiche::Connection` to `std::io::Write`.
pub struct QuicheWrite<'a> {
    conn: &'a mut Connection,
}

impl<'a> QuicheWrite<'a> {
    pub fn new(conn: &'a mut Connection) -> Self {
        QuicheWrite { conn }
    }
}

impl<'a> Write for QuicheWrite<'a> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        // Adapt the quiche connection to write data
        // This is a simplified example, in practice, you might need to handle more cases.
        match self.conn.send(buf) {
            Ok(_) => Ok(buf.len()),
            Err(_) => Err(io::Error::new(io::ErrorKind::Other, "Write error")),
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        // QUIC doesn't have a direct flush method, but you might want to handle it
        Ok(())
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants