Skip to content

Commit 2a4f9d6

Browse files
committed
Implement native TCP I/O
1 parent 1763f36 commit 2a4f9d6

File tree

10 files changed

+825
-93
lines changed

10 files changed

+825
-93
lines changed

src/libnative/io/file.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use super::IoResult;
2626
#[cfg(windows)] use std::ptr;
2727
#[cfg(windows)] use std::str;
2828

29-
fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 {
29+
pub fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 {
3030
#[cfg(windows)] static eintr: int = 0; // doesn't matter
3131
#[cfg(not(windows))] static eintr: int = libc::EINTR as int;
3232

@@ -92,7 +92,7 @@ impl FileDesc {
9292
Ok(ret as uint)
9393
}
9494
}
95-
fn inner_write(&mut self, buf: &[u8]) -> Result<(), IoError> {
95+
pub fn inner_write(&mut self, buf: &[u8]) -> Result<(), IoError> {
9696
#[cfg(windows)] type wlen = libc::c_uint;
9797
#[cfg(not(windows))] type wlen = libc::size_t;
9898
let ret = keep_going(buf, |buf, len| {
@@ -106,6 +106,8 @@ impl FileDesc {
106106
Ok(())
107107
}
108108
}
109+
110+
pub fn fd(&self) -> fd_t { self.fd }
109111
}
110112

111113
impl io::Reader for FileDesc {
@@ -902,7 +904,7 @@ pub fn utime(p: &CString, atime: u64, mtime: u64) -> IoResult<()> {
902904

903905
#[cfg(test)]
904906
mod tests {
905-
use super::{CFile, FileDesc, CloseFd};
907+
use super::{CFile, FileDesc};
906908
use std::io;
907909
use std::libc;
908910
use std::os;

src/libnative/io/mod.rs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub use self::process::Process;
4444
// Native I/O implementations
4545
pub mod file;
4646
pub mod process;
47+
pub mod net;
4748

4849
type IoResult<T> = Result<T, IoError>;
4950

@@ -60,7 +61,20 @@ fn translate_error(errno: i32, detail: bool) -> IoError {
6061
fn get_err(errno: i32) -> (io::IoErrorKind, &'static str) {
6162
match errno {
6263
libc::EOF => (io::EndOfFile, "end of file"),
63-
_ => (io::OtherIoError, "unknown error"),
64+
libc::WSAECONNREFUSED => (io::ConnectionRefused, "connection refused"),
65+
libc::WSAECONNRESET => (io::ConnectionReset, "connection reset"),
66+
libc::WSAEACCES => (io::PermissionDenied, "permission denied"),
67+
libc::WSAEWOULDBLOCK =>
68+
(io::ResourceUnavailable, "resource temporarily unavailable"),
69+
libc::WSAENOTCONN => (io::NotConnected, "not connected"),
70+
libc::WSAECONNABORTED => (io::ConnectionAborted, "connection aborted"),
71+
libc::WSAEADDRNOTAVAIL => (io::ConnectionRefused, "address not available"),
72+
libc::WSAEADDRINUSE => (io::ConnectionRefused, "address in use"),
73+
74+
x => {
75+
debug!("ignoring {}: {}", x, os::last_os_error());
76+
(io::OtherIoError, "unknown error")
77+
}
6478
}
6579
}
6680

@@ -69,13 +83,25 @@ fn translate_error(errno: i32, detail: bool) -> IoError {
6983
// XXX: this should probably be a bit more descriptive...
7084
match errno {
7185
libc::EOF => (io::EndOfFile, "end of file"),
86+
libc::ECONNREFUSED => (io::ConnectionRefused, "connection refused"),
87+
libc::ECONNRESET => (io::ConnectionReset, "connection reset"),
88+
libc::EPERM | libc::EACCES =>
89+
(io::PermissionDenied, "permission denied"),
90+
libc::EPIPE => (io::BrokenPipe, "broken pipe"),
91+
libc::ENOTCONN => (io::NotConnected, "not connected"),
92+
libc::ECONNABORTED => (io::ConnectionAborted, "connection aborted"),
93+
libc::EADDRNOTAVAIL => (io::ConnectionRefused, "address not available"),
94+
libc::EADDRINUSE => (io::ConnectionRefused, "address in use"),
7295

7396
// These two constants can have the same value on some systems, but
7497
// different values on others, so we can't use a match clause
7598
x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
7699
(io::ResourceUnavailable, "resource temporarily unavailable"),
77100

78-
_ => (io::OtherIoError, "unknown error"),
101+
x => {
102+
debug!("ignoring {}: {}", x, os::last_os_error());
103+
(io::OtherIoError, "unknown error")
104+
}
79105
}
80106
}
81107

@@ -121,15 +147,24 @@ fn retry(f: || -> libc::c_int) -> IoResult<libc::c_int> {
121147

122148
/// Implementation of rt::rtio's IoFactory trait to generate handles to the
123149
/// native I/O functionality.
124-
pub struct IoFactory;
150+
pub struct IoFactory {
151+
priv cannot_construct_outside_of_this_module: ()
152+
}
153+
154+
impl IoFactory {
155+
pub fn new() -> IoFactory {
156+
net::init();
157+
IoFactory { cannot_construct_outside_of_this_module: () }
158+
}
159+
}
125160

126161
impl rtio::IoFactory for IoFactory {
127162
// networking
128-
fn tcp_connect(&mut self, _addr: SocketAddr) -> IoResult<~RtioTcpStream> {
129-
Err(unimpl())
163+
fn tcp_connect(&mut self, addr: SocketAddr) -> IoResult<~RtioTcpStream> {
164+
net::TcpStream::connect(addr).map(|s| ~s as ~RtioTcpStream)
130165
}
131-
fn tcp_bind(&mut self, _addr: SocketAddr) -> IoResult<~RtioTcpListener> {
132-
Err(unimpl())
166+
fn tcp_bind(&mut self, addr: SocketAddr) -> IoResult<~RtioTcpListener> {
167+
net::TcpListener::bind(addr).map(|s| ~s as ~RtioTcpListener)
133168
}
134169
fn udp_bind(&mut self, _addr: SocketAddr) -> IoResult<~RtioUdpSocket> {
135170
Err(unimpl())
@@ -217,9 +252,7 @@ impl rtio::IoFactory for IoFactory {
217252
}
218253
fn tty_open(&mut self, fd: c_int, _readable: bool) -> IoResult<~RtioTTY> {
219254
if unsafe { libc::isatty(fd) } != 0 {
220-
// Don't ever close the stdio file descriptors, nothing good really
221-
// comes of that.
222-
Ok(~file::FileDesc::new(fd, fd > libc::STDERR_FILENO) as ~RtioTTY)
255+
Ok(~file::FileDesc::new(fd, true) as ~RtioTTY)
223256
} else {
224257
Err(IoError {
225258
kind: io::MismatchedFileTypeForOperation,

0 commit comments

Comments
 (0)