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 | |
parent | 718591938b4427abca456cb00c104375637904dd (diff) | |
download | nix-e71077d824f8d5e7003c00c0c1e939ba5955be8f.zip |
Add sys::socket send and recv
Diffstat (limited to 'src/sys/socket')
-rw-r--r-- | src/sys/socket/ffi.rs | 2 | ||||
-rw-r--r-- | src/sys/socket/mod.rs | 35 |
2 files changed, 36 insertions, 1 deletions
diff --git a/src/sys/socket/ffi.rs b/src/sys/socket/ffi.rs index c445c8c9..11fd1ff1 100644 --- a/src/sys/socket/ffi.rs +++ b/src/sys/socket/ffi.rs @@ -1,5 +1,5 @@ use libc::{c_int, c_void, socklen_t}; -pub use libc::{socket, listen, bind, accept, connect, setsockopt, sendto, recvfrom, getsockname, getpeername}; +pub use libc::{socket, listen, bind, accept, connect, setsockopt, sendto, recvfrom, getsockname, getpeername, recv, send}; extern { pub fn getsockopt( 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 { |