From c005a8792d8864788d2ea30e01ff944389c992fa Mon Sep 17 00:00:00 2001 From: melode11 Date: Tue, 19 May 2015 14:45:56 +0800 Subject: [PATCH 1/4] add tls branch --- src/internal/sio_client_impl.cpp | 31 +++++++++++++++++++++++++++---- src/internal/sio_client_impl.h | 18 +++++++++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index 92ce0afd..12d4c78a 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -49,7 +49,9 @@ namespace sio 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)); - +#if SIO_TLS + m_client.set_tls_init_handler(lib::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_encode_callback(lib::bind(&client_impl::on_encode,this,_1,_2)); @@ -198,13 +200,17 @@ namespace sio do{ websocketpp::uri uo(uri); ostringstream ss; - +#if SIO_TLS + ss<<"wss://"; +#else + ss<<"ws://"; +#endif if (m_sid.size()==0) { - ss<<"ws://"<set_options(boost::asio::ssl::context::default_workarounds | + boost::asio::ssl::context::no_sslv2 | + boost::asio::ssl::context::single_dh_use,ec); + if(ec) + { + cerr<<"Init tls failed,reason:"<< ec.message()< #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; #else #include typedef websocketpp::config::asio_client client_config; -#endif +#endif //SIO_TLS +#endif //DEBUG #include #include @@ -159,6 +169,12 @@ namespace sio void clear_timers(); + #if SIO_TLS + typedef websocketpp::lib::shared_ptr context_ptr; + + context_ptr on_tls_init(connection_hdl con); + #endif + // Connection pointer for client functions. connection_hdl m_con; client_type m_client; From 4f2e5d7b03837c5b88b0d09baeed6d5dc8c82203 Mon Sep 17 00:00:00 2001 From: melode11 Date: Thu, 21 May 2015 16:43:45 +0800 Subject: [PATCH 2/4] optimize message::list conversion constructor. --- src/sio_message.h | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/sio_message.h b/src/sio_message.h index c9c7c41b..8d829e15 100755 --- a/src/sio_message.h +++ b/src/sio_message.h @@ -151,11 +151,21 @@ namespace sio :message(flag_string),_v(v) { } + + string_message(string&& v) + :message(flag_string),_v(move(v)) + { + } public: static message::ptr create(string const& v) { return ptr(new string_message(v)); } + + static message::ptr create(string&& v) + { + return ptr(new string_message(move(v))); + } string const& get_string() const { @@ -258,15 +268,20 @@ namespace sio m_vector.push_back(message); } - list(string& text) + list(const string& text) { m_vector.push_back(string_message::create(text)); } - list(const char* text) + list(string&& text) { - if(text) - m_vector.push_back(string_message::create(text)); + m_vector.push_back(string_message::create(move(text))); + } + + list(shared_ptr const& binary) + { + if(binary) + m_vector.push_back(binary_message::create(binary)); } list(shared_ptr const& binary) From 98eaa16d798c73427e25a1945f8a046b56cba10d Mon Sep 17 00:00:00 2001 From: melode11 Date: Thu, 21 May 2015 21:00:51 +0800 Subject: [PATCH 3/4] fix Console demo compile in linux --- examples/Console/CMakeLists.txt | 10 ++++++++++ examples/Console/main.cpp | 16 ++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 examples/Console/CMakeLists.txt diff --git a/examples/Console/CMakeLists.txt b/examples/Console/CMakeLists.txt new file mode 100644 index 00000000..907c5d18 --- /dev/null +++ b/examples/Console/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) +include(${CMAKE_CURRENT_SOURCE_DIR}/../../CMakeLists.txt) +add_executable(sio_console_demo main.cpp) +set_property(TARGET sio_console_demo PROPERTY CXX_STANDARD 11) +set_property(TARGET sio_console_demo PROPERTY CXX_STANDARD_REQUIRED ON) +target_link_libraries(sio_console_demo sioclient) +target_link_libraries(sio_console_demo pthread ) +message(STATUS ${Boost_INCLUDE_DIRS} ) +#target_include_directories(sio_console_demo PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../src" ${Boost_INCLUDE_DIRS} ) + diff --git a/examples/Console/main.cpp b/examples/Console/main.cpp index 56d2399b..1dc4e685 100755 --- a/examples/Console/main.cpp +++ b/examples/Console/main.cpp @@ -71,16 +71,16 @@ socket::ptr current_socket; void bind_events(socket::ptr &socket) { - current_socket->on("new message",(sio::socket::event_listener_aux) [&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) + current_socket->on("new message", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) { _lock.lock(); string user = data->get_map()["username"]->get_string(); string message = data->get_map()["message"]->get_string(); EM(user<<":"<on("user joined", (sio::socket::event_listener_aux)[&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) + current_socket->on("user joined",sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) { _lock.lock(); string user = data->get_map()["username"]->get_string(); @@ -90,8 +90,8 @@ void bind_events(socket::ptr &socket) // abc " HIGHLIGHT(user<<" joined"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant")); _lock.unlock(); - }); - current_socket->on("user left",(sio::socket::event_listener_aux) [&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) + })); + current_socket->on("user left", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) { _lock.lock(); string user = data->get_map()["username"]->get_string(); @@ -99,7 +99,7 @@ void bind_events(socket::ptr &socket) bool plural = participants !=1; HIGHLIGHT(user<<" left"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant")); _lock.unlock(); - }); + })); } MAIN_FUNC @@ -126,7 +126,7 @@ MAIN_FUNC getline(cin, nickname); } - current_socket->on("login", (sio::socket::event_listener_aux)[&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp){ + current_socket->on("login", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp){ _lock.lock(); participants = data->get_map()["numUsers"]->get_int(); bool plural = participants !=1; @@ -134,7 +134,7 @@ MAIN_FUNC _cond.notify_all(); _lock.unlock(); current_socket->off("login"); - }); + })); current_socket->emit("add user", nickname); _lock.lock(); if (participants<0) { From 0c1be1bbaa404f9cf56113174ba00a4232b6ac44 Mon Sep 17 00:00:00 2001 From: melode11 Date: Sat, 23 May 2015 00:22:06 +0800 Subject: [PATCH 4/4] simplify the cmake script and add tls target --- CMakeLists.txt | 82 +++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 55 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b0706ee..c9ad9cee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,54 +1,6 @@ cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) PROJECT(sioclient) -function(get_lib_name out_lib_name component) -if(MSVC) -set(${out_lib_name} "${component}.lib" PARENT_SCOPE) -else() -set(${out_lib_name} "lib${component}.a" PARENT_SCOPE) -endif() -endfunction() - -function(install_debug_libs lib_dir) -get_lib_name(renamed_system "boost_system") -install(FILES ${Boost_SYSTEM_LIBRARY_DEBUG} - CONFIGURATIONS "Debug" DESTINATION ${lib_dir} RENAME ${renamed_system} -) -get_lib_name(renamed_date_time "boost_date_time") -install(FILES ${Boost_DATE_TIME_LIBRARY_DEBUG} - CONFIGURATIONS "Debug" DESTINATION ${lib_dir} RENAME ${renamed_date_time} -) -get_lib_name(renamed_random "boost_random") -install(FILES ${Boost_RANDOM_LIBRARY_DEBUG} - CONFIGURATIONS "Debug" DESTINATION ${lib_dir} RENAME ${renamed_random} -) - -install(TARGETS sioclient - CONFIGURATIONS "Debug" DESTINATION ${lib_dir} -) -endfunction() - -function(install_release_libs lib_dir) -get_lib_name(renamed_system "boost_system") -install(FILES ${Boost_SYSTEM_LIBRARY_RELEASE} - CONFIGURATIONS "Release" DESTINATION ${lib_dir} RENAME ${renamed_system} -) -get_lib_name(renamed_date_time "boost_date_time") -install(FILES ${Boost_DATE_TIME_LIBRARY_RELEASE} - CONFIGURATIONS "Release" DESTINATION ${lib_dir} RENAME ${renamed_date_time} -) -get_lib_name(renamed_random "boost_random") -install(FILES ${Boost_RANDOM_LIBRARY_RELEASE} - CONFIGURATIONS "Release" DESTINATION ${lib_dir} RENAME ${renamed_random} -) - -install(TARGETS sioclient - CONFIGURATIONS "Release" DESTINATION ${lib_dir} -) -endfunction() - - -MESSAGE(STATUS ${CMAKE_CURRENT_LIST_DIR}) if(NOT CMAKE_BUILD_TYPE ) MESSAGE(STATUS "not define build type, set to release" ) set(CMAKE_BUILD_TYPE Release ) @@ -67,7 +19,7 @@ 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 ) -MESSAGE(STATUS ${ALL_HEADERS} ) +set(SIO_INCLUDEDIR ${CMAKE_CURRENT_LIST_DIR}) add_library(sioclient STATIC ${ALL_SRC}) target_include_directories(sioclient PRIVATE ${Boost_INCLUDE_DIRS} @@ -79,12 +31,32 @@ target_include_directories(sioclient PRIVATE ${Boost_INCLUDE_DIRS} set_property(TARGET sioclient PROPERTY CXX_STANDARD 11) set_property(TARGET sioclient PROPERTY CXX_STANDARD_REQUIRED ON) target_link_libraries(sioclient PRIVATE ${Boost_LIBRARIES}) + +find_package(OpenSSL) +if(OPENSSL_FOUND) +add_library(sioclient_tls STATIC ${ALL_SRC}) +target_include_directories(sioclient_tls PRIVATE ${Boost_INCLUDE_DIRS} + ${CMAKE_CURRENT_LIST_DIR}/src + ${CMAKE_CURRENT_LIST_DIR}/lib/websocketpp + ${CMAKE_CURRENT_LIST_DIR}/lib/rapidjson/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_compile_definitions(sioclient_tls PRIVATE -DSIO_TLS) + +endif() + install(FILES ${ALL_HEADERS} DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/include" - ) +) -if(CMAKE_BUILD_TYPE STREQUAL "Debug") -install_debug_libs("${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" ) -else() -install_release_libs("${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" ) -endif() +install(TARGETS sioclient + DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" +) + +install(FILES ${Boost_LIBRARIES} + DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" +)