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