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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
#include "td/mtproto/MessageId.h"
|
2021-06-02 15:22:30 +02:00
|
|
|
#include "td/mtproto/MtprotoQuery.h"
|
2019-01-31 03:23:40 +01:00
|
|
|
#include "td/mtproto/PacketInfo.h"
|
2019-01-31 12:26:06 +01:00
|
|
|
#include "td/mtproto/RawConnection.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2023-08-05 15:09:36 +02:00
|
|
|
#include "td/utils/common.h"
|
2022-02-09 14:05:53 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2020-10-08 17:30:36 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Named.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2020-07-14 02:37:04 +02:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/utils/StorerBase.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2020-10-05 17:07:23 +02:00
|
|
|
|
|
|
|
extern int VERBOSITY_NAME(mtproto);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace mtproto_api {
|
|
|
|
class new_session_created;
|
|
|
|
class bad_msg_notification;
|
|
|
|
class bad_server_salt;
|
|
|
|
class msgs_ack;
|
|
|
|
class gzip_packed;
|
|
|
|
class pong;
|
|
|
|
class future_salts;
|
|
|
|
class msgs_state_info;
|
|
|
|
class msgs_all_info;
|
|
|
|
class msg_detailed_info;
|
|
|
|
class msg_new_detailed_info;
|
2017-12-29 21:34:39 +01:00
|
|
|
class DestroyAuthKeyRes;
|
|
|
|
class destroy_auth_key_ok;
|
|
|
|
class destroy_auth_key_fail;
|
|
|
|
class destroy_auth_key_none;
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace mtproto_api
|
|
|
|
|
|
|
|
namespace mtproto {
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class AuthData;
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SessionConnection final
|
2018-12-31 20:04:05 +01:00
|
|
|
: public Named
|
|
|
|
, private RawConnection::Callback {
|
|
|
|
public:
|
2021-10-24 12:23:47 +02:00
|
|
|
enum class Mode : int32 { Tcp, Http, HttpLongPoll };
|
2018-12-11 21:34:46 +01:00
|
|
|
SessionConnection(Mode mode, unique_ptr<RawConnection> raw_connection, AuthData *auth_data);
|
2021-10-24 12:23:47 +02:00
|
|
|
SessionConnection(const SessionConnection &) = delete;
|
|
|
|
SessionConnection &operator=(const SessionConnection &) = delete;
|
|
|
|
SessionConnection(SessionConnection &&) = delete;
|
|
|
|
SessionConnection &operator=(SessionConnection &&) = delete;
|
|
|
|
~SessionConnection() = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-14 09:42:40 +02:00
|
|
|
PollableFdInfo &get_poll_info();
|
2019-05-06 18:59:49 +02:00
|
|
|
unique_ptr<RawConnection> move_as_raw_connection();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Interface
|
2023-09-21 16:52:33 +02:00
|
|
|
Result<MessageId> TD_WARN_UNUSED_RESULT send_query(BufferSlice buffer, bool gzip_flag, MessageId message_id = {},
|
|
|
|
vector<MessageId> invoke_after_message_ids = {},
|
|
|
|
bool use_quick_ack = false);
|
|
|
|
std::pair<MessageId, BufferSlice> encrypted_bind(int64 perm_key, int64 nonce, int32 expires_at);
|
|
|
|
|
|
|
|
void get_state_info(MessageId message_id);
|
|
|
|
void resend_answer(MessageId message_id);
|
|
|
|
void cancel_answer(MessageId message_id);
|
2017-12-29 21:34:39 +01:00
|
|
|
void destroy_key();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-11-20 13:07:27 +01:00
|
|
|
void set_online(bool online_flag, bool is_main);
|
2022-05-03 15:17:14 +02:00
|
|
|
void force_ack();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
Callback() = default;
|
|
|
|
Callback(const Callback &) = delete;
|
|
|
|
Callback &operator=(const Callback &) = delete;
|
|
|
|
virtual ~Callback() = default;
|
|
|
|
|
|
|
|
virtual void on_connected() = 0;
|
|
|
|
virtual void on_closed(Status status) = 0;
|
|
|
|
|
|
|
|
virtual void on_server_salt_updated() = 0;
|
2023-05-14 20:58:54 +02:00
|
|
|
virtual void on_server_time_difference_updated(bool force) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
virtual void on_new_session_created(uint64 unique_id, MessageId first_message_id) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void on_session_failed(Status status) = 0;
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
virtual void on_container_sent(MessageId container_message_id, vector<MessageId> message_ids) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual Status on_pong() = 0;
|
|
|
|
|
2021-09-10 15:31:04 +02:00
|
|
|
virtual Status on_update(BufferSlice packet) = 0;
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
virtual void on_message_ack(MessageId message_id) = 0;
|
|
|
|
virtual Status on_message_result_ok(MessageId message_id, BufferSlice packet, size_t original_size) = 0;
|
|
|
|
virtual void on_message_result_error(MessageId message_id, int code, string message) = 0;
|
|
|
|
virtual void on_message_failed(MessageId message_id, Status status) = 0;
|
|
|
|
virtual void on_message_info(MessageId message_id, int32 state, MessageId answer_message_id, int32 answer_size,
|
|
|
|
int32 source) = 0;
|
2017-12-29 21:34:39 +01:00
|
|
|
|
|
|
|
virtual Status on_destroy_auth_key() = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
double flush(SessionConnection::Callback *callback);
|
|
|
|
|
|
|
|
// NB: Do not call force_close after on_closed callback
|
|
|
|
void force_close(SessionConnection::Callback *callback);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr int ACK_DELAY = 30; // 30s
|
|
|
|
static constexpr double QUERY_DELAY = 0.001; // 0.001s
|
|
|
|
static constexpr double RESEND_ANSWER_DELAY = 0.001; // 0.001s
|
|
|
|
|
2023-09-09 23:30:49 +02:00
|
|
|
struct MsgInfo {
|
2023-09-21 16:52:33 +02:00
|
|
|
MessageId message_id;
|
2023-09-09 23:30:49 +02:00
|
|
|
int32 seq_no;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &string_builder, const MsgInfo &info);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool online_flag_ = false;
|
2018-11-20 13:07:27 +01:00
|
|
|
bool is_main_ = false;
|
2021-10-24 12:23:47 +02:00
|
|
|
bool was_moved_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-27 23:49:56 +02:00
|
|
|
double rtt() const {
|
|
|
|
return max(2.0, raw_connection_->extra().rtt * 1.5 + 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-02-12 10:01:11 +01:00
|
|
|
|
2022-08-27 23:49:56 +02:00
|
|
|
double read_disconnect_delay() const {
|
|
|
|
return online_flag_ ? rtt() * 3.5 : 135 + random_delay_;
|
2018-11-20 13:07:27 +01:00
|
|
|
}
|
|
|
|
|
2022-08-27 23:49:56 +02:00
|
|
|
double ping_disconnect_delay() const {
|
|
|
|
return online_flag_ && is_main_ ? rtt() * 2.5 : 135 + random_delay_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-02-12 10:01:11 +01:00
|
|
|
|
2022-08-27 23:49:56 +02:00
|
|
|
double ping_may_delay() const {
|
|
|
|
return online_flag_ ? rtt() * 0.5 : 30 + random_delay_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-02-12 10:01:11 +01:00
|
|
|
|
2022-08-27 23:49:56 +02:00
|
|
|
double ping_must_delay() const {
|
|
|
|
return online_flag_ ? rtt() : 60 + random_delay_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 21:33:22 +02:00
|
|
|
double http_max_wait() const {
|
|
|
|
return 25.0; // 25s. Longer could be closed by proxy
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-27 23:49:56 +02:00
|
|
|
static constexpr int HTTP_MAX_AFTER = 10; // 0.01s
|
|
|
|
static constexpr int HTTP_MAX_DELAY = 30; // 0.03s
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-31 03:37:59 +01:00
|
|
|
vector<MtprotoQuery> to_send_;
|
2023-09-21 16:52:33 +02:00
|
|
|
vector<MessageId> to_ack_message_ids_;
|
2018-12-31 20:04:05 +01:00
|
|
|
double force_send_at_ = 0;
|
|
|
|
|
|
|
|
struct ServiceQuery {
|
2023-09-21 15:52:21 +02:00
|
|
|
enum Type { GetStateInfo, ResendAnswer } type_;
|
2023-09-21 16:52:33 +02:00
|
|
|
MessageId container_message_id_;
|
2023-09-21 15:52:21 +02:00
|
|
|
vector<int64> msg_ids_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2023-09-21 16:52:33 +02:00
|
|
|
vector<MessageId> to_resend_answer_message_ids_;
|
|
|
|
vector<MessageId> to_cancel_answer_message_ids_;
|
|
|
|
vector<MessageId> to_get_state_info_message_ids_;
|
|
|
|
FlatHashMap<MessageId, ServiceQuery, MessageIdHash> service_queries_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// nobody cleans up this map. But it should be really small.
|
2023-09-21 16:52:33 +02:00
|
|
|
FlatHashMap<MessageId, vector<MessageId>, MessageIdHash> container_to_service_message_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-27 23:49:56 +02:00
|
|
|
double random_delay_ = 0;
|
2018-11-20 13:07:27 +01:00
|
|
|
double last_read_at_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
double last_ping_at_ = 0;
|
|
|
|
double last_pong_at_ = 0;
|
2023-03-14 13:45:01 +01:00
|
|
|
double real_last_read_at_ = 0;
|
|
|
|
double real_last_pong_at_ = 0;
|
2023-09-21 16:52:33 +02:00
|
|
|
int64 cur_ping_id_ = 0;
|
|
|
|
MessageId last_ping_message_id_;
|
|
|
|
MessageId last_ping_container_message_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-09-20 16:46:10 +02:00
|
|
|
uint64 last_read_size_ = 0;
|
|
|
|
uint64 last_write_size_ = 0;
|
|
|
|
|
2021-11-11 15:39:09 +01:00
|
|
|
bool need_destroy_auth_key_ = false;
|
|
|
|
bool sent_destroy_auth_key_ = false;
|
2023-10-23 12:02:04 +02:00
|
|
|
double destroy_auth_key_send_time_ = 0.0;
|
2017-12-29 21:34:39 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
double flush_packet_at_ = 0;
|
|
|
|
|
|
|
|
double last_get_future_salt_at_ = 0;
|
|
|
|
enum { Init, Run, Fail, Closed } state_;
|
|
|
|
Mode mode_;
|
|
|
|
bool connected_flag_ = false;
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
MessageId container_message_id_;
|
|
|
|
MessageId main_message_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
double created_at_ = 0;
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<RawConnection> raw_connection_;
|
2023-10-23 12:02:04 +02:00
|
|
|
AuthData *const auth_data_;
|
2018-12-31 20:04:05 +01:00
|
|
|
SessionConnection::Callback *callback_ = nullptr;
|
2021-11-11 15:39:09 +01:00
|
|
|
BufferSlice *current_buffer_slice_ = nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
BufferSlice as_buffer_slice(Slice packet);
|
|
|
|
auto set_buffer_slice(BufferSlice *buffer_slice) TD_WARN_UNUSED_RESULT {
|
2018-12-11 21:34:46 +01:00
|
|
|
auto old_buffer_slice = current_buffer_slice_;
|
|
|
|
current_buffer_slice_ = buffer_slice;
|
2019-09-28 04:14:21 +02:00
|
|
|
return ScopeExit() + [&to = current_buffer_slice_, from = old_buffer_slice] {
|
|
|
|
to = from;
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void reset_server_time_difference(MessageId message_id);
|
2023-05-14 22:11:23 +02:00
|
|
|
|
2021-10-18 18:26:14 +02:00
|
|
|
static Status parse_message(TlParser &parser, MsgInfo *info, Slice *packet,
|
|
|
|
bool crypto_flag = true) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
Status parse_packet(TlParser &parser) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet_container(const MsgInfo &info, Slice packet) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet_rpc_result(const MsgInfo &info, Slice packet) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Status on_packet(const MsgInfo &info, const T &packet) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
Status on_packet(const MsgInfo &info,
|
|
|
|
const mtproto_api::new_session_created &new_session_created) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info,
|
|
|
|
const mtproto_api::bad_msg_notification &bad_msg_notification) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info, const mtproto_api::bad_server_salt &bad_server_salt) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info, const mtproto_api::msgs_ack &msgs_ack) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info, const mtproto_api::gzip_packed &gzip_packed) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info, const mtproto_api::pong &pong) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info, const mtproto_api::future_salts &salts) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2023-09-21 15:52:21 +02:00
|
|
|
Status on_msgs_state_info(const vector<int64> &msg_ids, Slice info) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
Status on_packet(const MsgInfo &info, const mtproto_api::msgs_state_info &msgs_state_info) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info, const mtproto_api::msgs_all_info &msgs_all_info) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info, const mtproto_api::msg_detailed_info &msg_detailed_info) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info,
|
|
|
|
const mtproto_api::msg_new_detailed_info &msg_new_detailed_info) TD_WARN_UNUSED_RESULT;
|
2017-12-29 21:34:39 +01:00
|
|
|
Status on_packet(const MsgInfo &info, const mtproto_api::destroy_auth_key_ok &destroy_auth_key) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info,
|
|
|
|
const mtproto_api::destroy_auth_key_none &destroy_auth_key) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_packet(const MsgInfo &info,
|
|
|
|
const mtproto_api::destroy_auth_key_fail &destroy_auth_key) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status on_destroy_auth_key(const mtproto_api::DestroyAuthKeyRes &destroy_auth_key);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Status on_slice_packet(const MsgInfo &info, Slice packet) TD_WARN_UNUSED_RESULT;
|
2023-09-09 23:04:55 +02:00
|
|
|
Status on_main_packet(const PacketInfo &packet_info, Slice packet) TD_WARN_UNUSED_RESULT;
|
2023-09-21 16:52:33 +02:00
|
|
|
void on_message_failed(MessageId message_id, Status status);
|
|
|
|
void on_message_failed_inner(MessageId message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void do_close(Status status);
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void send_ack(MessageId message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
void send_crypto(const Storer &storer, uint64 quick_ack_token);
|
|
|
|
void send_before(double tm);
|
|
|
|
bool may_ping() const;
|
|
|
|
bool must_ping() const;
|
|
|
|
bool must_flush_packet();
|
|
|
|
void flush_packet();
|
|
|
|
|
|
|
|
Status init() TD_WARN_UNUSED_RESULT;
|
|
|
|
Status do_flush() TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Status before_write() final TD_WARN_UNUSED_RESULT;
|
2023-09-09 23:04:55 +02:00
|
|
|
Status on_raw_packet(const PacketInfo &packet_info, BufferSlice packet) final;
|
2021-07-03 22:51:36 +02:00
|
|
|
Status on_quick_ack(uint64 quick_ack_token) final;
|
|
|
|
void on_read(size_t size) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|