2016-03-24 12:57:10 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
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
|
2016-03-24 12:57:10 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "mtproto/core_types.h"
|
2017-02-23 06:57:04 +00:00
|
|
|
#include "mtproto/dc_options.h"
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
namespace MTP {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
class AbstractConnection : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
AbstractConnection(QThread *thread) : _sentEncrypted(false) {
|
|
|
|
moveToThread(thread);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
static AbstractConnection *create(QThread *thread);
|
|
|
|
|
|
|
|
void setSentEncrypted() {
|
|
|
|
_sentEncrypted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void sendData(mtpBuffer &buffer) = 0; // has size + 3, buffer[0] = len, buffer[1] = packetnum, buffer[last] = crc32
|
|
|
|
virtual void disconnectFromServer() = 0;
|
2017-02-23 06:57:04 +00:00
|
|
|
virtual void connectTcp(const DcOptions::Endpoint &endpoint) = 0;
|
|
|
|
virtual void connectHttp(const DcOptions::Endpoint &endpoint) = 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;
|
|
|
|
|
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
|
2016-03-24 12:57:10 +00:00
|
|
|
bool _sentEncrypted;
|
|
|
|
|
|
|
|
// 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
|