Add Socket.peek to allow peeking at the receiving queue. (#1933)

* Add `Socket.peek` to allow peeking at the receiving queue.

This uses the `MSG_PEEK` flag to peek at the beginning of the
receiving queue without removing data from said queue.
This commit is contained in:
Jooris Hadeler
2025-02-07 21:00:54 +01:00
committed by GitHub
parent e6ec09f2c5
commit ea4c864d4b
4 changed files with 15 additions and 0 deletions

View File

@@ -156,6 +156,16 @@ fn void! Socket.close(&self) @inline @dynamic
self.sock.close()!;
}
fn usz! Socket.peek(&self, char[] bytes) @dynamic
{
$if env::WIN32:
isz n = libc::recv(self.sock, bytes.ptr, (int)bytes.len, os::MSG_PEEK);
$else
isz n = libc::recv(self.sock, bytes.ptr, bytes.len, os::MSG_PEEK);
$endif
if (n < 0) return os::socket_error()?;
return (usz)n;
}
enum SocketShutdownHow : (inline CInt native_value)
{