How pigbrain/hnet works
Project type and purpose
• This is a C++ socket-library (“HNet”) for Linux, exposing non-blocking TCP client/server abstractions, an epoll-based I/O manager, connector/link management, and a command framework.
• It is an open-source tool intended to be linked into sample applications (under sample/) or other C++ projects.
File structure
• Top-level directories:
• inc/ – public headers (API surface)
• src/ – implementation files corresponding to each header
• sample/ – example client/server programs demonstrating library usage
• Key sub-domains (from file names):
• Low-level socket wrapping (HSocket, HTCPSocket, HConnector, HLink, HEpoll)
• I/O management (HIOMgr, HTCPIOMgr)
• Connection managers (HClientMgr, HServerMgr, HTCPClientMgr, HTCPServerMgr)
• Command framework (HCommand, HCommandRouter, HCommandWorker, HCommandManager, etc.)
• Concurrency primitives (HMutex, HAtomicCounter)
• Logging (HNetLog)
README
• Confirms the project is a “socket library for linux.”
a. Main components
1. Public API (HNetLib) – the entry point library that user programs link against
2. Transport layer
• Epoll Engine (HEpoll)
• Socket Abstractions (HSocket, HTCPSocket)
• Connector/Link (HConnector, HLink, HLinkMap)
3. I/O Manager
• Generic I/O Manager (HIOMgr)
• TCP-specific I/O Manager (HTCPIOMgr)
4. Connection Managers
• Client Manager (HClientMgr, HTCPClientMgr)
• Server Manager (HServerMgr, HTCPServerMgr)
5. Command Framework
• Command Descriptors & Tables (HCommandDesc, CommandTable_HNET)
• Command Routing and Workers (HCommandRouter, HCommandWorker, HCommandCommunicator, HCommandStream)
• Command Manager and Parameter Handling (HCommandManager, HCommandParameter)
6. Utilities
• Thread safety (HMutex, HAtomicCounter)
• Logging (HNetLog)
b. Relationships and data flow
• User App → links against HNetLib → calls into Connection Managers (ClientMgr/ServerMgr).
• Connection Manager registers sockets with I/O Manager → HIOMgr/HTCPIOMgr uses HEpoll to wait for events.
• Upon read/write events, HIOMgr dispatches data to HCommandCommunicator → CommandRouter → CommandWorker.
• Command results/errors propagate back through communicator to the socket/link.
• Utilities (HMutex, HAtomicCounter) protect shared state across threads; HNetLog logs events.
c. Architectural patterns
• Reactor pattern (HEpoll + HIOMgr)
• Factory/Manager pattern for clients and servers
• Command pattern for request/response handling
• Layered abstraction: transport → I/O management → command processing → public API
d. Technologies & frameworks
• C++ on Linux
• epoll system calls
• Makefiles for build (src/Makefile, sample/Makefile)
Detailed steps for the principal software engineer
2. Inside HNetLib, group 3 vertical sections: Transport, I/O Manager, Command Framework.
3. Under Transport: place HEpoll, HSocket/HTCPSocket, HConnector/HLink.
4. Under I/O Manager: HIOMgr → HTCPIOMgr.
5. Under Command Framework: HCommandManager, HCommandRouter, HCommandWorker, etc., plus descriptor/table.
6. Connection Managers between Transport and I/O Manager, showing how they wire sockets into the reactor loop.
8. Side-bars for utilities: HMutex, HAtomicCounter, HNetLog, indicating that they are used across all modules.