net/socket.hpp | 84 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 37 deletions(-)
New commits: commit 6de885e5ffae29f3df4ef88c9ba14a46f0c28c6b Author: Ashod Nakashian <[email protected]> Date: Sat Feb 18 15:35:29 2017 -0500 nb: move more into BufferingSocket Reduces code duplication and makes buffer management easier. Change-Id: I4f3bcf02b273d7ae23f4fd7600e66644ccec0d2a Reviewed-on: https://gerrit.libreoffice.org/34414 Reviewed-by: Ashod Nakashian <[email protected]> Tested-by: Ashod Nakashian <[email protected]> diff --git a/net/socket.hpp b/net/socket.hpp index f350c0b..79663d9 100644 --- a/net/socket.hpp +++ b/net/socket.hpp @@ -318,38 +318,8 @@ public: HandleResult::CONTINUE; } - /// Override to reading data from socket. - virtual bool readIncomingData() = 0; - - /// Override to write data out to socket. - virtual void writeOutgoingData() = 0; - - int getPollEvents() override - { - // Only poll for read we if we have nothing to write. - return (_outBuffer.empty() ? POLLIN : POLLIN | POLLOUT); - } - -protected: - BufferingSocket(const int fd) : - Socket(fd) - { - } - - std::vector< char > _inBuffer; - std::vector< char > _outBuffer; - -private: - /// Override to handle read data. - /// Called after successful socket reads. - virtual void handleIncomingMessage() = 0; -}; - -/// A plain, non-blocking, data streaming socket. -class StreamSocket : public BufferingSocket -{ -public: - bool readIncomingData() override + /// Reads data by invoking readData() and buffering. + virtual bool readIncomingData() { ssize_t len; char buf[4096]; @@ -359,7 +329,7 @@ public: // TODO: Cap the buffer size, lest we grow beyond control. do { - len = ::read(getFD(), buf, sizeof(buf)); + len = readData(buf, sizeof(buf)); } while (len < 0 && errno == EINTR); @@ -376,13 +346,17 @@ public: return len != 0; // zero is eof / clean socket close. } - void writeOutgoingData() override + /// Override to write data out to socket. + virtual void writeOutgoingData() { assert (_outBuffer.size() > 0); ssize_t len; - do { - len = ::write(getFD(), &_outBuffer[0], _outBuffer.size()); - } while (len < 0 && errno == EINTR); + do + { + len = writeData(&_outBuffer[0], _outBuffer.size()); + } + while (len < 0 && errno == EINTR); + if (len > 0) { _outBuffer.erase(_outBuffer.begin(), @@ -391,6 +365,42 @@ public: // else poll will handle errors } + /// Override to handle reading of socket data differently. + virtual int readData(char* buf, int len) + { + return ::read(getFD(), buf, len); + } + + /// Override to handle writing data to socket differently. + virtual int writeData(const char* buf, const int len) + { + return ::write(getFD(), buf, len); + } + + int getPollEvents() override + { + // Only poll for read we if we have nothing to write. + return (_outBuffer.empty() ? POLLIN : POLLIN | POLLOUT); + } + +protected: + BufferingSocket(const int fd) : + Socket(fd) + { + } + + std::vector< char > _inBuffer; + std::vector< char > _outBuffer; + +private: + /// Override to handle read data. + /// Called after successful socket reads. + virtual void handleIncomingMessage() = 0; +}; + +/// A plain, non-blocking, data streaming socket. +class StreamSocket : public BufferingSocket +{ protected: StreamSocket(const int fd) : BufferingSocket(fd) _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
