mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 10:15:31 +00:00
add aquatic_common_tcp crate, move common functionality there
This commit is contained in:
parent
1eaf2a0351
commit
2e53a2adc1
14 changed files with 163 additions and 127 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use std::net::{SocketAddr, IpAddr};
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use flume::{Sender, Receiver};
|
||||
|
|
@ -34,7 +34,7 @@ pub enum PeerStatus {
|
|||
}
|
||||
|
||||
|
||||
// identical to ws version - FIXME only if bytes left is optional
|
||||
// almost identical to ws version - FIXME only if bytes left is optional
|
||||
impl PeerStatus {
|
||||
/// Determine peer status from announce event and number of bytes left.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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>;
|
||||
|
|
@ -1,3 +1,6 @@
|
|||
pub mod connection;
|
||||
pub mod utils;
|
||||
|
||||
use std::time::Duration;
|
||||
use std::io::ErrorKind;
|
||||
|
||||
|
|
@ -11,13 +14,50 @@ use crate::common::*;
|
|||
use crate::config::Config;
|
||||
use crate::protocol::*;
|
||||
|
||||
pub mod connection;
|
||||
pub mod utils;
|
||||
|
||||
use connection::*;
|
||||
use utils::*;
|
||||
|
||||
|
||||
|
||||
fn accept_new_streams(
|
||||
listener: &mut TcpListener,
|
||||
poll: &mut Poll,
|
||||
connections: &mut ConnectionMap,
|
||||
valid_until: ValidUntil,
|
||||
poll_token_counter: &mut Token,
|
||||
){
|
||||
loop {
|
||||
match listener.accept(){
|
||||
Ok((mut stream, _)) => {
|
||||
poll_token_counter.0 = poll_token_counter.0.wrapping_add(1);
|
||||
|
||||
if poll_token_counter.0 == 0 {
|
||||
poll_token_counter.0 = 1;
|
||||
}
|
||||
|
||||
let token = *poll_token_counter;
|
||||
|
||||
remove_connection_if_exists(connections, token);
|
||||
|
||||
poll.registry()
|
||||
.register(&mut stream, token, Interest::READABLE)
|
||||
.unwrap();
|
||||
|
||||
let connection = Connection::new(valid_until, stream);
|
||||
|
||||
connections.insert(token, connection);
|
||||
},
|
||||
Err(err) => {
|
||||
if err.kind() == ErrorKind::WouldBlock {
|
||||
break
|
||||
}
|
||||
|
||||
info!("error while accepting streams: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// will be almost identical to ws version
|
||||
pub fn run_socket_worker(
|
||||
config: Config,
|
||||
|
|
@ -119,54 +159,14 @@ pub fn run_poll_loop(
|
|||
}
|
||||
|
||||
|
||||
// will be identical to ws version
|
||||
fn accept_new_streams(
|
||||
listener: &mut TcpListener,
|
||||
poll: &mut Poll,
|
||||
connections: &mut ConnectionMap,
|
||||
valid_until: ValidUntil,
|
||||
poll_token_counter: &mut Token,
|
||||
){
|
||||
loop {
|
||||
match listener.accept(){
|
||||
Ok((mut stream, _)) => {
|
||||
poll_token_counter.0 = poll_token_counter.0.wrapping_add(1);
|
||||
|
||||
if poll_token_counter.0 == 0 {
|
||||
poll_token_counter.0 = 1;
|
||||
}
|
||||
|
||||
let token = *poll_token_counter;
|
||||
|
||||
remove_connection_if_exists(connections, token);
|
||||
|
||||
poll.registry()
|
||||
.register(&mut stream, token, Interest::READABLE)
|
||||
.unwrap();
|
||||
|
||||
let connection = Connection::new(valid_until, stream);
|
||||
|
||||
connections.insert(token, connection);
|
||||
},
|
||||
Err(err) => {
|
||||
if err.kind() == ErrorKind::WouldBlock {
|
||||
break
|
||||
}
|
||||
|
||||
info!("error while accepting streams: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// On the stream given by poll_token, get TLS (if requested) and tungstenite
|
||||
/// up and running, then read messages and pass on through channel.
|
||||
pub fn run_handshake_and_read_requests<'a>(
|
||||
pub fn run_handshake_and_read_requests(
|
||||
socket_worker_index: usize,
|
||||
request_channel_sender: &RequestChannelSender,
|
||||
opt_tls_acceptor: &'a Option<TlsAcceptor>, // If set, run TLS
|
||||
connections: &'a mut ConnectionMap<'a>,
|
||||
opt_tls_acceptor: &Option<TlsAcceptor>, // If set, run TLS
|
||||
connections: &mut ConnectionMap,
|
||||
poll_token: Token,
|
||||
valid_until: ValidUntil,
|
||||
){
|
||||
|
|
|
|||
|
|
@ -6,10 +6,10 @@ use socket2::{Socket, Domain, Type, Protocol};
|
|||
|
||||
use crate::config::Config;
|
||||
|
||||
use super::connection::*;
|
||||
use super::*;
|
||||
|
||||
|
||||
// will be identical to ws version
|
||||
// will be almost identical to ws version
|
||||
pub fn create_listener(
|
||||
config: &Config
|
||||
) -> ::anyhow::Result<::std::net::TcpListener> {
|
||||
|
|
@ -38,7 +38,6 @@ pub fn create_listener(
|
|||
}
|
||||
|
||||
|
||||
// will be identical to ws version
|
||||
/// Don't bother with deregistering from Poll. In my understanding, this is
|
||||
/// done automatically when the stream is dropped, as long as there are no
|
||||
/// other references to the file descriptor, such as when it is accessed
|
||||
|
|
@ -53,7 +52,6 @@ pub fn remove_connection_if_exists(
|
|||
}
|
||||
|
||||
|
||||
// will be identical to ws version
|
||||
// Close and remove inactive connections
|
||||
pub fn remove_inactive_connections(
|
||||
connections: &mut ConnectionMap,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use std::net::IpAddr;
|
||||
use hashbrown::HashMap;
|
||||
use serde::{Serialize, Deserialize, Serializer};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
use crate::common::Peer;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue