tdlight/td/telegram/net/MtprotoHeader.h

124 lines
3.0 KiB
C
Raw Normal View History

//
2022-12-31 22:28:08 +01:00
// 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/telegram/net/Proxy.h"
#include "td/utils/common.h"
2023-07-26 11:48:59 +02:00
#include "td/utils/port/RwMutex.h"
#include "td/utils/Slice.h"
namespace td {
class MtprotoHeader {
public:
struct Options {
2021-11-11 15:39:09 +01:00
int32 api_id = -1;
string system_language_code;
string device_model;
string system_version;
string application_version;
string language_pack;
string language_code;
string parameters;
2021-11-01 20:32:49 +01:00
int32 tz_offset = 0;
bool is_emulator = false;
Proxy proxy;
};
explicit MtprotoHeader(const Options &options) : options_(options) {
2023-07-26 11:48:59 +02:00
default_header_ = gen_header(options_, false);
anonymous_header_ = gen_header(options_, true);
}
void set_proxy(Proxy proxy) {
2023-07-26 11:48:59 +02:00
auto lock = rw_mutex_.lock_write();
2021-10-19 17:11:16 +02:00
options_.proxy = std::move(proxy);
default_header_ = gen_header(options_, false);
}
bool set_parameters(string parameters) {
2023-07-26 11:48:59 +02:00
auto lock = rw_mutex_.lock_write();
if (options_.parameters == parameters) {
return false;
}
2021-10-19 17:11:16 +02:00
options_.parameters = std::move(parameters);
default_header_ = gen_header(options_, false);
return true;
}
bool set_is_emulator(bool is_emulator) {
2023-07-26 11:48:59 +02:00
auto lock = rw_mutex_.lock_write();
if (options_.is_emulator == is_emulator) {
return false;
}
options_.is_emulator = is_emulator;
default_header_ = gen_header(options_, false);
return true;
}
bool set_language_pack(string language_pack) {
2023-07-26 11:48:59 +02:00
auto lock = rw_mutex_.lock_write();
if (options_.language_pack == language_pack) {
return false;
}
options_.language_pack = std::move(language_pack);
default_header_ = gen_header(options_, false);
return true;
}
bool set_language_code(string language_code) {
2023-07-26 11:48:59 +02:00
auto lock = rw_mutex_.lock_write();
if (options_.language_code == language_code) {
return false;
}
options_.language_code = std::move(language_code);
default_header_ = gen_header(options_, false);
return true;
}
2021-11-01 20:42:33 +01:00
bool set_tz_offset(int32 tz_offset) {
2023-07-26 11:48:59 +02:00
auto lock = rw_mutex_.lock_write();
2021-11-01 20:42:33 +01:00
if (options_.tz_offset == tz_offset) {
return false;
}
options_.tz_offset = tz_offset;
default_header_ = gen_header(options_, false);
return true;
}
2023-07-26 11:48:59 +02:00
string get_default_header() const {
auto lock = rw_mutex_.lock_read();
return default_header_;
}
2023-07-26 11:48:59 +02:00
string get_anonymous_header() const {
auto lock = rw_mutex_.lock_read();
return anonymous_header_;
}
string get_system_language_code() const {
2023-07-26 11:48:59 +02:00
auto lock = rw_mutex_.lock_read();
return options_.system_language_code;
}
private:
Options options_;
string default_header_;
string anonymous_header_;
2023-07-26 11:48:59 +02:00
mutable RwMutex rw_mutex_;
static string gen_header(const Options &options, bool is_anonymous);
};
} // namespace td