net/Socket.hpp | 20 +++++++++----------- net/SslSocket.hpp | 2 +- net/loolnb.cpp | 29 ++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 21 deletions(-)
New commits: commit e16fee0341b2c42f355db5a9f9f02b3373880d52 Author: Jan Holesovsky <ke...@collabora.com> Date: Thu Feb 23 13:14:01 2017 +0100 nb: Abstract WebSocketHandler. We don't need a special "WebSocket" class, as websocket itself is just an upgrade of an existing socket / connection, and trying to come up with a concept where a Socket class magically upgrades to a WebSocket class would be messy. So let's have just a WebSocketHandler, that communicates over a StreamSocket or SslStreamSocket, and be done with that :-) Change-Id: I449e4d662fbe2c5b1789e308053e4a71376fe481 diff --git a/net/Socket.hpp b/net/Socket.hpp index 9c037e9..6020ca8 100644 --- a/net/Socket.hpp +++ b/net/Socket.hpp @@ -286,7 +286,7 @@ private: class StreamSocket; /// Interface that handles the actual incoming message. -class ResponseClientInterface +class SocketHandlerInterface { public: /// Set the socket associated with this ResponseClient. @@ -300,11 +300,11 @@ public: class StreamSocket : public Socket { public: - StreamSocket(const int fd, ResponseClientInterface* responseClient) : + StreamSocket(const int fd, SocketHandlerInterface* socketHandler) : Socket(fd), - _responseClient(responseClient) + _socketHandler(socketHandler) { - _responseClient->setSocket(this); + _socketHandler->setSocket(this); } /// Called when a polling event is received. @@ -322,8 +322,8 @@ public: while (!_inBuffer.empty() && oldSize != _inBuffer.size()) { oldSize = _inBuffer.size(); - if (_responseClient) - _responseClient->handleIncomingMessage(); + if (_socketHandler) + _socketHandler->handleIncomingMessage(); } // SSL might want to do handshake, @@ -415,15 +415,13 @@ public: protected: /// Client handling the actual data. - std::unique_ptr<ResponseClientInterface> _responseClient; + std::unique_ptr<SocketHandlerInterface> _socketHandler; std::vector< char > _inBuffer; std::vector< char > _outBuffer; - // FIXME temporarily make this friend class; when integrating, we'll see - // what methods we need to introduce instead of direct access to the - // _inBuffer / _outBuffer. - friend class SimpleResponseClient; + // To be able to access _inBuffer and _outBuffer. + friend class WebSocketHandler; }; #endif diff --git a/net/SslSocket.hpp b/net/SslSocket.hpp index c46ffbc..87b4b33 100644 --- a/net/SslSocket.hpp +++ b/net/SslSocket.hpp @@ -21,7 +21,7 @@ class SslStreamSocket : public StreamSocket { public: - SslStreamSocket(const int fd, ResponseClientInterface* responseClient) : + SslStreamSocket(const int fd, SocketHandlerInterface* responseClient) : StreamSocket(fd, responseClient), _ssl(nullptr), _sslWantsTo(SslWantsTo::ReadOrWrite), diff --git a/net/loolnb.cpp b/net/loolnb.cpp index 4e4ac38..0428b4b 100644 --- a/net/loolnb.cpp +++ b/net/loolnb.cpp @@ -37,7 +37,7 @@ constexpr int SslPortNumber = 9193; static std::string computeAccept(const std::string &key); -class SimpleResponseClient : public ResponseClientInterface +class WebSocketHandler : public SocketHandlerInterface { std::unique_ptr<StreamSocket> _socket; int _wsVersion; @@ -47,18 +47,19 @@ class SimpleResponseClient : public ResponseClientInterface enum { HTTP, WEBSOCKET } _wsState; public: - SimpleResponseClient() : + WebSocketHandler() : _wsVersion(0), _wsState(HTTP) { } + /// Implementation of the SocketHandlerInterface. virtual void setSocket(StreamSocket* socket) override { _socket.reset(socket); } - void handleHTTP() + void handleWebsocketUpgrade() { int number = 0; MemoryInputStream message(&_socket->_inBuffer[0], _socket->_inBuffer.size()); @@ -133,12 +134,13 @@ public: // ... reserved }; + /// Implementation of the SocketHandlerInterface. virtual void handleIncomingMessage() override { std::cerr << "incoming message with buffer size " << _socket->_inBuffer.size() << "\n"; if (_wsState == HTTP) { - handleHTTP(); + handleWebsocketUpgrade(); return; } @@ -204,11 +206,11 @@ public: _socket->_inBuffer.erase(_socket->_inBuffer.begin(), _socket->_inBuffer.begin() + headerLen + payloadLen); // FIXME: fin, aggregating payloads into _wsPayload etc. - handleWSMessage(fin, code, _wsPayload); + handleMessage(fin, code, _wsPayload); _wsPayload.clear(); } - void queueWSMessage(const std::vector<char> &data, + void sendMessage(const std::vector<char> &data, WSOpCode code = WSOpCode::Binary) { size_t len = data.size(); @@ -253,7 +255,17 @@ public: _socket->_outBuffer.insert(_socket->_outBuffer.end(), data.begin(), data.end()); } - void handleWSMessage(bool fin, WSOpCode code, std::vector<char> &data) + virtual void handleMessage(bool fin, WSOpCode code, std::vector<char> &data) = 0; +}; + +class SimpleResponseClient : public WebSocketHandler +{ +public: + SimpleResponseClient() : WebSocketHandler() + { + } + + virtual void handleMessage(bool fin, WSOpCode code, std::vector<char> &data) override { std::cerr << "Message: fin? " << fin << " code " << code << " data size " << data.size(); if (code == WSOpCode::Text) @@ -284,9 +296,8 @@ public: reply.insert(reply.end(), data.begin(), data.end()); } - queueWSMessage(reply); + sendMessage(reply); } - }; // FIXME: use Poco Thread instead (?) _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits