ext/common/MessageReadersWriters.h in passenger-4.0.48 vs ext/common/MessageReadersWriters.h in passenger-4.0.49

- old
+ new

@@ -73,11 +73,11 @@ while (true) { // Read a bunch of network data... char buf[1024]; ssize_t size = recv(fd, buf, sizeof(buf)); size_t consumed = 0; - + // ...and process it all. We only feed data to the message object // that hasn't already been fed. while (consumed < size) { consumed += intReader.feed(buf + consumed, size - consumed); if (intMessage.done()) { @@ -117,86 +117,86 @@ /** * For 16-bit big-endian integers. */ class Uint16Message { private: - uint16_t val; - uint8_t consumed; - + boost::uint16_t val; + boost::uint8_t consumed; + public: Uint16Message() { consumed = 0; } - + void reset() { consumed = 0; } - + size_t feed(const char *data, size_t size) { size_t locallyConsumed; - - locallyConsumed = std::min(size, sizeof(uint16_t) - consumed); + + locallyConsumed = std::min(size, sizeof(boost::uint16_t) - consumed); memcpy((char *) &val + consumed, data, locallyConsumed); consumed += locallyConsumed; if (locallyConsumed > 0 && done()) { val = ntohs(val); } return locallyConsumed; } - + bool done() const { - return consumed == sizeof(uint16_t); + return consumed == sizeof(boost::uint16_t); } - - uint16_t value() const { + + boost::uint16_t value() const { return val; } - - static void generate(void *buf, uint16_t val) { + + static void generate(void *buf, boost::uint16_t val) { val = htons(val); memcpy(buf, &val, sizeof(val)); } }; /** * For 32-bit big-endian integers. */ class Uint32Message { private: - uint32_t val; - uint8_t consumed; - + boost::uint32_t val; + boost::uint8_t consumed; + public: Uint32Message() { consumed = 0; } - + void reset() { consumed = 0; } - + size_t feed(const char *data, size_t size) { size_t locallyConsumed; - - locallyConsumed = std::min(size, sizeof(uint32_t) - consumed); + + locallyConsumed = std::min(size, sizeof(boost::uint32_t) - consumed); memcpy((char *) &val + consumed, data, locallyConsumed); consumed += locallyConsumed; if (locallyConsumed > 0 && done()) { val = ntohl(val); } return locallyConsumed; } - + bool done() const { - return consumed == sizeof(uint32_t); + return consumed == sizeof(boost::uint32_t); } - - uint32_t value() const { + + boost::uint32_t value() const { return val; } - - static void generate(void *buf, uint32_t val) { + + static void generate(void *buf, boost::uint32_t val) { val = htonl(val); memcpy(buf, &val, sizeof(val)); } }; @@ -206,73 +206,73 @@ class ArrayMessage { public: enum Error { TOO_LARGE }; - + private: enum State { READING_HEADER, READING_BODY, DONE, ERROR }; - - uint16_t toReserve; - uint16_t maxSize; + + boost::uint16_t toReserve; + boost::uint16_t maxSize; Uint16Message headerReader; - uint8_t state; - uint8_t error; + boost::uint8_t state; + boost::uint8_t error; string buffer; vector<StaticString> result; - + void parseBody(const char *data, size_t size) { const char *start = data; const char *terminator; size_t rest = size; - + while ((terminator = (const char *) memchr(start, '\0', rest)) != NULL) { size_t len = terminator - start; result.push_back(StaticString(start, len)); start = terminator + 1; rest = size - (start - data); } } - + public: ArrayMessage() { state = READING_HEADER; toReserve = 0; maxSize = 0; } - - void reserve(uint16_t size) { + + void reserve(boost::uint16_t size) { toReserve = size; result.reserve(size); } - - void setMaxSize(uint16_t size) { + + void setMaxSize(boost::uint16_t size) { maxSize = size; } - + void reset() { state = READING_HEADER; headerReader.reset(); buffer.clear(); result.clear(); if (toReserve > 0) { result.reserve(toReserve); } } - + size_t feed(const char *data, size_t size) { size_t consumed = 0; - + while (consumed < size && !done()) { const char *current = data + consumed; size_t rest = size - consumed; - + switch (state) { case READING_HEADER: consumed += headerReader.feed(current, rest); if (headerReader.done()) { if (maxSize > 0 && headerReader.value() > maxSize) { @@ -309,27 +309,27 @@ abort(); } } return consumed; } - + bool done() const { return state == DONE || state == ERROR; } - + bool hasError() const { return state == ERROR; } - + Error errorCode() const { return (Error) error; } - + const vector<StaticString> &value() const { return result; } - + /** * Given a bunch of array items, generates an array message. The message is * generated in the form of an array of StaticStrings which must all be written * out (e.g. with writev()) in the given order. These StaticStrings point * to the buffers pointed to by <em>args</em> as well as <em>headerBuf</em>, @@ -344,34 +344,34 @@ * message data will be stored. Exactly <tt>outputSize(argsCount)</tt> * items will be stored in this array. * @param outCount The number of items in <em>out</em>. */ static void generate(StaticString args[], unsigned int argsCount, - char headerBuf[sizeof(uint16_t)], StaticString *out, unsigned int outCount) + char headerBuf[sizeof(boost::uint16_t)], StaticString *out, unsigned int outCount) { if (OXT_UNLIKELY(outCount < outputSize(argsCount))) { throw ArgumentException("outCount too small."); } - + unsigned int size = 0; unsigned int i; - + for (i = 0; i < argsCount; i++) { size += args[i].size() + 1; } if (OXT_UNLIKELY(size > 0xFFFF)) { throw ArgumentException("Data size exceeds maximum size for array messages."); } - + Uint16Message::generate(headerBuf, size); - out[0] = StaticString(headerBuf, sizeof(uint16_t)); + out[0] = StaticString(headerBuf, sizeof(boost::uint16_t)); for (i = 0; i < argsCount; i++) { out[1 + 2 * i] = args[i]; out[1 + 2 * i + 1] = StaticString("\0", 1); } } - + static unsigned int outputSize(unsigned int argsCount) { return argsCount * 2 + 1; } }; @@ -381,36 +381,36 @@ class ScalarMessage { public: enum Error { TOO_LARGE }; - + private: enum State { READING_HEADER, READING_BODY, DONE, ERROR }; - - uint8_t state; - uint8_t error; - uint32_t maxSize; + + boost::uint8_t state; + boost::uint8_t error; + boost::uint32_t maxSize; Uint32Message headerReader; string buffer; StaticString result; - + public: - ScalarMessage(uint32_t maxSize = 0) { + ScalarMessage(boost::uint32_t maxSize = 0) { state = READING_HEADER; this->maxSize = maxSize; } - - void setMaxSize(uint32_t maxSize) { + + void setMaxSize(boost::uint32_t maxSize) { this->maxSize = maxSize; } - + /** * Resets the internal state so that this object can be used for processing * another scalar message. * * If <em>zeroBuffer</em> is true, then the contents of the internal buffer @@ -423,18 +423,18 @@ MemZeroGuard guard(buffer); } headerReader.reset(); buffer.clear(); } - + size_t feed(const char *data, size_t size) { size_t consumed = 0; - + while (consumed < size && !done()) { const char *current = data + consumed; size_t rest = size - consumed; - + switch (state) { case READING_HEADER: consumed += headerReader.feed(current, rest); if (headerReader.done()) { if (maxSize > 0 && headerReader.value() > maxSize) { @@ -471,55 +471,55 @@ abort(); }; } return consumed; } - + bool done() const { return state == DONE || state == ERROR; } - + bool hasError() const { return state == ERROR; } - + Error errorCode() const { return (Error) error; } - + const StaticString &value() const { return result; } - - static void generate(const StaticString &data, char headerBuf[sizeof(uint32_t)], + + static void generate(const StaticString &data, char headerBuf[sizeof(boost::uint32_t)], StaticString output[2]) { if (OXT_UNLIKELY(data.size() > 0xFFFFFFFF)) { throw ArgumentException("Data size exceeds maximum size for scalar messages."); } - + Uint32Message::generate(headerBuf, data.size()); - output[0] = StaticString(headerBuf, sizeof(uint32_t)); + output[0] = StaticString(headerBuf, sizeof(boost::uint32_t)); output[1] = data; } // output must be at least count + 1 in length static void generate(const StaticString data[], unsigned int count, - char headerBuf[sizeof(uint32_t)], StaticString *output) + char headerBuf[sizeof(boost::uint32_t)], StaticString *output) { unsigned int i; - uint32_t totalSize = 0; + boost::uint32_t totalSize = 0; for (i = 0; i < count; i++) { if (OXT_UNLIKELY(data[i].size() > 0xFFFFFFFF)) { throw ArgumentException("Data size exceeds maximum size for scalar messages."); } totalSize += data[i].size(); output[i + 1] = data[i]; } Uint32Message::generate(headerBuf, totalSize); - output[0] = StaticString(headerBuf, sizeof(uint32_t)); + output[0] = StaticString(headerBuf, sizeof(boost::uint32_t)); } }; } // namespace Passenger