more log refactor

This commit is contained in:
John Smith
2021-12-17 19:18:25 -05:00
parent 46f8607998
commit 971fa94751
17 changed files with 283 additions and 237 deletions

View File

@@ -663,14 +663,15 @@ impl Network {
let peer_socket_addr = descriptor
.remote
.to_socket_addr()
.map_err(|_| "unable to get socket address".to_owned())?;
.map_err(map_to_string)
.map_err(logthru_net!(error))?;
if let Some(ph) =
self.find_best_udp_protocol_handler(&peer_socket_addr, &descriptor.local)
{
ph.clone()
.send_message(data, peer_socket_addr)
.await
.map_err(|_| "unable to send udp message".to_owned())?;
.map_err(logthru_net!())?;
// Data was consumed
return Ok(None);
}
@@ -683,11 +684,7 @@ impl Network {
.get_connection(descriptor)
{
// connection exists, send over it
entry
.conn
.send(data)
.await
.map_err(|_| "failed to send tcp or ws message".to_owned())?;
entry.conn.send(data).await.map_err(logthru_net!())?;
// Data was consumed
return Ok(None);
@@ -706,24 +703,22 @@ impl Network {
) -> Result<(), String> {
match &dial_info {
DialInfo::UDP(_) => {
let peer_socket_addr = dial_info
.to_socket_addr()
.map_err(|_| "failed to resolve dial info for UDP dial info".to_owned())?;
let peer_socket_addr = dial_info.to_socket_addr().map_err(logthru_net!())?;
RawUdpProtocolHandler::send_unbound_message(data, peer_socket_addr)
.await
.map_err(|_| "failed to send unbound message to UDP dial info".to_owned())
.map_err(logthru_net!())
}
DialInfo::TCP(_) => {
let peer_socket_addr = dial_info
.to_socket_addr()
.map_err(|_| "failed to resolve dial info for TCP dial info".to_owned())?;
let peer_socket_addr = dial_info.to_socket_addr().map_err(logthru_net!())?;
RawTcpProtocolHandler::send_unbound_message(data, peer_socket_addr)
.await
.map_err(|_| "failed to connect to TCP dial info".to_owned())
.map_err(logthru_net!())
}
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::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)),
}
}
@@ -736,38 +731,33 @@ impl Network {
let conn = match &dial_info {
DialInfo::UDP(_) => {
let peer_socket_addr = dial_info
.to_socket_addr()
.map_err(|_| "failed to resolve dial info for UDP dial info".to_owned())?;
let peer_socket_addr = dial_info.to_socket_addr().map_err(logthru_net!())?;
if let Some(ph) = self.find_best_udp_protocol_handler(&peer_socket_addr, &None) {
return ph
.send_message(data, peer_socket_addr)
.await
.map_err(|_| "failed to send message to UDP dial info".to_owned());
.map_err(logthru_net!());
} else {
return Err("no appropriate UDP protocol handler for dial_info".to_owned());
return Err("no appropriate UDP protocol handler for dial_info".to_owned())
.map_err(logthru_net!(error));
}
}
DialInfo::TCP(_) => {
let peer_socket_addr = dial_info
.to_socket_addr()
.map_err(|_| "failed to resolve dial info for TCP dial info".to_owned())?;
let peer_socket_addr = dial_info.to_socket_addr().map_err(logthru_net!())?;
let some_local_addr = self.find_best_tcp_local_address(&peer_socket_addr);
RawTcpProtocolHandler::connect(network_manager, some_local_addr, peer_socket_addr)
.await
.map_err(|_| "failed to connect to TCP dial info".to_owned())?
.map_err(logthru_net!())?
}
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> {

View File

@@ -210,7 +210,7 @@ impl RawTcpProtocolHandler {
socket
.connect(&remote_socket2_addr)
.map_err(map_to_string)
.map_err(logthru_net!("addr={}", remote_socket_addr))?;
.map_err(logthru_net!(error "addr={}", remote_socket_addr))?;
let std_stream: std::net::TcpStream = socket.into();
let ts = TcpStream::from(std_stream);

View File

@@ -63,10 +63,10 @@ impl RawUdpProtocolHandler {
pub async fn send_message(&self, data: Vec<u8>, socket_addr: SocketAddr) -> Result<(), String> {
if data.len() > MAX_MESSAGE_SIZE {
return Err("sending too large UDP message".to_owned());
return Err("sending too large UDP message".to_owned()).map_err(logthru_net!(error));
}
trace!(
log_net!(
"sending UDP message of length {} to {}",
data.len(),
socket_addr
@@ -76,9 +76,11 @@ impl RawUdpProtocolHandler {
let len = socket
.send_to(&data, socket_addr)
.await
.map_err(|e| format!("{}", e))?;
.map_err(map_to_string)
.map_err(logthru_net!(error "failed udp send: addr={}", socket_addr))?;
if len != data.len() {
Err("UDP partial send".to_owned())
Err("UDP partial send".to_owned()).map_err(logthru_net!(error))
} else {
Ok(())
}
@@ -89,10 +91,10 @@ impl RawUdpProtocolHandler {
socket_addr: SocketAddr,
) -> Result<(), String> {
if data.len() > MAX_MESSAGE_SIZE {
return Err("sending too large unbound UDP message".to_owned());
return Err("sending too large unbound UDP message".to_owned())
.map_err(logthru_net!(error));
}
xxx continue here
trace!(
log_net!(
"sending unbound message of length {} to {}",
data.len(),
socket_addr
@@ -107,13 +109,15 @@ impl RawUdpProtocolHandler {
};
let socket = UdpSocket::bind(local_socket_addr)
.await
.map_err(|e| format!("{}", e))?;
.map_err(map_to_string)
.map_err(logthru_net!(error "failed to bind unbound udp socket"))?;
let len = socket
.send_to(&data, socket_addr)
.await
.map_err(|e| format!("{}", e))?;
.map_err(map_to_string)
.map_err(logthru_net!(error "failed unbound udp send: addr={}", socket_addr))?;
if len != data.len() {
Err("UDP partial unbound send".to_owned())
Err("UDP partial unbound send".to_owned()).map_err(logthru_net!(error))
} else {
Ok(())
}

View File

@@ -101,6 +101,7 @@ where
.send(Message::binary(message))
.await
.map_err(map_to_string)
.map_err(logthru_net!(error "failed to send websocket message"))
})
}
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, String>> {
@@ -112,17 +113,18 @@ where
let out = match inner.ws_stream.next().await {
Some(Ok(Message::Binary(v))) => v,
Some(Ok(_)) => {
return Err("Unexpected WS message type".to_owned());
return Err("Unexpected WS message type".to_owned())
.map_err(logthru_net!(error));
}
Some(Err(e)) => {
return Err(e.to_string());
return Err(e.to_string()).map_err(logthru_net!(error));
}
None => {
return Err("WS stream closed".to_owned());
return Err("WS stream closed".to_owned()).map_err(logthru_net!());
}
};
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)
}
@@ -189,7 +191,10 @@ impl WebsocketProtocolHandler {
{
Ok(_) => (),
Err(e) => {
return Err(format!("failed to peek stream: {:?}", e));
if e.kind() == io::ErrorKind::TimedOut {
return Err(e).map_err(map_to_string).map_err(logthru_net!());
}
return Err(e).map_err(map_to_string).map_err(logthru_net!(error));
}
}
// Check for websocket path
@@ -261,13 +266,16 @@ impl WebsocketProtocolHandler {
let tcp_stream = TcpStream::connect(format!("{}:{}", &domain, &port))
.await
.map_err(|e| format!("failed to connect tcp stream: {}", e))?;
.map_err(map_to_string)
.map_err(logthru_net!(error))?;
let local_addr = tcp_stream
.local_addr()
.map_err(|e| format!("can't get local address for tcp stream: {}", e))?;
.map_err(map_to_string)
.map_err(logthru_net!(error))?;
let peer_socket_addr = tcp_stream
.peer_addr()
.map_err(|e| format!("can't get peer address for tcp stream: {}", e))?;
.map_err(map_to_string)
.map_err(logthru_net!(error))?;
let peer_addr = PeerAddress::new(
Address::from_socket_addr(peer_socket_addr),
peer_socket_addr.port(),
@@ -279,10 +287,12 @@ impl WebsocketProtocolHandler {
let tls_stream = connector
.connect(domain, tcp_stream)
.await
.map_err(|e| format!("can't connect tls: {}", e))?;
.map_err(map_to_string)
.map_err(logthru_net!(error))?;
let (ws_stream, _response) = client_async(request, tls_stream)
.await
.map_err(|e| format!("wss negotation failed: {}", e))?;
.map_err(map_to_string)
.map_err(logthru_net!(error))?;
let conn = NetworkConnection::Wss(WebsocketNetworkConnection::new(tls, ws_stream));
network_manager
.on_new_connection(
@@ -294,7 +304,8 @@ impl WebsocketProtocolHandler {
} else {
let (ws_stream, _response) = client_async(request, tcp_stream)
.await
.map_err(|e| format!("ws negotiate failed: {}", e))?;
.map_err(map_to_string)
.map_err(logthru_net!(error))?;
let conn = NetworkConnection::Ws(WebsocketNetworkConnection::new(tls, ws_stream));
network_manager
.on_new_connection(

View File

@@ -12,14 +12,14 @@ impl Network {
async fn request_public_address(&self, node_ref: NodeRef) -> Option<SocketAddr> {
let routing_table = self.routing_table();
let rpc = routing_table.rpc_processor();
let info_answer = match rpc.rpc_call_info(node_ref.clone()).await {
Err(e) => {
trace!("failed to get info answer from {:?}: {:?}", node_ref, e);
return None;
}
Ok(ia) => ia,
};
info_answer.sender_info.socket_address
rpc.rpc_call_info(node_ref.clone())
.await
.map_err(logthru_net!(
"failed to get info answer from {:?}",
node_ref
))
.map(|info_answer| info_answer.sender_info.socket_address)
.unwrap_or(None)
}
// find fast peers with a particular address type, and ask them to tell us what our external address is
@@ -31,7 +31,7 @@ impl Network {
let routing_table = self.routing_table();
let peers = routing_table.get_fast_nodes_of_type(protocol_address_type);
if peers.is_empty() {
trace!("no peers of type '{:?}'", protocol_address_type);
log_net!("no peers of type '{:?}'", protocol_address_type);
return None;
}
for peer in peers {
@@ -44,7 +44,7 @@ impl Network {
return Some((sa, peer));
}
}
trace!("no peers responded with an external address");
log_net!("no peers responded with an external address");
None
}
@@ -78,19 +78,13 @@ impl Network {
) -> bool {
let routing_table = self.routing_table();
let rpc = routing_table.rpc_processor();
match rpc
.rpc_call_validate_dial_info(node_ref.clone(), dial_info, redirect, alternate_port)
rpc.rpc_call_validate_dial_info(node_ref.clone(), dial_info, redirect, alternate_port)
.await
{
Err(e) => {
error!(
"failed to send validate_dial_info to {:?}: {:?}",
node_ref, e
);
false
}
Ok(val) => val,
}
.map_err(logthru_net!(
"failed to send validate_dial_info to {:?}",
node_ref
))
.unwrap_or(false)
}
async fn try_port_mapping<I: AsRef<[SocketAddr]>>(
@@ -103,7 +97,7 @@ impl Network {
}
pub async fn update_udpv4_dialinfo_task_routine(self, _l: u64, _t: u64) -> Result<(), String> {
trace!("looking for udpv4 public dial info");
log_net!("looking for udpv4 public dial info");
let routing_table = self.routing_table();
let mut retry_count = {
@@ -148,7 +142,7 @@ impl Network {
break;
} else {
// UDP firewall?
warn!("UDP static public dial info not reachable. UDP firewall may be blocking inbound to {:?} for {:?}",external1_dial_info, node_b);
log_net!("UDP static public dial info not reachable. UDP firewall may be blocking inbound to {:?} for {:?}",external1_dial_info, node_b);
}
} else {
// There is -some NAT-
@@ -260,7 +254,7 @@ impl Network {
}
pub async fn update_tcpv4_dialinfo_task_routine(self, _l: u64, _t: u64) -> Result<(), String> {
trace!("looking for tcpv4 public dial info");
log_net!("looking for tcpv4 public dial info");
// xxx
//Err("unimplemented".to_owned())
Ok(())

View File

@@ -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))
}
}

View File

@@ -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()) })
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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>> {