diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 1e0b7a9c8..dce720462 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -52,10 +52,10 @@ authenticationCodeTypeSms length:int32 = AuthenticationCodeType; //@description An authentication code is delivered via a phone call to the specified phone number @length Length of the code authenticationCodeTypeCall length:int32 = AuthenticationCodeType; -//@description An authentication code is delivered by an immediately canceled call to the specified phone number. The phone number from which the call was made is the code that must be entered automatically @pattern Pattern of the phone number from which the call will be made +//@description An authentication code is delivered by an immediately canceled call to the specified phone number. The phone number, from which the call was made, is the code that must be entered automatically @pattern Pattern of the phone number from which the call will be made authenticationCodeTypeFlashCall pattern:string = AuthenticationCodeType; -//@description An authentication code is delivered by an immediately canceled call to the specified phone number. The phone number from which the call was made is the code that is supposed to be entered manually by the user @phone_number_prefix Prefix of the phone number from which the call will be made @length Number of digits in the code, excluding the prefix +//@description An authentication code is delivered by an immediately canceled call to the specified phone number. The phone number, from which the call was made, is the code that is supposed to be entered manually by the user @phone_number_prefix Prefix of the phone number from which the call will be made @length Number of digits in the code, excluding the prefix authenticationCodeTypeMissedCall phone_number_prefix:string length:int32 = AuthenticationCodeType; @@ -2365,7 +2365,7 @@ call id:int32 user_id:int53 is_outgoing:Bool is_video:Bool state:CallState = Cal //@allow_missed_call Pass true if the authentication code may be sent via a missed call to the specified phone number //@is_current_phone_number Pass true if the authenticated phone number is used on the current device //@allow_sms_retriever_api For official applications only. True, if the application can use Android SMS Retriever API (requires Google Play Services >= 10.2) to automatically receive the authentication code from the SMS. See https://developers.google.com/identity/sms-retriever/ for more details -//@authentication_tokens List of authentication tokens, received in updateOption("authentication_token") in previously logged out sessions +//@authentication_tokens List of up to 20 authentication tokens, recently received in updateOption("authentication_token") in previously logged out sessions phoneNumberAuthenticationSettings allow_flash_call:Bool allow_missed_call:Bool is_current_phone_number:Bool allow_sms_retriever_api:Bool authentication_tokens:vector = PhoneNumberAuthenticationSettings; @@ -3488,7 +3488,7 @@ suggestedActionCheckPassword = SuggestedAction; //@description Suggests the user to check whether authorization phone number is correct and change the phone number if it is inaccessible suggestedActionCheckPhoneNumber = SuggestedAction; -//@description Suggests the user to see a hint about meaning of one and two ticks on sent message +//@description Suggests the user to see a hint about meaning of one and two ticks on sent messages suggestedActionSeeTicksHint = SuggestedAction; //@description Suggests the user to convert specified supergroup to a broadcast group @supergroup_id Supergroup identifier @@ -5132,9 +5132,6 @@ setGroupCallTitle group_call_id:int32 title:string = Ok; //@group_call_id Group call identifier @mute_new_participants New value of the mute_new_participants setting toggleGroupCallMuteNewParticipants group_call_id:int32 mute_new_participants:Bool = Ok; -//@description Revokes invite link for a group call. Requires groupCall.can_be_managed group call flag @group_call_id Group call identifier -revokeGroupCallInviteLink group_call_id:int32 = Ok; - //@description Invites users to an active group call. Sends a service message of type messageInviteToGroupCall for video chats //@group_call_id Group call identifier @user_ids User identifiers. At most 10 users can be invited simultaneously inviteGroupCallParticipants group_call_id:int32 user_ids:vector = Ok; @@ -5144,6 +5141,9 @@ inviteGroupCallParticipants group_call_id:int32 user_ids:vector = Ok; //@can_self_unmute Pass true if the invite link needs to contain an invite hash, passing which to joinGroupCall would allow the invited user to unmute themselves. Requires groupCall.can_be_managed group call flag getGroupCallInviteLink group_call_id:int32 can_self_unmute:Bool = HttpUrl; +//@description Revokes invite link for a group call. Requires groupCall.can_be_managed group call flag @group_call_id Group call identifier +revokeGroupCallInviteLink group_call_id:int32 = Ok; + //@description Starts recording of an active group call. Requires groupCall.can_be_managed group call flag @group_call_id Group call identifier @title Group call recording title; 0-64 characters //@record_video Pass true to record a video file instead of an audio file @use_portrait_orientation Pass true to use portrait orientation for video instead of landscape one startGroupCallRecording group_call_id:int32 title:string record_video:Bool use_portrait_orientation:Bool = Ok; @@ -5439,7 +5439,7 @@ toggleSupergroupIsAllHistoryAvailable supergroup_id:int53 is_all_history_availab //@description Upgrades supergroup to a broadcast group; requires owner privileges in the supergroup @supergroup_id Identifier of the supergroup toggleSupergroupIsBroadcastGroup supergroup_id:int53 = Ok; -//@description Reports some messages from a message sender in a supergroup as spam; requires administrator rights in the supergroup @supergroup_id Supergroup identifier @message_ids Identifiers of messages sent in the supergroup. All messages must be sent by the same sender. This list must be non-empty +//@description Reports messages in a supergroup as spam; requires administrator rights in the supergroup @supergroup_id Supergroup identifier @message_ids Identifiers of messages to report reportSupergroupSpam supergroup_id:int53 message_ids:vector = Ok; //@description Returns information about members or banned users in a supergroup or channel. Can be used only if supergroupFullInfo.can_get_members == true; additionally, administrator privileges may be required for some filters @supergroup_id Identifier of the supergroup or channel @@ -5720,7 +5720,7 @@ checkPhoneNumberConfirmationCode code:string = Ok; setBotUpdatesStatus pending_update_count:int32 error_message:string = Ok; -//@description Uploads a PNG image with a sticker; returns the uploaded file @user_id Sticker file owner; ignored for regular users @sticker Sticker file to upload +//@description Uploads a file with a sticker; returns the uploaded file @user_id Sticker file owner; ignored for regular users @sticker Sticker file to upload uploadStickerFile user_id:int53 sticker:InputSticker = File; //@description Returns a suggested name for a new sticker set with a given title @title Sticker set title; 1-64 characters diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index e4d030481..2cc89fe07 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -690,7 +690,7 @@ void AuthManager::on_log_out_result(NetQueryPtr &result) { on_query_ok(); } } -void AuthManager::on_authorization_lost(const string &source) { +void AuthManager::on_authorization_lost(string source) { LOG(WARNING) << "Lost authorization because of " << source; destroy_auth_keys(); } diff --git a/td/telegram/AuthManager.h b/td/telegram/AuthManager.h index d8d58ab82..1580cffed 100644 --- a/td/telegram/AuthManager.h +++ b/td/telegram/AuthManager.h @@ -49,7 +49,7 @@ class AuthManager final : public NetActor { void on_update_login_token(); - void on_authorization_lost(const string &source); + void on_authorization_lost(string source); void on_closing(bool destroy_flag); // can return nullptr if state isn't initialized yet diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index a9f81bd26..cd1737cb8 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -6666,13 +6666,11 @@ void ContactsManager::report_channel_spam(ChannelId channel_id, const vectoris_megagroup) { return promise.set_error(Status::Error(400, "Spam can be reported only in supergroups")); } - - if (message_ids.empty()) { - return promise.set_error(Status::Error(400, "Message list is empty")); + if (!c->status.is_administrator()) { + return promise.set_error(Status::Error(400, "Spam can be reported only by chat administrators")); } - DialogId sender_dialog_id; - vector server_message_ids; + std::unordered_map, DialogIdHash> server_message_ids; for (auto &message_id : message_ids) { if (message_id.is_valid_scheduled()) { return promise.set_error(Status::Error(400, "Can't report scheduled messages")); @@ -6686,34 +6684,25 @@ void ContactsManager::report_channel_spam(ChannelId channel_id, const vectormessages_manager_->get_message_sender({DialogId(channel_id), message_id}); - if (!current_sender_dialog_id.is_valid()) { - continue; + auto sender_dialog_id = td_->messages_manager_->get_dialog_message_sender({DialogId(channel_id), message_id}); + if (sender_dialog_id.is_valid() && sender_dialog_id != DialogId(get_my_id()) && + td_->messages_manager_->have_input_peer(sender_dialog_id, AccessRights::Know)) { + server_message_ids[sender_dialog_id].push_back(message_id); } - if (sender_dialog_id.is_valid()) { - if (current_sender_dialog_id != sender_dialog_id) { - return promise.set_error(Status::Error(400, "All messages nust be from the same sender")); - } - } else { - sender_dialog_id = current_sender_dialog_id; - } - - server_message_ids.push_back(message_id); } if (server_message_ids.empty()) { return promise.set_value(Unit()); } - CHECK(sender_dialog_id.is_valid()); - if (!td_->messages_manager_->have_input_peer(sender_dialog_id, AccessRights::Know)) { - return promise.set_error(Status::Error(400, "Have no access to the user")); - } - if (sender_dialog_id == DialogId(get_my_id())) { - return promise.set_error(Status::Error(400, "Can't report self")); + MultiPromiseActorSafe mpas{"ReportSupergroupSpamMultiPromiseActor"}; + mpas.add_promise(std::move(promise)); + auto lock_promise = mpas.get_promise(); + + for (auto &it : server_message_ids) { + td_->create_handler(mpas.get_promise())->send(channel_id, it.first, it.second); } - td_->create_handler(std::move(promise)) - ->send(channel_id, sender_dialog_id, server_message_ids); + lock_promise.set_value(Unit()); } void ContactsManager::delete_chat(ChatId chat_id, Promise &&promise) { diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index b333c0f8f..ceb38d0a9 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -11001,9 +11001,8 @@ void MessagesManager::delete_dialog_messages_by_sender(DialogId dialog_id, Dialo } vector message_ids; - find_messages(d->messages.get(), message_ids, [sender_dialog_id](const Message *m) { - return sender_dialog_id == MessagesManager::get_message_sender(m); - }); + find_messages(d->messages.get(), message_ids, + [sender_dialog_id](const Message *m) { return sender_dialog_id == get_message_sender(m); }); vector deleted_message_ids; bool need_update_dialog_pos = false; @@ -17148,8 +17147,8 @@ void MessagesManager::on_get_blocked_dialogs(int32 offset, int32 limit, int32 to promise.set_value(td_api::make_object(total_count, std::move(senders))); } -DialogId MessagesManager::get_message_sender(FullMessageId full_message_id) { - const auto *m = get_message_force(full_message_id, "get_message_sender"); +DialogId MessagesManager::get_dialog_message_sender(FullMessageId full_message_id) { + const auto *m = get_message_force(full_message_id, "get_dialog_message_sender"); if (m == nullptr) { return DialogId(); } diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 944e3c2dc..2f0c47531 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -569,7 +569,7 @@ class MessagesManager final : public Actor { bool can_get_message_statistics(FullMessageId full_message_id); - DialogId get_message_sender(FullMessageId full_message_id); + DialogId get_dialog_message_sender(FullMessageId full_message_id); bool have_message_force(FullMessageId full_message_id, const char *source); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index a3d230a5a..0dc409610 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3269,7 +3269,7 @@ bool Td::is_internal_config_option(Slice name) { return name == "revoke_pm_inbox" || name == "revoke_time_limit" || name == "revoke_pm_time_limit" || name == "rating_e_decay" || name == "recent_stickers_limit"; case 's': - return name == "saved_animations_limit"; + return name == "saved_animations_limit" || name == "session_count"; case 'v': return name == "video_note_size_max"; case 'w': @@ -3283,96 +3283,158 @@ void Td::on_config_option_updated(const string &name) { if (close_flag_) { return; } - if (name == "auth") { - send_closure(auth_manager_actor_, &AuthManager::on_authorization_lost, - G()->shared_config().get_option_string(name)); - return; - } else if (name == "saved_animations_limit") { - return animations_manager_->on_update_saved_animations_limit( - narrow_cast(G()->shared_config().get_option_integer(name))); - } else if (name == "animation_search_emojis") { - return animations_manager_->on_update_animation_search_emojis(G()->shared_config().get_option_string(name)); - } else if (name == "animation_search_provider") { - return animations_manager_->on_update_animation_search_provider(G()->shared_config().get_option_string(name)); - } else if (name == "animated_emoji_zoom") { - // update animated emoji zoom only at launch - return; - } else if (name == "recent_stickers_limit") { - return stickers_manager_->on_update_recent_stickers_limit( - narrow_cast(G()->shared_config().get_option_integer(name))); - } else if (name == "favorite_stickers_limit") { - stickers_manager_->on_update_favorite_stickers_limit( - narrow_cast(G()->shared_config().get_option_integer(name))); - } else if (name == "disable_animated_emoji") { - stickers_manager_->on_update_disable_animated_emojis(); - } else if (name == "my_id") { - G()->set_my_id(G()->shared_config().get_option_integer(name)); - } else if (name == "otherwise_relogin_days") { - auto days = narrow_cast(G()->shared_config().get_option_integer(name)); - if (days > 0) { - vector added_actions{SuggestedAction{SuggestedAction::Type::SetPassword, DialogId(), days}}; - send_closure(G()->td(), &Td::send_update, get_update_suggested_actions_object(added_actions, {})); - } - return; - } else if (name == "session_count") { - G()->net_query_dispatcher().update_session_count(); - } else if (name == "use_pfs") { - G()->net_query_dispatcher().update_use_pfs(); - } else if (name == "use_storage_optimizer") { - send_closure(storage_manager_, &StorageManager::update_use_storage_optimizer); - } else if (name == "rating_e_decay") { - return send_closure(top_dialog_manager_actor_, &TopDialogManager::update_rating_e_decay); - } else if (name == "disable_contact_registered_notifications") { - send_closure(notification_manager_actor_, - &NotificationManager::on_disable_contact_registered_notifications_changed); - } else if (name == "disable_top_chats") { - send_closure(top_dialog_manager_actor_, &TopDialogManager::update_is_enabled, - !G()->shared_config().get_option_boolean(name)); - } else if (name == "connection_parameters") { - if (G()->mtproto_header().set_parameters(G()->shared_config().get_option_string(name))) { - G()->net_query_dispatcher().update_mtproto_header(); - } - } else if (name == "is_emulator") { - if (G()->mtproto_header().set_is_emulator(G()->shared_config().get_option_boolean(name))) { - G()->net_query_dispatcher().update_mtproto_header(); - } - } else if (name == "localization_target") { - send_closure(language_pack_manager_, &LanguagePackManager::on_language_pack_changed); - if (G()->mtproto_header().set_language_pack(G()->shared_config().get_option_string(name))) { - G()->net_query_dispatcher().update_mtproto_header(); - } - } else if (name == "language_pack_id") { - send_closure(language_pack_manager_, &LanguagePackManager::on_language_code_changed); - if (G()->mtproto_header().set_language_code(G()->shared_config().get_option_string(name))) { - G()->net_query_dispatcher().update_mtproto_header(); - } - } else if (name == "language_pack_version") { - return send_closure(language_pack_manager_, &LanguagePackManager::on_language_pack_version_changed, false, -1); - } else if (name == "base_language_pack_version") { - return send_closure(language_pack_manager_, &LanguagePackManager::on_language_pack_version_changed, true, -1); - } else if (name == "utc_time_offset") { - if (G()->mtproto_header().set_tz_offset(static_cast(G()->shared_config().get_option_integer(name)))) { - G()->net_query_dispatcher().update_mtproto_header(); - } - } else if (name == "notification_group_count_max") { - send_closure(notification_manager_actor_, &NotificationManager::on_notification_group_count_max_changed, true); - } else if (name == "notification_group_size_max") { - send_closure(notification_manager_actor_, &NotificationManager::on_notification_group_size_max_changed); - } else if (name == "online_cloud_timeout_ms") { - return send_closure(notification_manager_actor_, &NotificationManager::on_online_cloud_timeout_changed); - } else if (name == "notification_cloud_delay_ms") { - return send_closure(notification_manager_actor_, &NotificationManager::on_notification_cloud_delay_changed); - } else if (name == "notification_default_delay_ms") { - return send_closure(notification_manager_actor_, &NotificationManager::on_notification_default_delay_changed); - } else if (name == "ignored_restriction_reasons") { - return send_closure(contacts_manager_actor_, &ContactsManager::on_ignored_restriction_reasons_changed); - } else if (name == "dice_emojis") { - return send_closure(stickers_manager_actor_, &StickersManager::on_update_dice_emojis); - } else if (name == "dice_success_values") { - return send_closure(stickers_manager_actor_, &StickersManager::on_update_dice_success_values); - } else if (name == "emoji_sounds") { - return send_closure(stickers_manager_actor_, &StickersManager::on_update_emoji_sounds); - } else if (is_internal_config_option(name)) { + switch (name[0]) { + case 'a': + if (name == "animated_emoji_zoom") { + // nothing to do: animated emoji zoom is updated only at launch + } + if (name == "animation_search_emojis") { + animations_manager_->on_update_animation_search_emojis(G()->shared_config().get_option_string(name)); + } + if (name == "animation_search_provider") { + animations_manager_->on_update_animation_search_provider(G()->shared_config().get_option_string(name)); + } + if (name == "auth") { + send_closure(auth_manager_actor_, &AuthManager::on_authorization_lost, + G()->shared_config().get_option_string(name)); + } + break; + case 'b': + if (name == "base_language_pack_version") { + send_closure(language_pack_manager_, &LanguagePackManager::on_language_pack_version_changed, true, -1); + } + break; + case 'c': + if (name == "connection_parameters") { + if (G()->mtproto_header().set_parameters(G()->shared_config().get_option_string(name))) { + G()->net_query_dispatcher().update_mtproto_header(); + } + } + break; + case 'd': + if (name == "dice_emojis") { + send_closure(stickers_manager_actor_, &StickersManager::on_update_dice_emojis); + } + if (name == "dice_success_values") { + send_closure(stickers_manager_actor_, &StickersManager::on_update_dice_success_values); + } + if (name == "disable_animated_emoji") { + stickers_manager_->on_update_disable_animated_emojis(); + } + if (name == "disable_contact_registered_notifications") { + send_closure(notification_manager_actor_, + &NotificationManager::on_disable_contact_registered_notifications_changed); + } + if (name == "disable_top_chats") { + send_closure(top_dialog_manager_actor_, &TopDialogManager::update_is_enabled, + !G()->shared_config().get_option_boolean(name)); + } + break; + case 'e': + if (name == "emoji_sounds") { + send_closure(stickers_manager_actor_, &StickersManager::on_update_emoji_sounds); + } + break; + case 'f': + if (name == "favorite_stickers_limit") { + stickers_manager_->on_update_favorite_stickers_limit( + narrow_cast(G()->shared_config().get_option_integer(name))); + } + break; + case 'i': + if (name == "ignored_restriction_reasons") { + send_closure(contacts_manager_actor_, &ContactsManager::on_ignored_restriction_reasons_changed); + } + if (name == "is_emulator") { + if (G()->mtproto_header().set_is_emulator(G()->shared_config().get_option_boolean(name))) { + G()->net_query_dispatcher().update_mtproto_header(); + } + } + break; + case 'l': + if (name == "language_pack_id") { + send_closure(language_pack_manager_, &LanguagePackManager::on_language_code_changed); + if (G()->mtproto_header().set_language_code(G()->shared_config().get_option_string(name))) { + G()->net_query_dispatcher().update_mtproto_header(); + } + } + if (name == "language_pack_version") { + send_closure(language_pack_manager_, &LanguagePackManager::on_language_pack_version_changed, false, -1); + } + if (name == "localization_target") { + send_closure(language_pack_manager_, &LanguagePackManager::on_language_pack_changed); + if (G()->mtproto_header().set_language_pack(G()->shared_config().get_option_string(name))) { + G()->net_query_dispatcher().update_mtproto_header(); + } + } + break; + case 'm': + if (name == "my_id") { + G()->set_my_id(G()->shared_config().get_option_integer(name)); + } + break; + case 'n': + if (name == "notification_cloud_delay_ms") { + send_closure(notification_manager_actor_, &NotificationManager::on_notification_cloud_delay_changed); + } + if (name == "notification_default_delay_ms") { + send_closure(notification_manager_actor_, &NotificationManager::on_notification_default_delay_changed); + } + if (name == "notification_group_count_max") { + send_closure(notification_manager_actor_, &NotificationManager::on_notification_group_count_max_changed, true); + } + if (name == "notification_group_size_max") { + send_closure(notification_manager_actor_, &NotificationManager::on_notification_group_size_max_changed); + } + break; + case 'o': + if (name == "online_cloud_timeout_ms") { + send_closure(notification_manager_actor_, &NotificationManager::on_online_cloud_timeout_changed); + } + if (name == "otherwise_relogin_days") { + auto days = narrow_cast(G()->shared_config().get_option_integer(name)); + if (days > 0) { + vector added_actions{SuggestedAction{SuggestedAction::Type::SetPassword, DialogId(), days}}; + send_closure(G()->td(), &Td::send_update, get_update_suggested_actions_object(added_actions, {})); + } + } + break; + case 'r': + if (name == "rating_e_decay") { + send_closure(top_dialog_manager_actor_, &TopDialogManager::update_rating_e_decay); + } + if (name == "recent_stickers_limit") { + stickers_manager_->on_update_recent_stickers_limit( + narrow_cast(G()->shared_config().get_option_integer(name))); + } + break; + case 's': + if (name == "saved_animations_limit") { + animations_manager_->on_update_saved_animations_limit( + narrow_cast(G()->shared_config().get_option_integer(name))); + } + if (name == "session_count") { + G()->net_query_dispatcher().update_session_count(); + } + break; + case 'u': + if (name == "use_pfs") { + G()->net_query_dispatcher().update_use_pfs(); + } + if (name == "use_storage_optimizer") { + send_closure(storage_manager_, &StorageManager::update_use_storage_optimizer); + } + if (name == "utc_time_offset") { + if (G()->mtproto_header().set_tz_offset(static_cast(G()->shared_config().get_option_integer(name)))) { + G()->net_query_dispatcher().update_mtproto_header(); + } + } + break; + default: + break; + } + + if (is_internal_config_option(name)) { return; } @@ -7388,7 +7450,7 @@ void Td::on_request(uint64 id, td_api::setOption &request) { LOG(INFO) << "Set option " << request.name_; - auto set_integer_option = [&](Slice name, int64 min = 0, int64 max = std::numeric_limits::max()) { + auto set_integer_option = [&](Slice name, int64 min_value = 0, int64 max_value = std::numeric_limits::max()) { if (request.name_ != name) { return false; } @@ -7401,13 +7463,13 @@ void Td::on_request(uint64 id, td_api::setOption &request) { G()->shared_config().set_option_empty(name); } else { int64 value = static_cast(request.value_.get())->value_; - if (value < min || value > max) { + if (value < min_value || value > max_value) { send_error_raw(id, 400, PSLICE() << "Option's \"" << name << "\" value " << value << " is outside of a valid range [" - << min << ", " << max << "]"); + << min_value << ", " << max_value << "]"); return true; } - G()->shared_config().set_option_integer(name, clamp(value, min, max)); + G()->shared_config().set_option_integer(name, clamp(value, min_value, max_value)); } send_closure(actor_id(this), &Td::send_result, id, make_tl_object()); return true; @@ -7652,9 +7714,6 @@ void Td::on_request(uint64 id, td_api::setOption &request) { } break; case 's': - if (set_integer_option("session_count", 0, 50)) { - return; - } if (set_integer_option("storage_max_files_size")) { return; } diff --git a/tdutils/td/utils/Random.cpp b/tdutils/td/utils/Random.cpp index d211d31f0..c67021835 100644 --- a/tdutils/td/utils/Random.cpp +++ b/tdutils/td/utils/Random.cpp @@ -134,18 +134,18 @@ uint64 Random::fast_uint64() { return static_cast((*gen)()); } -int Random::fast(int min, int max) { - if (min == std::numeric_limits::min() && max == std::numeric_limits::max()) { +int Random::fast(int min_value, int max_value) { + if (min_value == std::numeric_limits::min() && max_value == std::numeric_limits::max()) { // to prevent integer overflow and division by zero - min++; + min_value++; } - DCHECK(min <= max); - return static_cast(min + fast_uint32() % (max - min + 1)); // TODO signed_cast + DCHECK(min_value <= max_value); + return static_cast(min_value + fast_uint32() % (max_value - min_value + 1)); // TODO signed_cast } -double Random::fast(double min, double max) { - DCHECK(min <= max); - return min + fast_uint32() * 1.0 / std::numeric_limits::max() * (max - min); +double Random::fast(double min_value, double max_value) { + DCHECK(min_value <= max_value); + return min_value + fast_uint32() * 1.0 / std::numeric_limits::max() * (max_value - min_value); } bool Random::fast_bool() { @@ -179,11 +179,11 @@ uint64 Random::Xorshift128plus::operator()() { return seed_[1] + y; } -int Random::Xorshift128plus::fast(int min, int max) { - return static_cast((*this)() % (max - min + 1) + min); +int Random::Xorshift128plus::fast(int min_value, int max_value) { + return static_cast((*this)() % (max_value - min_value + 1) + min_value); } -int64 Random::Xorshift128plus::fast64(int64 min, int64 max) { - return static_cast((*this)() % (max - min + 1) + min); +int64 Random::Xorshift128plus::fast64(int64 min_value, int64 max_value) { + return static_cast((*this)() % (max_value - min_value + 1) + min_value); } void Random::Xorshift128plus::bytes(MutableSlice dest) { diff --git a/tdutils/td/utils/Random.h b/tdutils/td/utils/Random.h index 309f17408..02d361005 100644 --- a/tdutils/td/utils/Random.h +++ b/tdutils/td/utils/Random.h @@ -32,9 +32,9 @@ class Random { static uint32 fast_uint32(); static uint64 fast_uint64(); - // distribution is not uniform, min and max are included - static int fast(int min, int max); - static double fast(double min, double max); + // distribution is not uniform, min_value and max_value are included + static int fast(int min_value, int max_value); + static double fast(double min_value, double max_value); static bool fast_bool(); class Fast { @@ -48,8 +48,8 @@ class Random { explicit Xorshift128plus(uint64 seed); Xorshift128plus(uint64 seed_a, uint64 seed_b); uint64 operator()(); - int fast(int min, int max); - int64 fast64(int64 min, int64 max); + int fast(int min_value, int max_value); + int64 fast64(int64 min_value, int64 max_value); void bytes(MutableSlice dest); private: diff --git a/tdutils/td/utils/port/UdpSocketFd.cpp b/tdutils/td/utils/port/UdpSocketFd.cpp index f2b2e908a..81cae4f62 100644 --- a/tdutils/td/utils/port/UdpSocketFd.cpp +++ b/tdutils/td/utils/port/UdpSocketFd.cpp @@ -790,10 +790,10 @@ const NativeFd &UdpSocketFd::get_native_fd() const { } #if TD_PORT_POSIX -static Result maximize_buffer(int socket_fd, int optname, uint32 max) { - if (setsockopt(socket_fd, SOL_SOCKET, optname, &max, sizeof(max)) == 0) { +static Result maximize_buffer(int socket_fd, int optname, uint32 max_size) { + if (setsockopt(socket_fd, SOL_SOCKET, optname, &max_size, sizeof(max_size)) == 0) { // fast path - return max; + return max_size; } /* Start with the default size. */ @@ -807,32 +807,32 @@ static Result maximize_buffer(int socket_fd, int optname, uint32 max) { #endif /* Binary-search for the real maximum. */ - uint32 last_good = old_size; - uint32 min = old_size; - while (min <= max) { - uint32 avg = min + (max - min) / 2; - if (setsockopt(socket_fd, SOL_SOCKET, optname, &avg, sizeof(avg)) == 0) { - last_good = avg; - min = avg + 1; + uint32 last_good_size = old_size; + uint32 min_size = old_size; + while (min_size <= max_size) { + uint32 avg_size = min_size + (max_size - min_size) / 2; + if (setsockopt(socket_fd, SOL_SOCKET, optname, &avg_size, sizeof(avg_size)) == 0) { + last_good_size = avg_size; + min_size = avg_size + 1; } else { - max = avg - 1; + max_size = avg_size - 1; } } - return last_good; + return last_good_size; } -Result UdpSocketFd::maximize_snd_buffer(uint32 max) { - return maximize_buffer(get_native_fd().fd(), SO_SNDBUF, max == 0 ? DEFAULT_UDP_MAX_SND_BUFFER_SIZE : max); +Result UdpSocketFd::maximize_snd_buffer(uint32 max_size) { + return maximize_buffer(get_native_fd().fd(), SO_SNDBUF, max_size == 0 ? DEFAULT_UDP_MAX_SND_BUFFER_SIZE : max_size); } -Result UdpSocketFd::maximize_rcv_buffer(uint32 max) { - return maximize_buffer(get_native_fd().fd(), SO_RCVBUF, max == 0 ? DEFAULT_UDP_MAX_RCV_BUFFER_SIZE : max); +Result UdpSocketFd::maximize_rcv_buffer(uint32 max_size) { + return maximize_buffer(get_native_fd().fd(), SO_RCVBUF, max_size == 0 ? DEFAULT_UDP_MAX_RCV_BUFFER_SIZE : max_size); } #else -Result UdpSocketFd::maximize_snd_buffer(uint32 max) { +Result UdpSocketFd::maximize_snd_buffer(uint32 max_size) { return 0; } -Result UdpSocketFd::maximize_rcv_buffer(uint32 max) { +Result UdpSocketFd::maximize_rcv_buffer(uint32 max_size) { return 0; } #endif diff --git a/tdutils/td/utils/port/UdpSocketFd.h b/tdutils/td/utils/port/UdpSocketFd.h index ccdcfddfe..0ba7c6236 100644 --- a/tdutils/td/utils/port/UdpSocketFd.h +++ b/tdutils/td/utils/port/UdpSocketFd.h @@ -45,8 +45,8 @@ class UdpSocketFd { UdpSocketFd(const UdpSocketFd &) = delete; UdpSocketFd &operator=(const UdpSocketFd &) = delete; - Result maximize_snd_buffer(uint32 max_buffer_size = 0); - Result maximize_rcv_buffer(uint32 max_buffer_size = 0); + Result maximize_snd_buffer(uint32 max_size = 0); + Result maximize_rcv_buffer(uint32 max_size = 0); static Result open(const IPAddress &address) TD_WARN_UNUSED_RESULT;