2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-12-18 12:40:15 +00:00
|
|
|
#include "mtproto/type_utils.h"
|
2017-02-24 17:15:41 +00:00
|
|
|
#include "mtproto/mtp_instance.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
namespace MTP {
|
2019-12-02 13:10:19 +00:00
|
|
|
namespace details {
|
2016-03-24 08:57:11 +00:00
|
|
|
|
2019-09-13 16:45:48 +00:00
|
|
|
[[nodiscard]] bool paused();
|
2016-12-05 11:01:08 +00:00
|
|
|
void pause();
|
|
|
|
void unpause();
|
2016-03-24 08:57:11 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace details
|
2014-11-12 20:30:26 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// send(MTPhelp_GetConfig(), MTP::configDcId(dc)) - for dc enumeration
|
|
|
|
constexpr ShiftedDcId configDcId(DcId dcId) {
|
2018-06-11 18:35:27 +00:00
|
|
|
return ShiftDcId(dcId, kConfigDcShift);
|
2016-03-24 08:57:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// send(MTPauth_LogOut(), MTP::logoutDcId(dc)) - for logout of guest dcs enumeration
|
|
|
|
constexpr ShiftedDcId logoutDcId(DcId dcId) {
|
2018-06-11 18:35:27 +00:00
|
|
|
return ShiftDcId(dcId, kLogoutDcShift);
|
2016-03-24 08:57:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 18:12:23 +00:00
|
|
|
// send(MTPupload_GetFile(), MTP::updaterDcId(dc)) - for autoupdater
|
|
|
|
constexpr ShiftedDcId updaterDcId(DcId dcId) {
|
2018-06-11 18:35:27 +00:00
|
|
|
return ShiftDcId(dcId, kUpdaterDcShift);
|
2018-05-29 18:12:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
constexpr auto kUploadSessionsCount = 2;
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
namespace details {
|
2016-12-05 11:01:08 +00:00
|
|
|
|
|
|
|
constexpr ShiftedDcId downloadDcId(DcId dcId, int index) {
|
2019-12-04 06:51:21 +00:00
|
|
|
Expects(index < kMaxMediaDcCount);
|
|
|
|
|
2018-06-11 18:35:27 +00:00
|
|
|
return ShiftDcId(dcId, kBaseDownloadDcShift + index);
|
2016-12-05 11:01:08 +00:00
|
|
|
};
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace details
|
2016-03-24 08:57:11 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// send(req, callbacks, MTP::downloadDcId(dc, index)) - for download shifted dc id
|
|
|
|
inline ShiftedDcId downloadDcId(DcId dcId, int index) {
|
2019-12-02 13:10:19 +00:00
|
|
|
return details::downloadDcId(dcId, index);
|
2016-03-24 08:57:11 +00:00
|
|
|
}
|
2016-12-05 11:01:08 +00:00
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
inline constexpr bool isDownloadDcId(ShiftedDcId shiftedDcId) {
|
2019-12-02 13:10:19 +00:00
|
|
|
return (shiftedDcId >= details::downloadDcId(0, 0))
|
2019-12-04 06:51:21 +00:00
|
|
|
&& (shiftedDcId < details::downloadDcId(0, kMaxMediaDcCount - 1) + kDcShift);
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isCdnDc(MTPDdcOption::Flags flags) {
|
|
|
|
return (flags & MTPDdcOption::Flag::f_cdn);
|
2016-03-24 08:57:11 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 17:38:16 +00:00
|
|
|
inline bool isTemporaryDcId(ShiftedDcId shiftedDcId) {
|
2018-06-11 18:35:27 +00:00
|
|
|
auto dcId = BareDcId(shiftedDcId);
|
2017-06-26 17:38:16 +00:00
|
|
|
return (dcId >= Instance::Config::kTemporaryMainDc);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DcId getRealIdFromTemporaryDcId(ShiftedDcId shiftedDcId) {
|
2018-06-11 18:35:27 +00:00
|
|
|
auto dcId = BareDcId(shiftedDcId);
|
2017-06-26 17:38:16 +00:00
|
|
|
return (dcId >= Instance::Config::kTemporaryMainDc) ? (dcId - Instance::Config::kTemporaryMainDc) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DcId getTemporaryIdFromRealDcId(ShiftedDcId shiftedDcId) {
|
2018-06-11 18:35:27 +00:00
|
|
|
auto dcId = BareDcId(shiftedDcId);
|
2017-06-26 17:38:16 +00:00
|
|
|
return (dcId < Instance::Config::kTemporaryMainDc) ? (dcId + Instance::Config::kTemporaryMainDc) : 0;
|
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
namespace details {
|
2016-12-05 11:01:08 +00:00
|
|
|
|
|
|
|
constexpr ShiftedDcId uploadDcId(DcId dcId, int index) {
|
2018-06-11 18:35:27 +00:00
|
|
|
static_assert(kUploadSessionsCount < kMaxMediaDcCount, "Too large MTPUploadSessionsCount!");
|
|
|
|
return ShiftDcId(dcId, kBaseUploadDcShift + index);
|
2016-12-05 11:01:08 +00:00
|
|
|
};
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace details
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// send(req, callbacks, MTP::uploadDcId(index)) - for upload shifted dc id
|
2018-06-11 18:35:27 +00:00
|
|
|
// uploading always to the main dc so BareDcId(result) == 0
|
2017-02-25 16:44:02 +00:00
|
|
|
inline ShiftedDcId uploadDcId(int index) {
|
2017-03-23 16:11:35 +00:00
|
|
|
Expects(index >= 0 && index < kUploadSessionsCount);
|
2018-06-11 18:35:27 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
return details::uploadDcId(0, index);
|
2016-03-24 08:57:11 +00:00
|
|
|
};
|
2016-12-05 11:01:08 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
constexpr bool isUploadDcId(ShiftedDcId shiftedDcId) {
|
2019-12-02 13:10:19 +00:00
|
|
|
return (shiftedDcId >= details::uploadDcId(0, 0))
|
|
|
|
&& (shiftedDcId < details::uploadDcId(0, kUploadSessionsCount - 1) + kDcShift);
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ShiftedDcId destroyKeyNextDcId(ShiftedDcId shiftedDcId) {
|
2018-06-11 18:35:27 +00:00
|
|
|
const auto shift = GetDcIdShift(shiftedDcId);
|
|
|
|
return ShiftDcId(BareDcId(shiftedDcId), shift ? (shift + 1) : kDestroyKeyStartDcShift);
|
2016-03-24 08:57:11 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
enum {
|
|
|
|
DisconnectedState = 0,
|
|
|
|
ConnectingState = 1,
|
|
|
|
ConnectedState = 2,
|
|
|
|
};
|
2015-10-15 10:18:24 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
enum {
|
|
|
|
RequestSent = 0,
|
|
|
|
RequestConnecting = 1,
|
|
|
|
RequestSending = 2
|
|
|
|
};
|
2016-12-05 11:01:08 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
Instance *MainInstance();
|
2016-12-05 11:01:08 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
inline void restart() {
|
|
|
|
return MainInstance()->restart();
|
|
|
|
}
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
inline void restart(ShiftedDcId shiftedDcId) {
|
|
|
|
return MainInstance()->restart(shiftedDcId);
|
|
|
|
}
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
inline DcId maindc() {
|
|
|
|
return MainInstance()->mainDcId();
|
|
|
|
}
|
2015-05-14 16:50:04 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
inline int32 dcstate(ShiftedDcId shiftedDcId = 0) {
|
2019-01-21 13:42:21 +00:00
|
|
|
if (const auto instance = MainInstance()) {
|
2017-02-24 17:15:41 +00:00
|
|
|
return instance->dcstate(shiftedDcId);
|
|
|
|
}
|
|
|
|
return DisconnectedState;
|
|
|
|
}
|
2016-03-24 08:57:11 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
inline QString dctransport(ShiftedDcId shiftedDcId = 0) {
|
|
|
|
if (auto instance = MainInstance()) {
|
|
|
|
return instance->dctransport(shiftedDcId);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
return QString();
|
2016-03-24 08:57:11 +00:00
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
template <typename TRequest>
|
2017-12-18 12:40:15 +00:00
|
|
|
inline mtpRequestId send(
|
|
|
|
const TRequest &request,
|
|
|
|
RPCResponseHandler &&callbacks = {},
|
|
|
|
ShiftedDcId dcId = 0,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time msCanWait = 0,
|
2017-12-18 12:40:15 +00:00
|
|
|
mtpRequestId after = 0) {
|
2017-02-24 17:15:41 +00:00
|
|
|
return MainInstance()->send(request, std::move(callbacks), dcId, msCanWait, after);
|
2016-03-24 08:57:11 +00:00
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
|
|
|
|
template <typename TRequest>
|
2017-12-18 12:40:15 +00:00
|
|
|
inline mtpRequestId send(
|
|
|
|
const TRequest &request,
|
|
|
|
RPCDoneHandlerPtr &&onDone,
|
|
|
|
RPCFailHandlerPtr &&onFail = nullptr,
|
|
|
|
ShiftedDcId dcId = 0,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time msCanWait = 0,
|
2017-12-18 12:40:15 +00:00
|
|
|
mtpRequestId after = 0) {
|
2017-02-24 17:15:41 +00:00
|
|
|
return MainInstance()->send(request, std::move(onDone), std::move(onFail), dcId, msCanWait, after);
|
2016-03-24 08:57:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
inline void sendAnything(ShiftedDcId shiftedDcId = 0, crl::time msCanWait = 0) {
|
2019-01-21 13:42:21 +00:00
|
|
|
if (const auto instance = MainInstance()) {
|
|
|
|
instance->sendAnything(shiftedDcId, msCanWait);
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
inline void cancel(mtpRequestId requestId) {
|
2019-01-21 13:42:21 +00:00
|
|
|
if (const auto instance = MainInstance()) {
|
|
|
|
instance->cancel(requestId);
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
inline void ping() {
|
2019-01-21 13:42:21 +00:00
|
|
|
if (const auto instance = MainInstance()) {
|
|
|
|
instance->ping();
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
inline void killSession(ShiftedDcId shiftedDcId) {
|
2019-01-21 13:42:21 +00:00
|
|
|
if (const auto instance = MainInstance()) {
|
|
|
|
instance->killSession(shiftedDcId);
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void stopSession(ShiftedDcId shiftedDcId) {
|
2019-01-21 13:42:21 +00:00
|
|
|
if (const auto instance = MainInstance()) {
|
|
|
|
instance->stopSession(shiftedDcId);
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2014-08-01 18:49:43 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
inline int32 state(mtpRequestId requestId) { // < 0 means waiting for such count of ms
|
|
|
|
return MainInstance()->state(requestId);
|
|
|
|
}
|
2015-05-14 16:50:04 +00:00
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
} // namespace MTP
|