Module assets

Module assets 

Source
Expand description

Asset distribution for large file transfers.

This module provides efficient transfer of large files (textures, maps, mods) with chunking, integrity verification, and resumable downloads.

§Architecture

┌────────────────────────────────────────────────────────────────┐
│                      Asset Transfer                            │
│                                                                │
│  ┌─────────────┐    ┌─────────────┐    ┌─────────────┐        │
│  │   Chunker   │───▶│  Compressor │───▶│   Sender    │        │
│  │   (64KB)    │    │   (LZ4)     │    │  (Reliable) │        │
│  └─────────────┘    └─────────────┘    └─────────────┘        │
│                                                                │
│  ┌─────────────┐    ┌─────────────┐    ┌─────────────┐        │
│  │   Hasher    │◀───│ Assembler   │◀───│  Receiver   │        │
│  │  (BLAKE3)   │    │             │    │  (Ordered)  │        │
│  └─────────────┘    └─────────────┘    └─────────────┘        │
└────────────────────────────────────────────────────────────────┘

§Features

  • Chunked Transfer: Splits large files into 64KB chunks
  • Compression: Optional LZ4 compression for faster transfers
  • Integrity: BLAKE3 hash verification per chunk and per file
  • Resumable: Can resume interrupted transfers
  • Progress: Real-time progress callbacks

§Example

§Server (sending assets)

use fastnet::assets::{AssetServer, AssetConfig};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let mut server = AssetServer::new(AssetConfig::default());
     
    // Register assets to serve
    server.register("map_forest.pak", "/game/maps/forest.pak").await?;
    server.register("textures.pak", "/game/textures/pack1.pak").await?;
     
    // Handle requests (integrate with your game server)
    // server.handle_request(peer_id, request).await?;
     
    Ok(())
}

§Client (downloading assets)

use fastnet::assets::{AssetClient, AssetEvent};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let mut client = AssetClient::new();
     
    // Request an asset
    client.request("map_forest.pak", "/local/maps/forest.pak").await?;
     
    // Process download
    loop {
        for event in client.poll().await? {
            match event {
                AssetEvent::Progress { id, received, total } => {
                    let percent = (received as f64 / total as f64) * 100.0;
                    println!("Downloading: {:.1}%", percent);
                }
                AssetEvent::Completed { id, path } => {
                    println!("Downloaded: {}", path);
                    break;
                }
                AssetEvent::Failed { id, error } => {
                    eprintln!("Failed: {}", error);
                    break;
                }
            }
        }
    }
     
    Ok(())
}

Structs§

AssetChunk
Outgoing chunk for transfer.
AssetClient
Client-side asset downloading.
AssetConfig
Configuration for asset transfers.
AssetInfo
Asset metadata.
AssetServer
Server-side asset management.
TransferStats
Transfer statistics.

Enums§

AssetEvent
Asset transfer events.

Constants§

DEFAULT_CHUNK_SIZE
Default chunk size (64KB).

Functions§

deserialize_asset_info
Deserialize asset info from network.
deserialize_chunk
Deserialize a chunk from network.
serialize_asset_info
Serialize asset info for network transfer.
serialize_chunk
Serialize a chunk for network transfer.