API Reference

📘 API Version

This documentation covers the new simplified API introduced in VSTP v0.2.1. For the low-level API, see the Low-Level API Reference.

VstpClient

The VstpClient struct provides a high-level interface for sending and receiving messages.

Creating a Client

// 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?;

Methods

send

Sends any serializable data to the server.

async fn send<T: Serialize>(&self, data: T) -> Result<(), VstpError>

receive

Receives and deserializes data from the server.

async fn receive<T: DeserializeOwned>(&self) -> Result<T, VstpError>

send_with_ack

Sends data and waits for acknowledgment.

async fn send_with_ack<T: Serialize>(&self, data: T) -> Result<(), VstpError>

send_raw

Sends a raw frame without serialization.

async fn send_raw(&self, frame: Frame) -> Result<(), VstpError>

set_timeout

Sets the operation timeout.

fn set_timeout(&mut self, timeout: Duration)

VstpServer

The VstpServer struct provides a high-level interface for handling client connections and messages.

Creating a Server

// 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?;

Methods

serve

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 + 'static

set_timeout

Sets the operation timeout.

fn set_timeout(&mut self, timeout: Duration)

Error Handling

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),
}

⚠️ Error Handling Best Practices

  • Always check for timeouts and connection errors
  • Use send_with_ack for important messages
  • Handle serialization errors for better error messages

Timeouts

VSTP 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));

💡 Timeout Tips

  • Use shorter timeouts for real-time applications
  • Consider network conditions when setting timeouts
  • UDP operations might need longer timeouts for retransmission

Serialization

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?;

🎯 Serialization Tips

  • Use derive macros for simple types
  • Implement custom serialization for complex types
  • Consider message size in UDP mode

Next Steps

Now that you understand the API, check out these resources: