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
|
|
|
|
|
|
|
|
#include "mtproto/connection_abstract.h"
|
2019-07-05 07:54:53 +00:00
|
|
|
#include "mtproto/auth_key.h"
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
namespace MTP {
|
|
|
|
namespace internal {
|
|
|
|
|
2019-07-05 07:54:53 +00:00
|
|
|
class AbstractSocket;
|
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
class TcpConnection : public AbstractConnection {
|
2016-03-24 12:57:10 +00:00
|
|
|
public:
|
2019-07-05 07:54:53 +00:00
|
|
|
TcpConnection(QThread *thread, const ProxyData &proxy);
|
2018-04-30 15:49:03 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
ConnectionPointer clone(const ProxyData &proxy) override;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time pingTime() const override;
|
|
|
|
crl::time fullConnectTimeout() const override;
|
2018-06-25 18:55:27 +00:00
|
|
|
void sendData(mtpBuffer &&buffer) override;
|
2016-03-24 12:57:10 +00:00
|
|
|
void disconnectFromServer() override;
|
2018-04-24 19:09:20 +00:00
|
|
|
void connectToServer(
|
2018-05-17 10:27:27 +00:00
|
|
|
const QString &address,
|
2018-04-24 19:09:20 +00:00
|
|
|
int port,
|
|
|
|
const bytes::vector &protocolSecret,
|
|
|
|
int16 protocolDcId) override;
|
2016-03-24 12:57:10 +00:00
|
|
|
bool isConnected() const override;
|
2018-06-25 18:55:27 +00:00
|
|
|
bool requiresExtendedPadding() const override;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
int32 debugState() const override;
|
|
|
|
|
|
|
|
QString transport() const override;
|
2018-04-24 19:09:20 +00:00
|
|
|
QString tag() const override;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
~TcpConnection();
|
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
private:
|
|
|
|
enum class Status {
|
|
|
|
Waiting = 0,
|
|
|
|
Ready,
|
|
|
|
Finished,
|
|
|
|
};
|
|
|
|
|
|
|
|
void socketRead();
|
2019-07-08 15:41:34 +00:00
|
|
|
bytes::const_span prepareConnectionStartPrefix(bytes::span buffer);
|
2018-05-17 10:27:27 +00:00
|
|
|
|
2018-06-25 21:18:27 +00:00
|
|
|
void socketPacket(bytes::const_span bytes);
|
2018-05-17 10:27:27 +00:00
|
|
|
|
|
|
|
void socketConnected();
|
|
|
|
void socketDisconnected();
|
2019-07-05 07:54:53 +00:00
|
|
|
void socketError();
|
2018-05-17 10:27:27 +00:00
|
|
|
|
2018-06-25 21:18:27 +00:00
|
|
|
mtpBuffer parsePacket(bytes::const_span bytes);
|
2018-07-20 12:28:26 +00:00
|
|
|
void ensureAvailableInBuffer(int amount);
|
2018-05-17 10:27:27 +00:00
|
|
|
static uint32 fourCharsToUInt(char ch1, char ch2, char ch3, char ch4) {
|
|
|
|
char ch[4] = { ch1, ch2, ch3, ch4 };
|
|
|
|
return *reinterpret_cast<uint32*>(ch);
|
|
|
|
}
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2019-07-05 07:54:53 +00:00
|
|
|
std::unique_ptr<AbstractSocket> _socket;
|
2018-07-11 14:00:06 +00:00
|
|
|
bool _connectionStarted = false;
|
|
|
|
|
|
|
|
int _offsetBytes = 0;
|
|
|
|
int _readBytes = 0;
|
|
|
|
int _leftBytes = 0;
|
|
|
|
bytes::vector _smallBuffer;
|
|
|
|
bytes::vector _largeBuffer;
|
|
|
|
bool _usingLargeBuffer = false;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-05-17 10:27:27 +00:00
|
|
|
uchar _sendKey[CTRState::KeySize];
|
|
|
|
CTRState _sendState;
|
|
|
|
uchar _receiveKey[CTRState::KeySize];
|
|
|
|
CTRState _receiveState;
|
2018-06-25 18:55:27 +00:00
|
|
|
class Protocol;
|
|
|
|
std::unique_ptr<Protocol> _protocol;
|
2018-05-17 10:27:27 +00:00
|
|
|
int16 _protocolDcId = 0;
|
|
|
|
|
|
|
|
Status _status = Status::Waiting;
|
|
|
|
MTPint128 _checkNonce;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
QString _address;
|
2018-05-17 10:27:27 +00:00
|
|
|
int32 _port = 0;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _pingTime = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2019-07-05 07:54:53 +00:00
|
|
|
rpl::lifetime _connectedLifetime;
|
|
|
|
rpl::lifetime _lifetime;
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace MTP
|