From a0f15bfb56f15454ad0e77b70d041225952a71d3 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Fri, 11 Jan 2019 21:25:06 -0800 Subject: [PATCH] C++14 + use make_unique and make_shared to make shared pointers --- CMakeLists.txt | 2 +- examples/broadcast_server/CMakeLists.txt | 2 +- examples/chat/CMakeLists.txt | 2 +- examples/cobra_publisher/CMakeLists.txt | 2 +- examples/echo_server/CMakeLists.txt | 2 +- examples/ping_pong/CMakeLists.txt | 2 +- examples/ws_connect/CMakeLists.txt | 2 +- ixwebsocket/IXWebSocketPerMessageDeflate.cpp | 10 +++++----- ixwebsocket/IXWebSocketPerMessageDeflate.h | 4 ++-- ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp | 6 ++++-- ixwebsocket/IXWebSocketServer.cpp | 5 +++-- ixwebsocket/IXWebSocketTransport.cpp | 2 +- test/CMakeLists.txt | 2 +- 13 files changed, 23 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9aea0bdf..62c7bc08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.4.1) project(ixwebsocket C CXX) -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 14) set (CXX_STANDARD_REQUIRED ON) set (CMAKE_CXX_EXTENSIONS OFF) diff --git a/examples/broadcast_server/CMakeLists.txt b/examples/broadcast_server/CMakeLists.txt index bd37cb50..f845a384 100644 --- a/examples/broadcast_server/CMakeLists.txt +++ b/examples/broadcast_server/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to- set (OPENSSL_PREFIX /usr/local/opt/openssl) # Homebrew openssl -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 14) option(USE_TLS "Add TLS support" ON) diff --git a/examples/chat/CMakeLists.txt b/examples/chat/CMakeLists.txt index c878e35b..8970caf7 100644 --- a/examples/chat/CMakeLists.txt +++ b/examples/chat/CMakeLists.txt @@ -7,7 +7,7 @@ cmake_minimum_required (VERSION 3.4.1) project (cmd_websocket_chat) -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 14) option(USE_TLS "Add TLS support" ON) diff --git a/examples/cobra_publisher/CMakeLists.txt b/examples/cobra_publisher/CMakeLists.txt index 625f7a12..f3a1f252 100644 --- a/examples/cobra_publisher/CMakeLists.txt +++ b/examples/cobra_publisher/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to- set (OPENSSL_PREFIX /usr/local/opt/openssl) # Homebrew openssl -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 14) option(USE_TLS "Add TLS support" ON) diff --git a/examples/echo_server/CMakeLists.txt b/examples/echo_server/CMakeLists.txt index 069afdb1..b54e54c4 100644 --- a/examples/echo_server/CMakeLists.txt +++ b/examples/echo_server/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to- set (OPENSSL_PREFIX /usr/local/opt/openssl) # Homebrew openssl -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 14) option(USE_TLS "Add TLS support" ON) diff --git a/examples/ping_pong/CMakeLists.txt b/examples/ping_pong/CMakeLists.txt index 95ad2585..563a2650 100644 --- a/examples/ping_pong/CMakeLists.txt +++ b/examples/ping_pong/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required (VERSION 3.4.1) project (ping_pong) -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 14) option(USE_TLS "Add TLS support" ON) diff --git a/examples/ws_connect/CMakeLists.txt b/examples/ws_connect/CMakeLists.txt index 64c95a79..e620c39f 100644 --- a/examples/ws_connect/CMakeLists.txt +++ b/examples/ws_connect/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required (VERSION 3.4.1) project (ws_connect) -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 14) option(USE_TLS "Add TLS support" ON) diff --git a/ixwebsocket/IXWebSocketPerMessageDeflate.cpp b/ixwebsocket/IXWebSocketPerMessageDeflate.cpp index 2839cbe9..25376c11 100644 --- a/ixwebsocket/IXWebSocketPerMessageDeflate.cpp +++ b/ixwebsocket/IXWebSocketPerMessageDeflate.cpp @@ -51,16 +51,16 @@ namespace ix { - WebSocketPerMessageDeflate::WebSocketPerMessageDeflate() + WebSocketPerMessageDeflate::WebSocketPerMessageDeflate() : + _compressor(std::make_unique()), + _decompressor(std::make_unique()) { - _compressor.reset(new WebSocketPerMessageDeflateCompressor()); - _decompressor.reset(new WebSocketPerMessageDeflateDecompressor()); + ; } WebSocketPerMessageDeflate::~WebSocketPerMessageDeflate() { - _compressor.reset(); - _decompressor.reset(); + ; } bool WebSocketPerMessageDeflate::init(const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions) diff --git a/ixwebsocket/IXWebSocketPerMessageDeflate.h b/ixwebsocket/IXWebSocketPerMessageDeflate.h index b8370b24..779b0524 100644 --- a/ixwebsocket/IXWebSocketPerMessageDeflate.h +++ b/ixwebsocket/IXWebSocketPerMessageDeflate.h @@ -54,7 +54,7 @@ namespace ix bool decompress(const std::string& in, std::string& out); private: - std::shared_ptr _compressor; - std::shared_ptr _decompressor; + std::unique_ptr _compressor; + std::unique_ptr _decompressor; }; } diff --git a/ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp b/ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp index 3cbc7a9e..9f399923 100644 --- a/ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp +++ b/ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp @@ -55,7 +55,8 @@ namespace ix if (ret != Z_OK) return false; - _compressBuffer.reset(new unsigned char[_compressBufferSize]); + _compressBuffer = std::make_unique(_compressBufferSize); + _flush = (clientNoContextTakeOver) ? Z_FULL_FLUSH : Z_SYNC_FLUSH; @@ -153,7 +154,8 @@ namespace ix if (ret != Z_OK) return false; - _compressBuffer.reset(new unsigned char[_compressBufferSize]); + _compressBuffer = std::make_unique(_compressBufferSize); + _flush = (clientNoContextTakeOver) ? Z_FULL_FLUSH : Z_SYNC_FLUSH; diff --git a/ixwebsocket/IXWebSocketServer.cpp b/ixwebsocket/IXWebSocketServer.cpp index a6c07490..c33f274e 100644 --- a/ixwebsocket/IXWebSocketServer.cpp +++ b/ixwebsocket/IXWebSocketServer.cpp @@ -51,7 +51,7 @@ namespace ix void WebSocketServer::handleConnection(int fd) { - std::shared_ptr webSocket(new WebSocket); + auto webSocket = std::make_shared(); _onConnectionCallback(webSocket); webSocket->disableAutomaticReconnection(); @@ -99,6 +99,7 @@ namespace ix size_t WebSocketServer::getConnectedClientsCount() { - return getClients().size(); + std::lock_guard lock(_clientsMutex); + return _clients.size(); } } diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 47310dc0..4c92a901 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -538,7 +538,7 @@ namespace ix while (_txbuf.size()) { - int ret = _socket->send((char*)&_txbuf[0], _txbuf.size()); + ssize_t ret = _socket->send((char*)&_txbuf[0], _txbuf.size()); if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK || _socket->getErrno() == EAGAIN)) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d9498c10..c763530e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,7 +8,7 @@ project (ixwebsocket_unittest) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../third_party/sanitizers-cmake/cmake" ${CMAKE_MODULE_PATH}) find_package(Sanitizers) -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 14) if (NOT WIN32) option(USE_TLS "Add TLS support" ON)