From 6cf860f8d63d9dfaaaf34c45d490a384095b529c Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Mon, 24 Sep 2018 13:23:49 +0100 Subject: [PATCH] dinitctl: add utility method to extract string from cpbuffer. --- src/dinitctl.cc | 30 ++++++++++++++++++++++++++++++ src/includes/cpbuffer.h | 7 +++++++ 2 files changed, 37 insertions(+) diff --git a/src/dinitctl.cc b/src/dinitctl.cc index f568fe1..30746ab 100644 --- a/src/dinitctl.cc +++ b/src/dinitctl.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -321,6 +322,35 @@ int main(int argc, char **argv) } } +// Extract/read a string of specified length from the buffer/socket. The string is consumed +// from the buffer. +static std::string read_string(int socknum, cpbuffer_t &rbuffer, uint32_t length) +{ + int rb_len = rbuffer.get_length(); + if (rb_len >= length) { + std::string r = rbuffer.extract_string(0, length); + rbuffer.consume(length); + return r; + } + + std::string r = rbuffer.extract_string(0, rb_len); + uint32_t rlen = length - rb_len; + uint32_t clen; + do { + rbuffer.reset(); + rbuffer.fill(socknum); + char *bptr = rbuffer.get_ptr(0); + clen = rbuffer.get_length(); + clen = std::min(clen, rlen); + r.append(bptr, clen); + rlen -= clen; + } while (rlen > 0); + + rbuffer.consume(clen); + + return r; +} + // Start/stop a service static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *service_name, command_t command, bool do_pin, bool wait_for_service, bool verbose) diff --git a/src/includes/cpbuffer.h b/src/includes/cpbuffer.h index 832c3da..5273211 100644 --- a/src/includes/cpbuffer.h +++ b/src/includes/cpbuffer.h @@ -162,6 +162,13 @@ template class cpbuffer std::memcpy(buf, s, len); } } + + // reset the index and length. + void reset() + { + cur_idx = 0; + length = 0; + } }; #endif -- 2.25.1