Move AuthKeyState to a separate header.

This commit is contained in:
levlam 2023-05-04 14:53:07 +03:00
parent 53cd6c1a79
commit 14c570f334
7 changed files with 50 additions and 40 deletions

View File

@ -669,6 +669,7 @@ set(TDLIB_SOURCE
td/telegram/MinChannel.h
td/telegram/misc.h
td/telegram/net/AuthDataShared.h
td/telegram/net/AuthKeyState.h
td/telegram/net/ConnectionCreator.h
td/telegram/net/DcAuthManager.h
td/telegram/net/DcId.h

View File

@ -453,9 +453,6 @@ static ActorOwn<> get_full_config(DcOption option, Promise<tl_object_ptr<telegra
}
return res;
}
AuthKeyState get_auth_key_state() final {
return AuthDataShared::get_auth_key_state(get_auth_key());
}
void set_auth_key(const mtproto::AuthKey &auth_key) final {
G()->td_db()->get_binlog_pmc()->set(auth_key_key(), serialize(auth_key));

View File

@ -43,10 +43,6 @@ class AuthDataSharedImpl final : public AuthDataShared {
return res;
}
AuthKeyState get_auth_key_state() final {
return AuthDataShared::get_auth_key_state(get_auth_key());
}
void set_auth_key(const mtproto::AuthKey &auth_key) final {
G()->td_db()->get_binlog_pmc()->set(auth_key_key(), serialize(auth_key));
log_auth_key(auth_key);
@ -112,8 +108,7 @@ class AuthDataSharedImpl final : public AuthDataShared {
if (!salts.empty()) {
last_used = static_cast<int64>(salts[0].valid_until);
}
LOG(WARNING) << dc_id_ << " " << tag("auth_key_id", auth_key.id())
<< tag("state", AuthDataShared::get_auth_key_state(auth_key))
LOG(WARNING) << dc_id_ << " " << tag("auth_key_id", auth_key.id()) << tag("state", get_auth_key_state(auth_key))
<< tag("created_at", static_cast<int64>(auth_key.created_at())) << tag("last_used", last_used);
}
};

View File

@ -6,6 +6,7 @@
//
#pragma once
#include "td/telegram/net/AuthKeyState.h"
#include "td/telegram/net/DcId.h"
#include "td/telegram/net/PublicRsaKeyShared.h"
@ -14,28 +15,12 @@
#include "td/utils/common.h"
#include "td/utils/ScopeGuard.h"
#include "td/utils/StringBuilder.h"
#include <memory>
#include <utility>
namespace td {
enum class AuthKeyState : int32 { Empty, NoAuth, OK };
inline StringBuilder &operator<<(StringBuilder &sb, AuthKeyState state) {
switch (state) {
case AuthKeyState::Empty:
return sb << "Empty";
case AuthKeyState::NoAuth:
return sb << "NoAuth";
case AuthKeyState::OK:
return sb << "OK";
default:
return sb << "Unknown AuthKeyState";
}
}
class AuthDataShared {
public:
virtual ~AuthDataShared() = default;
@ -51,7 +36,6 @@ class AuthDataShared {
virtual DcId dc_id() const = 0;
virtual const std::shared_ptr<PublicRsaKeyShared> &public_rsa_key() = 0;
virtual mtproto::AuthKey get_auth_key() = 0;
virtual AuthKeyState get_auth_key_state() = 0;
virtual void set_auth_key(const mtproto::AuthKey &auth_key) = 0;
virtual void update_server_time_difference(double diff) = 0;
virtual double get_server_time_difference() = 0;
@ -60,16 +44,6 @@ class AuthDataShared {
virtual void set_future_salts(const std::vector<mtproto::ServerSalt> &future_salts) = 0;
virtual std::vector<mtproto::ServerSalt> get_future_salts() = 0;
static AuthKeyState get_auth_key_state(const mtproto::AuthKey &auth_key) {
if (auth_key.empty()) {
return AuthKeyState::Empty;
} else if (auth_key.auth_flag()) {
return AuthKeyState::OK;
} else {
return AuthKeyState::NoAuth;
}
}
static std::shared_ptr<AuthDataShared> create(DcId dc_id, std::shared_ptr<PublicRsaKeyShared> public_rsa_key,
std::shared_ptr<Guard> guard);
};

View File

@ -0,0 +1,41 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/mtproto/AuthKey.h"
#include "td/utils/common.h"
#include "td/utils/StringBuilder.h"
namespace td {
enum class AuthKeyState : int32 { Empty, NoAuth, OK };
inline AuthKeyState get_auth_key_state(const mtproto::AuthKey &auth_key) {
if (auth_key.empty()) {
return AuthKeyState::Empty;
} else if (auth_key.auth_flag()) {
return AuthKeyState::OK;
} else {
return AuthKeyState::NoAuth;
}
}
inline StringBuilder &operator<<(StringBuilder &sb, AuthKeyState state) {
switch (state) {
case AuthKeyState::Empty:
return sb << "Empty";
case AuthKeyState::NoAuth:
return sb << "NoAuth";
case AuthKeyState::OK:
return sb << "OK";
default:
return sb << "Unknown AuthKeyState";
}
}
} // namespace td

View File

@ -8,6 +8,7 @@
#include "td/telegram/Global.h"
#include "td/telegram/net/AuthDataShared.h"
#include "td/telegram/net/AuthKeyState.h"
#include "td/telegram/net/NetQuery.h"
#include "td/telegram/net/NetQueryDispatcher.h"
#include "td/telegram/TdDb.h"
@ -62,7 +63,7 @@ void DcAuthManager::add_dc(std::shared_ptr<AuthDataShared> auth_data) {
info.dc_id = auth_data->dc_id();
CHECK(info.dc_id.is_exact());
info.shared_auth_data = std::move(auth_data);
info.auth_key_state = info.shared_auth_data->get_auth_key_state();
info.auth_key_state = get_auth_key_state(info.shared_auth_data->get_auth_key());
VLOG(dc) << "Add " << info.dc_id << " with auth key state " << info.auth_key_state;
if (!main_dc_id_.is_exact()) {
main_dc_id_ = info.dc_id;
@ -95,7 +96,7 @@ DcAuthManager::DcInfo *DcAuthManager::find_dc(int32 dc_id) {
void DcAuthManager::update_auth_key_state() {
auto dc_id = narrow_cast<int32>(get_link_token());
auto &dc = get_dc(dc_id);
dc.auth_key_state = dc.shared_auth_data->get_auth_key_state();
dc.auth_key_state = get_auth_key_state(dc.shared_auth_data->get_auth_key());
VLOG(dc) << "Update " << dc_id << " auth key state from " << dc.auth_key_state << " to " << dc.auth_key_state;
loop();

View File

@ -7,6 +7,7 @@
#include "td/telegram/net/SessionProxy.h"
#include "td/telegram/Global.h"
#include "td/telegram/net/AuthKeyState.h"
#include "td/telegram/net/ConnectionCreator.h"
#include "td/telegram/net/DcId.h"
#include "td/telegram/net/NetQueryDispatcher.h"
@ -107,7 +108,7 @@ void SessionProxy::start_up() {
private:
ActorShared<SessionProxy> session_proxy_;
};
auth_key_state_ = auth_data_->get_auth_key_state();
auth_key_state_ = get_auth_key_state(auth_data_->get_auth_key());
auth_data_->add_auth_key_listener(make_unique<Listener>(actor_shared(this)));
open_session();
}
@ -222,7 +223,7 @@ void SessionProxy::open_session(bool force) {
void SessionProxy::update_auth_key_state() {
auto old_auth_key_state = auth_key_state_;
auth_key_state_ = auth_data_->get_auth_key_state();
auth_key_state_ = get_auth_key_state(auth_data_->get_auth_key());
if (auth_key_state_ != old_auth_key_state && old_auth_key_state == AuthKeyState::OK) {
close_session();
}