Skip to content

Commit

Permalink
support ssl
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jun 8, 2020
1 parent 2594175 commit 311d5ec
Show file tree
Hide file tree
Showing 10 changed files with 420 additions and 63 deletions.
14 changes: 13 additions & 1 deletion examples/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ project(basic_client)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11")

SET(ENABLE_SSL ON)

if (ENABLE_SSL)
add_definitions(-DCINATRA_ENABLE_SSL)
message(STATUS "Use SSL")
endif()

find_package(Boost COMPONENTS system REQUIRED)
include_directories(
"../../include"
"../../third/msgpack/include"
)

add_executable(basic_client main.cpp)
target_link_libraries(basic_client ${Boost_LIBRARIES})

if (ENABLE_SSL)
target_link_libraries(basic_client ${Boost_LIBRARIES} -lssl -lcrypto -lpthread)
else()
target_link_libraries(basic_client ${Boost_LIBRARIES})
endif()
2 changes: 2 additions & 0 deletions examples/client/basic_client.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -123,6 +124,7 @@
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down
52 changes: 52 additions & 0 deletions examples/client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,58 @@ void test_threads() {
std::cin >> str;
}

void test_ssl() {
bool is_ssl = true;
rpc_client client;
client.set_error_callback([](auto ec) {
std::cout << ec.message() << "\n";
});

#ifdef CINATRA_ENABLE_SSL
client.set_ssl_context_callback([](boost::asio::ssl::context& ctx) {
ctx.set_verify_mode(boost::asio::ssl::context::verify_peer);
ctx.load_verify_file("server.crt");
});
#endif

bool r = client.connect("127.0.0.1", 9000, is_ssl);
if (!r) {
return;
}

for (size_t i = 0; i < 100; i++) {
try {
auto result = client.call<std::string>("echo", "purecpp");
std::cout << result << " sync\n";
}
catch (const std::exception& e) {
std::cout << e.what() << " sync\n";
}

auto future = client.async_call<CallModel::future>("echo", "purecpp");
auto status = future.wait_for(std::chrono::milliseconds(5000));
if (status == std::future_status::timeout) {
std::cout << "timeout future\n";
}
else {
auto result1 = future.get();
std::cout << result1.as<std::string>() << " future\n";
}

client.async_call("echo", [](auto ec, auto data) {
if (ec) {
std::cout << ec.message() <<" "<< data << "\n";
return;
}

auto result = as<std::string>(data);
std::cout << result << " async\n";
}, "purecpp");
}

std::getchar();
}

int main() {
test_sub1();
test_connect();
Expand Down
16 changes: 14 additions & 2 deletions examples/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ project(basic_server)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11")

find_package(Boost COMPONENTS system REQUIRED)
SET(ENABLE_SSL OFF)

if (ENABLE_SSL)
add_definitions(-DCINATRA_ENABLE_SSL)
message(STATUS "Use SSL")
endif()

find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(
"../../include"
"../../third/msgpack/include"
)

add_executable(basic_server main.cpp)
target_link_libraries(basic_server ${Boost_LIBRARIES})

if (ENABLE_SSL)
target_link_libraries(basic_server ${Boost_LIBRARIES} -lssl -lcrypto -lpthread)
else()
target_link_libraries(basic_server ${Boost_LIBRARIES})
endif()
2 changes: 2 additions & 0 deletions examples/server/basic_server.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -123,6 +124,7 @@
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down
7 changes: 7 additions & 0 deletions examples/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ int get_int(rpc_conn conn, int val) {
return val;
}

void test_ssl() {
rpc_server server(9000, std::thread::hardware_concurrency(), { "server.crt", "server.key" });
server.register_handler("hello", hello);
server.register_handler("echo", echo);
server.run();
}

int main() {
rpc_server server(9000, std::thread::hardware_concurrency());

Expand Down
Loading

0 comments on commit 311d5ec

Please sign in to comment.