add aquatic_common_tcp crate, move common functionality there

This commit is contained in:
Joakim Frostegård 2020-07-02 16:34:36 +02:00
parent 1eaf2a0351
commit 2e53a2adc1
14 changed files with 163 additions and 127 deletions

View file

@ -1,6 +1,6 @@
use std::net::{SocketAddr};
use std::io::{Read, Write};
use std::io::ErrorKind;
use std::io::{Read, Write};
use either::Either;
use hashbrown::HashMap;
@ -9,81 +9,12 @@ use mio::Token;
use mio::net::TcpStream;
use native_tls::{TlsAcceptor, TlsStream, MidHandshakeTlsStream};
use aquatic_common_tcp::network::stream::Stream;
use crate::common::*;
use crate::protocol::{Request, Response};
pub enum Stream {
TcpStream(TcpStream),
TlsStream(TlsStream<TcpStream>),
}
impl Stream {
#[inline]
pub fn get_peer_addr(&self) -> SocketAddr {
match self {
Self::TcpStream(stream) => stream.peer_addr().unwrap(),
Self::TlsStream(stream) => stream.get_ref().peer_addr().unwrap(),
}
}
}
impl Read for Stream {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ::std::io::Error> {
match self {
Self::TcpStream(stream) => stream.read(buf),
Self::TlsStream(stream) => stream.read(buf),
}
}
/// Not used but provided for completeness
#[inline]
fn read_vectored(
&mut self,
bufs: &mut [::std::io::IoSliceMut<'_>]
) -> ::std::io::Result<usize> {
match self {
Self::TcpStream(stream) => stream.read_vectored(bufs),
Self::TlsStream(stream) => stream.read_vectored(bufs),
}
}
}
impl Write for Stream {
#[inline]
fn write(&mut self, buf: &[u8]) -> ::std::io::Result<usize> {
match self {
Self::TcpStream(stream) => stream.write(buf),
Self::TlsStream(stream) => stream.write(buf),
}
}
/// Not used but provided for completeness
#[inline]
fn write_vectored(
&mut self,
bufs: &[::std::io::IoSlice<'_>]
) -> ::std::io::Result<usize> {
match self {
Self::TcpStream(stream) => stream.write_vectored(bufs),
Self::TlsStream(stream) => stream.write_vectored(bufs),
}
}
#[inline]
fn flush(&mut self) -> ::std::io::Result<()> {
match self {
Self::TcpStream(stream) => stream.flush(),
Self::TlsStream(stream) => stream.flush(),
}
}
}
#[derive(Debug)]
pub enum RequestParseError {
NeedMoreData,
@ -296,4 +227,4 @@ impl Connection {
}
pub type ConnectionMap<'a> = HashMap<Token, Connection>;
pub type ConnectionMap = HashMap<Token, Connection>;