pub struct Connection<B> { /* private fields */ }
Expand description
Manages the state of a vsock-over-usb connection and the sockets over which data is being transmitted for them.
This implementation aims to be agnostic to both the underlying transport and the buffers used
to read and write from it. The buffer type must conform to PacketBuffer
, which is essentially
a type that holds a mutable slice of bytes and is Send
and Unpin
-able.
The client of this library will:
- Use methods on this struct to initiate actions like connecting and accepting connections to the other end.
- Provide buffers to be filled and sent to the other end with
Connection::fill_usb_packet
. - Pump usb packets received into it using
Connection::handle_vsock_packet
.
Implementations§
Source§impl<B: PacketBuffer> Connection<B>
impl<B: PacketBuffer> Connection<B>
Sourcepub fn new(
control_socket: Socket,
incoming_requests_tx: Sender<ConnectionRequest>,
) -> Self
pub fn new( control_socket: Socket, incoming_requests_tx: Sender<ConnectionRequest>, ) -> Self
Creates a new connection with:
- a
control_socket
, over which data addressed to and from cid 0, port 0 (a control channel between host and device) can be read and written from. - An
incoming_requests_tx
that is the sender half of a request queue for incoming connection requests from the other side.
Sourcepub async fn send_empty_echo(&self)
pub async fn send_empty_echo(&self)
Sends an echo packet to the remote end that you don’t care about the reply, so it doesn’t have a distinct target address or payload.
Sourcepub async fn connect(
&self,
addr: Address,
socket: Socket,
) -> Result<ConnectionState, Error>
pub async fn connect( &self, addr: Address, socket: Socket, ) -> Result<ConnectionState, Error>
Starts a connection attempt to the other end of the USB connection, and provides a socket
to read and write from. The function will complete when the other end has accepted or
rejected the connection, and the returned ConnectionState
handle can be used to wait
for the connection to be closed.
Sourcepub async fn close(&self, address: &Address)
pub async fn close(&self, address: &Address)
Sends a request for the other end to close the connection.
Sourcepub async fn reset(&self, address: &Address) -> Result<(), Error>
pub async fn reset(&self, address: &Address) -> Result<(), Error>
Resets the named connection without going through a close request.
Sourcepub async fn accept(
&self,
request: ConnectionRequest,
socket: Socket,
) -> Result<ConnectionState, Error>
pub async fn accept( &self, request: ConnectionRequest, socket: Socket, ) -> Result<ConnectionState, Error>
Accepts a connection for which an outstanding connection request has been made, and
provides a socket to read and write data packets to and from. The returned ConnectionState
can be used to wait for the connection to be closed.
Sourcepub async fn reject(&self, request: ConnectionRequest) -> Result<(), Error>
pub async fn reject(&self, request: ConnectionRequest) -> Result<(), Error>
Rejects a pending connection request from the other side.
Sourcepub async fn handle_vsock_packet(&self, packet: Packet<'_>) -> Result<(), Error>
pub async fn handle_vsock_packet(&self, packet: Packet<'_>) -> Result<(), Error>
Dispatches the given vsock packet type and handles its effect on any outstanding connections or the overall state of the connection.
Sourcepub async fn fill_usb_packet(
&self,
builder: UsbPacketBuilder<B>,
) -> UsbPacketBuilder<B>
pub async fn fill_usb_packet( &self, builder: UsbPacketBuilder<B>, ) -> UsbPacketBuilder<B>
Provides a packet builder for the state machine to write packets to. Returns a future that will be fulfilled when there is data available to send on the packet.
§Panics
Panics if called while another Self::fill_usb_packet
future is pending.