Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e018afe

Browse files
committed
Log socket client connect/disconnect and messages
1 parent 8ed9b43 commit e018afe

1 file changed

Lines changed: 63 additions & 4 deletions

File tree

src/bin/shm2_relayd.rs

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ fn run_tcp_listener(
269269
continue;
270270
}
271271
};
272+
let peer = stream
273+
.peer_addr()
274+
.map(|a| a.to_string())
275+
.unwrap_or_else(|_| "<unknown>".to_string());
276+
println!("[shm2_relayd] client connected {peer}");
272277
let current = count.fetch_add(1, Ordering::SeqCst) + 1;
273278
notify_client_count(&main_ctx, &input_pipeline, splash_pipeline.as_ref(), current);
274279

@@ -277,22 +282,48 @@ fn run_tcp_listener(
277282
let splash_pipeline = splash_pipeline.clone();
278283
let count_clone = count.clone();
279284
thread::spawn(move || {
280-
let _ = hold_connection(stream);
285+
let _ = hold_connection(stream, &peer);
281286
let current = count_clone.fetch_sub(1, Ordering::SeqCst).saturating_sub(1);
282287
notify_client_count(&main_ctx, &input_pipeline, splash_pipeline.as_ref(), current);
288+
println!("[shm2_relayd] client disconnected {peer}");
283289
});
284290
}
285291

286292
Ok(())
287293
}
288294

289-
fn hold_connection(mut stream: std::net::TcpStream) -> io::Result<()> {
295+
fn hold_connection(mut stream: std::net::TcpStream, peer: &str) -> io::Result<()> {
290296
let mut buf = [0u8; 1024];
297+
let mut pending = Vec::with_capacity(1024);
291298
loop {
292299
let n = stream.read(&mut buf)?;
293300
if n == 0 {
294301
break;
295302
}
303+
pending.extend_from_slice(&buf[..n]);
304+
while let Some(pos) = pending.iter().position(|b| *b == b'\n') {
305+
let line = pending.drain(..=pos).collect::<Vec<u8>>();
306+
let msg = String::from_utf8_lossy(&line);
307+
let msg = msg.trim_end_matches(['\r', '\n'].as_ref());
308+
if !msg.is_empty() {
309+
println!("[shm2_relayd] {peer}: {msg}");
310+
}
311+
}
312+
if pending.len() >= 1024 {
313+
let chunk = pending.drain(..).collect::<Vec<u8>>();
314+
let msg = String::from_utf8_lossy(&chunk);
315+
let msg = msg.trim_end_matches(['\r', '\n'].as_ref());
316+
if !msg.is_empty() {
317+
println!("[shm2_relayd] {peer}: {msg}");
318+
}
319+
}
320+
}
321+
if !pending.is_empty() {
322+
let msg = String::from_utf8_lossy(&pending);
323+
let msg = msg.trim_end_matches(['\r', '\n'].as_ref());
324+
if !msg.is_empty() {
325+
println!("[shm2_relayd] {peer}: {msg}");
326+
}
296327
}
297328
Ok(())
298329
}
@@ -346,6 +377,7 @@ fn run_vsock_listener(
346377
eprintln!("vsock accept error: {}", io::Error::last_os_error());
347378
continue;
348379
}
380+
println!("[shm2_relayd] client connected vsock:{cid}:{port}");
349381
let current = count.fetch_add(1, Ordering::SeqCst) + 1;
350382
notify_client_count(&main_ctx, &input_pipeline, splash_pipeline.as_ref(), current);
351383

@@ -354,16 +386,18 @@ fn run_vsock_listener(
354386
let splash_pipeline = splash_pipeline.clone();
355387
let count_clone = count.clone();
356388
thread::spawn(move || {
357-
let _ = hold_vsock_connection(conn);
389+
let _ = hold_vsock_connection(conn, cid, port);
358390
let current = count_clone.fetch_sub(1, Ordering::SeqCst).saturating_sub(1);
359391
notify_client_count(&main_ctx, &input_pipeline, splash_pipeline.as_ref(), current);
392+
println!("[shm2_relayd] client disconnected vsock:{cid}:{port}");
360393
});
361394
}
362395
}
363396

364397
#[cfg(target_os = "linux")]
365-
fn hold_vsock_connection(fd: RawFd) -> io::Result<()> {
398+
fn hold_vsock_connection(fd: RawFd, cid: u32, port: u32) -> io::Result<()> {
366399
let mut buf = [0u8; 1024];
400+
let mut pending = Vec::with_capacity(1024);
367401
loop {
368402
let n = unsafe { libc::read(fd, buf.as_mut_ptr() as *mut _, buf.len()) };
369403
if n == 0 {
@@ -374,6 +408,31 @@ fn hold_vsock_connection(fd: RawFd) -> io::Result<()> {
374408
unsafe { libc::close(fd) };
375409
return Err(err);
376410
}
411+
let n = n as usize;
412+
pending.extend_from_slice(&buf[..n]);
413+
while let Some(pos) = pending.iter().position(|b| *b == b'\n') {
414+
let line = pending.drain(..=pos).collect::<Vec<u8>>();
415+
let msg = String::from_utf8_lossy(&line);
416+
let msg = msg.trim_end_matches(['\r', '\n'].as_ref());
417+
if !msg.is_empty() {
418+
println!("[shm2_relayd] vsock:{cid}:{port}: {msg}");
419+
}
420+
}
421+
if pending.len() >= 1024 {
422+
let chunk = pending.drain(..).collect::<Vec<u8>>();
423+
let msg = String::from_utf8_lossy(&chunk);
424+
let msg = msg.trim_end_matches(['\r', '\n'].as_ref());
425+
if !msg.is_empty() {
426+
println!("[shm2_relayd] vsock:{cid}:{port}: {msg}");
427+
}
428+
}
429+
}
430+
if !pending.is_empty() {
431+
let msg = String::from_utf8_lossy(&pending);
432+
let msg = msg.trim_end_matches(['\r', '\n'].as_ref());
433+
if !msg.is_empty() {
434+
println!("[shm2_relayd] vsock:{cid}:{port}: {msg}");
435+
}
377436
}
378437
unsafe { libc::close(fd) };
379438
Ok(())

0 commit comments

Comments
 (0)