Bitchat C++ follows a modular service-based architecture designed for maintainability, testability, and extensibility. The system is organized into distinct layers with clear separation of concerns, using a service-oriented approach for better modularity.
The core layer contains the main orchestrators and business logic:
- Purpose: Main orchestrator that coordinates all components
- Responsibilities:
- Initialize and manage all services
- Provide high-level API for the application
- Handle lifecycle management (start/stop)
- Coordinate callbacks between components
- Dependencies: All services (NetworkService, MessageService, CryptoService, NoiseService)
- Purpose: Core data structures and constants
- Responsibilities:
- Define application constants
- Core data types
- Configuration structures
The service layer provides modular, focused functionality:
- Purpose: Manages network operations and peer discovery
- Responsibilities:
- Bluetooth interface management
- Peer discovery and tracking
- Packet routing and relay
- Network state management
- Dependencies: IBluetoothNetwork, BluetoothAnnounceRunner, CleanupRunner
- Purpose: Handles chat messages and conversation state
- Responsibilities:
- Message creation and processing
- Channel management
- Message history
- Private messaging
- Dependencies: NetworkService, CryptoService, NoiseService
- Purpose: Cryptographic operations and key management
- Responsibilities:
- Key pair generation/loading
- Digital signatures (Ed25519)
- Signature verification
- Random number generation
- Dependencies: OpenSSL
- Purpose: Noise protocol implementation for secure communication
- Responsibilities:
- Session management
- Handshake protocol
- Encryption/decryption
- Key exchange
- Dependencies: Noise protocol library
- Purpose: Identity and authentication management
- Responsibilities:
- User identity management
- Authentication state
- Identity verification
- Dependencies: CryptoService
Platform-specific implementations and abstractions:
- Purpose: Abstract interface for Bluetooth operations
- Responsibilities:
- Device discovery
- Connection management
- Data transmission
- Platform abstraction
- Purpose: Factory for creating platform-specific Bluetooth implementations
- Responsibilities:
- Platform detection
- Interface instantiation
Protocol definitions and serialization:
- BitchatPacket: Core packet structure
- BitchatMessage: Chat message structure
- PacketSerializer: Serialization utilities
- Purpose: Message padding for security
- Responsibilities:
- Add/remove padding
- Padding validation
Advanced cryptographic protocol implementation:
- Purpose: Noise protocol implementation
- Responsibilities:
- Protocol state management
- Handshake patterns
- Key exchange
- Purpose: Session management for Noise protocol
- Responsibilities:
- Session state
- Encryption/decryption
- Session lifecycle
- Purpose: Define roles in Noise protocol
- Responsibilities:
- Initiator/responder roles
- Role resolution
User interface abstractions:
- Purpose: Abstract interface for UI implementations
- Responsibilities:
- Message display
- User input handling
- Status updates
- Event callbacks
- Purpose: Console-based user interface
- Responsibilities:
- Terminal-based interaction
- Command parsing
- Text-based display
- Purpose: Testing and headless operation
- Responsibilities:
- Minimal UI for testing
- Automated operation
Background task management:
- Purpose: Periodic peer announcements
- Responsibilities:
- Broadcast presence
- Keep-alive messages
- Network discovery
- Purpose: Cleanup stale connections and data
- Responsibilities:
- Remove stale peers
- Cleanup expired sessions
- Memory management
Utility functions and helpers:
- Purpose: Data compression utilities
- Responsibilities:
- LZ4 compression/decompression
- Compression decision logic
- Purpose: Date and time utilities
- Responsibilities:
- Timestamp handling
- Time formatting
- Purpose: Noise protocol utilities
- Responsibilities:
- Protocol helpers
- Key management utilities
- Purpose: Protocol utilities
- Responsibilities:
- Packet validation
- Protocol compliance
- Purpose: String manipulation utilities
- Responsibilities:
- String formatting
- Text processing
graph TD
A[BitchatManager] --> B[NetworkService]
A --> C[MessageService]
A --> D[CryptoService]
A --> E[NoiseService]
A --> F[IdentityService]
A --> G[IUserInterface]
B --> H[IBluetoothNetwork]
B --> I[BluetoothAnnounceRunner]
B --> J[CleanupRunner]
C --> B
C --> D
C --> E
E --> K[NoiseProtocol]
E --> L[NoiseSession]
H --> M[Apple Bluetooth]
H --> N[Linux Bluetooth]
O[PacketSerializer] --> P[BitchatPacket]
O --> Q[BitchatMessage]
- BitchatManager receives send request
- MessageService creates message and packet
- CryptoService signs the packet
- NoiseService encrypts the payload
- CompressionHelper compresses if beneficial
- NetworkService sends via Bluetooth
- IBluetoothNetwork transmits to peers
- IBluetoothNetwork receives packet
- NetworkService processes and routes packet
- MessageService parses and validates message
- CryptoService verifies signature
- NoiseService decrypts the payload
- CompressionHelper decompresses if needed
- BitchatManager notifies UI via callback
- Main Thread: API calls and state management
- Announce Thread: Periodic peer announcements (BluetoothAnnounceRunner)
- Cleanup Thread: Stale peer removal (CleanupRunner)
- Main Thread: Message processing and history management
- Background: Async operations (if needed)
- Platform-specific: Bluetooth event handling
- Main Thread: API calls and state management
- All public methods are exception-safe
- RAII for resource management
- Graceful degradation on errors
- Return codes for recoverable errors
- Exceptions for unrecoverable errors
- Logging at appropriate levels
- Platform detection (Apple/Linux/Windows)
- Feature flags
- Compiler-specific optimizations
- Bluetooth settings
- Crypto parameters
- Compression thresholds
- Each service can be tested independently
- Mock interfaces for dependencies
- Protocol compliance testing
- End-to-end message flow
- Cross-platform compatibility
- Performance benchmarks
- Message encryption (implemented via Noise protocol)
- File sharing
- Voice messages
- Group channels
- Easy to add new features
- Platform independence
- Testable components
- Clear separation of concerns
- Service-oriented design
- RAII for automatic cleanup
- Smart pointers for ownership
- Efficient data structures
- Compression for large messages
- Connection pooling
- Bandwidth monitoring
- Lock-free operations where possible
- Minimal critical sections
- Async operations for I/O