Defines.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * This is the source code of tgnet library v. 1.1
  3. * It is licensed under GNU GPL v. 2 or later.
  4. * You should have received a copy of the license in this archive (see LICENSE).
  5. *
  6. * Copyright Nikolai Kudashov, 2015-2018.
  7. */
  8. #ifndef DEFINES_H
  9. #define DEFINES_H
  10. #include <functional>
  11. #include <list>
  12. #include <limits.h>
  13. #include <sstream>
  14. #include <inttypes.h>
  15. #include "ByteArray.h"
  16. #define USE_DEBUG_SESSION false
  17. #define READ_BUFFER_SIZE 1024 * 1024 * 2
  18. //#define DEBUG_VERSION
  19. #define PFS_ENABLED 1
  20. #define DEFAULT_DATACENTER_ID INT_MAX
  21. #define DC_UPDATE_TIME 60 * 60
  22. #define TEMP_AUTH_KEY_EXPIRE_TIME 24 * 60 * 60
  23. #define PROXY_CONNECTIONS_COUNT 4
  24. #define DOWNLOAD_CONNECTIONS_COUNT 2
  25. #define UPLOAD_CONNECTIONS_COUNT 4
  26. #define CONNECTION_BACKGROUND_KEEP_TIME 10000
  27. #define MAX_ACCOUNT_COUNT 5
  28. #define USE_DELEGATE_HOST_RESOLVE
  29. #define USE_IPV4_ONLY 0
  30. #define USE_IPV6_ONLY 1
  31. #define USE_IPV4_IPV6_RANDOM 2
  32. #define NETWORK_TYPE_MOBILE 0
  33. #define NETWORK_TYPE_WIFI 1
  34. #define NETWORK_TYPE_ROAMING 2
  35. class TLObject;
  36. class TL_error;
  37. class Request;
  38. class TL_message;
  39. class TL_config;
  40. class NativeByteBuffer;
  41. class Handshake;
  42. class ConnectionSocket;
  43. typedef std::function<void(TLObject *response, TL_error *error, int32_t networkType, int64_t responseTime)> onCompleteFunc;
  44. typedef std::function<void()> onQuickAckFunc;
  45. typedef std::function<void()> onWriteToSocketFunc;
  46. typedef std::function<void(int64_t messageId)> fillParamsFunc;
  47. typedef std::function<void(int64_t requestTime)> onRequestTimeFunc;
  48. typedef std::list<std::unique_ptr<Request>> requestsList;
  49. typedef requestsList::iterator requestsIter;
  50. typedef struct NetworkMessage {
  51. std::unique_ptr<TL_message> message;
  52. bool invokeAfter = false;
  53. bool needQuickAck = false;
  54. bool forceContainer = false;
  55. int32_t requestId;
  56. } NetworkMessage;
  57. enum ConnectionType {
  58. ConnectionTypeGeneric = 1,
  59. ConnectionTypeDownload = 2,
  60. ConnectionTypeUpload = 4,
  61. ConnectionTypePush = 8,
  62. ConnectionTypeTemp = 16,
  63. ConnectionTypeProxy = 32,
  64. ConnectionTypeGenericMedia = 64
  65. };
  66. enum TcpAddressFlag {
  67. TcpAddressFlagIpv6 = 1,
  68. TcpAddressFlagDownload = 2,
  69. TcpAddressFlagO = 4,
  70. TcpAddressFlagCdn = 8,
  71. TcpAddressFlagStatic = 16,
  72. TcpAddressFlagTemp = 2048
  73. };
  74. enum ConnectionState {
  75. ConnectionStateConnecting = 1,
  76. ConnectionStateWaitingForNetwork = 2,
  77. ConnectionStateConnected = 3,
  78. ConnectionStateConnectingViaProxy = 4
  79. };
  80. enum EventObjectType {
  81. EventObjectTypeConnection,
  82. EventObjectTypeTimer,
  83. EventObjectTypePipe,
  84. EventObjectTypeEvent
  85. };
  86. enum FileLoadState {
  87. FileLoadStateIdle,
  88. FileLoadStateDownloading,
  89. FileLoadStateFailed,
  90. FileLoadStateFinished
  91. };
  92. enum FileLoadFailReason {
  93. FileLoadFailReasonError,
  94. FileLoadFailReasonCanceled,
  95. FileLoadFailReasonRetryLimit
  96. };
  97. enum HandshakeType {
  98. HandshakeTypePerm,
  99. HandshakeTypeTemp,
  100. HandshakeTypeMediaTemp,
  101. HandshakeTypeCurrent,
  102. HandshakeTypeAll
  103. };
  104. class TcpAddress {
  105. public:
  106. std::string address;
  107. int32_t flags;
  108. int32_t port;
  109. std::string secret;
  110. TcpAddress(std::string addr, int32_t p, int32_t f, std::string s) {
  111. address = addr;
  112. port = p;
  113. flags = f;
  114. secret = s;
  115. }
  116. };
  117. typedef std::function<void(std::string path)> onFinishedFunc;
  118. typedef std::function<void(FileLoadFailReason reason)> onFailedFunc;
  119. typedef std::function<void(float progress)> onProgressChangedFunc;
  120. typedef struct ConnectiosManagerDelegate {
  121. virtual void onUpdate(int32_t instanceNum) = 0;
  122. virtual void onSessionCreated(int32_t instanceNum) = 0;
  123. virtual void onConnectionStateChanged(ConnectionState state, int32_t instanceNum) = 0;
  124. virtual void onUnparsedMessageReceived(int64_t reqMessageId, NativeByteBuffer *buffer, ConnectionType connectionType, int32_t instanceNum) = 0;
  125. virtual void onLogout(int32_t instanceNum) = 0;
  126. virtual void onUpdateConfig(TL_config *config, int32_t instanceNum) = 0;
  127. virtual void onInternalPushReceived(int32_t instanceNum) = 0;
  128. virtual void onBytesSent(int32_t amount, int32_t networkType, int32_t instanceNum) = 0;
  129. virtual void onBytesReceived(int32_t amount, int32_t networkType, int32_t instanceNum) = 0;
  130. virtual void onRequestNewServerIpAndPort(int32_t second, int32_t instanceNum) = 0;
  131. virtual void onProxyError(int32_t instanceNum) = 0;
  132. virtual void getHostByName(std::string domain, int32_t instanceNum, ConnectionSocket *socket) = 0;
  133. virtual int32_t getInitFlags(int32_t instanceNum) = 0;
  134. } ConnectiosManagerDelegate;
  135. typedef struct HandshakeDelegate {
  136. virtual void onHandshakeComplete(Handshake *handshake, int64_t keyId, ByteArray *authKey, int32_t timeDifference) = 0;
  137. } HandshakeDelegate;
  138. #define AllConnectionTypes ConnectionTypeGeneric | ConnectionTypeDownload | ConnectionTypeUpload
  139. enum RequestFlag {
  140. RequestFlagEnableUnauthorized = 1,
  141. RequestFlagFailOnServerErrors = 2,
  142. RequestFlagCanCompress = 4,
  143. RequestFlagWithoutLogin = 8,
  144. RequestFlagTryDifferentDc = 16,
  145. RequestFlagForceDownload = 32,
  146. RequestFlagInvokeAfter = 64,
  147. RequestFlagNeedQuickAck = 128,
  148. RequestFlagUseUnboundKey = 256,
  149. RequestFlagResendAfter = 512
  150. };
  151. inline std::string to_string_int32(int32_t value) {
  152. char buf[30];
  153. int len = sprintf(buf, "%d", value);
  154. return std::string(buf, (uint32_t) len);
  155. }
  156. inline std::string to_string_uint64(uint64_t value) {
  157. char buf[30];
  158. int len = sprintf(buf, "%" PRIu64, value);
  159. return std::string(buf, (uint32_t) len);
  160. }
  161. inline int32_t char2int(char input) {
  162. if (input >= '0' && input <= '9') {
  163. return input - '0';
  164. } else if (input >= 'A' && input <= 'F') {
  165. return (char) (input - 'A' + 10);
  166. } else if (input >= 'a' && input <= 'f') {
  167. return (char) (input - 'a' + 10);
  168. }
  169. return 0;
  170. }
  171. #endif