Skip to content

Commit c617682

Browse files
committed
Merge remote-tracking branch 'origin/tls'
2 parents cf5ec9e + 0c1be1b commit c617682

File tree

6 files changed

+108
-72
lines changed

6 files changed

+108
-72
lines changed

CMakeLists.txt

+27-55
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,6 @@
11
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
22
PROJECT(sioclient)
33

4-
function(get_lib_name out_lib_name component)
5-
if(MSVC)
6-
set(${out_lib_name} "${component}.lib" PARENT_SCOPE)
7-
else()
8-
set(${out_lib_name} "lib${component}.a" PARENT_SCOPE)
9-
endif()
10-
endfunction()
11-
12-
function(install_debug_libs lib_dir)
13-
get_lib_name(renamed_system "boost_system")
14-
install(FILES ${Boost_SYSTEM_LIBRARY_DEBUG}
15-
CONFIGURATIONS "Debug" DESTINATION ${lib_dir} RENAME ${renamed_system}
16-
)
17-
get_lib_name(renamed_date_time "boost_date_time")
18-
install(FILES ${Boost_DATE_TIME_LIBRARY_DEBUG}
19-
CONFIGURATIONS "Debug" DESTINATION ${lib_dir} RENAME ${renamed_date_time}
20-
)
21-
get_lib_name(renamed_random "boost_random")
22-
install(FILES ${Boost_RANDOM_LIBRARY_DEBUG}
23-
CONFIGURATIONS "Debug" DESTINATION ${lib_dir} RENAME ${renamed_random}
24-
)
25-
26-
install(TARGETS sioclient
27-
CONFIGURATIONS "Debug" DESTINATION ${lib_dir}
28-
)
29-
endfunction()
30-
31-
function(install_release_libs lib_dir)
32-
get_lib_name(renamed_system "boost_system")
33-
install(FILES ${Boost_SYSTEM_LIBRARY_RELEASE}
34-
CONFIGURATIONS "Release" DESTINATION ${lib_dir} RENAME ${renamed_system}
35-
)
36-
get_lib_name(renamed_date_time "boost_date_time")
37-
install(FILES ${Boost_DATE_TIME_LIBRARY_RELEASE}
38-
CONFIGURATIONS "Release" DESTINATION ${lib_dir} RENAME ${renamed_date_time}
39-
)
40-
get_lib_name(renamed_random "boost_random")
41-
install(FILES ${Boost_RANDOM_LIBRARY_RELEASE}
42-
CONFIGURATIONS "Release" DESTINATION ${lib_dir} RENAME ${renamed_random}
43-
)
44-
45-
install(TARGETS sioclient
46-
CONFIGURATIONS "Release" DESTINATION ${lib_dir}
47-
)
48-
endfunction()
49-
50-
51-
MESSAGE(STATUS ${CMAKE_CURRENT_LIST_DIR})
524
if(NOT CMAKE_BUILD_TYPE )
535
MESSAGE(STATUS "not define build type, set to release" )
546
set(CMAKE_BUILD_TYPE Release )
@@ -67,7 +19,7 @@ find_package(Boost ${BOOST_VER} REQUIRED COMPONENTS system date_time random)
6719
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src ALL_SRC)
6820
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src/internal ALL_SRC)
6921
file(GLOB ALL_HEADERS ${CMAKE_CURRENT_LIST_DIR}/src/*.h )
70-
MESSAGE(STATUS ${ALL_HEADERS} )
22+
set(SIO_INCLUDEDIR ${CMAKE_CURRENT_LIST_DIR})
7123

7224
add_library(sioclient STATIC ${ALL_SRC})
7325
target_include_directories(sioclient PRIVATE ${Boost_INCLUDE_DIRS}
@@ -79,12 +31,32 @@ target_include_directories(sioclient PRIVATE ${Boost_INCLUDE_DIRS}
7931
set_property(TARGET sioclient PROPERTY CXX_STANDARD 11)
8032
set_property(TARGET sioclient PROPERTY CXX_STANDARD_REQUIRED ON)
8133
target_link_libraries(sioclient PRIVATE ${Boost_LIBRARIES})
34+
35+
find_package(OpenSSL)
36+
if(OPENSSL_FOUND)
37+
add_library(sioclient_tls STATIC ${ALL_SRC})
38+
target_include_directories(sioclient_tls PRIVATE ${Boost_INCLUDE_DIRS}
39+
${CMAKE_CURRENT_LIST_DIR}/src
40+
${CMAKE_CURRENT_LIST_DIR}/lib/websocketpp
41+
${CMAKE_CURRENT_LIST_DIR}/lib/rapidjson/include
42+
${OPENSSL_INCLUDE_DIR}
43+
)
44+
45+
set_property(TARGET sioclient_tls PROPERTY CXX_STANDARD 11)
46+
set_property(TARGET sioclient_tls PROPERTY CXX_STANDARD_REQUIRED ON)
47+
target_link_libraries(sioclient_tls PRIVATE ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} )
48+
target_compile_definitions(sioclient_tls PRIVATE -DSIO_TLS)
49+
50+
endif()
51+
8252
install(FILES ${ALL_HEADERS}
8353
DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/include"
84-
)
54+
)
8555

86-
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
87-
install_debug_libs("${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" )
88-
else()
89-
install_release_libs("${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}" )
90-
endif()
56+
install(TARGETS sioclient
57+
DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}"
58+
)
59+
60+
install(FILES ${Boost_LIBRARIES}
61+
DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}"
62+
)

examples/Console/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
2+
include(${CMAKE_CURRENT_SOURCE_DIR}/../../CMakeLists.txt)
3+
add_executable(sio_console_demo main.cpp)
4+
set_property(TARGET sio_console_demo PROPERTY CXX_STANDARD 11)
5+
set_property(TARGET sio_console_demo PROPERTY CXX_STANDARD_REQUIRED ON)
6+
target_link_libraries(sio_console_demo sioclient)
7+
target_link_libraries(sio_console_demo pthread )
8+
message(STATUS ${Boost_INCLUDE_DIRS} )
9+
#target_include_directories(sio_console_demo PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../src" ${Boost_INCLUDE_DIRS} )
10+

examples/Console/main.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ socket::ptr current_socket;
7171

7272
void bind_events(socket::ptr &socket)
7373
{
74-
current_socket->on("new message",(sio::socket::event_listener_aux) [&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
74+
current_socket->on("new message", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
7575
{
7676
_lock.lock();
7777
string user = data->get_map()["username"]->get_string();
7878
string message = data->get_map()["message"]->get_string();
7979
EM(user<<":"<<message);
8080
_lock.unlock();
81-
});
81+
}));
8282

83-
current_socket->on("user joined", (sio::socket::event_listener_aux)[&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
83+
current_socket->on("user joined",sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
8484
{
8585
_lock.lock();
8686
string user = data->get_map()["username"]->get_string();
@@ -90,16 +90,16 @@ void bind_events(socket::ptr &socket)
9090
// abc "
9191
HIGHLIGHT(user<<" joined"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant"));
9292
_lock.unlock();
93-
});
94-
current_socket->on("user left",(sio::socket::event_listener_aux) [&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
93+
}));
94+
current_socket->on("user left", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp)
9595
{
9696
_lock.lock();
9797
string user = data->get_map()["username"]->get_string();
9898
participants = data->get_map()["numUsers"]->get_int();
9999
bool plural = participants !=1;
100100
HIGHLIGHT(user<<" left"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant"));
101101
_lock.unlock();
102-
});
102+
}));
103103
}
104104

105105
MAIN_FUNC
@@ -126,15 +126,15 @@ MAIN_FUNC
126126

127127
getline(cin, nickname);
128128
}
129-
current_socket->on("login", (sio::socket::event_listener_aux)[&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp){
129+
current_socket->on("login", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp){
130130
_lock.lock();
131131
participants = data->get_map()["numUsers"]->get_int();
132132
bool plural = participants !=1;
133133
HIGHLIGHT("Welcome to Socket.IO Chat-\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant"));
134134
_cond.notify_all();
135135
_lock.unlock();
136136
current_socket->off("login");
137-
});
137+
}));
138138
current_socket->emit("add user", nickname);
139139
_lock.lock();
140140
if (participants<0) {

src/internal/sio_client_impl.cpp

+27-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ namespace sio
4949
m_client.set_close_handler(lib::bind(&client_impl::on_close,this,_1));
5050
m_client.set_fail_handler(lib::bind(&client_impl::on_fail,this,_1));
5151
m_client.set_message_handler(lib::bind(&client_impl::on_message,this,_1,_2));
52-
52+
#if SIO_TLS
53+
m_client.set_tls_init_handler(lib::bind(&client_impl::on_tls_init,this,_1));
54+
#endif
5355
m_packet_mgr.set_decode_callback(lib::bind(&client_impl::on_decode,this,_1));
5456

5557
m_packet_mgr.set_encode_callback(lib::bind(&client_impl::on_encode,this,_1,_2));
@@ -198,13 +200,17 @@ namespace sio
198200
do{
199201
websocketpp::uri uo(uri);
200202
ostringstream ss;
201-
203+
#if SIO_TLS
204+
ss<<"wss://";
205+
#else
206+
ss<<"ws://";
207+
#endif
202208
if (m_sid.size()==0) {
203-
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL)<<queryString;
209+
ss<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL)<<queryString;
204210
}
205211
else
206212
{
207-
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&sid="<<m_sid<<"&t="<<time(NULL)<<queryString;
213+
ss<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&sid="<<m_sid<<"&t="<<time(NULL)<<queryString;
208214
}
209215
lib::error_code ec;
210216
client_type::connection_ptr con = m_client.get_connection(ss.str(), ec);
@@ -549,4 +555,21 @@ namespace sio
549555
m_sid.clear();
550556
m_packet_mgr.reset();
551557
}
558+
559+
#if SIO_TLS
560+
client_impl::context_ptr client_impl::on_tls_init(connection_hdl conn)
561+
{
562+
context_ptr ctx = context_ptr(new boost::asio::ssl::context(boost::asio::ssl::context::tlsv1));
563+
boost::system::error_code ec;
564+
ctx->set_options(boost::asio::ssl::context::default_workarounds |
565+
boost::asio::ssl::context::no_sslv2 |
566+
boost::asio::ssl::context::single_dh_use,ec);
567+
if(ec)
568+
{
569+
cerr<<"Init tls failed,reason:"<< ec.message()<<endl;
570+
}
571+
572+
return ctx;
573+
}
574+
#endif
552575
}

src/internal/sio_client_impl.h

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,22 @@
1414
#endif
1515
#include <websocketpp/client.hpp>
1616
#if _DEBUG || DEBUG
17+
#if SIO_TLS
18+
#include <websocketpp/config/debug_asio.hpp>
19+
typedef websocketpp::config::debug_asio_tls client_config;
20+
#else
1721
#include <websocketpp/config/debug_asio_no_tls.hpp>
1822
typedef websocketpp::config::debug_asio client_config;
23+
#endif //SIO_TLS
24+
#else
25+
#if SIO_TLS
26+
#include <websocketpp/config/asio_client.hpp>
27+
typedef websocketpp::config::asio_tls_client client_config;
1928
#else
2029
#include <websocketpp/config/asio_no_tls_client.hpp>
2130
typedef websocketpp::config::asio_client client_config;
22-
#endif
31+
#endif //SIO_TLS
32+
#endif //DEBUG
2333
#include <boost/asio/deadline_timer.hpp>
2434

2535
#include <memory>
@@ -159,6 +169,12 @@ namespace sio
159169

160170
void clear_timers();
161171

172+
#if SIO_TLS
173+
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
174+
175+
context_ptr on_tls_init(connection_hdl con);
176+
#endif
177+
162178
// Connection pointer for client functions.
163179
connection_hdl m_con;
164180
client_type m_client;

src/sio_message.h

+19-4
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,21 @@ namespace sio
151151
:message(flag_string),_v(v)
152152
{
153153
}
154+
155+
string_message(string&& v)
156+
:message(flag_string),_v(move(v))
157+
{
158+
}
154159
public:
155160
static message::ptr create(string const& v)
156161
{
157162
return ptr(new string_message(v));
158163
}
164+
165+
static message::ptr create(string&& v)
166+
{
167+
return ptr(new string_message(move(v)));
168+
}
159169

160170
string const& get_string() const
161171
{
@@ -258,15 +268,20 @@ namespace sio
258268
m_vector.push_back(message);
259269
}
260270

261-
list(string& text)
271+
list(const string& text)
262272
{
263273
m_vector.push_back(string_message::create(text));
264274
}
265275

266-
list(const char* text)
276+
list(string&& text)
267277
{
268-
if(text)
269-
m_vector.push_back(string_message::create(text));
278+
m_vector.push_back(string_message::create(move(text)));
279+
}
280+
281+
list(shared_ptr<string> const& binary)
282+
{
283+
if(binary)
284+
m_vector.push_back(binary_message::create(binary));
270285
}
271286

272287
list(shared_ptr<const string> const& binary)

0 commit comments

Comments
 (0)