Support for synchronous request and setAlarm before initialization.

GitOrigin-RevId: cd7c803d7755437a3240816f221817e08beb33d6
This commit is contained in:
levlam 2018-05-15 23:04:27 +03:00
parent 6c5fb4e6f6
commit 0fd4a3b780
4 changed files with 123 additions and 19 deletions

View File

@ -3143,7 +3143,7 @@ sendCustomRequest method:string parameters:string = CustomRequestResult;
answerCustomQuery custom_query_id:int64 data:string = Ok;
//@description Succeeds after a specified amount of time has passed. Can be called before authorization @seconds Number of seconds before the function returns
//@description Succeeds after a specified amount of time has passed. Can be called before authorization. Can be called before initialization @seconds Number of seconds before the function returns
setAlarm seconds:double = Ok;
@ -3160,7 +3160,7 @@ getTermsOfService = Text;
getDeepLinkInfo link:string = DeepLinkInfo;
//@description Adds a proxy server for network requests. Can be called before authorization. Can be called before initialization @server Proxy server IP address @port Proxy server port @enable True, if the proxy should be enabled @type Type of a proxy
//@description Adds a proxy server for network requests. Can be called before authorization @server Proxy server IP address @port Proxy server port @enable True, if the proxy should be enabled @type Type of a proxy
addProxy server:string port:int32 enable:Bool type:ProxyType = Proxy;
//@description Enables a proxy. Only one proxy can be enabled at a time. Can be called before authorization @proxy_id The proxy identifier

View File

