Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/tls'
Browse files Browse the repository at this point in the history
  • Loading branch information
melode11 committed May 22, 2015
2 parents cf5ec9e + 0c1be1b commit c617682
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 72 deletions.
82 changes: 27 additions & 55 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 )
Expand All @@ -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}
Expand All @@ -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}"
)
10 changes: 10 additions & 0 deletions examples/Console/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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} )

16 changes: 8 additions & 8 deletions examples/Console/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<<":"<<message);
_lock.unlock();
});
}));

current_socket->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();
Expand All @@ -90,16 +90,16 @@ 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();
participants = data->get_map()["numUsers"]->get_int();
bool plural = participants !=1;
HIGHLIGHT(user<<" left"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant"));
_lock.unlock();
});
}));
}

MAIN_FUNC
Expand All @@ -126,15 +126,15 @@ 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;
HIGHLIGHT("Welcome to Socket.IO Chat-\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant"));
_cond.notify_all();
_lock.unlock();
current_socket->off("login");
});
}));
current_socket->emit("add user", nickname);
_lock.lock();
if (participants<0) {
Expand Down
31 changes: 27 additions & 4 deletions src/internal/sio_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL)<<queryString;
ss<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL)<<queryString;
}
else
{
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&sid="<<m_sid<<"&t="<<time(NULL)<<queryString;
ss<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&sid="<<m_sid<<"&t="<<time(NULL)<<queryString;
}
lib::error_code ec;
client_type::connection_ptr con = m_client.get_connection(ss.str(), ec);
Expand Down Expand Up @@ -549,4 +555,21 @@ namespace sio
m_sid.clear();
m_packet_mgr.reset();
}

#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);
if(ec)
{
cerr<<"Init tls failed,reason:"<< ec.message()<<endl;
}

return ctx;
}
#endif
}
18 changes: 17 additions & 1 deletion src/internal/sio_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@
#endif
#include <websocketpp/client.hpp>
#if _DEBUG || DEBUG
#if SIO_TLS
#include <websocketpp/config/debug_asio.hpp>
typedef websocketpp::config::debug_asio_tls client_config;
#else
#include <websocketpp/config/debug_asio_no_tls.hpp>
typedef websocketpp::config::debug_asio client_config;
#endif //SIO_TLS
#else
#if SIO_TLS
#include <websocketpp/config/asio_client.hpp>
typedef websocketpp::config::asio_tls_client client_config;
#else
#include <websocketpp/config/asio_no_tls_client.hpp>
typedef websocketpp::config::asio_client client_config;
#endif
#endif //SIO_TLS
#endif //DEBUG
#include <boost/asio/deadline_timer.hpp>

#include <memory>
Expand Down Expand Up @@ -159,6 +169,12 @@ namespace sio

void clear_timers();

#if SIO_TLS
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;

context_ptr on_tls_init(connection_hdl con);
#endif

// Connection pointer for client functions.
connection_hdl m_con;
client_type m_client;
Expand Down
23 changes: 19 additions & 4 deletions src/sio_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<string> const& binary)
{
if(binary)
m_vector.push_back(binary_message::create(binary));
}

list(shared_ptr<const string> const& binary)
Expand Down

0 comments on commit c617682

Please sign in to comment.