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"
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
namespace MTP {
|
|
|
|
namespace internal {
|
|
|
|
|
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());
|
|
|
|
mtpResponseMap::const_iterator end = haveReceived.cend();
|
|
|
|
clearCallbacks.reserve(haveSent.size() + wereAcked.size());
|
|
|
|
for (mtpRequestMap::const_iterator i = haveSent.cbegin(), e = haveSent.cend(); i != e; ++i) {
|
|
|
|
mtpRequestId requestId = i.value()->requestId;
|
|
|
|
if (haveReceived.find(requestId) == end) {
|
|
|
|
clearCallbacks.push_back(requestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (mtpRequestIdsMap::const_iterator i = toResend.cbegin(), e = toResend.cend(); i != e; ++i) {
|
|
|
|
mtpRequestId requestId = i.value();
|
|
|
|
if (haveReceived.find(requestId) == end) {
|
|
|
|
clearCallbacks.push_back(requestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (mtpRequestIdsMap::const_iterator i = wereAcked.cbegin(), e = wereAcked.cend(); i != e; ++i) {
|
|
|
|
mtpRequestId requestId = i.value();
|
|
|
|
if (haveReceived.find(requestId) == end) {
|
|
|
|
clearCallbacks.push_back(requestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QWriteLocker locker(haveSentMutex());
|
|
|
|
haveSent.clear();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QWriteLocker locker(toResendMutex());
|
|
|
|
toResend.clear();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QWriteLocker locker(wereAckedMutex());
|
|
|
|
wereAcked.clear();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QWriteLocker locker(receivedIdsMutex());
|
|
|
|
receivedIds.clear();
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
instance->clearCallbacksDelayed(clearCallbacks);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
Session::Session(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);
|
|
|
|
|
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
|
|
|
|
|
|
|
ReadLockerAttempt lock(keyMutex());
|
|
|
|
data.setKey(lock ? dc->getKey() : AuthKeyPtr());
|
|
|
|
if (lock && 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;
|
|
|
|
}
|
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-11-25 12:15:29 +00:00
|
|
|
int32 cnt = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
while (true) {
|
|
|
|
mtpRequestId requestId;
|
|
|
|
mtpResponse response;
|
|
|
|
{
|
|
|
|
QWriteLocker locker(data.haveReceivedMutex());
|
|
|
|
mtpResponseMap &responses(data.haveReceivedMap());
|
|
|
|
mtpResponseMap::iterator i = responses.begin();
|
|
|
|
if (i == responses.end()) return;
|
|
|
|
|
|
|
|
requestId = i.key();
|
|
|
|
response = i.value();
|
|
|
|
responses.erase(i);
|
|
|
|
}
|
|
|
|
if (requestId <= 0) {
|
2016-03-24 08:57:11 +00:00
|
|
|
if (dcWithShift == bareDcId(dcWithShift)) { // call globalCallback only in main session
|
2017-02-24 17:15:41 +00:00
|
|
|
_instance->globalCallback(response.constData(), response.constData() + response.size());
|
2015-04-23 15:50:11 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2017-02-24 17:15:41 +00:00
|
|
|
_instance->execCallback(requestId, response.constData(), response.constData() + response.size());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2014-11-25 12:15:29 +00:00
|
|
|
++cnt;
|
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
|