Overview
TheBuffer module provides a thread-safe circular buffer implementation for managing multiple streams of game data. Each thread maintains its own read position, allowing concurrent access to the shared buffer without blocking.
Location: lib/common/buffer.rb
Stream Constants
The Buffer uses bitmask flags to identify different types of data streams:| Constant | Value | Description |
|---|---|---|
DOWNSTREAM_STRIPPED | 1 | Game output with ANSI/formatting stripped |
DOWNSTREAM_RAW | 2 | Raw game output with all formatting |
DOWNSTREAM_MOD | 4 | Modified downstream data (post-hook) |
UPSTREAM | 8 | Commands sent to the game server |
UPSTREAM_MOD | 16 | Modified upstream commands (post-hook) |
SCRIPT_OUTPUT | 32 | Output from scripts |
Module Methods
gets
Blocking read that waits for the next line matching the current thread’s stream filter.The next line object from the buffer matching the current stream filter. Blocks until a matching line is available.
- Automatically initializes the thread’s read position if first access
- Blocks (sleeps 0.05s intervals) while waiting for data
- Filters lines based on the thread’s stream mask
- Returns the first line where
(line.stream & thread_streams) != 0
gets?
Non-blocking read that returns the next available line ornil.
The next line object matching the stream filter, or
nil if no data is available- Same as
getsbut returnsnilimmediately if no matching data is available - Does not block or sleep
- Useful for polling or checking for data without waiting
clear
Read and remove all pending lines for the current thread.Array of all unread lines matching the current thread’s stream filter
- Advances the thread’s read position to the end of the buffer
- Returns all matching lines that were pending
- Useful for catching up or discarding old data
rewind
Reset the current thread’s read position to the beginning of the buffer.Returns self for method chaining
- Moves the thread’s index back to the buffer offset (oldest available line)
- Subsequent reads will re-read from the beginning
- Does not affect other threads’ positions
update
Add a new line to the buffer (typically called by core framework).Line object to add to the buffer
Optional stream bitmask to set on the line. If not provided, uses the line’s existing stream value.
Returns self for method chaining
- Duplicates and freezes the line before storing
- Maintains circular buffer by removing oldest lines when max size (3000) is exceeded
- Thread-safe via mutex synchronization
streams
Get the current thread’s stream filter mask.Current stream bitmask for the thread (default:
DOWNSTREAM_STRIPPED)streams=
Set the current thread’s stream filter mask.Bitmask specifying which streams to read. Must be a valid integer with at least one of the 6 stream bits set.
- Validates that the value is an integer with valid stream bits
- Raises an error if the mask is invalid (0 or outside valid range)
- Only affects the current thread’s reads
cleanup
Remove thread indexes for terminated threads (housekeeping).Returns self for method chaining
- Removes dead thread entries from internal indexes
- Called periodically by the framework
- Prevents memory leaks from thread churn
Examples
Basic Reading
Non-Blocking Check
Stream Filtering
Multiple Streams
Clearing Old Data
Re-reading Data
Thread Safety
The Buffer module is fully thread-safe:- Each thread maintains its own read position
- Multiple threads can read simultaneously without interference
- Internal state is protected by a mutex
- Lines are frozen before storage to prevent modification
Buffer Size
The buffer maintains a maximum of 3000 lines. When this limit is exceeded:- Oldest lines are automatically removed
- The global offset is incremented
- Thread positions are adjusted to stay valid
- Threads that fall too far behind are reset to the current offset
