more refactor

This commit is contained in:
John Smith
2022-01-03 16:29:04 -05:00
parent 94772094c5
commit 55a44e0c8f
18 changed files with 261 additions and 239 deletions

View File

@@ -1,27 +1,10 @@
pub mod wrtc;
pub mod ws;
use crate::connection_manager::*;
use crate::veilid_api::ProtocolType;
use crate::xx::*;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DummyNetworkConnection {}
impl DummyNetworkConnection {
pub fn connection_descriptor(&self) -> ConnectionDescriptor {
ConnectionDescriptor::new_no_local(PeerAddress::new(
SocketAddress::default(),
ProtocolType::UDP,
))
}
pub async fn send(&self, _message: Vec<u8>) -> Result<(), String> {
Ok(())
}
pub async fn recv(&self) -> Result<Vec<u8>, String> {
Ok(Vec::new())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum NetworkConnection {
Dummy(DummyNetworkConnection),
@@ -36,7 +19,7 @@ impl NetworkConnection {
) -> Result<NetworkConnection, String> {
match dial_info.protocol_type() {
ProtocolType::UDP => {
panic!("Should not connect to UDP dialinfo");
panic!("UDP dial info is not support on WASM targets");
}
ProtocolType::TCP => {
panic!("TCP dial info is not support on WASM targets");
@@ -46,13 +29,32 @@ impl NetworkConnection {
}
}
}
pub async fn send_unbound_message(
&self,
dial_info: &DialInfo,
data: Vec<u8>,
) -> Result<(), String> {
match dial_info.protocol_type() {
ProtocolType::UDP => {
panic!("UDP dial info is not support on WASM targets");
}
ProtocolType::TCP => {
panic!("TCP dial info is not support on WASM targets");
}
ProtocolType::WS | ProtocolType::WSS => {
ws::WebsocketProtocolHandler::send_unbound_message(dial_info, data).await
}
}
}
pub async fn send(&self, message: Vec<u8>) -> Result<(), String> {
match self {
Self::Dummy(d) => d.send(message).await,
Self::WS(w) => w.send(message).await,
}
}
pub async fn recv(&self) -> Result<Vec<u8>, String> {
match self {
Self::Dummy(d) => d.recv().await,

View File

@@ -125,4 +125,21 @@ impl WebsocketProtocolHandler {
Ok(NetworkConnection::WS(WebsocketNetworkConnection::new(tls, connection_descriptor, wsio)))
}
pub async fn send_unbound_message(dial_info: &DialInfo, data: Vec<u8>) -> Result<(), String> {
if data.len() > MAX_MESSAGE_SIZE {
return Err("sending too large unbound WS message".to_owned());
}
trace!(
"sending unbound websocket message of length {} to {}",
data.len(),
dial_info,
);
let conn = Self::connect(None, dial_info.clone())
.await
.map_err(|e| format!("failed to connect websocket for unbound message: {}", e))?;
conn.send(data).await
}
}