MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / copy_tcp_to_ws

Function copy_tcp_to_ws

crates/openshell-cli/src/edge_tunnel.rs:186–214  ·  view source on GitHub ↗

Copy bytes from a local TCP reader into WebSocket binary frames.

(
    mut tcp_read: tokio::io::ReadHalf<TcpStream>,
    mut ws_sink: SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>,
)

Source from the content-addressed store, hash-verified

184
185/// Copy bytes from a local TCP reader into WebSocket binary frames.
186async fn copy_tcp_to_ws(
187 mut tcp_read: tokio::io::ReadHalf<TcpStream>,
188 mut ws_sink: SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>,
189) {
190 let mut buf = vec![0u8; 32 * 1024];
191 loop {
192 match tcp_read.read(&mut buf).await {
193 Ok(0) => {
194 // EOF — send a close frame.
195 let _ = ws_sink.close().await;
196 break;
197 }
198 Ok(n) => {
199 if ws_sink
200 .send(Message::Binary(buf[..n].to_vec().into()))
201 .await
202 .is_err()
203 {
204 break;
205 }
206 }
207 Err(e) => {
208 debug!(error = %e, "tcp read error");
209 let _ = ws_sink.close().await;
210 break;
211 }
212 }
213 }
214}
215
216/// Copy bytes from WebSocket binary frames into a local TCP writer.
217async fn copy_ws_to_tcp(

Callers 1

handle_connectionFunction · 0.85

Calls 3

BinaryClass · 0.85
readMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected