Skip to content

Commit c2dcd5e

Browse files
committed
Runtime removal: refactor pipes and networking
This patch continues the runtime removal by moving pipe and networking-related code into `sys`. Because this eliminates APIs in `libnative` and `librustrt`, it is a: [breaking-change] This functionality is likely to be available publicly, in some form, from `std` in the future.
1 parent 0d4d242 commit c2dcd5e

File tree

19 files changed

+1183
-1289
lines changed

19 files changed

+1183
-1289
lines changed

src/libnative/io/mod.rs

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ pub use self::process::Process;
3535
mod helper_thread;
3636

3737
// Native I/O implementations
38-
pub mod addrinfo;
39-
pub mod net;
4038
pub mod process;
4139
mod util;
4240

@@ -53,14 +51,6 @@ pub mod timer;
5351
#[path = "timer_windows.rs"]
5452
pub mod timer;
5553

56-
#[cfg(unix)]
57-
#[path = "pipe_unix.rs"]
58-
pub mod pipe;
59-
60-
#[cfg(windows)]
61-
#[path = "pipe_windows.rs"]
62-
pub mod pipe;
63-
6454
#[cfg(windows)]
6555
#[path = "tty_windows.rs"]
6656
mod tty;
@@ -126,52 +116,11 @@ pub struct IoFactory {
126116

127117
impl IoFactory {
128118
pub fn new() -> IoFactory {
129-
net::init();
130119
IoFactory { _cannot_construct_outside_of_this_module: () }
131120
}
132121
}
133122

134123
impl rtio::IoFactory for IoFactory {
135-
// networking
136-
fn tcp_connect(&mut self, addr: rtio::SocketAddr,
137-
timeout: Option<u64>)
138-
-> IoResult<Box<rtio::RtioTcpStream + Send>>
139-
{
140-
net::TcpStream::connect(addr, timeout).map(|s| {
141-
box s as Box<rtio::RtioTcpStream + Send>
142-
})
143-
}
144-
fn tcp_bind(&mut self, addr: rtio::SocketAddr)
145-
-> IoResult<Box<rtio::RtioTcpListener + Send>> {
146-
net::TcpListener::bind(addr).map(|s| {
147-
box s as Box<rtio::RtioTcpListener + Send>
148-
})
149-
}
150-
fn udp_bind(&mut self, addr: rtio::SocketAddr)
151-
-> IoResult<Box<rtio::RtioUdpSocket + Send>> {
152-
net::UdpSocket::bind(addr).map(|u| {
153-
box u as Box<rtio::RtioUdpSocket + Send>
154-
})
155-
}
156-
fn unix_bind(&mut self, path: &CString)
157-
-> IoResult<Box<rtio::RtioUnixListener + Send>> {
158-
pipe::UnixListener::bind(path).map(|s| {
159-
box s as Box<rtio::RtioUnixListener + Send>
160-
})
161-
}
162-
fn unix_connect(&mut self, path: &CString,
163-
timeout: Option<u64>) -> IoResult<Box<rtio::RtioPipe + Send>> {
164-
pipe::UnixStream::connect(path, timeout).map(|s| {
165-
box s as Box<rtio::RtioPipe + Send>
166-
})
167-
}
168-
fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
169-
hint: Option<rtio::AddrinfoHint>)
170-
-> IoResult<Vec<rtio::AddrinfoInfo>>
171-
{
172-
addrinfo::GetAddrInfoRequest::run(host, servname, hint)
173-
}
174-
175124
// misc
176125
fn timer_init(&mut self) -> IoResult<Box<rtio::RtioTimer + Send>> {
177126
timer::Timer::new().map(|t| box t as Box<rtio::RtioTimer + Send>)
@@ -189,9 +138,6 @@ impl rtio::IoFactory for IoFactory {
189138
fn kill(&mut self, pid: libc::pid_t, signum: int) -> IoResult<()> {
190139
process::Process::kill(pid, signum)
191140
}
192-
fn pipe_open(&mut self, fd: c_int) -> IoResult<Box<rtio::RtioPipe + Send>> {
193-
Ok(box file::FileDesc::new(fd, true) as Box<rtio::RtioPipe + Send>)
194-
}
195141
#[cfg(unix)]
196142
fn tty_open(&mut self, fd: c_int, _readable: bool)
197143
-> IoResult<Box<rtio::RtioTTY + Send>> {

src/librustrt/rtio.rs

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@
1313
use core::prelude::*;
1414
use alloc::boxed::Box;
1515
use collections::string::String;
16-
use collections::vec::Vec;
17-
use core::fmt;
1816
use core::mem;
1917
use libc::c_int;
20-
use libc;
2118

22-
use c_str::CString;
2319
use local::Local;
2420
use task::Task;
2521

@@ -173,87 +169,15 @@ impl<'a> LocalIo<'a> {
173169
}
174170

175171
pub trait IoFactory {
176-
// networking
177-
fn tcp_connect(&mut self, addr: SocketAddr,
178-
timeout: Option<u64>) -> IoResult<Box<RtioTcpStream + Send>>;
179-
fn tcp_bind(&mut self, addr: SocketAddr)
180-
-> IoResult<Box<RtioTcpListener + Send>>;
181-
fn udp_bind(&mut self, addr: SocketAddr)
182-
-> IoResult<Box<RtioUdpSocket + Send>>;
183-
fn unix_bind(&mut self, path: &CString)
184-
-> IoResult<Box<RtioUnixListener + Send>>;
185-
fn unix_connect(&mut self, path: &CString,
186-
timeout: Option<u64>) -> IoResult<Box<RtioPipe + Send>>;
187-
fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
188-
hint: Option<AddrinfoHint>)
189-
-> IoResult<Vec<AddrinfoInfo>>;
190-
191-
// misc
192172
fn timer_init(&mut self) -> IoResult<Box<RtioTimer + Send>>;
193173
fn spawn(&mut self, cfg: ProcessConfig)
194174
-> IoResult<(Box<RtioProcess + Send>,
195175
Vec<Option<Box<RtioPipe + Send>>>)>;
196176
fn kill(&mut self, pid: libc::pid_t, signal: int) -> IoResult<()>;
197-
fn pipe_open(&mut self, fd: c_int) -> IoResult<Box<RtioPipe + Send>>;
198177
fn tty_open(&mut self, fd: c_int, readable: bool)
199178
-> IoResult<Box<RtioTTY + Send>>;
200179
}
201180

202-
pub trait RtioTcpListener : RtioSocket {
203-
fn listen(self: Box<Self>) -> IoResult<Box<RtioTcpAcceptor + Send>>;
204-
}
205-
206-
pub trait RtioTcpAcceptor : RtioSocket {
207-
fn accept(&mut self) -> IoResult<Box<RtioTcpStream + Send>>;
208-
fn accept_simultaneously(&mut self) -> IoResult<()>;
209-
fn dont_accept_simultaneously(&mut self) -> IoResult<()>;
210-
fn set_timeout(&mut self, timeout: Option<u64>);
211-
fn clone(&self) -> Box<RtioTcpAcceptor + Send>;
212-
fn close_accept(&mut self) -> IoResult<()>;
213-
}
214-
215-
pub trait RtioTcpStream : RtioSocket {
216-
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
217-
fn write(&mut self, buf: &[u8]) -> IoResult<()>;
218-
fn peer_name(&mut self) -> IoResult<SocketAddr>;
219-
fn control_congestion(&mut self) -> IoResult<()>;
220-
fn nodelay(&mut self) -> IoResult<()>;
221-
fn keepalive(&mut self, delay_in_seconds: uint) -> IoResult<()>;
222-
fn letdie(&mut self) -> IoResult<()>;
223-
fn clone(&self) -> Box<RtioTcpStream + Send>;
224-
fn close_write(&mut self) -> IoResult<()>;
225-
fn close_read(&mut self) -> IoResult<()>;
226-
fn set_timeout(&mut self, timeout_ms: Option<u64>);
227-
fn set_read_timeout(&mut self, timeout_ms: Option<u64>);
228-
fn set_write_timeout(&mut self, timeout_ms: Option<u64>);
229-
}
230-
231-
pub trait RtioSocket {
232-
fn socket_name(&mut self) -> IoResult<SocketAddr>;
233-
}
234-
235-
pub trait RtioUdpSocket : RtioSocket {
236-
fn recv_from(&mut self, buf: &mut [u8]) -> IoResult<(uint, SocketAddr)>;
237-
fn send_to(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()>;
238-
239-
fn join_multicast(&mut self, multi: IpAddr) -> IoResult<()>;
240-
fn leave_multicast(&mut self, multi: IpAddr) -> IoResult<()>;
241-
242-
fn loop_multicast_locally(&mut self) -> IoResult<()>;
243-
fn dont_loop_multicast_locally(&mut self) -> IoResult<()>;
244-
245-
fn multicast_time_to_live(&mut self, ttl: int) -> IoResult<()>;
246-
fn time_to_live(&mut self, ttl: int) -> IoResult<()>;
247-
248-
fn hear_broadcasts(&mut self) -> IoResult<()>;
249-
fn ignore_broadcasts(&mut self) -> IoResult<()>;
250-
251-
fn clone(&self) -> Box<RtioUdpSocket + Send>;
252-
fn set_timeout(&mut self, timeout_ms: Option<u64>);
253-
fn set_read_timeout(&mut self, timeout_ms: Option<u64>);
254-
fn set_write_timeout(&mut self, timeout_ms: Option<u64>);
255-
}
256-
257181
pub trait RtioTimer {
258182
fn sleep(&mut self, msecs: u64);
259183
fn oneshot(&mut self, msecs: u64, cb: Box<Callback + Send>);
@@ -313,54 +237,3 @@ pub struct IoError {
313237
}
314238

315239
pub type IoResult<T> = Result<T, IoError>;
316-
317-
#[deriving(PartialEq, Eq)]
318-
pub enum IpAddr {
319-
Ipv4Addr(u8, u8, u8, u8),
320-
Ipv6Addr(u16, u16, u16, u16, u16, u16, u16, u16),
321-
}
322-
323-
impl fmt::Show for IpAddr {
324-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
325-
match *self {
326-
Ipv4Addr(a, b, c, d) => write!(fmt, "{}.{}.{}.{}", a, b, c, d),
327-
Ipv6Addr(a, b, c, d, e, f, g, h) => {
328-
write!(fmt,
329-
"{:04x}:{:04x}:{:04x}:{:04x}:{:04x}:{:04x}:{:04x}:{:04x}",
330-
a, b, c, d, e, f, g, h)
331-
}
332-
}
333-
}
334-
}
335-
336-
#[deriving(PartialEq, Eq)]
337-
pub struct SocketAddr {
338-
pub ip: IpAddr,
339-
pub port: u16,
340-
}
341-
342-
pub enum StdioContainer {
343-
Ignored,
344-
InheritFd(i32),
345-
CreatePipe(bool, bool),
346-
}
347-
348-
pub enum ProcessExit {
349-
ExitStatus(int),
350-
ExitSignal(int),
351-
}
352-
353-
pub struct AddrinfoHint {
354-
pub family: uint,
355-
pub socktype: uint,
356-
pub protocol: uint,
357-
pub flags: uint,
358-
}
359-
360-
pub struct AddrinfoInfo {
361-
pub address: SocketAddr,
362-
pub family: uint,
363-
pub socktype: uint,
364-
pub protocol: uint,
365-
pub flags: uint,
366-
}

src/libstd/io/net/addrinfo.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ getaddrinfo()
2020
#![allow(missing_docs)]
2121

2222
use iter::Iterator;
23-
use io::{IoResult, IoError};
23+
use io::{IoResult};
2424
use io::net::ip::{SocketAddr, IpAddr};
2525
use option::{Option, Some, None};
26-
use result::{Ok, Err};
27-
use rt::rtio::{IoFactory, LocalIo};
28-
use rt::rtio;
26+
use sys;
2927
use vec::Vec;
3028

3129
/// Hints to the types of sockets that are desired when looking up hosts
@@ -94,31 +92,7 @@ pub fn get_host_addresses(host: &str) -> IoResult<Vec<IpAddr>> {
9492
#[allow(unused_variables)]
9593
fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option<Hint>)
9694
-> IoResult<Vec<Info>> {
97-
let hint = hint.map(|Hint { family, socktype, protocol, flags }| {
98-
rtio::AddrinfoHint {
99-
family: family,
100-
socktype: 0, // FIXME: this should use the above variable
101-
protocol: 0, // FIXME: this should use the above variable
102-
flags: flags,
103-
}
104-
});
105-
match LocalIo::maybe_raise(|io| {
106-
io.get_host_addresses(hostname, servname, hint)
107-
}) {
108-
Ok(v) => Ok(v.into_iter().map(|info| {
109-
Info {
110-
address: SocketAddr {
111-
ip: super::from_rtio(info.address.ip),
112-
port: info.address.port,
113-
},
114-
family: info.family,
115-
socktype: None, // FIXME: this should use the above variable
116-
protocol: None, // FIXME: this should use the above variable
117-
flags: info.flags,
118-
}
119-
}).collect()),
120-
Err(e) => Err(IoError::from_rtio_error(e)),
121-
}
95+
sys::addrinfo::get_host_addresses(hostname, servname, hint)
12296
}
12397

12498
// Ignored on android since we cannot give tcp/ip

src/libstd/io/net/mod.rs

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
1313
use io::{IoError, IoResult, InvalidInput};
1414
use option::None;
15-
use result::{Result, Ok, Err};
16-
use rt::rtio;
17-
use self::ip::{Ipv4Addr, Ipv6Addr, IpAddr, SocketAddr, ToSocketAddr};
15+
use result::{Ok, Err};
16+
use self::ip::{SocketAddr, ToSocketAddr};
1817

1918
pub use self::addrinfo::get_host_addresses;
2019

@@ -24,46 +23,6 @@ pub mod udp;
2423
pub mod ip;
2524
pub mod pipe;
2625

27-
fn to_rtio(ip: IpAddr) -> rtio::IpAddr {
28-
match ip {
29-
Ipv4Addr(a, b, c, d) => rtio::Ipv4Addr(a, b, c, d),
30-
Ipv6Addr(a, b, c, d, e, f, g, h) => {
31-
rtio::Ipv6Addr(a, b, c, d, e, f, g, h)
32-
}
33-
}
34-
}
35-
36-
fn from_rtio(ip: rtio::IpAddr) -> IpAddr {
37-
match ip {
38-
rtio::Ipv4Addr(a, b, c, d) => Ipv4Addr(a, b, c, d),
39-
rtio::Ipv6Addr(a, b, c, d, e, f, g, h) => {
40-
Ipv6Addr(a, b, c, d, e, f, g, h)
41-
}
42-
}
43-
}
44-
45-
fn with_addresses_io<A: ToSocketAddr, T>(
46-
addr: A,
47-
action: |&mut rtio::IoFactory, rtio::SocketAddr| -> Result<T, rtio::IoError>
48-
) -> Result<T, IoError> {
49-
const DEFAULT_ERROR: IoError = IoError {
50-
kind: InvalidInput,
51-
desc: "no addresses found for hostname",
52-
detail: None
53-
};
54-
55-
let addresses = try!(addr.to_socket_addr_all());
56-
let mut err = DEFAULT_ERROR;
57-
for addr in addresses.into_iter() {
58-
let addr = rtio::SocketAddr { ip: to_rtio(addr.ip), port: addr.port };
59-
match rtio::LocalIo::maybe_raise(|io| action(io, addr)) {
60-
Ok(r) => return Ok(r),
61-
Err(e) => err = IoError::from_rtio_error(e)
62-
}
63-
}
64-
Err(err)
65-
}
66-
6726
fn with_addresses<A: ToSocketAddr, T>(addr: A, action: |SocketAddr| -> IoResult<T>)
6827
-> IoResult<T> {
6928
const DEFAULT_ERROR: IoError = IoError {

0 commit comments

Comments
 (0)