2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#include "td/mtproto/HandshakeActor.h"
|
2018-09-27 03:19:03 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/HandshakeConnection.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
2019-07-09 05:56:06 +02:00
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
HandshakeActor::HandshakeActor(unique_ptr<AuthKeyHandshake> handshake, unique_ptr<RawConnection> raw_connection,
|
|
|
|
unique_ptr<AuthKeyHandshakeContext> context, double timeout,
|
|
|
|
Promise<unique_ptr<RawConnection>> raw_connection_promise,
|
|
|
|
Promise<unique_ptr<AuthKeyHandshake>> handshake_promise)
|
2018-12-31 20:04:05 +01:00
|
|
|
: handshake_(std::move(handshake))
|
2018-09-27 03:19:03 +02:00
|
|
|
, connection_(make_unique<HandshakeConnection>(std::move(raw_connection), handshake_.get(), std::move(context)))
|
2018-12-31 20:04:05 +01:00
|
|
|
, timeout_(timeout)
|
|
|
|
, raw_connection_promise_(std::move(raw_connection_promise))
|
|
|
|
, handshake_promise_(std::move(handshake_promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandshakeActor::close() {
|
2021-05-31 20:51:48 +02:00
|
|
|
finish(Status::Error("Canceled"));
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandshakeActor::start_up() {
|
2018-09-11 00:20:41 +02:00
|
|
|
Scheduler::subscribe(connection_->get_poll_info().extract_pollable_fd(this));
|
2018-12-31 20:04:05 +01:00
|
|
|
set_timeout_in(timeout_);
|
2021-07-22 18:23:08 +02:00
|
|
|
handshake_->set_timeout_in(timeout_);
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandshakeActor::loop() {
|
|
|
|
auto status = connection_->flush();
|
|
|
|
if (status.is_error()) {
|
|
|
|
finish(std::move(status));
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
if (handshake_->is_ready_for_finish()) {
|
|
|
|
finish(Status::OK());
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:03:34 +02:00
|
|
|
void HandshakeActor::hangup() {
|
|
|
|
finish(Status::Error(1, "Canceled"));
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandshakeActor::timeout_expired() {
|
|
|
|
finish(Status::Error("Timeout expired"));
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandshakeActor::tear_down() {
|
|
|
|
finish(Status::OK());
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandshakeActor::finish(Status status) {
|
|
|
|
// NB: order may be important for parent
|
|
|
|
return_connection(std::move(status));
|
|
|
|
return_handshake();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void HandshakeActor::return_connection(Status status) {
|
|
|
|
auto raw_connection = connection_->move_as_raw_connection();
|
|
|
|
if (!raw_connection) {
|
|
|
|
CHECK(!raw_connection_promise_);
|
|
|
|
return;
|
|
|
|
}
|
2021-03-26 16:04:01 +01:00
|
|
|
if (status.is_error() && !raw_connection->extra().debug_str.empty()) {
|
2021-06-14 00:24:15 +02:00
|
|
|
status = status.move_as_error_suffix(PSLICE() << " : " << raw_connection->extra().debug_str);
|
2018-03-13 17:09:59 +01:00
|
|
|
}
|
2018-09-11 00:20:41 +02:00
|
|
|
Scheduler::unsubscribe(raw_connection->get_poll_info().get_pollable_fd_ref());
|
2018-12-31 20:04:05 +01:00
|
|
|
if (raw_connection_promise_) {
|
|
|
|
if (status.is_error()) {
|
|
|
|
if (raw_connection->stats_callback()) {
|
|
|
|
raw_connection->stats_callback()->on_error();
|
|
|
|
}
|
|
|
|
raw_connection->close();
|
|
|
|
raw_connection_promise_.set_error(std::move(status));
|
|
|
|
} else {
|
|
|
|
if (raw_connection->stats_callback()) {
|
|
|
|
raw_connection->stats_callback()->on_pong();
|
|
|
|
}
|
|
|
|
raw_connection_promise_.set_value(std::move(raw_connection));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (raw_connection->stats_callback()) {
|
|
|
|
raw_connection->stats_callback()->on_error();
|
|
|
|
}
|
|
|
|
raw_connection->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandshakeActor::return_handshake() {
|
|
|
|
if (!handshake_promise_) {
|
|
|
|
CHECK(!handshake_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handshake_promise_.set_value(std::move(handshake_));
|
|
|
|
}
|
2019-07-09 05:56:06 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|