tsclientlib

repository·master·Indexed 40 days ago

https://github.com/respeak/tsclientlib

A Rust library for creating TeamSpeak clients and bots. It provides a high-level abstraction for managing server connections via the Connection struct, handling audio streams with AudioHandler, and managing file transfers. It is built on top of the low-level tsproto implementation and includes version 0.2.0.

Tokens
22.6K
Snippets
88
Records
112
Agent score
97%

What's inside tsclientlib

  1. Overview of TsClientlib

    master

    Tsclientlib is a library designed to simplify the creation of TeamSpeak clients and bots. It provides a high-level abstraction for managing connections to TeamSpeak servers.

    Key components:

    • Connection: The base class of the library. A single instance of this struct manages a single connection to a TeamSpeak server.

    If you are looking for a full client application rather than a library, consider using Qint.

  2. Overview of ts-bookkeeping

    master

    ts-bookkeeping is a crate designed to maintain a local representation of a TeamSpeak server's state, including its clients and channels.

    It functions as a "bookkeeping" tool by processing incoming TeamSpeak commands to update its internal state. This state tracking allows you to monitor the server and generate events based on command processing. Additionally, the crate provides generated structs for all TeamSpeak commands, allowing you to construct and send packets back to the server to perform various actions.

  3. Using the unstable feature in Tsclientlib

    master

    Tsclientlib provides an unstable feature flag. Enabling this feature allows for more granular control over the internals of a connection.

    Warning: Functionality provided by the unstable feature is subject to change in any minor release and should be used with caution if stability is required.

  4. Understand the relationship between TsProto and tsclientlib

    master

    Core Distinction

    • tsproto: This is a low-level implementation of the TeamSpeak3 protocol. It is intended for protocol-level work.
    • tsclientlib: This is the high-level, user-facing library designed for building TeamSpeak clients and bots. It provides a convenient interface built on top of tsproto.

    Recommendation: If your goal is to build a functional client or a bot, you should use tsclientlib instead of interacting with tsproto directly.

  5. Use tsproto for TeamSpeak3 protocol implementation

    master

    The tsproto library provides a low-level implementation of the TeamSpeak3 protocol.

    Note: If you are looking to build a usable client or bot, it is highly recommended to use tsclientlib instead. tsclientlib provides a convenient, high-level interface built on top of tsproto.

    For a reference implementation of a cross-platform TeamSpeak client using these libraries, see Qint.

  6. Understand the Bookkeeping schema structure

    master

    The bookkeeping system uses a schema defined in TOML (loaded via DATA_STR) to describe data structures. The schema is composed of BookDeclarations, which contain a collection of Struct definitions. Each Struct defines its own properties and identity (Id) markers. This schema is used to drive code generation and type resolution within the library.

    // The schema is globally available via the DATA static variable
    // which is a parsed BookDeclarations object.
    use tsproto_structs::DATA;
    
    let my_struct = DATA.get_struct("MyStructName");
  7. Manage incoming audio with AudioHandler

    master

    The AudioHandler is the primary interface for managing multiple incoming audio streams from different clients. It collects incoming InAudioBuf packets, manages an AudioQueue for each client, handles decoding (Opus), manages jitter via dynamic buffering, and deals with out-of-order or missing packets.

    Key workflows:

    1. Receive Packets: Use handle_packet to feed incoming audio data into the handler. If a client is new, it returns Some(id).
    2. Extract Audio: Use fill_buffer or fill_buffer_with_proc to decode the queued packets into a raw PCM buffer (f32) for playback or processing.
    3. Cleanup: The handler automatically removes clients from its internal queues if they experience too many consecutive packet losses (MAX_PACKET_LOSSES) or if an end-of-stream packet is received.
    // Example conceptual workflow
    let mut handler = AudioHandler::<ClientId>::new();
    
    // 1. Handle an incoming packet
    // Returns Some(id) if a new client started talking
    let result = handler.handle_packet(client_id, incoming_packet)?; 
    
    // 2. Fill an output buffer with decoded PCM samples
    let mut output_buf = vec![0.0; 4800];
    let inactive_clients = handler.fill_buffer(&mut output_buf);
  8. Use SyncConnectionHandle to manage connections across threads

    master

    The sync module provides a thread-safe way to interact with a connection. While SyncConnection is the primary object, it cannot be easily shared across threads. Instead, you should use SyncConnectionHandle, which is a Clone-able handle that allows you to perform asynchronous actions (like sending commands, uploading/downloading files, or disconnecting) from any thread.

    To use it, call .get_handle() on your SyncConnection instance. The actual SyncConnection object should typically be moved into a background task (e.g., using tokio::spawn) and polled as a Stream to process incoming events.

    // Typical pattern for using SyncConnection
    let con: SyncConnection = Connection::build("localhost").connect().unwrap().into();
    let mut handle = con.get_handle();
    
    // Move the connection to a background task to process the stream
    tokio::spawn(con.for_each(|item| async { /* handle SyncStreamItem */ }));
    
    // Use the handle in the main thread or other tasks
    handle.wait_until_connected().await.unwrap();