mtproto_abstract_socket.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "base/bytes.h"
  9. #include "base/basic_types.h"
  10. namespace MTP::details {
  11. class AbstractSocket : protected QObject {
  12. public:
  13. static std::unique_ptr<AbstractSocket> Create(
  14. not_null<QThread*> thread,
  15. const bytes::vector &secret,
  16. const QNetworkProxy &proxy,
  17. bool protocolForFiles);
  18. void setDebugId(const QString &id) {
  19. _debugId = id;
  20. }
  21. explicit AbstractSocket(not_null<QThread*> thread) {
  22. moveToThread(thread);
  23. }
  24. virtual ~AbstractSocket() = default;
  25. [[nodiscard]] rpl::producer<> connected() const {
  26. return _connected.events();
  27. }
  28. [[nodiscard]] rpl::producer<> disconnected() const {
  29. return _disconnected.events();
  30. }
  31. [[nodiscard]] rpl::producer<> readyRead() const {
  32. return _readyRead.events();
  33. }
  34. [[nodiscard]] rpl::producer<> error() const {
  35. return _error.events();
  36. }
  37. [[nodiscard]] rpl::producer<> syncTimeRequests() const {
  38. return _syncTimeRequests.events();
  39. }
  40. virtual void connectToHost(const QString &address, int port) = 0;
  41. [[nodiscard]] virtual bool isGoodStartNonce(bytes::const_span nonce) = 0;
  42. virtual void timedOut() = 0;
  43. [[nodiscard]] virtual bool isConnected() = 0;
  44. [[nodiscard]] virtual bool hasBytesAvailable() = 0;
  45. [[nodiscard]] virtual int64 read(bytes::span buffer) = 0;
  46. virtual void write(
  47. bytes::const_span prefix,
  48. bytes::const_span buffer) = 0;
  49. virtual int32 debugState() = 0;
  50. [[nodiscard]] virtual QString debugPostfix() const = 0;
  51. protected:
  52. static const int kFilesSendBufferSize = 2 * 1024 * 1024;
  53. static const int kFilesReceiveBufferSize = 2 * 1024 * 1024;
  54. void logError(int errorCode, const QString &errorText);
  55. QString _debugId;
  56. rpl::event_stream<> _connected;
  57. rpl::event_stream<> _disconnected;
  58. rpl::event_stream<> _readyRead;
  59. rpl::event_stream<> _error;
  60. rpl::event_stream<> _syncTimeRequests;
  61. };
  62. } // namespace MTP::details