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

@@ -94,3 +94,4 @@ const CShort POLLATTRIB = 0x0400; // file attributes may have changed
const CShort POLLNLINK = 0x0800; // (un)link/rename may have happened
const CShort POLLWRITE = 0x1000; // file's contents may have changed
const CInt MSG_PEEK = 0x0002;

View File

@@ -88,3 +88,5 @@ const CUShort POLLREMOVE = 0x1000;
const CUShort POLLRDHUP = 0x2000;
const CUShort POLLFREE = 0x4000;
const CUShort POLL_BUSY_LOOP = 0x8000;
const CInt MSG_PEEK = 0x0002;

View File

@@ -101,3 +101,5 @@ const CUShort POLLRDNORM = win32::POLLRDNORM;
const CUShort POLLRDBAND = win32::POLLRDBAND;
const CUShort POLLWRNORM = win32::POLLWRNORM;
const CUShort POLLWRBAND = win32::POLLWRBAND;
const int MSG_PEEK = 0x0002;

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