[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp
net/clientnb.cpp |1 - net/loolnb.cpp |1 - 2 files changed, 2 deletions(-) New commits: commit a3aba8270ca0321a9accf740b9e9c26fb4c0dab4 Author: Ashod Nakashian Date: Sun Feb 19 21:23:57 2017 -0500 nb: cleanup some logging Change-Id: I28e211e5a727ea308824651f417a91d7b388a30d Reviewed-on: https://gerrit.libreoffice.org/34452 Reviewed-by: Ashod Nakashian Tested-by: Ashod Nakashian diff --git a/net/clientnb.cpp b/net/clientnb.cpp index 5467fe9..0f7c796 100644 --- a/net/clientnb.cpp +++ b/net/clientnb.cpp @@ -224,7 +224,6 @@ struct Client : public Poco::Util::Application std::vector res; for (size_t i = 1; i < (1 << 14); ++i) { -std::cerr << "\n" << i; const std::vector data = Util::rng::getBytes(i); ws->sendFrame(data.data(), data.size(), WebSocket::SendFlags::FRAME_BINARY); diff --git a/net/loolnb.cpp b/net/loolnb.cpp index e088001..c6abe72 100644 --- a/net/loolnb.cpp +++ b/net/loolnb.cpp @@ -273,7 +273,6 @@ public: reply.insert(reply.end(), data.begin(), data.end()); } -std::cerr << "reply: " << reply.size() << std::endl; queueWSMessage(reply); } ___ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp
net/clientnb.cpp | 35 ++ net/loolnb.cpp | 56 --- 2 files changed, 56 insertions(+), 35 deletions(-) New commits: commit 84be891579089b29070c1506531aec004d69414e Author: Michael Meeks Date: Sat Feb 18 00:58:49 2017 + WS: cleanup calculation, masking, short reads and other bits. diff --git a/net/clientnb.cpp b/net/clientnb.cpp index 96fdc80..2dc7950 100644 --- a/net/clientnb.cpp +++ b/net/clientnb.cpp @@ -167,6 +167,11 @@ struct Client : public Poco::Util::Application { Session session("ws"); std::shared_ptr ws = session.getWebSocket(); + +std::string send = "hello there"; +ws->sendFrame(&send[0], send.length(), + WebSocket::SendFlags::FRAME_TEXT); + for (size_t i = 0; i < 10; i++) { ws->sendFrame(&i, sizeof(i), WebSocket::SendFlags::FRAME_BINARY); @@ -184,26 +189,24 @@ public: const bool https = (args.size() > 0 && args[0] == "ssl"); std::cerr << "Starting " << (https ? "HTTPS" : "HTTP") << " client." << std::endl; -if (getenv("WS")) -testWebsocket(); -else -{ -Session first("init", https); -Session second("init", https); +testWebsocket(); -int count = 42, back; -first.sendPing(count); -second.sendPing(count + 1); +Session first("init"); +Session second("init"); -back = first.getResponse(); -assert (back == count + 1); +int count = 42, back; +first.sendPing(count); +second.sendPing(count + 1); -back = second.getResponse(); -assert (back == count + 2); +back = first.getResponse(); +assert (back == count + 1); + +back = second.getResponse(); +assert (back == count + 2); + +testLadder(); +testParallel(); -testLadder(); -testParallel(); -} return 0; } }; diff --git a/net/loolnb.cpp b/net/loolnb.cpp index 2b5773c..b91e4d2 100644 --- a/net/loolnb.cpp +++ b/net/loolnb.cpp @@ -137,50 +137,59 @@ public: // websocket fun ! size_t len = T::_inBuffer.size(); char *p = &T::_inBuffer[0]; -char *data, *mask; if (len < 2) // partial read return; -bool fin = *p & 0x80; -WSOpCode code = static_cast(*p & 0x0f); -p++; -bool hasMask = *p & 0x80; -size_t payloadLen = *p & 0x7f; -p++; +bool fin = p[0] & 0x80; +WSOpCode code = static_cast(p[0] & 0x0f); +bool hasMask = p[1] & 0x80; +size_t payloadLen = p[1] & 0x7f; +size_t headerLen = 2; +// normally - 7 bit length. if (payloadLen == 126) // 2 byte length { if (len < 2 + 2) return; std::cerr << "Implement me 2 byte\n"; -data = p + 2; -len -= 2; +headerLen += 2; } else if (payloadLen == 127) // 8 byte length { if (len < 2 + 8) return; std::cerr << "Implement me 8 byte\n"; -data = p + 8; -len -= 8; +// FIXME: crop read length to remove top / sign bits. +headerLen += 8; } -else + +char *data, *mask; + +if (hasMask) { -data = p; +mask = p + headerLen; +headerLen += 4; +} + +if (payloadLen + headerLen > len) +{ // partial read wait for more data. +return; } +data = p + headerLen; + if (hasMask) { -mask = data; -data += 4; -len -= 4; for (size_t i = 0; i < len; ++i) data[i] = data[i] ^ mask[i % 4]; // FIXME: copy and un-mask at the same time ... -_wsPayload.insert(_wsPayload.end(), data, data + std::min(payloadLen, len)); +_wsPayload.insert(_wsPayload.end(), data, data + payloadLen); } else -_wsPayload.insert(_wsPayload.end(), data, data + std::min(payloadLen, len)); +_wsPayload.insert(_wsPayload.end(), data, data + payloadLen); + +T::_inBuffer.erase(T::_inBuffer.begin(), T::_inBuffer.begin() + headerLen + payloadLen); + // FIXME: fin, aggregating payloads into _wsPayload etc. handleWSMessage(fin, code, _wsPayload); _wsPayload.clear(); @@ -225,7 +234,16 @@ public: virtual void handleWSMessage( bool fin, WSOpCode code, std::vector &data) { -std::cerr << "Message: fin? " << fin << " code " << code << " data size " << data.size() << "\n"; +std::cerr << "Message: fin? " << fin << " code " << code << " data size " << data.size(); +if (code == WSOpCode::Text) +
[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp
net/clientnb.cpp | 54 ++ net/loolnb.cpp | 14 -- 2 files changed, 46 insertions(+), 22 deletions(-) New commits: commit dc52b96aa756198e75d0fe85a7bbb8db1acf7496 Author: Michael Meeks Date: Thu Feb 16 14:14:12 2017 + Websocket client test with WS=1 diff --git a/net/clientnb.cpp b/net/clientnb.cpp index aa123e9..de6576c 100644 --- a/net/clientnb.cpp +++ b/net/clientnb.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -39,6 +40,7 @@ using Poco::Net::HTTPClientSession; using Poco::Net::HTTPRequest; using Poco::Net::HTTPResponse; +using Poco::Net::WebSocket; using Poco::Runnable; using Poco::Thread; using Poco::URI; @@ -107,6 +109,15 @@ struct Session } return number; } + +std::shared_ptr getWebSocket() +{ +_session->setTimeout(Poco::Timespan(10, 0)); +HTTPRequest request(HTTPRequest::HTTP_GET, "/ws"); +HTTPResponse response; +return std::shared_ptr( +new WebSocket(*_session, request, response)); +} }; struct ThreadWorker : public Runnable @@ -151,25 +162,44 @@ struct Client : public Poco::Util::Application snakes[i].join(); } +void testWebsocket() +{ +Session session("ws"); +std::shared_ptr ws = session.getWebSocket(); +for (size_t i = 0; i < 10; i++) +{ +ws->sendFrame(&i, sizeof(i), WebSocket::SendFlags::FRAME_BINARY); +size_t back[5]; +int flags = 0; +int recvd = ws->receiveFrame((void *)back, sizeof(back), flags); +assert(recvd == sizeof(size_t)); +assert(back[0] == i + 1); +} +} + public: int main(const std::vector& /* args */) override { -Session first("init"); -Session second("init"); - -int count = 42, back; -first.sendPing(count); -second.sendPing(count + 1); +if (getenv("WS")) +testWebsocket(); +else +{ +Session first("init"); +Session second("init"); -back = first.getResponse(); -assert (back == count + 1); +int count = 42, back; +first.sendPing(count); +second.sendPing(count + 1); -back = second.getResponse(); -assert (back == count + 2); +back = first.getResponse(); +assert (back == count + 1); -testLadder(); -testParallel(); +back = second.getResponse(); +assert (back == count + 2); +testLadder(); +testParallel(); +} return 0; } }; diff --git a/net/loolnb.cpp b/net/loolnb.cpp index 5c61a78..01fc3c5 100644 --- a/net/loolnb.cpp +++ b/net/loolnb.cpp @@ -210,15 +210,6 @@ void server(SocketPoll& clientPoller) } } -/// Poll client sockets and do IO. -void pollAndComm(SocketPoll& poller, std::atomic& stop) -{ -while (!stop) -{ -poller.poll(5000); -} -} - int main(int, const char**) { // Used to poll client sockets. @@ -227,7 +218,10 @@ int main(int, const char**) // Start the client polling thread. Thread threadPoll([&poller](std::atomic& stop) { -pollAndComm(poller, stop); +while (!stop) +{ +poller.poll(5000); +} }); // Start the server. ___ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp
net/clientnb.cpp | 26 ++ net/loolnb.cpp |4 2 files changed, 26 insertions(+), 4 deletions(-) New commits: commit 1a9941c23fa538f7d0e8e5622169f8186cc6d37f Author: Michael Meeks Date: Thu Feb 16 11:52:22 2017 + Bang on the server with some threads. diff --git a/net/clientnb.cpp b/net/clientnb.cpp index 822804d..aa123e9 100644 --- a/net/clientnb.cpp +++ b/net/clientnb.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include @@ -64,6 +63,11 @@ struct Session else _session = new Poco::Net::HTTPClientSession(HostName, PortNumber); } +~Session() +{ +delete _session; +} + void sendPing(int i) { Poco::Net::HTTPRequest request( @@ -108,7 +112,7 @@ struct Session struct ThreadWorker : public Runnable { const char *_domain; - ThreadWorker (const char *domain) + ThreadWorker (const char *domain = NULL) : _domain(domain) { } @@ -116,7 +120,7 @@ struct ThreadWorker : public Runnable { for (int i = 0; i < 100; ++i) { -Session ping(_domain); +Session ping(_domain ? _domain : "init"); ping.sendPing(i); int back = ping.getResponse(); assert(back == i + 1); @@ -128,12 +132,25 @@ struct Client : public Poco::Util::Application { void testLadder() { -ThreadWorker ladder("init"); +ThreadWorker ladder; Thread thread; thread.start(ladder); thread.join(); } +void testParallel() +{ +const int num = 10; +Thread snakes[num]; +ThreadWorker ladders[num]; + +for (size_t i = 0; i < num; i++) +snakes[i].start(ladders[i]); + +for (int i = 0; i < num; i++) +snakes[i].join(); +} + public: int main(const std::vector& /* args */) override { @@ -151,6 +168,7 @@ public: assert (back == count + 2); testLadder(); +testParallel(); return 0; } diff --git a/net/loolnb.cpp b/net/loolnb.cpp index 7c4f23f..5c61a78 100644 --- a/net/loolnb.cpp +++ b/net/loolnb.cpp @@ -22,6 +22,8 @@ #include #include #include +#include +#include using Poco::MemoryInputStream; using Poco::StringTokenizer; @@ -75,6 +77,8 @@ public: } }; +// FIXME: use Poco Thread instead (?) + /// Generic thread class. class Thread { ___ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp
net/clientnb.cpp |3 ++- net/loolnb.cpp |7 +++ 2 files changed, 5 insertions(+), 5 deletions(-) New commits: commit de41fffde6cfeaebdc85a060e2ed8ef92e539a85 Author: Michael Meeks Date: Thu Feb 16 11:24:07 2017 + Get number response / ping bits working. diff --git a/net/clientnb.cpp b/net/clientnb.cpp index a8ec054..87ee8b5 100644 --- a/net/clientnb.cpp +++ b/net/clientnb.cpp @@ -91,6 +91,7 @@ struct Session std::string result(std::istreambuf_iterator(responseStream), {}); std::cerr << "Got response '" << result << "'\n"; +number = std::stoi(result); } catch (const Poco::Exception &e) { @@ -118,7 +119,7 @@ public: assert (back == count + 1); back = second.getResponse(); -assert (back == count + 1); +assert (back == count + 2); return 0; } diff --git a/net/loolnb.cpp b/net/loolnb.cpp index 963a631..7c4f23f 100644 --- a/net/loolnb.cpp +++ b/net/loolnb.cpp @@ -58,20 +58,19 @@ public: // complex algorithmic core: number = number + 1; +std::string numberString = std::to_string(number); std::ostringstream oss; oss << "HTTP/1.1 200 OK\r\n" << "Date: Once, Upon a time GMT\r\n" // Mon, 27 Jul 2009 12:28:53 GMT << "Server: madeup string (Linux)\r\n" -<< "Content-Length: " << _inBuffer.size() << "\r\n" +<< "Content-Length: " << numberString.size() << "\r\n" << "Content-Type: text/plain\r\n" << "Connection: Closed\r\n" << "\r\n" +<< numberString; ; std::string str = oss.str(); _outBuffer.insert(_outBuffer.end(), str.begin(), str.end()); - -// append the content we got: -_outBuffer.insert(_outBuffer.end(), _inBuffer.begin(), _inBuffer.end()); _inBuffer.clear(); } }; ___ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp net/socket.hpp
net/clientnb.cpp |3 - net/loolnb.cpp | 125 +++ net/socket.hpp | 55 ++-- 3 files changed, 88 insertions(+), 95 deletions(-) New commits: commit ded9607faf8e3e1fa122b200353a3465c94347ae Author: Michael Meeks Date: Tue Feb 14 23:45:24 2017 + Implement basic buffering. The socket now buffers input, and output, updates its poll record too. We pass a simple message from client to server and back using lamers HTTP. Sub-classed ClientSocket to provide a simple message handler. not very convinced by templatization here, but made it consistent. more ideal to have some virtual socket pieces. diff --git a/net/clientnb.cpp b/net/clientnb.cpp index e4a7fc3..ec7c578 100644 --- a/net/clientnb.cpp +++ b/net/clientnb.cpp @@ -82,7 +82,8 @@ public: std::cerr << "try to get response\n"; std::istream& responseStream = session->receiveResponse(response); -std::cerr << "Got response '" << responseStream << "'\n"; +std::string result(std::istreambuf_iterator(responseStream), {}); +std::cerr << "Got response '" << result << "'\n"; } catch (const Poco::Exception &e) { diff --git a/net/loolnb.cpp b/net/loolnb.cpp index 952c44a..d457471 100644 --- a/net/loolnb.cpp +++ b/net/loolnb.cpp @@ -24,6 +24,34 @@ constexpr int PortNumber = 9191; +class SimpleResponseClient : public ClientSocket +{ +public: +SimpleResponseClient(const int fd) : +ClientSocket(fd) +{ +} +virtual void handleIncomingMessage() override +{ +std::cerr << "message had size " << _inBuffer.size() << "\n"; +std::ostringstream oss; +oss << "HTTP/1.1 200 OK\r\n" +<< "Date: Once, Upon a time GMT\r\n" // Mon, 27 Jul 2009 12:28:53 GMT +<< "Server: madeup string (Linux)\r\n" +<< "Content-Length: " << _inBuffer.size() << "\r\n" +<< "Content-Type: text/plain\r\n" +<< "Connection: Closed\r\n" +<< "\r\n" +; +std::string str = oss.str(); +_outBuffer.insert(_outBuffer.end(), str.begin(), str.end()); + +// append the content we got: +_outBuffer.insert(_outBuffer.end(), _inBuffer.begin(), _inBuffer.end()); +_inBuffer.clear(); +} +}; + /// Handles non-blocking socket event polling. /// Only polls on N-Sockets and invokes callback and /// doesn't manage buffers or client data. @@ -102,7 +130,7 @@ public: /// Insert a new socket to be polled. /// Sockets are removed only when the handler return false. -void insertNewSocket(const std::shared_ptr& newSocket) +void insertNewSocket(const std::shared_ptr& newSocket) { std::lock_guard lock(_mutex); @@ -138,8 +166,7 @@ private: for (size_t i = 0; i < size; ++i) { _pollFds[i].fd = _pollSockets[i]->getFD(); -//TODO: Get from the socket. -_pollFds[i].events = POLLIN | POLLOUT; +_pollFds[i].events = _pollSockets[i]->getPollEvents(); _pollFds[i].revents = 0; } @@ -153,10 +180,10 @@ private: /// main-loop wakeup pipe int _wakeup[2]; /// The sockets we're controlling -std::vector> _pollSockets; +std::vector> _pollSockets; /// Protects _newSockets std::mutex _mutex; -std::vector> _newSockets; +std::vector> _newSockets; /// The fds to poll. std::vector _pollFds; }; @@ -197,39 +224,7 @@ private: Poco::Net::SocketAddress addr("127.0.0.1", PortNumber); -void client(const int timeoutMs) -{ -const auto client = std::make_shared(); -if (!client->connect(addr, timeoutMs) && errno != EINPROGRESS) -{ -const std::string msg = "Failed to call connect. (errno: "; -throw std::runtime_error(msg + std::strerror(errno) + ")"); -} - -std::cout << "Connected " << client->getFD() << std::endl; - -client->send("1", 1); -int sent = 1; -while (sent > 0 && client->pollRead(5000)) -{ -char buf[1024]; -const int recv = client->recv(buf, sizeof(buf)); -if (recv <= 0) -{ -perror("recv"); -break; -} -else -{ -const std::string msg = std::string(buf, recv); -const int num = stoi(msg); -const std::string new_msg = std::to_string(num + 1); -sent = client->send(new_msg.data(), new_msg.size()); -} -} -} - -void server(SocketPoll& poller) +void server(SocketPoll& poller) { // Start server. auto server = std::make_shared(); @@ -250,7 +245,7 @@ void server(SocketPoll& poller) { if (server->pollRead(3)) { -std::shared_ptr clientSocket = server->accept(); +std::shared_ptr clientSocket = server->accept(); if (!clientSocket) {