4 releases

0.1.3 Jun 4, 2024
0.1.2 Jun 4, 2024
0.1.1 Jun 4, 2024
0.1.0 Jun 4, 2024

#26 in #serialization-deserialization

MIT license

15KB
240 lines

tcpr

Simple and ergonomic TCP Client/Server with built in serialization/deserialization.

Requirements

Installation

cargo add tcpr

Documentation

This README provides a general overview. Full crate documentation can be found here at docs.rs

Usage

Basic Example

    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.

License

MIT | See LICENSE.md

Project status

I plan to maintain this project for the foreseeable future; although it is a base utility, so I would not expect any breaking changes.

Dependencies

~3–12MB
~126K SLoC