#socket #networking #io #async

rustdtp

Cross-platform networking interfaces for Rust

14 unstable releases (5 breaking)

new 0.6.1 Feb 10, 2024
0.5.1 Feb 3, 2024
0.4.1 Mar 20, 2023
0.3.2 Aug 17, 2022
0.3.0 Jul 30, 2022

#273 in Cryptography

Download history 14/week @ 2023-10-26 3/week @ 2023-11-02 11/week @ 2023-11-09 2/week @ 2023-11-16 20/week @ 2023-11-23 23/week @ 2023-11-30 12/week @ 2023-12-07 10/week @ 2023-12-14 20/week @ 2023-12-21 10/week @ 2023-12-28 32/week @ 2024-01-04 2/week @ 2024-01-11 6/week @ 2024-01-18 13/week @ 2024-01-25 38/week @ 2024-02-01 89/week @ 2024-02-08

147 downloads per month

MIT license

170KB
3K SLoC

Data Transfer Protocol for Rust

Cross-platform networking interfaces for Rust.

Data Transfer Protocol

The Data Transfer Protocol (DTP) is a larger project to make ergonomic network programming available in any language. See the full project here.

Installation

Add the package in Cargo.toml:

rustdtp = "0.6"

Creating a server

A server can be built using the Server implementation:

use rustdtp::*;

#[tokio::main]
async fn main() {
    // Create a server that receives strings and returns the length of each string
    let (mut server, mut server_events) = Server::builder()
        .sending::<usize>()
        .receiving::<String>()
        .with_event_channel()
        .start(("0.0.0.0", 0))
        .await
        .unwrap();

    // Iterate over events
    while let Some(event) = server_events.next().await {
        match event {
            ServerEvent::Connect { client_id } => {
                println!("Client with ID {} connected", client_id);
            }
            ServerEvent::Disconnect { client_id } => {
                println!("Client with ID {} disconnected", client_id);
            }
            ServerEvent::Receive { client_id, data } => {
                // Send back the length of the string
                server.send(client_id, data.len()).await.unwrap();
            }
            ServerEvent::Stop => {
                // No more events will be sent, and the loop will end
                println!("Server closed");
            }
        }
    }
}

Creating a client

A client can be built using the Client implementation:

use rustdtp::*;

#[tokio::main]
async fn main() {
    // Create a client that sends a message to the server and receives the length of the message
    let (mut client, mut client_events) = Client::builder()
        .sending::<String>()
        .receiving::<usize>()
        .with_event_channel()
        .connect(("127.0.0.1", 29275))
        .await
        .unwrap();

    // Send a message to the server
    let msg = "Hello, server!".to_owned();
    client.send(msg.clone()).await.unwrap();

    // Receive the response
    match client_events.next().await.unwrap() {
        ClientEvent::Receive { data } => {
            // Validate the response
            println!("Received response from server: {}", data);
            assert_eq!(data, msg.len());
        }
        event => {
            // Unexpected response
            panic!("expected to receive a response from the server, instead got {:?}", event);
        }
    }
}

Security

Information security comes included. Every message sent over a network interface is encrypted with AES-256. Key exchanges are performed using a 2048-bit RSA key-pair.

Dependencies

~7–15MB
~182K SLoC