refactor(daemon): share the output ring-window helper
The embedded daemon and the xmrig manager each kept their own copy of the console output ring: append new bytes, trim the front to a cap at a newline boundary, and hand a reader everything since its offset. The two copies had drifted. The daemon tracked an absolute byte total so a reader's offset stayed valid across a front-trim, while the miner keyed its reader off the live buffer size, so a trim could silently rewind the console feed. - The append/trim and the since-offset logic now live in one small header, outputWindowSince and appendBounded, included by both. The trim keeps the newline-boundary cut and the guard that stops a lone trailing newline from erasing the whole buffer. - The xmrig manager now carries the same absolute byte total, so its reader resolves against the window the same way and survives a trim (and a process restart that clears the buffer but keeps the total). Tests cover feeding the ring well past the trim point while a reader polls each chunk (the feed is continuous and never dies at the trim), the lone-trailing-newline guard, and a cleared buffer with a retained total. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,8 @@
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
#include "output_window.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
@@ -106,13 +108,7 @@ public:
|
||||
*/
|
||||
std::string getOutputSince(size_t& offset) const {
|
||||
std::lock_guard<std::mutex> lk(output_mutex_);
|
||||
if (offset >= process_output_.size()) {
|
||||
offset = process_output_.size();
|
||||
return {};
|
||||
}
|
||||
std::string result = process_output_.substr(offset);
|
||||
offset = process_output_.size();
|
||||
return result;
|
||||
return outputWindowSince(process_output_, output_total_, offset);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,6 +165,7 @@ private:
|
||||
|
||||
mutable std::mutex output_mutex_;
|
||||
std::string process_output_;
|
||||
std::size_t output_total_ = 0; // bytes ever appended (absolute end; guarded by output_mutex_, kept across restarts)
|
||||
|
||||
// xmrig HTTP API credentials (random per session)
|
||||
int api_port_ = 0;
|
||||
|
||||
Reference in New Issue
Block a user