mirror of
https://github.com/kotatogram/kotatogram-desktop
synced 2025-08-31 06:35:14 +00:00
moved connection implementations from connection module to separate modules, Xcode and QtCreator projects need fixing
This commit is contained in:
@@ -22,6 +22,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
||||
|
||||
#include "mtproto/core_types.h"
|
||||
#include "mtproto/auth_key.h"
|
||||
#include "mtproto/connection_abstract.h"
|
||||
|
||||
namespace MTP {
|
||||
namespace internal {
|
||||
@@ -68,244 +69,6 @@ private:
|
||||
|
||||
};
|
||||
|
||||
class AbstractConnection : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
typedef QList<mtpBuffer> BuffersQueue;
|
||||
|
||||
public:
|
||||
|
||||
AbstractConnection() : _sentEncrypted(false) {
|
||||
}
|
||||
AbstractConnection(const AbstractConnection &other) = delete;
|
||||
AbstractConnection &operator=(const AbstractConnection &other) = delete;
|
||||
virtual ~AbstractConnection() = 0{
|
||||
}
|
||||
|
||||
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;
|
||||
virtual void connectTcp(const QString &addr, int32 port, MTPDdcOption::Flags flags) = 0;
|
||||
virtual void connectHttp(const QString &addr, int32 port, MTPDdcOption::Flags flags) = 0;
|
||||
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;
|
||||
|
||||
BuffersQueue &received() {
|
||||
return receivedQueue;
|
||||
}
|
||||
|
||||
signals:
|
||||
|
||||
void receivedData();
|
||||
void receivedSome(); // to stop restart timer
|
||||
|
||||
void error(bool mayBeBadKey = false);
|
||||
|
||||
void connected();
|
||||
void disconnected();
|
||||
|
||||
protected:
|
||||
|
||||
BuffersQueue receivedQueue; // list of received packets, not processed yet
|
||||
bool _sentEncrypted;
|
||||
|
||||
};
|
||||
|
||||
class AbstractTcpConnection : public AbstractConnection {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
AbstractTcpConnection();
|
||||
virtual ~AbstractTcpConnection() = 0 {
|
||||
}
|
||||
|
||||
public slots:
|
||||
|
||||
void socketRead();
|
||||
|
||||
protected:
|
||||
|
||||
QTcpSocket sock;
|
||||
uint32 packetNum; // sent packet number
|
||||
|
||||
uint32 packetRead, packetLeft; // reading from socket
|
||||
bool readingToShort;
|
||||
char *currentPos;
|
||||
mtpBuffer longBuffer;
|
||||
mtpPrime shortBuffer[MTPShortBufferSize];
|
||||
virtual void socketPacket(const char *packet, uint32 length) = 0;
|
||||
|
||||
};
|
||||
|
||||
class AutoConnection : public AbstractTcpConnection {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
AutoConnection(QThread *thread);
|
||||
|
||||
void sendData(mtpBuffer &buffer) override;
|
||||
void disconnectFromServer() override;
|
||||
void connectTcp(const QString &addr, int32 port, MTPDdcOption::Flags flags) override;
|
||||
void connectHttp(const QString &addr, int32 port, MTPDdcOption::Flags flags) override;
|
||||
bool isConnected() const override;
|
||||
bool usingHttpWait() override;
|
||||
bool needHttpWait() override;
|
||||
|
||||
int32 debugState() const override;
|
||||
|
||||
QString transport() const override;
|
||||
|
||||
public slots:
|
||||
|
||||
void socketError(QAbstractSocket::SocketError e);
|
||||
void requestFinished(QNetworkReply *reply);
|
||||
|
||||
void onSocketConnected();
|
||||
void onSocketDisconnected();
|
||||
void onHttpStart();
|
||||
|
||||
void onTcpTimeoutTimer();
|
||||
|
||||
protected:
|
||||
|
||||
void socketPacket(const char *packet, uint32 length) override;
|
||||
|
||||
private:
|
||||
|
||||
void tcpSend(mtpBuffer &buffer);
|
||||
void httpSend(mtpBuffer &buffer);
|
||||
enum Status {
|
||||
WaitingBoth = 0,
|
||||
WaitingHttp,
|
||||
WaitingTcp,
|
||||
HttpReady,
|
||||
UsingHttp,
|
||||
UsingTcp,
|
||||
FinishedWork
|
||||
};
|
||||
Status status;
|
||||
MTPint128 tcpNonce, httpNonce;
|
||||
QTimer httpStartTimer;
|
||||
|
||||
QNetworkAccessManager manager;
|
||||
QUrl address;
|
||||
|
||||
typedef QSet<QNetworkReply*> Requests;
|
||||
Requests requests;
|
||||
|
||||
QString _addrTcp, _addrHttp;
|
||||
int32 _portTcp, _portHttp;
|
||||
MTPDdcOption::Flags _flagsTcp, _flagsHttp;
|
||||
int32 _tcpTimeout;
|
||||
QTimer tcpTimeoutTimer;
|
||||
|
||||
};
|
||||
|
||||
class TCPConnection : public AbstractTcpConnection {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
TCPConnection(QThread *thread);
|
||||
|
||||
void sendData(mtpBuffer &buffer) override;
|
||||
void disconnectFromServer() override;
|
||||
void connectTcp(const QString &addr, int32 port, MTPDdcOption::Flags flags) override;
|
||||
void connectHttp(const QString &addr, int32 port, MTPDdcOption::Flags flags) override { // not supported
|
||||
}
|
||||
bool isConnected() const override;
|
||||
|
||||
int32 debugState() const override;
|
||||
|
||||
QString transport() const override;
|
||||
|
||||
public slots:
|
||||
|
||||
void socketError(QAbstractSocket::SocketError e);
|
||||
|
||||
void onSocketConnected();
|
||||
void onSocketDisconnected();
|
||||
|
||||
void onTcpTimeoutTimer();
|
||||
|
||||
protected:
|
||||
|
||||
void socketPacket(const char *packet, uint32 length) override;
|
||||
|
||||
private:
|
||||
|
||||
enum Status {
|
||||
WaitingTcp = 0,
|
||||
UsingTcp,
|
||||
FinishedWork
|
||||
};
|
||||
Status status;
|
||||
MTPint128 tcpNonce;
|
||||
|
||||
QString _addr;
|
||||
int32 _port, _tcpTimeout;
|
||||
MTPDdcOption::Flags _flags;
|
||||
QTimer tcpTimeoutTimer;
|
||||
|
||||
};
|
||||
|
||||
class HTTPConnection : public AbstractConnection {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
HTTPConnection(QThread *thread);
|
||||
|
||||
void sendData(mtpBuffer &buffer) override;
|
||||
void disconnectFromServer() override;
|
||||
void connectTcp(const QString &addr, int32 port, MTPDdcOption::Flags flags) override { // not supported
|
||||
}
|
||||
void connectHttp(const QString &addr, int32 port, MTPDdcOption::Flags flags) override;
|
||||
bool isConnected() const override;
|
||||
bool usingHttpWait() override;
|
||||
bool needHttpWait() override;
|
||||
|
||||
int32 debugState() const override;
|
||||
|
||||
QString transport() const override;
|
||||
|
||||
public slots:
|
||||
|
||||
void requestFinished(QNetworkReply *reply);
|
||||
|
||||
private:
|
||||
|
||||
enum Status {
|
||||
WaitingHttp = 0,
|
||||
UsingHttp,
|
||||
FinishedWork
|
||||
};
|
||||
Status status;
|
||||
MTPint128 httpNonce;
|
||||
MTPDdcOption::Flags _flags;
|
||||
|
||||
QNetworkAccessManager manager;
|
||||
QUrl address;
|
||||
|
||||
typedef QSet<QNetworkReply*> Requests;
|
||||
Requests requests;
|
||||
|
||||
};
|
||||
|
||||
class ConnectionPrivate : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
|
Reference in New Issue
Block a user