2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
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
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
#include "mtproto/connection.h"
|
2017-06-26 17:38:16 +00:00
|
|
|
#include "mtproto/dcenter.h"
|
|
|
|
#include "mtproto/auth_key.h"
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
namespace MTP {
|
|
|
|
namespace internal {
|
|
|
|
|
2017-06-26 17:38:16 +00:00
|
|
|
void SessionData::setKey(const AuthKeyPtr &key) {
|
|
|
|
if (_authKey != key) {
|
|
|
|
uint64 session = rand_value<uint64>();
|
|
|
|
_authKey = key;
|
|
|
|
|
|
|
|
DEBUG_LOG(("MTP Info: new auth key set in SessionData, id %1, setting random server_session %2").arg(key ? key->keyId() : 0).arg(session));
|
|
|
|
QWriteLocker locker(&_lock);
|
|
|
|
if (_session != session) {
|
|
|
|
_session = session;
|
|
|
|
_messagesSent = 0;
|
|
|
|
}
|
|
|
|
_layerInited = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void SessionData::clear(Instance *instance) {
|
2014-05-30 08:53:19 +00:00
|
|
|
RPCCallbackClears clearCallbacks;
|
|
|
|
{
|
|
|
|
QReadLocker locker1(haveSentMutex()), locker2(toResendMutex()), locker3(haveReceivedMutex()), locker4(wereAckedMutex());
|
2017-04-30 14:23:57 +00:00
|
|
|
auto receivedResponsesEnd = _receivedResponses.cend();
|
|
|
|
clearCallbacks.reserve(_haveSent.size() + _wereAcked.size());
|
|
|
|
for (auto i = _haveSent.cbegin(), e = _haveSent.cend(); i != e; ++i) {
|
|
|
|
auto requestId = i.value()->requestId;
|
|
|
|
if (!_receivedResponses.contains(requestId)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
clearCallbacks.push_back(requestId);
|
|
|
|
}
|
|
|
|
}
|
2017-04-30 14:23:57 +00:00
|
|
|
for (auto i = _toResend.cbegin(), e = _toResend.cend(); i != e; ++i) {
|
|
|
|
auto requestId = i.value();
|
|
|
|
if (!_receivedResponses.contains(requestId)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
clearCallbacks.push_back(requestId);
|
|
|
|
}
|
|
|
|
}
|
2017-04-30 14:23:57 +00:00
|
|
|
for (auto i = _wereAcked.cbegin(), e = _wereAcked.cend(); i != e; ++i) {
|
|
|
|
auto requestId = i.value();
|
|
|
|
if (!_receivedResponses.contains(requestId)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
clearCallbacks.push_back(requestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QWriteLocker locker(haveSentMutex());
|
2017-04-30 14:23:57 +00:00
|
|
|
_haveSent.clear();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
QWriteLocker locker(toResendMutex());
|
2017-04-30 14:23:57 +00:00
|
|
|
_toResend.clear();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
QWriteLocker locker(wereAckedMutex());
|
2017-04-30 14:23:57 +00:00
|
|
|
_wereAcked.clear();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
QWriteLocker locker(receivedIdsMutex());
|
2017-04-30 14:23:57 +00:00
|
|
|
_receivedIds.clear();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
instance->clearCallbacksDelayed(clearCallbacks);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 15:38:40 +00:00
|
|
|
Session::Session(gsl::not_null<Instance*> instance, ShiftedDcId shiftedDcId) : QObject()
|
2017-02-24 17:15:41 +00:00
|
|
|
, _instance(instance)
|
2017-02-25 16:44:02 +00:00
|
|
|
, data(this)
|
|
|
|
, dcWithShift(shiftedDcId) {
|
2014-05-30 08:53:19 +00:00
|
|
|
connect(&timeouter, SIGNAL(timeout()), this, SLOT(checkRequestsByTimer()));
|
|
|
|
timeouter.start(1000);
|
|
|
|
|
2017-05-30 09:31:32 +00:00
|
|
|
data.setSystemLangCode(instance->systemLangCode());
|
|
|
|
data.setCloudLangCode(instance->cloudLangCode());
|
2017-04-13 17:59:05 +00:00
|
|
|
|
2014-12-05 13:44:27 +00:00
|
|
|
connect(&sender, SIGNAL(timeout()), this, SLOT(needToResumeAndSend()));
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
void Session::start() {
|
|
|
|
createDcData();
|
2017-02-24 17:15:41 +00:00
|
|
|
_connection = std::make_unique<Connection>(_instance);
|
2017-02-25 16:44:02 +00:00
|
|
|
_connection->start(&data, dcWithShift);
|
|
|
|
if (_instance->isKeysDestroyer()) {
|
|
|
|
_instance->scheduleKeyDestroy(dcWithShift);
|
2016-02-29 16:53:26 +00:00
|
|
|
}
|
2016-10-24 15:36:17 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-24 15:36:17 +00:00
|
|
|
void Session::createDcData() {
|
|
|
|
if (dc) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-25 16:44:02 +00:00
|
|
|
dc = _instance->getDcById(dcWithShift);
|
2016-10-24 15:36:17 +00:00
|
|
|
|
2017-06-26 17:38:16 +00:00
|
|
|
if (auto lock = ReadLockerAttempt(keyMutex())) {
|
|
|
|
data.setKey(dc->getKey());
|
|
|
|
if (dc->connectionInited()) {
|
|
|
|
data.setLayerWasInited(true);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
connect(dc.get(), SIGNAL(authKeyCreated()), this, SLOT(authKeyCreatedForDC()), Qt::QueuedConnection);
|
|
|
|
connect(dc.get(), SIGNAL(layerWasInited(bool)), this, SLOT(layerWasInitedForDC(bool)), Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::registerRequest(mtpRequestId requestId, ShiftedDcId dcWithShift) {
|
|
|
|
return _instance->registerRequest(requestId, dcWithShift);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpRequestId Session::storeRequest(mtpRequest &request, const RPCResponseHandler &parser) {
|
|
|
|
return _instance->storeRequest(request, parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpRequest Session::getRequest(mtpRequestId requestId) {
|
|
|
|
return _instance->getRequest(requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Session::rpcErrorOccured(mtpRequestId requestId, const RPCFailHandlerPtr &onFail, const RPCError &err) { // return true if need to clean request data
|
|
|
|
return _instance->rpcErrorOccured(requestId, onFail, err);
|
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;
|
|
|
|
}
|
2017-05-30 09:31:32 +00:00
|
|
|
data.setSystemLangCode(_instance->systemLangCode());
|
|
|
|
data.setCloudLangCode(_instance->cloudLangCode());
|
2014-11-24 13:21:27 +00:00
|
|
|
emit needToRestart();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::stop() {
|
2016-02-29 16:53:26 +00:00
|
|
|
if (_killed) {
|
|
|
|
DEBUG_LOG(("Session Error: can't kill a killed session"));
|
|
|
|
return;
|
|
|
|
}
|
2015-05-14 16:50:04 +00:00
|
|
|
DEBUG_LOG(("Session Info: stopping session dcWithShift %1").arg(dcWithShift));
|
2016-02-29 16:53:26 +00:00
|
|
|
if (_connection) {
|
|
|
|
_connection->kill();
|
2017-02-24 17:15:41 +00:00
|
|
|
_instance->queueQuittingConnection(std::move(_connection));
|
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;
|
|
|
|
DEBUG_LOG(("Session Info: marked session dcWithShift %1 as killed").arg(dcWithShift));
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::unpaused() {
|
2015-10-15 10:18:24 +00:00
|
|
|
if (_needToReceive) {
|
|
|
|
_needToReceive = false;
|
2015-11-04 17:55:00 +00:00
|
|
|
QTimer::singleShot(0, this, SLOT(tryToReceive()));
|
2015-10-15 10:18:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
void Session::sendAnything(qint64 msCanWait) {
|
2015-05-14 16:50:04 +00:00
|
|
|
if (_killed) {
|
|
|
|
DEBUG_LOG(("Session Error: can't send anything in a killed session"));
|
|
|
|
return;
|
|
|
|
}
|
2016-12-01 19:20:33 +00:00
|
|
|
auto ms = getms(true);
|
2014-11-05 17:43:32 +00:00
|
|
|
if (msSendCall) {
|
|
|
|
if (ms > msSendCall + msWait) {
|
|
|
|
msWait = 0;
|
|
|
|
} else {
|
|
|
|
msWait = (msSendCall + msWait) - ms;
|
|
|
|
if (msWait > msCanWait) {
|
|
|
|
msWait = msCanWait;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msWait = msCanWait;
|
|
|
|
}
|
|
|
|
if (msWait) {
|
2015-05-14 16:50:04 +00:00
|
|
|
DEBUG_LOG(("MTP Info: dcWithShift %1 can wait for %2ms from current %3").arg(dcWithShift).arg(msWait).arg(msSendCall));
|
2014-11-05 17:43:32 +00:00
|
|
|
msSendCall = ms;
|
2014-11-22 09:45:04 +00:00
|
|
|
sender.start(msWait);
|
2014-11-05 17:43:32 +00:00
|
|
|
} else {
|
2015-05-14 16:50:04 +00:00
|
|
|
DEBUG_LOG(("MTP Info: dcWithShift %1 stopped send timer, can wait for %2ms from current %3").arg(dcWithShift).arg(msWait).arg(msSendCall));
|
2014-11-22 09:45:04 +00:00
|
|
|
sender.stop();
|
2014-11-05 17:43:32 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-02-29 16:53:26 +00:00
|
|
|
if (!_connection) {
|
2015-05-14 16:50:04 +00:00
|
|
|
DEBUG_LOG(("Session Info: resuming session dcWithShift %1").arg(dcWithShift));
|
2016-10-24 15:36:17 +00:00
|
|
|
createDcData();
|
2017-02-25 16:44:02 +00:00
|
|
|
_connection = std::make_unique<Connection>(_instance);
|
|
|
|
_connection->start(&data, dcWithShift);
|
2014-12-05 13:44:27 +00:00
|
|
|
}
|
2015-04-16 14:59:42 +00:00
|
|
|
if (_ping) {
|
|
|
|
_ping = false;
|
|
|
|
emit needToPing();
|
|
|
|
} else {
|
|
|
|
emit needToSend();
|
|
|
|
}
|
2014-12-05 13:44:27 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::sendPong(quint64 msgId, quint64 pingId) {
|
2014-11-25 12:15:29 +00:00
|
|
|
send(MTP_pong(MTP_long(msgId), MTP_long(pingId)));
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::sendMsgsStateInfo(quint64 msgId, QByteArray data) {
|
2017-03-09 18:13:55 +00:00
|
|
|
auto info = std::string();
|
2014-11-25 12:15:29 +00:00
|
|
|
if (!data.isEmpty()) {
|
2017-03-09 18:13:55 +00:00
|
|
|
info.resize(data.size());
|
|
|
|
auto src = gsl::as_bytes(gsl::make_span(data));
|
|
|
|
// auto dst = gsl::as_writeable_bytes(gsl::make_span(info));
|
|
|
|
auto dst = gsl::as_writeable_bytes(gsl::make_span(&info[0], info.size()));
|
|
|
|
base::copy_bytes(dst, src);
|
2014-11-25 12:15:29 +00:00
|
|
|
}
|
2017-03-09 18:13:55 +00:00
|
|
|
send(MTPMsgsStateInfo(MTP_msgs_state_info(MTP_long(msgId), MTP_string(std::move(info)))));
|
2014-11-25 12:15:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::checkRequestsByTimer() {
|
2014-05-30 08:53:19 +00:00
|
|
|
QVector<mtpMsgId> resendingIds;
|
|
|
|
QVector<mtpMsgId> removingIds; // remove very old (10 minutes) containers and resend requests
|
2014-11-05 17:43:32 +00:00
|
|
|
QVector<mtpMsgId> stateRequestIds;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
QReadLocker locker(data.haveSentMutex());
|
|
|
|
mtpRequestMap &haveSent(data.haveSentMap());
|
|
|
|
uint32 haveSentCount(haveSent.size());
|
2016-12-01 19:20:33 +00:00
|
|
|
auto ms = getms(true);
|
2014-05-30 08:53:19 +00:00
|
|
|
for (mtpRequestMap::iterator i = haveSent.begin(), e = haveSent.end(); i != e; ++i) {
|
|
|
|
mtpRequest &req(i.value());
|
|
|
|
if (req->msDate > 0) {
|
|
|
|
if (req->msDate + MTPCheckResendTimeout < ms) { // need to resend or check state
|
|
|
|
if (mtpRequestData::messageSize(req) < MTPResendThreshold) { // resend
|
|
|
|
resendingIds.reserve(haveSentCount);
|
|
|
|
resendingIds.push_back(i.key());
|
|
|
|
} else {
|
|
|
|
req->msDate = ms;
|
|
|
|
stateRequestIds.reserve(haveSentCount);
|
2014-11-05 17:43:32 +00:00
|
|
|
stateRequestIds.push_back(i.key());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-16 09:31:10 +00:00
|
|
|
} else if (unixtime() > (int32)(i.key() >> 32) + MTPContainerLives) {
|
2014-05-30 08:53:19 +00:00
|
|
|
removingIds.reserve(haveSentCount);
|
|
|
|
removingIds.push_back(i.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stateRequestIds.size()) {
|
2016-01-11 15:43:29 +00:00
|
|
|
DEBUG_LOG(("MTP Info: requesting state of msgs: %1").arg(Logs::vector(stateRequestIds)));
|
2014-11-05 17:43:32 +00:00
|
|
|
{
|
|
|
|
QWriteLocker locker(data.stateRequestMutex());
|
|
|
|
for (uint32 i = 0, l = stateRequestIds.size(); i < l; ++i) {
|
|
|
|
data.stateRequestMap().insert(stateRequestIds[i], true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sendAnything(MTPCheckResendWaiting);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2014-11-05 17:43:32 +00:00
|
|
|
if (!resendingIds.isEmpty()) {
|
|
|
|
for (uint32 i = 0, l = resendingIds.size(); i < l; ++i) {
|
|
|
|
DEBUG_LOG(("MTP Info: resending request %1").arg(resendingIds[i]));
|
|
|
|
resend(resendingIds[i], MTPCheckResendWaiting);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2014-11-05 17:43:32 +00:00
|
|
|
if (!removingIds.isEmpty()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
RPCCallbackClears clearCallbacks;
|
|
|
|
{
|
|
|
|
QWriteLocker locker(data.haveSentMutex());
|
|
|
|
mtpRequestMap &haveSent(data.haveSentMap());
|
2014-11-05 17:43:32 +00:00
|
|
|
for (uint32 i = 0, l = removingIds.size(); i < l; ++i) {
|
2014-05-30 08:53:19 +00:00
|
|
|
mtpRequestMap::iterator j = haveSent.find(removingIds[i]);
|
|
|
|
if (j != haveSent.cend()) {
|
|
|
|
if (j.value()->requestId) {
|
|
|
|
clearCallbacks.push_back(j.value()->requestId);
|
|
|
|
}
|
|
|
|
haveSent.erase(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
_instance->clearCallbacksDelayed(clearCallbacks);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::onConnectionStateChange(qint32 newState) {
|
2017-02-24 17:15:41 +00:00
|
|
|
_instance->onStateChange(dcWithShift, newState);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::onResetDone() {
|
2017-02-24 17:15:41 +00:00
|
|
|
_instance->onSessionReset(dcWithShift);
|
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) {
|
|
|
|
QWriteLocker locker(data.toSendMutex());
|
|
|
|
data.toSendMap().remove(requestId);
|
|
|
|
}
|
|
|
|
if (msgId) {
|
|
|
|
QWriteLocker locker(data.haveSentMutex());
|
|
|
|
data.haveSentMap().remove(msgId);
|
|
|
|
}
|
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;
|
|
|
|
sendAnything(0);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (_connection) {
|
|
|
|
int32 s = _connection->state();
|
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;
|
|
|
|
}
|
|
|
|
if (!requestId) return MTP::RequestSent;
|
|
|
|
|
|
|
|
QWriteLocker locker(data.toSendMutex());
|
|
|
|
const mtpPreRequestMap &toSend(data.toSendMap());
|
|
|
|
mtpPreRequestMap::const_iterator i = toSend.constFind(requestId);
|
|
|
|
if (i != toSend.cend()) {
|
|
|
|
return MTP::RequestSending;
|
|
|
|
} else {
|
|
|
|
return MTP::RequestSent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if (_connection) {
|
|
|
|
int32 s = _connection->state();
|
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 {
|
2016-02-29 16:53:26 +00:00
|
|
|
return _connection ? _connection->transport() : QString();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
mtpRequestId Session::resend(quint64 msgId, qint64 msCanWait, bool forceContainer, bool sendMsgStateInfo) {
|
2014-05-30 08:53:19 +00:00
|
|
|
mtpRequest request;
|
|
|
|
{
|
|
|
|
QWriteLocker locker(data.haveSentMutex());
|
|
|
|
mtpRequestMap &haveSent(data.haveSentMap());
|
|
|
|
|
|
|
|
mtpRequestMap::iterator i = haveSent.find(msgId);
|
|
|
|
if (i == haveSent.end()) {
|
|
|
|
if (sendMsgStateInfo) {
|
|
|
|
char cantResend[2] = {1, 0};
|
|
|
|
DEBUG_LOG(("Message Info: cant resend %1, request not found").arg(msgId));
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2016-09-29 11:37:16 +00:00
|
|
|
return send(MTP_msgs_state_info(MTP_long(msgId), MTP_string(std::string(cantResend, cantResend + 1))));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
request = i.value();
|
|
|
|
haveSent.erase(i);
|
|
|
|
}
|
|
|
|
if (mtpRequestData::isSentContainer(request)) { // for container just resend all messages we can
|
|
|
|
DEBUG_LOG(("Message Info: resending container from haveSent, msgId %1").arg(msgId));
|
|
|
|
const mtpMsgId *ids = (const mtpMsgId *)(request->constData() + 8);
|
|
|
|
for (uint32 i = 0, l = (request->size() - 8) >> 1; i < l; ++i) {
|
|
|
|
resend(ids[i], 10, true);
|
|
|
|
}
|
|
|
|
return 0xFFFFFFFF;
|
|
|
|
} else if (!mtpRequestData::isStateRequest(request)) {
|
2014-11-12 20:30:26 +00:00
|
|
|
request->msDate = forceContainer ? 0 : getms(true);
|
2014-05-30 08:53:19 +00:00
|
|
|
sendPrepared(request, msCanWait, false);
|
|
|
|
{
|
|
|
|
QWriteLocker locker(data.toResendMutex());
|
|
|
|
data.toResendMap().insert(msgId, request->requestId);
|
|
|
|
}
|
|
|
|
return request->requestId;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
void Session::resendMany(QVector<quint64> msgIds, qint64 msCanWait, bool forceContainer, bool sendMsgStateInfo) {
|
2014-11-25 12:15:29 +00:00
|
|
|
for (int32 i = 0, l = msgIds.size(); i < l; ++i) {
|
|
|
|
resend(msgIds.at(i), msCanWait, forceContainer, sendMsgStateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::resendAll() {
|
2014-05-30 08:53:19 +00:00
|
|
|
QVector<mtpMsgId> toResend;
|
|
|
|
{
|
|
|
|
QReadLocker locker(data.haveSentMutex());
|
|
|
|
const mtpRequestMap &haveSent(data.haveSentMap());
|
|
|
|
toResend.reserve(haveSent.size());
|
|
|
|
for (mtpRequestMap::const_iterator i = haveSent.cbegin(), e = haveSent.cend(); i != e; ++i) {
|
|
|
|
if (i.value()->requestId) toResend.push_back(i.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (uint32 i = 0, l = toResend.size(); i < l; ++i) {
|
|
|
|
resend(toResend[i], 10, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
void Session::sendPrepared(const mtpRequest &request, TimeMs msCanWait, bool newRequest) { // returns true, if emit of needToSend() is needed
|
2014-05-30 08:53:19 +00:00
|
|
|
{
|
|
|
|
QWriteLocker locker(data.toSendMutex());
|
|
|
|
data.toSendMap().insert(request->requestId, request);
|
|
|
|
|
|
|
|
if (newRequest) {
|
|
|
|
*(mtpMsgId*)(request->data() + 4) = 0;
|
|
|
|
*(request->data() + 6) = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_LOG(("MTP Info: added, requestId %1").arg(request->requestId));
|
|
|
|
|
2014-11-05 17:43:32 +00:00
|
|
|
sendAnything(msCanWait);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
QReadWriteLock *Session::keyMutex() const {
|
2014-05-30 08:53:19 +00:00
|
|
|
return dc->keyMutex();
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::authKeyCreatedForDC() {
|
2016-03-25 18:30:19 +00:00
|
|
|
DEBUG_LOG(("AuthKey Info: Session::authKeyCreatedForDC slot, emitting authKeyCreated(), dcWithShift %1").arg(dcWithShift));
|
2014-05-30 08:53:19 +00:00
|
|
|
data.setKey(dc->getKey());
|
|
|
|
emit authKeyCreated();
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void Session::notifyKeyCreated(AuthKeyPtr &&key) {
|
2016-03-25 18:30:19 +00:00
|
|
|
DEBUG_LOG(("AuthKey Info: Session::keyCreated(), setting, dcWithShift %1").arg(dcWithShift));
|
2017-02-24 17:15:41 +00:00
|
|
|
dc->setKey(std::move(key));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::layerWasInitedForDC(bool wasInited) {
|
2016-03-25 18:30:19 +00:00
|
|
|
DEBUG_LOG(("MTP Info: Session::layerWasInitedForDC slot, dcWithShift %1").arg(dcWithShift));
|
2014-11-14 23:23:35 +00:00
|
|
|
data.setLayerWasInited(wasInited);
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::notifyLayerInited(bool wasInited) {
|
2016-01-11 15:43:29 +00:00
|
|
|
DEBUG_LOG(("MTP Info: emitting MTProtoDC::layerWasInited(%1), dcWithShift %2").arg(Logs::b(wasInited)).arg(dcWithShift));
|
2014-11-14 23:23:35 +00:00
|
|
|
dc->setConnectionInited(wasInited);
|
|
|
|
emit dc->layerWasInited(wasInited);
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
void Session::destroyKey() {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!dc) return;
|
|
|
|
|
|
|
|
if (data.getKey()) {
|
2015-05-14 16:50:04 +00:00
|
|
|
DEBUG_LOG(("MTP Info: destroying auth_key for dcWithShift %1").arg(dcWithShift));
|
2014-05-30 08:53:19 +00:00
|
|
|
if (data.getKey() == dc->getKey()) {
|
|
|
|
dc->destroyKey();
|
|
|
|
}
|
2016-03-24 12:57:10 +00:00
|
|
|
data.setKey(AuthKeyPtr());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
int32 Session::getDcWithShift() const {
|
2015-05-14 16:50:04 +00:00
|
|
|
return dcWithShift;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2017-04-30 14:23:57 +00:00
|
|
|
auto requestId = mtpRequestId(0);
|
|
|
|
auto isUpdate = false;
|
|
|
|
auto message = SerializedMessage();
|
2014-05-30 08:53:19 +00:00
|
|
|
{
|
|
|
|
QWriteLocker locker(data.haveReceivedMutex());
|
2017-04-30 14:23:57 +00:00
|
|
|
auto &responses = data.haveReceivedResponses();
|
|
|
|
auto response = responses.begin();
|
|
|
|
if (response == responses.cend()) {
|
|
|
|
auto &updates = data.haveReceivedUpdates();
|
|
|
|
auto update = updates.begin();
|
|
|
|
if (update == updates.cend()) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
message = std::move(*update);
|
|
|
|
isUpdate = true;
|
|
|
|
updates.pop_front();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
requestId = response.key();
|
|
|
|
message = std::move(response.value());
|
|
|
|
responses.erase(response);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-04-30 14:23:57 +00:00
|
|
|
if (isUpdate) {
|
2016-03-24 08:57:11 +00:00
|
|
|
if (dcWithShift == bareDcId(dcWithShift)) { // call globalCallback only in main session
|
2017-04-30 14:23:57 +00:00
|
|
|
_instance->globalCallback(message.constData(), message.constData() + message.size());
|
2015-04-23 15:50:11 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2017-04-30 14:23:57 +00:00
|
|
|
_instance->execCallback(requestId, message.constData(), message.constData() + message.size());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
Session::~Session() {
|
2017-02-24 17:15:41 +00:00
|
|
|
t_assert(_connection == nullptr);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MTPrpcError rpcClientError(const QString &type, const QString &description) {
|
|
|
|
return MTP_rpc_error(MTP_int(0), MTP_string(("CLIENT_" + type + (description.length() ? (": " + description) : "")).toUtf8().constData()));
|
|
|
|
}
|
2016-03-24 08:57:11 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace MTP
|