From e71077d824f8d5e7003c00c0c1e939ba5955be8f Mon Sep 17 00:00:00 2001 From: Phil Dawes Date: Mon, 25 May 2015 20:45:50 +0100 Subject: Add sys::socket send and recv --- src/sys/socket/mod.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/sys/socket/mod.rs') diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 202687a6..affd7739 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -211,6 +211,26 @@ pub fn connect(fd: Fd, addr: &SockAddr) -> Result<()> { from_ffi(res) } +/// Receive data from a connection-oriented socket. Returns the number of +/// bytes read +/// +/// [Further reading](http://man7.org/linux/man-pages/man2/recv.2.html) +pub fn recv(sockfd: Fd, buf: &mut [u8], flags: SockMessageFlags) -> Result { + unsafe { + let ret = ffi::recv( + sockfd, + buf.as_ptr() as *mut c_void, + buf.len() as size_t, + flags); + + if ret < 0 { + Err(Error::last()) + } else { + Ok(ret as usize) + } + } +} + /// Receive data from a connectionless or connection-oriented socket. Returns /// the number of bytes read and the socket address of the sender. /// @@ -250,6 +270,21 @@ pub fn sendto(fd: Fd, buf: &[u8], addr: &SockAddr, flags: SockMessageFlags) -> R } } +/// Send data to a connection-oriented socket. Returns the number of bytes read +/// +/// [Further reading](http://man7.org/linux/man-pages/man2/send.2.html) +pub fn send(fd: Fd, buf: &[u8], flags: SockMessageFlags) -> Result { + let ret = unsafe { + ffi::send(fd, buf.as_ptr() as *const c_void, buf.len() as size_t, flags) + }; + + if ret < 0 { + Err(Error::last()) + } else { + Ok(ret as usize) + } +} + #[repr(C)] #[derive(Clone, Copy, Debug)] pub struct linger { -- cgit v1.2.3