pub struct BufferedSocket { /* private fields */ }
Expand description
An adapter around a raw Socket that directly handles buffering and conversion from/to wayland messages
Implementations§
Source§impl BufferedSocket
impl BufferedSocket
Sourcepub fn new(socket: Socket) -> BufferedSocket
pub fn new(socket: Socket) -> BufferedSocket
Wrap a Socket into a Buffered Socket
Examples found in repository?
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
fn main() {
let xdg_dir = env::var_os("XDG_RUNTIME_DIR").unwrap();
let mut path: PathBuf = xdg_dir.into();
path.push("wayland-0");
let socket = UnixStream::connect(path).unwrap();
let mut socket = BufferedSocket::new(unsafe { Socket::from_raw_fd(socket.into_raw_fd()) });
socket
.write_message(&Message {
sender_id: 1, // wl_display
opcode: 1, // get registry
args: smallvec![
Argument::NewId(2), // id of the created registry
],
})
.unwrap();
socket.flush().unwrap();
::std::thread::sleep(::std::time::Duration::from_millis(500)); // sleep 0.5 seconds
let ret = socket.read_messages(
|id, opcode| match (id, opcode) {
(2, 0) => Some(&GLOBAL_EVENT.signature),
_ => None,
},
|msg| {
println!("{:?}", msg);
true
},
);
println!("{:?}", ret);
}
Sourcepub fn get_socket(&mut self) -> &mut Socket
pub fn get_socket(&mut self) -> &mut Socket
Get direct access to the underlying socket
Sourcepub fn into_socket(self) -> Socket
pub fn into_socket(self) -> Socket
Retrieve ownership of the underlying Socket
Any leftover content in the internal buffers will be lost
Sourcepub fn flush(&mut self) -> NixResult<()>
pub fn flush(&mut self) -> NixResult<()>
Flush the contents of the outgoing buffer into the socket
Examples found in repository?
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
fn main() {
let xdg_dir = env::var_os("XDG_RUNTIME_DIR").unwrap();
let mut path: PathBuf = xdg_dir.into();
path.push("wayland-0");
let socket = UnixStream::connect(path).unwrap();
let mut socket = BufferedSocket::new(unsafe { Socket::from_raw_fd(socket.into_raw_fd()) });
socket
.write_message(&Message {
sender_id: 1, // wl_display
opcode: 1, // get registry
args: smallvec![
Argument::NewId(2), // id of the created registry
],
})
.unwrap();
socket.flush().unwrap();
::std::thread::sleep(::std::time::Duration::from_millis(500)); // sleep 0.5 seconds
let ret = socket.read_messages(
|id, opcode| match (id, opcode) {
(2, 0) => Some(&GLOBAL_EVENT.signature),
_ => None,
},
|msg| {
println!("{:?}", msg);
true
},
);
println!("{:?}", ret);
}
Sourcepub fn write_message(&mut self, msg: &Message) -> NixResult<()>
pub fn write_message(&mut self, msg: &Message) -> NixResult<()>
Write a message to the outgoing buffer
This method may flush the internal buffer if necessary (if it is full).
If the message is too big to fit in the buffer, the error Error::Sys(E2BIG)
will be returned.
Examples found in repository?
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
fn main() {
let xdg_dir = env::var_os("XDG_RUNTIME_DIR").unwrap();
let mut path: PathBuf = xdg_dir.into();
path.push("wayland-0");
let socket = UnixStream::connect(path).unwrap();
let mut socket = BufferedSocket::new(unsafe { Socket::from_raw_fd(socket.into_raw_fd()) });
socket
.write_message(&Message {
sender_id: 1, // wl_display
opcode: 1, // get registry
args: smallvec![
Argument::NewId(2), // id of the created registry
],
})
.unwrap();
socket.flush().unwrap();
::std::thread::sleep(::std::time::Duration::from_millis(500)); // sleep 0.5 seconds
let ret = socket.read_messages(
|id, opcode| match (id, opcode) {
(2, 0) => Some(&GLOBAL_EVENT.signature),
_ => None,
},
|msg| {
println!("{:?}", msg);
true
},
);
println!("{:?}", ret);
}
Sourcepub fn fill_incoming_buffers(&mut self) -> NixResult<()>
pub fn fill_incoming_buffers(&mut self) -> NixResult<()>
Try to fill the incoming buffers of this socket, to prepare a new round of parsing.
Sourcepub fn read_one_message<F>(
&mut self,
signature: F,
) -> Result<Message, MessageParseError>
pub fn read_one_message<F>( &mut self, signature: F, ) -> Result<Message, MessageParseError>
Read and deserialize a single message from the incoming buffers socket
This method requires one closure that given an object id and an opcode,
must provide the signature of the associated request/event, in the form of
a &'static [ArgumentType]
. If it returns None
, meaning that
the couple object/opcode does not exist, an error will be returned.
There are 3 possibilities of return value:
Ok(Ok(msg))
: no error occurred, this is the messageOk(Err(e))
: either a malformed message was encountered or we need more data, in the latter case you need to try callingfill_incoming_buffers()
.Err(e)
: an I/O error occurred reading from the socked, details are ine
(this can be a “wouldblock” error, which just means that no message is available to read)
Sourcepub fn read_messages<F1, F2>(
&mut self,
signature: F1,
callback: F2,
) -> NixResult<Result<usize, MessageParseError>>
pub fn read_messages<F1, F2>( &mut self, signature: F1, callback: F2, ) -> NixResult<Result<usize, MessageParseError>>
Read and deserialize messages from the socket
This method requires two closures:
- The first one, given an object id and an opcode, must provide
the signature of the associated request/event, in the form of
a
&'static [ArgumentType]
. If it returnsNone
, meaning that the couple object/opcode does not exist, the parsing will be prematurely interrupted and this method will return aMessageParseError::Malformed
error. - The second closure is charged to process the parsed message. If it
returns
false
, the iteration will be prematurely stopped.
In both cases of early stopping, the remaining unused data will be left in the buffers, and will start to be processed at the next call of this method.
There are 3 possibilities of return value:
Ok(Ok(n))
: no error occurred,n
messages where processedOk(Err(MessageParseError::Malformed))
: a malformed message was encountered (this is a protocol error and is supposed to be fatal to the connection).Err(e)
: an I/O error occurred reading from the socked, details are ine
(this can be a “wouldblock” error, which just means that no message is available to read)
Examples found in repository?
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
fn main() {
let xdg_dir = env::var_os("XDG_RUNTIME_DIR").unwrap();
let mut path: PathBuf = xdg_dir.into();
path.push("wayland-0");
let socket = UnixStream::connect(path).unwrap();
let mut socket = BufferedSocket::new(unsafe { Socket::from_raw_fd(socket.into_raw_fd()) });
socket
.write_message(&Message {
sender_id: 1, // wl_display
opcode: 1, // get registry
args: smallvec![
Argument::NewId(2), // id of the created registry
],
})
.unwrap();
socket.flush().unwrap();
::std::thread::sleep(::std::time::Duration::from_millis(500)); // sleep 0.5 seconds
let ret = socket.read_messages(
|id, opcode| match (id, opcode) {
(2, 0) => Some(&GLOBAL_EVENT.signature),
_ => None,
},
|msg| {
println!("{:?}", msg);
true
},
);
println!("{:?}", ret);
}