Skip to content

Commit

Permalink
Impl From tungs_wams::Error for ConnSendError
Browse files Browse the repository at this point in the history
  • Loading branch information
flub committed Jan 3, 2025
1 parent b5dd41f commit 8c0810c
Showing 1 changed file with 16 additions and 33 deletions.
49 changes: 16 additions & 33 deletions iroh-relay/src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ pub enum ConnSendError {
Protocol(&'static str),
}

impl From<tokio_tungstenite_wasm::Error> for ConnSendError {
fn from(source: tokio_tungstenite_wasm::Error) -> Self {
let io_err = match source {
tokio_tungstenite_wasm::Error::Io(io_err) => io_err,
_ => std::io::Error::new(std::io::ErrorKind::Other, source.to_string()),
};
Self::Io(io_err)
}
}

/// A connection to a relay server.
///
/// This holds a connection to a relay server. It is:
Expand Down Expand Up @@ -104,13 +114,6 @@ async fn server_handshake(writer: &mut Conn, secret_key: &SecretKey) -> Result<(
Ok(())
}

fn tung_wasm_to_io_err(e: tokio_tungstenite_wasm::Error) -> std::io::Error {
match e {
tokio_tungstenite_wasm::Error::Io(io_err) => io_err,
_ => std::io::Error::new(std::io::ErrorKind::Other, e.to_string()),
}
}

impl Stream for Conn {
type Item = Result<ReceivedMessage>;

Expand Down Expand Up @@ -152,10 +155,7 @@ impl Sink<Frame> for Conn {
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match *self {
Self::Relay { ref mut conn } => Pin::new(conn).poll_ready(cx).map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn)
.poll_ready(cx)
.map_err(tung_wasm_to_io_err)
.map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn).poll_ready(cx).map_err(Into::into),
}
}

Expand All @@ -171,28 +171,21 @@ impl Sink<Frame> for Conn {
.start_send(tokio_tungstenite_wasm::Message::binary(
frame.encode_for_ws_msg(),
))
.map_err(tung_wasm_to_io_err)
.map_err(Into::into),
}
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match *self {
Self::Relay { ref mut conn } => Pin::new(conn).poll_flush(cx).map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn)
.poll_flush(cx)
.map_err(tung_wasm_to_io_err)
.map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn).poll_flush(cx).map_err(Into::into),
}
}

fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match *self {
Self::Relay { ref mut conn } => Pin::new(conn).poll_close(cx).map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn)
.poll_close(cx)
.map_err(tung_wasm_to_io_err)
.map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn).poll_close(cx).map_err(Into::into),
}
}
}
Expand All @@ -203,10 +196,7 @@ impl Sink<SendMessage> for Conn {
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match *self {
Self::Relay { ref mut conn } => Pin::new(conn).poll_ready(cx).map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn)
.poll_ready(cx)
.map_err(tung_wasm_to_io_err)
.map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn).poll_ready(cx).map_err(Into::into),
}
}

Expand All @@ -223,28 +213,21 @@ impl Sink<SendMessage> for Conn {
.start_send(tokio_tungstenite_wasm::Message::binary(
frame.encode_for_ws_msg(),
))
.map_err(tung_wasm_to_io_err)
.map_err(Into::into),
}
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match *self {
Self::Relay { ref mut conn } => Pin::new(conn).poll_flush(cx).map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn)
.poll_flush(cx)
.map_err(tung_wasm_to_io_err)
.map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn).poll_flush(cx).map_err(Into::into),
}
}

fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match *self {
Self::Relay { ref mut conn } => Pin::new(conn).poll_close(cx).map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn)
.poll_close(cx)
.map_err(tung_wasm_to_io_err)
.map_err(Into::into),
Self::Ws { ref mut conn, .. } => Pin::new(conn).poll_close(cx).map_err(Into::into),
}
}
}
Expand Down

0 comments on commit 8c0810c

Please sign in to comment.