more log refactor
This commit is contained in:
@@ -45,8 +45,12 @@ impl Network {
|
||||
data: Vec<u8>,
|
||||
) -> Result<Option<Vec<u8>>, String> {
|
||||
match descriptor.protocol_type() {
|
||||
ProtocolType::UDP => return Err("no support for udp protocol".to_owned()),
|
||||
ProtocolType::TCP => return Err("no support for tcp protocol".to_owned()),
|
||||
ProtocolType::UDP => {
|
||||
return Err("no support for udp protocol".to_owned()).map_err(logthru_net!(error))
|
||||
}
|
||||
ProtocolType::TCP => {
|
||||
return Err("no support for tcp protocol".to_owned()).map_err(logthru_net!(error))
|
||||
}
|
||||
ProtocolType::WS | ProtocolType::WSS => {
|
||||
// find an existing connection in the connection table if one exists
|
||||
let network_manager = self.inner.lock().network_manager.clone();
|
||||
@@ -55,11 +59,7 @@ impl Network {
|
||||
.get_connection(&descriptor)
|
||||
{
|
||||
// connection exists, send over it
|
||||
entry
|
||||
.conn
|
||||
.send(data)
|
||||
.await
|
||||
.map_err(|_| "failed to send ws message".to_owned())?;
|
||||
entry.conn.send(data).await.map_err(logthru_net!())?;
|
||||
// Data was consumed
|
||||
return Ok(None);
|
||||
}
|
||||
@@ -78,10 +78,16 @@ impl Network {
|
||||
let network_manager = self.inner.lock().network_manager.clone();
|
||||
|
||||
match &dial_info {
|
||||
DialInfo::UDP(_) => return Err("no support for UDP protocol".to_owned()),
|
||||
DialInfo::TCP(_) => return Err("no support for TCP protocol".to_owned()),
|
||||
DialInfo::WS(_) => Err("WS protocol does not support unbound messages".to_owned()),
|
||||
DialInfo::WSS(_) => Err("WSS protocol does not support unbound messages".to_owned()),
|
||||
DialInfo::UDP(_) => {
|
||||
return Err("no support for UDP protocol".to_owned()).map_err(logthru_net!(error))
|
||||
}
|
||||
DialInfo::TCP(_) => {
|
||||
return Err("no support for TCP protocol".to_owned()).map_err(logthru_net!(error))
|
||||
}
|
||||
DialInfo::WS(_) => Err("WS protocol does not support unbound messages".to_owned())
|
||||
.map_err(logthru_net!(error)),
|
||||
DialInfo::WSS(_) => Err("WSS protocol does not support unbound messages".to_owned())
|
||||
.map_err(logthru_net!(error)),
|
||||
}
|
||||
}
|
||||
pub async fn send_data_to_dial_info(
|
||||
@@ -92,19 +98,21 @@ impl Network {
|
||||
let network_manager = self.inner.lock().network_manager.clone();
|
||||
|
||||
let conn = match &dial_info {
|
||||
DialInfo::UDP(_) => return Err("no support for UDP protocol".to_owned()),
|
||||
DialInfo::TCP(_) => return Err("no support for TCP protocol".to_owned()),
|
||||
DialInfo::UDP(_) => {
|
||||
return Err("no support for UDP protocol".to_owned()).map_err(logthru_net!(error))
|
||||
}
|
||||
DialInfo::TCP(_) => {
|
||||
return Err("no support for TCP protocol".to_owned()).map_err(logthru_net!(error))
|
||||
}
|
||||
DialInfo::WS(_) => WebsocketProtocolHandler::connect(network_manager, dial_info)
|
||||
.await
|
||||
.map_err(|_| "failed to connect to WS dial info".to_owned())?,
|
||||
.map_err(logthru_net!(error))?,
|
||||
DialInfo::WSS(_) => WebsocketProtocolHandler::connect(network_manager, dial_info)
|
||||
.await
|
||||
.map_err(|_| "failed to connect to WSS dial info".to_owned())?,
|
||||
.map_err(logthru_net!(error))?,
|
||||
};
|
||||
|
||||
conn.send(data)
|
||||
.await
|
||||
.map_err(|_| "failed to send data to dial info".to_owned())
|
||||
conn.send(data).await.map_err(logthru_net!(error))
|
||||
}
|
||||
|
||||
pub async fn send_data(&self, node_ref: NodeRef, data: Vec<u8>) -> Result<(), String> {
|
||||
@@ -129,9 +137,13 @@ impl Network {
|
||||
|
||||
// If that fails, try to make a connection or reach out to the peer via its dial info
|
||||
if let Some(di) = dial_info {
|
||||
self.clone().send_data_to_dial_info(&di, di_data).await
|
||||
self.clone()
|
||||
.send_data_to_dial_info(&di, di_data)
|
||||
.await
|
||||
.map_err(logthru_net!(error))
|
||||
} else {
|
||||
Err("couldn't send data, no dial info or peer address".to_owned())
|
||||
.map_err(logthru_net!(error))
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -11,10 +11,10 @@ impl DummyNetworkConnection {
|
||||
pub fn protocol_type(&self) -> ProtocolType {
|
||||
ProtocolType::UDP
|
||||
}
|
||||
pub fn send(&self, _message: Vec<u8>) -> SystemPinBoxFuture<Result<(), ()>> {
|
||||
pub fn send(&self, _message: Vec<u8>) -> SystemPinBoxFuture<Result<(), String>> {
|
||||
Box::pin(async { Ok(()) })
|
||||
}
|
||||
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, ()>> {
|
||||
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, String>> {
|
||||
Box::pin(async { Ok(Vec::new()) })
|
||||
}
|
||||
}
|
||||
|
@@ -56,28 +56,31 @@ impl WebsocketNetworkConnection {
|
||||
let inner = self.inner.clone();
|
||||
Box::pin(async move {
|
||||
if message.len() > MAX_MESSAGE_SIZE {
|
||||
return Err("sending too large WS message".to_owned());
|
||||
return Err("sending too large WS message".to_owned()).map_err(logthru_net!(error));
|
||||
}
|
||||
inner.lock().ws.send_with_u8_array(&message).map_err(map_to_string)
|
||||
inner
|
||||
.lock()
|
||||
.ws
|
||||
.send_with_u8_array(&message)
|
||||
.map_err(|_| "failed to send to websocket".to_owned())
|
||||
.map_err(logthru_net!(error))
|
||||
})
|
||||
}
|
||||
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, String)>> {
|
||||
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, String>> {
|
||||
let inner = self.inner.clone();
|
||||
Box::pin(async move {
|
||||
let out = match inner.lock().ws_stream.next().await {
|
||||
Some(WsMessage::Binary(v)) => v,
|
||||
Some(_) => {
|
||||
return Err("Unexpected WS message type".to_owned());
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
return Err(|e| e.to_string());
|
||||
return Err("Unexpected WS message type".to_owned())
|
||||
.map_err(logthru_net!(error));
|
||||
}
|
||||
None => {
|
||||
return Err("WS stream closed".to_owned());
|
||||
return Err("WS stream closed".to_owned()).map_err(logthru_net!(error));
|
||||
}
|
||||
};
|
||||
if out.len() > MAX_MESSAGE_SIZE {
|
||||
Err("sending too large WS message".to_owned())
|
||||
Err("sending too large WS message".to_owned()).map_err(logthru_net!(error))
|
||||
} else {
|
||||
Ok(out)
|
||||
}
|
||||
@@ -99,15 +102,22 @@ impl WebsocketProtocolHandler {
|
||||
let (tls, host, port, protocol_type) = match dial_info {
|
||||
DialInfo::WS(ws) => (false, ws.host.clone(), ws.port, ProtocolType::WS),
|
||||
DialInfo::WSS(wss) => (true, wss.host.clone(), wss.port, ProtocolType::WSS),
|
||||
_ => return Err("wrong protocol for WebsocketProtocolHandler".to_owned()),
|
||||
_ => {
|
||||
return Err("wrong protocol for WebsocketProtocolHandler".to_owned())
|
||||
.map_err(logthru_net!(error))
|
||||
}
|
||||
};
|
||||
|
||||
let peer_addr = PeerAddress::new(Address::from_str(&host)?, port, protocol_type);
|
||||
let peer_addr = PeerAddress::new(
|
||||
Address::from_str(&host).map_err(logthru_net!(error))?,
|
||||
port,
|
||||
protocol_type,
|
||||
);
|
||||
|
||||
let (ws, wsio) = match WsMeta::connect(url, None).await {
|
||||
Ok(conn) => conn,
|
||||
Err(e) => return Err(format!("couldn't connect to WS url: {}", e)),
|
||||
};
|
||||
let (ws, wsio) = WsMeta::connect(url, None)
|
||||
.await
|
||||
.map_err(map_to_string)
|
||||
.map_err(logthru_net!(error))?;
|
||||
|
||||
let conn = NetworkConnection::WS(WebsocketNetworkConnection::new(tls, ws, wsio));
|
||||
network_manager
|
||||
|
@@ -29,11 +29,11 @@ pub fn get_timestamp() -> u64 {
|
||||
}
|
||||
|
||||
pub fn get_timestamp_string() -> String {
|
||||
let date = Date::now();
|
||||
let hours = Date::get_utc_hours(date);
|
||||
let minutes = Date::get_utc_minutes(date);
|
||||
let seconds = Date::get_utc_seconds(date);
|
||||
let milliseconds = Date::get_utc_milliseconds(date);
|
||||
let date = Date::new_0();
|
||||
let hours = Date::get_utc_hours(&date);
|
||||
let minutes = Date::get_utc_minutes(&date);
|
||||
let seconds = Date::get_utc_seconds(&date);
|
||||
let milliseconds = Date::get_utc_milliseconds(&date);
|
||||
format!(
|
||||
"{:02}:{:02}:{:02}.{}",
|
||||
hours, minutes, seconds, milliseconds
|
||||
|
@@ -1,5 +1,6 @@
|
||||
use crate::xx::*;
|
||||
use alloc::collections::VecDeque;
|
||||
use core::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Channel<T> {
|
||||
@@ -58,6 +59,19 @@ pub enum TrySendError<T> {
|
||||
Disconnected(T),
|
||||
}
|
||||
|
||||
impl<T> fmt::Display for TrySendError<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
TrySendError::Full(_) => {
|
||||
write!(f, "Full")
|
||||
}
|
||||
TrySendError::Disconnected(_) => {
|
||||
write!(f, "Disconnected")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Sender<T> {
|
||||
// NOTE: This needs a timeout or you could block a very long time
|
||||
// pub async fn send(&self, msg: T) -> Result<(), SendError<T>> {
|
||||
|
Reference in New Issue
Block a user