pub struct Peer<Transport: Transport> { /* private fields */ }
Expand description
Peer read/write loop.
This struct is used to run the read/write loop of the peer.
To send or receive requests and stream messages,
you need to use the PeerHandle
instead.
Implementations§
source§impl<Transport: Transport> Peer<Transport>
impl<Transport: Transport> Peer<Transport>
sourcepub fn new(transport: Transport) -> (Self, PeerHandle<Transport::Body>)
pub fn new(transport: Transport) -> (Self, PeerHandle<Transport::Body>)
Create a new peer and a handle to it.
The Peer
itself is used to run the read/write loop.
The returned PeerHandle
is used to send and receive requests and stream messages.
If Self::run()
is not called (or aborted),
none of the functions of the PeerHandle
will work.
They will just wait forever.
You can also use Self::spawn()
to run the read/write loop in a newly spawned task,
and only get a PeerHandle
.
You should only use Self::spawn()
if you do not need full control over the execution of the read/write loop.
sourcepub fn spawn(transport: Transport) -> PeerHandle<Transport::Body>
pub fn spawn(transport: Transport) -> PeerHandle<Transport::Body>
Spawn a peer in a new task, and get a handle to the peer.
The spawned task will immediately be detached. It can not be joined.
The returned PeerHandle
can be used to send and receive requests and stream messages.
If you need more control of the execution of the peer read/write loop,
you should use Self::new()
instead.
sourcepub async fn connect<'a, Address>(
address: Address,
config: Transport::Config
) -> Result<(PeerHandle<Transport::Body>, Transport::Info)>where
Address: 'a,
Transport: Connect<'a, Address>,
pub async fn connect<'a, Address>(
address: Address,
config: Transport::Config
) -> Result<(PeerHandle<Transport::Body>, Transport::Info)>where
Address: 'a,
Transport: Connect<'a, Address>,
Connect to a remote server.
Similar to Self::spawn()
, this spawns a background task for the peer.
The returned PeerHandle
can be used to send and receive requests and stream messages.
The type of address accepted depends on the transport.
For internet transports such as TCP, the address must implement tokio::net::ToSocketAddrs
.
For unix transports, the address must implement AsRef<std::path::Path>
.
sourcepub fn transport(&self) -> &Transport
pub fn transport(&self) -> &Transport
Get direct access to the underlying transport.
sourcepub fn transport_mut(&mut self) -> &mut Transport
pub fn transport_mut(&mut self) -> &mut Transport
Get direct mutable access to the underlying transport.