2013-02-03 18:15:43 -08:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-09-16 15:28:56 -07:00
|
|
|
use libc;
|
2013-02-03 18:15:43 -08:00
|
|
|
use option::*;
|
|
|
|
use result::*;
|
2013-10-25 21:55:10 -07:00
|
|
|
use comm::{SharedChan, PortOne, Port};
|
2013-08-19 16:10:43 -07:00
|
|
|
use libc::c_int;
|
2013-10-16 16:48:30 -07:00
|
|
|
use c_str::CString;
|
2013-02-03 18:15:43 -08:00
|
|
|
|
2013-10-15 20:37:39 -07:00
|
|
|
use ai = rt::io::net::addrinfo;
|
2013-04-24 20:20:03 -07:00
|
|
|
use rt::io::IoError;
|
2013-09-19 12:03:50 +08:00
|
|
|
use rt::io::signal::Signum;
|
2013-09-16 15:28:56 -07:00
|
|
|
use super::io::process::ProcessConfig;
|
2013-08-03 02:06:24 +04:00
|
|
|
use super::io::net::ip::{IpAddr, SocketAddr};
|
2013-08-19 16:10:43 -07:00
|
|
|
use path::Path;
|
2013-08-22 15:03:28 -07:00
|
|
|
use super::io::{SeekStyle};
|
2013-10-25 17:04:37 -07:00
|
|
|
use super::io::{FileMode, FileAccess, FileStat, FilePermission};
|
2013-04-17 17:55:21 -07:00
|
|
|
|
2013-11-04 12:45:05 -08:00
|
|
|
pub trait Callback {
|
|
|
|
fn call(&mut self);
|
|
|
|
}
|
|
|
|
|
2013-02-03 18:15:43 -08:00
|
|
|
pub trait EventLoop {
|
|
|
|
fn run(&mut self);
|
2013-11-04 12:45:05 -08:00
|
|
|
fn callback(&mut self, proc());
|
2013-08-16 13:41:30 -07:00
|
|
|
fn pausible_idle_callback(&mut self) -> ~PausibleIdleCallback;
|
2013-11-04 12:45:05 -08:00
|
|
|
fn remote_callback(&mut self, ~Callback) -> ~RemoteCallback;
|
2013-10-16 17:05:28 -07:00
|
|
|
|
2013-02-03 18:15:43 -08:00
|
|
|
/// The asynchronous I/O services. Not all event loops may provide one
|
2013-10-16 17:05:28 -07:00
|
|
|
// FIXME(#9382) this is an awful interface
|
|
|
|
fn io<'a>(&'a mut self, f: &fn(&'a mut IoFactory));
|
2013-02-03 18:15:43 -08:00
|
|
|
}
|
|
|
|
|
2013-05-20 18:23:56 -07:00
|
|
|
pub trait RemoteCallback {
|
2013-08-16 13:41:30 -07:00
|
|
|
/// Trigger the remote callback. Note that the number of times the
|
|
|
|
/// callback is run is not guaranteed. All that is guaranteed is
|
|
|
|
/// that, after calling 'fire', the callback will be called at
|
|
|
|
/// least once, but multiple callbacks may be coalesced and
|
|
|
|
/// callbacks may be called more often requested. Destruction also
|
|
|
|
/// triggers the callback.
|
2013-05-20 18:23:56 -07:00
|
|
|
fn fire(&mut self);
|
|
|
|
}
|
|
|
|
|
2013-08-19 16:10:43 -07:00
|
|
|
/// Data needed to make a successful open(2) call
|
|
|
|
/// Using unix flag conventions for now, which happens to also be what's supported
|
|
|
|
/// libuv (it does translation to windows under the hood).
|
|
|
|
pub struct FileOpenConfig {
|
|
|
|
/// Path to file to be opened
|
|
|
|
path: Path,
|
|
|
|
/// Flags for file access mode (as per open(2))
|
|
|
|
flags: int,
|
|
|
|
/// File creation mode, ignored unless O_CREAT is passed as part of flags
|
2013-10-20 06:03:09 +05:30
|
|
|
priv mode: int
|
2013-08-19 16:10:43 -07:00
|
|
|
}
|
|
|
|
|
2013-10-22 09:36:30 -07:00
|
|
|
/// Description of what to do when a file handle is closed
|
|
|
|
pub enum CloseBehavior {
|
|
|
|
/// Do not close this handle when the object is destroyed
|
|
|
|
DontClose,
|
|
|
|
/// Synchronously close the handle, meaning that the task will block when
|
|
|
|
/// the handle is destroyed until it has been fully closed.
|
|
|
|
CloseSynchronously,
|
|
|
|
/// Asynchronously closes a handle, meaning that the task will *not* block
|
|
|
|
/// when the handle is destroyed, but the handle will still get deallocated
|
|
|
|
/// and cleaned up (but this will happen asynchronously on the local event
|
|
|
|
/// loop).
|
|
|
|
CloseAsynchronously,
|
|
|
|
}
|
|
|
|
|
2013-10-16 17:05:28 -07:00
|
|
|
pub fn with_local_io<T>(f: &fn(&mut IoFactory) -> Option<T>) -> Option<T> {
|
|
|
|
use rt::sched::Scheduler;
|
|
|
|
use rt::local::Local;
|
|
|
|
use rt::io::{io_error, standard_error, IoUnavailable};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let sched: *mut Scheduler = Local::unsafe_borrow();
|
|
|
|
let mut io = None;
|
|
|
|
(*sched).event_loop.io(|i| io = Some(i));
|
|
|
|
match io {
|
|
|
|
Some(io) => f(io),
|
|
|
|
None => {
|
|
|
|
io_error::cond.raise(standard_error(IoUnavailable));
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-03 18:15:43 -08:00
|
|
|
pub trait IoFactory {
|
2013-10-29 23:31:07 -07:00
|
|
|
// networking
|
2013-10-16 14:48:05 -07:00
|
|
|
fn tcp_connect(&mut self, addr: SocketAddr) -> Result<~RtioTcpStream, IoError>;
|
2013-10-17 12:13:29 -07:00
|
|
|
fn tcp_bind(&mut self, addr: SocketAddr) -> Result<~RtioTcpListener, IoError>;
|
2013-10-16 14:48:05 -07:00
|
|
|
fn udp_bind(&mut self, addr: SocketAddr) -> Result<~RtioUdpSocket, IoError>;
|
2013-10-29 23:31:07 -07:00
|
|
|
fn unix_bind(&mut self, path: &CString) ->
|
|
|
|
Result<~RtioUnixListener, IoError>;
|
|
|
|
fn unix_connect(&mut self, path: &CString) -> Result<~RtioPipe, IoError>;
|
2013-10-16 17:05:28 -07:00
|
|
|
fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
|
|
|
|
hint: Option<ai::Hint>) -> Result<~[ai::Info], IoError>;
|
2013-10-29 23:31:07 -07:00
|
|
|
|
|
|
|
// filesystem operations
|
2013-10-22 09:36:30 -07:00
|
|
|
fn fs_from_raw_fd(&mut self, fd: c_int, close: CloseBehavior) -> ~RtioFileStream;
|
2013-10-16 16:48:30 -07:00
|
|
|
fn fs_open(&mut self, path: &CString, fm: FileMode, fa: FileAccess)
|
2013-08-20 15:38:41 -07:00
|
|
|
-> Result<~RtioFileStream, IoError>;
|
2013-10-16 17:05:28 -07:00
|
|
|
fn fs_unlink(&mut self, path: &CString) -> Result<(), IoError>;
|
2013-10-16 16:48:30 -07:00
|
|
|
fn fs_stat(&mut self, path: &CString) -> Result<FileStat, IoError>;
|
2013-10-25 17:04:37 -07:00
|
|
|
fn fs_mkdir(&mut self, path: &CString,
|
|
|
|
mode: FilePermission) -> Result<(), IoError>;
|
|
|
|
fn fs_chmod(&mut self, path: &CString,
|
|
|
|
mode: FilePermission) -> Result<(), IoError>;
|
2013-10-16 16:48:30 -07:00
|
|
|
fn fs_rmdir(&mut self, path: &CString) -> Result<(), IoError>;
|
2013-10-25 16:50:08 -07:00
|
|
|
fn fs_rename(&mut self, path: &CString, to: &CString) -> Result<(), IoError>;
|
2013-10-16 16:48:30 -07:00
|
|
|
fn fs_readdir(&mut self, path: &CString, flags: c_int) ->
|
2013-09-16 13:25:10 -07:00
|
|
|
Result<~[Path], IoError>;
|
2013-10-29 23:31:07 -07:00
|
|
|
fn fs_lstat(&mut self, path: &CString) -> Result<FileStat, IoError>;
|
|
|
|
fn fs_chown(&mut self, path: &CString, uid: int, gid: int) ->
|
|
|
|
Result<(), IoError>;
|
|
|
|
fn fs_readlink(&mut self, path: &CString) -> Result<Path, IoError>;
|
|
|
|
fn fs_symlink(&mut self, src: &CString, dst: &CString) -> Result<(), IoError>;
|
|
|
|
fn fs_link(&mut self, src: &CString, dst: &CString) -> Result<(), IoError>;
|
2013-11-05 15:48:27 -08:00
|
|
|
fn fs_utime(&mut self, src: &CString, atime: u64, mtime: u64) ->
|
|
|
|
Result<(), IoError>;
|
2013-10-29 23:31:07 -07:00
|
|
|
|
|
|
|
// misc
|
|
|
|
fn timer_init(&mut self) -> Result<~RtioTimer, IoError>;
|
2013-09-16 15:28:56 -07:00
|
|
|
fn spawn(&mut self, config: ProcessConfig)
|
2013-10-16 14:48:05 -07:00
|
|
|
-> Result<(~RtioProcess, ~[Option<~RtioPipe>]), IoError>;
|
2013-10-18 14:01:22 -07:00
|
|
|
fn pipe_open(&mut self, fd: c_int) -> Result<~RtioPipe, IoError>;
|
2013-10-17 17:04:51 -07:00
|
|
|
fn tty_open(&mut self, fd: c_int, readable: bool)
|
2013-10-16 14:48:05 -07:00
|
|
|
-> Result<~RtioTTY, IoError>;
|
2013-09-19 12:03:50 +08:00
|
|
|
fn signal(&mut self, signal: Signum, channel: SharedChan<Signum>)
|
|
|
|
-> Result<~RtioSignal, IoError>;
|
2013-08-26 07:24:10 -07:00
|
|
|
}
|
|
|
|
|
2013-07-02 16:40:57 -07:00
|
|
|
pub trait RtioTcpListener : RtioSocket {
|
2013-10-17 12:13:29 -07:00
|
|
|
fn listen(~self) -> Result<~RtioTcpAcceptor, IoError>;
|
2013-08-27 10:01:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RtioTcpAcceptor : RtioSocket {
|
2013-10-16 14:48:05 -07:00
|
|
|
fn accept(&mut self) -> Result<~RtioTcpStream, IoError>;
|
2013-07-25 19:42:19 -04:00
|
|
|
fn accept_simultaneously(&mut self) -> Result<(), IoError>;
|
|
|
|
fn dont_accept_simultaneously(&mut self) -> Result<(), IoError>;
|
2013-02-03 18:15:43 -08:00
|
|
|
}
|
|
|
|
|
2013-08-29 14:20:48 -07:00
|
|
|
pub trait RtioTcpStream : RtioSocket {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<uint, IoError>;
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<(), IoError>;
|
2013-08-03 02:06:24 +04:00
|
|
|
fn peer_name(&mut self) -> Result<SocketAddr, IoError>;
|
2013-07-25 19:42:19 -04:00
|
|
|
fn control_congestion(&mut self) -> Result<(), IoError>;
|
|
|
|
fn nodelay(&mut self) -> Result<(), IoError>;
|
|
|
|
fn keepalive(&mut self, delay_in_seconds: uint) -> Result<(), IoError>;
|
|
|
|
fn letdie(&mut self) -> Result<(), IoError>;
|
2013-02-03 18:15:43 -08:00
|
|
|
}
|
2013-06-17 12:32:21 -07:00
|
|
|
|
2013-07-02 16:40:57 -07:00
|
|
|
pub trait RtioSocket {
|
2013-08-03 02:06:24 +04:00
|
|
|
fn socket_name(&mut self) -> Result<SocketAddr, IoError>;
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RtioUdpSocket : RtioSocket {
|
2013-08-03 02:06:24 +04:00
|
|
|
fn recvfrom(&mut self, buf: &mut [u8]) -> Result<(uint, SocketAddr), IoError>;
|
|
|
|
fn sendto(&mut self, buf: &[u8], dst: SocketAddr) -> Result<(), IoError>;
|
2013-07-02 16:40:57 -07:00
|
|
|
|
2013-07-25 22:21:46 -04:00
|
|
|
fn join_multicast(&mut self, multi: IpAddr) -> Result<(), IoError>;
|
|
|
|
fn leave_multicast(&mut self, multi: IpAddr) -> Result<(), IoError>;
|
2013-07-02 16:40:57 -07:00
|
|
|
|
2013-07-25 22:21:46 -04:00
|
|
|
fn loop_multicast_locally(&mut self) -> Result<(), IoError>;
|
|
|
|
fn dont_loop_multicast_locally(&mut self) -> Result<(), IoError>;
|
2013-07-02 16:40:57 -07:00
|
|
|
|
2013-07-25 22:21:46 -04:00
|
|
|
fn multicast_time_to_live(&mut self, ttl: int) -> Result<(), IoError>;
|
|
|
|
fn time_to_live(&mut self, ttl: int) -> Result<(), IoError>;
|
2013-07-02 16:40:57 -07:00
|
|
|
|
2013-07-25 22:21:46 -04:00
|
|
|
fn hear_broadcasts(&mut self) -> Result<(), IoError>;
|
|
|
|
fn ignore_broadcasts(&mut self) -> Result<(), IoError>;
|
2013-06-17 12:32:21 -07:00
|
|
|
}
|
2013-07-19 16:02:38 -07:00
|
|
|
|
|
|
|
pub trait RtioTimer {
|
2013-08-08 18:58:18 -07:00
|
|
|
fn sleep(&mut self, msecs: u64);
|
2013-10-25 21:55:10 -07:00
|
|
|
fn oneshot(&mut self, msecs: u64) -> PortOne<()>;
|
|
|
|
fn period(&mut self, msecs: u64) -> Port<()>;
|
2013-07-19 16:02:38 -07:00
|
|
|
}
|
2013-08-19 16:10:43 -07:00
|
|
|
|
2013-08-20 15:38:41 -07:00
|
|
|
pub trait RtioFileStream {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<int, IoError>;
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<(), IoError>;
|
|
|
|
fn pread(&mut self, buf: &mut [u8], offset: u64) -> Result<int, IoError>;
|
|
|
|
fn pwrite(&mut self, buf: &[u8], offset: u64) -> Result<(), IoError>;
|
2013-08-22 15:03:28 -07:00
|
|
|
fn seek(&mut self, pos: i64, whence: SeekStyle) -> Result<u64, IoError>;
|
2013-08-20 15:38:41 -07:00
|
|
|
fn tell(&self) -> Result<u64, IoError>;
|
2013-10-29 23:31:07 -07:00
|
|
|
fn fsync(&mut self) -> Result<(), IoError>;
|
|
|
|
fn datasync(&mut self) -> Result<(), IoError>;
|
|
|
|
fn truncate(&mut self, offset: i64) -> Result<(), IoError>;
|
2013-08-19 16:10:43 -07:00
|
|
|
}
|
2013-09-16 15:28:56 -07:00
|
|
|
|
|
|
|
pub trait RtioProcess {
|
|
|
|
fn id(&self) -> libc::pid_t;
|
|
|
|
fn kill(&mut self, signal: int) -> Result<(), IoError>;
|
|
|
|
fn wait(&mut self) -> int;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RtioPipe {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<uint, IoError>;
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<(), IoError>;
|
|
|
|
}
|
2013-10-15 19:44:08 -07:00
|
|
|
|
|
|
|
pub trait RtioUnixListener {
|
2013-10-17 12:13:29 -07:00
|
|
|
fn listen(~self) -> Result<~RtioUnixAcceptor, IoError>;
|
2013-10-15 19:44:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RtioUnixAcceptor {
|
2013-10-16 14:48:05 -07:00
|
|
|
fn accept(&mut self) -> Result<~RtioPipe, IoError>;
|
2013-10-15 19:44:08 -07:00
|
|
|
}
|
2013-10-16 11:47:12 -07:00
|
|
|
|
|
|
|
pub trait RtioTTY {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<uint, IoError>;
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<(), IoError>;
|
|
|
|
fn set_raw(&mut self, raw: bool) -> Result<(), IoError>;
|
|
|
|
fn get_winsize(&mut self) -> Result<(int, int), IoError>;
|
2013-10-17 17:04:51 -07:00
|
|
|
fn isatty(&self) -> bool;
|
2013-10-16 11:47:12 -07:00
|
|
|
}
|
2013-10-16 14:48:05 -07:00
|
|
|
|
|
|
|
pub trait PausibleIdleCallback {
|
2013-11-04 12:45:05 -08:00
|
|
|
fn start(&mut self, f: ~Callback);
|
2013-10-16 14:48:05 -07:00
|
|
|
fn pause(&mut self);
|
|
|
|
fn resume(&mut self);
|
|
|
|
fn close(&mut self);
|
|
|
|
}
|
2013-09-19 12:03:50 +08:00
|
|
|
|
|
|
|
pub trait RtioSignal {}
|