| 6 | namespace tgcalls { |
| 7 | namespace { |
| 8 |
| 9 | std::function<void(std::string const &)> globalLoggingFunction; |
| 10 |
| 11 | std::map<std::string, std::shared_ptr<Meta>> &MetaMap() { |
| 12 | static auto result = std::map<std::string, std::shared_ptr<Meta>>(); |
| 13 | return result; |
| 14 | } |
| 15 |
| 16 | } // namespace |
| 17 |
| 18 | std::vector<std::string> Meta::Versions() { |
| 19 | auto &map = MetaMap(); |
| 20 | auto result = std::vector<std::string>(); |
| 21 | result.reserve(map.size()); |
| 22 | for (const auto &entry : map) { |
| 23 | result.push_back(entry.first); |
| 24 | } |
| 25 | return result; |
| 26 | } |
| 27 |
| 28 | int Meta::MaxLayer() { |
| 29 | auto result = 0; |
| 30 | for (const auto &entry : MetaMap()) { |
| 31 | result = std::max(result, entry.second->connectionMaxLayer()); |
| 32 | } |
| 33 | return result; |
| 34 | } |
| 35 |
| 36 | std::unique_ptr<Instance> Meta::Create( |
| 37 | const std::string &version, |
| 38 | Descriptor &&descriptor) { |
| 39 | const auto i = MetaMap().find(version); |
| 40 |
| 41 | // Enforce correct protocol version. |
| 42 | if (version == "2.7.7") { |
| 43 | descriptor.config.protocolVersion = ProtocolVersion::V0; |
| 44 | } else if (version == "5.0.0") { |
| 45 | descriptor.config.protocolVersion = ProtocolVersion::V1; |
| 46 | } |
| 47 |
| 48 | return (i != MetaMap().end()) |
| 49 | ? i->second->construct(std::move(descriptor)) |
| 50 | : nullptr; |
| 51 | } |
| 52 |
| 53 | void Meta::RegisterOne(std::shared_ptr<Meta> meta) { |
| 54 | if (meta) { |
| 55 | const auto versions = meta->versions(); |
| 56 | for (auto &it : versions) { |
| 57 | MetaMap().emplace(it, meta); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 |
| 62 | void SetLoggingFunction(std::function<void(std::string const &)> loggingFunction) { |
| 63 | globalLoggingFunction = loggingFunction; |
| 64 | } |
| 65 |
| 66 | } // namespace tgcalls |
| 67 |