Verified Commit 74e674b6 authored by jD91mZM2's avatar jD91mZM2

Redox support

parent e1e43ff8
......@@ -16,9 +16,10 @@ categories = ["asynchronous"]
travis-ci = { repository = "alexcrichton/mio-uds" }
[target."cfg(unix)".dependencies]
iovec = "0.1"
libc = "0.2.42"
mio = "0.6.5"
[target.'cfg(any(unix, target_os = "redox"))'.dependencies]
iovec = "0.1"
mio = { git = "https://gitlab.redox-os.org/redox-os/mio" }
[dev-dependencies]
tempdir = "0.3"
//! MIO bindings for Unix Domain Sockets
#![cfg(unix)]
#![cfg(any(unix, target_os = "redox"))]
#![deny(missing_docs)]
#![doc(html_root_url = "https://docs.rs/mio-uds/0.6")]
#![cfg_attr(target_os = "redox", feature(unix_socket_redox))]
extern crate iovec;
#[cfg(not(target_os = "redox"))]
extern crate libc;
extern crate mio;
#[cfg(not(target_os = "redox"))]
use std::io;
#[cfg(not(target_os = "redox"))]
mod datagram;
mod listener;
#[cfg(not(target_os = "redox"))]
mod socket;
mod stream;
pub use stream::UnixStream;
pub use listener::UnixListener;
#[cfg(not(target_os = "redox"))]
pub use datagram::UnixDatagram;
pub use listener::UnixListener;
pub use stream::UnixStream;
#[cfg(not(target_os = "redox"))]
fn cvt(i: libc::c_int) -> io::Result<libc::c_int> {
if i == -1 {
Err(io::Error::last_os_error())
......
......@@ -2,14 +2,16 @@ use std::io;
use std::os::unix::net;
use std::os::unix::prelude::*;
use std::path::Path;
use std::os::unix::io::RawFd;
use libc;
use mio::event::Evented;
use mio::unix::EventedFd;
use mio::{Poll, PollOpt, Ready, Token};
use UnixStream;
use cvt;
#[cfg(not(target_os = "redox"))]
use {cvt, libc};
#[cfg(not(target_os = "redox"))]
use socket::{sockaddr_un, Socket};
/// A structure representing a Unix domain socket server.
......@@ -28,6 +30,7 @@ impl UnixListener {
UnixListener::_bind(path.as_ref())
}
#[cfg(not(target_os = "redox"))]
fn _bind(path: &Path) -> io::Result<UnixListener> {
unsafe {
let (addr, len) = try!(sockaddr_un(path));
......@@ -40,6 +43,10 @@ impl UnixListener {
Ok(UnixListener::from_raw_fd(fd.into_fd()))
}
}
#[cfg(target_os = "redox")]
fn _bind(path: &Path) -> io::Result<UnixListener> {
Self::from_listener(net::UnixListener::bind(path)?)
}
/// Consumes a standard library `UnixListener` and returns a wrapped
/// `UnixListener` compatible with mio.
......@@ -123,19 +130,19 @@ impl Evented for UnixListener {
}
impl AsRawFd for UnixListener {
fn as_raw_fd(&self) -> i32 {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}
impl IntoRawFd for UnixListener {
fn into_raw_fd(self) -> i32 {
fn into_raw_fd(self) -> RawFd {
self.inner.into_raw_fd()
}
}
impl FromRawFd for UnixListener {
unsafe fn from_raw_fd(fd: i32) -> UnixListener {
unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
UnixListener {
inner: net::UnixListener::from_raw_fd(fd),
}
......
#[cfg(not(target_os = "redox"))]
use std::cmp;
use std::io::prelude::*;
use std::io;
......@@ -5,15 +6,18 @@ use std::os::unix::net;
use std::os::unix::prelude::*;
use std::path::Path;
use std::net::Shutdown;
use std::os::unix::io::RawFd;
use iovec::IoVec;
#[cfg(not(target_os = "redox"))]
use iovec::unix as iovec;
use libc;
use mio::event::Evented;
use mio::unix::EventedFd;
use mio::{Poll, Token, Ready, PollOpt};
use cvt;
#[cfg(not(target_os = "redox"))]
use {cvt, libc};
#[cfg(not(target_os = "redox"))]
use socket::{sockaddr_un, Socket};
/// A Unix stream socket.
......@@ -47,6 +51,7 @@ impl UnixStream {
UnixStream::_connect(p.as_ref())
}
#[cfg(not(target_os = "redox"))]
fn _connect(path: &Path) -> io::Result<UnixStream> {
unsafe {
let (addr, len) = try!(sockaddr_un(path));
......@@ -61,6 +66,10 @@ impl UnixStream {
Ok(UnixStream::from_raw_fd(socket.into_fd()))
}
}
#[cfg(target_os = "redox")]
fn _connect(path: &Path) -> io::Result<UnixStream> {
Self::from_stream(net::UnixStream::connect(path)?)
}
/// Consumes a standard library `UnixStream` and returns a wrapped
/// `UnixStream` compatible with mio.
......@@ -75,12 +84,21 @@ impl UnixStream {
/// Creates an unnamed pair of connected sockets.
///
/// Returns two `UnixStream`s which are connected to each other.
#[cfg(not(target_os = "redox"))]
pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
Socket::pair(libc::SOCK_STREAM).map(|(a, b)| unsafe {
(UnixStream::from_raw_fd(a.into_fd()),
UnixStream::from_raw_fd(b.into_fd()))
})
}
/// Creates an unnamed pair of connected sockets.
///
/// Returns two `UnixStream`s which are connected to each other.
#[cfg(target_os = "redox")]
pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
let (one, two) = net::UnixStream::pair()?;
Ok((Self::from_stream(one)?, Self::from_stream(two)?))
}
/// Creates a new independently owned handle to the underlying socket.
///
......@@ -130,6 +148,7 @@ impl UnixStream {
/// The number of bytes read is returned, if successful, or an error is
/// returned otherwise. If no bytes are available to be read yet then
/// a "would block" error is returned. This operation does not block.
#[cfg(not(target_os = "redox"))]
pub fn read_bufs(&self, bufs: &mut [&mut IoVec]) -> io::Result<usize> {
unsafe {
let slice = iovec::as_os_slice_mut(bufs);
......@@ -144,6 +163,30 @@ impl UnixStream {
}
}
}
/// Read in a list of buffers all at once.
///
/// This operation will attempt to read bytes from this socket and place
/// them into the list of buffers provided. Note that each buffer is an
/// `IoVec` which can be created from a byte slice.
///
/// The buffers provided will be filled in sequentially. A buffer will be
/// entirely filled up before the next is written to.
///
/// The number of bytes read is returned, if successful, or an error is
/// returned otherwise. If no bytes are available to be read yet then
/// a "would block" error is returned. This operation does not block.
#[cfg(target_os = "redox")]
pub fn read_bufs(&self, bufs: &mut [&mut IoVec]) -> io::Result<usize> {
let mut total = 0;
for buf in bufs {
let n = (&self.inner).read(buf)?;
total += n;
if n < buf.len() {
return Ok(total);
}
}
Ok(total)
}
/// Write a list of buffers all at once.
///
......@@ -157,6 +200,7 @@ impl UnixStream {
/// The number of bytes written is returned, if successful, or an error is
/// returned otherwise. If the socket is not currently writable then a
/// "would block" error is returned. This operation does not block.
#[cfg(not(target_os = "redox"))]
pub fn write_bufs(&self, bufs: &[&IoVec]) -> io::Result<usize> {
unsafe {
let slice = iovec::as_os_slice(bufs);
......@@ -171,6 +215,30 @@ impl UnixStream {
}
}
}
/// Write a list of buffers all at once.
///
/// This operation will attempt to write a list of byte buffers to this
/// socket. Note that each buffer is an `IoVec` which can be created from a
/// byte slice.
///
/// The buffers provided will be written sequentially. A buffer will be
/// entirely written before the next is written.
///
/// The number of bytes written is returned, if successful, or an error is
/// returned otherwise. If the socket is not currently writable then a
/// "would block" error is returned. This operation does not block.
#[cfg(target_os = "redox")]
pub fn write_bufs(&self, bufs: &[&IoVec]) -> io::Result<usize> {
let mut total = 0;
for buf in bufs {
let n = (&self.inner).write(buf)?;
total += n;
if n < buf.len() {
return Ok(total);
}
}
Ok(total)
}
}
impl Evented for UnixStream {
......@@ -228,19 +296,19 @@ impl<'a> Write for &'a UnixStream {
}
impl AsRawFd for UnixStream {
fn as_raw_fd(&self) -> i32 {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}
impl IntoRawFd for UnixStream {
fn into_raw_fd(self) -> i32 {
fn into_raw_fd(self) -> RawFd {
self.inner.into_raw_fd()
}
}
impl FromRawFd for UnixStream {
unsafe fn from_raw_fd(fd: i32) -> UnixStream {
unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
UnixStream { inner: net::UnixStream::from_raw_fd(fd) }
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment