This documentation covers the new simplified API introduced in VSTP v0.2.1. For the low-level API, see the Low-Level API Reference.
The VstpClient struct provides a high-level interface for sending and receiving messages.
// TCP client
let client = VstpClient::connect_tcp("127.0.0.1:8080").await?;
// UDP client
let client = VstpClient::connect_udp("127.0.0.1:8080").await?;Sends any serializable data to the server.
async fn send<T: Serialize>(&self, data: T) -> Result<(), VstpError>Receives and deserializes data from the server.
async fn receive<T: DeserializeOwned>(&self) -> Result<T, VstpError>Sends data and waits for acknowledgment.
async fn send_with_ack<T: Serialize>(&self, data: T) -> Result<(), VstpError>Sends a raw frame without serialization.
async fn send_raw(&self, frame: Frame) -> Result<(), VstpError>Sets the operation timeout.
fn set_timeout(&mut self, timeout: Duration)The VstpServer struct provides a high-level interface for handling client connections and messages.
// TCP server
let server = VstpServer::bind_tcp("127.0.0.1:8080").await?;
// UDP server
let server = VstpServer::bind_udp("127.0.0.1:8080").await?;Starts the server with a message handler.
async fn serve<F, Fut, T, R>(self, handler: F) -> Result<(), VstpError>
where
F: Fn(T) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R, VstpError>> + Send,
T: DeserializeOwned + Send + 'static,
R: Serialize + Send + 'staticSets the operation timeout.
fn set_timeout(&mut self, timeout: Duration)VSTP provides comprehensive error handling through the VstpError enum:
pub enum VstpError {
// IO errors
Io(std::io::Error),
// Protocol errors
Protocol(String),
InvalidVersion { expected: u8, got: u8 },
InvalidFrameType(u8),
InvalidMagic([u8; 2]),
// Operation errors
Timeout,
InvalidAddress,
SerializationError,
DeserializationError,
UnexpectedFrameType,
ConnectionClosed,
ServerError(String),
}send_with_ack for important messagesVSTP provides configurable timeouts for all operations. The default timeout is 30 seconds.
// Set client timeout
let mut client = VstpClient::connect_tcp("127.0.0.1:8080").await?;
client.set_timeout(Duration::from_secs(5));
// Set server timeout
let mut server = VstpServer::bind_tcp("127.0.0.1:8080").await?;
server.set_timeout(Duration::from_secs(10));VSTP uses serde for automatic serialization and deserialization of messages.
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct MyMessage {
field1: String,
field2: i32,
field3: Vec<f64>,
}
// Send typed message
client.send(MyMessage {
field1: "hello".to_string(),
field2: 42,
field3: vec![1.0, 2.0, 3.0],
}).await?;
// Receive typed message
let msg: MyMessage = client.receive().await?;Now that you understand the API, check out these resources: