Struct qp2p::Endpoint [−][src]
Endpoint instance which can be used to create connections to peers, and listen to incoming messages from other peers.
Implementations
impl Endpoint
[src]
pub fn local_addr(&self) -> SocketAddr
[src]
Endpoint local address
pub fn socket_addr(&self) -> SocketAddr
[src]
Returns the socket address of the endpoint
pub fn disconnect_from(&mut self, peer_addr: &SocketAddr) -> Result<()>
[src]
Removes all existing connections to a given peer
pub async fn connect_to(&self, node_addr: &SocketAddr) -> Result<()>
[src]
Connects to another peer.
Returns Connection
which is a handle for sending messages to the peer and
IncomingMessages
which is a stream of messages received from the peer.
The incoming messages stream might be None
. See the next section for more info.
Connection pooling
Connection are stored in an internal pool and reused if possible. A connection remains in
the pool while its IncomingMessages
instances exists and while the connection is open.
When a new connection is established, this function returns both the Connection
instance
and the IncomingMessages
stream. If an existing connection is retrieved from the pool,
the incoming messages will be None
. Multiple Connection
instances can exists
simultaneously and they all share the same underlying connection resource. On the other
hand, at most one IncomingMessages
stream can exist per peer.
How to handle the IncomingMessages
depends on the networking model of the application:
In the peer-to-peer model, where peers can arbitrarily send and receive messages to/from
other peers, it is recommended to keep the IncomingMessages
around and listen on it for
new messages by repeatedly calling next
and only drop it when it returns None
.
On the other hand, there is no need to keep Connection
around as it can be cheaply
retrieved again when needed by calling connect_to
. When the connection gets closed by the
peer or it timeouts due to inactivity, the incoming messages stream gets closed and once
it’s dropped the connection gets removed from the pool automatically. Calling connect_to
afterwards will open a new connection.
In the client-server model, where only the client send requests to the server and then
listens for responses and never the other way around, it’s OK to ignore (drop) the incoming
messages stream and only use bi-directional streams obtained by calling
Connection::open_bi
. In this case the connection won’t be pooled and the application is
responsible for caching it.
When sending a message on Connection
fails, the connection is also automatically removed
from the pool and the subsequent call to connect_to
is guaranteed to reopen new connection
too.
pub async fn open_bidirectional_stream(
&self,
peer_addr: &SocketAddr
) -> Result<(SendStream, RecvStream)>
[src]
&self,
peer_addr: &SocketAddr
) -> Result<(SendStream, RecvStream)>
Open a bi-directional peer with a given peer
pub async fn send_message(&self, msg: Bytes, dest: &SocketAddr) -> Result<()>
[src]
Sends a message to a peer. This will attempt to use an existing connection
to the destination peer. If a connection does not exist, this will fail with Error::MissingConnection
pub fn close(&self)
[src]
Close all the connections of this endpoint immediately and stop accepting new connections.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Endpoint
impl Send for Endpoint
impl Sync for Endpoint
impl Unpin for Endpoint
impl !UnwindSafe for Endpoint
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,