| 6 |
| * Copyright Nikolai Kudashov, 2015. |
| 7 | */ |
| 8 |
| 9 | #ifndef DEFINES_H |
| 10 | #define DEFINES_H |
| 11 |
| 12 | #include <functional> |
| 13 | #include <list> |
| 14 | #include <limits.h> |
| 15 | #include <bits/unique_ptr.h> |
| 16 | #include <sstream> |
| 17 |
| 18 | #define USE_DEBUG_SESSION false |
| 19 | #define READ_BUFFER_SIZE 1024 * 128 |
| 20 | //#define DEBUG_VERSION |
| 21 | #define DEFAULT_DATACENTER_ID INT_MAX |
| 22 | #define DC_UPDATE_TIME 60 * 60 |
| 23 | #define DOWNLOAD_CONNECTIONS_COUNT 2 |
| 24 | #define UPLOAD_CONNECTIONS_COUNT 2 |
| 25 | #define CONNECTION_BACKGROUND_KEEP_TIME 10000 |
| 26 |
| 27 | #define DOWNLOAD_CHUNK_SIZE 1024 * 32 |
| 28 | #define DOWNLOAD_CHUNK_BIG_SIZE 1024 * 128 |
| 29 | #define DOWNLOAD_MAX_REQUESTS 4 |
| 30 | #define DOWNLOAD_MAX_BIG_REQUESTS 4 |
| 31 | #define DOWNLOAD_BIG_FILE_MIN_SIZE 1024 * 1024 |
| 32 |
| 33 | #define NETWORK_TYPE_MOBILE 0 |
| 34 | #define NETWORK_TYPE_WIFI 1 |
| 35 | #define NETWORK_TYPE_ROAMING 2 |
| 36 |
| 37 | class TLObject; |
| 38 | class TL_error; |
| 39 | class Request; |
| 40 | class TL_message; |
| 41 | class TL_config; |
| 42 | class NativeByteBuffer; |
| 43 | class FileLoadOperation; |
| 44 |
| 45 | typedef std::function<void(TLObject *response, TL_error *error, int32_t networkType)> onCompleteFunc; |
| 46 | typedef std::function<void()> onQuickAckFunc; |
| 47 | typedef std::list<std::unique_ptr<Request>> requestsList; |
| 48 | typedef requestsList::iterator requestsIter; |
| 49 |
| 50 | typedef struct NetworkMessage { |
| 51 | std::unique_ptr<TL_message> message; |
| 52 | bool invokeAfter = false; |
| 53 | bool needQuickAck = false; |
| 54 | int32_t requestId; |
| 55 | } NetworkMessage; |
| 56 |
| 57 | enum ConnectionType { |
| 58 | ConnectionTypeGeneric = 1, |
| 59 | ConnectionTypeDownload = 2, |
| 60 | ConnectionTypeUpload = 4, |
| 61 | ConnectionTypePush = 8, |
| 62 | }; |
| 63 |
| 64 | enum ConnectionState { |
| 65 | ConnectionStateConnecting = 1, |
| 66 | ConnectionStateWaitingForNetwork = 2, |
| 67 | ConnectionStateConnected = 3 |
| 68 | }; |
| 69 |
| 70 | enum EventObjectType { |
| 71 | EventObjectTypeConnection, |
| 72 | EventObjectTypeTimer, |
| 73 | EventObjectTypePipe, |
| 74 | EventObjectTypeEvent |
| 75 | }; |
| 76 |
| 77 | enum FileLoadState { |
| 78 | FileLoadStateIdle, |
| 79 | FileLoadStateDownloading, |
| 80 | FileLoadStateFailed, |
| 81 | FileLoadStateFinished |
| 82 | }; |
| 83 |
| 84 | enum FileLoadFailReason { |
| 85 | FileLoadFailReasonError, |
| 86 | FileLoadFailReasonCanceled, |
| 87 | FileLoadFailReasonRetryLimit |
| 88 | }; |
| 89 |
| 90 | typedef std::function<void(std::string path)> onFinishedFunc; |
| 91 | typedef std::function<void(FileLoadFailReason reason)> onFailedFunc; |
| 92 | typedef std::function<void(float progress)> onProgressChangedFunc; |
| 93 |
| 94 | typedef struct ConnectiosManagerDelegate { |
| 95 | virtual void onUpdate() = 0; |
| 96 | virtual void onSessionCreated() = 0; |
| 97 | virtual void onConnectionStateChanged(ConnectionState state) = 0; |
| 98 | virtual void onUnparsedMessageReceived(int64_t reqMessageId, NativeByteBuffer *buffer, ConnectionType connectionType) = 0; |
| 99 | virtual void onLogout() = 0; |
| 100 | virtual void onUpdateConfig(TL_config *config) = 0; |
| 101 | virtual void onInternalPushReceived() = 0; |
| 102 | virtual void onBytesSent(int32_t amount, int32_t networkType) = 0; |
| 103 | virtual void onBytesReceived(int32_t amount, int32_t networkType) = 0; |
| 104 | } ConnectiosManagerDelegate; |
| 105 |
| 106 | #define AllConnectionTypes ConnectionTypeGeneric | ConnectionTypeDownload | ConnectionTypeUpload |
| 107 |
| 108 | enum RequestFlag { |
| 109 | RequestFlagEnableUnauthorized = 1, |
| 110 | RequestFlagFailOnServerErrors = 2, |
| 111 | RequestFlagCanCompress = 4, |
| 112 | RequestFlagWithoutLogin = 8, |
| 113 | RequestFlagTryDifferentDc = 16, |
| 114 | RequestFlagForceDownload = 32, |
| 115 | RequestFlagInvokeAfter = 64, |
| 116 | RequestFlagNeedQuickAck = 128 |
| 117 | }; |
| 118 |
| 119 | inline std::string to_string_int32(int32_t value) { |
| 120 | char buf[30]; |
| 121 | int len = sprintf(buf, "%d", value); |
| 122 | return std::string(buf, len); |
| 123 | } |
| 124 |
| 125 | inline std::string to_string_uint64(uint64_t value) { |
| 126 | char buf[30]; |
| 127 | int len = sprintf(buf, "%llu", value); |
| 128 | return std::string(buf, len); |
| 129 | } |
| 130 |
| 131 | #endif |
| 132 |