Replace std::function with AnyInvocable

Also refactored the callbacks to make sure that we call either on_success or on_failure callback, and only once.

PiperOrigin-RevId: 557299838
This commit is contained in:
Janusz Sobczak
2023-08-15 17:20:30 -07:00
committed by Copybara-Service
parent 59add37c96
commit ec139d1e8b
4 changed files with 78 additions and 48 deletions
+19 -12
View File
@@ -18,6 +18,7 @@
#include <string>
#include "securegcm/ukey2_handshake.h"
#include "absl/functional/any_invocable.h"
#include "connections/implementation/client_proxy.h"
#include "connections/implementation/endpoint_channel.h"
#include "connections/listeners.h"
@@ -39,14 +40,20 @@ class EncryptionRunner {
~EncryptionRunner();
struct ResultListener {
void CallSuccessCallback(const std::string& endpoint_id,
std::unique_ptr<securegcm::UKey2Handshake> ukey2,
const std::string& auth_token,
const ByteArray& raw_auth_token);
void CallFailureCallback(const std::string& endpoint_id,
EndpointChannel* channel);
void Reset();
// @EncryptionRunnerThread
std::function<void(const std::string& endpoint_id,
std::unique_ptr<securegcm::UKey2Handshake> ukey2,
const std::string& auth_token,
const ByteArray& raw_auth_token)>
on_success_cb = [](const std::string&,
std::unique_ptr<securegcm::UKey2Handshake>,
const std::string&, const ByteArray&) {};
absl::AnyInvocable<void(const std::string& endpoint_id,
std::unique_ptr<securegcm::UKey2Handshake> ukey2,
const std::string& auth_token,
const ByteArray& raw_auth_token) &&>
on_success_cb;
// Encryption has failed. The remote_endpoint_id and channel are given so
// that any pending state can be cleaned up.
@@ -57,19 +64,19 @@ class EncryptionRunner {
// channel to the same endpoint.
//
// @EncryptionRunnerThread
std::function<void(const std::string& endpoint_id,
EndpointChannel* channel)>
on_failure_cb = [](const std::string&, EndpointChannel*) {};
absl::AnyInvocable<void(const std::string& endpoint_id,
EndpointChannel* channel) &&>
on_failure_cb;
};
// @AnyThread
void StartServer(ClientProxy* client, const std::string& endpoint_id,
EndpointChannel* endpoint_channel,
ResultListener&& result_listener);
ResultListener result_listener);
// @AnyThread
void StartClient(ClientProxy* client, const std::string& endpoint_id,
EndpointChannel* endpoint_channel,
ResultListener&& result_listener);
ResultListener result_listener);
private:
ScheduledExecutor alarm_executor_;