Skip to content

Commit

Permalink
feat: update deprecated api
Browse files Browse the repository at this point in the history
  • Loading branch information
shuai132 committed Nov 15, 2024
1 parent eb88ca7 commit a63c20a
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
submodules: true

- name: Init ASIO
run: git clone https://github.com/chriskohlhoff/asio.git -b asio-1-30-2 --depth=1
run: git clone https://github.com/chriskohlhoff/asio.git -b asio-1-32-0 --depth=1

- name: Configure (${{ matrix.configuration }})
env:
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ endif ()

if (ASIO_NET_BUILD_TEST)
message(STATUS "ASIO_PATH: $ENV{ASIO_PATH}")
add_definitions(-DASIO_NO_DEPRECATED)
include_directories($ENV{ASIO_PATH})
link_libraries(${PROJECT_NAME})
if (NOT MSVC)
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Release](https://img.shields.io/github/release/shuai132/asio_net.svg)](https://github.com/shuai132/asio_net/releases)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

a tiny Async TCP/UDP/RPC/DDS library based on [asio](http://think-async.com/Asio/)
a tiny C++14 Async TCP/UDP/RPC/DDS library based on [asio](http://think-async.com/Asio/)
and [rpc_core](https://github.com/shuai132/rpc_core)

## Features
Expand Down Expand Up @@ -142,6 +142,15 @@ please refer to the source code: [test](test)
client.run();
```

and you can use C++20 coroutine see: [rpc_c_coroutine.cpp](test/rpc_c_coroutine.cpp)

```c++
asio::co_spawn(context, [rpc]() -> asio::awaitable<void> {
auto result = co_await rpc->cmd("cmd")->msg(std::string("hello"))->async_call<std::string>();
assert(result.data == "world");
}, asio::detached);
```
* DDS
```c++
Expand Down
2 changes: 1 addition & 1 deletion include/asio_net/detail/rpc_session_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class rpc_session_t : noncopyable, public std::enable_shared_from_this<rpc_sessi
rpc_session->on_close();
}
// post delay destroy rpc_session, ensure rpc.rsp() callback finish
io_context_.post([rpc_session = std::move(rpc_session)] {});
asio::post(io_context_, [rpc_session = std::move(rpc_session)] {});
// clear tcp_session->on_close, avoid called more than once by close api
tcp_session_.lock()->on_close = nullptr;
};
Expand Down
4 changes: 2 additions & 2 deletions include/asio_net/detail/tcp_client_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class tcp_client_t : public tcp_channel_t<T> {
}

void run() {
asio::io_context::work work(io_context_);
auto work = asio::make_work_guard(io_context_);
io_context_.run();
}

Expand All @@ -92,7 +92,7 @@ class tcp_client_t : public tcp_channel_t<T> {
static_assert(T == socket_type::normal || T == socket_type::ssl, "");
auto resolver = std::make_unique<typename socket_impl<T>::resolver>(io_context_);
auto rp = resolver.get();
rp->async_resolve(typename socket_impl<T>::resolver::query(host, std::to_string(port)),
rp->async_resolve(host, std::to_string(port),
[this, resolver = std::move(resolver), alive = std::weak_ptr<void>(this->is_alive_)](
const std::error_code& ec, const typename socket_impl<T>::resolver::results_type& endpoints) mutable {
if (alive.expired()) return;
Expand Down
4 changes: 2 additions & 2 deletions include/asio_net/serial_port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class serial_port : detail::noncopyable {
}

void run() {
asio::io_context::work work(io_context_);
auto work = asio::make_work_guard(io_context_);
io_context_.run();
}

Expand Down Expand Up @@ -113,7 +113,7 @@ class serial_port : detail::noncopyable {
}

if (!write_msg_queue_.empty()) {
io_context_.post([this, msg = std::move(write_msg_queue_.front())]() mutable {
asio::post(io_context_, [this, msg = std::move(write_msg_queue_.front())]() mutable {
do_write(std::move(msg), true);
});
write_msg_queue_.pop_front();
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "asio_net",
"description": "a tiny Async TCP/UDP/RPC library based on asio",
"description": "a tiny C++14 Async TCP/UDP/RPC/DDS library based on asio",
"keywords": "tcp,udp,rpc,asio",
"authors": {
"name": "shuai132",
Expand Down
2 changes: 1 addition & 1 deletion test/domain_udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main(int argc, char** argv) {
std::thread([&] {
asio::io_context context;
domain_udp_client client(context);
context.post([&] {
asio::post(context, [&] {
domain_udp_client::endpoint endpoint(ENDPOINT);
for (uint32_t i = 0; i < test_count_max; ++i) {
auto data = std::to_string(i);
Expand Down
6 changes: 3 additions & 3 deletions test/rpc_c_check_destroy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main() {
std::shared_ptr<rpc_client> client;

std::thread([&] {
context.post([&] {
asio::post(context, [&] {
client = std::make_shared<rpc_client>(context);
client->on_open = [&](const std::shared_ptr<rpc_core::rpc>& rpc) {
LOG("client on_open:");
Expand All @@ -33,15 +33,15 @@ int main() {
});

std::this_thread::sleep_for(std::chrono::milliseconds(100));
context.post([&] {
asio::post(context, [&] {
client = nullptr;
});

std::this_thread::sleep_for(std::chrono::milliseconds(100));
context.stop();
}).detach();

asio::io_context::work work(context);
auto work = asio::make_work_guard(context);
context.run();
return 0;
}
9 changes: 3 additions & 6 deletions test/server_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ int main() {
auto getFirstIp = [](asio::io_context& context) {
using namespace asio::ip;
tcp::resolver resolver(context);
tcp::resolver::query query(host_name(), "");
tcp::resolver::iterator iter = resolver.resolve(query);
tcp::resolver::iterator end;
if (iter != end) {
tcp::endpoint ep = *iter;
return ep.address().to_string();
auto res = resolver.resolve(host_name(), "");
if (!res.empty()) {
return res.begin()->endpoint().address().to_string();
}
return std::string();
};
Expand Down
4 changes: 2 additions & 2 deletions test/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ int main(int argc, char** argv) {
std::thread([&] {
asio::io_context context;
udp_client client(context);
context.post([&] {
udp_client::endpoint endpoint(asio::ip::address_v4::from_string("127.0.0.1"), PORT);
asio::post(context, [&] {
udp_client::endpoint endpoint(asio::ip::make_address_v4("127.0.0.1"), PORT);
for (uint32_t i = 0; i < test_count_max; ++i) {
auto data = std::to_string(i);
client.send_to(data, endpoint, [&](const std::error_code& ec, std::size_t size) {
Expand Down
2 changes: 1 addition & 1 deletion test/udp_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const uint16_t PORT = 6666;
int main() {
asio::io_context context;
udp_client client(context);
udp_client::endpoint endpoint(asio::ip::address_v4::from_string("127.0.0.1"), PORT);
udp_client::endpoint endpoint(asio::ip::make_address("127.0.0.1"), PORT);
client.send_to("hello", endpoint, [&](const std::error_code& ec, std::size_t size) {
LOG("result: ec: %d, size: %zu", ec.value(), size);
});
Expand Down

0 comments on commit a63c20a

Please sign in to comment.