Skip to content

Commit

Permalink
MTP global state moved to MTP::Instance class.
Browse files Browse the repository at this point in the history
Now there will be ability to start multiple mtproto instances.
  • Loading branch information
john-preston committed Feb 25, 2017
1 parent c3b3819 commit dd933cf
Show file tree
Hide file tree
Showing 23 changed files with 1,869 additions and 1,342 deletions.
6 changes: 3 additions & 3 deletions Telegram/SourceFiles/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ namespace {
} // namespace

void logOut() {
if (MTP::started()) {
MTP::logoutKeys(rpcDone(&loggedOut), rpcFail(&loggedOut));
if (auto mtproto = Messenger::Instance().mtp()) {
mtproto->logout(rpcDone(&loggedOut), rpcFail(&loggedOut));
} else {
loggedOut();
MTP::start();
Messenger::Instance().startMtp();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void unixtimeSet(int32 serverTime, bool force) {
}

TimeId unixtime() {
TimeId result = myunixtime();
auto result = myunixtime();

QReadLocker locker(&unixtimeLock);
return result + unixtimeDelta;
Expand Down
47 changes: 46 additions & 1 deletion Telegram/SourceFiles/core/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "core/basic_types.h"
#include <array>
#include <algorithm>
#include <set>

namespace base {

Expand Down Expand Up @@ -89,6 +90,50 @@ inline bool contains(const Container &container, const T &value) {
return std::find(std::begin(container), end, value) != end;
}

// We need a custom comparator for std::set<std::unique_ptr<T>>::find to work with pointers.
// thanks to http://stackoverflow.com/questions/18939882/raw-pointer-lookup-for-sets-of-unique-ptrs
template <typename T>
struct pointer_comparator {
using is_transparent = std::true_type;

// helper does some magic in order to reduce the number of
// pairs of types we need to know how to compare: it turns
// everything into a pointer, and then uses `std::less<T*>`
// to do the comparison:
struct helper {
T *ptr = nullptr;
helper() = default;
helper(const helper &other) = default;
helper(T *p) : ptr(p) {
}
template <typename ...Ts>
helper(const std::shared_ptr<Ts...> &other) : ptr(other.get()) {
}
template <typename ...Ts>
helper(const std::unique_ptr<Ts...> &other) : ptr(other.get()) {
}
bool operator<(helper other) const {
return std::less<T*>()(ptr, other.ptr);
}
};

// without helper, we'd need 2^n different overloads, where
// n is the number of types we want to support (so, 8 with
// raw pointers, unique pointers, and shared pointers). That
// seems silly.
// && helps enforce rvalue use only
bool operator()(const helper &&lhs, const helper &&rhs) const {
return lhs < rhs;
}

};

template <typename T>
using set_of_unique_ptr = std::set<std::unique_ptr<T>, base::pointer_comparator<T>>;

template <typename T>
using set_of_shared_ptr = std::set<std::shared_ptr<T>, base::pointer_comparator<T>>;

} // namespace base

// using for_const instead of plain range-based for loop to ensure usage of const_iterator
Expand Down Expand Up @@ -162,7 +207,6 @@ inline void t_assert_fail(const char *message, const char *file, int32 line) {

class Exception : public std::exception {
public:

Exception(const QString &msg, bool isFatal = true) : _fatal(isFatal), _msg(msg.toUtf8()) {
LOG(("Exception: %1").arg(msg));
}
Expand All @@ -179,6 +223,7 @@ class Exception : public std::exception {
private:
bool _fatal;
QByteArray _msg;

};

class MTPint;
Expand Down
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/intro/introwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ bool Widget::resetFail(const RPCError &error) {
void Widget::gotNearestDC(const MTPNearestDc &result) {
auto &nearest = result.c_nearestDc();
DEBUG_LOG(("Got nearest dc, country: %1, nearest: %2, this: %3").arg(nearest.vcountry.c_string().v.c_str()).arg(nearest.vnearest_dc.v).arg(nearest.vthis_dc.v));
MTP::setdc(nearest.vnearest_dc.v, true);
Messenger::Instance().mtp()->suggestMainDcId(nearest.vnearest_dc.v);
auto nearestCountry = qs(nearest.vcountry);
if (getData()->country != nearestCountry) {
getData()->country = nearestCountry;
Expand Down
36 changes: 19 additions & 17 deletions Telegram/SourceFiles/localstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ enum { // Local Storage Keys
enum {
dbiKey = 0x00,
dbiUser = 0x01,
dbiDcOptionOld = 0x02,
dbiDcOptionOldOld = 0x02,
dbiChatSizeMax = 0x03,
dbiMutePeer = 0x04,
dbiSendKey = 0x05,
Expand Down Expand Up @@ -541,7 +541,7 @@ enum {
dbiRecentEmojiOld = 0x24,
dbiEmojiVariantsOld = 0x25,
dbiRecentStickers = 0x26,
dbiDcOption = 0x27,
dbiDcOptionOld = 0x27,
dbiTryIPv6 = 0x28,
dbiSongVolume = 0x29,
dbiWindowsNotificationsOld = 0x30,
Expand All @@ -567,6 +567,7 @@ enum {
dbiDialogsWidthRatio = 0x48,
dbiUseExternalVideoPlayer = 0x49,
dbiDcOptions = 0x4a,
dbiMtpAuthorization = 0x4b,

dbiEncryptedWithSalt = 333,
dbiEncrypted = 444,
Expand Down Expand Up @@ -843,7 +844,7 @@ void applyReadContext(const ReadSettingsContext &context) {

bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSettingsContext &context) {
switch (blockId) {
case dbiDcOptionOld: {
case dbiDcOptionOldOld: {
quint32 dcId, port;
QString host, ip;
stream >> dcId >> host >> ip >> port;
Expand All @@ -852,7 +853,7 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting
context.dcOptions.constructAddOne(dcId, 0, ip.toStdString(), port);
} break;

case dbiDcOption: {
case dbiDcOptionOld: {
quint32 dcIdWithShift, port;
qint32 flags;
QString ip;
Expand All @@ -863,7 +864,7 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting
} break;

case dbiDcOptions: {
QByteArray serialized;
auto serialized = QByteArray();
stream >> serialized;
if (!_checkStreamStatus(stream)) return false;

Expand Down Expand Up @@ -909,8 +910,7 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting
if (!_checkStreamStatus(stream)) return false;

DEBUG_LOG(("MTP Info: user found, dc %1, uid %2").arg(dcId).arg(userId));
MTP::configure(dcId);

Messenger::Instance().setMtpMainDcId(dcId);
if (userId) {
Messenger::Instance().authSessionCreate(UserId(userId));
}
Expand All @@ -923,7 +923,15 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting
stream.readRawData(key.data(), key.size());
if (!_checkStreamStatus(stream)) return false;

MTP::setKey(dcId, key);
Messenger::Instance().setMtpKey(dcId, key);
} break;

case dbiMtpAuthorization: {
auto serialized = QByteArray();
stream >> serialized;
if (!_checkStreamStatus(stream)) return false;

Messenger::Instance().setMtpAuthorization(serialized);
} break;

case dbiAutoStart: {
Expand Down Expand Up @@ -1777,18 +1785,12 @@ void _writeMtpData() {
return;
}

auto keys = MTP::getKeys();
auto mtpAuthorizationSerialized = Messenger::Instance().serializeMtpAuthorization();

quint32 size = sizeof(quint32) + sizeof(qint32) + sizeof(quint32);
size += keys.size() * (sizeof(quint32) + sizeof(quint32) + MTP::AuthKey::kSize);
quint32 size = sizeof(quint32) + Serialize::bytearraySize(mtpAuthorizationSerialized);

EncryptedDescriptor data(size);
data.stream << quint32(dbiUser) << qint32(AuthSession::CurrentUserId()) << quint32(MTP::maindc());
for_const (auto &key, keys) {
data.stream << quint32(dbiKey) << quint32(key->getDC());
key->write(data.stream);
}

data.stream << quint32(dbiMtpAuthorization) << mtpAuthorizationSerialized;
mtp.writeEncrypted(data);
}

Expand Down
6 changes: 3 additions & 3 deletions Telegram/SourceFiles/logs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include <new>

#include "pspecific.h"
#include "mtproto/connection.h"

#ifndef TDESKTOP_DISABLE_CRASH_REPORTS

Expand Down Expand Up @@ -87,9 +88,8 @@ QString _logsEntryStart() {
static int32 index = 0;
QDateTime tm(QDateTime::currentDateTime());

QThread *thread = QThread::currentThread();
MTP::internal::Thread *mtpThread = qobject_cast<MTP::internal::Thread*>(thread);
uint threadId = mtpThread ? mtpThread->getThreadId() : 0;
auto thread = qobject_cast<MTP::internal::Thread*>(QThread::currentThread());
auto threadId = thread ? thread->getThreadIndex() : 0;

return QString("[%1 %2-%3]").arg(tm.toString("hh:mm:ss.zzz")).arg(QString("%1").arg(threadId, 2, 10, QChar('0'))).arg(++index, 7, 10, QChar('0'));
}
Expand Down
8 changes: 4 additions & 4 deletions Telegram/SourceFiles/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ MainWidget::MainWidget(QWidget *parent) : TWidget(parent)
, _playerPanel(this, Media::Player::Panel::Layout::Full)
, _mediaType(this, st::defaultDropdownMenu)
, _api(new ApiWrap(this)) {
MTP::setGlobalDoneHandler(rpcDone(&MainWidget::updateReceived));
Messenger::Instance().mtp()->setUpdatesHandler(rpcDone(&MainWidget::updateReceived));
Messenger::Instance().mtp()->setGlobalFailHandler(rpcFail(&MainWidget::updateFail));

_ptsWaiter.setRequesting(true);
updateScrollColors();

Expand Down Expand Up @@ -174,8 +176,6 @@ MainWidget::MainWidget(QWidget *parent) : TWidget(parent)

orderWidgets();

MTP::setGlobalFailHandler(rpcFail(&MainWidget::updateFail));

_mediaType->hide();
_mediaType->setOrigin(Ui::PanelAnimation::Origin::TopRight);
_topBar->mediaTypeButton()->installEventFilter(_mediaType);
Expand Down Expand Up @@ -4279,7 +4279,7 @@ MainWidget::~MainWidget() {
_hider = nullptr;
delete hider;
}
MTP::clearGlobalHandlers();
Messenger::Instance().mtp()->clearGlobalHandlers();

if (App::wnd()) App::wnd()->noMain(this);
}
Expand Down
Loading

0 comments on commit dd933cf

Please sign in to comment.