ZLToolKit Documentation

repository·master·Indexed 24 days ago

https://github.com/zlmediakit/zltoolkit

A lightweight, high-performance C++11 network programming framework for commercial server applications. It features a network library with thread-safe TCP/UDP clients and servers, a threading library with thread pools and timers, and a utility library covering MySQL connection pools, SSL encryption, and asynchronous logging. Includes wepoll, a library that implements the Linux epoll API for Windows Vista and higher.

Tokens
8.6K
Snippets
6
Records
66
Agent score
81%

What's inside ZLToolKit

  1. Overview of zltoolkit modules

    master

    zltoolkit is a lightweight C++11 network programming framework organized into several functional modules located in the src directory:

    • NetWork: Provides socket abstractions, including TCP servers/clients, UDP sockets, and session management for long-lived connections.
    • Poller: Handles main-thread event polling, including pipe abstractions, select model wrappers, and timers.
    • Thread: Mantains threading primitives such as thread pools, asynchronous task threads, task queues, and synchronization primitives (spin locks, semaphores, read-write locks).
    • Util: A collection of utility tools including file operations, logging, MD5 encryption, INI configuration parsing, MySQL connection pooling, SSL encapsulation, and ring buffers.
  2. Overview of ZLToolKit

    master

    ZLToolKit is a lightweight, easy-to-use C++11 network programming framework designed for high concurrency and cross-platform compatibility. It utilizes an epoll + thread pool + asynchronous network IO model to ensure superior performance.

    Key features include:

    • Network Library: Thread-safe TCP/UDP clients and template-based TCP/UDP servers (using a Session class logic).
    • Thread Library: Timers, semaphores, thread groups, and a thread pool supporting functional and lambda expressions.
    • Utility Library: File operations, color-highlighted asynchronous logging, INI configuration, listener pattern message broadcaster, smart-pointer-based object pools, ring buffers, MySQL connection pools (supporting placeholder ? syntax), SSL encryption/decryption, and command-line parsing.
  3. Use the Util module for common programming tasks

    master

    The Util module contains various helper classes for common requirements:

    • Configuration: mini.h provides INI configuration file reading and writing (supports both Unix and Windows line endings).
    • Data Buffering: RingBuffer.h provides an adaptive-sized ring buffer, suitable for GOP (Group of Pictures) caching.
    • Database: SqlPool.h provides a MySQL connection pool and a simple SQL statement generation tool.
    • Security: SSLBox.h provides a black-box encapsulation of OpenSSL, hiding SSL handshake details and supporting multi-threading.
    • Messaging: NoticeCenter.h is a message broadcaster that can broadcast any number of arguments of any type.
    • Memory Management: ResourcePool.h is a circular pool based on smart pointers that eliminates the need for manual object reclamation.
    • Logging: logger.h provides logging capabilities.
  4. Use the Thread module for concurrency and task management

    master

    The Thread module provides various ways to manage concurrency:

    • ThreadPool: Allows submitting std::function tasks to be executed by background threads.
    • AsyncTaskThread: A background thread for asynchronous tasks that can be scheduled to repeat at specific intervals.
    • WorkThreadPool: Provides access to available thread pools (supports load balancing algorithms).
    • TaskQueue: A queue for std::function tasks.
    • Synchronization Primitives: Includes spin_mutex.h (for low-latency critical sections), semaphore.h (implemented via condition variables), and rwmutex.h (read-write locks).
  5. Use the NetWork module for TCP networking

    master

    The NetWork module provides the building blocks for network applications:

    • TcpServer<T>: A template class used to implement high-performance private protocol servers.
    • TcpClient: A class for implementing TCP client programs.
    • Session: A base class for implementing private protocols over TCP/UDP, designed to handle long-connection data and responses.
    • Socket: An abstraction covering TCP servers, clients, and UDP sockets.
    • sockutil: Unified wrappers for system-level network APIs.
  6. How wepoll manages epoll ports and sockets

    master

    wepoll mimics the Linux epoll behavior but adapts it to Windows primitives:

    • Epoll Ports: Instead of file descriptors, an epoll port is represented by a Windows HANDLE. Use epoll_create or epoll_create1 to create one.
    • Sockets: The library works with sockets created via socket(), WSASocket(), or accept().
    • Thread Safety: The library is fully thread-safe. Multiple threads can poll the same epoll port, and sockets can be added to multiple epoll sets.
    • Error Handling: On failure, all functions set both errno and GetLastError().
  7. Integrate wepoll into your Windows project

    master

    wepoll is a lightweight library that implements the Linux epoll API for Windows, allowing efficient socket polling for hundreds of thousands of connections.

    To use it:

    1. Include wepoll.h in your source files.
    2. Compile wepoll.c as part of your project.

    Requirements & Compatibility:

    • OS: Windows Vista or higher.
    • Compilers: MSVC, Clang, or GCC.
    • Limitations: It only works with sockets and does not support Edge-triggered (EPOLLET) mode.
  8. Compile ZLToolKit for iOS

    master

    You can compile for iOS using the provided script or by manually generating an Xcode project via CMake. When generating the project, the files will be located in the build directory.

    cd ZLToolKit
    ./build_for_ios.sh

    Or generate Xcode project manually

    cd ZLToolKit
    mkdir -p build
    cd build
    cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/iOS.cmake -DIOS_PLATFORM=SIMULATOR64 -G "Xcode"
  9. Compile ZLToolKit on Windows

    master

    Compilation on Windows requires Visual Studio 2017, OpenSSL, mysqlclient, and cmake-gui.

    Steps:

    1. Use cmake-gui to generate the VS project files.
    2. Open ZLToolKit.sln in Visual Studio 2017.
    3. Set the build configuration to Release.
    4. Build the following targets in order: ZLToolKit_static, ZLToolKit_shared, ALL_BUILD, and INSTALL.
    5. Run the test cases to verify.
    6. The installed headers and libraries will be located in the root of the source partition.
  10. Compile ZLToolKit for Android

    master

    To compile for Android, you need the Android NDK (tested with android-ndk-r14b) and must export the ANDROID_NDK_ROOT environment variable.

    cd ZLToolKit
    export ANDROID_NDK_ROOT=/path/to/ndk
    ./build_for_android.sh