diff --git a/veilid-core/src/network_manager/native/network_tcp.rs b/veilid-core/src/network_manager/native/network_tcp.rs index 241b3ad6..28763b2d 100644 --- a/veilid-core/src/network_manager/native/network_tcp.rs +++ b/veilid-core/src/network_manager/native/network_tcp.rs @@ -132,33 +132,33 @@ impl Network { } }; - #[cfg(all(feature = "rt-async-std", unix))] - { - // async-std does not directly support linger on TcpStream yet - use std::os::fd::{AsRawFd, FromRawFd}; - if let Err(e) = unsafe { socket2::Socket::from_raw_fd(tcp_stream.as_raw_fd()) } - .set_linger(Some(core::time::Duration::from_secs(0))) - { - log_net!(debug "Couldn't set TCP linger: {}", e); - return; - } - } - #[cfg(all(feature = "rt-async-std", windows))] - { - // async-std does not directly support linger on TcpStream yet - use std::os::windows::io::{AsRawSocket, FromRawSocket}; - if let Err(e) = unsafe { socket2::Socket::from_raw_socket(tcp_stream.as_raw_socket()) } - .set_linger(Some(core::time::Duration::from_secs(0))) - { - log_net!(debug "Couldn't set TCP linger: {}", e); - return; - } - } - #[cfg(not(feature = "rt-async-std"))] - if let Err(e) = tcp_stream.set_linger(Some(core::time::Duration::from_secs(0))) { - log_net!(debug "Couldn't set TCP linger: {}", e); - return; - } + // #[cfg(all(feature = "rt-async-std", unix))] + // { + // // async-std does not directly support linger on TcpStream yet + // use std::os::fd::{AsRawFd, FromRawFd}; + // if let Err(e) = unsafe { socket2::Socket::from_raw_fd(tcp_stream.as_raw_fd()) } + // .set_linger(Some(core::time::Duration::from_secs(0))) + // { + // log_net!(debug "Couldn't set TCP linger: {}", e); + // return; + // } + // } + // #[cfg(all(feature = "rt-async-std", windows))] + // { + // // async-std does not directly support linger on TcpStream yet + // use std::os::windows::io::{AsRawSocket, FromRawSocket}; + // if let Err(e) = unsafe { socket2::Socket::from_raw_socket(tcp_stream.as_raw_socket()) } + // .set_linger(Some(core::time::Duration::from_secs(0))) + // { + // log_net!(debug "Couldn't set TCP linger: {}", e); + // return; + // } + // } + // #[cfg(not(feature = "rt-async-std"))] + // if let Err(e) = tcp_stream.set_linger(Some(core::time::Duration::from_secs(0))) { + // log_net!(debug "Couldn't set TCP linger: {}", e); + // return; + // } if let Err(e) = tcp_stream.set_nodelay(true) { log_net!(debug "Couldn't set TCP nodelay: {}", e); return; diff --git a/veilid-core/src/network_manager/native/protocol/mod.rs b/veilid-core/src/network_manager/native/protocol/mod.rs index e2fe457c..aad619d8 100644 --- a/veilid-core/src/network_manager/native/protocol/mod.rs +++ b/veilid-core/src/network_manager/native/protocol/mod.rs @@ -55,15 +55,15 @@ impl ProtocolNetworkConnection { } } - // pub async fn close(&self) -> io::Result> { - // match self { - // Self::Dummy(d) => d.close(), - // Self::RawTcp(t) => t.close().await, - // Self::WsAccepted(w) => w.close().await, - // Self::Ws(w) => w.close().await, - // Self::Wss(w) => w.close().await, - // } - // } + pub async fn close(&self) -> io::Result> { + match self { + Self::Dummy(d) => d.close(), + Self::RawTcp(t) => t.close().await, + Self::WsAccepted(w) => w.close().await, + Self::Ws(w) => w.close().await, + Self::Wss(w) => w.close().await, + } + } pub async fn send(&self, message: Vec) -> io::Result> { match self { diff --git a/veilid-core/src/network_manager/native/protocol/sockets.rs b/veilid-core/src/network_manager/native/protocol/sockets.rs index 1b125d6d..b015d6a9 100644 --- a/veilid-core/src/network_manager/native/protocol/sockets.rs +++ b/veilid-core/src/network_manager/native/protocol/sockets.rs @@ -112,9 +112,9 @@ pub fn new_unbound_tcp_socket(domain: Domain) -> io::Result { #[instrument(level = "trace", ret)] pub fn new_unbound_shared_tcp_socket(domain: Domain) -> io::Result { let socket = Socket::new(domain, Type::STREAM, Some(Protocol::TCP))?; - if let Err(e) = socket.set_linger(Some(core::time::Duration::from_secs(0))) { - log_net!(error "Couldn't set TCP linger: {}", e); - } + // if let Err(e) = socket.set_linger(Some(core::time::Duration::from_secs(0))) { + // log_net!(error "Couldn't set TCP linger: {}", e); + // } if let Err(e) = socket.set_nodelay(true) { log_net!(error "Couldn't set TCP nodelay: {}", e); } @@ -148,9 +148,9 @@ pub fn new_bound_first_tcp_socket(local_address: SocketAddr) -> io::Result io::Result> { - // // Make an attempt to flush the stream - // self.stream.clone().close().await?; - // // Then shut down the write side of the socket to effect a clean close - // cfg_if! { - // if #[cfg(feature="rt-async-std")] { - // self.tcp_stream - // .shutdown(async_std::net::Shutdown::Write) - // } else if #[cfg(feature="rt-tokio")] { - // use tokio::io::AsyncWriteExt; - // self.tcp_stream.get_mut() - // .shutdown() - // .await - // } else { - // compile_error!("needs executor implementation") - // } - // } - // } + #[cfg_attr( + feature = "verbose-tracing", + instrument(level = "trace", err, skip(self)) + )] + pub async fn close(&self) -> io::Result> { + // Make an attempt to flush the stream + self.stream.clone().close().await?; + Ok(NetworkResult::value(())) + + // // Then shut down the write side of the socket to effect a clean close + // cfg_if! { + // if #[cfg(feature="rt-async-std")] { + // self.tcp_stream + // .shutdown(async_std::net::Shutdown::Write) + // } else if #[cfg(feature="rt-tokio")] { + // use tokio::io::AsyncWriteExt; + // self.tcp_stream.get_mut() + // .shutdown() + // .await + // } else { + // compile_error!("needs executor implementation") + // } + // } + } async fn send_internal( stream: &mut AsyncPeekStream, diff --git a/veilid-core/src/network_manager/native/protocol/ws.rs b/veilid-core/src/network_manager/native/protocol/ws.rs index a98519d6..7e234860 100644 --- a/veilid-core/src/network_manager/native/protocol/ws.rs +++ b/veilid-core/src/network_manager/native/protocol/ws.rs @@ -77,15 +77,24 @@ where self.descriptor } - // #[instrument(level = "trace", err, skip(self))] - // pub async fn close(&self) -> io::Result<()> { - // // Make an attempt to flush the stream - // self.stream.clone().close().await.map_err(to_io_error_other)?; - // // Then forcibly close the socket - // self.tcp_stream - // .shutdown(Shutdown::Both) - // .map_err(to_io_error_other) - // } + #[cfg_attr( + feature = "verbose-tracing", + instrument(level = "trace", err, skip(self)) + )] + pub async fn close(&self) -> io::Result> { + // Make an attempt to flush the stream + self.stream + .clone() + .close() + .await + .map_err(to_io_error_other)?; + // // Then forcibly close the socket + // self.tcp_stream + // .shutdown(Shutdown::Both) + // .map_err(to_io_error_other) + + Ok(NetworkResult::value(())) + } #[cfg_attr(feature="verbose-tracing", instrument(level = "trace", err, skip(self, message), fields(network_result, message.len = message.len())))] pub async fn send(&self, message: Vec) -> io::Result> { diff --git a/veilid-core/src/network_manager/network_connection.rs b/veilid-core/src/network_manager/network_connection.rs index 247866f0..b9f94c1a 100644 --- a/veilid-core/src/network_manager/network_connection.rs +++ b/veilid-core/src/network_manager/network_connection.rs @@ -54,9 +54,9 @@ impl DummyNetworkConnection { pub fn descriptor(&self) -> ConnectionDescriptor { self.descriptor } - // pub fn close(&self) -> io::Result<()> { - // Ok(()) - // } + pub fn close(&self) -> io::Result> { + Ok(NetworkResult::Value(())) + } pub fn send(&self, _message: Vec) -> io::Result> { Ok(NetworkResult::Value(())) } diff --git a/veilid-core/src/network_manager/wasm/protocol/mod.rs b/veilid-core/src/network_manager/wasm/protocol/mod.rs index c3f3392b..168486a6 100644 --- a/veilid-core/src/network_manager/wasm/protocol/mod.rs +++ b/veilid-core/src/network_manager/wasm/protocol/mod.rs @@ -41,13 +41,12 @@ impl ProtocolNetworkConnection { Self::Ws(w) => w.descriptor(), } } - - // pub async fn close(&self) -> io::Result> { - // match self { - // Self::Dummy(d) => d.close(), - // Self::Ws(w) => w.close().await, - // } - // } + pub async fn close(&self) -> io::Result> { + match self { + Self::Dummy(d) => d.close(), + Self::Ws(w) => w.close().await, + } + } pub async fn send(&self, message: Vec) -> io::Result> { match self { Self::Dummy(d) => d.send(message), diff --git a/veilid-core/src/network_manager/wasm/protocol/ws.rs b/veilid-core/src/network_manager/wasm/protocol/ws.rs index 670c70c9..09cefda6 100644 --- a/veilid-core/src/network_manager/wasm/protocol/ws.rs +++ b/veilid-core/src/network_manager/wasm/protocol/ws.rs @@ -5,7 +5,7 @@ use std::io; use ws_stream_wasm::*; struct WebsocketNetworkConnectionInner { - _ws_meta: WsMeta, + ws_meta: WsMeta, ws_stream: CloneStream, } @@ -49,7 +49,7 @@ impl WebsocketNetworkConnection { Self { descriptor, inner: Arc::new(WebsocketNetworkConnectionInner { - _ws_meta: ws_meta, + ws_meta, ws_stream: CloneStream::new(ws_stream), }), } @@ -59,10 +59,14 @@ impl WebsocketNetworkConnection { self.descriptor } - // #[instrument(level = "trace", err, skip(self))] - // pub async fn close(&self) -> io::Result<()> { - // self.inner.ws_meta.close().await.map_err(to_io).map(drop) - // } + #[cfg_attr( + feature = "verbose-tracing", + instrument(level = "trace", err, skip(self)) + )] + pub async fn close(&self) -> io::Result> { + let _ = self.inner.ws_meta.close().await.map_err(to_io)?; + Ok(NetworkResult::value(())) + } #[cfg_attr(feature="verbose-tracing", instrument(level = "trace", err, skip(self, message), fields(network_result, message.len = message.len())))] pub async fn send(&self, message: Vec) -> io::Result> {