From 2254421eade4e4ae745482d39b3f801738ca2177 Mon Sep 17 00:00:00 2001 From: Dimon4eg Date: Sat, 11 May 2019 22:20:58 +0300 Subject: [PATCH] minor improvements (#66) * minor improvements * fix build * improve tests code --- ixwebsocket/IXWebSocket.cpp | 6 +++-- ixwebsocket/IXWebSocket.h | 3 ++- ixwebsocket/IXWebSocketSendInfo.h | 3 --- ixwebsocket/IXWebSocketTransport.cpp | 2 +- .../windows/IXSetThreadName_windows.cpp | 1 - test/IXTest.h | 9 ------- ...IXWebSocketTestConnectionDisconnection.cpp | 26 +++++++++---------- ws/snake/IXSnakeProtocol.cpp | 1 + 8 files changed, 21 insertions(+), 30 deletions(-) diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index 10f788c0..669645ed 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -148,6 +148,8 @@ namespace ix if (_thread.joinable()) { + // wait until working thread will exit + // it will exit after close operation is finished _stop = true; _thread.join(); _stop = false; @@ -361,10 +363,10 @@ namespace ix } } - WebSocketSendInfo WebSocket::send(const std::string& text, + WebSocketSendInfo WebSocket::send(const std::string& data, const OnProgressCallback& onProgressCallback) { - return sendMessage(text, SendMessageKind::Binary, onProgressCallback); + return sendMessage(data, SendMessageKind::Binary, onProgressCallback); } WebSocketSendInfo WebSocket::sendText(const std::string& text, diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index f263eca2..9ce6c73d 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -106,7 +106,8 @@ namespace ix WebSocketInitResult connect(int timeoutSecs); void run(); - WebSocketSendInfo send(const std::string& text, + // send binary data + WebSocketSendInfo send(const std::string& data, const OnProgressCallback& onProgressCallback = nullptr); WebSocketSendInfo sendText(const std::string& text, const OnProgressCallback& onProgressCallback = nullptr); diff --git a/ixwebsocket/IXWebSocketSendInfo.h b/ixwebsocket/IXWebSocketSendInfo.h index 1e684f0e..8258657a 100644 --- a/ixwebsocket/IXWebSocketSendInfo.h +++ b/ixwebsocket/IXWebSocketSendInfo.h @@ -6,9 +6,6 @@ #pragma once -#include -#include - namespace ix { struct WebSocketSendInfo diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 21530e77..9c774b42 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -719,7 +719,7 @@ namespace ix bool compress, const OnProgressCallback& onProgressCallback) { - if (_readyState == CLOSING || _readyState == CLOSED) + if (_readyState != OPEN) { return WebSocketSendInfo(); } diff --git a/ixwebsocket/windows/IXSetThreadName_windows.cpp b/ixwebsocket/windows/IXSetThreadName_windows.cpp index 3c21a852..92810010 100644 --- a/ixwebsocket/windows/IXSetThreadName_windows.cpp +++ b/ixwebsocket/windows/IXSetThreadName_windows.cpp @@ -4,7 +4,6 @@ * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. */ #include "../IXSetThreadName.h" -#include #include namespace ix diff --git a/test/IXTest.h b/test/IXTest.h index c438406f..177b83c0 100644 --- a/test/IXTest.h +++ b/test/IXTest.h @@ -27,15 +27,6 @@ namespace ix struct Logger { public: - Logger& operator<<(const std::string& msg) - { - std::lock_guard lock(_mutex); - - std::cerr << msg; - std::cerr << std::endl; - return *this; - } - template Logger& operator<<(T const& obj) { diff --git a/test/IXWebSocketTestConnectionDisconnection.cpp b/test/IXWebSocketTestConnectionDisconnection.cpp index 55d5c37a..12dc0b94 100644 --- a/test/IXWebSocketTestConnectionDisconnection.cpp +++ b/test/IXWebSocketTestConnectionDisconnection.cpp @@ -109,38 +109,38 @@ TEST_CASE("websocket_connections", "[websocket]") { SECTION("Try to connect to invalid servers.") { - IXWebSocketTestConnectionDisconnection chatA; + IXWebSocketTestConnectionDisconnection test; - chatA.start(GOOGLE_URL); + test.start(GOOGLE_URL); ix::msleep(1000); - chatA.stop(); + test.stop(); - chatA.start(UNKNOWN_URL); + test.start(UNKNOWN_URL); ix::msleep(1000); - chatA.stop(); + test.stop(); } SECTION("Try to connect and disconnect with different timing, not enough time to succesfully connect") { - IXWebSocketTestConnectionDisconnection chatA; + IXWebSocketTestConnectionDisconnection test; for (int i = 0; i < 50; ++i) { log(std::string("Run: ") + std::to_string(i)); - chatA.start(WEBSOCKET_DOT_ORG_URL); + test.start(WEBSOCKET_DOT_ORG_URL); ix::msleep(i); - chatA.stop(); + test.stop(); } } - SECTION("Try to connect and disconnect with different timing, from not enough time to successfull connect") + /*SECTION("Try to connect and disconnect with different timing, from not enough time to successfull connect") { - IXWebSocketTestConnectionDisconnection chatA; + IXWebSocketTestConnectionDisconnection test; for (int i = 0; i < 20; ++i) { log(std::string("Run: ") + std::to_string(i)); - chatA.start(WEBSOCKET_DOT_ORG_URL); + test.start(WEBSOCKET_DOT_ORG_URL); ix::msleep(i*50); - chatA.stop(); + test.stop(); } - } + }*/ } diff --git a/ws/snake/IXSnakeProtocol.cpp b/ws/snake/IXSnakeProtocol.cpp index 138612e3..acdfc3cc 100644 --- a/ws/snake/IXSnakeProtocol.cpp +++ b/ws/snake/IXSnakeProtocol.cpp @@ -14,6 +14,7 @@ #include "nlohmann/json.hpp" #include +#include namespace snake {