Skip to content

Commit

Permalink
L2CAP: Allow sockets to assume to be bound/connected
Browse files Browse the repository at this point in the history
  • Loading branch information
gh2o committed Jan 17, 2022
1 parent a7647eb commit f1b4054
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions bluer/src/l2cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,22 @@ impl Socket<Stream> {
/// Establish a stream connection with a peer at the specified socket address.
pub async fn connect(self, sa: SocketAddr) -> Result<Stream> {
self.connect_priv(sa).await?;
Ok(Stream::from_socket(self))
}

/// Assume that `bind()` has already been successfully called on this socket, and return a
/// [StreamListener].
///
/// Only use this function if the socket was created with [Socket::from_raw_fd].
pub fn assume_bound(self) -> StreamListener {
StreamListener { socket: self }
}

/// Assume that `connect()` has already successfully been called on this socket, and return a
/// [Stream].
///
/// Only use this function if the socket was created with [Socket::from_raw_fd].
pub fn assume_connected(self) -> Stream {
Stream::from_socket(self)
}
}
Expand Down Expand Up @@ -509,6 +525,22 @@ impl Socket<SeqPacket> {
self.connect_priv(sa).await?;
Ok(SeqPacket { socket: self })
}

/// Assume that `bind()` has already been successfully called on this socket, and return a
/// [SeqPacketListener].
///
/// Only use this function if the socket was created with [Socket::from_raw_fd].
pub fn assume_bound(self) -> SeqPacketListener {
SeqPacketListener { socket: self }
}

/// Assume that `connect()` has already successfully been called on this socket, and return a
/// [SeqPacket].
///
/// Only use this function if the socket was created with [Socket::from_raw_fd].
pub fn assume_connected(self) -> SeqPacket {
SeqPacket { socket: self }
}
}

impl Socket<Datagram> {
Expand Down Expand Up @@ -543,13 +575,13 @@ impl StreamListener {
/// Accepts a new incoming connection from this listener.
pub async fn accept(&self) -> Result<(Stream, SocketAddr)> {
let (socket, sa) = self.socket.accept_priv().await?;
Ok((Stream::from_socket(socket)?, sa))
Ok((Stream::from_socket(socket), sa))
}

/// Polls to accept a new incoming connection to this listener.
pub fn poll_accept(&self, cx: &mut Context) -> Poll<Result<(Stream, SocketAddr)>> {
let (socket, sa) = ready!(self.socket.poll_accept_priv(cx))?;
Poll::Ready(Ok((Stream::from_socket(socket)?, sa)))
Poll::Ready(Ok((Stream::from_socket(socket), sa)))
}
}

Expand All @@ -574,8 +606,8 @@ pub struct Stream {

impl Stream {
/// Create Stream from Socket.
fn from_socket(socket: Socket<Stream>) -> Result<Self> {
Ok(Self { socket, send_mtu: 0.into() })
fn from_socket(socket: Socket<Stream>) -> Self {
Self { socket, send_mtu: 0.into() }
}

/// Establish a stream connection with a peer at the specified socket address.
Expand Down

0 comments on commit f1b4054

Please sign in to comment.