1 /*
2 This file is part of Telegram Desktop,
3 the official desktop application for the Telegram messaging service.
4 
5 For license and copyright information please follow this link:
6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
7 */
8 #pragma once
9 
10 #include <QtCore/QReadWriteLock>
11 
12 namespace MTP {
13 
14 class Instance;
15 class AuthKey;
16 using AuthKeyPtr = std::shared_ptr<AuthKey>;
17 enum class DcType;
18 
19 namespace details {
20 
21 enum class TemporaryKeyType {
22 	Regular,
23 	MediaCluster
24 };
25 
26 enum class CreatingKeyType {
27 	None,
28 	Persistent,
29 	TemporaryRegular,
30 	TemporaryMediaCluster
31 };
32 
33 [[nodiscard]] TemporaryKeyType TemporaryKeyTypeByDcType(DcType type);
34 
35 class Dcenter : public QObject {
36 public:
37 	// Main thread.
38 	Dcenter(DcId dcId, AuthKeyPtr &&key);
39 
40 	// Thread-safe.
41 	[[nodiscard]] DcId id() const;
42 	[[nodiscard]] AuthKeyPtr getPersistentKey() const;
43 	[[nodiscard]] AuthKeyPtr getTemporaryKey(TemporaryKeyType type) const;
44 	[[nodiscard]] CreatingKeyType acquireKeyCreation(DcType type);
45 	bool releaseKeyCreationOnDone(
46 		CreatingKeyType type,
47 		const AuthKeyPtr &temporaryKey,
48 		const AuthKeyPtr &persistentKeyUsedForBind);
49 	void releaseKeyCreationOnFail(CreatingKeyType type);
50 	bool destroyTemporaryKey(uint64 keyId);
51 	bool destroyConfirmedForgottenKey(uint64 keyId);
52 
53 	[[nodiscard]] bool connectionInited() const;
54 	void setConnectionInited(bool connectionInited = true);
55 
56 private:
57 	static constexpr auto kTemporaryKeysCount = 2;
58 
59 	const DcId _id = 0;
60 	mutable QReadWriteLock _mutex;
61 
62 	AuthKeyPtr _temporaryKeys[kTemporaryKeysCount];
63 	AuthKeyPtr _persistentKey;
64 	bool _connectionInited = false;
65 	std::atomic<bool> _creatingKeys[kTemporaryKeysCount] = { false };
66 
67 };
68 
69 } // namespace details
70 } // namespace MTP
71