How butianyoushu/libhv works
Project Type and Purpose
• libhv is an open-source, cross-platform C/C++ networking and event-loop library—similar to libevent/libuv but with richer built-in protocols (HTTP, WebSocket, MQTT, RPC) and simplified APIs. It provides non-blocking IO, timers, thread-safety, SSL/TLS, RUDP (KCP), and utilities (logging, JSON, INI, file IO, etc.) for building high-performance servers and clients.
File-Structure Analysis
• Top-level build and config: BUILD.bazel, CMakeLists.txt, Makefile, scripts/, .github/, WORKSPACE.bazel
• Core modules:
• base/ — core abstractions (threads, mutex, atomic, sockets, logging, error, time)
• event/ — EventLoop and OS adapters (epoll, kqueue, IOCP, wepoll, NIO) plus custom/timer/signal events, KCP support
• http/ — HTTP/1.x & HTTP/2 parser, HTTP client/server, WebSocketChannel, middleware, router
• ssl/ — Pluggable SSL/TLS back-ends (OpenSSL, GNUTLS, MBEDTLS, WinTLS, NoSSL)
• protocol/ — Additional protocol parsers/servers (DNS, FTP, ICMP, SMTP)
• mqtt/ — MQTT client implementation
• util/ & cpputil/ — Misc utilities: base64, MD5/SHA1, file path, thread-pool, URL, INI parser, LRU cache, object pool, RAII wrappers
• C++ wrapper: evpp/ provides higher-level C++ classes around hv core.
• Examples, echo-servers, unittest: illustrate usage and benchmark performance.
Key Insights from README
• Cross-platform support (Linux, Windows, macOS, Android, iOS, BSD, Solaris)
• Feature list emphasizes EventLoop, TCP/UDP clients & servers, HTTP/2 & gRPC, WebSocket, MQTT, multi-thread safety, SSL, unpacking modes, RUDP.
• Getting-started includes sample httpd, curl, wrk tools—demonstrates layering of application on libhv.
• CORE:
• EventLoop
• OS-specific Pollers (epoll, kqueue, IOCP, WePoll, select/poll)
• Timer, Idle, Custom Event, Signal modules
• TRANSPORT:
• SocketChannel abstraction (wraps TCP/UDP sockets)
• ThreadPool for multi-threaded dispatch
• PROTOCOL LAYERS:
• TCP/UDP Server & Client APIs
• HTTP Module (Parser, Server, Client, Router, Middleware)
• WebSocket Module (over HTTP handshake + frame parser)
• MQTT Module
• RPC & other protocol handlers (DNS, FTP, ICMP, SMTP)
• RUDP/KCP
• SECURITY:
• SSL/TLS Abstraction (pluggable back-ends)
• UTILITIES:
• Logging, Config parser, JSON (header-only), Buffer, LRUCache, Path/File IO, ObjectPool, RAII
• APPLICATION:
• Example executables (httpd, curl, wrk, echo servers)
• BUILD & CI:
• Bazel, CMake, Makefiles, scripts, CI workflows
1. External Network → OS Kernel Socket → OS Poller
2. OS Poller → EventLoop → SocketChannel
3. SocketChannel → Protocol Parser (e.g., HTTPParser, WebSocketParser)
4. Protocol Handler → User Callback (application logic)
5. User Callback → Response Generator → EventLoop → OS Socket → External Network
6. Timer Signals and Custom Events flow into EventLoop similarly
7. For SSL: SocketChannel wraps SSL BIO → encrypted data flows through SSL stack → EventLoop
c. Architectural Patterns & Design Principles
• Reactor pattern (EventLoop + non-blocking IO)
• Layered protocol stack (transport → protocol → application)
• Pluggable modules (SSL back-ends, unpacking modes, protocol handlers)
• Thread-pool integration for concurrency
• Cross-platform abstraction: OS-conditional adapters hidden behind consistent APIs
d. Key Technologies & Frameworks
• C/C++11, header-only JSON, zlib (optional), OpenSSL/GNUTLS/MBEDTLS
• Build: Bazel, CMake, Make, vcpkg, xmake
• OS APIs: epoll (Linux), kqueue (BSD/macOS), IOCP (Windows), WePoll (Windows), select/poll