more refactor, not quite done.

This commit is contained in:
John Smith
2022-01-03 23:58:26 -05:00
parent 55a44e0c8f
commit e32984a5aa
19 changed files with 318 additions and 339 deletions

View File

@@ -301,7 +301,7 @@ impl Network {
}
// Handle connection-oriented protocols
if let Some(conn) = self.connection_manager().get_connection(descriptor) {
if let Some(conn) = self.connection_manager().get_connection(descriptor).await {
// connection exists, send over it
conn.send(data).await.map_err(logthru_net!())?;

View File

@@ -1,6 +1,6 @@
use super::*;
use crate::connection_manager::*;
use crate::intf::*;
use crate::network_connection::*;
use utils::clone_stream::*;
use async_tls::TlsAcceptor;

View File

@@ -3,13 +3,13 @@ pub mod udp;
pub mod wrtc;
pub mod ws;
use crate::connection_manager::*;
use crate::network_connection::*;
use crate::xx::*;
use crate::*;
use socket2::{Domain, Protocol, Socket, Type};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum NetworkConnection {
#[derive(Debug)]
pub enum ProtocolNetworkConnection {
Dummy(DummyNetworkConnection),
RawTcp(tcp::RawTcpNetworkConnection),
WsAccepted(ws::WebSocketNetworkConnectionAccepted),
@@ -18,7 +18,7 @@ pub enum NetworkConnection {
//WebRTC(wrtc::WebRTCNetworkConnection),
}
impl NetworkConnection {
impl ProtocolNetworkConnection {
pub async fn connect(
local_address: Option<SocketAddr>,
dial_info: DialInfo,
@@ -35,11 +35,8 @@ impl NetworkConnection {
}
}
}
pub async fn send_unbound_message(
&self,
dial_info: &DialInfo,
data: Vec<u8>,
) -> Result<(), String> {
pub async fn send_unbound_message(dial_info: &DialInfo, data: Vec<u8>) -> Result<(), String> {
match dial_info.protocol_type() {
ProtocolType::UDP => {
let peer_socket_addr = dial_info.to_socket_addr();
@@ -59,27 +56,18 @@ impl NetworkConnection {
}
}
pub fn connection_descriptor(&self) -> ConnectionDescriptor {
pub async fn send(&mut self, message: Vec<u8>) -> Result<(), String> {
match self {
Self::Dummy(d) => d.connection_descriptor(),
Self::RawTcp(t) => t.connection_descriptor(),
Self::WsAccepted(w) => w.connection_descriptor(),
Self::Ws(w) => w.connection_descriptor(),
Self::Wss(w) => w.connection_descriptor(),
}
}
pub async fn send(&self, message: Vec<u8>) -> Result<(), String> {
match self {
Self::Dummy(d) => d.send(message).await,
Self::Dummy(d) => d.send(message),
Self::RawTcp(t) => t.send(message).await,
Self::WsAccepted(w) => w.send(message).await,
Self::Ws(w) => w.send(message).await,
Self::Wss(w) => w.send(message).await,
}
}
pub async fn recv(&self) -> Result<Vec<u8>, String> {
pub async fn recv(&mut self) -> Result<Vec<u8>, String> {
match self {
Self::Dummy(d) => d.recv().await,
Self::Dummy(d) => d.recv(),
Self::RawTcp(t) => t.recv().await,
Self::WsAccepted(w) => w.recv().await,
Self::Ws(w) => w.recv().await,

View File

@@ -5,79 +5,46 @@ use crate::network_manager::MAX_MESSAGE_SIZE;
use crate::*;
use async_std::net::*;
use async_std::prelude::*;
use async_std::sync::Mutex as AsyncMutex;
use std::fmt;
struct RawTcpNetworkConnectionInner {
stream: AsyncPeekStream,
}
#[derive(Clone)]
pub struct RawTcpNetworkConnection {
inner: Arc<AsyncMutex<RawTcpNetworkConnectionInner>>,
connection_descriptor: ConnectionDescriptor,
stream: AsyncPeekStream,
}
impl fmt::Debug for RawTcpNetworkConnection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RawTCPNetworkConnection")
.field("connection_descriptor", &self.connection_descriptor)
.finish()
f.debug_struct("RawTCPNetworkConnection").finish()
}
}
impl PartialEq for RawTcpNetworkConnection {
fn eq(&self, other: &Self) -> bool {
Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner)
}
}
impl Eq for RawTcpNetworkConnection {}
impl RawTcpNetworkConnection {
fn new_inner(stream: AsyncPeekStream) -> RawTcpNetworkConnectionInner {
RawTcpNetworkConnectionInner { stream }
pub fn new(stream: AsyncPeekStream) -> Self {
Self { stream }
}
pub fn new(stream: AsyncPeekStream, connection_descriptor: ConnectionDescriptor) -> Self {
Self {
inner: Arc::new(AsyncMutex::new(Self::new_inner(stream))),
connection_descriptor,
}
}
pub fn connection_descriptor(&self) -> ConnectionDescriptor {
self.connection_descriptor.clone()
}
pub async fn send(&self, message: Vec<u8>) -> Result<(), String> {
pub async fn send(&mut self, message: Vec<u8>) -> Result<(), String> {
if message.len() > MAX_MESSAGE_SIZE {
return Err("sending too large TCP message".to_owned());
}
let len = message.len() as u16;
let header = [b'V', b'L', len as u8, (len >> 8) as u8];
let mut inner = self.inner.lock().await;
inner
.stream
self.stream
.write_all(&header)
.await
.map_err(map_to_string)
.map_err(logthru_net!())?;
inner
.stream
self.stream
.write_all(&message)
.await
.map_err(map_to_string)
.map_err(logthru_net!())
}
pub async fn recv(&self) -> Result<Vec<u8>, String> {
pub async fn recv(&mut self) -> Result<Vec<u8>, String> {
let mut header = [0u8; 4];
let mut inner = self.inner.lock().await;
inner
.stream
self.stream
.read_exact(&mut header)
.await
.map_err(|e| format!("TCP recv error: {}", e))?;
@@ -90,8 +57,7 @@ impl RawTcpNetworkConnection {
}
let mut out: Vec<u8> = vec![0u8; len];
inner
.stream
self.stream
.read_exact(&mut out)
.await
.map_err(map_to_string)?;
@@ -143,10 +109,10 @@ impl RawTcpProtocolHandler {
ProtocolType::TCP,
);
let local_address = self.inner.lock().local_address;
let conn = NetworkConnection::RawTcp(RawTcpNetworkConnection::new(
stream,
let conn = NetworkConnection::from_protocol(
ConnectionDescriptor::new(peer_addr, SocketAddress::from_socket_addr(local_address)),
));
ProtocolNetworkConnection::RawTcp(RawTcpNetworkConnection::new(stream)),
);
Ok(Some(conn))
}
@@ -182,13 +148,13 @@ impl RawTcpProtocolHandler {
let ps = AsyncPeekStream::new(ts);
// Wrap the stream in a network connection and return it
let conn = NetworkConnection::RawTcp(RawTcpNetworkConnection::new(
ps,
let conn = NetworkConnection::from_protocol(
ConnectionDescriptor {
local: Some(SocketAddress::from_socket_addr(actual_local_address)),
remote: dial_info.to_peer_address(),
},
));
ProtocolNetworkConnection::RawTcp(RawTcpNetworkConnection::new(ps)),
);
Ok(conn)
}

View File

@@ -3,32 +3,21 @@ use crate::network_manager::MAX_MESSAGE_SIZE;
use crate::*;
use async_std::net::*;
struct RawUdpProtocolHandlerInner {
#[derive(Clone)]
pub struct RawUdpProtocolHandler {
socket: Arc<UdpSocket>,
}
#[derive(Clone)]
pub struct RawUdpProtocolHandler {
inner: Arc<Mutex<RawUdpProtocolHandlerInner>>,
}
impl RawUdpProtocolHandler {
fn new_inner(socket: Arc<UdpSocket>) -> RawUdpProtocolHandlerInner {
RawUdpProtocolHandlerInner { socket }
}
pub fn new(socket: Arc<UdpSocket>) -> Self {
Self {
inner: Arc::new(Mutex::new(Self::new_inner(socket))),
}
Self { socket }
}
pub async fn recv_message(
&self,
data: &mut [u8],
) -> Result<(usize, ConnectionDescriptor), String> {
let socket = self.inner.lock().socket.clone();
let (size, remote_addr) = socket.recv_from(data).await.map_err(map_to_string)?;
let (size, remote_addr) = self.socket.recv_from(data).await.map_err(map_to_string)?;
if size > MAX_MESSAGE_SIZE {
return Err("received too large UDP message".to_owned());
@@ -45,7 +34,7 @@ impl RawUdpProtocolHandler {
SocketAddress::from_socket_addr(remote_addr),
ProtocolType::UDP,
);
let local_socket_addr = socket.local_addr().map_err(map_to_string)?;
let local_socket_addr = self.socket.local_addr().map_err(map_to_string)?;
let descriptor = ConnectionDescriptor::new(
peer_addr,
SocketAddress::from_socket_addr(local_socket_addr),
@@ -64,8 +53,8 @@ impl RawUdpProtocolHandler {
socket_addr
);
let socket = self.inner.lock().socket.clone();
let len = socket
let len = self
.socket
.send_to(&data, socket_addr)
.await
.map_err(map_to_string)

View File

@@ -5,7 +5,6 @@ use crate::network_manager::MAX_MESSAGE_SIZE;
use crate::*;
use async_std::io;
use async_std::net::*;
use async_std::sync::Mutex as AsyncMutex;
use async_tls::TlsConnector;
use async_tungstenite::tungstenite::protocol::Message;
use async_tungstenite::{accept_async, client_async, WebSocketStream};
@@ -32,7 +31,6 @@ where
T: io::Read + io::Write + Send + Unpin + 'static,
{
tls: bool,
connection_descriptor: ConnectionDescriptor,
inner: Arc<AsyncMutex<WebSocketNetworkConnectionInner<T>>>,
}
@@ -43,7 +41,6 @@ where
fn clone(&self) -> Self {
Self {
tls: self.tls,
connection_descriptor: self.connection_descriptor.clone(),
inner: self.inner.clone(),
}
}
@@ -58,41 +55,19 @@ where
}
}
impl<T> PartialEq for WebsocketNetworkConnection<T>
where
T: io::Read + io::Write + Send + Unpin + 'static,
{
fn eq(&self, other: &Self) -> bool {
self.tls == other.tls
&& self.connection_descriptor == other.connection_descriptor
&& Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner)
}
}
impl<T> Eq for WebsocketNetworkConnection<T> where T: io::Read + io::Write + Send + Unpin + 'static {}
impl<T> WebsocketNetworkConnection<T>
where
T: io::Read + io::Write + Send + Unpin + 'static,
{
pub fn new(
tls: bool,
connection_descriptor: ConnectionDescriptor,
ws_stream: WebSocketStream<T>,
) -> Self {
pub fn new(tls: bool, ws_stream: WebSocketStream<T>) -> Self {
Self {
tls,
connection_descriptor,
inner: Arc::new(AsyncMutex::new(WebSocketNetworkConnectionInner {
ws_stream,
})),
}
}
pub fn connection_descriptor(&self) -> ConnectionDescriptor {
self.connection_descriptor.clone()
}
pub async fn send(&self, message: Vec<u8>) -> Result<(), String> {
if message.len() > MAX_MESSAGE_SIZE {
return Err("received too large WS message".to_owned());
@@ -130,7 +105,7 @@ where
///////////////////////////////////////////////////////////
///
struct WebsocketProtocolHandlerInner {
struct WebsocketProtocolHandlerArc {
tls: bool,
local_address: SocketAddr,
request_path: Vec<u8>,
@@ -142,7 +117,7 @@ pub struct WebsocketProtocolHandler
where
Self: ProtocolAcceptHandler,
{
inner: Arc<WebsocketProtocolHandlerInner>,
arc: Arc<WebsocketProtocolHandlerArc>,
}
impl WebsocketProtocolHandler {
pub fn new(config: VeilidConfig, tls: bool, local_address: SocketAddr) -> Self {
@@ -158,14 +133,13 @@ impl WebsocketProtocolHandler {
c.network.connection_initial_timeout
};
let inner = WebsocketProtocolHandlerInner {
tls,
local_address,
request_path: path.as_bytes().to_vec(),
connection_initial_timeout,
};
Self {
inner: Arc::new(inner),
arc: Arc::new(WebsocketProtocolHandlerArc {
tls,
local_address,
request_path: path.as_bytes().to_vec(),
connection_initial_timeout,
}),
}
}
@@ -174,10 +148,10 @@ impl WebsocketProtocolHandler {
ps: AsyncPeekStream,
socket_addr: SocketAddr,
) -> Result<Option<NetworkConnection>, String> {
let request_path_len = self.inner.request_path.len() + 2;
let request_path_len = self.arc.request_path.len() + 2;
let mut peekbuf: Vec<u8> = vec![0u8; request_path_len];
match io::timeout(
Duration::from_micros(self.inner.connection_initial_timeout),
Duration::from_micros(self.arc.connection_initial_timeout),
ps.peek_exact(&mut peekbuf),
)
.await
@@ -191,7 +165,7 @@ impl WebsocketProtocolHandler {
}
}
// Check for websocket path
let matches_path = &peekbuf[0..request_path_len - 2] == self.inner.request_path.as_slice()
let matches_path = &peekbuf[0..request_path_len - 2] == self.arc.request_path.as_slice()
&& (peekbuf[request_path_len - 2] == b' '
|| (peekbuf[request_path_len - 2] == b'/'
&& peekbuf[request_path_len - 1] == b' '));
@@ -208,7 +182,7 @@ impl WebsocketProtocolHandler {
.map_err(logthru_net!("failed websockets handshake"))?;
// Wrap the websocket in a NetworkConnection and register it
let protocol_type = if self.inner.tls {
let protocol_type = if self.arc.tls {
ProtocolType::WSS
} else {
ProtocolType::WS
@@ -217,14 +191,16 @@ impl WebsocketProtocolHandler {
let peer_addr =
PeerAddress::new(SocketAddress::from_socket_addr(socket_addr), protocol_type);
let conn = NetworkConnection::WsAccepted(WebsocketNetworkConnection::new(
self.inner.tls,
let conn = NetworkConnection::from_protocol(
ConnectionDescriptor::new(
peer_addr,
SocketAddress::from_socket_addr(self.inner.local_address),
SocketAddress::from_socket_addr(self.arc.local_address),
),
ws_stream,
));
ProtocolNetworkConnection::WsAccepted(WebsocketNetworkConnection::new(
self.arc.tls,
ws_stream,
)),
);
Ok(Some(conn))
}
@@ -271,7 +247,7 @@ impl WebsocketProtocolHandler {
.map_err(logthru_net!())?;
// Make our connection descriptor
let connection_descriptor = ConnectionDescriptor {
let descriptor = ConnectionDescriptor {
local: Some(SocketAddress::from_socket_addr(actual_local_addr)),
remote: dial_info.to_peer_address(),
};
@@ -288,21 +264,19 @@ impl WebsocketProtocolHandler {
.map_err(map_to_string)
.map_err(logthru_net!(error))?;
Ok(NetworkConnection::Wss(WebsocketNetworkConnection::new(
tls,
connection_descriptor,
ws_stream,
)))
Ok(NetworkConnection::from_protocol(
descriptor,
ProtocolNetworkConnection::Wss(WebsocketNetworkConnection::new(tls, ws_stream)),
))
} else {
let (ws_stream, _response) = client_async(request, tcp_stream)
.await
.map_err(map_to_string)
.map_err(logthru_net!(error))?;
Ok(NetworkConnection::Ws(WebsocketNetworkConnection::new(
tls,
connection_descriptor,
ws_stream,
)))
Ok(NetworkConnection::from_protocol(
descriptor,
ProtocolNetworkConnection::Ws(WebsocketNetworkConnection::new(tls, ws_stream)),
))
}
}

View File

@@ -5,14 +5,14 @@ use crate::connection_manager::*;
use crate::veilid_api::ProtocolType;
use crate::xx::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum NetworkConnection {
#[derive(Debug)]
pub enum ProtocolNetworkConnection {
Dummy(DummyNetworkConnection),
WS(ws::WebsocketNetworkConnection),
//WebRTC(wrtc::WebRTCNetworkConnection),
}
impl NetworkConnection {
impl ProtocolNetworkConnection {
pub async fn connect(
local_address: Option<SocketAddr>,
dial_info: DialInfo,
@@ -31,7 +31,6 @@ impl NetworkConnection {
}
pub async fn send_unbound_message(
&self,
dial_info: &DialInfo,
data: Vec<u8>,
) -> Result<(), String> {
@@ -48,17 +47,17 @@ impl NetworkConnection {
}
}
pub async fn send(&self, message: Vec<u8>) -> Result<(), String> {
pub async fn send(&mut self, message: Vec<u8>) -> Result<(), String> {
match self {
Self::Dummy(d) => d.send(message).await,
Self::WS(w) => w.send(message).await,
Self::Dummy(d) => d.send(message),
Self::WS(w) => w.send(message),
}
}
pub async fn recv(&self) -> Result<Vec<u8>, String> {
pub async fn recv(&mut self) -> Result<Vec<u8>, String> {
match self {
Self::Dummy(d) => d.recv().await,
Self::WS(w) => w.recv().await,
Self::Dummy(d) => d.recv(),
Self::WS(w) => w.recv(),
}
}
}

View File

@@ -14,7 +14,6 @@ struct WebsocketNetworkConnectionInner {
#[derive(Clone)]
pub struct WebsocketNetworkConnection {
tls: bool,
connection_descriptor: ConnectionDescriptor,
inner: Arc<Mutex<WebsocketNetworkConnectionInner>>,
}
@@ -24,20 +23,11 @@ impl fmt::Debug for WebsocketNetworkConnection {
}
}
impl PartialEq for WebsocketNetworkConnection {
fn eq(&self, other: &Self) -> bool {
self.tls == other.tls && Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner)
}
}
impl Eq for WebsocketNetworkConnection {}
impl WebsocketNetworkConnection {
pub fn new(tls: bool, connection_descriptor: ConnectionDescriptor, ws_stream: WsStream) -> Self {
pub fn new(tls: bool, ws_stream: WsStream) -> Self {
let ws = ws_stream.wrapped().clone();
Self {
tls,
connection_descriptor,
inner: Arc::new(Mutex::new(WebsocketNetworkConnectionInner {
ws_stream,
ws,
@@ -45,11 +35,10 @@ impl WebsocketNetworkConnection {
}
}
pub fn connection_descriptor(&self) -> ConnectionDescriptor {
self.connection_descriptor.clone()
}
xxx convert this to async and use stream api not low level websocket
xxx implement close() everywhere and skip using eventual for loop shutdown
pub async fn send(&self, message: Vec<u8>) -> Result<(), String> {
pub fn send(&self, message: Vec<u8>) -> Result<(), String> {
if message.len() > MAX_MESSAGE_SIZE {
return Err("sending too large WS message".to_owned()).map_err(logthru_net!(error));
}
@@ -60,7 +49,7 @@ impl WebsocketNetworkConnection {
.map_err(|_| "failed to send to websocket".to_owned())
.map_err(logthru_net!(error))
}
pub async fn recv(&self) -> Result<Vec<u8>, String> {
pub fn recv(&self) -> Result<Vec<u8>, String> {
let out = match self.inner.lock().ws_stream.next().await {
Some(WsMessage::Binary(v)) => v,
Some(_) => {
@@ -123,7 +112,7 @@ impl WebsocketProtocolHandler {
remote: dial_info.to_peer_address(),
};
Ok(NetworkConnection::WS(WebsocketNetworkConnection::new(tls, connection_descriptor, wsio)))
Ok(NetworkConnection::from_protocol(descriptor,ProtocolNetworkConnection::WS(WebsocketNetworkConnection::new(tls, wsio))))
}
pub async fn send_unbound_message(dial_info: &DialInfo, data: Vec<u8>) -> Result<(), String> {