2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetType.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/net/NetStats.h"
|
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2023-08-05 15:09:36 +02:00
|
|
|
#include "td/utils/common.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
struct NetworkStatsEntry {
|
|
|
|
FileType file_type{FileType::None};
|
|
|
|
|
|
|
|
NetType net_type{NetType::Other};
|
|
|
|
int64 rx{0};
|
|
|
|
int64 tx{0};
|
|
|
|
|
|
|
|
bool is_call{false};
|
|
|
|
int64 count{0};
|
|
|
|
double duration{0};
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
tl_object_ptr<td_api::NetworkStatisticsEntry> get_network_statistics_entry_object() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_call) {
|
2020-06-22 03:28:03 +02:00
|
|
|
return make_tl_object<td_api::networkStatisticsEntryCall>(get_network_type_object(net_type), tx, rx, duration);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2020-06-22 03:28:03 +02:00
|
|
|
return make_tl_object<td_api::networkStatisticsEntryFile>(get_file_type_object(file_type),
|
|
|
|
get_network_type_object(net_type), tx, rx);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NetworkStats {
|
|
|
|
int32 since = 0;
|
|
|
|
std::vector<NetworkStatsEntry> entries;
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
auto get_network_statistics_object() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result = make_tl_object<td_api::networkStatistics>();
|
|
|
|
result->since_date_ = since;
|
|
|
|
result->entries_.reserve(entries.size());
|
|
|
|
for (const auto &entry : entries) {
|
2022-04-25 18:00:40 +02:00
|
|
|
if ((entry.rx != 0 || entry.tx != 0) && entry.file_type != FileType::SecureDecrypted) {
|
2020-06-22 03:28:03 +02:00
|
|
|
result->entries_.push_back(entry.get_network_statistics_entry_object());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class NetStatsManager final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit NetStatsManager(ActorShared<> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
// Call init just after actor is registered and before getting callbacks
|
|
|
|
void init();
|
|
|
|
std::shared_ptr<NetStatsCallback> get_common_stats_callback() const;
|
|
|
|
std::shared_ptr<NetStatsCallback> get_media_stats_callback() const;
|
|
|
|
std::vector<std::shared_ptr<NetStatsCallback>> get_file_stats_callbacks() const;
|
|
|
|
|
|
|
|
void get_network_stats(bool current, Promise<NetworkStats> promise);
|
|
|
|
|
|
|
|
void reset_network_stats();
|
|
|
|
|
|
|
|
void add_network_stats(const NetworkStatsEntry &entry);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<> parent_;
|
|
|
|
|
|
|
|
static constexpr size_t net_type_size() {
|
|
|
|
return static_cast<size_t>(NetType::Size);
|
|
|
|
}
|
2020-06-22 03:28:03 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static CSlice net_type_string(NetType type) {
|
|
|
|
switch (type) {
|
|
|
|
case NetType::Other:
|
|
|
|
return CSlice("other");
|
2019-05-14 01:03:05 +02:00
|
|
|
case NetType::WiFi:
|
2018-12-31 20:04:05 +01:00
|
|
|
return CSlice("wifi");
|
|
|
|
case NetType::Mobile:
|
|
|
|
return CSlice("mobile");
|
|
|
|
case NetType::MobileRoaming:
|
|
|
|
return CSlice("mobile_roaming");
|
|
|
|
default:
|
|
|
|
return CSlice("bug");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NetStatsInfo {
|
|
|
|
string key;
|
|
|
|
NetStats stats;
|
|
|
|
NetStatsData last_sync_stats;
|
|
|
|
NetType net_type = NetType::None;
|
|
|
|
|
|
|
|
struct TypeStats {
|
|
|
|
uint64 dirty_size = 0;
|
|
|
|
NetStatsData mem_stats;
|
|
|
|
NetStatsData db_stats;
|
|
|
|
};
|
|
|
|
std::array<TypeStats, 5 /*NetStatsManager::net_type_size()*/> stats_by_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
int32 since_total_{0};
|
|
|
|
int32 since_current_{0};
|
|
|
|
NetStatsInfo common_net_stats_;
|
|
|
|
NetStatsInfo media_net_stats_;
|
2020-06-22 01:27:58 +02:00
|
|
|
std::array<NetStatsInfo, MAX_FILE_TYPE> files_stats_;
|
2018-12-31 20:04:05 +01:00
|
|
|
NetStatsInfo call_net_stats_;
|
2020-06-22 01:27:58 +02:00
|
|
|
static constexpr int32 CALL_NET_STATS_ID{MAX_FILE_TYPE + 2};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class F>
|
|
|
|
void for_each_stat(F &&f) {
|
|
|
|
f(common_net_stats_, 0, CSlice("common"), FileType::None);
|
|
|
|
f(media_net_stats_, 1, CSlice("media"), FileType::None);
|
2020-06-22 01:27:58 +02:00
|
|
|
for (int32 file_type_i = 0; file_type_i < MAX_FILE_TYPE; file_type_i++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &stat = files_stats_[file_type_i];
|
2019-01-19 23:59:37 +01:00
|
|
|
auto file_type = static_cast<FileType>(file_type_i);
|
|
|
|
f(stat, file_type_i + 2, get_file_type_name(file_type), file_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-06-16 04:10:16 +02:00
|
|
|
f(call_net_stats_, CALL_NET_STATS_ID, CSlice("calls"), FileType::None);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-21 11:51:16 +02:00
|
|
|
static void add_network_stats_impl(NetStatsInfo &info, const NetworkStatsEntry &entry);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
2021-10-21 11:51:16 +02:00
|
|
|
|
|
|
|
static void update(NetStatsInfo &info, bool force_save);
|
2021-10-19 17:11:16 +02:00
|
|
|
static void save_stats(NetStatsInfo &info, NetType net_type);
|
2021-10-21 11:51:16 +02:00
|
|
|
static void info_loop(NetStatsInfo &info);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void on_stats_updated(size_t id);
|
|
|
|
void on_net_type_updated(NetType net_type);
|
|
|
|
};
|
2019-07-09 05:56:06 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
networkTypeBluetooth = NetworkType; ?
|
|
|
|
networkTypeEthernet = NetworkType; ?
|
|
|
|
|
|
|
|
Android NetType.
|
|
|
|
TYPE_BLUETOOTH The Bluetooth data connection.
|
|
|
|
TYPE_DUMMY Dummy data connection.
|
|
|
|
TYPE_ETHERNET The Ethernet data connection.
|
|
|
|
TYPE_MOBILE The Mobile data connection.
|
|
|
|
TYPE_MOBILE_DUN A DUN-specific Mobile data connection.
|
|
|
|
TYPE_VPN A virtual network using one or more native bearers.
|
|
|
|
TYPE_WIFI The WIFI data connection.
|
|
|
|
TYPE_WIMAX The WiMAX data connection.
|
|
|
|
|
|
|
|
ios NetType
|
|
|
|
ReachableViaWiFi,
|
|
|
|
ReachableViaWWAN
|
|
|
|
|
|
|
|
|
|
|
|
Mobile subtype
|
|
|
|
2G
|
|
|
|
NETWORK_TYPE_IDEN ~25 kbps
|
|
|
|
NETWORK_TYPE_CDMA ~ 14-64 kbps
|
|
|
|
CTRadioAccessTechnologyCDMA1x
|
|
|
|
NETWORK_TYPE_1xRTT ~ 50-100 kbps
|
|
|
|
NETWORK_TYPE_GPRS ~ 100 kbps
|
|
|
|
CTRadioAccessTechnologyEdge
|
|
|
|
NETWORK_TYPE_EDGE ~ 50-100 kbps
|
|
|
|
CTRadioAccessTechnologyGPRS
|
|
|
|
|
|
|
|
3G
|
|
|
|
NETWORK_TYPE_EVDO_0 ~ 400-1000 kbps
|
|
|
|
CTRadioAccessTechnologyCDMAEVDORev0
|
|
|
|
NETWORK_TYPE_EVDO_A ~ 600-1400 kbps
|
|
|
|
CTRadioAccessTechnologyCDMAEVDORevA
|
|
|
|
NETWORK_TYPE_HSPA ~ 700-1700 kbps
|
|
|
|
NETWORK_TYPE_UMTS ~ 400-7000 kbps
|
|
|
|
NETWORK_TYPE_EHRPD ~ 1-2 Mbps
|
|
|
|
NETWORK_TYPE_EVDO_B ~ 5 Mbps
|
|
|
|
CTRadioAccessTechnologyCDMAEVDORevB
|
|
|
|
NETWORK_TYPE_HSDPA ~ 2-14 Mbps
|
|
|
|
CTRadioAccessTechnologyHSDPA
|
|
|
|
NETWORK_TYPE_HSPAP ~ 10-20 Mbps
|
|
|
|
NETWORK_TYPE_HSUPA ~ 1-23 Mbps
|
|
|
|
CTRadioAccessTechnologyHSUPA
|
|
|
|
|
|
|
|
CTRadioAccessTechnologyWCDMA
|
|
|
|
CTRadioAccessTechnologyeHRPD
|
|
|
|
|
|
|
|
4G
|
|
|
|
NETWORK_TYPE_LTE ~ 10+ Mbps
|
|
|
|
CTRadioAccessTechnologyLTE
|
|
|
|
|
|
|
|
NETWORK_TYPE_GSM
|
|
|
|
NETWORK_TYPE_IWLAN
|
|
|
|
NETWORK_TYPE_TD_SCDMA
|
|
|
|
NETWORK_TYPE_UNKNOWN
|
|
|
|
|
|
|
|
*/
|