Skip to content

Commit

Permalink
feat(api): create new try_send_message API and change send_message
Browse files Browse the repository at this point in the history
functionality
  • Loading branch information
lionel-faber authored and joshuef committed May 25, 2021
1 parent 9c64e75 commit 1264246
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,23 @@ impl Endpoint {

/// 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 async fn send_message(&self, msg: Bytes, dest: &SocketAddr) -> Result<()> {
pub async fn try_send_message(&self, msg: Bytes, dest: &SocketAddr) -> Result<()> {
let connection = self.get_connection(dest).ok_or(Error::MissingConnection)?;
connection.send_uni(msg).await?;
Ok(())
}

/// Sends a message to a peer. This will attempt to use an existing connection
/// to the peer first. If this connection is broken or doesn't exist
/// a new connection is created and the message is sent.
pub async fn send_message(&self, msg: Bytes, dest: &SocketAddr) -> Result<()> {
if self.try_send_message(msg.clone(), dest).await.is_ok() {
return Ok(());
}
self.connect_to(dest).await?;
self.try_send_message(msg, dest).await
}

/// Close all the connections of this endpoint immediately and stop accepting new connections.
pub fn close(&self) {
self.quic_endpoint.close(0_u32.into(), b"")
Expand Down

0 comments on commit 1264246

Please sign in to comment.