pub struct Listener { /* private fields */ }
rfcomm
only.Expand description
An RFCOMM socket server, listening for Stream connections.
Implementations§
source§impl Listener
impl Listener
sourcepub async fn bind(sa: SocketAddr) -> Result<Self>
pub async fn bind(sa: SocketAddr) -> Result<Self>
Creates a new Listener, which will be bound to the specified socket address.
Specify SocketAddr::any for any local adapter address with a dynamically allocated channel.
This will not register an SDP record for this channel. Register a Bluetooth RFCOMM profile instead, if you need a service record.
Examples found in repository?
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
async fn main() -> bluer::Result<()> {
env_logger::init();
let session = bluer::Session::new().await?;
let adapter = session.default_adapter().await?;
adapter.set_powered(true).await?;
adapter.set_discoverable(true).await?;
let adapter_addr = adapter.address().await?;
let local_sa = SocketAddr::new(adapter_addr, CHANNEL);
let listener = Listener::bind(local_sa).await?;
println!(
"Listening on {} channel {}. Press enter to quit.",
listener.as_ref().local_addr()?.addr,
listener.as_ref().local_addr()?.channel
);
let stdin = BufReader::new(tokio::io::stdin());
let mut lines = stdin.lines();
loop {
println!("\nWaiting for connection...");
let (mut stream, sa) = tokio::select! {
l = listener.accept() => {
match l {
Ok(v) => v,
Err(err) => {
println!("Accepting connection failed: {}", &err);
continue;
}}
},
_ = lines.next_line() => break,
};
println!("Accepted connection from {:?}", &sa);
println!("Sending hello");
if let Err(err) = stream.write_all(HELLO_MSG).await {
println!("Write failed: {}", &err);
continue;
}
loop {
let buf_size = 1024;
let mut buf = vec![0; buf_size as _];
let n = match stream.read(&mut buf).await {
Ok(0) => {
println!("Stream ended");
break;
}
Ok(n) => n,
Err(err) => {
println!("Read failed: {}", &err);
break;
}
};
let buf = &buf[..n];
println!("Echoing {} bytes", buf.len());
if let Err(err) = stream.write_all(buf).await {
println!("Write failed: {}", &err);
continue;
}
}
}
Ok(())
}
sourcepub async fn accept(&self) -> Result<(Stream, SocketAddr)>
pub async fn accept(&self) -> Result<(Stream, SocketAddr)>
Accepts a new incoming connection from this listener.
Examples found in repository?
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
async fn main() -> bluer::Result<()> {
env_logger::init();
let session = bluer::Session::new().await?;
let adapter = session.default_adapter().await?;
adapter.set_powered(true).await?;
adapter.set_discoverable(true).await?;
let adapter_addr = adapter.address().await?;
let local_sa = SocketAddr::new(adapter_addr, CHANNEL);
let listener = Listener::bind(local_sa).await?;
println!(
"Listening on {} channel {}. Press enter to quit.",
listener.as_ref().local_addr()?.addr,
listener.as_ref().local_addr()?.channel
);
let stdin = BufReader::new(tokio::io::stdin());
let mut lines = stdin.lines();
loop {
println!("\nWaiting for connection...");
let (mut stream, sa) = tokio::select! {
l = listener.accept() => {
match l {
Ok(v) => v,
Err(err) => {
println!("Accepting connection failed: {}", &err);
continue;
}}
},
_ = lines.next_line() => break,
};
println!("Accepted connection from {:?}", &sa);
println!("Sending hello");
if let Err(err) = stream.write_all(HELLO_MSG).await {
println!("Write failed: {}", &err);
continue;
}
loop {
let buf_size = 1024;
let mut buf = vec![0; buf_size as _];
let n = match stream.read(&mut buf).await {
Ok(0) => {
println!("Stream ended");
break;
}
Ok(n) => n,
Err(err) => {
println!("Read failed: {}", &err);
break;
}
};
let buf = &buf[..n];
println!("Echoing {} bytes", buf.len());
if let Err(err) = stream.write_all(buf).await {
println!("Write failed: {}", &err);
continue;
}
}
}
Ok(())
}
sourcepub fn poll_accept(
&self,
cx: &mut Context<'_>
) -> Poll<Result<(Stream, SocketAddr)>>
pub fn poll_accept( &self, cx: &mut Context<'_> ) -> Poll<Result<(Stream, SocketAddr)>>
Polls to accept a new incoming connection to this listener.
sourcepub unsafe fn from_raw_fd(fd: RawFd) -> Result<Self>
pub unsafe fn from_raw_fd(fd: RawFd) -> Result<Self>
Constructs a new Listener from the given raw file descriptor.
The file descriptor must have been set to non-blocking mode.
This function consumes ownership of the specified file descriptor. The returned object will take responsibility for closing it when the object goes out of scope.
§Safety
If the passed file descriptor is invalid, undefined behavior may occur.
Trait Implementations§
source§impl FromRawFd for Listener
impl FromRawFd for Listener
source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
Constructs a new instance of Self
from the given raw file
descriptor.
The file descriptor must have been set to non-blocking mode.
§Panics
Panics when the conversion fails. Use Listener::from_raw_fd for a non-panicking variant.