-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update version to "0.1.1"
- Loading branch information
Showing
7 changed files
with
97 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use cancellable::Cancellable; | ||
use reception::{client::Client, connection::Connection, SenderHandle}; | ||
use tokio::{net::TcpStream, sync::mpsc::unbounded_channel}; | ||
use tokio_tungstenite::WebSocketStream; | ||
use tokio_util::sync::CancellationToken; | ||
|
||
#[derive(Debug)] | ||
struct Model { | ||
payload: Vec<u8>, | ||
} | ||
|
||
impl Into<Vec<u8>> for Model { | ||
fn into(self) -> Vec<u8> { | ||
self.payload | ||
} | ||
} | ||
|
||
impl TryFrom<Vec<u8>> for Model { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(payload: Vec<u8>) -> Result<Self, Self::Error> { | ||
Ok(Self { payload }) | ||
} | ||
} | ||
|
||
fn handle_client( | ||
client: Client<Connection<WebSocketStream<TcpStream>>, Model, Model>, | ||
cancellation_token: CancellationToken, | ||
) -> Result<(), Client<Connection<WebSocketStream<TcpStream>>, Model, Model>> { | ||
tokio::spawn(async move { | ||
let (tx, mut rx) = unbounded_channel(); | ||
let mut handle = client | ||
.spawn_with_callback(cancellation_token, move |msg| { | ||
tx.send(msg).unwrap(); | ||
Ok(()) | ||
}) | ||
.await; | ||
|
||
while let Some(msg) = rx.recv().await { | ||
handle.send(msg).await.unwrap(); | ||
} | ||
}); | ||
Ok(()) | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<(), anyhow::Error> { | ||
let cancellation_token = CancellationToken::new(); | ||
|
||
let listener = | ||
reception::Listener::<Model, Model>::bind(Default::default(), cancellation_token.clone()) | ||
.await?; | ||
|
||
let handle = listener | ||
.spawn_with_callback(cancellation_token.clone(), move |client| { | ||
handle_client(client, cancellation_token.clone()) | ||
}) | ||
.await; | ||
|
||
handle.await??; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ use std::{ | |
atomic::{AtomicBool, Ordering}, | ||
Arc, Weak, | ||
}, | ||
vec, | ||
}; | ||
|
||
use async_trait::async_trait; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
use async_trait::async_trait; | ||
|
||
/// Sender trait implemented for some service handles. | ||
/// | ||
/// Useful for sending data into the service. | ||
#[async_trait] | ||
pub trait SenderHandle { | ||
/// Type of value send into the service. | ||
type Item; | ||
|
||
/// Type of error returned when the send has failed. | ||
type Error; | ||
|
||
/// Sends the given `item` into the service. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `item` - value to the send into the service. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns `Ok(())` when it successfully send data into the service, | ||
/// `Err(Self::Error)` otherwise. | ||
async fn send(&mut self, item: Self::Item) -> Result<(), Self::Error>; | ||
} |