Skip to content

Commit

Permalink
Merge pull request #4 from swimos/socket-closure
Browse files Browse the repository at this point in the history
Socket closure
  • Loading branch information
SirCipher authored Aug 12, 2022
2 parents 85bdd4b + 9919cd7 commit 30898fe
Show file tree
Hide file tree
Showing 8 changed files with 689 additions and 217 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: 1.56.1
override: true
- uses: actions-rs/cargo@v1
with:
Expand All @@ -31,7 +31,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: 1.56.1
override: true
- uses: actions-rs/cargo@v1
with:
Expand All @@ -46,7 +46,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: 1.56.1
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
Expand All @@ -62,7 +62,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: 1.56.1
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
Expand Down
13 changes: 11 additions & 2 deletions ratchet_core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,18 @@ impl From<InvalidHeaderValue> for Error {
}

#[derive(Clone, Copy, Error, Debug, PartialEq)]
/// The channel is already closed
/// The channel is closed
#[error("The channel is already closed")]
pub struct CloseError;
pub enum CloseCause {
/// The channel closed nominally. This is **not** an error and instead indicates a clean closure
/// of the channel by either ourselves or the peer.
#[error("The channel closed as expected")]
Stopped,
/// This is only produced when a user attempts to reuse a closed channel and instead indicates a
/// bug in your code.
#[error("Attempted to use a closed channel")]
Error,
}

/// WebSocket protocol errors.
#[derive(Copy, Clone, Debug, PartialEq, Error)]
Expand Down
5 changes: 4 additions & 1 deletion ratchet_core/src/framed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,10 @@ where
)
.await
}

pub async fn close(&mut self) {
let _r = self.io.shutdown().await;
}
}

pub async fn read_next<I, E>(
Expand Down Expand Up @@ -624,7 +628,6 @@ where
|_, _| Ok(()),
)
.await
.map_err(|e| Error::with_cause(ErrorKind::Close, e))
}

pub async fn write_fragmented<A, I, F>(
Expand Down
8 changes: 5 additions & 3 deletions ratchet_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
unused_import_braces
)]

extern crate core;

#[cfg(test)]
mod test_fixture;

Expand All @@ -40,14 +42,14 @@ mod ws;
/// Split WebSocket implementation.
#[cfg(feature = "split")]
mod split;
#[cfg(feature = "split")]
pub use split::{Receiver, ReuniteError, Sender};

#[allow(missing_docs)]
#[cfg(feature = "fixture")]
pub mod fixture {
pub use super::protocol::write_text_frame_header;
}
#[cfg(feature = "split")]
pub use split::{Receiver, ReuniteError, Sender};

pub use builder::{WebSocketClientBuilder, WebSocketServerBuilder};
pub use errors::*;
Expand All @@ -59,7 +61,7 @@ pub use handshake::{
pub use protocol::{
CloseCode, CloseReason, Message, MessageType, PayloadType, Role, WebSocketConfig,
};
pub use ws::WebSocket;
pub use ws::{CloseState, WebSocket};

use tokio::io::{AsyncRead, AsyncWrite};

Expand Down
Loading

0 comments on commit 30898fe

Please sign in to comment.