How ithewei/libhv works
Project Type and Purpose
• libhv is a cross-platform, high-performance networking library written in C/C++.
• It exposes an event-driven API (event loop, non-blocking IO, timers) and layered protocol support (TCP/UDP, HTTP/1.x & HTTP/2, WebSocket, MQTT, RUDP/KCP, SSL/TLS).
• It is intended as an embeddable middleware for building servers, clients, and proxies in C/C++.
File-Structure Analysis
• Top-level build and config: BUILD.bazel, CMakeLists.txt, Makefile, scripts/, etc.
• Core library modules:
• base/: low-level abstractions (threading, atomics, socket wrappers, logging, memory, queues)
• event/: event loop implementation and IO pollers (epoll, kqueue, iocp, select, custom watches)
• http/: HTTP protocol stack (parsers, client, server, routers, middleware)
• ssl/: pluggable SSL/TLS back-ends
• protocol/: other protocols (DNS, FTP, SMTP, ICMP)
• mqtt/: MQTT client
• util/: helper algorithms (base64, MD5, SHA1)
• Example layers: evpp/ (C++ wrapper/API), examples/, echo-servers/, unittest/, docs/, html/
Architectural Components
• OS Abstraction Layer
• System calls, thread, mutex, atomics, time – implemented in base/ and hplatform.h
• Event Loop Core
• hv::EventLoop, pollers (epoll, kqueue, iocp) – event/hloop.*
• Non-Blocking IO Layer
• SocketChannel, watchers – event/hsocket.*, event/iowatcher.h
• Protocol Modules (layered on top of IO)
• TCP/UDP client & server – base socket wrappers, event loop
• HTTP stack – parsers (Http1Parser, Http2Parser), HttpServer, HttpClient, middleware
• WebSocket – framing, channels
• MQTT – packet parser, client
• RUDP (KCP) – ikcp integration
• Other protocols (DNS, FTP, SMTP)
• SSL/TLS Pluggable Backend
• Openssl, GnuTLS, mbedtls adapters under ssl/
• C++ Wrappers (evpp/)
• Clean C++ classes: Channel, EventLoop, TcpServer, HttpService
• Application-Facing API
• exposed headers (hv.h, hv_http.h, hv_ssl.h, etc.)
• Build & Deployment
• CMake, Bazel, Make, vcpkg/xrepo
Relationships & Data Flows
• Protocol modules invoking callbacks into user code.
• How SSL wraps socket channels transparently.
• C++ evpp wrappers calling the C core.
Architectural Patterns & Principles
• Layered Architecture: separation of concerns from OS to high-level API.
• Event-Driven Callback Pattern: non-blocking IO dispatch, user registers onConnection/onMessage handlers.
• Adapter Pattern: SSL back-ends; KCP integration.
• Modular Design: each protocol module sits in its own directory, with clear public headers.