Use TokioIo wrapper instead of Box<dyn Fullstream>

This commit is contained in:
Mike Dilger 2024-06-22 17:24:21 +12:00
parent 6a0fe2c0e0
commit b3a726695f
2 changed files with 16 additions and 16 deletions

View File

@ -3,7 +3,6 @@ use chorus::counting_stream::CountingStream;
use chorus::error::Error;
use chorus::globals::GLOBALS;
use chorus::ip::HashedPeer;
use chorus::FullStream;
use std::env;
use std::fs::OpenOptions;
use std::io::Read;
@ -109,10 +108,13 @@ async fn main() -> Result<(), Error> {
let maybe_tls_acceptor_clone = maybe_tls_acceptor.clone();
tokio::spawn(async move {
let stream: Box<dyn FullStream> = match maybe_tls_acceptor_clone {
match maybe_tls_acceptor_clone {
Some(tls_acceptor) => {
match tls_acceptor.accept(counting_stream).await {
Ok(stream) => Box::new(stream),
Ok(stream) => {
let io = hyper_util::rt::TokioIo::new(stream);
chorus::serve(io, hashed_peer).await;
},
Err(e) => {
log::error!(
target: "Client",
@ -122,9 +124,11 @@ async fn main() -> Result<(), Error> {
}
}
},
None => Box::new(counting_stream)
None => {
let io = hyper_util::rt::TokioIo::new(counting_stream);
chorus::serve(io, hashed_peer).await;
}
};
chorus::serve(stream, hashed_peer).await;
});
}
};

View File

@ -9,7 +9,6 @@ pub mod tls;
pub mod web;
use crate::config::{Config, FriendlyConfig};
use crate::counting_stream::CountingStream;
use crate::error::{ChorusError, Error};
use crate::globals::GLOBALS;
use crate::ip::{HashedIp, HashedPeer, IpData, SessionExit};
@ -39,25 +38,22 @@ use std::sync::atomic::Ordering;
use std::time::Duration;
use textnonce::TextNonce;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::TcpStream;
use tokio::time::Instant;
use tokio_rustls::server::TlsStream;
use tungstenite::protocol::WebSocketConfig;
use tungstenite::Message;
pub trait FullStream: AsyncRead + AsyncWrite + Unpin + Send {}
impl FullStream for CountingStream<TcpStream> {}
impl FullStream for TlsStream<CountingStream<TcpStream>> {}
/// Serve a single network connection
pub async fn serve(stream: Box<dyn FullStream>, peer: HashedPeer) {
pub async fn serve<T>(stream: TokioIo<T>, peer: HashedPeer)
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
// Serve the network stream with our http server and our ChorusService
let service = ChorusService { peer };
let io = hyper_util::rt::TokioIo::new(stream);
let http1builder = GLOBALS.http1builder.clone();
let connection = http1builder.serve_connection(io, service).with_upgrades();
let connection = http1builder
.serve_connection(stream, service)
.with_upgrades();
// If our service exits with an error, log the error
if let Err(he) = connection.await {