diff options
author | Phil Dawes <phil@phildawes.net> | 2015-05-25 20:45:50 +0100 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2015-05-27 11:47:11 -0700 |
commit | e71077d824f8d5e7003c00c0c1e939ba5955be8f (patch) | |
tree | 8d11f3070216e4bda9fcb7736c6cf187e958e4e8 /src/sys/socket/mod.rs | |
parent | 718591938b4427abca456cb00c104375637904dd (diff) | |
download | nix-e71077d824f8d5e7003c00c0c1e939ba5955be8f.zip |
Add sys::socket send and recv
Diffstat (limited to 'src/sys/socket/mod.rs')
-rw-r--r-- | src/sys/socket/mod.rs | 35 |
1 files changed, 35 insertions, 0 deletions
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<usize> { + 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<usize> { + 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 { |