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.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2014-12-01 10:47:38 +00:00
|
|
|
Copyright (c) 2014 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "mtproto/mtpSession.h"
|
|
|
|
#include "mtproto/mtpFileLoader.h"
|
|
|
|
|
|
|
|
namespace _mtp_internal {
|
|
|
|
MTProtoSessionPtr getSession(int32 dc = 0); // 0 - current set dc
|
|
|
|
|
|
|
|
void registerRequest(mtpRequestId requestId, int32 dc);
|
|
|
|
void unregisterRequest(mtpRequestId requestId);
|
|
|
|
|
|
|
|
static const uint32 dcShift = 10000;
|
|
|
|
|
|
|
|
mtpRequestId storeRequest(mtpRequest &request, const RPCResponseHandler &parser);
|
2014-11-05 17:43:32 +00:00
|
|
|
mtpRequest getRequest(mtpRequestId req);
|
2014-11-14 23:23:35 +00:00
|
|
|
void wrapInvokeAfter(mtpRequest &to, const mtpRequest &from, const mtpRequestMap &haveSent, int32 skipBeforeRequest = 0);
|
2014-05-30 08:53:19 +00:00
|
|
|
void clearCallbacks(mtpRequestId requestId, int32 errorCode = RPCError::NoError); // 0 - do not toggle onError callback
|
|
|
|
void clearCallbacksDelayed(const RPCCallbackClears &requestIds);
|
|
|
|
void performDelayedClear();
|
|
|
|
void execCallback(mtpRequestId requestId, const mtpPrime *from, const mtpPrime *end);
|
2014-10-30 16:23:44 +00:00
|
|
|
bool hasCallbacks(mtpRequestId requestId);
|
2014-05-30 08:53:19 +00:00
|
|
|
void globalCallback(const mtpPrime *from, const mtpPrime *end);
|
|
|
|
void onStateChange(int32 dc, int32 state);
|
2014-08-01 18:49:43 +00:00
|
|
|
void onSessionReset(int32 dc);
|
2014-05-30 08:53:19 +00:00
|
|
|
bool rpcErrorOccured(mtpRequestId requestId, const RPCFailHandlerPtr &onFail, const RPCError &err); // return true if need to clean request data
|
|
|
|
inline bool rpcErrorOccured(mtpRequestId requestId, const RPCResponseHandler &handler, const RPCError &err) {
|
|
|
|
return rpcErrorOccured(requestId, handler.onFail, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RequestResender : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
2014-11-12 20:30:26 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
RequestResender();
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
public slots:
|
|
|
|
|
|
|
|
void checkDelayed();
|
2014-11-12 20:30:26 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
SingleTimer _timer;
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace MTP {
|
|
|
|
|
2014-10-30 16:23:44 +00:00
|
|
|
static const uint32 cfg = 1 * _mtp_internal::dcShift; // send(MTPhelp_GetConfig(), MTP::cfg + dc) - for dc enum
|
|
|
|
static const uint32 dld[MTPDownloadSessionsCount] = { // send(req, callbacks, MTP::dld[i] + dc) - for download
|
|
|
|
0x10 * _mtp_internal::dcShift,
|
|
|
|
0x11 * _mtp_internal::dcShift,
|
|
|
|
0x12 * _mtp_internal::dcShift,
|
|
|
|
0x13 * _mtp_internal::dcShift,
|
|
|
|
};
|
|
|
|
static const uint32 upl[MTPUploadSessionsCount] = { // send(req, callbacks, MTP::upl[i] + dc) - for upload
|
|
|
|
0x20 * _mtp_internal::dcShift,
|
|
|
|
0x21 * _mtp_internal::dcShift,
|
|
|
|
0x22 * _mtp_internal::dcShift,
|
|
|
|
0x23 * _mtp_internal::dcShift,
|
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
void start();
|
2015-03-02 12:34:16 +00:00
|
|
|
bool started();
|
2014-05-30 08:53:19 +00:00
|
|
|
void restart();
|
2014-08-01 18:49:43 +00:00
|
|
|
void restart(int32 dcMask);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-03-02 12:34:16 +00:00
|
|
|
void configure(int32 dc, int32 user);
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void setdc(int32 dc, bool fromZeroOnly = false);
|
|
|
|
int32 maindc();
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
int32 dcstate(int32 dc = 0);
|
|
|
|
QString dctransport(int32 dc = 0);
|
|
|
|
void initdc(int32 dc);
|
|
|
|
template <typename TRequest>
|
2014-11-05 17:43:32 +00:00
|
|
|
inline mtpRequestId send(const TRequest &request, RPCResponseHandler callbacks = RPCResponseHandler(), int32 dc = 0, uint64 msCanWait = 0, mtpRequestId after = 0) {
|
2014-11-13 18:26:17 +00:00
|
|
|
MTProtoSessionPtr session = _mtp_internal::getSession(dc);
|
|
|
|
if (!session) return 0;
|
|
|
|
|
2014-11-14 23:23:35 +00:00
|
|
|
return session->send(request, callbacks, msCanWait, true, !dc, after);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
template <typename TRequest>
|
2014-11-05 17:43:32 +00:00
|
|
|
inline mtpRequestId send(const TRequest &request, RPCDoneHandlerPtr onDone, RPCFailHandlerPtr onFail = RPCFailHandlerPtr(), int32 dc = 0, uint64 msCanWait = 0, mtpRequestId after = 0) {
|
|
|
|
return send(request, RPCResponseHandler(onDone, onFail), dc, msCanWait, after);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-04-16 14:59:42 +00:00
|
|
|
void ping();
|
2014-05-30 08:53:19 +00:00
|
|
|
void cancel(mtpRequestId req);
|
2014-08-01 18:49:43 +00:00
|
|
|
void killSession(int32 dc);
|
2014-12-05 13:44:27 +00:00
|
|
|
void stopSession(int32 dc);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
RequestSent = 0,
|
|
|
|
RequestConnecting = 1,
|
|
|
|
RequestSending = 2
|
|
|
|
};
|
|
|
|
int32 state(mtpRequestId req); // < 0 means waiting for such count of ms
|
|
|
|
|
|
|
|
void defOnError(const RPCError &err);
|
|
|
|
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
void authed(int32 uid);
|
|
|
|
int32 authedId();
|
2014-07-13 09:50:38 +00:00
|
|
|
void logoutKeys(RPCDoneHandlerPtr onDone, RPCFailHandlerPtr onFail);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
void setGlobalDoneHandler(RPCDoneHandlerPtr handler);
|
|
|
|
void setGlobalFailHandler(RPCFailHandlerPtr handler);
|
|
|
|
void setStateChangedHandler(MTPStateChangedHandler handler);
|
2014-08-01 18:49:43 +00:00
|
|
|
void setSessionResetHandler(MTPSessionResetHandler handler);
|
2014-05-30 08:53:19 +00:00
|
|
|
void clearGlobalHandlers();
|
|
|
|
|
2014-08-01 18:49:43 +00:00
|
|
|
void updateDcOptions(const QVector<MTPDcOption> &options);
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
template <typename T>
|
|
|
|
T nonce() {
|
|
|
|
T result;
|
|
|
|
memset_rand(&result, sizeof(T));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-02 12:34:16 +00:00
|
|
|
mtpKeysMap getKeys();
|
|
|
|
void setKey(int32 dc, mtpAuthKeyPtr key);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "mtproto/mtpSessionImpl.h"
|