2016-03-24 12:57:10 +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.
|
2016-03-24 12:57:10 +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
|
2016-03-24 12:57:10 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-02-23 06:57:04 +00:00
|
|
|
#include "mtproto/dc_options.h"
|
2018-04-24 19:09:20 +00:00
|
|
|
#include "base/bytes.h"
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
namespace MTP {
|
2018-05-17 19:58:00 +00:00
|
|
|
|
|
|
|
bytes::vector ProtocolSecretFromPassword(const QString &password);
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
namespace internal {
|
|
|
|
|
2018-04-24 08:46:27 +00:00
|
|
|
struct ConnectionOptions;
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
class AbstractConnection;
|
|
|
|
|
|
|
|
class ConnectionPointer {
|
|
|
|
public:
|
|
|
|
ConnectionPointer();
|
|
|
|
ConnectionPointer(std::nullptr_t);
|
|
|
|
ConnectionPointer(ConnectionPointer &&other);
|
|
|
|
ConnectionPointer &operator=(ConnectionPointer &&other);
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
template <typename ConnectionType, typename ...Args>
|
|
|
|
static ConnectionPointer New(Args &&...args) {
|
|
|
|
return ConnectionPointer(new ConnectionType(
|
|
|
|
std::forward<Args>(args)...
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
AbstractConnection *get() const;
|
|
|
|
void reset(AbstractConnection *value = nullptr);
|
|
|
|
operator AbstractConnection*() const;
|
|
|
|
AbstractConnection *operator->() const;
|
|
|
|
AbstractConnection &operator*() const;
|
|
|
|
explicit operator bool() const;
|
|
|
|
|
|
|
|
~ConnectionPointer();
|
|
|
|
|
|
|
|
private:
|
2018-05-17 19:58:00 +00:00
|
|
|
explicit ConnectionPointer(AbstractConnection *value);
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
AbstractConnection *_value = nullptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
class AbstractConnection : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2018-05-17 19:58:00 +00:00
|
|
|
AbstractConnection(
|
|
|
|
QThread *thread,
|
|
|
|
const ProxyData &proxy);
|
2016-03-24 12:57:10 +00:00
|
|
|
AbstractConnection(const AbstractConnection &other) = delete;
|
|
|
|
AbstractConnection &operator=(const AbstractConnection &other) = delete;
|
2016-03-24 13:27:34 +00:00
|
|
|
virtual ~AbstractConnection() = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
// virtual constructor
|
2018-05-17 19:58:00 +00:00
|
|
|
static ConnectionPointer Create(
|
|
|
|
not_null<Instance*> instance,
|
2018-04-24 19:09:20 +00:00
|
|
|
DcOptions::Variants::Protocol protocol,
|
2018-05-17 19:58:00 +00:00
|
|
|
QThread *thread,
|
|
|
|
const ProxyData &proxy);
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
virtual ConnectionPointer clone(const ProxyData &proxy) = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-04-30 15:49:03 +00:00
|
|
|
virtual TimeMs pingTime() const = 0;
|
2018-05-17 19:58:00 +00:00
|
|
|
virtual TimeMs fullConnectTimeout() const = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
virtual void sendData(mtpBuffer &buffer) = 0; // has size + 3, buffer[0] = len, buffer[1] = packetnum, buffer[last] = crc32
|
|
|
|
virtual void disconnectFromServer() = 0;
|
2018-04-24 19:09:20 +00:00
|
|
|
virtual void connectToServer(
|
|
|
|
const QString &ip,
|
|
|
|
int port,
|
|
|
|
const bytes::vector &protocolSecret,
|
|
|
|
int16 protocolDcId) = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
virtual bool isConnected() const = 0;
|
|
|
|
virtual bool usingHttpWait() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
virtual bool needHttpWait() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int32 debugState() const = 0;
|
|
|
|
|
|
|
|
virtual QString transport() const = 0;
|
2018-04-24 19:09:20 +00:00
|
|
|
virtual QString tag() const = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void setSentEncrypted() {
|
|
|
|
_sentEncrypted = true;
|
|
|
|
}
|
|
|
|
|
2017-02-27 09:51:03 +00:00
|
|
|
using BuffersQueue = std::deque<mtpBuffer>;
|
2016-03-24 12:57:10 +00:00
|
|
|
BuffersQueue &received() {
|
2017-02-27 09:51:03 +00:00
|
|
|
return _receivedQueue;
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// Used to emit error(...) with no real code from the server.
|
|
|
|
static constexpr auto kErrorCodeOther = -499;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
signals:
|
2016-03-24 12:57:10 +00:00
|
|
|
void receivedData();
|
|
|
|
void receivedSome(); // to stop restart timer
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
void error(qint32 errorCodebool);
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
void connected();
|
|
|
|
void disconnected();
|
|
|
|
|
|
|
|
protected:
|
2017-02-27 09:51:03 +00:00
|
|
|
BuffersQueue _receivedQueue; // list of received packets, not processed yet
|
2018-04-30 15:49:03 +00:00
|
|
|
bool _sentEncrypted = false;
|
|
|
|
int _pingTime = 0;
|
2018-05-17 19:58:00 +00:00
|
|
|
ProxyData _proxy;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
// first we always send fake MTPReq_pq to see if connection works at all
|
|
|
|
// we send them simultaneously through TCP/HTTP/IPv4/IPv6 to choose the working one
|
|
|
|
static mtpBuffer preparePQFake(const MTPint128 &nonce);
|
|
|
|
static MTPResPQ readPQFakeReply(const mtpBuffer &buffer);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace MTP
|