Expand description
§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.7"
§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 Ok(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.
Structs§
- A socket client.
- A builder for the
Client
. - Configuration for a client’s event callbacks.
- Known client event reporting type, stored as the type parameter
E
. - Client event reporting via callbacks.
- Client event reporting via a channel.
- Client event reporting via an event handler.
- Unknown client event reporting type.
- An asynchronous stream of client events.
- A handle to the client.
- Known client receiving type, stored as the type parameter
R
. - Unknown client receiving type.
- Known client sending type, stored as the type parameter
S
. - Unknown client sending type.
- A socket server.
- A builder for the
Server
. - Configuration for a server’s event callbacks.
- Known server event reporting type, stored as the type parameter
E
. - Server event reporting via callbacks.
- Server event reporting via a channel.
- Server event reporting via an event handler.
- Unknown server event reporting type.
- An asynchronous stream of server events.
- A handle to the server.
- Known server receiving type, stored as the type parameter
R
. - Unknown server receiving type.
- Known server sending type, stored as the type parameter
S
. - Unknown server sending type.
Enums§
- A command sent from the client handle to the background client task.
- The return value of a command executed on the background client task.
- An event from the client.
- A command sent from the server background task to a client background task.
- The return value of a command executed on a client background task.
- A command sent from the server handle to the background server task.
- The return value of a command executed on the background server task.
- An event from the server.
Traits§
- An event handling trait for the client.
- An event handling trait for the server.