net/Socket.hpp | 15 +++++++ net/WebSocketHandler.hpp | 5 +- net/loolnb.cpp | 89 ++++++++++++++++++++++++++--------------------- 3 files changed, 69 insertions(+), 40 deletions(-)
New commits: commit 0903776a57fda95ea6b59e33a632636c585cf0c1 Author: Michael Meeks <[email protected]> Date: Fri Feb 24 16:45:31 2017 +0000 Cleanup logging, and add message tracing / dumper. diff --git a/net/Socket.hpp b/net/Socket.hpp index 08112c5..59be92b 100644 --- a/net/Socket.hpp +++ b/net/Socket.hpp @@ -27,6 +27,8 @@ #include <Poco/Net/SocketAddress.h> +#include "Log.hpp" + /// A non-blocking, streaming socket. class Socket { @@ -321,6 +323,13 @@ public: // Always try to read. closeSocket = !readIncomingData(); + auto& log = Log::logger(); + if (log.trace()) { + LOG_TRC("Incoming data buffer " << _inBuffer.size() << + " closeSocket? " << closeSocket << "\n"); + log.dump("", &_inBuffer[0], _inBuffer.size()); + } + // If we have data, allow the app to consume. size_t oldSize = 0; while (!_inBuffer.empty() && oldSize != _inBuffer.size()) @@ -383,6 +392,12 @@ public: do { len = writeData(&_outBuffer[0], _outBuffer.size()); + + auto& log = Log::logger(); + if (log.trace()) { + LOG_TRC("Wrote outgoing data " << len << " bytes\n"); + log.dump("", &_outBuffer[0], len); + } } while (len < 0 && errno == EINTR); diff --git a/net/WebSocketHandler.hpp b/net/WebSocketHandler.hpp index 286ac53..324da6b 100644 --- a/net/WebSocketHandler.hpp +++ b/net/WebSocketHandler.hpp @@ -10,6 +10,7 @@ #ifndef INCLUDED_WEBSOCKETHANDLER_HPP #define INCLUDED_WEBSOCKETHANDLER_HPP +#include "Log.hpp" #include "Socket.hpp" class WebSocketHandler : public SocketHandlerInterface @@ -55,7 +56,7 @@ public: /// Implementation of the SocketHandlerInterface. virtual void handleIncomingMessage() override { - std::cerr << "incoming message with buffer size " << _socket->_inBuffer.size() << "\n"; + LOG_TRC("incoming WebSocket message"); if (_wsState == HTTP) { handleWebsocketUpgrade(); @@ -129,7 +130,7 @@ public: } void sendMessage(const std::vector<char> &data, - WSOpCode code = WSOpCode::Binary) + WSOpCode code = WSOpCode::Binary) { size_t len = data.size(); bool fin = false; diff --git a/net/loolnb.cpp b/net/loolnb.cpp index 201f8a4..489cfa7 100644 --- a/net/loolnb.cpp +++ b/net/loolnb.cpp @@ -21,6 +21,7 @@ #include <Poco/MemoryStream.h> #include <Poco/Net/SocketAddress.h> #include <Poco/Net/HTTPRequest.h> +#include <Poco/Util/ServerApplication.h> #include <Poco/StringTokenizer.h> #include <Poco/Runnable.h> #include <Poco/Thread.h> @@ -145,54 +146,66 @@ void server(const Poco::Net::SocketAddress& addr, SocketPoll& clientPoller, } } -int main(int argc, const char**argv) +class LOOLNB : public Poco::Util::ServerApplication { - // TODO: These would normally come from config. - SslContext::initialize("/etc/loolwsd/cert.pem", - "/etc/loolwsd/key.pem", - "/etc/loolwsd/ca-chain.cert.pem"); - - // Used to poll client sockets. - SocketPoll poller; - - // Start the client polling thread. - Thread threadPoll([&poller](std::atomic<bool>& stop) +public: + int main(const std::vector<std::string>& args) override { - while (!stop) + const char* logLevel = std::getenv("LOOL_LOGLEVEL"); + std::map<std::string, std::string> props; + if (logLevel) + Log::initialize("loolnb", logLevel ? logLevel : "", + false, false, props); + + // TODO: These would normally come from config. + SslContext::initialize("/etc/loolwsd/cert.pem", + "/etc/loolwsd/key.pem", + "/etc/loolwsd/ca-chain.cert.pem"); + + // Used to poll client sockets. + SocketPoll poller; + + // Start the client polling thread. + Thread threadPoll([&poller](std::atomic<bool>& stop) + { + while (!stop) + { + poller.poll(5000); + } + }); + + class PlainSocketFactory : public SocketFactory { - poller.poll(5000); - } - }); + std::shared_ptr<Socket> create(const int fd) override + { + return std::make_shared<StreamSocket>(fd, new SimpleResponseClient()); + } + }; - class PlainSocketFactory : public SocketFactory - { - std::shared_ptr<Socket> create(const int fd) override + class SslSocketFactory : public SocketFactory { - return std::make_shared<StreamSocket>(fd, new SimpleResponseClient()); - } - }; + std::shared_ptr<Socket> create(const int fd) override + { + return std::make_shared<SslStreamSocket>(fd, new SimpleResponseClient()); + } + }; - class SslSocketFactory : public SocketFactory - { - std::shared_ptr<Socket> create(const int fd) override - { - return std::make_shared<SslStreamSocket>(fd, new SimpleResponseClient()); - } - }; + // Start the server. + if (args.back() == "ssl") + server(addrSsl, poller, std::unique_ptr<SocketFactory>{new SslSocketFactory}); + else + server(addrHttp, poller, std::unique_ptr<SocketFactory>{new PlainSocketFactory}); - // Start the server. - if (!strcmp(argv[argc-1], "ssl")) - server(addrSsl, poller, std::unique_ptr<SocketFactory>{new SslSocketFactory}); - else - server(addrHttp, poller, std::unique_ptr<SocketFactory>{new PlainSocketFactory}); + std::cout << "Shutting down server." << std::endl; - std::cout << "Shutting down server." << std::endl; + threadPoll.stop(); - threadPoll.stop(); + SslContext::uninitialize(); + return 0; + } +}; - SslContext::uninitialize(); - return 0; -} +POCO_SERVER_MAIN(LOOLNB) /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
