Crates.io | tcpr |
lib.rs | tcpr |
version | 0.1.3 |
source | src |
created_at | 2024-06-04 00:54:28.706182 |
updated_at | 2024-06-04 18:53:03.735284 |
description | Ergonomic TCP Client/Server |
homepage | |
repository | https://gitlab.com/robertlopezdev/tcpr |
max_upload_size | |
id | 1260605 |
size | 15,893 |
Simple and ergonomic TCP Client/Server with built in serialization/deserialization.
cargo add tcpr
This README provides a general overview. Full crate documentation can be found here at docs.rs
let (sx1, mut rx1) = channel::<Message>(4);
let (sx2, mut rx2) = channel::<Message>(4);
tokio::spawn(async move {
loop {
if let Some(message) = rx1.recv().await {
// ... process message
}
}
});
tokio::spawn(async move {
loop {
if let Some(message) = rx2.recv().await {
// ... process message
}
}
});
let server1 = TcpServer::new("127.0.0.1:1337", sx1, None);
let server2 = TcpServer::new("127.0.0.1:1338", sx2, None);
tokio::spawn(async move {
server1.listen().await.unwrap();
});
tokio::spawn(async move {
server2.listen().await.unwrap();
});
sleep(Duration::from_secs(3)).await;
let client1 = TcpClient::connect("127.0.0.1:1337").await.unwrap();
let client2 = TcpClient::connect("127.0.0.1:1338").await.unwrap();
sleep(Duration::from_secs(3)).await;
client1
.write(&Message::String("Hello".to_string()))
.await
.unwrap();
let mut map = HashMap::new();
map.insert("Hello".to_string(), "world".to_string());
sleep(Duration::from_secs(3)).await;
client2.write(&Message::HashMap(map)).await.unwrap();
TcpServer
also exposes the option for a error_sender
to propagates TcpListenerError
to the receiver.
MIT | See LICENSE.md
I plan to maintain this project for the foreseeable future; although it is a base utility, so I would not expect any breaking changes.