Skip to content

Commit

Permalink
client: add exmaple of how to use custom transports
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVerevkin committed Sep 9, 2024
1 parent a719af2 commit adb1908
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions wayrs-client/examples/custom_transport.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//! An example of how to use your own custom transport implementation.
//! Here, the transport keeps track of how much bytes were sent/received.
use std::collections::VecDeque;
use std::env;
use std::io;
use std::os::fd::{OwnedFd, RawFd};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;

use wayrs_client::core::transport::Transport;
use wayrs_client::protocol::wl_registry;
use wayrs_client::{ConnectionBuilder, IoMode};

fn main() {
let mut conn = ConnectionBuilder::with_transport(MyTransport::connect()).build();

conn.add_registry_cb(|_conn, _state, event| match event {
wl_registry::Event::Global(g) => println!(
"global ({}) {} added",
g.name,
g.interface.to_string_lossy(),
),
wl_registry::Event::GlobalRemove(name) => println!("global ({name}) removed"),
_ => unreachable!(),
});

loop {
conn.flush(IoMode::Blocking).unwrap();
conn.recv_events(IoMode::Blocking).unwrap();
conn.dispatch_events(&mut ());

let t = conn.transport::<MyTransport>().unwrap();
eprintln!("up: {}b down: {}b", t.bytes_sent, t.bytes_read);
}
}

struct MyTransport {
socket: UnixStream,
bytes_read: usize,
bytes_sent: usize,
}

impl Transport for MyTransport {
fn pollable_fd(&self) -> RawFd {
self.socket.pollable_fd()
}

fn send(
&mut self,
bytes: &[std::io::IoSlice],
fds: &[OwnedFd],
mode: IoMode,
) -> io::Result<usize> {
let n = self.socket.send(bytes, fds, mode)?;
self.bytes_sent += n;
Ok(n)
}

fn recv(
&mut self,
bytes: &mut [std::io::IoSliceMut],
fds: &mut VecDeque<OwnedFd>,
mode: IoMode,
) -> io::Result<usize> {
let n = self.socket.recv(bytes, fds, mode)?;
self.bytes_read += n;
Ok(n)
}
}

impl MyTransport {
fn connect() -> Self {
let runtime_dir = env::var_os("XDG_RUNTIME_DIR").unwrap();
let wayland_disp = env::var_os("WAYLAND_DISPLAY").unwrap();

let mut path = PathBuf::new();
path.push(runtime_dir);
path.push(wayland_disp);

Self {
socket: UnixStream::connect(path).unwrap(),
bytes_read: 0,
bytes_sent: 0,
}
}
}

0 comments on commit adb1908

Please sign in to comment.