diff --git a/td/telegram/net/MtprotoHeader.h b/td/telegram/net/MtprotoHeader.h index 34d22c4a3..2dae30972 100644 --- a/td/telegram/net/MtprotoHeader.h +++ b/td/telegram/net/MtprotoHeader.h @@ -9,6 +9,7 @@ #include "td/telegram/net/Proxy.h" #include "td/utils/common.h" +#include "td/utils/port/RwMutex.h" #include "td/utils/Slice.h" namespace td { @@ -30,15 +31,18 @@ class MtprotoHeader { }; explicit MtprotoHeader(const Options &options) : options_(options) { - gen_headers(); + default_header_ = gen_header(options_, false); + anonymous_header_ = gen_header(options_, true); } void set_proxy(Proxy proxy) { + auto lock = rw_mutex_.lock_write(); options_.proxy = std::move(proxy); default_header_ = gen_header(options_, false); } bool set_parameters(string parameters) { + auto lock = rw_mutex_.lock_write(); if (options_.parameters == parameters) { return false; } @@ -49,6 +53,7 @@ class MtprotoHeader { } bool set_is_emulator(bool is_emulator) { + auto lock = rw_mutex_.lock_write(); if (options_.is_emulator == is_emulator) { return false; } @@ -59,6 +64,7 @@ class MtprotoHeader { } bool set_language_pack(string language_pack) { + auto lock = rw_mutex_.lock_write(); if (options_.language_pack == language_pack) { return false; } @@ -69,6 +75,7 @@ class MtprotoHeader { } bool set_language_code(string language_code) { + auto lock = rw_mutex_.lock_write(); if (options_.language_code == language_code) { return false; } @@ -79,6 +86,7 @@ class MtprotoHeader { } bool set_tz_offset(int32 tz_offset) { + auto lock = rw_mutex_.lock_write(); if (options_.tz_offset == tz_offset) { return false; } @@ -88,14 +96,18 @@ class MtprotoHeader { return true; } - Slice get_default_header() const { + string get_default_header() const { + auto lock = rw_mutex_.lock_read(); return default_header_; } - Slice get_anonymous_header() const { + + string get_anonymous_header() const { + auto lock = rw_mutex_.lock_read(); return anonymous_header_; } string get_system_language_code() const { + auto lock = rw_mutex_.lock_read(); return options_.system_language_code; } @@ -103,11 +115,7 @@ class MtprotoHeader { Options options_; string default_header_; string anonymous_header_; - - void gen_headers() { - default_header_ = gen_header(options_, false); - anonymous_header_ = gen_header(options_, true); - } + mutable RwMutex rw_mutex_; static string gen_header(const Options &options, bool is_anonymous); }; diff --git a/td/telegram/net/Session.cpp b/td/telegram/net/Session.cpp index 5e4b6b3d2..c6b587182 100644 --- a/td/telegram/net/Session.cpp +++ b/td/telegram/net/Session.cpp @@ -278,9 +278,9 @@ Session::Session(unique_ptr callback, std::shared_ptr long_poll_connection_.connection_id_ = 1; if (is_cdn) { - auth_data_.set_header(G()->mtproto_header().get_anonymous_header().str()); + auth_data_.set_header(G()->mtproto_header().get_anonymous_header()); } else { - auth_data_.set_header(G()->mtproto_header().get_default_header().str()); + auth_data_.set_header(G()->mtproto_header().get_default_header()); } last_activity_timestamp_ = now; last_success_timestamp_ = now - 366 * 86400;