Skip to content

Commit 30898fe

Browse files
authored
Merge pull request #4 from swimos/socket-closure
Socket closure
2 parents 85bdd4b + 9919cd7 commit 30898fe

File tree

8 files changed

+689
-217
lines changed

8 files changed

+689
-217
lines changed

.github/workflows/ci.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- uses: actions-rs/toolchain@v1
1212
with:
1313
profile: minimal
14-
toolchain: stable
14+
toolchain: 1.56.1
1515
override: true
1616
- uses: actions-rs/cargo@v1
1717
with:
@@ -31,7 +31,7 @@ jobs:
3131
- uses: actions-rs/toolchain@v1
3232
with:
3333
profile: minimal
34-
toolchain: stable
34+
toolchain: 1.56.1
3535
override: true
3636
- uses: actions-rs/cargo@v1
3737
with:
@@ -46,7 +46,7 @@ jobs:
4646
- uses: actions-rs/toolchain@v1
4747
with:
4848
profile: minimal
49-
toolchain: stable
49+
toolchain: 1.56.1
5050
override: true
5151
- run: rustup component add rustfmt
5252
- uses: actions-rs/cargo@v1
@@ -62,7 +62,7 @@ jobs:
6262
- uses: actions-rs/toolchain@v1
6363
with:
6464
profile: minimal
65-
toolchain: stable
65+
toolchain: 1.56.1
6666
override: true
6767
- run: rustup component add clippy
6868
- uses: actions-rs/cargo@v1

ratchet_core/src/errors.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,18 @@ impl From<InvalidHeaderValue> for Error {
255255
}
256256

257257
#[derive(Clone, Copy, Error, Debug, PartialEq)]
258-
/// The channel is already closed
258+
/// The channel is closed
259259
#[error("The channel is already closed")]
260-
pub struct CloseError;
260+
pub enum CloseCause {
261+
/// The channel closed nominally. This is **not** an error and instead indicates a clean closure
262+
/// of the channel by either ourselves or the peer.
263+
#[error("The channel closed as expected")]
264+
Stopped,
265+
/// This is only produced when a user attempts to reuse a closed channel and instead indicates a
266+
/// bug in your code.
267+
#[error("Attempted to use a closed channel")]
268+
Error,
269+
}
261270

262271
/// WebSocket protocol errors.
263272
#[derive(Copy, Clone, Debug, PartialEq, Error)]

ratchet_core/src/framed/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,10 @@ where
572572
)
573573
.await
574574
}
575+
576+
pub async fn close(&mut self) {
577+
let _r = self.io.shutdown().await;
578+
}
575579
}
576580

577581
pub async fn read_next<I, E>(
@@ -624,7 +628,6 @@ where
624628
|_, _| Ok(()),
625629
)
626630
.await
627-
.map_err(|e| Error::with_cause(ErrorKind::Close, e))
628631
}
629632

630633
pub async fn write_fragmented<A, I, F>(

ratchet_core/src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
unused_import_braces
2727
)]
2828

29+
extern crate core;
30+
2931
#[cfg(test)]
3032
mod test_fixture;
3133

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

4448
#[allow(missing_docs)]
4549
#[cfg(feature = "fixture")]
4650
pub mod fixture {
4751
pub use super::protocol::write_text_frame_header;
4852
}
49-
#[cfg(feature = "split")]
50-
pub use split::{Receiver, ReuniteError, Sender};
5153

5254
pub use builder::{WebSocketClientBuilder, WebSocketServerBuilder};
5355
pub use errors::*;
@@ -59,7 +61,7 @@ pub use handshake::{
5961
pub use protocol::{
6062
CloseCode, CloseReason, Message, MessageType, PayloadType, Role, WebSocketConfig,
6163
};
62-
pub use ws::WebSocket;
64+
pub use ws::{CloseState, WebSocket};
6365

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

0 commit comments

Comments
 (0)