From 877f23525493aed24746fc3a0c85af2e862582ef Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 12:01:26 -0700 Subject: [PATCH 01/28] checked in ASIO standalone as a replacement for Boost --- .gitmodules | 3 +++ lib/asio | 1 + 2 files changed, 4 insertions(+) create mode 160000 lib/asio diff --git a/.gitmodules b/.gitmodules index 2644de04..6c672230 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "lib/rapidjson"] path = lib/rapidjson url = https://github.com/miloyip/rapidjson.git +[submodule "lib/asio"] + path = lib/asio + url = https://github.com/chriskohlhoff/asio.git diff --git a/lib/asio b/lib/asio new file mode 160000 index 00000000..230c0d2a --- /dev/null +++ b/lib/asio @@ -0,0 +1 @@ +Subproject commit 230c0d2ae035c5ce1292233fcab03cea0d341264 From cce6d86d38aeb73e600d92fc73eccdb6340e870f Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 12:47:31 -0700 Subject: [PATCH 02/28] upgraded websocketpp to tag 0.7.0 Previous tag did not have a proper notion of "ASIO_STANDALONE" --- lib/websocketpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/websocketpp b/lib/websocketpp index ac5d7ea5..378437ae 160000 --- a/lib/websocketpp +++ b/lib/websocketpp @@ -1 +1 @@ -Subproject commit ac5d7ea5af9734de965688b54a7860259887b537 +Subproject commit 378437aecdcb1dfe62096ffd5d944bf1f640ccc3 From 4a44d6d5e0b4608a15664230e6ee9b995d5c8bf1 Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 12:47:48 -0700 Subject: [PATCH 03/28] removed boost from the top level cmake file --- CMakeLists.txt | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c2fad3f..2a6c8a5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) PROJECT(sioclient) option(BUILD_SHARED_LIBS "Build the shared library" OFF) -option(Boost_USE_STATIC_LIBS "Use Boost static version" ON) set(MAJOR 1) set(MINOR 6) @@ -16,27 +15,32 @@ MESSAGE(SEND_ERROR "CMAKE_BUILD_TYPE must be either Release or Debug") return() endif() -set(BOOST_VER "1.55.0" CACHE STRING "boost version" ) - -set(Boost_USE_MULTITHREADED ON) -set(Boost_USE_STATIC_RUNTIME OFF) -find_package(Boost ${BOOST_VER} REQUIRED COMPONENTS system date_time random) - aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src ALL_SRC) aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src/internal ALL_SRC) file(GLOB ALL_HEADERS ${CMAKE_CURRENT_LIST_DIR}/src/*.h ) set(SIO_INCLUDEDIR ${CMAKE_CURRENT_LIST_DIR}) +add_definitions( + # These will force ASIO to compile without Boost + -DBOOST_DATE_TIME_NO_LIB + -DBOOST_REGEX_NO_LIB + -DASIO_STANDALONE + # These will force WebsocketPP to compile with C++11 + -D_WEBSOCKETPP_CPP11_STL_ + -D_WEBSOCKETPP_CPP11_FUNCTIONAL_ +) + add_library(sioclient ${ALL_SRC}) -target_include_directories(sioclient PRIVATE ${Boost_INCLUDE_DIRS} +target_include_directories(sioclient PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src ${CMAKE_CURRENT_LIST_DIR}/lib/websocketpp ${CMAKE_CURRENT_LIST_DIR}/lib/rapidjson/include + ${CMAKE_CURRENT_LIST_DIR}/lib/asio/asio/include ) set_property(TARGET sioclient PROPERTY CXX_STANDARD 11) set_property(TARGET sioclient PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(sioclient PRIVATE ${Boost_LIBRARIES}) + if(BUILD_SHARED_LIBS) set_target_properties(sioclient PROPERTIES @@ -49,16 +53,17 @@ list(APPEND TARGET_LIBRARIES sioclient) find_package(OpenSSL) if(OPENSSL_FOUND) add_library(sioclient_tls ${ALL_SRC}) -target_include_directories(sioclient_tls PRIVATE ${Boost_INCLUDE_DIRS} +target_include_directories(sioclient_tls PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src ${CMAKE_CURRENT_LIST_DIR}/lib/websocketpp ${CMAKE_CURRENT_LIST_DIR}/lib/rapidjson/include + ${CMAKE_CURRENT_LIST_DIR}/lib/asio/asio/include ${OPENSSL_INCLUDE_DIR} ) set_property(TARGET sioclient_tls PROPERTY CXX_STANDARD 11) set_property(TARGET sioclient_tls PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(sioclient_tls PRIVATE ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ) +target_link_libraries(sioclient_tls PRIVATE ${OPENSSL_LIBRARIES} ) target_compile_definitions(sioclient_tls PRIVATE -DSIO_TLS) if(BUILD_SHARED_LIBS) set_target_properties(sioclient_tls @@ -78,7 +83,3 @@ install(FILES ${ALL_HEADERS} install(TARGETS ${TARGET_LIBRARIES} DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" ) - -install(FILES ${Boost_LIBRARIES} - DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" -) From cc53a6f34b518e00dd8d8103e119935d9443fbbd Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 12:48:03 -0700 Subject: [PATCH 04/28] removed boost from sio_client_impl --- src/internal/sio_client_impl.cpp | 99 +++++++++++++++++--------------- src/internal/sio_client_impl.h | 26 +++++---- 2 files changed, 69 insertions(+), 56 deletions(-) diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index eae50161..41787242 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -7,8 +7,9 @@ // #include "sio_client_impl.h" +#include #include -#include +#include #include #include // Comment this out to disable handshake logging to stdout @@ -18,7 +19,13 @@ #define LOG(x) #endif -using boost::posix_time::milliseconds; +#if SIO_TLS +// If using Asio's SSL support, you will also need to add this #include. +// Source: http://think-async.com/Asio/asio-1.10.6/doc/asio/using.html +// #include +#endif + +using std::chrono::milliseconds; using namespace std; namespace sio @@ -43,18 +50,18 @@ namespace sio m_client.init_asio(); // Bind the clients we are using - using websocketpp::lib::placeholders::_1; - using websocketpp::lib::placeholders::_2; - m_client.set_open_handler(lib::bind(&client_impl::on_open,this,_1)); - m_client.set_close_handler(lib::bind(&client_impl::on_close,this,_1)); - m_client.set_fail_handler(lib::bind(&client_impl::on_fail,this,_1)); - m_client.set_message_handler(lib::bind(&client_impl::on_message,this,_1,_2)); + using std::placeholders::_1; + using std::placeholders::_2; + m_client.set_open_handler(std::bind(&client_impl::on_open,this,_1)); + m_client.set_close_handler(std::bind(&client_impl::on_close,this,_1)); + m_client.set_fail_handler(std::bind(&client_impl::on_fail,this,_1)); + m_client.set_message_handler(std::bind(&client_impl::on_message,this,_1,_2)); #if SIO_TLS - m_client.set_tls_init_handler(lib::bind(&client_impl::on_tls_init,this,_1)); + m_client.set_tls_init_handler(std::bind(&client_impl::on_tls_init,this,_1)); #endif - m_packet_mgr.set_decode_callback(lib::bind(&client_impl::on_decode,this,_1)); + m_packet_mgr.set_decode_callback(std::bind(&client_impl::on_decode,this,_1)); - m_packet_mgr.set_encode_callback(lib::bind(&client_impl::on_encode,this,_1,_2)); + m_packet_mgr.set_encode_callback(std::bind(&client_impl::on_encode,this,_1,_2)); } client_impl::~client_impl() @@ -102,8 +109,8 @@ namespace sio m_http_headers = headers; this->reset_states(); - m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,uri,m_query_string)); - m_network_thread.reset(new thread(lib::bind(&client_impl::run_loop,this)));//uri lifecycle? + m_client.get_io_service().dispatch(std::bind(&client_impl::connect_impl,this,uri,m_query_string)); + m_network_thread.reset(new thread(std::bind(&client_impl::run_loop,this)));//uri lifecycle? } @@ -141,14 +148,14 @@ namespace sio { m_con_state = con_closing; this->sockets_invoke_void(&sio::socket::close); - m_client.get_io_service().dispatch(lib::bind(&client_impl::close_impl, this,close::status::normal,"End by user")); + m_client.get_io_service().dispatch(std::bind(&client_impl::close_impl, this,close::status::normal,"End by user")); } void client_impl::sync_close() { m_con_state = con_closing; this->sockets_invoke_void(&sio::socket::close); - m_client.get_io_service().dispatch(lib::bind(&client_impl::close_impl, this,close::status::normal,"End by user")); + m_client.get_io_service().dispatch(std::bind(&client_impl::close_impl, this,close::status::normal,"End by user")); if(m_network_thread) { m_network_thread->join(); @@ -172,7 +179,7 @@ namespace sio } } - boost::asio::io_service& client_impl::get_io_service() + asio::io_service& client_impl::get_io_service() { return m_client.get_io_service(); } @@ -273,11 +280,11 @@ namespace sio } } - void client_impl::ping(const boost::system::error_code& ec) + void client_impl::ping(const asio::error_code& ec) { if(ec || m_con.expired()) { - if (ec != boost::asio::error::operation_aborted) + if (ec != asio::error::operation_aborted) LOG("ping exit,con is expired?"<expires_from_now(milliseconds(m_ping_interval), e_code); - m_ping_timer->async_wait(lib::bind(&client_impl::ping,this,lib::placeholders::_1)); + m_ping_timer->async_wait(std::bind(&client_impl::ping,this, std::placeholders::_1)); } if(!m_ping_timeout_timer) { - m_ping_timeout_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service())); - boost::system::error_code timeout_ec; + m_ping_timeout_timer.reset(new asio::steady_timer(m_client.get_io_service())); + std::error_code timeout_ec; m_ping_timeout_timer->expires_from_now(milliseconds(m_ping_timeout), timeout_ec); - m_ping_timeout_timer->async_wait(lib::bind(&client_impl::timeout_pong, this,lib::placeholders::_1)); + m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_pong, this, std::placeholders::_1)); } } - void client_impl::timeout_pong(const boost::system::error_code &ec) + void client_impl::timeout_pong(const asio::error_code &ec) { if(ec) { return; } LOG("Pong timeout"<reset_states(); LOG("Reconnecting..."<next_delay(); if(m_reconnect_listener) m_reconnect_listener(m_reconn_made,delay); - m_reconn_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service())); - boost::system::error_code ec; + m_reconn_timer.reset(new asio::steady_timer(m_client.get_io_service())); + asio::error_code ec; m_reconn_timer->expires_from_now(milliseconds(delay), ec); - m_reconn_timer->async_wait(lib::bind(&client_impl::timeout_reconnect,this,lib::placeholders::_1)); + m_reconn_timer->async_wait(std::bind(&client_impl::timeout_reconnect,this, std::placeholders::_1)); } else { @@ -427,10 +434,10 @@ namespace sio LOG("Reconnect for attempt:"<next_delay(); if(m_reconnect_listener) m_reconnect_listener(m_reconn_made,delay); - m_reconn_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service())); - boost::system::error_code ec; + m_reconn_timer.reset(new asio::steady_timer(m_client.get_io_service())); + asio::error_code ec; m_reconn_timer->expires_from_now(milliseconds(delay), ec); - m_reconn_timer->async_wait(lib::bind(&client_impl::timeout_reconnect,this,lib::placeholders::_1)); + m_reconn_timer->async_wait(std::bind(&client_impl::timeout_reconnect,this, std::placeholders::_1)); return; } reason = client::close_reason_drop; @@ -445,9 +452,9 @@ namespace sio void client_impl::on_message(connection_hdl con, client_type::message_ptr msg) { if (m_ping_timeout_timer) { - boost::system::error_code ec; + asio::error_code ec; m_ping_timeout_timer->expires_from_now(milliseconds(m_ping_timeout),ec); - m_ping_timeout_timer->async_wait(lib::bind(&client_impl::timeout_pong, this,lib::placeholders::_1)); + m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_pong, this, std::placeholders::_1)); } // Parse the incoming message according to socket.IO rules m_packet_mgr.put_payload(msg->get_payload()); @@ -485,17 +492,17 @@ namespace sio m_ping_timeout = 60000; } - m_ping_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service())); - boost::system::error_code ec; + m_ping_timer.reset(new asio::steady_timer(m_client.get_io_service())); + asio::error_code ec; m_ping_timer->expires_from_now(milliseconds(m_ping_interval), ec); if(ec)LOG("ec:"<async_wait(lib::bind(&client_impl::ping,this,lib::placeholders::_1)); + m_ping_timer->async_wait(std::bind(&client_impl::ping,this, std::placeholders::_1)); LOG("On handshake,sid:"< const& payload) { LOG("encoded payload length:"<length()<cancel(ec); @@ -565,11 +572,11 @@ namespace sio #if SIO_TLS client_impl::context_ptr client_impl::on_tls_init(connection_hdl conn) { - context_ptr ctx = context_ptr(new boost::asio::ssl::context(boost::asio::ssl::context::tlsv1)); - boost::system::error_code ec; - ctx->set_options(boost::asio::ssl::context::default_workarounds | - boost::asio::ssl::context::no_sslv2 | - boost::asio::ssl::context::single_dh_use,ec); + context_ptr ctx = context_ptr(new asio::ssl::context(asio::ssl::context::tlsv1)); + asio::error_code ec; + ctx->set_options(asio::ssl::context::default_workarounds | + asio::ssl::context::no_sslv2 | + asio::ssl::context::single_dh_use,ec); if(ec) { cerr<<"Init tls failed,reason:"<< ec.message()< #ifdef _WIN32 #define _WEBSOCKETPP_CPP11_THREAD_ -#define BOOST_ALL_NO_LIB //#define _WEBSOCKETPP_CPP11_RANDOM_DEVICE_ #define _WEBSOCKETPP_NO_CPP11_FUNCTIONAL_ #define INTIALIZER(__TYPE__) @@ -30,7 +29,14 @@ typedef websocketpp::config::asio_tls_client client_config; typedef websocketpp::config::asio_client client_config; #endif //SIO_TLS #endif //DEBUG -#include + +#if SIO_TLS +#include +#endif + +#include +#include +#include #include #include @@ -122,7 +128,7 @@ namespace sio void remove_socket(std::string const& nsp); - boost::asio::io_service& get_io_service(); + asio::io_service& get_io_service(); void on_socket_closed(std::string const& nsp); @@ -137,11 +143,11 @@ namespace sio void send_impl(std::shared_ptr const& payload_ptr,frame::opcode::value opcode); - void ping(const boost::system::error_code& ec); + void ping(const asio::error_code& ec); - void timeout_pong(const boost::system::error_code& ec); + void timeout_pong(const asio::error_code& ec); - void timeout_reconnect(boost::system::error_code const& ec); + void timeout_reconnect(asio::error_code const& ec); unsigned next_delay() const; @@ -171,7 +177,7 @@ namespace sio void clear_timers(); #if SIO_TLS - typedef websocketpp::lib::shared_ptr context_ptr; + typedef websocketpp::lib::shared_ptr context_ptr; context_ptr on_tls_init(connection_hdl con); #endif @@ -192,11 +198,11 @@ namespace sio packet_manager m_packet_mgr; - std::unique_ptr m_ping_timer; + std::unique_ptr m_ping_timer; - std::unique_ptr m_ping_timeout_timer; + std::unique_ptr m_ping_timeout_timer; - std::unique_ptr m_reconn_timer; + std::unique_ptr m_reconn_timer; con_state m_con_state; From 573f10e48eb0e99071f9d456313064ea08d05db1 Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 12:48:15 -0700 Subject: [PATCH 05/28] removed boost from sio_packet --- src/internal/sio_packet.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/internal/sio_packet.cpp b/src/internal/sio_packet.cpp index ec4551d9..374213cc 100755 --- a/src/internal/sio_packet.cpp +++ b/src/internal/sio_packet.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #define kBIN_PLACE_HOLDER "_placeholder" @@ -288,7 +287,7 @@ namespace sio pos++; if (_type == type_binary_event || _type == type_binary_ack) { size_t score_pos = payload_ptr.find('-'); - _pending_buffers = boost::lexical_cast(payload_ptr.substr(pos,score_pos - pos)); + _pending_buffers = static_cast(std::stoul(payload_ptr.substr(pos, score_pos - pos))); pos = score_pos+1; } } @@ -328,7 +327,7 @@ namespace sio if(pos(payload_ptr.substr(pos,json_pos - pos)); + _pack_id = std::stoi(payload_ptr.substr(pos,json_pos - pos)); } if (_frame == frame_message && (_type == type_binary_event || _type == type_binary_ack)) { //parse later when all buffers are arrived. From 6ba9c9cd4854d37dd7f1aa7ef57a4cb3eb73b27a Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 12:48:34 -0700 Subject: [PATCH 06/28] removed boost from sio_client --- src/sio_client.cpp | 1 - src/sio_socket.cpp | 26 ++++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/sio_client.cpp b/src/sio_client.cpp index 48d0ce20..d85f7bfe 100644 --- a/src/sio_client.cpp +++ b/src/sio_client.cpp @@ -8,7 +8,6 @@ #include "internal/sio_client_impl.h" using namespace websocketpp; -using boost::posix_time::milliseconds; using std::stringstream; namespace sio diff --git a/src/sio_socket.cpp b/src/sio_socket.cpp index b48134e9..a8d9aa58 100644 --- a/src/sio_socket.cpp +++ b/src/sio_socket.cpp @@ -1,10 +1,12 @@ #include "sio_socket.h" #include "internal/sio_packet.h" #include "internal/sio_client_impl.h" -#include -#include +#include +#include #include +#include #include +#include #if DEBUG || _DEBUG #define LOG(x) std::cout << x @@ -157,7 +159,7 @@ namespace sio void ack(int msgId,string const& name,message::list const& ack_message); - void timeout_connection(const boost::system::error_code &ec); + void timeout_connection(const asio::error_code &ec); void send_connect(); @@ -178,7 +180,7 @@ namespace sio error_listener m_error_listener; - std::unique_ptr m_connection_timer; + std::unique_ptr m_connection_timer; std::queue m_packet_queue; @@ -273,9 +275,9 @@ namespace sio } packet p(packet::type_connect,m_nsp); m_client->send(p); - m_connection_timer.reset(new boost::asio::deadline_timer(m_client->get_io_service())); - boost::system::error_code ec; - m_connection_timer->expires_from_now(boost::posix_time::milliseconds(20000), ec); + m_connection_timer.reset(new asio::steady_timer(m_client->get_io_service())); + asio::error_code ec; + m_connection_timer->expires_from_now(std::chrono::milliseconds(20000), ec); m_connection_timer->async_wait(std::bind(&socket::impl::timeout_connection,this, std::placeholders::_1)); } @@ -289,11 +291,11 @@ namespace sio if(!m_connection_timer) { - m_connection_timer.reset(new boost::asio::deadline_timer(m_client->get_io_service())); + m_connection_timer.reset(new asio::steady_timer(m_client->get_io_service())); } - boost::system::error_code ec; - m_connection_timer->expires_from_now(boost::posix_time::milliseconds(3000), ec); - m_connection_timer->async_wait(lib::bind(&socket::impl::on_close, this)); + asio::error_code ec; + m_connection_timer->expires_from_now(std::chrono::milliseconds(3000), ec); + m_connection_timer->async_wait(std::bind(&socket::impl::on_close, this)); } } @@ -475,7 +477,7 @@ namespace sio if(m_error_listener)m_error_listener(err_message); } - void socket::impl::timeout_connection(const boost::system::error_code &ec) + void socket::impl::timeout_connection(const asio::error_code &ec) { NULL_GUARD(m_client); if(ec) From fbc429e894ac76e43b282cf8735f17a4368c2cf7 Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 13:05:02 -0700 Subject: [PATCH 07/28] Added Catch++ as a replacement for Boost Test --- .gitmodules | 3 +++ lib/catch | 1 + 2 files changed, 4 insertions(+) create mode 160000 lib/catch diff --git a/.gitmodules b/.gitmodules index 6c672230..42509be9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "lib/asio"] path = lib/asio url = https://github.com/chriskohlhoff/asio.git +[submodule "lib/catch"] + path = lib/catch + url = https://github.com/philsquared/Catch.git diff --git a/lib/catch b/lib/catch new file mode 160000 index 00000000..9c07718b --- /dev/null +++ b/lib/catch @@ -0,0 +1 @@ +Subproject commit 9c07718b5f779bc1405f98ca6b5b693026f6eac7 From 51c1bba5cdd7187983e522b64a7dce6357240ad0 Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 13:05:20 -0700 Subject: [PATCH 08/28] removed boost from unit tests --- test/CMakeLists.txt | 7 +- test/sio_test.cpp | 222 ++++++++++++++++++++++---------------------- 2 files changed, 115 insertions(+), 114 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c9c78d81..849e2e22 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,9 +1,10 @@ cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) include(${CMAKE_CURRENT_SOURCE_DIR}/../CMakeLists.txt) -find_package(Boost ${BOOST_VER} REQUIRED COMPONENTS unit_test_framework) add_executable(sio_test sio_test.cpp) set_property(TARGET sio_test PROPERTY CXX_STANDARD 11) set_property(TARGET sio_test PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(sioclient PRIVATE ${Boost_LIBRARIES}) target_link_libraries(sio_test sioclient) -target_include_directories(sio_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../src" ${Boost_INCLUDE_DIRS} ) +target_include_directories(sio_test PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}/../lib/catch/include" + "${CMAKE_CURRENT_SOURCE_DIR}/../src" +) diff --git a/test/sio_test.cpp b/test/sio_test.cpp index fb6f3e50..45143d2b 100644 --- a/test/sio_test.cpp +++ b/test/sio_test.cpp @@ -10,65 +10,66 @@ #include #include -#define BOOST_TEST_MODULE sio_test +#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file +#include "catch.hpp" -#include #ifndef _WIN32 #include "json.hpp" //nlohmann::json cannot build in MSVC #endif using namespace sio; -BOOST_AUTO_TEST_SUITE(test_packet) -BOOST_AUTO_TEST_CASE( test_packet_construct_1 ) +TEST_CASE( "test_packet_construct_1" ) { packet p("/nsp",nullptr,1001,true); - BOOST_CHECK(p.get_frame() == packet::frame_message); - BOOST_CHECK(p.get_message() == nullptr); - BOOST_CHECK(p.get_nsp() == std::string("/nsp")); - BOOST_CHECK(p.get_pack_id() == 1001); + CHECK(p.get_frame() == packet::frame_message); + CHECK(p.get_message() == nullptr); + CHECK(p.get_nsp() == std::string("/nsp")); + CHECK(p.get_pack_id() == 1001); } -BOOST_AUTO_TEST_CASE( test_packet_construct_2 ) +TEST_CASE( "test_packet_construct_2" ) { packet p(packet::frame_ping); - BOOST_CHECK(p.get_frame() == packet::frame_ping); - BOOST_CHECK(p.get_message() == nullptr); - BOOST_CHECK(p.get_nsp() == std::string("")); - BOOST_CHECK(p.get_pack_id() == 0xFFFFFFFF); + CHECK(p.get_frame() == packet::frame_ping); + CHECK(p.get_message() == nullptr); + CHECK(p.get_nsp() == std::string("")); + CHECK(p.get_pack_id() == 0xFFFFFFFF); } -BOOST_AUTO_TEST_CASE( test_packet_construct_3 ) +TEST_CASE( "test_packet_construct_3" ) { packet p(packet::type_connect,"/nsp",nullptr); - BOOST_CHECK(p.get_frame() == packet::frame_message); - BOOST_CHECK(p.get_type() == packet::type_connect); - BOOST_CHECK(p.get_message() == nullptr); - BOOST_CHECK(p.get_nsp() == std::string("/nsp")); - BOOST_CHECK(p.get_pack_id() == 0xFFFFFFFF); + CHECK(p.get_frame() == packet::frame_message); + CHECK(p.get_type() == packet::type_connect); + CHECK(p.get_message() == nullptr); + CHECK(p.get_nsp() == std::string("/nsp")); + CHECK(p.get_pack_id() == 0xFFFFFFFF); } -BOOST_AUTO_TEST_CASE( test_packet_accept_1 ) +TEST_CASE( "test_packet_accept_1" ) { packet p(packet::type_connect,"/nsp",nullptr); std::string payload; std::vector > buffers; p.accept(payload,buffers); - BOOST_CHECK(buffers.size() == 0); - BOOST_CHECK_MESSAGE(payload == "40/nsp",std::string("outputing payload:")+payload); + CHECK(buffers.size() == 0); + CHECK(payload == "40/nsp"); + INFO("outputing payload:" << payload) } -BOOST_AUTO_TEST_CASE( test_packet_accept_2 ) +TEST_CASE( "test_packet_accept_2" ) { packet p(packet::frame_ping); std::string payload; std::vector > buffers; p.accept(payload,buffers); - BOOST_CHECK(buffers.size() == 0); - BOOST_CHECK_MESSAGE(payload == "2",std::string("outputing payload:")+payload); + CHECK(buffers.size() == 0); + CHECK(payload == "2"); + INFO("outputing payload:" << payload) } -BOOST_AUTO_TEST_CASE( test_packet_accept_3 ) +TEST_CASE( "test_packet_accept_3" ) { message::ptr array = array_message::create(); array->get_vector().push_back(string_message::create("event")); @@ -77,13 +78,14 @@ BOOST_AUTO_TEST_CASE( test_packet_accept_3 ) std::string payload; std::vector > buffers; p.accept(payload,buffers); - BOOST_CHECK(p.get_type() == packet::type_ack); - BOOST_CHECK(buffers.size() == 0); - BOOST_CHECK_MESSAGE(payload == "43/nsp,1001[\"event\",\"text\"]",std::string("outputing payload:")+payload); + CHECK(p.get_type() == packet::type_ack); + CHECK(buffers.size() == 0); + CHECK(payload == "43/nsp,1001[\"event\",\"text\"]"); + INFO("outputing payload:" << payload) } #ifndef _WIN32 -BOOST_AUTO_TEST_CASE( test_packet_accept_4 ) +TEST_CASE( "test_packet_accept_4" ) { message::ptr binObj = object_message::create(); binObj->get_map()["desc"] = string_message::create("Bin of 100 bytes"); @@ -98,132 +100,130 @@ BOOST_AUTO_TEST_CASE( test_packet_accept_4 ) std::string payload; std::vector > buffers; p.accept(payload,buffers); - BOOST_CHECK(p.get_type() == packet::type_binary_event); - BOOST_REQUIRE(buffers.size() == 2); + CHECK(p.get_type() == packet::type_binary_event); + REQUIRE(buffers.size() == 2); size_t json_start = payload.find("{"); - BOOST_REQUIRE(json_start!=std::string::npos); + REQUIRE(json_start!=std::string::npos); std::string header = payload.substr(0,json_start); - BOOST_CHECK_MESSAGE(header=="452-/nsp,1001",std::string("outputing payload header:")+header); + CHECK(header=="452-/nsp,1001",std::string("outputing payload header:")+header); std::string json = payload.substr(json_start); nlohmann::json j = nlohmann::json::parse(json); - BOOST_CHECK_MESSAGE(j["desc"].get() == "Bin of 100 bytes", std::string("outputing payload desc:") + j["desc"].get()); - BOOST_CHECK_MESSAGE((bool)j["bin1"]["_placeholder"] , std::string("outputing payload bin1:") + j["bin1"].dump()); - BOOST_CHECK_MESSAGE((bool)j["bin2"]["_placeholder"] , std::string("outputing payload bin2:") + j["bin2"].dump()); + CHECK(j["desc"].get() == "Bin of 100 bytes", std::string("outputing payload desc:") + j["desc"].get()); + CHECK((bool)j["bin1"]["_placeholder"] , std::string("outputing payload bin1:") + j["bin1"].dump()); + CHECK((bool)j["bin2"]["_placeholder"] , std::string("outputing payload bin2:") + j["bin2"].dump()); int bin1Num = j["bin1"]["num"].get(); char numchar[] = {0,0}; numchar[0] = bin1Num+'0'; - BOOST_CHECK_MESSAGE(buffers[bin1Num]->length()==101 , std::string("outputing payload bin1 num:")+numchar); - BOOST_CHECK(buffers[bin1Num]->at(50)==0 && buffers[bin1Num]->at(0) == packet::frame_message); + CHECK(buffers[bin1Num]->length()==101 , std::string("outputing payload bin1 num:")+numchar); + CHECK(buffers[bin1Num]->at(50)==0 && buffers[bin1Num]->at(0) == packet::frame_message); int bin2Num = j["bin2"]["num"].get(); numchar[0] = bin2Num+'0'; - BOOST_CHECK_MESSAGE(buffers[bin2Num]->length()==51 , std::string("outputing payload bin2 num:") + numchar); - BOOST_CHECK(buffers[bin2Num]->at(25)==1 && buffers[bin2Num]->at(0) == packet::frame_message); + CHECK(buffers[bin2Num]->length()==51 , std::string("outputing payload bin2 num:") + numchar); + CHECK(buffers[bin2Num]->at(25)==1 && buffers[bin2Num]->at(0) == packet::frame_message); } #endif -BOOST_AUTO_TEST_CASE( test_packet_parse_1 ) +TEST_CASE( "test_packet_parse_1" ) { packet p; bool hasbin = p.parse("42/nsp,1001[\"event\",\"text\"]"); - BOOST_CHECK(!hasbin); - BOOST_CHECK(p.get_frame() == packet::frame_message); - BOOST_CHECK(p.get_type() == packet::type_event); - BOOST_CHECK(p.get_nsp() == "/nsp"); - BOOST_CHECK(p.get_pack_id() == 1001); - BOOST_CHECK(p.get_message()->get_flag() == message::flag_array); - BOOST_REQUIRE(p.get_message()->get_vector()[0]->get_flag() == message::flag_string); - BOOST_CHECK(p.get_message()->get_vector()[0]->get_string() == "event"); - BOOST_REQUIRE(p.get_message()->get_vector()[1]->get_flag() == message::flag_string); - BOOST_CHECK(p.get_message()->get_vector()[1]->get_string() == "text"); + CHECK(!hasbin); + CHECK(p.get_frame() == packet::frame_message); + CHECK(p.get_type() == packet::type_event); + CHECK(p.get_nsp() == "/nsp"); + CHECK(p.get_pack_id() == 1001); + CHECK(p.get_message()->get_flag() == message::flag_array); + REQUIRE(p.get_message()->get_vector()[0]->get_flag() == message::flag_string); + CHECK(p.get_message()->get_vector()[0]->get_string() == "event"); + REQUIRE(p.get_message()->get_vector()[1]->get_flag() == message::flag_string); + CHECK(p.get_message()->get_vector()[1]->get_string() == "text"); hasbin = p.parse("431111[\"ack\",{\"count\":5}]"); - BOOST_CHECK(!hasbin); - BOOST_CHECK(p.get_frame() == packet::frame_message); - BOOST_CHECK(p.get_type() == packet::type_ack); - BOOST_CHECK(p.get_pack_id() == 1111); - BOOST_CHECK(p.get_nsp() == "/"); - BOOST_CHECK(p.get_message()->get_flag() == message::flag_array); - BOOST_REQUIRE(p.get_message()->get_vector()[0]->get_flag() == message::flag_string); - BOOST_CHECK(p.get_message()->get_vector()[0]->get_string() == "ack"); - BOOST_REQUIRE(p.get_message()->get_vector()[1]->get_flag() == message::flag_object); - BOOST_CHECK(p.get_message()->get_vector()[1]->get_map()["count"]->get_int() == 5); + CHECK(!hasbin); + CHECK(p.get_frame() == packet::frame_message); + CHECK(p.get_type() == packet::type_ack); + CHECK(p.get_pack_id() == 1111); + CHECK(p.get_nsp() == "/"); + CHECK(p.get_message()->get_flag() == message::flag_array); + REQUIRE(p.get_message()->get_vector()[0]->get_flag() == message::flag_string); + CHECK(p.get_message()->get_vector()[0]->get_string() == "ack"); + REQUIRE(p.get_message()->get_vector()[1]->get_flag() == message::flag_object); + CHECK(p.get_message()->get_vector()[1]->get_map()["count"]->get_int() == 5); } -BOOST_AUTO_TEST_CASE( test_packet_parse_2 ) +TEST_CASE( "test_packet_parse_2" ) { packet p; bool hasbin = p.parse("3"); - BOOST_CHECK(!hasbin); - BOOST_CHECK(p.get_frame() == packet::frame_pong); - BOOST_CHECK(!p.get_message()); - BOOST_CHECK(p.get_nsp() == "/"); - BOOST_CHECK(p.get_pack_id() == -1); + CHECK(!hasbin); + CHECK(p.get_frame() == packet::frame_pong); + CHECK(!p.get_message()); + CHECK(p.get_nsp() == "/"); + CHECK(p.get_pack_id() == -1); hasbin = p.parse("2"); - BOOST_CHECK(!hasbin); - BOOST_CHECK(p.get_frame() == packet::frame_ping); - BOOST_CHECK(!p.get_message()); - BOOST_CHECK(p.get_nsp() == "/"); - BOOST_CHECK(p.get_pack_id() == -1); + CHECK(!hasbin); + CHECK(p.get_frame() == packet::frame_ping); + CHECK(!p.get_message()); + CHECK(p.get_nsp() == "/"); + CHECK(p.get_pack_id() == -1); } -BOOST_AUTO_TEST_CASE( test_packet_parse_3 ) +TEST_CASE( "test_packet_parse_3" ) { packet p; bool hasbin = p.parse("40/nsp"); - BOOST_CHECK(!hasbin); - BOOST_CHECK(p.get_type() == packet::type_connect); - BOOST_CHECK(p.get_frame() == packet::frame_message); - BOOST_CHECK(p.get_nsp() == "/nsp"); - BOOST_CHECK(p.get_pack_id() == -1); - BOOST_CHECK(!p.get_message()); + CHECK(!hasbin); + CHECK(p.get_type() == packet::type_connect); + CHECK(p.get_frame() == packet::frame_message); + CHECK(p.get_nsp() == "/nsp"); + CHECK(p.get_pack_id() == -1); + CHECK(!p.get_message()); p.parse("40"); - BOOST_CHECK(p.get_type() == packet::type_connect); - BOOST_CHECK(p.get_nsp() == "/"); - BOOST_CHECK(p.get_pack_id() == -1); - BOOST_CHECK(!p.get_message()); + CHECK(p.get_type() == packet::type_connect); + CHECK(p.get_nsp() == "/"); + CHECK(p.get_pack_id() == -1); + CHECK(!p.get_message()); p.parse("44\"error\""); - BOOST_CHECK(p.get_type() == packet::type_error); - BOOST_CHECK(p.get_nsp() == "/"); - BOOST_CHECK(p.get_pack_id() == -1); - BOOST_CHECK(p.get_message()->get_flag() == message::flag_string); + CHECK(p.get_type() == packet::type_error); + CHECK(p.get_nsp() == "/"); + CHECK(p.get_pack_id() == -1); + CHECK(p.get_message()->get_flag() == message::flag_string); p.parse("44/nsp,\"error\""); - BOOST_CHECK(p.get_type() == packet::type_error); - BOOST_CHECK(p.get_nsp() == "/nsp"); - BOOST_CHECK(p.get_pack_id() == -1); - BOOST_CHECK(p.get_message()->get_flag() == message::flag_string); + CHECK(p.get_type() == packet::type_error); + CHECK(p.get_nsp() == "/nsp"); + CHECK(p.get_pack_id() == -1); + CHECK(p.get_message()->get_flag() == message::flag_string); } -BOOST_AUTO_TEST_CASE( test_packet_parse_4 ) +TEST_CASE( "test_packet_parse_4" ) { packet p; bool hasbin = p.parse("452-/nsp,101[\"bin_event\",[{\"_placeholder\":true,\"num\":1},{\"_placeholder\":true,\"num\":0},\"text\"]]"); - BOOST_CHECK(hasbin); + CHECK(hasbin); char buf[101]; buf[0] = packet::frame_message; memset(buf+1,0,100); std::string bufstr(buf,101); std::string bufstr2(buf,51); - BOOST_CHECK(p.parse_buffer(bufstr)); - BOOST_CHECK(!p.parse_buffer(bufstr2)); + CHECK(p.parse_buffer(bufstr)); + CHECK(!p.parse_buffer(bufstr2)); - BOOST_CHECK(p.get_frame() == packet::frame_message); - BOOST_CHECK(p.get_nsp() == "/nsp"); - BOOST_CHECK(p.get_pack_id() == 101); + CHECK(p.get_frame() == packet::frame_message); + CHECK(p.get_nsp() == "/nsp"); + CHECK(p.get_pack_id() == 101); message::ptr msg = p.get_message(); - BOOST_REQUIRE(msg&&msg->get_flag() == message::flag_array); - BOOST_CHECK(msg->get_vector()[0]->get_string() == "bin_event"); + REQUIRE(msg); + REQUIRE(msg->get_flag() == message::flag_array); + CHECK(msg->get_vector()[0]->get_string() == "bin_event"); message::ptr array = msg->get_vector()[1]; - BOOST_REQUIRE(array->get_flag() == message::flag_array); - BOOST_REQUIRE(array->get_vector()[0]->get_flag() == message::flag_binary); - BOOST_REQUIRE(array->get_vector()[1]->get_flag() == message::flag_binary); - BOOST_REQUIRE(array->get_vector()[2]->get_flag() == message::flag_string); - BOOST_CHECK(array->get_vector()[0]->get_binary()->size() == 50); - BOOST_CHECK(array->get_vector()[1]->get_binary()->size() == 100); - BOOST_CHECK(array->get_vector()[2]->get_string() == "text"); + REQUIRE(array->get_flag() == message::flag_array); + REQUIRE(array->get_vector()[0]->get_flag() == message::flag_binary); + REQUIRE(array->get_vector()[1]->get_flag() == message::flag_binary); + REQUIRE(array->get_vector()[2]->get_flag() == message::flag_string); + CHECK(array->get_vector()[0]->get_binary()->size() == 50); + CHECK(array->get_vector()[1]->get_binary()->size() == 100); + CHECK(array->get_vector()[2]->get_string() == "text"); } - -BOOST_AUTO_TEST_SUITE_END() - From 7bfaaf39e566518336ac217d13e0bfcfc6cf7925 Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 13:12:01 -0700 Subject: [PATCH 09/28] removed Boost from docs --- BOOST.md | 21 --------------------- INSTALL.md | 51 ++++++++------------------------------------------- 2 files changed, 8 insertions(+), 64 deletions(-) delete mode 100644 BOOST.md diff --git a/BOOST.md b/BOOST.md deleted file mode 100644 index d9c46460..00000000 --- a/BOOST.md +++ /dev/null @@ -1,21 +0,0 @@ -## Boost setup - -1. Download boost from [boost.org](http://www.boost.org/). -1. Unpack boost to some place. -1. Run either .\bootstrap.bat (on Windows), or ./bootstrap.sh (on other operating systems) under boost folder. - -## Boost build (Build the necessary subset only) - -#### Windows (or other mainstream desktop platforms shall work too): -Run with following script will build the necessary subset: - -```bash -bjam install --prefix="" --with-system --with-date_time --with-random link=static runtime-link=shared threading=multi -``` -Optionally You can merge all output .lib files into a fat one,especially if you're not using cmake. - -In output folder, run: - -```bash -lib.exe /OUT:boost.lib * -``` diff --git a/INSTALL.md b/INSTALL.md index 614280df..49fc262d 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -1,48 +1,13 @@ ## Install ### With CMake -1. Install boost, see [Boost setup](#boost_setup) section. -2. Use `git clone --recurse-submodules https://github.com/socketio/socket.io-client-cpp.git` to clone your local repo. -3. Run `cmake -DBOOST_ROOT:STRING= -DBOOST_VER:STRING= ./` -4. Run `make install`(if makefile generated) or open generated project (if project file generated) to build. -5. Outputs is under `./build`, link with the all static libs under `./build/lib` and include headers under `./build/include` in your client code where you want to use it. - -* If you're using boost without install,you can specify `boost include dir` and `boost lib dir` separately by: -```bash -cmake --DBOOST_INCLUDEDIR= --DBOOST_LIBRARYDIR= --DBOOST_VER:STRING= -./ -``` -* CMake didn't allow merging static libraries,but they're all copied to `./build/lib`, you can DIY if you like. +1. Use `git clone --recurse-submodules https://github.com/socketio/socket.io-client-cpp.git` to clone your local repo. +2. Run `cmake ./` +3. Run `make install`(if makefile generated) or open generated project (if project file generated) to build. +4. Outputs is under `./build`, link with the all static libs under `./build/lib` and include headers under `./build/include` in your client code where you want to use it. ### Without CMake -1. Install boost, see [Boost setup](#boost_setup) section. -2. Use `git clone --recurse-submodules https://github.com/socketio/socket.io-client-cpp.git` to clone your local repo. -3. Add `/include`,`./lib/websocketpp` and `./lib/rapidjson/include` to headers search path. -4. Include all files under `./src` in your project, add `sio_client.cpp`,`sio_socket.cpp`,`internal/sio_client_impl.cpp`, `internal/sio_packet.cpp` to source list. -5. Add `/lib` to library search path, add `boost.lib`(Win32) or `-lboost`(Other) link option. -6. Include `sio_client.h` in your client code where you want to use it. - -## Boost setup - -1. Download boost from [boost.org](http://www.boost.org/). -1. Unpack boost to some place. -1. Run either .\bootstrap.bat (on Windows), or ./bootstrap.sh (on other operating systems) under boost folder. - -## Boost build (Build the necessary subset only) -Windows (or other mainstream desktop platforms shall work too): - -The following script will build the necessary subset: - -```bash -bjam install --prefix="" --with-system --with-date_time --with-random link=static runtime-link=shared threading=multi -``` -Optionally You can merge all output .lib files into a fat one, especially if you're not using cmake. - -In output folder, run: - -```bash -lib.exe /OUT:boost.lib * -``` +1. Use `git clone --recurse-submodules https://github.com/socketio/socket.io-client-cpp.git` to clone your local repo. +2. Add `./lib/asio/asio/include`, `./lib/websocketpp` and `./lib/rapidjson/include` to headers search path. +3. Include all files under `./src` in your project, add `sio_client.cpp`,`sio_socket.cpp`,`internal/sio_client_impl.cpp`, `internal/sio_packet.cpp` to source list. +4. Include `sio_client.h` in your client code where you want to use it. From 4dd6afd1c2efc4b5ab13a45844fc02507c2196a5 Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 13:19:30 -0700 Subject: [PATCH 10/28] avoid installing boost on Travis --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9c865bac..4b67cdbd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,11 +5,9 @@ compiler: before_install: - sudo apt-get install clang git libssl-dev - - sudo add-apt-repository -y ppa:boost-latest/ppa - sudo add-apt-repository -y ppa:kubuntu-ppa/backports - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test -y - sudo apt-get update -yqq - - sudo apt-get install -y libboost1.55-dev libboost-system1.55-dev libboost-date-time1.55-dev libboost-random1.55-dev - sudo apt-get install -y cmake g++-4.8 - sed -i -e 's/cmake_minimum_required(VERSION 3.1.0/cmake_minimum_required(VERSION 2.8.12/' ./CMakeLists.txt script: From 13fdb7aad866612fb643c1f7666610343fdf850e Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 13:43:50 -0700 Subject: [PATCH 11/28] integrated unit tests with CTest so it can be executed normally on Travis --- .travis.yml | 3 ++- CMakeLists.txt | 7 +++++++ test/CMakeLists.txt | 3 +-- test/README.md | 18 ------------------ 4 files changed, 10 insertions(+), 21 deletions(-) delete mode 100644 test/README.md diff --git a/.travis.yml b/.travis.yml index 4b67cdbd..eac4142e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ before_install: - sudo apt-get install -y cmake g++-4.8 - sed -i -e 's/cmake_minimum_required(VERSION 3.1.0/cmake_minimum_required(VERSION 2.8.12/' ./CMakeLists.txt script: - - cmake -D CMAKE_CXX_FLAGS=-std=c++11 . + - cmake -D CMAKE_CXX_FLAGS=-std=c++11 -D BUILD_UNIT_TESTS=ON . - make + - make test - make install diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a6c8a5a..4187f21f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) PROJECT(sioclient) option(BUILD_SHARED_LIBS "Build the shared library" OFF) +option(BUILD_UNIT_TESTS "Builds unit tests target" OFF) set(MAJOR 1) set(MINOR 6) @@ -83,3 +84,9 @@ install(FILES ${ALL_HEADERS} install(TARGETS ${TARGET_LIBRARIES} DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" ) + +if(BUILD_UNIT_TESTS) +message(STATUS "Building with unit test support.") +enable_testing() +add_subdirectory(test) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 849e2e22..5da7085d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,3 @@ -cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) -include(${CMAKE_CURRENT_SOURCE_DIR}/../CMakeLists.txt) add_executable(sio_test sio_test.cpp) set_property(TARGET sio_test PROPERTY CXX_STANDARD 11) set_property(TARGET sio_test PROPERTY CXX_STANDARD_REQUIRED ON) @@ -8,3 +6,4 @@ target_include_directories(sio_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../lib/catch/include" "${CMAKE_CURRENT_SOURCE_DIR}/../src" ) +add_test(sioclient_test sio_test) diff --git a/test/README.md b/test/README.md deleted file mode 100644 index ee7b663f..00000000 --- a/test/README.md +++ /dev/null @@ -1,18 +0,0 @@ -You need have your boost unpacked on your disk, at least staged following modules: - -* system -* date_time -* random -* unit_test_framework - -Then use following instruction to gen makefile or VS project. -```bash -cmake -DBOOST_LIBRARYDIR=`` -DBOOST_INCLUDEDIR=`` -DBOOST_VER:STRING=`` -DCMAKE_BUILD_TYPE=Debug ./ -``` -Then run `make` or open by VS. - -For example I've installed boost 1.57.0 at `D:\boost_1_57_0` and staged the static lib at `D\boost_1_57_0\build\lib` then the command should be: -```bash -cmake -DBOOST_LIBRARYDIR=D:\boost_1_57_0\build\lib -DBOOST_INCLUDEDIR=D:\boost_1_57_0 -DBOOST_VER:STRING=1.57.0 -DCMAKE_BUILD_TYPE=Debug ./ -``` -In this case(Windows) CMake will create a VS project under `./test` folder. Open in VS and run it. \ No newline at end of file From c300bc8b6816596826305eede9679c7cecce3036 Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sun, 10 Sep 2017 13:53:26 -0700 Subject: [PATCH 12/28] fixed invalid calls to CHECK for non-windows platforms --- test/sio_test.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/test/sio_test.cpp b/test/sio_test.cpp index 45143d2b..2ee159bb 100644 --- a/test/sio_test.cpp +++ b/test/sio_test.cpp @@ -105,21 +105,29 @@ TEST_CASE( "test_packet_accept_4" ) size_t json_start = payload.find("{"); REQUIRE(json_start!=std::string::npos); std::string header = payload.substr(0,json_start); - CHECK(header=="452-/nsp,1001",std::string("outputing payload header:")+header); + CHECK(header=="452-/nsp,1001"); + INFO("outputing payload:" << payload) std::string json = payload.substr(json_start); nlohmann::json j = nlohmann::json::parse(json); - CHECK(j["desc"].get() == "Bin of 100 bytes", std::string("outputing payload desc:") + j["desc"].get()); - CHECK((bool)j["bin1"]["_placeholder"] , std::string("outputing payload bin1:") + j["bin1"].dump()); - CHECK((bool)j["bin2"]["_placeholder"] , std::string("outputing payload bin2:") + j["bin2"].dump()); + CHECK(j["desc"].get() == "Bin of 100 bytes"); + INFO("outputing payload desc::" << j["desc"].get()) + CHECK((bool)j["bin1"]["_placeholder"]); + INFO("outputing payload bin1:" << j["bin1"].dump()) + CHECK((bool)j["bin2"]["_placeholder"]); + INFO("outputing payload bin2:" << j["bin2"].dump()) int bin1Num = j["bin1"]["num"].get(); char numchar[] = {0,0}; numchar[0] = bin1Num+'0'; - CHECK(buffers[bin1Num]->length()==101 , std::string("outputing payload bin1 num:")+numchar); - CHECK(buffers[bin1Num]->at(50)==0 && buffers[bin1Num]->at(0) == packet::frame_message); + CHECK(buffers[bin1Num]->length()==101); + INFO("outputing payload bin1 num:" << numchar) + CHECK(buffers[bin1Num]->at(50)==0); + CHECK(buffers[bin1Num]->at(0) == packet::frame_message); int bin2Num = j["bin2"]["num"].get(); numchar[0] = bin2Num+'0'; - CHECK(buffers[bin2Num]->length()==51 , std::string("outputing payload bin2 num:") + numchar); - CHECK(buffers[bin2Num]->at(25)==1 && buffers[bin2Num]->at(0) == packet::frame_message); + CHECK(buffers[bin2Num]->length()==51); + INFO("outputing payload bin2 num:" << numchar) + CHECK(buffers[bin2Num]->at(25)==1); + CHECK(buffers[bin2Num]->at(0) == packet::frame_message); } #endif From ab62b31f79a6af04e55f55deda8ec4fb97fff9c3 Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Wed, 30 Oct 2019 13:12:36 +0300 Subject: [PATCH 13/28] fix build on some compilers --- src/internal/sio_client_impl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index e8da085e..edc2e296 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -279,7 +279,7 @@ namespace sio if(ec || m_con.expired()) { if (ec != boost::asio::error::operation_aborted) - LOG("ping exit,con is expired?"<expires_from_now(milliseconds(m_ping_interval), ec); - if(ec)LOG("ec:"<async_wait(lib::bind(&client_impl::ping,this,lib::placeholders::_1)); LOG("On handshake,sid:"< Date: Fri, 8 Nov 2019 17:06:33 +0300 Subject: [PATCH 14/28] fix release version LOG --- src/internal/sio_client_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index 27fbea74..a0e067d5 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -16,7 +16,7 @@ #if DEBUG || _DEBUG #define LOG(x) std::cout << x #else -#define LOG(x) +#define LOG(x) do {} while (0) #endif #if SIO_TLS From a41c94472d22cddec9a2c8525535b4df68c4bbf2 Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Fri, 8 Nov 2019 17:07:28 +0300 Subject: [PATCH 15/28] fix release version LOG --- src/sio_socket.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sio_socket.cpp b/src/sio_socket.cpp index e3a14736..6306ab1c 100644 --- a/src/sio_socket.cpp +++ b/src/sio_socket.cpp @@ -11,7 +11,7 @@ #if DEBUG || _DEBUG #define LOG(x) std::cout << x #else -#define LOG(x) +#define LOG(x) do {} while (0) #endif #define NULL_GUARD(_x_) \ From cb7e1f0f55517b96b3c7bb2b6b3b43399271cdf0 Mon Sep 17 00:00:00 2001 From: "a.cherdantceva" <> Date: Wed, 23 Dec 2020 18:56:27 +0300 Subject: [PATCH 16/28] use original rapidjson v1.1.0 --- .gitmodules | 2 +- lib/rapidjson | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 42509be9..dbeff05d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,7 +3,7 @@ url = https://github.com/zaphoyd/websocketpp.git [submodule "lib/rapidjson"] path = lib/rapidjson - url = https://github.com/miloyip/rapidjson.git + url = https://github.com/Tencent/rapidjson.git [submodule "lib/asio"] path = lib/asio url = https://github.com/chriskohlhoff/asio.git diff --git a/lib/rapidjson b/lib/rapidjson index a36110e1..f54b0e47 160000 --- a/lib/rapidjson +++ b/lib/rapidjson @@ -1 +1 @@ -Subproject commit a36110e11874bcf35af854940e0ce910c19a8b49 +Subproject commit f54b0e47a08782a6131cc3d60f94d038fa6e0a51 From 9d6e6c9a6c73e543c68ca227eeefe689586a0317 Mon Sep 17 00:00:00 2001 From: "a.cherdantceva" <> Date: Wed, 23 Dec 2020 18:57:37 +0300 Subject: [PATCH 17/28] use websocketpp v0.8.2 --- lib/websocketpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/websocketpp b/lib/websocketpp index 378437ae..56123c87 160000 --- a/lib/websocketpp +++ b/lib/websocketpp @@ -1 +1 @@ -Subproject commit 378437aecdcb1dfe62096ffd5d944bf1f640ccc3 +Subproject commit 56123c87598f8b1dd471be83ca841ceae07f95ba From 7771ecccdc67555930f8476aac1b4ff3b56cce50 Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Tue, 16 Mar 2021 15:02:31 +0300 Subject: [PATCH 18/28] added qmake project files --- .gitignore | 3 +- socket.io-client-depend.pri | 10 +++++++ socket.io-client.pri | 55 +++++++++++++++++++++++++++++++++++++ socket.io-client.pro | 43 +++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 socket.io-client-depend.pri create mode 100644 socket.io-client.pri create mode 100644 socket.io-client.pro diff --git a/.gitignore b/.gitignore index 989bd14a..355ee14b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ -**/*.user \ No newline at end of file +bin*/ +**/*.user diff --git a/socket.io-client-depend.pri b/socket.io-client-depend.pri new file mode 100644 index 00000000..60a87ba7 --- /dev/null +++ b/socket.io-client-depend.pri @@ -0,0 +1,10 @@ +include(socket.io-client.pri) + +msvc { + PRE_TARGETDEPS += $$SOCKET_IO_CLIENT_BIN/socket.io-client.lib +} else { + PRE_TARGETDEPS += $$SOCKET_IO_CLIENT_BIN/libsocket.io-client.a +} + +LIBS += -L$$SOCKET_IO_CLIENT_BIN +LIBS += -lsocket.io-client diff --git a/socket.io-client.pri b/socket.io-client.pri new file mode 100644 index 00000000..88bb249b --- /dev/null +++ b/socket.io-client.pri @@ -0,0 +1,55 @@ +SOCKET_IO_CLIENT_ROOT = $$PWD/socket.io-client +SOCKET_IO_CLIENT_SRC = $$SOCKET_IO_CLIENT_ROOT/src +SOCKET_IO_CLIENT_BIN = $$PWD/bin +SOCKET_IO_CLIENT_DEPEND = $$SOCKET_IO_CLIENT_ROOT/lib + +msvc { + lessThan(QMAKE_MSC_VER, 1900) { + DEFINES += _WEBSOCKETPP_NOEXCEPT_TOKEN_=_NOEXCEPT + } else { + DEFINES += _WEBSOCKETPP_NOEXCEPT_TOKEN_=noexcept + } + QMAKE_CXXFLAGS += /wd4503 +} else { + DEFINES += _WEBSOCKETPP_NOEXCEPT_TOKEN_=noexcept +} + +win32 { + DEFINES += _WINSOCK_DEPRECATED_NO_WARNINGS +} else { + DEFINES += _WEBSOCKETPP_CPP11_THREAD_ +} + +DEFINES += ASIO_STANDALONE +DEFINES += _WEBSOCKETPP_CPP11_TYPE_TRAITS_ +DEFINES += _WEBSOCKETPP_CPP11_MEMORY_ +DEFINES += _WEBSOCKETPP_CPP11_FUNCTIONAL_ +DEFINES += _WEBSOCKETPP_CPP11_CHRONO_ +DEFINES += _WEBSOCKETPP_CPP11_RANDOM_DEVICE_ +DEFINES += _WEBSOCKETPP_CPP11_REGEX_ +DEFINES += _WEBSOCKETPP_CPP11_SYSTEM_ERROR_ +DEFINES += _WEBSOCKETPP_INITIALIZER_LISTS_ +DEFINES += _WEBSOCKETPP_CONSTEXPR_TOKEN_= +DEFINES += RAPIDJSON_HAS_CXX11_RVALUE_REFS=1 + +macx:SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-macx +linux:SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-linux +win32 { + SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-win32 + msvc:SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-msvc +} +clang:SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-clang +else::gcc:SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-gcc + +SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-$$QT_ARCH + +CONFIG(debug, debug|release) { + SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN/debug +} + +INCLUDEPATH += \ + $$SOCKET_IO_CLIENT_SRC \ + $$SOCKET_IO_CLIENT_DEPEND/asio/asio/include \ + $$SOCKET_IO_CLIENT_DEPEND/catch/single_include \ + $$SOCKET_IO_CLIENT_DEPEND/rapidjson/include \ + $$SOCKET_IO_CLIENT_DEPEND/websocketpp diff --git a/socket.io-client.pro b/socket.io-client.pro new file mode 100644 index 00000000..8622f4e8 --- /dev/null +++ b/socket.io-client.pro @@ -0,0 +1,43 @@ +CONFIG -= qt + +TARGET = socket.io-client + +TEMPLATE = lib +CONFIG += staticlib + +include(socket.io-client.pri) + +CONFIG += c++11 warn_off + +unix|win32-g++ { + QMAKE_CXXFLAGS_WARN_OFF -= -w + QMAKE_CXXFLAGS += -Wall +} else { + win32 { + QMAKE_CXXFLAGS_WARN_OFF -= -W0 + QMAKE_CXXFLAGS += -W3 /wd4267 + DEFINES += _CRT_SECURE_NO_WARNINGS + DEFINES += _SCL_SECURE_NO_WARNINGS + } +} + +CONFIG(debug, debug|release) { + DEFINES += DEBUG=1 +} else { + DEFINES += NDEBUG +} + +HEADERS += \ + $$SOCKET_IO_CLIENT_SRC/sio_client.h \ + $$SOCKET_IO_CLIENT_SRC/sio_message.h \ + $$SOCKET_IO_CLIENT_SRC/sio_socket.h \ + $$SOCKET_IO_CLIENT_SRC/internal/sio_client_impl.h \ + $$SOCKET_IO_CLIENT_SRC/internal/sio_packet.h + +SOURCES += \ + $$SOCKET_IO_CLIENT_SRC/sio_client.cpp \ + $$SOCKET_IO_CLIENT_SRC/sio_socket.cpp \ + $$SOCKET_IO_CLIENT_SRC/internal/sio_client_impl.cpp \ + $$SOCKET_IO_CLIENT_SRC/internal/sio_packet.cpp + +DESTDIR = $$SOCKET_IO_CLIENT_BIN From 48f395326ae96ad3d675e59e8faf2d3a0301058b Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Tue, 16 Mar 2021 15:41:01 +0300 Subject: [PATCH 19/28] [qmake] fix SOCKET_IO_CLIENT_ROOT --- socket.io-client.pri | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/socket.io-client.pri b/socket.io-client.pri index 88bb249b..68332dec 100644 --- a/socket.io-client.pri +++ b/socket.io-client.pri @@ -1,4 +1,4 @@ -SOCKET_IO_CLIENT_ROOT = $$PWD/socket.io-client +SOCKET_IO_CLIENT_ROOT = $$PWD SOCKET_IO_CLIENT_SRC = $$SOCKET_IO_CLIENT_ROOT/src SOCKET_IO_CLIENT_BIN = $$PWD/bin SOCKET_IO_CLIENT_DEPEND = $$SOCKET_IO_CLIENT_ROOT/lib From 7036bfdc128086dcaef25e2add83c20e4661e692 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Sat, 9 Jan 2021 02:13:39 +0100 Subject: [PATCH 20/28] refactor: use correct Engine.IO protocol revision The 3rd revision is included in Socket.IO v1/v2, whereas the 4th is included in Socket.IO v3. Note: this query parameter is not currently checked on the server-side Reference: https://github.com/socketio/engine.io-protocol --- src/internal/sio_client_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index a0e067d5..3eae687f 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -222,7 +222,7 @@ namespace sio } else { ss<0){ ss<<"&sid="< Date: Thu, 15 Jul 2021 18:06:21 +0300 Subject: [PATCH 21/28] support both TLS and non TLS connections, if server url hashttps or wss schema TLS is used, otherwise TLS is not used --- socket.io-client-depend.pri | 18 ++ socket.io-client.pri | 3 + socket.io-client.pro | 6 + src/internal/sio_client_config.h | 42 +++ src/internal/sio_client_impl.cpp | 400 +++++++------------------ src/internal/sio_client_impl.h | 495 +++++++++++++++++++++++-------- src/sio_client.cpp | 53 ++-- src/sio_client.h | 50 ++-- src/sio_socket.cpp | 186 ++++++------ src/sio_socket.h | 6 +- 10 files changed, 697 insertions(+), 562 deletions(-) create mode 100644 src/internal/sio_client_config.h diff --git a/socket.io-client-depend.pri b/socket.io-client-depend.pri index 60a87ba7..d2c15549 100644 --- a/socket.io-client-depend.pri +++ b/socket.io-client-depend.pri @@ -8,3 +8,21 @@ msvc { LIBS += -L$$SOCKET_IO_CLIENT_BIN LIBS += -lsocket.io-client + +!isEmpty(OPENSSL_INCLUDE_DIR):!isEmpty(OPENSSL_LIB_DIR) { + + LIBS += -L$$OPENSSL_LIB_DIR + + msvc { + LIBS += -llibcrypto -llibssl + PRE_TARGETDEPS += \ + $$OPENSSL_LIB_DIR/libcrypto.lib \ + $$OPENSSL_LIB_DIR/libssl.lib + } else { + LIBS += -lcrypto -lssl + PRE_TARGETDEPS += \ + $$OPENSSL_LIB_DIR/libcrypto.a \ + $$OPENSSL_LIB_DIR/libssl.a + } +} + diff --git a/socket.io-client.pri b/socket.io-client.pri index 68332dec..5908c1bc 100644 --- a/socket.io-client.pri +++ b/socket.io-client.pri @@ -20,6 +20,9 @@ win32 { DEFINES += _WEBSOCKETPP_CPP11_THREAD_ } +OPENSSL_INCLUDE_DIR = $$(OPENSSL_INCLUDE_DIR) +OPENSSL_LIB_DIR = $$(OPENSSL_LIB_DIR) + DEFINES += ASIO_STANDALONE DEFINES += _WEBSOCKETPP_CPP11_TYPE_TRAITS_ DEFINES += _WEBSOCKETPP_CPP11_MEMORY_ diff --git a/socket.io-client.pro b/socket.io-client.pro index 8622f4e8..1cefe92a 100644 --- a/socket.io-client.pro +++ b/socket.io-client.pro @@ -7,6 +7,11 @@ CONFIG += staticlib include(socket.io-client.pri) +!isEmpty(OPENSSL_INCLUDE_DIR):!isEmpty(OPENSSL_LIB_DIR) { + DEFINES += HAVE_OPENSSL + INCLUDEPATH += $$OPENSSL_INCLUDE_DIR +} + CONFIG += c++11 warn_off unix|win32-g++ { @@ -31,6 +36,7 @@ HEADERS += \ $$SOCKET_IO_CLIENT_SRC/sio_client.h \ $$SOCKET_IO_CLIENT_SRC/sio_message.h \ $$SOCKET_IO_CLIENT_SRC/sio_socket.h \ + $$SOCKET_IO_CLIENT_SRC/internal/sio_client_config.h \ $$SOCKET_IO_CLIENT_SRC/internal/sio_client_impl.h \ $$SOCKET_IO_CLIENT_SRC/internal/sio_packet.h diff --git a/src/internal/sio_client_config.h b/src/internal/sio_client_config.h new file mode 100644 index 00000000..eec89b66 --- /dev/null +++ b/src/internal/sio_client_config.h @@ -0,0 +1,42 @@ +#pragma once + +#include + +namespace sio +{ +template +struct client_config : public INHERIT +{ + using type = client_config; + using base = INHERIT; + + using concurrency_type = typename base::concurrency_type; + + using request_type = typename base::request_type; + using response_type = typename base::response_type; + + using message_type = typename base::message_type; + using con_msg_manager_type = typename base::con_msg_manager_type; + using endpoint_msg_manager_type = typename base::endpoint_msg_manager_type; + + using alog_type = typename base::alog_type; + using elog_type = typename base::elog_type; + + using rng_type = typename base::rng_type; + + struct transport_config : public base::transport_config + { + using concurrency_type = type::concurrency_type; + using alog_type = type::alog_type; + using elog_type = type::elog_type; + using request_type = type::request_type; + using response_type = type::response_type; + using socket_type = SOCKET_TYPE; + + static const long timeout_dns_resolve = 15000; + }; + + using transport_type = + websocketpp::transport::asio::endpoint; +}; +} diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index 3eae687f..d15b7894 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -7,31 +7,16 @@ // #include "sio_client_impl.h" + #include #include -#include #include #include -// Comment this out to disable handshake logging to stdout -#if DEBUG || _DEBUG -#define LOG(x) std::cout << x -#else -#define LOG(x) do {} while (0) -#endif - -#if SIO_TLS -// If using Asio's SSL support, you will also need to add this #include. -// Source: http://think-async.com/Asio/asio-1.10.6/doc/asio/using.html -// #include -#endif - -using std::chrono::milliseconds; -using namespace std; namespace sio { /*************************public:*************************/ - client_impl::client_impl() : + client_impl_base::client_impl_base() : m_ping_interval(0), m_ping_timeout(0), m_network_thread(), @@ -41,36 +26,31 @@ namespace sio m_reconn_attempts(0xFFFFFFFF), m_reconn_made(0) { - using websocketpp::log::alevel; -#ifndef DEBUG - m_client.clear_access_channels(alevel::all); - m_client.set_access_channels(alevel::connect|alevel::disconnect|alevel::app); -#endif - // Initialize the Asio transport policy - m_client.init_asio(); - // Bind the clients we are using using std::placeholders::_1; using std::placeholders::_2; - m_client.set_open_handler(std::bind(&client_impl::on_open,this,_1)); - m_client.set_close_handler(std::bind(&client_impl::on_close,this,_1)); - m_client.set_fail_handler(std::bind(&client_impl::on_fail,this,_1)); - m_client.set_message_handler(std::bind(&client_impl::on_message,this,_1,_2)); -#if SIO_TLS - m_client.set_tls_init_handler(std::bind(&client_impl::on_tls_init,this,_1)); -#endif - m_packet_mgr.set_decode_callback(std::bind(&client_impl::on_decode,this,_1)); - - m_packet_mgr.set_encode_callback(std::bind(&client_impl::on_encode,this,_1,_2)); + + m_packet_mgr.set_decode_callback(std::bind(&client_impl_base::on_decode,this,_1)); + + m_packet_mgr.set_encode_callback(std::bind(&client_impl_base::on_encode,this,_1,_2)); + } + + void client_impl_base::clear_con_listeners() + { + m_open_listener = nullptr; + m_close_listener = nullptr; + m_fail_listener = nullptr; + m_reconnect_listener = nullptr; + m_reconnecting_listener = nullptr; } - - client_impl::~client_impl() + + void client_impl_base::clear_socket_listeners() { - this->sockets_invoke_void(&sio::socket::on_close); - sync_close(); + m_socket_open_listener = nullptr; + m_socket_close_listener = nullptr; } - - void client_impl::connect(const string& uri, const map& query, const map& headers) + + void client_impl_base::connect(const string& uri, const map& query, const map& headers) { if(m_reconn_timer) { @@ -109,13 +89,13 @@ namespace sio m_http_headers = headers; - this->reset_states(); - m_client.get_io_service().dispatch(std::bind(&client_impl::connect_impl,this,uri,m_query_string)); - m_network_thread.reset(new thread(std::bind(&client_impl::run_loop,this)));//uri lifecycle? + reset_states(); + get_io_service().dispatch(std::bind(&client_impl_base::connect_impl,this,uri,m_query_string)); + m_network_thread.reset(new thread(std::bind(&client_impl_base::run_loop,this)));//uri lifecycle? } - socket::ptr const& client_impl::socket(string const& nsp) + socket::ptr const& client_impl_base::socket(string const& nsp) { lock_guard guard(m_socket_mutex); string aux; @@ -145,18 +125,37 @@ namespace sio } } - void client_impl::close() + void client_impl_base::close() { m_con_state = con_closing; - this->sockets_invoke_void(&sio::socket::close); - m_client.get_io_service().dispatch(std::bind(&client_impl::close_impl, this,close::status::normal,"End by user")); + sockets_invoke_void(&sio::socket::close); + get_io_service().dispatch(std::bind(&client_impl_base::close_impl, this,close::status::normal,"End by user")); + } + + void client_impl_base::sync_close() + { + sync_close_internal(get_io_service()); + } + + void client_impl_base::set_reconnect_delay(unsigned millis) + { + m_reconn_delay = millis; + if (m_reconn_delay_max < millis) + m_reconn_delay_max = millis; + } + + void client_impl_base::set_reconnect_delay_max(unsigned millis) + { + m_reconn_delay_max = millis; + if (m_reconn_delay > millis) + m_reconn_delay = millis; } - void client_impl::sync_close() + void client_impl_base::sync_close_internal(lib::asio::io_service& service) { m_con_state = con_closing; - this->sockets_invoke_void(&sio::socket::close); - m_client.get_io_service().dispatch(std::bind(&client_impl::close_impl, this,close::status::normal,"End by user")); + sockets_invoke_void(&sio::socket::close); + service.dispatch(std::bind(&client_impl_base::close_impl, this,close::status::normal,"End by user")); if(m_network_thread) { m_network_thread->join(); @@ -165,12 +164,12 @@ namespace sio } /*************************protected:*************************/ - void client_impl::send(packet& p) + void client_impl_base::send(packet& p) { m_packet_mgr.encode(p); } - void client_impl::remove_socket(string const& nsp) + void client_impl_base::remove_socket(string const& nsp) { lock_guard guard(m_socket_mutex); auto it = m_sockets.find(nsp); @@ -180,147 +179,27 @@ namespace sio } } - asio::io_service& client_impl::get_io_service() - { - return m_client.get_io_service(); - } - - void client_impl::on_socket_closed(string const& nsp) + void client_impl_base::on_socket_closed(string const& nsp) { if(m_socket_close_listener)m_socket_close_listener(nsp); } - void client_impl::on_socket_opened(string const& nsp) + void client_impl_base::on_socket_opened(string const& nsp) { if(m_socket_open_listener)m_socket_open_listener(nsp); } - /*************************private:*************************/ - void client_impl::run_loop() - { - - m_client.run(); - m_client.reset(); - m_client.get_alog().write(websocketpp::log::alevel::devel, - "run loop end"); - } - - void client_impl::connect_impl(const string& uri, const string& queryString) - { - do{ - websocketpp::uri uo(uri); - ostringstream ss; -#if SIO_TLS - ss<<"wss://"; -#else - ss<<"ws://"; -#endif - const std::string host(uo.get_host()); - // As per RFC2732, literal IPv6 address should be enclosed in "[" and "]". - if(host.find(':')!=std::string::npos){ - ss<<"["<0){ - ss<<"&sid="<replace_header(header.first, header.second); - } - - m_client.connect(con); - return; - } - while(0); - if(m_fail_listener) - { - m_fail_listener(); - } - } - - void client_impl::close_impl(close::status::value const& code,string const& reason) - { - LOG("Close by reason:"<cancel(); - m_reconn_timer.reset(); - } - if (m_con.expired()) - { - cerr << "Error: No active session" << endl; - } - else - { - lib::error_code ec; - m_client.close(m_con, code, reason, ec); - } - } - - void client_impl::send_impl(shared_ptr const& payload_ptr,frame::opcode::value opcode) - { - if(m_con_state == con_opened) - { - lib::error_code ec; - m_client.send(m_con,*payload_ptr,opcode,ec); - if(ec) - { - cerr<<"Send failed,reason:"<< ec.message()< payload) - { - lib::error_code ec; - this->m_client.send(this->m_con, *payload, frame::opcode::text, ec); - }); - if(m_ping_timer) - { - asio::error_code e_code; - m_ping_timer->expires_from_now(milliseconds(m_ping_interval), e_code); - m_ping_timer->async_wait(std::bind(&client_impl::ping,this, std::placeholders::_1)); - } - if(!m_ping_timeout_timer) - { - m_ping_timeout_timer.reset(new asio::steady_timer(m_client.get_io_service())); - std::error_code timeout_ec; - m_ping_timeout_timer->expires_from_now(milliseconds(m_ping_timeout), timeout_ec); - m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_pong, this, std::placeholders::_1)); - } - } - - void client_impl::timeout_pong(const asio::error_code &ec) + void client_impl_base::timeout_pong(const asio::error_code &ec) { if(ec) { return; } LOG("Pong timeout"<reset_states(); + reset_states(); LOG("Reconnecting..."<(m_reconn_made,32);//protect the pow result to be too big. return static_cast(min(m_reconn_delay * pow(1.5,reconn_made),m_reconn_delay_max)); } - socket::ptr client_impl::get_socket_locked(string const& nsp) + socket::ptr client_impl_base::get_socket_locked(string const& nsp) { lock_guard guard(m_socket_mutex); auto it = m_sockets.find(nsp); @@ -358,7 +237,7 @@ namespace sio } } - void client_impl::sockets_invoke_void(void (sio::socket::*fn)(void)) + void client_impl_base::sockets_invoke_void(void (socket::*fn)(void)) { map socks; { @@ -370,102 +249,40 @@ namespace sio } } - void client_impl::on_fail(connection_hdl) + void client_impl_base::on_fail(connection_hdl) { m_con.reset(); m_con_state = con_closed; - this->sockets_invoke_void(&sio::socket::on_disconnect); + socket_invoke_disconnected(); LOG("Connection failed." << endl); if(m_reconn_madenext_delay(); + unsigned delay = next_delay(); if(m_reconnect_listener) m_reconnect_listener(m_reconn_made,delay); - m_reconn_timer.reset(new asio::steady_timer(m_client.get_io_service())); + m_reconn_timer.reset(new asio::steady_timer(get_io_service())); asio::error_code ec; m_reconn_timer->expires_from_now(milliseconds(delay), ec); - m_reconn_timer->async_wait(std::bind(&client_impl::timeout_reconnect,this, std::placeholders::_1)); + m_reconn_timer->async_wait(std::bind(&client_impl_base::timeout_reconnect,this, std::placeholders::_1)); } else { if(m_fail_listener)m_fail_listener(); } } - - void client_impl::on_open(connection_hdl con) + + void client_impl_base::on_open(connection_hdl con) { LOG("Connected." << endl); m_con_state = con_opened; m_con = con; m_reconn_made = 0; - this->sockets_invoke_void(&sio::socket::on_open); - this->socket(""); + sockets_invoke_void(&sio::socket::on_open); + socket(""); if(m_open_listener)m_open_listener(); } - - void client_impl::on_close(connection_hdl con) - { - LOG("Client Disconnected." << endl); - con_state m_con_state_was = m_con_state; - m_con_state = con_closed; - lib::error_code ec; - close::status::value code = close::status::normal; - client_type::connection_ptr conn_ptr = m_client.get_con_from_hdl(con, ec); - if (ec) { - LOG("OnClose get conn failed"<get_local_close_code(); - } - - m_con.reset(); - this->clear_timers(); - client::close_reason reason; - // If we initiated the close, no matter what the close status was, - // we'll consider it a normal close. (When using TLS, we can - // sometimes get a TLS Short Read error when closing.) - if(code == close::status::normal || m_con_state_was == con_closing) - { - this->sockets_invoke_void(&sio::socket::on_disconnect); - reason = client::close_reason_normal; - } - else - { - this->sockets_invoke_void(&sio::socket::on_disconnect); - if(m_reconn_madenext_delay(); - if(m_reconnect_listener) m_reconnect_listener(m_reconn_made,delay); - m_reconn_timer.reset(new asio::steady_timer(m_client.get_io_service())); - asio::error_code ec; - m_reconn_timer->expires_from_now(milliseconds(delay), ec); - m_reconn_timer->async_wait(std::bind(&client_impl::timeout_reconnect,this, std::placeholders::_1)); - return; - } - reason = client::close_reason_drop; - } - - if(m_close_listener) - { - m_close_listener(reason); - } - } - - void client_impl::on_message(connection_hdl, client_type::message_ptr msg) - { - if (m_ping_timeout_timer) { - asio::error_code ec; - m_ping_timeout_timer->expires_from_now(milliseconds(m_ping_timeout),ec); - m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_pong, this, std::placeholders::_1)); - } - // Parse the incoming message according to socket.IO rules - m_packet_mgr.put_payload(msg->get_payload()); - } - - void client_impl::on_handshake(message::ptr const& message) + void client_impl_base::on_handshake(message::ptr const& message) { if(message && message->get_flag() == message::flag_object) { @@ -497,20 +314,20 @@ namespace sio m_ping_timeout = 60000; } - m_ping_timer.reset(new asio::steady_timer(m_client.get_io_service())); + m_ping_timer.reset(new asio::steady_timer(get_io_service())); asio::error_code ec; m_ping_timer->expires_from_now(milliseconds(m_ping_interval), ec); if(ec)LOG("ec:"<async_wait(std::bind(&client_impl::ping,this, std::placeholders::_1)); + m_ping_timer->async_wait(std::bind(&client_impl_base::ping,this, std::placeholders::_1)); LOG("On handshake,sid:"<on_handshake(p.get_message()); + on_handshake(p.get_message()); break; case packet::frame_close: //FIXME how to deal? - this->close_impl(close::status::abnormal_close, "End by server"); + close_impl(close::status::abnormal_close, "End by server"); break; case packet::frame_pong: - this->on_pong(); + on_pong(); break; default: break; } } - - void client_impl::on_encode(bool isBinary,shared_ptr const& payload) + + void client_impl_base::on_encode( + bool isBinary,shared_ptr const& payload) { LOG("encoded payload length:"<length()<set_options(asio::ssl::context::default_workarounds | - asio::ssl::context::no_sslv2 | - asio::ssl::context::single_dh_use,ec); - if(ec) - { - cerr<<"Init tls failed,reason:"<< ec.message()<= 'a' && c <= 'z') || (c>= 'A' && c<= 'Z') || (c >= '0' && c<= '9')){ + if((c >= 'a' && c <= 'z') || (c>= 'A' && c<= 'Z') + || (c >= '0' && c<= '9')){ ss << c; } else { - ss << '%' << std::uppercase << std::setw(2) << int((unsigned char) c) << std::nouppercase; + ss << '%' << std::uppercase << std::setw(2) + << int((unsigned char) c) << std::nouppercase; } } ss << std::dec; return ss.str(); } + + void client_impl_base::socket_invoke_disconnected() + { + sockets_invoke_void(&sio::socket::on_disconnect); + } + + void client_impl_base::socket_invoke_closed() + { + sockets_invoke_void(&sio::socket::on_close); + } } diff --git a/src/internal/sio_client_impl.h b/src/internal/sio_client_impl.h index 577b1dd2..1e65457c 100644 --- a/src/internal/sio_client_impl.h +++ b/src/internal/sio_client_impl.h @@ -1,36 +1,24 @@ -#ifndef SIO_CLIENT_IMPL_H -#define SIO_CLIENT_IMPL_H +#pragma once #include #ifdef _WIN32 #define _WEBSOCKETPP_CPP11_THREAD_ //#define _WEBSOCKETPP_CPP11_RANDOM_DEVICE_ #define _WEBSOCKETPP_NO_CPP11_FUNCTIONAL_ -#define INTIALIZER(__TYPE__) #else #define _WEBSOCKETPP_CPP11_STL_ 1 -#define INTIALIZER(__TYPE__) (__TYPE__) #endif #include -#if _DEBUG || DEBUG -#if SIO_TLS -#include -typedef websocketpp::config::debug_asio_tls client_config; -#else -#include -typedef websocketpp::config::debug_asio client_config; -#endif //SIO_TLS -#else -#if SIO_TLS -#include -typedef websocketpp::config::asio_tls_client client_config; + +#if defined(_DEBUG) || defined(DEBUG) +#include #else -#include -typedef websocketpp::config::asio_client client_config; -#endif //SIO_TLS -#endif //DEBUG +#include +#endif -#if SIO_TLS +#include +#ifdef HAVE_OPENSSL +#include #include #endif @@ -41,18 +29,50 @@ typedef websocketpp::config::asio_client client_config; #include #include #include +#include + #include "../sio_client.h" #include "sio_packet.h" +#include "sio_client_config.h" + +// Comment this out to disable handshake logging to stdout +#if defined(_DEBUG) || defined(DEBUG) +#define LOG(x) std::cout << x +#else +#define LOG(x) do {} while (0) +#endif namespace sio { +#if defined(_DEBUG) || defined(DEBUG) +#ifdef HAVE_OPENSSL + using tls_client_config = client_config< + websocketpp::config::debug_core, + websocketpp::transport::asio::tls_socket::endpoint>; +#endif + using non_tls_client_config = client_config< + websocketpp::config::debug_core, + websocketpp::transport::asio::basic_socket::endpoint>; +#else +#ifdef HAVE_OPENSSL + using tls_client_config = client_config< + websocketpp::config::core_client, + websocketpp::transport::asio::tls_socket::endpoint>; +#endif + using non_tls_client_config = client_config< + websocketpp::config::core_client, + websocketpp::transport::asio::basic_socket::endpoint>; +#endif + + using std::chrono::milliseconds; using namespace websocketpp; - - typedef websocketpp::client client_type; - - class client_impl { - + class client_impl_base { + public: + virtual ~client_impl_base() = default; + protected: + client_impl_base(); + enum con_state { con_opening, @@ -60,134 +80,111 @@ namespace sio con_closing, con_closed }; - - client_impl(); - - ~client_impl(); - - //set listeners and event bindings. + + public: + // set listeners #define SYNTHESIS_SETTER(__TYPE__,__FIELD__) \ - void set_##__FIELD__(__TYPE__ const& l) \ + inline void set_##__FIELD__(__TYPE__ const& l) \ { m_##__FIELD__ = l;} - + SYNTHESIS_SETTER(client::con_listener,open_listener) - + SYNTHESIS_SETTER(client::con_listener,fail_listener) SYNTHESIS_SETTER(client::reconnect_listener,reconnect_listener) SYNTHESIS_SETTER(client::con_listener,reconnecting_listener) - + SYNTHESIS_SETTER(client::close_listener,close_listener) - + SYNTHESIS_SETTER(client::socket_listener,socket_open_listener) - + SYNTHESIS_SETTER(client::socket_listener,socket_close_listener) - + #undef SYNTHESIS_SETTER - - - void clear_con_listeners() - { - m_open_listener = nullptr; - m_close_listener = nullptr; - m_fail_listener = nullptr; - m_reconnect_listener = nullptr; - m_reconnecting_listener = nullptr; - } - - void clear_socket_listeners() + + // set reconnection config + inline void set_reconnect_attempts(unsigned attempts) { - m_socket_open_listener = nullptr; - m_socket_close_listener = nullptr; + m_reconn_attempts = attempts; } - - // Client Functions - such as send, etc. - void connect(const std::string& uri, const std::map& queryString, - const std::map& httpExtraHeaders); - + + void set_reconnect_delay(unsigned millis); + + void set_reconnect_delay_max(unsigned millis); + + // cleanups + void clear_con_listeners(); + void clear_socket_listeners(); + + // opens connection + void connect(const std::string& uri, const std::map& queryString, const std::map& httpExtraHeaders); + sio::socket::ptr const& socket(const std::string& nsp); - + + inline bool opened() const { return m_con_state == con_opened; } + + inline std::string const& get_sessionid() const { return m_sid; } + // Closes the connection void close(); - void sync_close(); - - bool opened() const { return m_con_state == con_opened; } - - std::string const& get_sessionid() const { return m_sid; } - void set_reconnect_attempts(unsigned attempts) {m_reconn_attempts = attempts;} + protected: + // pure virtual + virtual asio::io_service& get_io_service() = 0; + virtual void run_loop() = 0; + virtual void connect_impl(const std::string& uri, const std::string& query) = 0; + virtual void close_impl(websocketpp::close::status::value const& code,std::string const& reason) = 0; + virtual void send_impl(std::shared_ptr const& payload_ptr,websocketpp::frame::opcode::value opcode) = 0; - void set_reconnect_delay(unsigned millis) {m_reconn_delay = millis;if(m_reconn_delay_maxmillis) m_reconn_delay = millis;} - - protected: + virtual void reset_states() = 0; + + // client methods void send(packet& p); - + void remove_socket(std::string const& nsp); - - asio::io_service& get_io_service(); - - void on_socket_closed(std::string const& nsp); - - void on_socket_opened(std::string const& nsp); - - private: - void run_loop(); - void connect_impl(const std::string& uri, const std::string& query); + void sockets_invoke_void(void (sio::socket::*fn)(void)); - void close_impl(close::status::value const& code,std::string const& reason); - - void send_impl(std::shared_ptr const& payload_ptr,frame::opcode::value opcode); - - void ping(const asio::error_code& ec); - - void timeout_pong(const asio::error_code& ec); + void socket_invoke_disconnected(); + void socket_invoke_closed(); - void timeout_reconnect(asio::error_code const& ec); - - unsigned next_delay() const; + void sync_close_internal(asio::io_service&); socket::ptr get_socket_locked(std::string const& nsp); - - void sockets_invoke_void(void (sio::socket::*fn)(void)); - - void on_decode(packet const& pack); - void on_encode(bool isBinary,shared_ptr const& payload); - + //websocket callbacks void on_fail(connection_hdl con); void on_open(connection_hdl con); - void on_close(connection_hdl con); + void on_socket_closed(std::string const& nsp); - void on_message(connection_hdl con, client_type::message_ptr msg); + void on_socket_opened(std::string const& nsp); //socketio callbacks void on_handshake(message::ptr const& message); void on_pong(); - void reset_states(); + void on_decode(packet const& pack); + void on_encode(bool isBinary,shared_ptr const& payload); + + // timers + void timeout_pong(const asio::error_code& ec); + void timeout_reconnect(asio::error_code const& ec); void clear_timers(); - - #if SIO_TLS - typedef websocketpp::lib::shared_ptr context_ptr; - - context_ptr on_tls_init(connection_hdl con); - #endif - + unsigned next_delay() const; + // Percent encode query string std::string encode_query_string(const std::string &query); - // Connection pointer for client functions. + protected: connection_hdl m_con; - client_type m_client; // Socket.IO server settings std::string m_sid; std::string m_base_url; @@ -196,30 +193,30 @@ namespace sio unsigned int m_ping_interval; unsigned int m_ping_timeout; - + std::unique_ptr m_network_thread; - + packet_manager m_packet_mgr; - + std::unique_ptr m_ping_timer; - + std::unique_ptr m_ping_timeout_timer; std::unique_ptr m_reconn_timer; - + con_state m_con_state; - + client::con_listener m_open_listener; client::con_listener m_fail_listener; client::con_listener m_reconnecting_listener; client::reconnect_listener m_reconnect_listener; client::close_listener m_close_listener; - + client::socket_listener m_socket_open_listener; client::socket_listener m_socket_close_listener; - + std::map m_sockets; - + std::mutex m_socket_mutex; unsigned m_reconn_delay; @@ -229,10 +226,264 @@ namespace sio unsigned m_reconn_attempts; unsigned m_reconn_made; - - friend class sio::client; - friend class sio::socket; + + friend class socket; }; -} -#endif // SIO_CLIENT_IMPL_H + template + class client_impl : public client_impl_base { + public: + using client_type = websocketpp::client; + + client_impl() + { +#ifndef DEBUG + using websocketpp::log::alevel; + m_client.clear_access_channels(alevel::all); + m_client.set_access_channels(alevel::connect|alevel::disconnect|alevel::app); +#endif + // Initialize the Asio transport policy + m_client.init_asio(); + + // Bind the clients we are using + using std::placeholders::_1; + using std::placeholders::_2; + m_client.set_open_handler(std::bind(&client_impl::on_open,this,_1)); + m_client.set_close_handler(std::bind(&client_impl::on_close,this,_1)); + m_client.set_fail_handler(std::bind(&client_impl::on_fail,this,_1)); + m_client.set_message_handler(std::bind(&client_impl::on_message,this,_1,_2)); +#ifdef HAVE_OPENSSL + init_tls_handler(m_client); +#endif + } + + virtual ~client_impl() override + { + socket_invoke_closed(); + sync_close_internal(m_client.get_io_service()); + } + + protected: + virtual asio::io_service& get_io_service() override + { + return m_client.get_io_service(); + } + + virtual void run_loop() override + { + m_client.run(); + m_client.reset(); + m_client.get_alog().write(websocketpp::log::alevel::devel, + "run loop end"); + } + + virtual void connect_impl(const std::string& uri, const std::string& queryString) override + { + do{ + websocketpp::uri uo(uri); + ostringstream ss; + + if (m_client.is_secure()) { + ss<<"wss://"; + } else { + ss<<"ws://"; + } + const std::string host(uo.get_host()); + // As per RFC2732, literal IPv6 address should be enclosed in "[" and "]". + if(host.find(':')!=std::string::npos){ + ss<<"["<0){ + ss<<"&sid="<replace_header(header.first, header.second); + } + + m_client.connect(con); + return; + } + while(0); + if(m_fail_listener) + { + m_fail_listener(); + } + } + + virtual void close_impl(websocketpp::close::status::value const& code,std::string const& reason) override + { + LOG("Close by reason:"<cancel(); + m_reconn_timer.reset(); + } + if (m_con.expired()) + { + cerr << "Error: No active session" << endl; + } + else + { + websocketpp::lib::error_code ec; + m_client.close(m_con, code, reason, ec); + } + } + + virtual void send_impl(std::shared_ptr const& payload_ptr,websocketpp::frame::opcode::value opcode) override + { + if(m_con_state == con_opened) + { + websocketpp::lib::error_code ec; + m_client.send(m_con,*payload_ptr,opcode,ec); + if(ec) + { + cerr<<"Send failed,reason:"<< ec.message()<get_local_close_code(); + } + + m_con.reset(); + clear_timers(); + client::close_reason reason; + + socket_invoke_disconnected(); + // If we initiated the close, no matter what the close status was, + // we'll consider it a normal close. (When using TLS, we can + // sometimes get a TLS Short Read error when closing.) + if(code == close::status::normal || m_con_state_was == con_closing) + { + reason = client::close_reason_normal; + } + else + { + if(m_reconn_madeexpires_from_now(std::chrono::milliseconds(delay), ec); + m_reconn_timer->async_wait(std::bind(&client_impl::timeout_reconnect,this, std::placeholders::_1)); + return; + } + reason = client::close_reason_drop; + } + + if(m_close_listener) + { + m_close_listener(reason); + } + } + + void on_message(connection_hdl, typename client_type::message_ptr msg) + { + if (m_ping_timeout_timer) { + asio::error_code ec; + m_ping_timeout_timer->expires_from_now(std::chrono::milliseconds(m_ping_timeout),ec); + m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_pong, this, std::placeholders::_1)); + } + // Parse the incoming message according to socket.IO rules + m_packet_mgr.put_payload(msg->get_payload()); + } + + virtual void ping(const asio::error_code& ec) override + { + if(ec || m_con.expired()) + { + if (ec != asio::error::operation_aborted) + LOG("ping exit,con is expired?"< payload) + { + lib::error_code ec; + m_client.send(m_con, *payload, frame::opcode::text, ec); + }); + if(m_ping_timer) + { + asio::error_code e_code; + m_ping_timer->expires_from_now(milliseconds(m_ping_interval), e_code); + m_ping_timer->async_wait(std::bind(&client_impl::ping,this, std::placeholders::_1)); + } + if(!m_ping_timeout_timer) + { + m_ping_timeout_timer.reset(new asio::steady_timer(m_client.get_io_service())); + std::error_code timeout_ec; + m_ping_timeout_timer->expires_from_now(milliseconds(m_ping_timeout), timeout_ec); + m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_pong, this, std::placeholders::_1)); + } + } + + virtual void reset_states() override + { + m_client.reset(); + m_sid.clear(); + m_packet_mgr.reset(); + } + +#ifdef HAVE_OPENSSL + typedef websocketpp::lib::shared_ptr context_ptr; + + context_ptr on_tls_init(connection_hdl) + { + context_ptr ctx = context_ptr(new asio::ssl::context(asio::ssl::context::tlsv1)); + asio::error_code ec; + ctx->set_options(asio::ssl::context::default_workarounds | + asio::ssl::context::no_sslv2 | + asio::ssl::context::single_dh_use,ec); + if(ec) + { + cerr<<"Init tls failed,reason:"<< ec.message()<& client) + { + using std::placeholders::_1; + client.set_tls_init_handler( + std::bind(&client_impl::on_tls_init,this,_1)); + } + + void init_tls_handler(websocketpp::client&) + { + // do nothing + } +#endif + + private: + client_type m_client; + }; +} diff --git a/src/sio_client.cpp b/src/sio_client.cpp index d85f7bfe..92f6354b 100644 --- a/src/sio_client.cpp +++ b/src/sio_client.cpp @@ -13,35 +13,32 @@ using std::stringstream; namespace sio { client::client(): - m_impl(new client_impl()) + m_impl(nullptr) { } - - client::~client() - { - delete m_impl; - } - + + client::~client() = default; + void client::set_open_listener(con_listener const& l) { m_impl->set_open_listener(l); } - + void client::set_fail_listener(con_listener const& l) { m_impl->set_fail_listener(l); } - + void client::set_close_listener(close_listener const& l) { m_impl->set_close_listener(l); } - + void client::set_socket_open_listener(socket_listener const& l) { m_impl->set_socket_open_listener(l); } - + void client::set_reconnect_listener(reconnect_listener const& l) { m_impl->set_reconnect_listener(l); @@ -56,12 +53,12 @@ namespace sio { m_impl->set_socket_close_listener(l); } - + void client::clear_con_listeners() { m_impl->clear_con_listeners(); } - + void client::clear_socket_listeners() { m_impl->clear_socket_listeners(); @@ -69,41 +66,49 @@ namespace sio void client::connect(const std::string& uri) { - m_impl->connect(uri, {}, {}); + connect(uri, {}, {}); } void client::connect(const std::string& uri, const std::map& query) { - m_impl->connect(uri, query, {}); + connect(uri, query, {}); } - void client::connect(const std::string& uri, const std::map& query, - const std::map& http_extra_headers) - { + void client::connect(const std::string& uri, const std::map& query, const std::map& http_extra_headers) + { +#ifdef HAVE_OPENSSL + if ((uri.length() > 8 && memcmp(uri.c_str(), "https://", 8) == 0) + || (uri.length() > 6 && memcmp(uri.c_str(), "wss://", 6) == 0)) { + m_impl.reset(new client_impl()); + } else +#endif + { + m_impl.reset(new client_impl()); + } m_impl->connect(uri, query, http_extra_headers); } - + socket::ptr const& client::socket(const std::string& nsp) { return m_impl->socket(nsp); } - + // Closes the connection void client::close() { m_impl->close(); } - + void client::sync_close() { m_impl->sync_close(); } - + bool client::opened() const { return m_impl->opened(); } - + std::string const& client::get_sessionid() const { return m_impl->get_sessionid(); @@ -123,5 +128,5 @@ namespace sio { m_impl->set_reconnect_delay_max(millis); } - + } diff --git a/src/sio_client.h b/src/sio_client.h index 77be0a2d..cd7816b7 100644 --- a/src/sio_client.h +++ b/src/sio_client.h @@ -13,8 +13,8 @@ namespace sio { - class client_impl; - + class client_impl_base; + class client { public: enum close_reason @@ -22,37 +22,37 @@ namespace sio close_reason_normal, close_reason_drop }; - + typedef std::function con_listener; - + typedef std::function close_listener; typedef std::function reconnect_listener; - + typedef std::function socket_listener; - + client(); ~client(); - + //set listeners and event bindings. void set_open_listener(con_listener const& l); - + void set_fail_listener(con_listener const& l); - + void set_reconnecting_listener(con_listener const& l); void set_reconnect_listener(reconnect_listener const& l); void set_close_listener(close_listener const& l); - + void set_socket_open_listener(socket_listener const& l); - + void set_socket_close_listener(socket_listener const& l); - + void clear_con_listeners(); - + void clear_socket_listeners(); - + // Client Functions - such as send, etc. void connect(const std::string& uri); @@ -66,26 +66,26 @@ namespace sio void set_reconnect_delay(unsigned millis); void set_reconnect_delay_max(unsigned millis); - + sio::socket::ptr const& socket(const std::string& nsp = ""); - + // Closes the connection void close(); - + void sync_close(); - + bool opened() const; - + std::string const& get_sessionid() const; - + private: //disable copy constructor and assign operator. - client(client const&){} - void operator=(client const&){} - - client_impl* m_impl; + client(client const&) = delete; + void operator=(client const&) = delete; + + std::unique_ptr m_impl; }; - + } diff --git a/src/sio_socket.cpp b/src/sio_socket.cpp index 6306ab1c..c6ce9fba 100644 --- a/src/sio_socket.cpp +++ b/src/sio_socket.cpp @@ -26,28 +26,28 @@ namespace sio { func(event.get_name(),event.get_message(),event.need_ack(),event.get_ack_message_impl()); } - + static inline socket::event_listener do_adapt(socket::event_listener_aux const& func) { return std::bind(&event_adapter::adapt_func, func,std::placeholders::_1); } - + static inline event create_event(std::string const& nsp,std::string const& name,message::list&& message,bool need_ack) { return event(nsp,name,message,need_ack); } }; - + const std::string& event::get_nsp() const { return m_nsp; } - + const std::string& event::get_name() const { return m_name; } - + const message::ptr& event::get_message() const { if(m_messages.size()>0) @@ -63,18 +63,18 @@ namespace sio { return m_messages; } - + bool event::need_ack() const { return m_need_ack; } - + void event::put_ack_message(message::list const& ack_message) { if(m_need_ack) m_ack_message = std::move(ack_message); } - + inline event::event(std::string const& nsp,std::string const& name,message::list&& messages,bool need_ack): m_nsp(nsp), @@ -92,116 +92,116 @@ namespace sio m_need_ack(need_ack) { } - + message::list const& event::get_ack_message() const { return m_ack_message; } - + inline message::list& event::get_ack_message_impl() { return m_ack_message; } - + class socket::impl { public: - - impl(client_impl *,std::string const&); + + impl(client_impl_base *,std::string const&); ~impl(); - + void on(std::string const& event_name,event_listener_aux const& func); - + void on(std::string const& event_name,event_listener const& func); - + void off(std::string const& event_name); - + void off_all(); - + #define SYNTHESIS_SETTER(__TYPE__,__FIELD__) \ void set_##__FIELD__(__TYPE__ const& l) \ { m_##__FIELD__ = l;} - + SYNTHESIS_SETTER(error_listener, error_listener) //socket io errors - + #undef SYNTHESIS_SETTER - + void on_error(error_listener const& l); - + void off_error(); - + void close(); - + void emit(std::string const& name, message::list const& msglist, std::function const& ack); - + std::string const& get_namespace() const {return m_nsp;} - + protected: void on_connected(); - + void on_close(); - + void on_open(); - + void on_message_packet(packet const& packet); - + void on_disconnect(); - + private: - + // Message Parsing callbacks. void on_socketio_event(const std::string& nsp, int msgId,const std::string& name, message::list&& message); void on_socketio_ack(int msgId, message::list const& message); void on_socketio_error(message::ptr const& err_message); - + event_listener get_bind_listener_locked(string const& event); - + void ack(int msgId,string const& name,message::list const& ack_message); - + void timeout_connection(const asio::error_code &ec); - + void send_connect(); - + void send_packet(packet& p); - + static event_listener s_null_event_listener; - + static unsigned int s_global_event_id; - - sio::client_impl *m_client; - + + sio::client_impl_base *m_client; + bool m_connected; std::string m_nsp; - + std::map > m_acks; - + std::map m_event_binding; - + error_listener m_error_listener; - + std::unique_ptr m_connection_timer; - + std::queue m_packet_queue; - + std::mutex m_event_mutex; std::mutex m_packet_mutex; - + friend class socket; }; - + void socket::impl::on(std::string const& event_name,event_listener_aux const& func) { this->on(event_name,event_adapter::do_adapt(func)); } - + void socket::impl::on(std::string const& event_name,event_listener const& func) { std::lock_guard guard(m_event_mutex); m_event_binding[event_name] = func; } - + void socket::impl::off(std::string const& event_name) { std::lock_guard guard(m_event_mutex); @@ -211,24 +211,24 @@ namespace sio m_event_binding.erase(it); } } - + void socket::impl::off_all() { std::lock_guard guard(m_event_mutex); m_event_binding.clear(); } - + void socket::impl::on_error(error_listener const& l) { m_error_listener = l; } - + void socket::impl::off_error() { m_error_listener = nullptr; } - - socket::impl::impl(client_impl *client,std::string const& nsp): + + socket::impl::impl(client_impl_base *client,std::string const& nsp): m_client(client), m_connected(false), m_nsp(nsp) @@ -239,14 +239,14 @@ namespace sio send_connect(); } } - + socket::impl::~impl() { - + } - + unsigned int socket::impl::s_global_event_id = 1; - + void socket::impl::emit(std::string const& name, message::list const& msglist, std::function const& ack) { NULL_GUARD(m_client); @@ -265,7 +265,7 @@ namespace sio packet p(m_nsp, msg_ptr,pack_id); send_packet(p); } - + void socket::impl::send_connect() { NULL_GUARD(m_client); @@ -280,7 +280,7 @@ namespace sio m_connection_timer->expires_from_now(std::chrono::milliseconds(20000), ec); m_connection_timer->async_wait(std::bind(&socket::impl::timeout_connection,this, std::placeholders::_1)); } - + void socket::impl::close() { NULL_GUARD(m_client); @@ -288,7 +288,7 @@ namespace sio { packet p(packet::type_disconnect,m_nsp); send_packet(p); - + if(!m_connection_timer) { m_connection_timer.reset(new asio::steady_timer(m_client->get_io_service())); @@ -298,7 +298,7 @@ namespace sio m_connection_timer->async_wait(std::bind(&socket::impl::on_close, this)); } } - + void socket::impl::on_connected() { if(m_connection_timer) @@ -325,11 +325,11 @@ namespace sio } } } - + void socket::impl::on_close() { NULL_GUARD(m_client); - sio::client_impl *client = m_client; + auto client = m_client; m_client = NULL; if(m_connection_timer) @@ -347,12 +347,12 @@ namespace sio client->on_socket_closed(m_nsp); client->remove_socket(m_nsp); } - + void socket::impl::on_open() { send_connect(); } - + void socket::impl::on_disconnect() { NULL_GUARD(m_client); @@ -365,7 +365,7 @@ namespace sio } } } - + void socket::impl::on_message_packet(packet const& p) { NULL_GUARD(m_client); @@ -438,7 +438,7 @@ namespace sio } } } - + void socket::impl::on_socketio_event(const std::string& nsp,int msgId,const std::string& name, message::list && message) { bool needAck = msgId >= 0; @@ -450,13 +450,13 @@ namespace sio this->ack(msgId, name, ev.get_ack_message()); } } - + void socket::impl::ack(int msgId, const string &, const message::list &ack_message) { packet p(m_nsp, ack_message.to_array_message(),msgId,true); send_packet(p); } - + void socket::impl::on_socketio_ack(int msgId, message::list const& message) { std::function l; @@ -471,12 +471,12 @@ namespace sio } if(l)l(message); } - + void socket::impl::on_socketio_error(message::ptr const& err_message) { if(m_error_listener)m_error_listener(err_message); } - + void socket::impl::timeout_connection(const asio::error_code &ec) { NULL_GUARD(m_client); @@ -489,7 +489,7 @@ namespace sio //Should close socket if no connected message arrive.Otherwise we'll never ask for open again. this->on_close(); } - + void socket::impl::send_packet(sio::packet &p) { NULL_GUARD(m_client); @@ -515,7 +515,7 @@ namespace sio m_packet_queue.push(p); } } - + socket::event_listener socket::impl::get_bind_listener_locked(const string &event) { std::lock_guard guard(m_event_mutex); @@ -526,47 +526,47 @@ namespace sio } return socket::event_listener(); } - - socket::socket(client_impl* client,std::string const& nsp): + + socket::socket(client_impl_base* client,std::string const& nsp): m_impl(new impl(client,nsp)) { } - + socket::~socket() { delete m_impl; } - + void socket::on(std::string const& event_name,event_listener const& func) { m_impl->on(event_name, func); } - + void socket::on(std::string const& event_name,event_listener_aux const& func) { m_impl->on(event_name, func); } - + void socket::off(std::string const& event_name) { m_impl->off(event_name); } - + void socket::off_all() { m_impl->off_all(); } - + void socket::close() { m_impl->close(); } - + void socket::on_error(error_listener const& l) { m_impl->on_error(l); } - + void socket::off_error() { m_impl->off_error(); @@ -576,32 +576,32 @@ namespace sio { m_impl->emit(name, msglist,ack); } - + std::string const& socket::get_namespace() const { return m_impl->get_namespace(); } - + void socket::on_connected() { m_impl->on_connected(); } - + void socket::on_close() { m_impl->on_close(); } - + void socket::on_open() { m_impl->on_open(); } - + void socket::on_message_packet(packet const& p) { m_impl->on_message_packet(p); } - + void socket::on_disconnect() { m_impl->on_disconnect(); diff --git a/src/sio_socket.h b/src/sio_socket.h index 69ef1f47..19addff5 100644 --- a/src/sio_socket.h +++ b/src/sio_socket.h @@ -39,7 +39,7 @@ namespace sio friend class event_adapter; }; - class client_impl; + class client_impl_base; class packet; //The name 'socket' is taken from concept of official socket.io. @@ -75,7 +75,7 @@ namespace sio std::string const& get_namespace() const; protected: - socket(client_impl*,std::string const&); + socket(client_impl_base*,std::string const&); void on_connected(); @@ -87,7 +87,7 @@ namespace sio void on_message_packet(packet const& p); - friend class client_impl; + friend class client_impl_base; private: //disable copy constructor and assign operator. From e8f6c5da86bdd3a81f538b5023fe521b4458dba5 Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Fri, 16 Jul 2021 09:50:54 +0300 Subject: [PATCH 22/28] save sio::client configuration in sio::client::connect --- src/internal/sio_client_impl.cpp | 15 +++++++++++++++ src/internal/sio_client_impl.h | 22 ++++++++++++++++++---- src/sio_client.cpp | 15 ++++++++++----- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index d15b7894..95397455 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -35,6 +35,21 @@ namespace sio m_packet_mgr.set_encode_callback(std::bind(&client_impl_base::on_encode,this,_1,_2)); } + client_impl_base::client_impl_base(const client_impl_base &other) + : client_impl_base() + { + m_open_listener = other.m_open_listener; + m_fail_listener = other.m_fail_listener; + m_reconnecting_listener = other.m_reconnecting_listener; + m_reconnect_listener = other.m_reconnect_listener; + m_close_listener = other.m_close_listener; + m_socket_open_listener = other.m_socket_open_listener; + m_socket_close_listener = other.m_socket_close_listener; + m_reconn_delay = other.m_reconn_delay; + m_reconn_delay_max = other.m_reconn_delay_max; + m_reconn_attempts = other.m_reconn_attempts; + } + void client_impl_base::clear_con_listeners() { m_open_listener = nullptr; diff --git a/src/internal/sio_client_impl.h b/src/internal/sio_client_impl.h index 1e65457c..5dcbaae4 100644 --- a/src/internal/sio_client_impl.h +++ b/src/internal/sio_client_impl.h @@ -72,6 +72,7 @@ namespace sio protected: client_impl_base(); + client_impl_base(const client_impl_base& other); enum con_state { @@ -232,10 +233,8 @@ namespace sio template class client_impl : public client_impl_base { - public: - using client_type = websocketpp::client; - - client_impl() + private: + void init_client() { #ifndef DEBUG using websocketpp::log::alevel; @@ -257,6 +256,21 @@ namespace sio #endif } + public: + using client_type = websocketpp::client; + + client_impl(const client_impl_base& other) + : client_impl_base(other) + { + init_client(); + } + + client_impl() + : client_impl_base() + { + init_client(); + } + virtual ~client_impl() override { socket_invoke_closed(); diff --git a/src/sio_client.cpp b/src/sio_client.cpp index 92f6354b..c79417c5 100644 --- a/src/sio_client.cpp +++ b/src/sio_client.cpp @@ -13,7 +13,7 @@ using std::stringstream; namespace sio { client::client(): - m_impl(nullptr) + m_impl(new client_impl()) { } @@ -78,13 +78,18 @@ namespace sio { #ifdef HAVE_OPENSSL if ((uri.length() > 8 && memcmp(uri.c_str(), "https://", 8) == 0) - || (uri.length() > 6 && memcmp(uri.c_str(), "wss://", 6) == 0)) { - m_impl.reset(new client_impl()); + || (uri.length() > 6 && memcmp(uri.c_str(), "wss://", 6) == 0)) + { + if (!dynamic_cast*>(m_impl.get())) + { + m_impl.reset(new client_impl(*m_impl)); + } } else -#endif + if (!dynamic_cast*>(m_impl.get())) { - m_impl.reset(new client_impl()); + m_impl.reset(new client_impl(*m_impl)); } +#endif m_impl->connect(uri, query, http_extra_headers); } From 4bba449dd76d308d8634d794a38fcaf07dfb7130 Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Wed, 30 Mar 2022 10:29:24 +0300 Subject: [PATCH 23/28] support mingw32 clang --- socket.io-client-depend.pri | 6 +++--- socket.io-client.pri | 1 + socket.io-client.pro | 28 +++++++++++++++------------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/socket.io-client-depend.pri b/socket.io-client-depend.pri index d2c15549..dca286b9 100644 --- a/socket.io-client-depend.pri +++ b/socket.io-client-depend.pri @@ -9,8 +9,9 @@ msvc { LIBS += -L$$SOCKET_IO_CLIENT_BIN LIBS += -lsocket.io-client -!isEmpty(OPENSSL_INCLUDE_DIR):!isEmpty(OPENSSL_LIB_DIR) { - +!isEmpty(USE_SYSTEM_OPENSSL) { + LIBS += -lcrypto -lssl +} else:!isEmpty(OPENSSL_INCLUDE_DIR):!isEmpty(OPENSSL_LIB_DIR) { LIBS += -L$$OPENSSL_LIB_DIR msvc { @@ -25,4 +26,3 @@ LIBS += -lsocket.io-client $$OPENSSL_LIB_DIR/libssl.a } } - diff --git a/socket.io-client.pri b/socket.io-client.pri index 5908c1bc..34b8005c 100644 --- a/socket.io-client.pri +++ b/socket.io-client.pri @@ -22,6 +22,7 @@ win32 { OPENSSL_INCLUDE_DIR = $$(OPENSSL_INCLUDE_DIR) OPENSSL_LIB_DIR = $$(OPENSSL_LIB_DIR) +USE_SYSTEM_OPENSSL = $$(USE_SYSTEM_OPENSSL) DEFINES += ASIO_STANDALONE DEFINES += _WEBSOCKETPP_CPP11_TYPE_TRAITS_ diff --git a/socket.io-client.pro b/socket.io-client.pro index 1cefe92a..e5f86062 100644 --- a/socket.io-client.pro +++ b/socket.io-client.pro @@ -7,23 +7,25 @@ CONFIG += staticlib include(socket.io-client.pri) -!isEmpty(OPENSSL_INCLUDE_DIR):!isEmpty(OPENSSL_LIB_DIR) { +!isEmpty(USE_SYSTEM_OPENSSL) { + DEFINES += HAVE_OPENSSL +} else:!isEmpty(OPENSSL_INCLUDE_DIR):!isEmpty(OPENSSL_LIB_DIR) { DEFINES += HAVE_OPENSSL INCLUDEPATH += $$OPENSSL_INCLUDE_DIR } -CONFIG += c++11 warn_off - -unix|win32-g++ { - QMAKE_CXXFLAGS_WARN_OFF -= -w - QMAKE_CXXFLAGS += -Wall -} else { - win32 { - QMAKE_CXXFLAGS_WARN_OFF -= -W0 - QMAKE_CXXFLAGS += -W3 /wd4267 - DEFINES += _CRT_SECURE_NO_WARNINGS - DEFINES += _SCL_SECURE_NO_WARNINGS - } +CONFIG += c++11 + +msvc { + QMAKE_CXXFLAGS_WARN_ON += /wd4267 + DEFINES += _CRT_SECURE_NO_WARNINGS + DEFINES += _SCL_SECURE_NO_WARNINGS +} else:clang|gcc { + QMAKE_CXXFLAGS_WARN_ON += \ + -Wno-null-pointer-subtraction \ + -Wno-unknown-warning-option \ + -Wno-unknown-warning \ + -Wno-unused-command-line-argument } CONFIG(debug, debug|release) { From 72b2d3ee6a084d12ca38e2db99d86588ffe1ad3d Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Mon, 22 May 2023 14:43:21 +0300 Subject: [PATCH 24/28] support TLS v1.2 --- src/internal/sio_client_impl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/sio_client_impl.h b/src/internal/sio_client_impl.h index 5dcbaae4..cf3aca79 100644 --- a/src/internal/sio_client_impl.h +++ b/src/internal/sio_client_impl.h @@ -471,7 +471,7 @@ namespace sio context_ptr on_tls_init(connection_hdl) { - context_ptr ctx = context_ptr(new asio::ssl::context(asio::ssl::context::tlsv1)); + context_ptr ctx = context_ptr(new asio::ssl::context(asio::ssl::context::tlsv12)); asio::error_code ec; ctx->set_options(asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | From b3d6de6a96ab3f75619c5b782a4ff8ac0482b6c3 Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Tue, 23 Jan 2024 18:27:31 +0200 Subject: [PATCH 25/28] mac support arm64 and universal builds --- socket.io-client.pri | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/socket.io-client.pri b/socket.io-client.pri index 34b8005c..c8b87aab 100644 --- a/socket.io-client.pri +++ b/socket.io-client.pri @@ -45,7 +45,18 @@ win32 { clang:SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-clang else::gcc:SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-gcc -SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-$$QT_ARCH +ARCH = $$QT_ARCH +macx { + isEqual(QMAKE_APPLE_DEVICE_ARCHS, "x86_64") { + ARCH = $$QMAKE_APPLE_DEVICE_ARCHS + } else:isEqual(QMAKE_APPLE_DEVICE_ARCHS, "arm64") { + ARCH = $$QMAKE_APPLE_DEVICE_ARCHS + } else:contains(QMAKE_APPLE_DEVICE_ARCHS, "x86_64"):contains(QMAKE_APPLE_DEVICE_ARCHS, "arm64") { + ARCH = "universal" + } +} + +SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN-$$ARCH CONFIG(debug, debug|release) { SOCKET_IO_CLIENT_BIN = $$SOCKET_IO_CLIENT_BIN/debug From 747ba1b005bcae8a68002efe28086daf0fdd452f Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Wed, 31 Jan 2024 14:40:24 +0200 Subject: [PATCH 26/28] fix warnings --- socket.io-client.pro | 1 + src/internal/sio_client_impl.cpp | 2 +- src/sio_message.h | 16 ++++++++-------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/socket.io-client.pro b/socket.io-client.pro index e5f86062..a2bc049d 100644 --- a/socket.io-client.pro +++ b/socket.io-client.pro @@ -23,6 +23,7 @@ msvc { } else:clang|gcc { QMAKE_CXXFLAGS_WARN_ON += \ -Wno-null-pointer-subtraction \ + -Wno-deprecated-declarations \ -Wno-unknown-warning-option \ -Wno-unknown-warning \ -Wno-unused-command-line-argument diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index 95397455..7d58cbbc 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -100,7 +100,7 @@ namespace sio string query_str_value=encode_query_string(it->second); query_str.append(query_str_value); } - m_query_string=move(query_str); + m_query_string = std::move(query_str); m_http_headers = headers; diff --git a/src/sio_message.h b/src/sio_message.h index 6ad0d9b9..2801699a 100755 --- a/src/sio_message.h +++ b/src/sio_message.h @@ -204,7 +204,7 @@ namespace sio } string_message(std::string&& v) - :message(flag_string),_v(move(v)) + :message(flag_string),_v(std::move(v)) { } public: @@ -215,7 +215,7 @@ namespace sio static message::ptr create(std::string&& v) { - return ptr(new string_message(move(v))); + return ptr(new string_message(std::move(v))); } std::string const& get_string() const @@ -269,7 +269,7 @@ namespace sio void push(std::string&& text) { - _v.push_back(string_message::create(move(text))); + _v.push_back(string_message::create(std::move(text))); } void push(std::shared_ptr const& binary) @@ -296,7 +296,7 @@ namespace sio void insert(size_t pos,std::string&& text) { - _v.insert(_v.begin()+pos, string_message::create(move(text))); + _v.insert(_v.begin()+pos, string_message::create(std::move(text))); } void insert(size_t pos,std::shared_ptr const& binary) @@ -361,7 +361,7 @@ namespace sio void insert(const std::string & key,std::string&& text) { - _v[key] = string_message::create(move(text)); + _v[key] = string_message::create(std::move(text)); } void insert(const std::string & key,std::shared_ptr const& binary) @@ -461,7 +461,7 @@ namespace sio list(std::string&& text) { - m_vector.push_back(string_message::create(move(text))); + m_vector.push_back(string_message::create(std::move(text))); } list(std::shared_ptr const& binary) @@ -489,7 +489,7 @@ namespace sio void push(std::string&& text) { - m_vector.push_back(string_message::create(move(text))); + m_vector.push_back(string_message::create(std::move(text))); } void push(std::shared_ptr const& binary) @@ -516,7 +516,7 @@ namespace sio void insert(size_t pos,std::string&& text) { - m_vector.insert(m_vector.begin()+pos, string_message::create(move(text))); + m_vector.insert(m_vector.begin()+pos, string_message::create(std::move(text))); } void insert(size_t pos,std::shared_ptr const& binary) From 103c9b5232c7607848a6289d337ded9b9eb128ce Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Fri, 26 Apr 2024 14:17:59 +0300 Subject: [PATCH 27/28] move headers --- src/internal/sio_client_impl.cpp | 6 +++--- src/internal/sio_client_impl.h | 2 +- src/sio_client.cpp | 4 ++-- src/sio_client.h | 3 +-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index 7d58cbbc..ec0d1b36 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -65,7 +65,7 @@ namespace sio m_socket_close_listener = nullptr; } - void client_impl_base::connect(const string& uri, const map& query, const map& headers) + void client_impl_base::connect(const string& uri, const map& query, map headers) { if(m_reconn_timer) { @@ -93,7 +93,7 @@ namespace sio m_reconn_made = 0; string query_str; - for(map::const_iterator it=query.begin();it!=query.end();++it){ + for(auto it=query.begin();it!=query.end();++it){ query_str.append("&"); query_str.append(it->first); query_str.append("="); @@ -102,7 +102,7 @@ namespace sio } m_query_string = std::move(query_str); - m_http_headers = headers; + m_http_headers = std::move(headers); reset_states(); get_io_service().dispatch(std::bind(&client_impl_base::connect_impl,this,uri,m_query_string)); diff --git a/src/internal/sio_client_impl.h b/src/internal/sio_client_impl.h index cf3aca79..b9e9e8dc 100644 --- a/src/internal/sio_client_impl.h +++ b/src/internal/sio_client_impl.h @@ -119,7 +119,7 @@ namespace sio void clear_socket_listeners(); // opens connection - void connect(const std::string& uri, const std::map& queryString, const std::map& httpExtraHeaders); + void connect(const std::string& uri, const std::map& queryString, std::map httpExtraHeaders); sio::socket::ptr const& socket(const std::string& nsp); diff --git a/src/sio_client.cpp b/src/sio_client.cpp index c79417c5..1f48dc84 100644 --- a/src/sio_client.cpp +++ b/src/sio_client.cpp @@ -74,7 +74,7 @@ namespace sio connect(uri, query, {}); } - void client::connect(const std::string& uri, const std::map& query, const std::map& http_extra_headers) + void client::connect(const std::string& uri, const std::map& query, std::map http_extra_headers) { #ifdef HAVE_OPENSSL if ((uri.length() > 8 && memcmp(uri.c_str(), "https://", 8) == 0) @@ -90,7 +90,7 @@ namespace sio m_impl.reset(new client_impl(*m_impl)); } #endif - m_impl->connect(uri, query, http_extra_headers); + m_impl->connect(uri, query, std::move(http_extra_headers)); } socket::ptr const& client::socket(const std::string& nsp) diff --git a/src/sio_client.h b/src/sio_client.h index cd7816b7..29d675c4 100644 --- a/src/sio_client.h +++ b/src/sio_client.h @@ -58,8 +58,7 @@ namespace sio void connect(const std::string& uri, const std::map& query); - void connect(const std::string& uri, const std::map& query, - const std::map& http_extra_headers); + void connect(const std::string& uri, const std::map& query, std::map http_extra_headers); void set_reconnect_attempts(int attempts); From 668cc86f8ac19f7d0e13e98a888b0e542bc87770 Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Fri, 16 Aug 2024 10:13:12 +0300 Subject: [PATCH 28/28] fix crash on client destruction --- src/internal/sio_client_impl.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/internal/sio_client_impl.h b/src/internal/sio_client_impl.h index b9e9e8dc..1a9a7ba7 100644 --- a/src/internal/sio_client_impl.h +++ b/src/internal/sio_client_impl.h @@ -339,6 +339,7 @@ namespace sio virtual void close_impl(websocketpp::close::status::value const& code,std::string const& reason) override { LOG("Close by reason:"<cancel();