(index<- ) ./libstd/rt/uv/pipe.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use prelude::*;
12 use libc;
13
14 use rt::uv;
15 use rt::uv::net;
16 use rt::uv::uvll;
17
18 pub struct Pipe(*uvll::uv_pipe_t);
19
20 impl uv::Watcher for Pipe {}
21
22 impl Pipe {
23 pub fn new(loop_: &uv::Loop, ipc: bool) -> Pipe {
24 unsafe {
25 let handle = uvll::malloc_handle(uvll::UV_NAMED_PIPE);
26 assert!(handle.is_not_null());
27 let ipc = ipc as libc::c_int;
28 assert_eq!(uvll::pipe_init(loop_.native_handle(), handle, ipc), 0);
29 let mut ret: Pipe =
30 uv::NativeHandle::from_native_handle(handle);
31 ret.install_watcher_data();
32 ret
33 }
34 }
35
36 pub fn as_stream(&self) -> net::StreamWatcher {
37 net::StreamWatcher(**self as *uvll::uv_stream_t)
38 }
39
40 pub fn close(self, cb: uv::NullCallback) {
41 {
42 let mut this = self;
43 let data = this.get_watcher_data();
44 assert!(data.close_cb.is_none());
45 data.close_cb = Some(cb);
46 }
47
48 unsafe { uvll::close(self.native_handle(), close_cb); }
49
50 extern fn close_cb(handle: *uvll::uv_pipe_t) {
51 let mut process: Pipe = uv::NativeHandle::from_native_handle(handle);
52 process.get_watcher_data().close_cb.take_unwrap()();
53 process.drop_watcher_data();
54 unsafe { uvll::free_handle(handle as *libc::c_void) }
55 }
56 }
57 }
58
59 impl uv::NativeHandle<*uvll::uv_pipe_t> for Pipe {
60 fn from_native_handle(handle: *uvll::uv_pipe_t) -> Pipe {
61 Pipe(handle)
62 }
63 fn native_handle(&self) -> *uvll::uv_pipe_t {
64 match self { &Pipe(ptr) => ptr }
65 }
66 }