mirror of https://github.com/procxx/kepka.git
				
				
				
			Don't always restart connection on config change.
This commit is contained in:
		
							parent
							
								
									e6c86b19db
								
							
						
					
					
						commit
						06f5f7f7d9
					
				|  | @ -399,14 +399,18 @@ void Application::badMtprotoConfigurationError() { | ||||||
| 
 | 
 | ||||||
| void Application::startLocalStorage() { | void Application::startLocalStorage() { | ||||||
| 	Local::start(); | 	Local::start(); | ||||||
| 	subscribe(_dcOptions->changed(), [this](const MTP::DcOptions::Ids &ids) { | 
 | ||||||
|  | 	const auto writing = _lifetime.make_state<bool>(false); | ||||||
|  | 	_dcOptions->changed( | ||||||
|  | 	) | rpl::filter([=] { | ||||||
|  | 		return !*writing; | ||||||
|  | 	}) | rpl::start_with_next([=] { | ||||||
|  | 		*writing = true; | ||||||
|  | 		Ui::PostponeCall(this, [=] { | ||||||
| 			Local::writeSettings(); | 			Local::writeSettings(); | ||||||
| 		if (const auto instance = activeAccount().mtp()) { |  | ||||||
| 			for (const auto id : ids) { |  | ||||||
| 				instance->restart(id); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		}); | 		}); | ||||||
|  | 	}, _lifetime); | ||||||
|  | 
 | ||||||
| 	_saveSettingsTimer.setCallback([=] { Local::writeSettings(); }); | 	_saveSettingsTimer.setCallback([=] { Local::writeSettings(); }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -126,7 +126,9 @@ Connection::~Connection() { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void Connection::start(std::shared_ptr<SessionData> sessionData, ShiftedDcId shiftedDcId) { | void Connection::start( | ||||||
|  | 		std::shared_ptr<SessionData> sessionData, | ||||||
|  | 		ShiftedDcId shiftedDcId) { | ||||||
| 	Expects(_thread == nullptr && _private == nullptr); | 	Expects(_thread == nullptr && _private == nullptr); | ||||||
| 
 | 
 | ||||||
| 	_thread = std::make_unique<QThread>(); | 	_thread = std::make_unique<QThread>(); | ||||||
|  | @ -137,6 +139,16 @@ void Connection::start(std::shared_ptr<SessionData> sessionData, ShiftedDcId shi | ||||||
| 		std::move(sessionData), | 		std::move(sessionData), | ||||||
| 		shiftedDcId); | 		shiftedDcId); | ||||||
| 
 | 
 | ||||||
|  | 	_instance->dcOptions()->changed( | ||||||
|  | 	) | rpl::filter([=](DcId dcId) { | ||||||
|  | 		return (BareDcId(shiftedDcId) == dcId) && (_private != nullptr); | ||||||
|  | 	}) | rpl::start_with_next([=] { | ||||||
|  | 		const auto raw = _private; | ||||||
|  | 		InvokeQueued(raw, [=] { | ||||||
|  | 			raw->dcOptionsChanged(); | ||||||
|  | 		}); | ||||||
|  | 	}, _lifetime); | ||||||
|  | 
 | ||||||
| 	// will be deleted in the thread::finished signal
 | 	// will be deleted in the thread::finished signal
 | ||||||
| 	_private = newData.release(); | 	_private = newData.release(); | ||||||
| 	_thread->start(); | 	_thread->start(); | ||||||
|  | @ -329,10 +341,6 @@ ConnectionPrivate::~ConnectionPrivate() { | ||||||
| 	Expects(_testConnections.empty()); | 	Expects(_testConnections.empty()); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void ConnectionPrivate::onConfigLoaded() { |  | ||||||
| 	connectToServer(true); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void ConnectionPrivate::onCDNConfigLoaded() { | void ConnectionPrivate::onCDNConfigLoaded() { | ||||||
| 	restart(); | 	restart(); | ||||||
| } | } | ||||||
|  | @ -341,6 +349,11 @@ int32 ConnectionPrivate::getShiftedDcId() const { | ||||||
| 	return _shiftedDcId; | 	return _shiftedDcId; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void ConnectionPrivate::dcOptionsChanged() { | ||||||
|  | 	_retryTimeout = 1; | ||||||
|  | 	connectToServer(true); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| int32 ConnectionPrivate::getState() const { | int32 ConnectionPrivate::getState() const { | ||||||
| 	QReadLocker lock(&_stateMutex); | 	QReadLocker lock(&_stateMutex); | ||||||
| 	int32 result = _state; | 	int32 result = _state; | ||||||
|  | @ -1004,7 +1017,6 @@ void ConnectionPrivate::connectToServer(bool afterConfig) { | ||||||
| 			return restart(); | 			return restart(); | ||||||
| 		} | 		} | ||||||
| 		DEBUG_LOG(("MTP Info: DC %1 options not found, waiting for config").arg(_shiftedDcId)); | 		DEBUG_LOG(("MTP Info: DC %1 options not found, waiting for config").arg(_shiftedDcId)); | ||||||
| 		connect(_instance, SIGNAL(configLoaded()), this, SLOT(onConfigLoaded()), Qt::UniqueConnection); |  | ||||||
| 		InvokeQueued(_instance, [instance = _instance] { | 		InvokeQueued(_instance, [instance = _instance] { | ||||||
| 			instance->requestConfig(); | 			instance->requestConfig(); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
|  | @ -58,6 +58,7 @@ private: | ||||||
| 	not_null<Instance*> _instance; | 	not_null<Instance*> _instance; | ||||||
| 	std::unique_ptr<QThread> _thread; | 	std::unique_ptr<QThread> _thread; | ||||||
| 	ConnectionPrivate *_private = nullptr; | 	ConnectionPrivate *_private = nullptr; | ||||||
|  | 	rpl::lifetime _lifetime; | ||||||
| 
 | 
 | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | @ -76,6 +77,7 @@ public: | ||||||
| 	void stop(); | 	void stop(); | ||||||
| 
 | 
 | ||||||
| 	int32 getShiftedDcId() const; | 	int32 getShiftedDcId() const; | ||||||
|  | 	void dcOptionsChanged(); | ||||||
| 
 | 
 | ||||||
| 	int32 getState() const; | 	int32 getState() const; | ||||||
| 	QString transport() const; | 	QString transport() const; | ||||||
|  | @ -90,7 +92,6 @@ public slots: | ||||||
| 
 | 
 | ||||||
| 	void updateAuthKey(); | 	void updateAuthKey(); | ||||||
| 
 | 
 | ||||||
| 	void onConfigLoaded(); |  | ||||||
| 	void onCDNConfigLoaded(); | 	void onCDNConfigLoaded(); | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|  |  | ||||||
|  | @ -170,7 +170,7 @@ void DcOptions::processFromList( | ||||||
| 		ApplyOneOption(data, dcId, flags, ip, port, secret); | 		ApplyOneOption(data, dcId, flags, ip, port, secret); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	auto difference = [&] { | 	const auto difference = [&] { | ||||||
| 		WriteLocker lock(this); | 		WriteLocker lock(this); | ||||||
| 		auto result = CountOptionsDifference(_data, data); | 		auto result = CountOptionsDifference(_data, data); | ||||||
| 		if (!result.empty()) { | 		if (!result.empty()) { | ||||||
|  | @ -178,8 +178,8 @@ void DcOptions::processFromList( | ||||||
| 		} | 		} | ||||||
| 		return result; | 		return result; | ||||||
| 	}(); | 	}(); | ||||||
| 	if (!difference.empty()) { | 	for (const auto dcId : difference) { | ||||||
| 		_changed.notify(std::move(difference)); | 		_changed.fire_copy(dcId); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -232,9 +232,8 @@ void DcOptions::addFromOther(DcOptions &&options) { | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 	for (const auto dcId : idsChanged) { | ||||||
| 	if (!idsChanged.empty()) { | 		_changed.fire_copy(dcId); | ||||||
| 		_changed.notify(std::move(idsChanged)); |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -280,10 +279,10 @@ bool DcOptions::ApplyOneOption( | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| auto DcOptions::CountOptionsDifference( | std::vector<DcId> DcOptions::CountOptionsDifference( | ||||||
| 		const std::map<DcId, std::vector<Endpoint>> &a, | 		const std::map<DcId, std::vector<Endpoint>> &a, | ||||||
| 		const std::map<DcId, std::vector<Endpoint>> &b) -> Ids { | 		const std::map<DcId, std::vector<Endpoint>> &b) { | ||||||
| 	auto result = Ids(); | 	auto result = std::vector<DcId>(); | ||||||
| 	const auto find = []( | 	const auto find = []( | ||||||
| 			const std::vector<Endpoint> &where, | 			const std::vector<Endpoint> &where, | ||||||
| 			const Endpoint &what) { | 			const Endpoint &what) { | ||||||
|  | @ -514,8 +513,12 @@ void DcOptions::constructFromSerialized(const QByteArray &serialized) { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| DcOptions::Ids DcOptions::configEnumDcIds() const { | rpl::producer<DcId> DcOptions::changed() const { | ||||||
| 	auto result = Ids(); | 	return _changed.events(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | std::vector<DcId> DcOptions::configEnumDcIds() const { | ||||||
|  | 	auto result = std::vector<DcId>(); | ||||||
| 	{ | 	{ | ||||||
| 		ReadLocker lock(this); | 		ReadLocker lock(this); | ||||||
| 		result.reserve(_data.size()); | 		result.reserve(_data.size()); | ||||||
|  |  | ||||||
|  | @ -66,15 +66,12 @@ public: | ||||||
| 		const bytes::vector &secret); | 		const bytes::vector &secret); | ||||||
| 	QByteArray serialize() const; | 	QByteArray serialize() const; | ||||||
| 
 | 
 | ||||||
| 	using Ids = std::vector<DcId>; | 	[[nodiscard]] rpl::producer<DcId> changed() const; | ||||||
| 	base::Observable<Ids> &changed() const { |  | ||||||
| 		return _changed; |  | ||||||
| 	} |  | ||||||
| 	void setFromList(const MTPVector<MTPDcOption> &options); | 	void setFromList(const MTPVector<MTPDcOption> &options); | ||||||
| 	void addFromList(const MTPVector<MTPDcOption> &options); | 	void addFromList(const MTPVector<MTPDcOption> &options); | ||||||
| 	void addFromOther(DcOptions &&options); | 	void addFromOther(DcOptions &&options); | ||||||
| 
 | 
 | ||||||
| 	Ids configEnumDcIds() const; | 	[[nodiscard]] std::vector<DcId> configEnumDcIds() const; | ||||||
| 
 | 
 | ||||||
| 	struct Variants { | 	struct Variants { | ||||||
| 		enum Address { | 		enum Address { | ||||||
|  | @ -119,7 +116,7 @@ private: | ||||||
| 		const std::string &ip, | 		const std::string &ip, | ||||||
| 		int port, | 		int port, | ||||||
| 		const bytes::vector &secret); | 		const bytes::vector &secret); | ||||||
| 	static Ids CountOptionsDifference( | 	static std::vector<DcId> CountOptionsDifference( | ||||||
| 		const std::map<DcId, std::vector<Endpoint>> &a, | 		const std::map<DcId, std::vector<Endpoint>> &a, | ||||||
| 		const std::map<DcId, std::vector<Endpoint>> &b); | 		const std::map<DcId, std::vector<Endpoint>> &b); | ||||||
| 	static void FilterIfHasWithFlag(Variants &variants, Flag flag); | 	static void FilterIfHasWithFlag(Variants &variants, Flag flag); | ||||||
|  | @ -143,7 +140,7 @@ private: | ||||||
| 	std::map<DcId, std::map<uint64, internal::RSAPublicKey>> _cdnPublicKeys; | 	std::map<DcId, std::map<uint64, internal::RSAPublicKey>> _cdnPublicKeys; | ||||||
| 	mutable QReadWriteLock _useThroughLockers; | 	mutable QReadWriteLock _useThroughLockers; | ||||||
| 
 | 
 | ||||||
| 	mutable base::Observable<Ids> _changed; | 	rpl::event_stream<DcId> _changed; | ||||||
| 
 | 
 | ||||||
| 	// True when we have overriden options from a .tdesktop-endpoints file.
 | 	// True when we have overriden options from a .tdesktop-endpoints file.
 | ||||||
| 	bool _immutable = false; | 	bool _immutable = false; | ||||||
|  |  | ||||||
|  | @ -860,8 +860,6 @@ void Instance::Private::configLoadDone(const MTPConfig &result) { | ||||||
| 	_configExpiresAt = crl::now() | 	_configExpiresAt = crl::now() | ||||||
| 		+ (data.vexpires().v - base::unixtime::now()) * crl::time(1000); | 		+ (data.vexpires().v - base::unixtime::now()) * crl::time(1000); | ||||||
| 	requestConfigIfExpired(); | 	requestConfigIfExpired(); | ||||||
| 
 |  | ||||||
| 	emit _instance->configLoaded(); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool Instance::Private::configLoadFail(const RPCError &error) { | bool Instance::Private::configLoadFail(const RPCError &error) { | ||||||
|  |  | ||||||
|  | @ -199,7 +199,6 @@ public: | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| signals: | signals: | ||||||
| 	void configLoaded(); |  | ||||||
| 	void cdnConfigLoaded(); | 	void cdnConfigLoaded(); | ||||||
| 	void allKeysDestroyed(); | 	void allKeysDestroyed(); | ||||||
| 	void proxyDomainResolved( | 	void proxyDomainResolved( | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue