Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

* Minimal and Efficient : minimal codebase to provide efficient, memory-safe WebSocket handling.

* Multi Environment : `tokio`, `smol`, `glommio` are supported as async runtime ( by feature flags `rt_{name}` ).
* Multi-Environment : `tokio`, `smol`, `glommio` are supported as async runtime ( by feature flags `rt_{name}` ).

## Note

Expand All @@ -33,10 +33,11 @@ mews = { version = "0.2", features = ["rt_tokio"] }
tokio = { version = "1", features = ["rt"] }
# ...
```
*( with pseudo Request & Response )*
```rust
**( with pseudo Request & Response )**
```rust,ignore
/* server */

use tokio::net::TcpStream;
use mews::{WebSocketContext, Connection, Message};

async fn handle_websocket(
Expand All @@ -48,7 +49,7 @@ async fn handle_websocket(
);

let (sign, ws) = ctx.on_upgrade(
|mut conn: Connection| async move {
|mut conn: Connection<TcpStream>| async move {
while let Ok(Some(Message::Text(text))) = conn.recv().await {
conn.send(text).await
.expect("failed to send message");
Expand All @@ -67,9 +68,11 @@ async fn handle_websocket(
ws.manage(tcp);
}
```
```rust
```rust,ignore
/* client */

use tokio::net::TcpStream;

async fn start_websocket(
mut tcp: TcpStream
) {
Expand All @@ -80,7 +83,7 @@ async fn start_websocket(
);

let (sign, ws) = ctx.on_upgrade(
|mut conn: Connection| async move {
|mut conn: Connection<TcpStream>| async move {
conn.send("Hello!").await.expect("failed to send message");
while let Ok(Some(Message::Text(text))) = conn.recv().await {
println!("got: `{text}`")
Expand Down
14 changes: 3 additions & 11 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{sync::Arc, io::Error};
pub trait UnderlyingConnection: AsyncRead + AsyncWrite + Unpin + 'static {}
impl<T: AsyncRead + AsyncWrite + Unpin + 'static> UnderlyingConnection for T {}

pub struct Connection<C: UnderlyingConnection = crate::runtime::TcpStream> {
pub struct Connection<C: UnderlyingConnection> {
__closed__: Arc<RwLock<bool>>,

conn: Arc<std::cell::UnsafeCell<C>>,
Expand Down Expand Up @@ -343,14 +343,6 @@ pub mod split {
<tokio::net::TcpStream>::split(self)
}
}
#[cfg(feature="rt_nio")]
impl<'split> Splitable<'split> for nio::net::TcpStream {
type ReadHalf = nio::net::tcp::ReadHalf <'split>;
type WriteHalf = nio::net::tcp::WriteHalf<'split>;
fn split(&'split mut self) -> (Self::ReadHalf, Self::WriteHalf) {
<nio::net::TcpStream>::split(self)
}
}
#[cfg(feature="rt_glommio")]
impl<'split, T: AsyncRead + AsyncWrite + Unpin + 'split> Splitable<'split> for T {
type ReadHalf = futures_util::io::ReadHalf <&'split mut T>;
Expand All @@ -371,7 +363,7 @@ pub mod split {
}
};

pub struct ReadHalf<C: AsyncRead + Unpin = <crate::runtime::TcpStream as Splitable<'static>>::ReadHalf> {
pub struct ReadHalf<C: AsyncRead + Unpin> {
__closed__: Arc<RwLock<bool>>,
conn: C,
config: Config,
Expand All @@ -389,7 +381,7 @@ pub mod split {
}
}

pub struct WriteHalf<C: AsyncWrite + Unpin = <crate::runtime::TcpStream as Splitable<'static>>::WriteHalf> {
pub struct WriteHalf<C: AsyncWrite + Unpin> {
__closed__: Arc<RwLock<bool>>,
conn: C,
config: Config,
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ compile_error! {"More than one runtime feature flags can't be activated"}
mod runtime {
#[cfg(feature="rt_tokio")]
pub use {
tokio::net::TcpStream,
tokio::io::AsyncReadExt as AsyncRead,
tokio::io::AsyncWriteExt as AsyncWrite,
tokio::sync::RwLock,
Expand All @@ -45,7 +44,6 @@ mod runtime {

#[cfg(feature="rt_smol")]
pub use {
smol::net::TcpStream,
smol::io::AsyncReadExt as AsyncRead,
smol::io::AsyncWriteExt as AsyncWrite,
smol::lock::RwLock,
Expand All @@ -57,7 +55,6 @@ mod runtime {

#[cfg(feature="rt_glommio")]
pub use {
glommio::net::TcpStream,
futures_util::AsyncReadExt as AsyncRead,
futures_util::AsyncWriteExt as AsyncWrite,
glommio::sync::RwLock,
Expand Down
9 changes: 4 additions & 5 deletions src/websocket.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::runtime;
use crate::message::{CloseFrame, CloseCode};
use crate::connection::{UnderlyingConnection, Connection};

Expand Down Expand Up @@ -47,7 +46,7 @@ pub type Handler<C> = Box<dyn
/// );
///
/// let (sign, ws) = ctx.on_upgrade(
/// |mut conn: Connection| async move {
/// |mut conn: Connection<TcpStream>| async move {
/// while let Ok(Some(Message::Text(text))) = conn.recv().await {
/// conn.send(text).await
/// .expect("failed to send message");
Expand Down Expand Up @@ -130,7 +129,7 @@ const _: () = {
/// tcp: TcpStream
/// ) -> Response {
/// let (sign, ws) = ctx.on_upgrade(
/// |mut conn: Connection| async move {
/// |mut conn: Connection<TcpStream>| async move {
/// while let Ok(Some(Message::Text(text))) = conn.recv().await {
/// conn.send(text).await
/// .expect("failed to send message");
Expand All @@ -145,7 +144,7 @@ const _: () = {
/// }
/// ```
#[must_use = "`WebSocket` does nothing unless `.manage()` or `.manage_with_timeout()` is called"]
pub struct WebSocket<C: UnderlyingConnection = runtime::TcpStream> {
pub struct WebSocket<C: UnderlyingConnection> {
config: Config,
handler: Handler<C>,
}
Expand Down Expand Up @@ -185,7 +184,7 @@ impl<C: UnderlyingConnection> WebSocket<C> {
const _: () = {
impl<C: UnderlyingConnection> PartialEq for WebSocket<C>
where
dyn FnOnce(Connection<C>) -> std::pin::Pin<Box<(dyn std::future::Future<Output = ()> + Send + 'static)>> + Send + Sync
dyn FnOnce(Connection<C>) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'static>> + Send + Sync
: PartialEq
{
fn eq(&self, other: &Self) -> bool {
Expand Down