C++14 + use make_unique and make_shared to make shared pointers

This commit is contained in:
Benjamin Sergeant 2019-01-11 21:25:06 -08:00
parent 7fabd14a63
commit a0f15bfb56
13 changed files with 23 additions and 20 deletions

View File

@ -6,7 +6,7 @@
cmake_minimum_required(VERSION 3.4.1) cmake_minimum_required(VERSION 3.4.1)
project(ixwebsocket C CXX) project(ixwebsocket C CXX)
set (CMAKE_CXX_STANDARD 11) set (CMAKE_CXX_STANDARD 14)
set (CXX_STANDARD_REQUIRED ON) set (CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF) set (CMAKE_CXX_EXTENSIONS OFF)

View File

@ -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 (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) option(USE_TLS "Add TLS support" ON)

View File

@ -7,7 +7,7 @@
cmake_minimum_required (VERSION 3.4.1) cmake_minimum_required (VERSION 3.4.1)
project (cmd_websocket_chat) project (cmd_websocket_chat)
set (CMAKE_CXX_STANDARD 11) set (CMAKE_CXX_STANDARD 14)
option(USE_TLS "Add TLS support" ON) option(USE_TLS "Add TLS support" ON)

View File

@ -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 (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) option(USE_TLS "Add TLS support" ON)

View File

@ -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 (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) option(USE_TLS "Add TLS support" ON)

View File

@ -6,7 +6,7 @@
cmake_minimum_required (VERSION 3.4.1) cmake_minimum_required (VERSION 3.4.1)
project (ping_pong) project (ping_pong)
set (CMAKE_CXX_STANDARD 11) set (CMAKE_CXX_STANDARD 14)
option(USE_TLS "Add TLS support" ON) option(USE_TLS "Add TLS support" ON)

View File

@ -6,7 +6,7 @@
cmake_minimum_required (VERSION 3.4.1) cmake_minimum_required (VERSION 3.4.1)
project (ws_connect) project (ws_connect)
set (CMAKE_CXX_STANDARD 11) set (CMAKE_CXX_STANDARD 14)
option(USE_TLS "Add TLS support" ON) option(USE_TLS "Add TLS support" ON)

View File

@ -51,16 +51,16 @@
namespace ix namespace ix
{ {
WebSocketPerMessageDeflate::WebSocketPerMessageDeflate() WebSocketPerMessageDeflate::WebSocketPerMessageDeflate() :
_compressor(std::make_unique<WebSocketPerMessageDeflateCompressor>()),
_decompressor(std::make_unique<WebSocketPerMessageDeflateDecompressor>())
{ {
_compressor.reset(new WebSocketPerMessageDeflateCompressor()); ;
_decompressor.reset(new WebSocketPerMessageDeflateDecompressor());
} }
WebSocketPerMessageDeflate::~WebSocketPerMessageDeflate() WebSocketPerMessageDeflate::~WebSocketPerMessageDeflate()
{ {
_compressor.reset(); ;
_decompressor.reset();
} }
bool WebSocketPerMessageDeflate::init(const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions) bool WebSocketPerMessageDeflate::init(const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions)

View File

@ -54,7 +54,7 @@ namespace ix
bool decompress(const std::string& in, std::string& out); bool decompress(const std::string& in, std::string& out);
private: private:
std::shared_ptr<WebSocketPerMessageDeflateCompressor> _compressor; std::unique_ptr<WebSocketPerMessageDeflateCompressor> _compressor;
std::shared_ptr<WebSocketPerMessageDeflateDecompressor> _decompressor; std::unique_ptr<WebSocketPerMessageDeflateDecompressor> _decompressor;
}; };
} }

View File

@ -55,7 +55,8 @@ namespace ix
if (ret != Z_OK) return false; if (ret != Z_OK) return false;
_compressBuffer.reset(new unsigned char[_compressBufferSize]); _compressBuffer = std::make_unique<unsigned char[]>(_compressBufferSize);
_flush = (clientNoContextTakeOver) _flush = (clientNoContextTakeOver)
? Z_FULL_FLUSH ? Z_FULL_FLUSH
: Z_SYNC_FLUSH; : Z_SYNC_FLUSH;
@ -153,7 +154,8 @@ namespace ix
if (ret != Z_OK) return false; if (ret != Z_OK) return false;
_compressBuffer.reset(new unsigned char[_compressBufferSize]); _compressBuffer = std::make_unique<unsigned char[]>(_compressBufferSize);
_flush = (clientNoContextTakeOver) _flush = (clientNoContextTakeOver)
? Z_FULL_FLUSH ? Z_FULL_FLUSH
: Z_SYNC_FLUSH; : Z_SYNC_FLUSH;

View File

@ -51,7 +51,7 @@ namespace ix
void WebSocketServer::handleConnection(int fd) void WebSocketServer::handleConnection(int fd)
{ {
std::shared_ptr<WebSocket> webSocket(new WebSocket); auto webSocket = std::make_shared<WebSocket>();
_onConnectionCallback(webSocket); _onConnectionCallback(webSocket);
webSocket->disableAutomaticReconnection(); webSocket->disableAutomaticReconnection();
@ -99,6 +99,7 @@ namespace ix
size_t WebSocketServer::getConnectedClientsCount() size_t WebSocketServer::getConnectedClientsCount()
{ {
return getClients().size(); std::lock_guard<std::mutex> lock(_clientsMutex);
return _clients.size();
} }
} }

View File

@ -538,7 +538,7 @@ namespace ix
while (_txbuf.size()) 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 || if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK ||
_socket->getErrno() == EAGAIN)) _socket->getErrno() == EAGAIN))

View File

@ -8,7 +8,7 @@ project (ixwebsocket_unittest)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../third_party/sanitizers-cmake/cmake" ${CMAKE_MODULE_PATH}) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../third_party/sanitizers-cmake/cmake" ${CMAKE_MODULE_PATH})
find_package(Sanitizers) find_package(Sanitizers)
set (CMAKE_CXX_STANDARD 11) set (CMAKE_CXX_STANDARD 14)
if (NOT WIN32) if (NOT WIN32)
option(USE_TLS "Add TLS support" ON) option(USE_TLS "Add TLS support" ON)