mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
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:
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user