Module: check_mk
Branch: master
Commit: 2e0b6f249932b41a575efb1c1980284b3dcaad22
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2e0b6f249932b4…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed Jan 20 16:19:25 2016 +0100
Cleanup: Use a scoped enumeration instead of the traditional integral/CPP mess.
---
livestatus/src/InputBuffer.cc | 42 +++++++++++++++++++-------------------
livestatus/src/InputBuffer.h | 45 +++++++++++++++++++----------------------
livestatus/src/Store.cc | 6 +++---
3 files changed, 45 insertions(+), 48 deletions(-)
diff --git a/livestatus/src/InputBuffer.cc b/livestatus/src/InputBuffer.cc
index 3a385d7..bb88846 100644
--- a/livestatus/src/InputBuffer.cc
+++ b/livestatus/src/InputBuffer.cc
@@ -64,7 +64,7 @@ void InputBuffer::setFd(int fd)
// (and maybe more). If this method returns IB_REQUEST_READ
// then you can subsequently retrieve the lines of the
// request with nextLine().
-int InputBuffer::readRequest()
+InputBuffer::Result InputBuffer::readRequest()
{
// Remember when we started waiting for a request. This
// is needed for the idle_timeout. A connection may
@@ -100,16 +100,16 @@ int InputBuffer::readRequest()
// further data into the buffer.
if (_write_pointer < _end_pointer)
{
- int rd = readData(); // tries to read in further data into buffer
- if (rd == IB_TIMEOUT) {
+ Result rd = readData(); // tries to read in further data into buffer
+ if (rd == Result::timeout) {
if (query_started) {
logger(LG_INFO, "Timeout of %d ms exceeded while reading
query", g_query_timeout_msec);
- return IB_TIMEOUT;
+ return Result::timeout;
}
// Check if we exceeded the maximum time between two queries
else if (timeout_reached(&start_of_idle, g_idle_timeout_msec)) {
logger(LG_INFO, "Idle timeout of %d ms exceeded. Going to
close connection.", g_idle_timeout_msec);
- return IB_TIMEOUT;
+ return Result::timeout;
}
}
@@ -117,27 +117,27 @@ int InputBuffer::readRequest()
// read an incomplete line. If the last thing we read was
// a linefeed, then we consider the current request to
// be valid, if it is not empty.
- else if (rd == IB_END_OF_FILE && r == _read_pointer /* currently
at beginning of a line */)
+ else if (rd == Result::eof && r == _read_pointer /* currently at
beginning of a line */)
{
if (_requestlines.empty()) {
- return IB_END_OF_FILE; // empty request -> no request
+ return Result::eof; // empty request -> no request
}
else {
// socket has been closed but request is complete
- return IB_REQUEST_READ;
+ return Result::request_read;
// the current state is now:
// _read_pointer == r == _write_pointer => buffer is empty
// that way, if the main program tries to read the
- // next request, it will get an IB_UNEXPECTED_END_OF_FILE
+ // next request, it will get an IB_UNEXPECTED_EOF
}
}
// if we are *not* at an end of line while reading
// a request, we got an invalid request.
- else if (rd == IB_END_OF_FILE)
- return IB_UNEXPECTED_END_OF_FILE;
+ else if (rd == Result::eof)
+ return Result::unexpected_eof;
// Other status codes
- else if (rd == IB_SHOULD_TERMINATE)
+ else if (rd == Result::should_terminate)
return rd;
}
// OK. So no space is left in the buffer. But maybe at the
@@ -158,7 +158,7 @@ int InputBuffer::readRequest()
// buffer is full, but still no end of line found => buffer is too small
else {
logger(LG_INFO, "Error: maximum length of request line
exceeded");
- return IB_LINE_TOO_LONG;
+ return Result::line_too_long;
}
}
else // end of line found
@@ -167,10 +167,10 @@ int InputBuffer::readRequest()
_read_pointer = r + 1;
// Was ist, wenn noch keine korrekte Zeile gelesen wurde?
if (_requestlines.size() == 0) {
- return IB_EMPTY_REQUEST;
+ return Result::empty_request;
}
else
- return IB_REQUEST_READ;
+ return Result::request_read;
}
else { // non-empty line: belongs to current request
storeRequestLine(_read_pointer, r - _read_pointer);
@@ -184,7 +184,7 @@ int InputBuffer::readRequest()
// read at least *some* data. Return IB_TIMEOUT if that
// lasts more than g_query_timeout_msec msecs.
-int InputBuffer::readData()
+InputBuffer::Result InputBuffer::readData()
{
struct timeval start;
gettimeofday(&start, NULL);
@@ -193,7 +193,7 @@ int InputBuffer::readData()
while (!*_termination_flag)
{
if (timeout_reached(&start, g_query_timeout_msec))
- return IB_TIMEOUT;
+ return Result::timeout;
tv.tv_sec = READ_TIMEOUT_USEC / 1000000;
tv.tv_usec = READ_TIMEOUT_USEC % 1000000;
@@ -207,18 +207,18 @@ int InputBuffer::readData()
if (retval > 0 && FD_ISSET(_fd, &fds)) {
ssize_t r = read(_fd, _write_pointer, _end_pointer - _write_pointer);
if (r < 0) {
- return IB_END_OF_FILE;
+ return Result::eof;
}
else if (r == 0) {
- return IB_END_OF_FILE;
+ return Result::eof;
}
else {
_write_pointer += r;
- return IB_DATA_READ;
+ return Result::data_read;
}
}
}
- return IB_SHOULD_TERMINATE;
+ return Result::should_terminate;
}
diff --git a/livestatus/src/InputBuffer.h b/livestatus/src/InputBuffer.h
index 58a7dee..7b0e00a 100644
--- a/livestatus/src/InputBuffer.h
+++ b/livestatus/src/InputBuffer.h
@@ -25,26 +25,32 @@
#ifndef InputBuffer_h
#define InputBuffer_h
-#include "config.h" // IWYU pragma: keep
+#include "config.h" // IWYU pragma: keep
#include <deque>
#include <string>
+#define IB_BUFFER_SIZE 65536
-#define IB_REQUEST_READ 0
-#define IB_DATA_READ 1
-#define IB_NO_MORE_REQUEST 2
-#define IB_UNEXPECTED_END_OF_FILE 3
-#define IB_SHOULD_TERMINATE 4
-#define IB_LINE_TOO_LONG 5
-#define IB_END_OF_FILE 6
-#define IB_EMPTY_REQUEST 7
-#define IB_TIMEOUT 8
-
-#define IB_BUFFER_SIZE 65536
+class InputBuffer {
+public:
+ enum class Result {
+ request_read,
+ data_read,
+ unexpected_eof,
+ should_terminate,
+ line_too_long,
+ eof,
+ empty_request,
+ timeout
+ };
+ explicit InputBuffer(int *termination_flag);
+ void setFd(int fd);
+ Result readRequest();
+ bool moreLines() { return !_requestlines.empty(); }
+ std::string nextLine();
-class InputBuffer
-{
+private:
int _fd;
int *_termination_flag;
typedef std::deque<std::string> _requestlines_t;
@@ -54,17 +60,8 @@ class InputBuffer
char *_write_pointer;
char *_end_pointer;
- // some buffer
-public:
- explicit InputBuffer(int *termination_flag);
- void setFd(int fd);
- int readRequest();
- bool moreLines() { return !_requestlines.empty(); }
- std::string nextLine();
-
-private:
void storeRequestLine(char *line, int length);
- int readData();
+ Result readData();
};
#endif // InputBuffer_h
diff --git a/livestatus/src/Store.cc b/livestatus/src/Store.cc
index 75dc517..54613ac 100644
--- a/livestatus/src/Store.cc
+++ b/livestatus/src/Store.cc
@@ -133,9 +133,9 @@ void Store::registerDowntime(nebstruct_downtime_data *d)
bool Store::answerRequest(InputBuffer *input, OutputBuffer *output)
{
output->reset();
- int r = input->readRequest();
- if (r != IB_REQUEST_READ) {
- if (r != IB_END_OF_FILE)
+ InputBuffer::Result r = input->readRequest();
+ if (r != InputBuffer::Result::request_read) {
+ if (r != InputBuffer::Result::eof)
output->setError(RESPONSE_CODE_INCOMPLETE_REQUEST,
"Client connection terminated while request still
incomplete");
return false;