Skip to content

Commit

Permalink
asio with cpm
Browse files Browse the repository at this point in the history
  • Loading branch information
weidonglian committed Aug 24, 2024
1 parent bf56f3e commit a15eb1d
Show file tree
Hide file tree
Showing 633 changed files with 65 additions and 170,019 deletions.
2 changes: 0 additions & 2 deletions examples/asio_http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
//

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include "asio/ts/internet.hpp"

Expand Down
44 changes: 21 additions & 23 deletions examples/asio_tcp_clients.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
#include <iostream>

#include "asio.hpp"

int main(int argc, char** argv) {

int main(int argc, char ** argv) {
using namespace asio;
io_context ctx;
ip::tcp::resolver resolver(ctx);
ip::tcp::resolver::results_type results = resolver.resolve("www.boost.org", "https");

using namespace asio;
io_context ctx;
ip::tcp::resolver resolver(ctx);
ip::tcp::resolver::query query("www.boost.org", "https");
// End points
for (const auto& endpoint : results) {
std::cout << endpoint.endpoint() << std::endl;
}

// End points
{
ip::tcp::resolver::iterator iter = resolver.resolve(query);
ip::tcp::resolver::iterator end; // End marker.

while (iter != end) {
ip::tcp::endpoint endpoint = *iter++;
std::cout << endpoint << std::endl;
}
ip::tcp::socket sokt(ctx);
async_connect(sokt, results, [](const error_code& er, const ip::tcp::endpoint& endpoint) {
if (!er) {
std::cout << "Connected to: " << endpoint << std::endl;
} else {
std::cout << "async_connect error: " << er.message() << std::endl;
}
});

ip::tcp::socket sokt(ctx);
async_connect(sokt, resolver.resolve(query), [](const error_code& er, const ip::tcp::endpoint& endpoint) {
if (!er) {
// Run the io_context to handle the asynchronous operations
ctx.run();

} else {
std::cout << "async_connect error:" << er.message() << std::endl;
}
});
return 0;
}
return 0;
}
56 changes: 44 additions & 12 deletions third_party/asio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
add_library(asio INTERFACE)
target_include_directories(asio
INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
)

if (WIN32)
target_compile_definitions(asio INTERFACE -DASIO_HAS_STD_THREAD -DASIO_STANDALONE -D_WIN32_WINNT=0x0600)
else(WIN32)
target_compile_definitions(asio INTERFACE -DASIO_HAS_STD_THREAD -DASIO_STANDALONE)
target_compile_options(asio INTERFACE -Wno-shadow -Wno-implicit-fallthrough)
endif()
find_package(Threads REQUIRED)
CPMAddPackage("gh:chriskohlhoff/asio#[email protected]")

# ASIO doesn't use CMake, we have to configure it manually. Extra notes for using on Windows:
#
# 1) If _WIN32_WINNT is not set, ASIO assumes _WIN32_WINNT=0x0501, i.e. Windows XP target, which is
# definitely not the platform which most users target.
#
# 2) WIN32_LEAN_AND_MEAN is defined to make Winsock2 work.
if(asio_ADDED)
add_library(asio INTERFACE)
target_include_directories(asio SYSTEM INTERFACE ${asio_SOURCE_DIR}/asio/include)
target_compile_definitions(asio INTERFACE ASIO_STANDALONE ASIO_NO_DEPRECATED)
target_link_libraries(asio INTERFACE Threads::Threads)

if(WIN32)
# macro see @ https://stackoverflow.com/a/40217291/1746503
macro(get_win32_winnt version)
if(CMAKE_SYSTEM_VERSION)
set(ver ${CMAKE_SYSTEM_VERSION})
string(REGEX MATCH "^([0-9]+).([0-9])" ver ${ver})
string(REGEX MATCH "^([0-9]+)" verMajor ${ver})
# Check for Windows 10, b/c we'll need to convert to hex 'A'.
if("${verMajor}" MATCHES "10")
set(verMajor "A")
string(REGEX REPLACE "^([0-9]+)" ${verMajor} ver ${ver})
endif("${verMajor}" MATCHES "10")
# Remove all remaining '.' characters.
string(REPLACE "." "" ver ${ver})
# Prepend each digit with a zero.
string(REGEX REPLACE "([0-9A-Z])" "0\\1" ver ${ver})
set(${version} "0x${ver}")
endif()
endmacro()

if(NOT DEFINED _WIN32_WINNT)
get_win32_winnt(ver)
set(_WIN32_WINNT ${ver})
endif()

message(STATUS "Set _WIN32_WINNET=${_WIN32_WINNT}")

target_compile_definitions(asio INTERFACE _WIN32_WINNT=${_WIN32_WINNT} WIN32_LEAN_AND_MEAN)
endif()
endif()
205 changes: 0 additions & 205 deletions third_party/asio/asio.hpp

This file was deleted.

Loading

0 comments on commit a15eb1d

Please sign in to comment.