tcpr

Crates.iotcpr
lib.rstcpr
version0.1.3
sourcesrc
created_at2024-06-04 00:54:28.706182
updated_at2024-06-04 18:53:03.735284
descriptionErgonomic TCP Client/Server
homepage
repositoryhttps://gitlab.com/robertlopezdev/tcpr
max_upload_size
id1260605
size15,893
(throwaway124567)

documentation

README

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.

Commit count: 14

cargo fmt