How zhllxt/asio2 works
Project Type and Purpose
• This is an open-source, header-only C++ network library (“asio2”) built on top of standalone Asio (or Boost.Asio).
• It provides a unified API for TCP, UDP (including reliable KCP), HTTP/HTTPS, WebSocket/WSS, RPC, ICMP (ping), serial port I/O, timers, and utility functions (e.g., encryption, base64, event dispatching).
High-Level Structure from File Tree
• Top-level directories:
• /asio2 – the public API (core base modules, protocol modules, components)
• 3rd – bundled third-party libraries (asio, cereal, fmt, openssl, spdlog)
• example – usage examples for each protocol/service
• test – unit & benchmark tests
• doc – tutorials and design notes
• Core base under include/asio2/base: session management, servers, clients, iopools, timers, logging, error handling.
• Protocol layers under include/asio2: tcp/, udp/, http/, websocket/, rpc/, mqtt/, icmp/, serial_port/, proxy/ etc.
• Components under include/asio2/component: reusable modules like socks5, rdc, etc.
• Utilities under include/asio2/util: crypto, compression, threading, event dispatcher.
Key Architectural Patterns
• Layered modules:
• External dependencies (Asio, OpenSSL, cereal, fmt, spdlog)
• Core engine (I/O context pool, server/client/session abstractions, timers, events)
• Protocol adapters (specialized send/receive, framing, handlers)
• Components (higher-level features: RPC invokers, SOCKS5, publish/subscribe)
• Examples and tests (demonstrating composition of layers)
• Inversion-of-Control: users bind callbacks (connect, recv, disconnect).
• Composition over inheritance: protocol modules compose core base rather than inherit heavy frameworks.
• Header-only, template-driven extensibility: all modules compile in client code, no separate linking.
Main Components and Interactions
• External libs → provide low-level I/O, serialization, logging, crypto.
• Core Engine (asio2::iopool, server, client, session, timer, condition_event)
• Manages Asio’s io_context threads, dispatches async operations, ensures orderly start/stop, safe shutdown.
• Protocol Modules
• TCP/TCPS (SSL), UDP/KCP, HTTP/HTTPS, WebSocket/WSS, RPC/RPCS, MQTT, ICMP, Serial Port.
• Each module exposes server/client/session classes that adapt core base for protocol semantics (framing, headers).
• Components
• e.g., socks5, rdc: plug into core engine and protocol modules to add functions (authentication, relaying).
• Utilities
• Encryption (AES, DES, Base64), compression (zlib), event dispatcher, reflection, UUID, INI parser, thread pool.
• Examples & Tests connect everything and demonstrate typical data flow: Client → Server → Session → Protocol handler → User callback.