How gooddbird/tinyrpc works
Project type and purpose
• tinyrpc is an open-source, header-only C++11 framework for building high-performance, asynchronous RPC services.
• It provides:
• A core runtime (logging, coroutines, reactor, TCP, HTTP, TinyPB codec)
• An RPC stub/dispatcher layer for Protobuf-based and HTTP−based RPC
• A code generator (tinyrpc_generator.py) that scaffolds new RPC services from .proto files
• Examples and test cases under testcases/
File structure
• Top‐level directories:
• tinyrpc/comm – common utilities (config, async‐logging, thread pool, error codes, string utils)
• tinyrpc/coroutine – libco‐based coroutine abstraction, hooks, coroutine pool
• tinyrpc/net – Reactor wrapper, TCP server/client, HTTP codec/dispatcher, TinyPB codec/dispatcher
• generator – Python script + templates for scaffolding new services/clients
• testcases – example protocols, servers, clients, unit tests
• conf – XML config samples (TinyPB and HTTP server)
• .devcontainer, Dockerfile – development environment setup
• build.sh, CMakeLists.txt, makefile – build scripts (CMake and Makefile)
• Configuration files: XML under conf/ parsed via TinyXML
• Build scripts: build.sh invokes CMake; top-level makefile drives Makefile install/uninstall
README for architectural insights
• Core modules described in “概要设计” (Section 5)
• Indicates use of:
• Asynchronous logging (producer/consumer thread)
• m:n thread:coroutine model
• Reactor pattern (epoll under the hood)
• TCPServer / TCPConnection abstractions
• TinyPB (custom Protobuf binary protocol)
• HTTP Servlet interface
Main components & their relationships
• Protobuf library (serialization of business messages)
• TinyXML (XML config parsing)
• libco (underlying coroutine swap routines)
• Docker/DevContainer (optional development environment)
A. Configuration & Bootstrapping
• InitConfig(xml) → populates global settings
• StartRpcServer() → spins up I/O threads, Reactor, thread pool
B. Asynchronous Logging
• Logging macros (DebugLog, AppInfoLog)
• Log producer buffers → Async logger thread → file rolling
C. Coroutine Scheduler
• Hooks: connect(), read(), write() redirect to non-blocking + yield
• Coroutine Pool: coctx, coroutine.cc, coroutine_pool
D. Reactor
• fd_event registers events on epoll; dispatches readiness callbacks
• Tied to coroutines: on I/O readiness, resume appropriate coroutine
E. TCP Layer
• TcpServer listens, accepts → spawns per-connection coroutine
• TcpConnection reads raw bytes, hands off to appropriate codec
F. Protocol Codecs & Dispatchers
• TinyPB module: tinypb_codec, tinypb_rpc_channel, tinypb_rpc_dispatcher
• HTTP module: http_codec, http_dispatcher, HttpServlet plug-in
G. RPC Layer
• Business services inherit generated Service interfaces (from Protobuf)
• REGISTER_SERVICE() or REGISTER_HTTP_SERVLET() hooks implementation
H. Code Generator
• generator/tinyrpc_generator.py + templates/
• Produces skeleton: interface/, service/, main.cc, makefile, test_client
Architectural patterns & design principles
• Reactor pattern for scalable I/O multiplexing (epoll)
• m:n threading with cooperative coroutines for non-blocking concurrency
• Asynchronous producer/consumer logging
• Layered separation: config → logging → coroutine → reactor → transport → codec → dispatch → business
• Template-based code generation for rapid service scaffolding
Essential architectural elements
• M:n model: multiple IO threads share a central coroutine queue
• Asynchronous logging thread separate from I/O threads
• “REGISTER_SERVICE” and “REGISTER_HTTP_SERVLET” as plug-in registration points