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
|
|
|
*/
|
2016-03-23 18:43:12 +00:00
|
|
|
#include "mtproto/session.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
#include "mtproto/details/mtproto_dcenter.h"
|
|
|
|
#include "mtproto/session_private.h"
|
2019-11-18 09:28:14 +00:00
|
|
|
#include "mtproto/mtproto_auth_key.h"
|
2019-07-10 17:28:33 +00:00
|
|
|
#include "base/unixtime.h"
|
2019-11-15 13:04:32 +00:00
|
|
|
#include "base/openssl_help.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
2017-02-24 17:15:41 +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-12-02 13:10:19 +00:00
|
|
|
SessionOptions::SessionOptions(
|
2018-04-24 08:46:27 +00:00
|
|
|
const QString &systemLangCode,
|
|
|
|
const QString &cloudLangCode,
|
2018-08-20 11:31:40 +00:00
|
|
|
const QString &langPackName,
|
2018-04-24 08:46:27 +00:00
|
|
|
const ProxyData &proxy,
|
|
|
|
bool useIPv4,
|
|
|
|
bool useIPv6,
|
|
|
|
bool useHttp,
|
|
|
|
bool useTcp)
|
|
|
|
: systemLangCode(systemLangCode)
|
|
|
|
, cloudLangCode(cloudLangCode)
|
2018-08-20 11:31:40 +00:00
|
|
|
, langPackName(langPackName)
|
2018-04-24 08:46:27 +00:00
|
|
|
, proxy(proxy)
|
|
|
|
, useIPv4(useIPv4)
|
|
|
|
, useIPv6(useIPv6)
|
|
|
|
, useHttp(useHttp)
|
|
|
|
, useTcp(useTcp) {
|
|
|
|
}
|
|
|
|
|
2019-11-18 12:53:37 +00:00
|
|
|
template <typename Callback>
|
|
|
|
void SessionData::withSession(Callback &&callback) {
|
|
|
|
QMutexLocker lock(&_ownerMutex);
|
|
|
|
if (const auto session = _owner) {
|
|
|
|
InvokeQueued(session, [
|
|
|
|
session,
|
|
|
|
callback = std::forward<Callback>(callback)
|
|
|
|
] {
|
|
|
|
callback(session);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
void SessionData::notifyConnectionInited(const SessionOptions &options) {
|
2019-11-15 13:04:32 +00:00
|
|
|
// #TODO race
|
2019-12-02 13:10:19 +00:00
|
|
|
const auto current = this->options();
|
2019-11-15 13:04:32 +00:00
|
|
|
if (current.cloudLangCode == _options.cloudLangCode
|
|
|
|
&& current.systemLangCode == _options.systemLangCode
|
|
|
|
&& current.langPackName == _options.langPackName
|
|
|
|
&& current.proxy == _options.proxy) {
|
2019-11-20 13:33:45 +00:00
|
|
|
QMutexLocker lock(&_ownerMutex);
|
|
|
|
if (_owner) {
|
|
|
|
_owner->notifyDcConnectionInited();
|
|
|
|
}
|
2018-05-17 19:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 12:53:37 +00:00
|
|
|
void SessionData::queueTryToReceive() {
|
|
|
|
withSession([](not_null<Session*> session) {
|
|
|
|
session->tryToReceive();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionData::queueNeedToResumeAndSend() {
|
|
|
|
withSession([](not_null<Session*> session) {
|
|
|
|
session->needToResumeAndSend();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionData::queueConnectionStateChange(int newState) {
|
|
|
|
withSession([=](not_null<Session*> session) {
|
|
|
|
session->connectionStateChange(newState);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionData::queueResetDone() {
|
|
|
|
withSession([](not_null<Session*> session) {
|
|
|
|
session->resetDone();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionData::queueSendAnything(crl::time msCanWait) {
|
|
|
|
withSession([=](not_null<Session*> session) {
|
|
|
|
session->sendAnything(msCanWait);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SessionData::connectionInited() const {
|
|
|
|
QMutexLocker lock(&_ownerMutex);
|
|
|
|
return _owner ? _owner->connectionInited() : false;
|
|
|
|
}
|
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
AuthKeyPtr SessionData::getTemporaryKey(TemporaryKeyType type) const {
|
2019-11-18 12:53:37 +00:00
|
|
|
QMutexLocker lock(&_ownerMutex);
|
2019-11-21 10:37:39 +00:00
|
|
|
return _owner ? _owner->getTemporaryKey(type) : nullptr;
|
2019-11-19 10:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuthKeyPtr SessionData::getPersistentKey() const {
|
|
|
|
QMutexLocker lock(&_ownerMutex);
|
|
|
|
return _owner ? _owner->getPersistentKey() : nullptr;
|
2019-11-18 12:53:37 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 10:52:29 +00:00
|
|
|
CreatingKeyType SessionData::acquireKeyCreation(DcType type) {
|
2019-11-18 12:53:37 +00:00
|
|
|
QMutexLocker lock(&_ownerMutex);
|
2019-11-21 10:37:39 +00:00
|
|
|
return _owner ? _owner->acquireKeyCreation(type) : CreatingKeyType::None;
|
2019-11-18 12:53:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
bool SessionData::releaseKeyCreationOnDone(
|
2019-11-19 10:10:51 +00:00
|
|
|
const AuthKeyPtr &temporaryKey,
|
2019-11-21 10:37:39 +00:00
|
|
|
const AuthKeyPtr &persistentKeyUsedForBind) {
|
2019-11-18 12:53:37 +00:00
|
|
|
QMutexLocker lock(&_ownerMutex);
|
2019-11-21 10:37:39 +00:00
|
|
|
return _owner
|
|
|
|
? _owner->releaseKeyCreationOnDone(
|
|
|
|
temporaryKey,
|
|
|
|
persistentKeyUsedForBind)
|
|
|
|
: false;
|
2019-11-18 12:53:37 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 10:52:29 +00:00
|
|
|
bool SessionData::releaseCdnKeyCreationOnDone(
|
|
|
|
const AuthKeyPtr &temporaryKey) {
|
|
|
|
QMutexLocker lock(&_ownerMutex);
|
|
|
|
return _owner
|
|
|
|
? _owner->releaseCdnKeyCreationOnDone(temporaryKey)
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
|
2019-11-18 12:53:37 +00:00
|
|
|
void SessionData::releaseKeyCreationOnFail() {
|
|
|
|
QMutexLocker lock(&_ownerMutex);
|
|
|
|
if (_owner) {
|
|
|
|
_owner->releaseKeyCreationOnFail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
void SessionData::destroyTemporaryKey(uint64 keyId) {
|
2019-11-18 12:53:37 +00:00
|
|
|
QMutexLocker lock(&_ownerMutex);
|
|
|
|
if (_owner) {
|
2019-11-19 10:10:51 +00:00
|
|
|
_owner->destroyTemporaryKey(keyId);
|
2019-11-18 12:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionData::detach() {
|
|
|
|
QMutexLocker lock(&_ownerMutex);
|
|
|
|
_owner = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-11-15 07:28:33 +00:00
|
|
|
Session::Session(
|
|
|
|
not_null<Instance*> instance,
|
2019-11-27 15:09:56 +00:00
|
|
|
not_null<QThread*> thread,
|
2019-11-15 07:28:33 +00:00
|
|
|
ShiftedDcId shiftedDcId,
|
2019-11-20 13:33:45 +00:00
|
|
|
not_null<Dcenter*> dc)
|
2019-11-27 14:22:22 +00:00
|
|
|
: _instance(instance)
|
2019-11-14 07:13:17 +00:00
|
|
|
, _shiftedDcId(shiftedDcId)
|
2019-11-20 13:33:45 +00:00
|
|
|
, _dc(dc)
|
2019-11-18 12:53:37 +00:00
|
|
|
, _data(std::make_shared<SessionData>(this))
|
2019-11-27 15:09:56 +00:00
|
|
|
, _thread(thread)
|
2019-11-14 07:13:17 +00:00
|
|
|
, _sender([=] { needToResumeAndSend(); }) {
|
2019-11-14 17:39:15 +00:00
|
|
|
_timeouter.callEach(1000);
|
2018-05-17 19:58:00 +00:00
|
|
|
refreshOptions();
|
2019-11-20 13:33:45 +00:00
|
|
|
watchDcKeyChanges();
|
2019-11-27 14:22:22 +00:00
|
|
|
watchDcOptionsChanges();
|
2019-11-27 15:09:56 +00:00
|
|
|
start();
|
2019-11-27 14:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Session::~Session() {
|
2019-12-02 13:10:19 +00:00
|
|
|
Expects(!_private);
|
2019-11-27 14:22:22 +00:00
|
|
|
|
|
|
|
if (_myKeyCreation != CreatingKeyType::None) {
|
|
|
|
releaseKeyCreationOnFail();
|
|
|
|
}
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-11-18 12:53:37 +00:00
|
|
|
void Session::watchDcKeyChanges() {
|
2019-11-19 10:10:51 +00:00
|
|
|
_instance->dcTemporaryKeyChanged(
|
2019-11-18 12:53:37 +00:00
|
|
|
) | rpl::filter([=](DcId dcId) {
|
|
|
|
return (dcId == _shiftedDcId) || (dcId == BareDcId(_shiftedDcId));
|
|
|
|
}) | rpl::start_with_next([=] {
|
2019-12-02 09:40:53 +00:00
|
|
|
DEBUG_LOG(("AuthKey Info: dcTemporaryKeyChanged in Session %1"
|
|
|
|
).arg(_shiftedDcId));
|
2019-12-02 13:10:19 +00:00
|
|
|
if (const auto captured = _private) {
|
|
|
|
InvokeQueued(captured, [=] {
|
2019-12-02 09:40:53 +00:00
|
|
|
DEBUG_LOG(("AuthKey Info: calling Connection::updateAuthKey in Session %1"
|
|
|
|
).arg(_shiftedDcId));
|
2019-12-02 13:10:19 +00:00
|
|
|
captured->updateAuthKey();
|
2019-11-27 14:22:22 +00:00
|
|
|
});
|
|
|
|
}
|
2019-11-18 12:53:37 +00:00
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:22:22 +00:00
|
|
|
void Session::watchDcOptionsChanges() {
|
2020-06-17 09:36:25 +00:00
|
|
|
_instance->dcOptions().changed(
|
2019-11-27 14:22:22 +00:00
|
|
|
) | rpl::filter([=](DcId dcId) {
|
2019-12-02 13:10:19 +00:00
|
|
|
return (BareDcId(_shiftedDcId) == dcId) && (_private != nullptr);
|
2019-11-27 14:22:22 +00:00
|
|
|
}) | rpl::start_with_next([=] {
|
2019-12-02 13:10:19 +00:00
|
|
|
InvokeQueued(_private, [captured = _private] {
|
|
|
|
captured->dcOptionsChanged();
|
2019-11-27 14:22:22 +00:00
|
|
|
});
|
|
|
|
}, _lifetime);
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
if (_instance->dcOptions().dcType(_shiftedDcId) == DcType::Cdn) {
|
|
|
|
_instance->dcOptions().cdnConfigChanged(
|
2019-11-27 14:22:22 +00:00
|
|
|
) | rpl::filter([=] {
|
2019-12-02 13:10:19 +00:00
|
|
|
return (_private != nullptr);
|
2019-11-27 14:22:22 +00:00
|
|
|
}) | rpl::start_with_next([=] {
|
2019-12-02 13:10:19 +00:00
|
|
|
InvokeQueued(_private, [captured = _private] {
|
|
|
|
captured->cdnConfigChanged();
|
2019-11-27 14:22:22 +00:00
|
|
|
});
|
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
void Session::start() {
|
2019-11-27 14:22:22 +00:00
|
|
|
killConnection();
|
2019-12-02 13:10:19 +00:00
|
|
|
_private = new SessionPrivate(
|
2019-11-27 14:22:22 +00:00
|
|
|
_instance,
|
2019-11-27 15:09:56 +00:00
|
|
|
_thread.get(),
|
2019-11-27 14:22:22 +00:00
|
|
|
_data,
|
|
|
|
_shiftedDcId);
|
2016-10-24 15:36:17 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
bool Session::rpcErrorOccured(
|
|
|
|
mtpRequestId requestId,
|
|
|
|
const RPCFailHandlerPtr &onFail,
|
|
|
|
const RPCError &error) { // return true if need to clean request data
|
|
|
|
return _instance->rpcErrorOccured(requestId, onFail, error);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::restart() {
|
2015-05-14 16:50:04 +00:00
|
|
|
if (_killed) {
|
|
|
|
DEBUG_LOG(("Session Error: can't restart a killed session"));
|
|
|
|
return;
|
|
|
|
}
|
2018-05-17 19:58:00 +00:00
|
|
|
refreshOptions();
|
2019-12-02 13:10:19 +00:00
|
|
|
if (const auto captured = _private) {
|
|
|
|
InvokeQueued(captured, [=] {
|
|
|
|
captured->restartNow();
|
2019-11-27 14:22:22 +00:00
|
|
|
});
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void Session::refreshOptions() {
|
2018-04-27 17:26:45 +00:00
|
|
|
const auto &proxy = Global::SelectedProxy();
|
2018-11-05 13:58:24 +00:00
|
|
|
const auto proxyType =
|
|
|
|
(Global::ProxySettings() == ProxyData::Settings::Enabled
|
|
|
|
? proxy.type
|
|
|
|
: ProxyData::Type::None);
|
2018-04-27 17:26:45 +00:00
|
|
|
const auto useTcp = (proxyType != ProxyData::Type::Http);
|
|
|
|
const auto useHttp = (proxyType != ProxyData::Type::Mtproto);
|
2018-04-24 08:46:27 +00:00
|
|
|
const auto useIPv4 = true;
|
2018-04-24 19:09:20 +00:00
|
|
|
const auto useIPv6 = Global::TryIPv6();
|
2019-12-02 13:10:19 +00:00
|
|
|
_data->setOptions(SessionOptions(
|
2018-04-24 08:46:27 +00:00
|
|
|
_instance->systemLangCode(),
|
|
|
|
_instance->cloudLangCode(),
|
2018-08-20 11:31:40 +00:00
|
|
|
_instance->langPackName(),
|
2018-11-05 13:58:24 +00:00
|
|
|
(Global::ProxySettings() == ProxyData::Settings::Enabled
|
|
|
|
? proxy
|
|
|
|
: ProxyData()),
|
2018-04-24 08:46:27 +00:00
|
|
|
useIPv4,
|
|
|
|
useIPv6,
|
|
|
|
useHttp,
|
|
|
|
useTcp));
|
|
|
|
}
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void Session::reInitConnection() {
|
2019-11-15 13:04:32 +00:00
|
|
|
_dc->setConnectionInited(false);
|
2018-05-17 19:58:00 +00:00
|
|
|
restart();
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::stop() {
|
2016-02-29 16:53:26 +00:00
|
|
|
if (_killed) {
|
2019-11-27 14:22:22 +00:00
|
|
|
DEBUG_LOG(("Session Error: can't stop a killed session"));
|
2016-02-29 16:53:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-14 07:13:17 +00:00
|
|
|
DEBUG_LOG(("Session Info: stopping session dcWithShift %1").arg(_shiftedDcId));
|
2019-11-27 14:22:22 +00:00
|
|
|
killConnection();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::kill() {
|
2015-05-14 16:50:04 +00:00
|
|
|
stop();
|
|
|
|
_killed = true;
|
2019-11-18 12:53:37 +00:00
|
|
|
_data->detach();
|
2019-11-14 07:13:17 +00:00
|
|
|
DEBUG_LOG(("Session Info: marked session dcWithShift %1 as killed").arg(_shiftedDcId));
|
2015-05-14 16:50:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::unpaused() {
|
2015-10-15 10:18:24 +00:00
|
|
|
if (_needToReceive) {
|
|
|
|
_needToReceive = false;
|
2019-11-18 12:53:37 +00:00
|
|
|
InvokeQueued(this, [=] {
|
|
|
|
tryToReceive();
|
|
|
|
});
|
2015-10-15 10:18:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 12:53:37 +00:00
|
|
|
void Session::sendAnything(crl::time msCanWait) {
|
2015-05-14 16:50:04 +00:00
|
|
|
if (_killed) {
|
|
|
|
DEBUG_LOG(("Session Error: can't send anything in a killed session"));
|
|
|
|
return;
|
|
|
|
}
|
2019-11-14 07:13:17 +00:00
|
|
|
const auto ms = crl::now();
|
|
|
|
if (_msSendCall) {
|
|
|
|
if (ms > _msSendCall + _msWait) {
|
|
|
|
_msWait = 0;
|
2014-11-05 17:43:32 +00:00
|
|
|
} else {
|
2019-11-14 07:13:17 +00:00
|
|
|
_msWait = (_msSendCall + _msWait) - ms;
|
|
|
|
if (_msWait > msCanWait) {
|
|
|
|
_msWait = msCanWait;
|
2014-11-05 17:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-14 07:13:17 +00:00
|
|
|
_msWait = msCanWait;
|
2014-11-05 17:43:32 +00:00
|
|
|
}
|
2019-11-14 07:13:17 +00:00
|
|
|
if (_msWait) {
|
|
|
|
DEBUG_LOG(("MTP Info: dcWithShift %1 can wait for %2ms from current %3").arg(_shiftedDcId).arg(_msWait).arg(_msSendCall));
|
|
|
|
_msSendCall = ms;
|
|
|
|
_sender.callOnce(_msWait);
|
2014-11-05 17:43:32 +00:00
|
|
|
} else {
|
2019-11-14 07:13:17 +00:00
|
|
|
DEBUG_LOG(("MTP Info: dcWithShift %1 stopped send timer, can wait for %2ms from current %3").arg(_shiftedDcId).arg(_msWait).arg(_msSendCall));
|
|
|
|
_sender.cancel();
|
|
|
|
_msSendCall = 0;
|
2014-12-05 13:44:27 +00:00
|
|
|
needToResumeAndSend();
|
2014-11-05 17:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::needToResumeAndSend() {
|
2015-05-14 16:50:04 +00:00
|
|
|
if (_killed) {
|
|
|
|
DEBUG_LOG(("Session Info: can't resume a killed session"));
|
|
|
|
return;
|
|
|
|
}
|
2019-12-02 13:10:19 +00:00
|
|
|
if (!_private) {
|
2019-11-14 07:13:17 +00:00
|
|
|
DEBUG_LOG(("Session Info: resuming session dcWithShift %1").arg(_shiftedDcId));
|
2019-11-14 17:39:15 +00:00
|
|
|
start();
|
2014-12-05 13:44:27 +00:00
|
|
|
}
|
2019-12-02 13:10:19 +00:00
|
|
|
const auto captured = _private;
|
2019-11-27 14:22:22 +00:00
|
|
|
const auto ping = base::take(_ping);
|
2019-12-02 13:10:19 +00:00
|
|
|
InvokeQueued(captured, [=] {
|
2019-11-27 14:22:22 +00:00
|
|
|
if (ping) {
|
2019-12-02 13:10:19 +00:00
|
|
|
captured->sendPingForce();
|
2019-11-27 14:22:22 +00:00
|
|
|
} else {
|
2019-12-02 13:10:19 +00:00
|
|
|
captured->tryToSend();
|
2019-11-27 14:22:22 +00:00
|
|
|
}
|
|
|
|
});
|
2014-12-05 13:44:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 12:53:37 +00:00
|
|
|
void Session::connectionStateChange(int newState) {
|
2019-11-14 07:13:17 +00:00
|
|
|
_instance->onStateChange(_shiftedDcId, newState);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 12:53:37 +00:00
|
|
|
void Session::resetDone() {
|
2019-11-14 07:13:17 +00:00
|
|
|
_instance->onSessionReset(_shiftedDcId);
|
2014-08-01 18:49:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::cancel(mtpRequestId requestId, mtpMsgId msgId) {
|
2014-11-05 17:43:32 +00:00
|
|
|
if (requestId) {
|
2019-11-18 12:53:37 +00:00
|
|
|
QWriteLocker locker(_data->toSendMutex());
|
|
|
|
_data->toSendMap().remove(requestId);
|
2014-11-05 17:43:32 +00:00
|
|
|
}
|
|
|
|
if (msgId) {
|
2019-11-18 12:53:37 +00:00
|
|
|
QWriteLocker locker(_data->haveSentMutex());
|
|
|
|
_data->haveSentMap().remove(msgId);
|
2014-11-05 17:43:32 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::ping() {
|
2015-04-16 14:59:42 +00:00
|
|
|
_ping = true;
|
2019-11-18 12:53:37 +00:00
|
|
|
sendAnything();
|
2015-04-16 14:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
int32 Session::requestState(mtpRequestId requestId) const {
|
2014-05-30 08:53:19 +00:00
|
|
|
int32 result = MTP::RequestSent;
|
2016-02-29 16:53:26 +00:00
|
|
|
|
|
|
|
bool connected = false;
|
2019-12-02 13:10:19 +00:00
|
|
|
if (_private) {
|
|
|
|
const auto s = _private->getState();
|
2016-03-24 08:57:11 +00:00
|
|
|
if (s == ConnectedState) {
|
2016-02-29 16:53:26 +00:00
|
|
|
connected = true;
|
2016-03-24 08:57:11 +00:00
|
|
|
} else if (s == ConnectingState || s == DisconnectedState) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (result < 0 || result == MTP::RequestSent) {
|
|
|
|
result = MTP::RequestConnecting;
|
|
|
|
}
|
|
|
|
} else if (s < 0) {
|
2014-06-16 09:31:10 +00:00
|
|
|
if ((result < 0 && s > result) || result == MTP::RequestSent) {
|
2014-05-30 08:53:19 +00:00
|
|
|
result = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 16:53:26 +00:00
|
|
|
if (!connected) {
|
2014-05-30 08:53:19 +00:00
|
|
|
return result;
|
2019-11-22 09:15:38 +00:00
|
|
|
} else if (!requestId) {
|
|
|
|
return MTP::RequestSent;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 12:53:37 +00:00
|
|
|
QWriteLocker locker(_data->toSendMutex());
|
2019-11-22 09:15:38 +00:00
|
|
|
return _data->toSendMap().contains(requestId)
|
|
|
|
? MTP::RequestSending
|
|
|
|
: MTP::RequestSent;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
int32 Session::getState() const {
|
2014-05-30 08:53:19 +00:00
|
|
|
int32 result = -86400000;
|
2016-02-29 16:53:26 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
if (_private) {
|
|
|
|
const auto s = _private->getState();
|
2016-03-24 08:57:11 +00:00
|
|
|
if (s == ConnectedState) {
|
2014-05-30 08:53:19 +00:00
|
|
|
return s;
|
2016-03-24 08:57:11 +00:00
|
|
|
} else if (s == ConnectingState || s == DisconnectedState) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (result < 0) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
} else if (s < 0) {
|
|
|
|
if (result < 0 && s > result) {
|
|
|
|
result = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result == -86400000) {
|
2016-03-24 08:57:11 +00:00
|
|
|
result = DisconnectedState;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
QString Session::transport() const {
|
2019-12-02 13:10:19 +00:00
|
|
|
return _private ? _private->transport() : QString();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
void Session::sendPrepared(
|
2019-12-02 13:10:19 +00:00
|
|
|
const SerializedRequest &request,
|
2019-11-22 09:06:48 +00:00
|
|
|
crl::time msCanWait) {
|
2018-06-02 14:29:21 +00:00
|
|
|
DEBUG_LOG(("MTP Info: adding request to toSendMap, msCanWait %1"
|
|
|
|
).arg(msCanWait));
|
2014-05-30 08:53:19 +00:00
|
|
|
{
|
2019-11-18 12:53:37 +00:00
|
|
|
QWriteLocker locker(_data->toSendMutex());
|
2019-11-22 09:15:38 +00:00
|
|
|
_data->toSendMap().emplace(request->requestId, request);
|
2019-11-22 09:06:48 +00:00
|
|
|
*(mtpMsgId*)(request->data() + 4) = 0;
|
|
|
|
*(request->data() + 6) = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_LOG(("MTP Info: added, requestId %1").arg(request->requestId));
|
2019-11-19 16:14:50 +00:00
|
|
|
if (msCanWait >= 0) {
|
|
|
|
InvokeQueued(this, [=] {
|
|
|
|
sendAnything(msCanWait);
|
|
|
|
});
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 10:52:29 +00:00
|
|
|
CreatingKeyType Session::acquireKeyCreation(DcType type) {
|
2019-11-21 10:37:39 +00:00
|
|
|
Expects(_myKeyCreation == CreatingKeyType::None);
|
2019-11-18 12:53:37 +00:00
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
_myKeyCreation = _dc->acquireKeyCreation(type);
|
|
|
|
return _myKeyCreation;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
bool Session::releaseKeyCreationOnDone(
|
2019-11-19 10:10:51 +00:00
|
|
|
const AuthKeyPtr &temporaryKey,
|
2019-11-21 10:37:39 +00:00
|
|
|
const AuthKeyPtr &persistentKeyUsedForBind) {
|
|
|
|
Expects(_myKeyCreation != CreatingKeyType::None);
|
|
|
|
Expects(persistentKeyUsedForBind != nullptr);
|
|
|
|
|
2019-12-28 10:52:29 +00:00
|
|
|
return releaseGenericKeyCreationOnDone(
|
|
|
|
temporaryKey,
|
|
|
|
persistentKeyUsedForBind);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Session::releaseCdnKeyCreationOnDone(
|
|
|
|
const AuthKeyPtr &temporaryKey) {
|
|
|
|
Expects(_myKeyCreation == CreatingKeyType::TemporaryRegular);
|
|
|
|
|
|
|
|
return releaseGenericKeyCreationOnDone(temporaryKey, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Session::releaseGenericKeyCreationOnDone(
|
|
|
|
const AuthKeyPtr &temporaryKey,
|
|
|
|
const AuthKeyPtr &persistentKeyUsedForBind) {
|
2019-11-21 10:37:39 +00:00
|
|
|
const auto wasKeyCreation = std::exchange(
|
|
|
|
_myKeyCreation,
|
|
|
|
CreatingKeyType::None);
|
|
|
|
const auto result = _dc->releaseKeyCreationOnDone(
|
|
|
|
wasKeyCreation,
|
|
|
|
temporaryKey,
|
|
|
|
persistentKeyUsedForBind);
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
DEBUG_LOG(("AuthKey Info: Persistent key changed "
|
|
|
|
"while binding temporary, dcWithShift %1"
|
|
|
|
).arg(_shiftedDcId));
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-18 12:53:37 +00:00
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
DEBUG_LOG(("AuthKey Info: Session key bound, setting, dcWithShift %1"
|
|
|
|
).arg(_shiftedDcId));
|
2019-11-18 12:53:37 +00:00
|
|
|
|
2019-11-20 13:33:45 +00:00
|
|
|
const auto dcId = _dc->id();
|
|
|
|
const auto instance = _instance;
|
|
|
|
InvokeQueued(instance, [=] {
|
2019-11-21 10:37:39 +00:00
|
|
|
if (wasKeyCreation == CreatingKeyType::Persistent) {
|
|
|
|
instance->dcPersistentKeyChanged(dcId, persistentKeyUsedForBind);
|
2019-11-20 13:33:45 +00:00
|
|
|
} else {
|
|
|
|
instance->dcTemporaryKeyChanged(dcId);
|
|
|
|
}
|
|
|
|
});
|
2019-11-21 10:37:39 +00:00
|
|
|
return true;
|
2014-11-14 23:23:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
void Session::releaseKeyCreationOnFail() {
|
2019-11-21 10:37:39 +00:00
|
|
|
Expects(_myKeyCreation != CreatingKeyType::None);
|
2019-11-19 10:10:51 +00:00
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
const auto wasKeyCreation = std::exchange(
|
|
|
|
_myKeyCreation,
|
|
|
|
CreatingKeyType::None);
|
|
|
|
_dc->releaseKeyCreationOnFail(wasKeyCreation);
|
2019-11-19 10:10:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void Session::notifyDcConnectionInited() {
|
2019-11-27 14:22:22 +00:00
|
|
|
DEBUG_LOG(("MTP Info: MTProtoDC::connectionWasInited(), dcWithShift %1"
|
|
|
|
).arg(_shiftedDcId));
|
2019-11-15 13:04:32 +00:00
|
|
|
_dc->setConnectionInited();
|
2014-11-14 23:23:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
void Session::destroyTemporaryKey(uint64 keyId) {
|
|
|
|
if (!_dc->destroyTemporaryKey(keyId)) {
|
2019-11-18 12:53:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-20 13:33:45 +00:00
|
|
|
const auto dcId = _dc->id();
|
|
|
|
const auto instance = _instance;
|
|
|
|
InvokeQueued(instance, [=] {
|
|
|
|
instance->dcTemporaryKeyChanged(dcId);
|
|
|
|
});
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
int32 Session::getDcWithShift() const {
|
2019-11-14 07:13:17 +00:00
|
|
|
return _shiftedDcId;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
AuthKeyPtr Session::getTemporaryKey(TemporaryKeyType type) const {
|
|
|
|
return _dc->getTemporaryKey(type);
|
2019-11-19 10:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuthKeyPtr Session::getPersistentKey() const {
|
|
|
|
return _dc->getPersistentKey();
|
2019-11-15 13:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Session::connectionInited() const {
|
|
|
|
return _dc->connectionInited();
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::tryToReceive() {
|
2016-02-29 16:53:26 +00:00
|
|
|
if (_killed) {
|
|
|
|
DEBUG_LOG(("Session Error: can't receive in a killed session"));
|
|
|
|
return;
|
|
|
|
}
|
2016-03-24 08:57:11 +00:00
|
|
|
if (paused()) {
|
2015-10-15 10:18:24 +00:00
|
|
|
_needToReceive = true;
|
|
|
|
return;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
while (true) {
|
2019-11-22 09:15:38 +00:00
|
|
|
auto lock = QWriteLocker(_data->haveReceivedMutex());
|
|
|
|
const auto responses = base::take(_data->haveReceivedResponses());
|
|
|
|
const auto updates = base::take(_data->haveReceivedUpdates());
|
|
|
|
lock.unlock();
|
|
|
|
if (responses.empty() && updates.empty()) {
|
|
|
|
break;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2019-11-22 09:15:38 +00:00
|
|
|
for (const auto &[requestId, response] : responses) {
|
|
|
|
_instance->execCallback(
|
|
|
|
requestId,
|
|
|
|
response.constData(),
|
|
|
|
response.constData() + response.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call globalCallback only in main session.
|
|
|
|
if (_shiftedDcId == BareDcId(_shiftedDcId)) {
|
|
|
|
for (const auto &update : updates) {
|
|
|
|
_instance->globalCallback(
|
|
|
|
update.constData(),
|
|
|
|
update.constData() + update.size());
|
2015-04-23 15:50:11 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:22:22 +00:00
|
|
|
void Session::killConnection() {
|
2019-12-02 13:10:19 +00:00
|
|
|
if (!_private) {
|
2019-11-27 14:22:22 +00:00
|
|
|
return;
|
2019-11-18 12:53:37 +00:00
|
|
|
}
|
2019-11-27 14:22:22 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
base::take(_private)->deleteLater();
|
2019-11-27 14:22:22 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
Ensures(_private == nullptr);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace details
|
2016-03-24 08:57:11 +00:00
|
|
|
} // namespace MTP
|