1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use core::prelude::*;
use alloc::owned::Box;
use collections::string::String;
use collections::vec::Vec;
use core::fmt;
use core::mem;
use libc::c_int;
use libc;
use c_str::CString;
use local::Local;
use task::Task;
pub trait EventLoop {
fn run(&mut self);
fn callback(&mut self, arg: proc(): Send);
fn pausable_idle_callback(&mut self, Box<Callback + Send>)
-> Box<PausableIdleCallback + Send>;
fn remote_callback(&mut self, Box<Callback + Send>)
-> Box<RemoteCallback + Send>;
fn io<'a>(&'a mut self) -> Option<&'a mut IoFactory>;
fn has_active_io(&self) -> bool;
}
pub trait Callback {
fn call(&mut self);
}
pub trait RemoteCallback {
fn fire(&mut self);
}
pub enum CloseBehavior {
DontClose,
CloseSynchronously,
CloseAsynchronously,
}
pub struct ProcessConfig<'a> {
pub program: &'a CString,
pub args: &'a [CString],
pub env: Option<&'a [(CString, CString)]>,
pub cwd: Option<&'a CString>,
pub stdin: StdioContainer,
pub stdout: StdioContainer,
pub stderr: StdioContainer,
pub extra_io: &'a [StdioContainer],
pub uid: Option<uint>,
pub gid: Option<uint>,
pub detach: bool,
}
pub struct LocalIo<'a> {
factory: &'a mut IoFactory,
}
#[unsafe_destructor]
impl<'a> Drop for LocalIo<'a> {
fn drop(&mut self) {}
}
impl<'a> LocalIo<'a> {
pub fn borrow() -> Option<LocalIo> {let mut t: Box<Task> = match Local::try_take() {
Some(t) => t,
None => return None,
};
let ret = t.local_io().map(|t| {
unsafe { mem::transmute_copy(&t) }
});
Local::put(t);
return ret;
}
pub fn maybe_raise<T>(f: |io: &mut IoFactory| -> IoResult<T>)
-> IoResult<T>
{
#[cfg(unix)] use ERROR = libc::EINVAL;
#[cfg(windows)] use ERROR = libc::ERROR_CALL_NOT_IMPLEMENTED;
match LocalIo::borrow() {
Some(mut io) => f(io.get()),
None => Err(IoError {
code: ERROR as uint,
extra: 0,
detail: None,
}),
}
}
pub fn new<'a>(io: &'a mut IoFactory) -> LocalIo<'a> {
LocalIo { factory: io }
}
#[inline]
pub fn get<'a>(&'a mut self) -> &'a mut IoFactory {
let f: &'a mut IoFactory = self.factory;
f
}
}
pub trait IoFactory {fn tcp_connect(&mut self, addr: SocketAddr,
timeout: Option<u64>) -> IoResult<Box<RtioTcpStream + Send>>;
fn tcp_bind(&mut self, addr: SocketAddr)
-> IoResult<Box<RtioTcpListener + Send>>;
fn udp_bind(&mut self, addr: SocketAddr)
-> IoResult<Box<RtioUdpSocket + Send>>;
fn unix_bind(&mut self, path: &CString)
-> IoResult<Box<RtioUnixListener + Send>>;
fn unix_connect(&mut self, path: &CString,
timeout: Option<u64>) -> IoResult<Box<RtioPipe + Send>>;
fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
hint: Option<AddrinfoHint>)
-> IoResult<Vec<AddrinfoInfo>>;fn fs_from_raw_fd(&mut self, fd: c_int, close: CloseBehavior)
-> Box<RtioFileStream + Send>;
fn fs_open(&mut self, path: &CString, fm: FileMode, fa: FileAccess)
-> IoResult<Box<RtioFileStream + Send>>;
fn fs_unlink(&mut self, path: &CString) -> IoResult<()>;
fn fs_stat(&mut self, path: &CString) -> IoResult<FileStat>;
fn fs_mkdir(&mut self, path: &CString, mode: uint) -> IoResult<()>;
fn fs_chmod(&mut self, path: &CString, mode: uint) -> IoResult<()>;
fn fs_rmdir(&mut self, path: &CString) -> IoResult<()>;
fn fs_rename(&mut self, path: &CString, to: &CString) -> IoResult<()>;
fn fs_readdir(&mut self, path: &CString, flags: c_int) ->
IoResult<Vec<CString>>;
fn fs_lstat(&mut self, path: &CString) -> IoResult<FileStat>;
fn fs_chown(&mut self, path: &CString, uid: int, gid: int) ->
IoResult<()>;
fn fs_readlink(&mut self, path: &CString) -> IoResult<CString>;
fn fs_symlink(&mut self, src: &CString, dst: &CString) -> IoResult<()>;
fn fs_link(&mut self, src: &CString, dst: &CString) -> IoResult<()>;
fn fs_utime(&mut self, src: &CString, atime: u64, mtime: u64) ->
IoResult<()>;fn timer_init(&mut self) -> IoResult<Box<RtioTimer + Send>>;
fn spawn(&mut self, cfg: ProcessConfig)
-> IoResult<(Box<RtioProcess + Send>,
Vec<Option<Box<RtioPipe + Send>>>)>;
fn kill(&mut self, pid: libc::pid_t, signal: int) -> IoResult<()>;
fn pipe_open(&mut self, fd: c_int) -> IoResult<Box<RtioPipe + Send>>;
fn tty_open(&mut self, fd: c_int, readable: bool)
-> IoResult<Box<RtioTTY + Send>>;
fn signal(&mut self, signal: int, cb: Box<Callback + Send>)
-> IoResult<Box<RtioSignal + Send>>;
}
pub trait RtioTcpListener : RtioSocket {
fn listen(~self) -> IoResult<Box<RtioTcpAcceptor + Send>>;
}
pub trait RtioTcpAcceptor : RtioSocket {
fn accept(&mut self) -> IoResult<Box<RtioTcpStream + Send>>;
fn accept_simultaneously(&mut self) -> IoResult<()>;
fn dont_accept_simultaneously(&mut self) -> IoResult<()>;
fn set_timeout(&mut self, timeout: Option<u64>);
}
pub trait RtioTcpStream : RtioSocket {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
fn write(&mut self, buf: &[u8]) -> IoResult<()>;
fn peer_name(&mut self) -> IoResult<SocketAddr>;
fn control_congestion(&mut self) -> IoResult<()>;
fn nodelay(&mut self) -> IoResult<()>;
fn keepalive(&mut self, delay_in_seconds: uint) -> IoResult<()>;
fn letdie(&mut self) -> IoResult<()>;
fn clone(&self) -> Box<RtioTcpStream + Send>;
fn close_write(&mut self) -> IoResult<()>;
fn close_read(&mut self) -> IoResult<()>;
fn set_timeout(&mut self, timeout_ms: Option<u64>);
fn set_read_timeout(&mut self, timeout_ms: Option<u64>);
fn set_write_timeout(&mut self, timeout_ms: Option<u64>);
}
pub trait RtioSocket {
fn socket_name(&mut self) -> IoResult<SocketAddr>;
}
pub trait RtioUdpSocket : RtioSocket {
fn recvfrom(&mut self, buf: &mut [u8]) -> IoResult<(uint, SocketAddr)>;
fn sendto(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()>;
fn join_multicast(&mut self, multi: IpAddr) -> IoResult<()>;
fn leave_multicast(&mut self, multi: IpAddr) -> IoResult<()>;
fn loop_multicast_locally(&mut self) -> IoResult<()>;
fn dont_loop_multicast_locally(&mut self) -> IoResult<()>;
fn multicast_time_to_live(&mut self, ttl: int) -> IoResult<()>;
fn time_to_live(&mut self, ttl: int) -> IoResult<()>;
fn hear_broadcasts(&mut self) -> IoResult<()>;
fn ignore_broadcasts(&mut self) -> IoResult<()>;
fn clone(&self) -> Box<RtioUdpSocket + Send>;
fn set_timeout(&mut self, timeout_ms: Option<u64>);
fn set_read_timeout(&mut self, timeout_ms: Option<u64>);
fn set_write_timeout(&mut self, timeout_ms: Option<u64>);
}
pub trait RtioTimer {
fn sleep(&mut self, msecs: u64);
fn oneshot(&mut self, msecs: u64, cb: Box<Callback + Send>);
fn period(&mut self, msecs: u64, cb: Box<Callback + Send>);
}
pub trait RtioFileStream {
fn read(&mut self, buf: &mut [u8]) -> IoResult<int>;
fn write(&mut self, buf: &[u8]) -> IoResult<()>;
fn pread(&mut self, buf: &mut [u8], offset: u64) -> IoResult<int>;
fn pwrite(&mut self, buf: &[u8], offset: u64) -> IoResult<()>;
fn seek(&mut self, pos: i64, whence: SeekStyle) -> IoResult<u64>;
fn tell(&self) -> IoResult<u64>;
fn fsync(&mut self) -> IoResult<()>;
fn datasync(&mut self) -> IoResult<()>;
fn truncate(&mut self, offset: i64) -> IoResult<()>;
fn fstat(&mut self) -> IoResult<FileStat>;
}
pub trait RtioProcess {
fn id(&self) -> libc::pid_t;
fn kill(&mut self, signal: int) -> IoResult<()>;
fn wait(&mut self) -> IoResult<ProcessExit>;
fn set_timeout(&mut self, timeout: Option<u64>);
}
pub trait RtioPipe {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
fn write(&mut self, buf: &[u8]) -> IoResult<()>;
fn clone(&self) -> Box<RtioPipe + Send>;
fn close_write(&mut self) -> IoResult<()>;
fn close_read(&mut self) -> IoResult<()>;
fn set_timeout(&mut self, timeout_ms: Option<u64>);
fn set_read_timeout(&mut self, timeout_ms: Option<u64>);
fn set_write_timeout(&mut self, timeout_ms: Option<u64>);
}
pub trait RtioUnixListener {
fn listen(~self) -> IoResult<Box<RtioUnixAcceptor + Send>>;
}
pub trait RtioUnixAcceptor {
fn accept(&mut self) -> IoResult<Box<RtioPipe + Send>>;
fn set_timeout(&mut self, timeout: Option<u64>);
}
pub trait RtioTTY {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
fn write(&mut self, buf: &[u8]) -> IoResult<()>;
fn set_raw(&mut self, raw: bool) -> IoResult<()>;
fn get_winsize(&mut self) -> IoResult<(int, int)>;
fn isatty(&self) -> bool;
}
pub trait PausableIdleCallback {
fn pause(&mut self);
fn resume(&mut self);
}
pub trait RtioSignal {}
pub struct IoError {
pub code: uint,
pub extra: uint,
pub detail: Option<String>,
}
pub type IoResult<T> = Result<T, IoError>;
#[deriving(PartialEq, Eq)]
pub enum IpAddr {
Ipv4Addr(u8, u8, u8, u8),
Ipv6Addr(u16, u16, u16, u16, u16, u16, u16, u16),
}
impl fmt::Show for IpAddr {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Ipv4Addr(a, b, c, d) => write!(fmt, "{}.{}.{}.{}", a, b, c, d),
Ipv6Addr(a, b, c, d, e, f, g, h) => {
write!(fmt,
"{:04x}:{:04x}:{:04x}:{:04x}:{:04x}:{:04x}:{:04x}:{:04x}",
a, b, c, d, e, f, g, h)
}
}
}
}
#[deriving(PartialEq, Eq)]
pub struct SocketAddr {
pub ip: IpAddr,
pub port: u16,
}
pub enum StdioContainer {
Ignored,
InheritFd(i32),
CreatePipe(bool, bool),
}
pub enum ProcessExit {
ExitStatus(int),
ExitSignal(int),
}
pub enum FileMode {
Open,
Append,
Truncate,
}
pub enum FileAccess {
Read,
Write,
ReadWrite,
}
pub struct FileStat {
pub size: u64,
pub kind: u64,
pub perm: u64,
pub created: u64,
pub modified: u64,
pub accessed: u64,
pub device: u64,
pub inode: u64,
pub rdev: u64,
pub nlink: u64,
pub uid: u64,
pub gid: u64,
pub blksize: u64,
pub blocks: u64,
pub flags: u64,
pub gen: u64,
}
pub enum SeekStyle {
SeekSet,
SeekEnd,
SeekCur,
}
pub struct AddrinfoHint {
pub family: uint,
pub socktype: uint,
pub protocol: uint,
pub flags: uint,
}
pub struct AddrinfoInfo {
pub address: SocketAddr,
pub family: uint,
pub socktype: uint,
pub protocol: uint,
pub flags: uint,
}