mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
aquatic_ws: simplify network code further
This commit is contained in:
parent
6a8047a5fe
commit
7e2f371007
2 changed files with 58 additions and 59 deletions
3
TODO.md
3
TODO.md
|
|
@ -2,9 +2,6 @@
|
||||||
|
|
||||||
## aquatic_ws
|
## aquatic_ws
|
||||||
* network
|
* network
|
||||||
* think about if at least established connections could be boxed behind some
|
|
||||||
dyn trait so that there are not two different version for what is
|
|
||||||
essentially the same thing
|
|
||||||
* actually run tls. probably add config fields for number of tls and non-tls
|
* actually run tls. probably add config fields for number of tls and non-tls
|
||||||
workers, then run that amount of each
|
workers, then run that amount of each
|
||||||
* test tls!
|
* test tls!
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ use std::io::ErrorKind;
|
||||||
use tungstenite::WebSocket;
|
use tungstenite::WebSocket;
|
||||||
use tungstenite::handshake::{HandshakeError, server::ServerHandshake};
|
use tungstenite::handshake::{HandshakeError, server::ServerHandshake};
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
use native_tls::TlsAcceptor;
|
use native_tls::{TlsAcceptor, TlsStream};
|
||||||
|
|
||||||
use mio::{Events, Poll, Interest, Token};
|
use mio::{Events, Poll, Interest, Token};
|
||||||
use mio::net::TcpListener;
|
use mio::net::{TcpListener, TcpStream};
|
||||||
|
|
||||||
use crate::common::*;
|
use crate::common::*;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
@ -137,6 +137,44 @@ fn accept_new_streams(
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn handle_tls_handshake_result(
|
||||||
|
connections: &mut ConnectionMap,
|
||||||
|
poll_token: Token,
|
||||||
|
valid_until: ValidUntil,
|
||||||
|
result: Result<TlsStream<TcpStream>, ::native_tls::HandshakeError<TcpStream>>,
|
||||||
|
) -> bool {
|
||||||
|
match result {
|
||||||
|
Ok(stream) => {
|
||||||
|
println!("handshake established");
|
||||||
|
|
||||||
|
let connection = Connection {
|
||||||
|
valid_until,
|
||||||
|
stage: ConnectionStage::TlsStream(stream)
|
||||||
|
};
|
||||||
|
|
||||||
|
connections.insert(poll_token, connection);
|
||||||
|
},
|
||||||
|
Err(native_tls::HandshakeError::WouldBlock(handshake)) => {
|
||||||
|
println!("interrupted");
|
||||||
|
|
||||||
|
let connection = Connection {
|
||||||
|
valid_until,
|
||||||
|
stage: ConnectionStage::TlsMidHandshake(handshake),
|
||||||
|
};
|
||||||
|
|
||||||
|
connections.insert(poll_token, connection);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
Err(native_tls::HandshakeError::Failure(err)) => {
|
||||||
|
dbg!(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn handle_ws_handshake_result(
|
pub fn handle_ws_handshake_result(
|
||||||
connections: &mut ConnectionMap,
|
connections: &mut ConnectionMap,
|
||||||
poll_token: Token,
|
poll_token: Token,
|
||||||
|
|
@ -193,8 +231,6 @@ pub fn run_handshakes_and_read_messages(
|
||||||
poll_token: Token,
|
poll_token: Token,
|
||||||
valid_until: ValidUntil,
|
valid_until: ValidUntil,
|
||||||
){
|
){
|
||||||
println!("poll_token: {}", poll_token.0);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let established = if let Some(c) = connections.get(&poll_token){
|
let established = if let Some(c) = connections.get(&poll_token){
|
||||||
c.stage.is_established()
|
c.stage.is_established()
|
||||||
|
|
@ -208,32 +244,15 @@ pub fn run_handshakes_and_read_messages(
|
||||||
match conn.stage {
|
match conn.stage {
|
||||||
ConnectionStage::TcpStream(stream) => {
|
ConnectionStage::TcpStream(stream) => {
|
||||||
if let Some(tls_acceptor) = opt_tls_acceptor {
|
if let Some(tls_acceptor) = opt_tls_acceptor {
|
||||||
match tls_acceptor.accept(stream){
|
let stop_loop = handle_tls_handshake_result(
|
||||||
Ok(stream) => {
|
connections,
|
||||||
println!("handshake established");
|
poll_token,
|
||||||
|
valid_until,
|
||||||
let connection = Connection {
|
tls_acceptor.accept(stream)
|
||||||
valid_until,
|
);
|
||||||
stage: ConnectionStage::TlsStream(stream)
|
|
||||||
};
|
if stop_loop {
|
||||||
|
break
|
||||||
connections.insert(poll_token, connection);
|
|
||||||
},
|
|
||||||
Err(native_tls::HandshakeError::WouldBlock(handshake)) => {
|
|
||||||
println!("interrupted");
|
|
||||||
|
|
||||||
let connection = Connection {
|
|
||||||
valid_until,
|
|
||||||
stage: ConnectionStage::TlsMidHandshake(handshake),
|
|
||||||
};
|
|
||||||
|
|
||||||
connections.insert(poll_token, connection);
|
|
||||||
|
|
||||||
break
|
|
||||||
},
|
|
||||||
Err(native_tls::HandshakeError::Failure(err)) => {
|
|
||||||
dbg!(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let handshake_result = ::tungstenite::server::accept_hdr(
|
let handshake_result = ::tungstenite::server::accept_hdr(
|
||||||
|
|
@ -271,32 +290,15 @@ pub fn run_handshakes_and_read_messages(
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ConnectionStage::TlsMidHandshake(handshake) => {
|
ConnectionStage::TlsMidHandshake(handshake) => {
|
||||||
match handshake.handshake() {
|
let stop_loop = handle_tls_handshake_result(
|
||||||
Ok(stream) => {
|
connections,
|
||||||
println!("handshake established");
|
poll_token,
|
||||||
|
valid_until,
|
||||||
let connection = Connection {
|
handshake.handshake()
|
||||||
valid_until,
|
);
|
||||||
stage: ConnectionStage::TlsStream(stream)
|
|
||||||
};
|
if stop_loop {
|
||||||
|
break
|
||||||
connections.insert(poll_token, connection);
|
|
||||||
},
|
|
||||||
Err(native_tls::HandshakeError::WouldBlock(handshake)) => {
|
|
||||||
println!("interrupted");
|
|
||||||
|
|
||||||
let connection = Connection {
|
|
||||||
valid_until,
|
|
||||||
stage: ConnectionStage::TlsMidHandshake(handshake),
|
|
||||||
};
|
|
||||||
|
|
||||||
connections.insert(poll_token, connection);
|
|
||||||
|
|
||||||
break
|
|
||||||
},
|
|
||||||
Err(native_tls::HandshakeError::Failure(err)) => {
|
|
||||||
dbg!(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ConnectionStage::WsMidHandshake(handshake) => {
|
ConnectionStage::WsMidHandshake(handshake) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue