Skip to content

Commit

Permalink
net:icmp-client use AnyInvocable
Browse files Browse the repository at this point in the history
  • Loading branch information
iceboy233 committed Feb 16, 2025
1 parent ee7aa17 commit 0769b7e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions net/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ cc_library(
":timer-list",
"//base:types",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:any_invocable",
"@org_boost_boost//:endian",
"@org_boost_boost//:process",
"@org_boost_boost//:smart_ptr",
Expand Down
12 changes: 6 additions & 6 deletions net/icmp-client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class IcmpClient::Operation : public boost::intrusive_ref_counter<
IcmpClient &client,
const icmp::endpoint &endpoint,
uint16_t sequence_number,
std::function<void(std::error_code, ConstBufferSpan)> callback);
absl::AnyInvocable<void(std::error_code, ConstBufferSpan) &&> callback);
~Operation();

Operation(const Operation &) = delete;
Expand All @@ -58,7 +58,7 @@ class IcmpClient::Operation : public boost::intrusive_ref_counter<
IcmpClient &client_;
icmp::endpoint endpoint_;
uint16_t sequence_number_;
std::function<void(std::error_code, ConstBufferSpan)> callback_;
absl::AnyInvocable<void(std::error_code, ConstBufferSpan) &&> callback_;
std::optional<TimerList::Timer> timer_;
IcmpHeader request_header_;
};
Expand All @@ -76,10 +76,10 @@ IcmpClient::IcmpClient(const any_io_executor &executor, const Options &options)
void IcmpClient::request(
const icmp::endpoint &endpoint,
ConstBufferSpan buffer,
std::function<void(std::error_code, ConstBufferSpan)> callback) {
absl::AnyInvocable<void(std::error_code, ConstBufferSpan) &&> callback) {
uint16_t sequence_number = next_sequence_number_;
if (operations_.find(sequence_number) != operations_.end()) {
callback(make_error_code(std::errc::no_buffer_space), {});
std::move(callback)(make_error_code(std::errc::no_buffer_space), {});
return;
}
++next_sequence_number_;
Expand Down Expand Up @@ -125,7 +125,7 @@ IcmpClient::Operation::Operation(
IcmpClient &client,
const icmp::endpoint &endpoint,
uint16_t sequence_number,
std::function<void(std::error_code, ConstBufferSpan)> callback)
absl::AnyInvocable<void(std::error_code, ConstBufferSpan) &&> callback)
: client_(client),
endpoint_(endpoint),
sequence_number_(sequence_number),
Expand Down Expand Up @@ -158,7 +158,7 @@ void IcmpClient::Operation::start(ConstBufferSpan buffer) {
}

void IcmpClient::Operation::finish(std::error_code ec, ConstBufferSpan buffer) {
callback_(ec, buffer);
std::move(callback_)(ec, buffer);
intrusive_ptr_release(this);
}

Expand Down
4 changes: 2 additions & 2 deletions net/icmp-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
#include <system_error>

#include "absl/container/flat_hash_map.h"
#include "absl/functional/any_invocable.h"
#include "base/types.h"
#include "net/asio.h"
#include "net/timer-list.h"
Expand All @@ -31,7 +31,7 @@ class IcmpClient {
void request(
const icmp::endpoint &endpoint,
ConstBufferSpan buffer,
std::function<void(std::error_code, ConstBufferSpan)> callback);
absl::AnyInvocable<void(std::error_code, ConstBufferSpan) &&> callback);

private:
class Operation;
Expand Down

0 comments on commit 0769b7e

Please sign in to comment.