connection_tcp.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "mtproto/connection_abstract.h"
  9. #include "mtproto/mtproto_auth_key.h"
  10. namespace MTP {
  11. namespace details {
  12. class AbstractSocket;
  13. class TcpConnection : public AbstractConnection {
  14. public:
  15. TcpConnection(
  16. not_null<Instance*> instance,
  17. QThread *thread,
  18. const ProxyData &proxy);
  19. ConnectionPointer clone(const ProxyData &proxy) override;
  20. crl::time pingTime() const override;
  21. crl::time fullConnectTimeout() const override;
  22. void sendData(mtpBuffer &&buffer) override;
  23. void disconnectFromServer() override;
  24. void connectToServer(
  25. const QString &address,
  26. int port,
  27. const bytes::vector &protocolSecret,
  28. int16 protocolDcId,
  29. bool protocolForFiles) override;
  30. void timedOut() override;
  31. bool isConnected() const override;
  32. int32 debugState() const override;
  33. QString transport() const override;
  34. QString tag() const override;
  35. ~TcpConnection();
  36. private:
  37. enum class Status {
  38. Waiting = 0,
  39. Ready,
  40. Finished,
  41. };
  42. void socketRead();
  43. bytes::const_span prepareConnectionStartPrefix(bytes::span buffer);
  44. void socketPacket(bytes::const_span bytes);
  45. void socketConnected();
  46. void socketDisconnected();
  47. void socketError();
  48. mtpBuffer parsePacket(bytes::const_span bytes);
  49. void ensureAvailableInBuffer(int amount);
  50. static uint32 fourCharsToUInt(char ch1, char ch2, char ch3, char ch4) {
  51. char ch[4] = { ch1, ch2, ch3, ch4 };
  52. return *reinterpret_cast<uint32*>(ch);
  53. }
  54. const not_null<Instance*> _instance;
  55. std::unique_ptr<AbstractSocket> _socket;
  56. bool _connectionStarted = false;
  57. int _offsetBytes = 0;
  58. int _readBytes = 0;
  59. int _leftBytes = 0;
  60. bytes::vector _smallBuffer;
  61. bytes::vector _largeBuffer;
  62. bool _usingLargeBuffer = false;
  63. uchar _sendKey[CTRState::KeySize];
  64. CTRState _sendState;
  65. uchar _receiveKey[CTRState::KeySize];
  66. CTRState _receiveState;
  67. class Protocol;
  68. std::unique_ptr<Protocol> _protocol;
  69. int16 _protocolDcId = 0;
  70. Status _status = Status::Waiting;
  71. MTPint128 _checkNonce;
  72. QString _address;
  73. int32 _port = 0;
  74. crl::time _pingTime = 0;
  75. rpl::lifetime _connectedLifetime;
  76. rpl::lifetime _lifetime;
  77. };
  78. } // namespace details
  79. } // namespace MTP