Skip to content

Commit

Permalink
Let RRDUDSSocket read bytes
Browse files Browse the repository at this point in the history
for FETCHBIN

CMK-18929

Change-Id: I40322faa303ccb798f0bb2d00dc202ef8ea52841
  • Loading branch information
Synss committed Jan 22, 2025
1 parent 10c8757 commit 57d1ab5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/livestatus/include/livestatus/RRDUDSSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RRDUDSSocket {
void connect();
[[nodiscard]] Logger *logger() const { return logger_; }
[[nodiscard]] std::string readLine() const;
[[nodiscard]] std::string read(std::size_t count) const;
[[nodiscard]] ssize_t write(std::string_view text,
std::chrono::milliseconds timeout) const;
[[nodiscard]] bool isConnected() const { return socket_ != -1; }
Expand Down
14 changes: 14 additions & 0 deletions packages/livestatus/src/RRDUDSSocket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <sys/socket.h>
#include <sys/un.h>

#include <algorithm>
#include <array>
#include <utility>

#include "livestatus/Logger.h"
Expand Down Expand Up @@ -53,6 +55,18 @@ std::string RRDUDSSocket::readLine() const {
return mk::rstrip(answer);
}

std::string RRDUDSSocket::read(std::size_t count) const {
std::array<char, 512> answer{};
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-array-to-pointer-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
const auto rd = ::fread(answer.data(), sizeof(decltype(answer)::value_type),
std::min(count, answer.size()), file_);
if (feof(file_) != 0 || ferror(file_) != 0) {
throw generic_error("cannot read reply");
}
return {answer.begin(), answer.begin() + rd};
// NOLINTEND(cppcoreguidelines-pro-bounds-array-to-pointer-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
}

ssize_t RRDUDSSocket::write(std::string_view text,
std::chrono::milliseconds timeout) const {
if (!isConnected()) {
Expand Down

0 comments on commit 57d1ab5

Please sign in to comment.