Files
leonarski_fandClaude Opus 5.5 3047ccd13e Receivers and pullers: comments as plain sentences, with the reasons put back
Rewrites the one-line comments that did not parse (JFJochReceiverStream::ExpectedImages,
ImagePuller's ring and republish cap) and restores what the comment-shortening pass
dropped: the ZeroMQ pusher drops an image after one second, so a ring too small
costs images; RugnuxStructure::Map's margin where the model reaches beyond the
cell, and the header's mean and RMS being the cell's; RugnuxLiveStatus' time base,
the START message preceding the first image by start_to_first_image_s, and the
frame-rate formula. Code does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK
2026-11-08 09:56:11 +01:00

84 lines
3.0 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <atomic>
#include <chrono>
#include <string>
#include <memory>
#include <vector>
#include "../common/ZMQWrappers.h"
#include "../frame_serialize/CBORStream2Deserializer.h"
#include "../common/ThreadSafeFIFO.h"
#include "../common/JfjochTCP.h"
#include "ReceiveRingBuffer.h"
struct PullerAckMessage {
TCPFrameType ack_for = TCPFrameType::DATA;
bool ok = true;
bool fatal = false;
uint64_t run_number = 0;
uint32_t socket_number = 0;
uint64_t image_number = 0;
uint64_t processed_images = 0;
TCPAckCode error_code = TCPAckCode::None;
std::string error_text;
};
struct RawFrame {
TcpFrameHeader header{};
bool end = false;
};
struct ImagePullerOutput {
// The message as it came off the socket, in the ring buffer; held there as long as any copy of this struct lives
std::shared_ptr<ReceiveBufferRegion> raw;
std::shared_ptr<RawFrame> tcp_msg;
std::shared_ptr<CBORStream2DeserializerOutput> cbor;
// When the message came off the socket, before it was queued or deserialised
std::chrono::steady_clock::time_point received{};
};
struct ImagePullerStatistics {
size_t outside_fifo_max_util = 0;
};
class ImagePuller {
protected:
ThreadSafeFIFO<ImagePullerOutput> outside_fifo; // assuming 1 file is <1000 files, this should be enough to buffer
// Holds every received message until it is consumed. The queues count messages, the ring bounds bytes.
// With size 0 there is no ring: messages are held on the heap, bounded by the queues alone.
ReceiveRingBuffer ring;
// Bytes republish holds now. Republish keeps the oldest unsent images and the ring is reclaimed in arrival order,
// so a slow republish consumer would pin everything received after them. That is why it is capped at a share of the ring.
std::atomic<size_t> repub_bytes{0};
bool RepubCanHold(size_t bytes) const {
return ring.GetCapacity() == 0 || repub_bytes.load() + bytes <= ring.GetCapacity() / 4;
}
public:
constexpr static size_t DefaultQueueSize = 1000;
constexpr static auto PollTimeout = std::chrono::milliseconds(100);
explicit ImagePuller(size_t fifo_size = DefaultQueueSize, size_t ring_size = 0);
virtual ~ImagePuller() = default;
[[nodiscard]] std::optional<ImagePullerOutput> PollImage(std::chrono::microseconds timeout = PollTimeout);
[[nodiscard]] size_t GetMaxFifoUtilization() const;
[[nodiscard]] size_t GetCurrentFifoUtilization() const;
[[nodiscard]] size_t GetRingBufferCapacity() const;
[[nodiscard]] size_t GetMaxRingBufferUtilization() const;
virtual bool SupportsAck() const { return false; }
virtual bool SendAck(const PullerAckMessage &) { return false; }
virtual void Disconnect() = 0;
// Images the puller had no room for, which the receiver therefore never gets.
virtual size_t GetDroppedImages() const { return 0; }
};