@ -3929,6 +3929,85 @@ bool Td::is_online() const {
return is_online_;
}
bool Td::is_authentication_request(int32 id) {
switch (id) {
case td_api::setTdlibParameters::ID:
case td_api::checkDatabaseEncryptionKey::ID:
case td_api::setDatabaseEncryptionKey::ID:
case td_api::getAuthorizationState::ID:
case td_api::setAuthenticationPhoneNumber::ID:
case td_api::resendAuthenticationCode::ID:
case td_api::checkAuthenticationCode::ID:
case td_api::checkAuthenticationPassword::ID:
case td_api::requestAuthenticationPasswordRecovery::ID:
case td_api::recoverAuthenticationPassword::ID:
case td_api::logOut::ID:
case td_api::close::ID:
case td_api::destroy::ID:
case td_api::checkAuthenticationBotToken::ID:
return true;
default:
return false;
}
}
bool Td::is_synchronous_request(int32 id) {
switch (id) {
case td_api::getTextEntities::ID:
case td_api::parseTextEntities::ID:
case td_api::getFileMimeType::ID:
case td_api::getFileExtension::ID:
case td_api::cleanFileName::ID:
return true;
default:
return false;
}
}
bool Td::is_preinitialization_request(int32 id) {
switch (id) {
case td_api::setAlarm::ID:
case td_api::testUseUpdate::ID:
case td_api::testUseError::ID:
case td_api::testCallEmpty::ID:
case td_api::testSquareInt::ID:
case td_api::testCallString::ID:
case td_api::testCallBytes::ID:
case td_api::testCallVectorInt::ID:
case td_api::testCallVectorIntObject::ID:
case td_api::testCallVectorString::ID:
case td_api::testCallVectorStringObject::ID:
return true;
default:
return false;
}
}
bool Td::is_preauthentication_request(int32 id) {
switch (id) {
case td_api::processDcUpdate::ID:
case td_api::getOption::ID:
case td_api::setOption::ID:
case td_api::setNetworkType::ID:
case td_api::getNetworkStatistics::ID:
case td_api::addNetworkStatistics::ID:
case td_api::resetNetworkStatistics::ID:
case td_api::getCountryCode::ID:
case td_api::getTermsOfService::ID:
case td_api::getDeepLinkInfo::ID:
case td_api::addProxy::ID:
case td_api::enableProxy::ID:
case td_api::disableProxy::ID:
case td_api::removeProxy::ID:
case td_api::getProxies::ID:
case td_api::pingProxy::ID:
case td_api::testNetwork::ID:
return true;
default:
return false;
}
}
void Td::request(uint64 id, tl_object_ptr<td_api::Function> function) {
request_set_.insert(id);
@ -3942,9 +4021,10 @@ void Td::request(uint64 id, tl_object_ptr<td_api::Function> function) {
}
VLOG(td_requests) << "Receive request " << id << ": " << to_string(function);
int32 function_id = function->get_id();
switch (state_) {
case State::WaitParameters: {
switch (function->get_id()) {
switch (function_id) {
case td_api::getAuthorizationState::ID:
return send_closure(actor_id(this), &Td::send_result, id,
td_api::make_object<td_api::authorizationStateWaitTdlibParameters>());
@ -3952,13 +4032,20 @@ void Td::request(uint64 id, tl_object_ptr<td_api::Function> function) {
return answer_ok_query(
id, set_parameters(std::move(move_tl_object_as<td_api::setTdlibParameters>(function)->parameters_)));
default:
if (is_synchronous_request(function_id) || is_preinitialization_request(function_id)) {
break;
}
if (is_preauthentication_request(function_id)) {
// pending_preauthentication_requests_.emplace_back(id, std::move(function));
// return;
}
return send_error_raw(id, 401, "Initialization parameters are needed");
}
break;
}
case State::Decrypt: {
string encryption_key;
switch (function->get_id()) {
switch (function_id) {
case td_api::getAuthorizationState::ID:
return send_closure(
actor_id(this), &Td::send_result, id,
@ -3978,12 +4065,19 @@ void Td::request(uint64 id, tl_object_ptr<td_api::Function> function) {
case td_api::destroy::ID:
return destroy();
default:
if (is_synchronous_request(function_id) || is_preinitialization_request(function_id)) {
break;
}
if (is_preauthentication_request(function_id)) {
// pending_preauthentication_requests_.emplace_back(id, std::move(function));
// return;
}
return send_error_raw(id, 401, "Database encryption key is needed");
}
return answer_ok_query(id, init(as_db_key(encryption_key)));
}
case State::Close: {
if (function->get_id() == td_api::getAuthorizationState::ID) {
if (function_id == td_api::getAuthorizationState::ID) {
if (close_flag_ == 5) {
return send_closure(actor_id(this), &Td::send_result, id,
td_api::make_object<td_api::authorizationStateClosed>());
@ -3992,6 +4086,9 @@ void Td::request(uint64 id, tl_object_ptr<td_api::Function> function) {
td_api::make_object<td_api::authorizationStateClosing>());
}
}
if (is_synchronous_request(function_id)) {
break;
}
return send_error_raw(id, 401, "Unauthorized");
}
case State::Run:
@ -4186,6 +4283,9 @@ void Td::start_up() {
LOG_IF(FATAL, symbol != c) << "TDLib requires little-endian platform";
}
alarm_timeout_.set_callback(on_alarm_timeout_callback);
alarm_timeout_.set_callback_data(static_cast<void *>(this));
CHECK(state_ == State::WaitParameters);
send_update(td_api::make_object<td_api::updateAuthorizationState>(
td_api::make_object<td_api::authorizationStateWaitTdlibParameters>()));
@ -4910,10 +5010,6 @@ Status Td::set_parameters(td_api::object_ptr<td_api::tdlibParameters> parameters
TRY_RESULT(encryption_info, TdDb::check_encryption(parameters_));
encryption_info_ = std::move(encryption_info);
VLOG(td_init) << "Init alarm multitimeout...";
alarm_timeout_.set_callback(on_alarm_timeout_callback);
alarm_timeout_.set_callback_data(static_cast<void *>(this));
VLOG(td_init) << "Create Global";
set_context(std::make_shared<Global>());
inc_request_actor_refcnt(); // guard
@ -7136,27 +7232,22 @@ void Td::on_request(uint64 id, const td_api::pingProxy &request) {
}
void Td::on_request(uint64 id, const td_api::getTextEntities &request) {
// don't check authorization state
send_closure(actor_id(this), &Td::send_result, id, do_static_request(request));
}
void Td::on_request(uint64 id, td_api::parseTextEntities &request) {
// don't check authorization state
send_closure(actor_id(this), &Td::send_result, id, do_static_request(request));
}
void Td::on_request(uint64 id, const td_api::getFileMimeType &request) {
// don't check authorization state
send_closure(actor_id(this), &Td::send_result, id, do_static_request(request));
}
void Td::on_request(uint64 id, const td_api::getFileExtension &request) {
// don't check authorization state
send_closure(actor_id(this), &Td::send_result, id, do_static_request(request));
}
void Td::on_request(uint64 id, const td_api::cleanFileName &request) {
// don't check authorization state
send_closure(actor_id(this), &Td::send_result, id, do_static_request(request));
}

View File

@ -245,6 +245,8 @@ class Td final : public NetQueryCallback {
std::unordered_map<int64, uint64> pending_alarms_;
MultiTimeout alarm_timeout_;
vector<std::pair<uint64, td_api::object_ptr<td_api::Function>>> pending_preauthentication_requests_;
static void on_alarm_timeout_callback(void *td_ptr, int64 alarm_id);
void on_alarm_timeout(int64 alarm_id);
@ -283,6 +285,14 @@ class Td final : public NetQueryCallback {
Promise<Unit> create_ok_request_promise(uint64 id);
static bool is_authentication_request(int32 id);
static bool is_synchronous_request(int32 id);
static bool is_preinitialization_request(int32 id);
static bool is_preauthentication_request(int32 id);
template <class T>
void on_request(uint64 id, const T &request) = delete;

View File

@ -669,6 +669,14 @@ class CliClient final : public Actor {
td_ = create_actor<ClientActor>("ClientActor2", make_td_callback());
ready_to_stop_ = false;
for (int i = 0; i < 4; i++) {
send_closure_later(td_, &ClientActor::request, std::numeric_limits<uint64>::max(),
td_api::make_object<td_api::setAlarm>(0.001 + 1000 * (i / 2)));
}
send_request(td_api::make_object<td_api::getTextEntities>(
"@telegram /test_command https://telegram.org telegram.me @gif @test"));
auto bad_parameters = td_api::make_object<td_api::tdlibParameters>();
bad_parameters->database_directory_ = "/..";
bad_parameters->api_id_ = api_id_;
@ -687,11 +695,6 @@ class CliClient final : public Actor {
parameters->application_version_ = "tg_cli";
send_request(td_api::make_object<td_api::setTdlibParameters>(std::move(parameters)));
send_request(td_api::make_object<td_api::checkDatabaseEncryptionKey>());
for (int i = 0; i < 4; i++) {
send_closure_later(td_, &ClientActor::request, std::numeric_limits<uint64>::max(),
td_api::make_object<td_api::setAlarm>(0.001 + 1000 * (i / 2)));
}
}
void init() {