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.5"
Creating a server
A server can be built using the Server
implementation:
use rustdtp::*;
#[tokio::main]
async fn main() {
let (mut server, mut server_events) = Server::builder()
.sending::<usize>()
.receiving::<String>()
.with_event_channel()
.start(("0.0.0.0", 0))
.await
.unwrap();
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 } => {
server.send(client_id, data.len()).await.unwrap();
}
ServerEvent::Stop => {
println!("Server closed");
}
}
}
}
Creating a client
A client can be built using the Client
implementation:
use rustdtp::*;
#[tokio::main]
async fn main() {
let (mut client, mut client_events) = Client::builder()
.sending::<String>()
.receiving::<usize>()
.with_event_channel()
.connect(("127.0.0.1", 29275))
.await
.unwrap();
let msg = "Hello, server!".to_owned();
client.send(msg.clone()).await.unwrap();
match client_events.next().await.unwrap() {
ClientEvent::Receive { data } => {
println!("Received response from server: {}", data);
assert_eq!(data, msg.len());
}
event => {
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.