From d9ff82ce83bd3081b3e4f28bc020251acabe2a08 Mon Sep 17 00:00:00 2001 From: Michael Meeks Date: Thu, 16 Feb 2017 14:14:12 +0000 Subject: [PATCH] Websocket client test with WS=1 --- net/clientnb.cpp | 54 +++++++++++++++++++++++++++++++++++++----------- net/loolnb.cpp | 14 ++++--------- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/net/clientnb.cpp b/net/clientnb.cpp index aa123e936..de6576ca5 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"); + if (getenv("WS")) + testWebsocket(); + else + { + Session first("init"); + Session second("init"); - int count = 42, back; - first.sendPing(count); - second.sendPing(count + 1); + int count = 42, back; + first.sendPing(count); + second.sendPing(count + 1); - back = first.getResponse(); - assert (back == count + 1); + back = first.getResponse(); + assert (back == count + 1); - back = second.getResponse(); - assert (back == count + 2); - - testLadder(); - testParallel(); + back = second.getResponse(); + assert (back == count + 2); + testLadder(); + testParallel(); + } return 0; } }; diff --git a/net/loolnb.cpp b/net/loolnb.cpp index 5c61a78e3..01fc3c554 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.