mtproto_abstract_socket.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "mtproto/details/mtproto_abstract_socket.h"
  8. #include "mtproto/details/mtproto_tcp_socket.h"
  9. #include "mtproto/details/mtproto_tls_socket.h"
  10. namespace MTP::details {
  11. std::unique_ptr<AbstractSocket> AbstractSocket::Create(
  12. not_null<QThread*> thread,
  13. const bytes::vector &secret,
  14. const QNetworkProxy &proxy,
  15. bool protocolForFiles) {
  16. if (secret.size() >= 21 && secret[0] == bytes::type(0xEE)) {
  17. return std::make_unique<TlsSocket>(
  18. thread,
  19. secret,
  20. proxy,
  21. protocolForFiles);
  22. } else {
  23. return std::make_unique<TcpSocket>(thread, proxy, protocolForFiles);
  24. }
  25. }
  26. void AbstractSocket::logError(int errorCode, const QString &errorText) {
  27. const auto log = [&](const QString &message) {
  28. DEBUG_LOG(("Socket %1 Error: ").arg(_debugId) + message);
  29. };
  30. switch (errorCode) {
  31. case QAbstractSocket::ConnectionRefusedError:
  32. log(u"Socket connection refused - %1."_q.arg(errorText));
  33. break;
  34. case QAbstractSocket::RemoteHostClosedError:
  35. log(u"Remote host closed socket connection - %1."_q.arg(errorText));
  36. break;
  37. case QAbstractSocket::HostNotFoundError:
  38. log(u"Host not found - %1."_q.arg(errorText));
  39. break;
  40. case QAbstractSocket::SocketTimeoutError:
  41. log(u"Socket timeout - %1."_q.arg(errorText));
  42. break;
  43. case QAbstractSocket::NetworkError: {
  44. log(u"Network - %1."_q.arg(errorText));
  45. } break;
  46. case QAbstractSocket::ProxyAuthenticationRequiredError:
  47. case QAbstractSocket::ProxyConnectionRefusedError:
  48. case QAbstractSocket::ProxyConnectionClosedError:
  49. case QAbstractSocket::ProxyConnectionTimeoutError:
  50. case QAbstractSocket::ProxyNotFoundError:
  51. case QAbstractSocket::ProxyProtocolError:
  52. log(u"Proxy (%1) - %2."_q.arg(errorCode).arg(errorText));
  53. break;
  54. default:
  55. log(u"Other (%1) - %2."_q.arg(errorCode).arg(errorText));
  56. break;
  57. }
  58. }
  59. } // namespace MTP::details