pub struct UnixStream { /* private fields */ }
Expand description
A raw Unix byte stream connected to a file.
Implementations§
Source§impl UnixStream
impl UnixStream
Sourcepub fn connect<P: AsRef<Path>>(path: P, cx: &MainContext) -> UnixStreamConnect
pub fn connect<P: AsRef<Path>>(path: P, cx: &MainContext) -> UnixStreamConnect
Creates a new TCP stream that will connect to the specified address.
The returned TCP stream will be associated with the provided context. A future is returned representing the connected TCP stream.
Examples found in repository?
57fn main() {
58 futures_glib::init();
59
60 let path = "/tmp/named.socket";
61
62 let cx = MainContext::default(|cx| cx.clone());
63 let lp = MainLoop::new(None);
64 let ex = Executor::new();
65 ex.attach(&cx);
66
67 let remote = ex.remote();
68
69 let (sender, receiver) = channel();
70
71 let inner_path = path.clone();
72 thread::spawn(move || {
73 remote.spawn(move |ex: Executor| {
74 let cx = MainContext::default(|cx| cx.clone());
75 remove_file(inner_path).ok();
76 let listener = UnixListener::bind(inner_path, &cx).unwrap();
77
78 sender.send(true);
79
80 let inner_ex = ex.clone();
81 let incoming = listener.incoming()
82 .for_each(move |(stream, _addr)| {
83 let frame = stream.framed(LineCodec);
84
85 inner_ex.spawn(frame.for_each(|value| {
86 println!("Received: {:?}", value);
87 Ok(())
88 }).map_err(|_| ()));
89 Ok(())
90 })
91 .map_err(|_| ());
92
93 ex.spawn(incoming);
94 Ok(())
95 });
96 });
97
98 let send = receiver.then(move |_| {
99 UnixStream::connect(path, &cx)
100 })
101 .and_then(|stream2| {
102 Ok(stream2.framed(LineCodec))
103 })
104 .and_then(|frame2| {
105 frame2.send("Hello".to_string())
106 });
107 // TODO: send too early? Should accept before?
108 ex.spawn(send.map(|_| ())
109 .map_err(|_| ()));
110
111 lp.run();
112 ex.destroy();
113}
More examples
56fn main() {
57 futures_glib::init();
58
59 let path = "/tmp/named.socket";
60
61 let cx = MainContext::default(|cx| cx.clone());
62 let lp = MainLoop::new(None);
63 let ex = Executor::new();
64 ex.attach(&cx);
65
66 remove_file(path).ok();
67 let listener = UnixListener::bind(path, &cx).unwrap();
68
69 let remote = ex.remote();
70
71 let inner_ex = ex.clone();
72 let incoming = listener.incoming()
73 .for_each(move |(stream, _addr)| {
74 let (reader, writer) = stream.split();
75 let mut framed_writer = FramedWrite::new(writer, LineCodec);
76 let framed_reader = FramedRead::new(reader, LineCodec);
77
78 inner_ex.spawn(framed_reader.and_then(|value| {
79 println!("Received: {:?}", value);
80 Ok(())
81 }).for_each(move |_| {
82 if let Ok(AsyncSink::Ready) = framed_writer.start_send("Received".to_string()) {
83 framed_writer.poll_complete().unwrap();
84 }
85 Ok(())
86 }).map_err(|_| ()));
87 Ok(())
88 })
89 .map_err(|_| ());
90
91 ex.spawn(incoming);
92
93 thread::spawn(move || {
94 remote.spawn(move |ex: Executor| {
95 let connection = UnixStream::connect(path, &cx);
96 let exe = ex.clone();
97 let future = connection.and_then(move |stream| {
98 let (reader, writer) = stream.split();
99 let framed_writer = FramedWrite::new(writer, LineCodec);
100 let framed_reader = FramedRead::new(reader, LineCodec);
101 exe.spawn(framed_reader.for_each(|value| {
102 println!("Thread received: {:?}", value);
103 Ok(())
104 }).map_err(|_| ()));
105 exe.spawn(framed_writer.send("Hello".to_string())
106 .map(|_| ())
107 .map_err(|_| ()));
108 Ok(())
109 })
110 .map_err(|_| ());
111 ex.spawn(future);
112 Ok(())
113 });
114 });
115
116 lp.run();
117 ex.destroy();
118}
Sourcepub fn connect_abstract(
name: &[u8],
cx: &MainContext,
) -> Result<UnixStreamConnect>
pub fn connect_abstract( name: &[u8], cx: &MainContext, ) -> Result<UnixStreamConnect>
Examples found in repository?
55fn main() {
56 futures_glib::init();
57
58 let path = b"named-socket";
59
60 let cx = MainContext::default(|cx| cx.clone());
61 let lp = MainLoop::new(None);
62 let ex = Executor::new();
63 ex.attach(&cx);
64
65 let listener = UnixListener::bind_abstract(path, &cx).unwrap();
66
67 let remote = ex.remote();
68
69 let inner_ex = ex.clone();
70 let incoming = listener.incoming()
71 .for_each(move |(stream, _addr)| {
72 let (reader, writer) = stream.split();
73 let mut framed_writer = FramedWrite::new(writer, LineCodec);
74 let framed_reader = FramedRead::new(reader, LineCodec);
75
76 inner_ex.spawn(framed_reader.and_then(|value| {
77 println!("Received: {:?}", value);
78 Ok(())
79 }).for_each(move |_| {
80 if let Ok(AsyncSink::Ready) = framed_writer.start_send("Received".to_string()) {
81 framed_writer.poll_complete().unwrap();
82 }
83 Ok(())
84 }).map_err(|_| ()));
85 Ok(())
86 })
87 .map_err(|_| ());
88
89 ex.spawn(incoming);
90
91 thread::spawn(move || {
92 remote.spawn(move |ex: Executor| {
93 let connection = UnixStream::connect_abstract(path, &cx).unwrap();
94 let exe = ex.clone();
95 let future = connection.and_then(move |stream| {
96 let (reader, writer) = stream.split();
97 let framed_writer = FramedWrite::new(writer, LineCodec);
98 let framed_reader = FramedRead::new(reader, LineCodec);
99 exe.spawn(framed_reader.for_each(|value| {
100 println!("Thread received: {:?}", value);
101 Ok(())
102 }).map_err(|_| ()));
103 exe.spawn(framed_writer.send("Hello".to_string())
104 .map(|_| ())
105 .map_err(|_| ()));
106 Ok(())
107 })
108 .map_err(|_| ());
109 ex.spawn(future);
110 Ok(())
111 });
112 });
113
114 lp.run();
115 ex.destroy();
116}
pub unsafe fn from_fd(fd: RawFd, cx: &MainContext) -> UnixStreamConnect
Sourcepub fn poll_read(&self) -> Async<()>
pub fn poll_read(&self) -> Async<()>
Test whether this socket is ready to be read or not.
If the socket is not readable then the current task is scheduled to
get a notification when the socket does become readable. That is, this
is only suitable for calling in a Future::poll
method and will
automatically handle ensuring a retry once the socket is readable again.
Sourcepub fn poll_write(&self) -> Async<()>
pub fn poll_write(&self) -> Async<()>
Tests to see if this source is ready to be written to or not.
If this stream is not ready for a write then NotReady
will be returned
and the current task will be scheduled to receive a notification when
the stream is writable again. In other words, this method is only safe
to call from within the context of a future’s task, typically done in a
Future::poll
method.
Trait Implementations§
Source§impl AsRawFd for UnixStream
impl AsRawFd for UnixStream
Source§impl AsyncRead for UnixStream
impl AsyncRead for UnixStream
Source§unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool
read
. Returns
true
if the supplied buffer was zeroed out. Read moreSource§fn read_buf<B: BufMut>(&mut self, buf: &mut B) -> Poll<usize, Error>
fn read_buf<B: BufMut>(&mut self, buf: &mut B) -> Poll<usize, Error>
BufMut
, returning
how many bytes were read. Read moreSource§fn framed<T>(self, codec: T) -> Framed<Self, T>where
T: Encoder + Decoder,
Self: Sized + AsyncWrite,
fn framed<T>(self, codec: T) -> Framed<Self, T>where
T: Encoder + Decoder,
Self: Sized + AsyncWrite,
Stream
and Sink
interface for reading and writing to this
I/O object, using Decode
and Encode
to read and write the raw data. Read moreSource§impl AsyncWrite for UnixStream
impl AsyncWrite for UnixStream
Source§fn shutdown(&mut self) -> Poll<(), Error>
fn shutdown(&mut self) -> Poll<(), Error>
Source§fn write_buf<B: Buf>(&mut self, buf: &mut B) -> Poll<usize, Error>
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> Poll<usize, Error>
Buf
into this value, returning how many bytes were written. Read moreSource§impl<'a> Read for &'a UnixStream
impl<'a> Read for &'a UnixStream
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl Read for UnixStream
impl Read for UnixStream
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl<'a> Write for &'a UnixStream
impl<'a> Write for &'a UnixStream
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Source§impl Write for UnixStream
impl Write for UnixStream
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Auto Trait Implementations§
impl Freeze for UnixStream
impl !RefUnwindSafe for UnixStream
impl !Send for UnixStream
impl !Sync for UnixStream
impl Unpin for UnixStream
impl !UnwindSafe for UnixStream
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<R> ReadBytesExt for R
impl<R> ReadBytesExt for R
Source§fn read_u8(&mut self) -> Result<u8, Error>
fn read_u8(&mut self) -> Result<u8, Error>
Source§fn read_i8(&mut self) -> Result<i8, Error>
fn read_i8(&mut self) -> Result<i8, Error>
Source§fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
Source§fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
Source§fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
Source§fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
Source§fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
Source§fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
Source§fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
Source§fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
Source§fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
Source§fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
Source§fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
Source§fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
Source§fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
Source§fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
read_f32_into
instead