|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +**nanonext** is an R package providing a binding to NNG (Nanomsg Next Gen), a high-performance messaging library. The package is implemented almost entirely in C, wrapping the NNG C library to provide R interfaces for socket-based messaging, asynchronous I/O operations, and distributed computing primitives. |
| 8 | + |
| 9 | +## Core Architecture |
| 10 | + |
| 11 | +### Dual Interface Design |
| 12 | + |
| 13 | +The package provides two equivalent interfaces: |
| 14 | + |
| 15 | +1. **Functional Interface**: Socket objects passed as first argument to functions like `send()`, `recv()`, `dial()`, `listen()` |
| 16 | +2. **Object-Oriented Interface**: Nano objects with methods accessible via `$send()`, `$recv()`, etc. |
| 17 | + |
| 18 | +### Key C Source Files |
| 19 | + |
| 20 | +- `src/nanonext.h` - Main header with type definitions, macros, and function declarations |
| 21 | +- `src/core.c` - Core serialization/unserialization, hooks, and fundamental utilities |
| 22 | +- `src/proto.c` - Protocol implementations (socket opening, option handling) |
| 23 | +- `src/aio.c` - Asynchronous I/O operations and callbacks |
| 24 | +- `src/comms.c` - Send/receive operations and data marshaling |
| 25 | +- `src/sync.c` - Synchronization primitives (condition variables, mutexes) |
| 26 | +- `src/thread.c` - Thread management and concurrent operations |
| 27 | +- `src/ncurl.c` - HTTP client implementation |
| 28 | +- `src/net.c` - Network utilities (IP address retrieval) |
| 29 | +- `src/tls.c` - TLS configuration and certificate handling |
| 30 | +- `src/utils.c` - Utility functions |
| 31 | +- `src/init.c` - Package initialization and registration |
| 32 | + |
| 33 | +### Key R Source Files |
| 34 | + |
| 35 | +- `R/socket.R` - Socket creation and management (functional interface) |
| 36 | +- `R/nano.R` - Nano object creation (OO interface) |
| 37 | +- `R/sendrecv.R` - Send/receive operations |
| 38 | +- `R/aio.R` - Async I/O wrappers |
| 39 | +- `R/context.R` - Context management for concurrent operations |
| 40 | +- `R/sync.R` - Synchronization primitives |
| 41 | +- `R/ncurl.R` - HTTP client functions |
| 42 | +- `R/stream.R` - WebSocket and stream interfaces |
| 43 | +- `R/tls.R` - TLS configuration |
| 44 | + |
| 45 | +### External Dependencies |
| 46 | + |
| 47 | +The package bundles and can compile: |
| 48 | +- `libnng` v1.11.0 (messaging library) - in `src/nng/` |
| 49 | +- `libmbedtls` v3.6.2 (TLS implementation) - in `src/mbedtls/` |
| 50 | + |
| 51 | +These bundled libraries are compiled during installation if system libraries are not found or if `NANONEXT_LIBS=1` is set. |
| 52 | + |
| 53 | +## Common Development Commands |
| 54 | + |
| 55 | +### Building and Testing |
| 56 | + |
| 57 | +```r |
| 58 | +# Install package from source |
| 59 | +R CMD INSTALL . |
| 60 | + |
| 61 | +# Build package |
| 62 | +R CMD build . |
| 63 | + |
| 64 | +# Check package (run all tests and CRAN checks) |
| 65 | +R CMD check nanonext_*.tar.gz |
| 66 | + |
| 67 | +# Run tests only |
| 68 | +Rscript tests/tests.R |
| 69 | +``` |
| 70 | + |
| 71 | +### Development Workflow |
| 72 | + |
| 73 | +```r |
| 74 | +# In R session - load package for development |
| 75 | +devtools::load_all() |
| 76 | + |
| 77 | +# Build documentation |
| 78 | +devtools::document() |
| 79 | + |
| 80 | +# Run tests |
| 81 | +devtools::test() |
| 82 | + |
| 83 | +# Check package |
| 84 | +devtools::check() |
| 85 | +``` |
| 86 | + |
| 87 | +### Compilation |
| 88 | + |
| 89 | +The package uses a configure script (`configure`) that: |
| 90 | +1. Detects or compiles `libmbedtls` (requires >= 2.5.0) |
| 91 | +2. Detects or compiles `libnng` (requires >= 1.9.0) |
| 92 | +3. Generates `src/Makevars` with appropriate flags |
| 93 | + |
| 94 | +For forced compilation of bundled libraries: |
| 95 | +```bash |
| 96 | +Sys.setenv(NANONEXT_LIBS = 1) |
| 97 | +``` |
| 98 | + |
| 99 | +### Testing Notes |
| 100 | + |
| 101 | +- Tests are in `tests/tests.R` using a minimal custom testing framework |
| 102 | +- Tests cover: sockets, protocols, async I/O, serialization, HTTP client, TLS, streams, contexts |
| 103 | +- Network-dependent tests may fail if external services are unavailable |
| 104 | +- Some tests only run when `NOT_CRAN=true` or on specific platforms (Linux) |
| 105 | + |
| 106 | +## Key Concepts |
| 107 | + |
| 108 | +### Protocols |
| 109 | + |
| 110 | +Implements scalability protocols: |
| 111 | +- **Bus** (mesh): `"bus"` |
| 112 | +- **Pair** (1-to-1): `"pair"` |
| 113 | +- **Poly** (polyamorous 1-to-1): `"poly"` |
| 114 | +- **Pipeline**: `"push"`, `"pull"` |
| 115 | +- **Pub/Sub**: `"pub"`, `"sub"` |
| 116 | +- **Request/Reply**: `"req"`, `"rep"` |
| 117 | +- **Survey**: `"surveyor"`, `"respondent"` |
| 118 | + |
| 119 | +### Transports |
| 120 | + |
| 121 | +Supported URL schemes: |
| 122 | +- `inproc://` - in-process (zero-copy) |
| 123 | +- `ipc://` - inter-process |
| 124 | +- `tcp://` - TCP/IP |
| 125 | +- `ws://` - WebSocket |
| 126 | +- `wss://` - WebSocket over TLS |
| 127 | +- `tls+tcp://` - TLS over TCP |
| 128 | + |
| 129 | +### Async I/O (AIO) |
| 130 | + |
| 131 | +Async operations return "aio" objects that auto-resolve: |
| 132 | +- `send_aio()`, `recv_aio()` - async send/receive |
| 133 | +- `request()` - async request/reply |
| 134 | +- `ncurl_aio()` - async HTTP |
| 135 | +- Objects have `$data`, `$result` fields |
| 136 | +- Use `call_aio()` to wait/retrieve results |
| 137 | +- Use `unresolved()` to check status |
| 138 | + |
| 139 | +### Serialization Modes |
| 140 | + |
| 141 | +Data can be sent in multiple modes: |
| 142 | +- `mode = "serial"` or `1` - R serialization (default, handles any R object) |
| 143 | +- `mode = "raw"` or `2` - raw bytes (for atomic vectors) |
| 144 | +- `mode = "string"` or `8` - character conversion |
| 145 | +- Various receive modes for type coercion |
| 146 | + |
| 147 | +Custom serialization supported via `serial_config()` for specific object classes. |
| 148 | + |
| 149 | +### Object System |
| 150 | + |
| 151 | +The package uses R's S3 object system with heavy use of external pointers tagged with specific symbols (defined in `src/init.c`): |
| 152 | +- `nano_SocketSymbol` - Socket objects |
| 153 | +- `nano_ContextSymbol` - Context objects |
| 154 | +- `nano_AioSymbol` - AIO objects |
| 155 | +- `nano_CvSymbol` - Condition variables |
| 156 | +- etc. |
| 157 | + |
| 158 | +External pointers are stored in pairlists with finalizers for automatic cleanup. |
| 159 | + |
| 160 | +## Important Implementation Details |
| 161 | + |
| 162 | +### Memory Management |
| 163 | + |
| 164 | +- C code uses `NANO_ALLOC`, `NANO_FREE` macros for buffer management |
| 165 | +- R objects protected via `nano_precious` environment (prevents GC) |
| 166 | +- Finalizers registered for cleanup of C resources |
| 167 | +- Careful use of `PROTECT`/`UNPROTECT` in C code |
| 168 | + |
| 169 | +### Thread Safety |
| 170 | + |
| 171 | +- Package implements its own threading via NNG's platform layer |
| 172 | +- Condition variables (`cv()`) for cross-thread signaling |
| 173 | +- Mutexes used internally for shared state |
| 174 | +- R interrupt handling on both Unix (via `R_interrupts_pending`) and Windows |
| 175 | + |
| 176 | +### Error Handling |
| 177 | + |
| 178 | +- NNG errors returned as integer error codes |
| 179 | +- `mk_error()` creates error values of class `"errorValue"` |
| 180 | +- Error messages via `nng_strerror()` |
| 181 | +- Use `ERROR_OUT()` macro to error, `ERROR_RET()` to return error value |
| 182 | + |
| 183 | +### Configuration Options |
| 184 | + |
| 185 | +Set/get socket/dialer/listener options via: |
| 186 | +- `opt(object, "option-name")` - get |
| 187 | +- `opt(object, "option-name") <- value` - set |
| 188 | + |
| 189 | +Options are protocol-specific (e.g., `"req:resend-time"`, `"sub:prefnew"`). |
| 190 | + |
| 191 | +## Build System |
| 192 | + |
| 193 | +- `configure` - POSIX shell script for Unix-like systems |
| 194 | +- `configure.win` - Windows configuration |
| 195 | +- `configure.ucrt` - UCRT-specific Windows config |
| 196 | +- Requires `cmake` if compiling bundled libraries (no longer requires `xz` as libraries are pre-extracted) |
| 197 | +- Environment variables: `INCLUDE_DIR`, `LIB_DIR`, `NANONEXT_LIBS` |
| 198 | + |
| 199 | +## Documentation |
| 200 | + |
| 201 | +- Roxygen2-based documentation in R files |
| 202 | +- Main vignette: `vignettes/nanonext.Rmd` |
| 203 | +- Website: https://nanonext.r-lib.org |
| 204 | +- Generated with pkgdown |
| 205 | + |
| 206 | +## Code Style |
| 207 | + |
| 208 | +- C code follows NNG conventions with `nano_` prefixes for package functions |
| 209 | +- R code uses snake_case for functions |
| 210 | +- Clear separation between user-facing and internal functions (`.function` prefix for internal) |
| 211 | +- Extensive use of macros for code clarity (`NANO_PTR`, `NANO_DATAPTR`, etc.) |
0 commit comments