2015-09-24 22:52:02 +02:00
|
|
|
/*
|
2018-07-30 04:07:02 +02:00
|
|
|
* This is the source code of tgnet library v. 1.1
|
2015-09-24 22:52:02 +02:00
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
2018-07-30 04:07:02 +02:00
|
|
|
* Copyright Nikolai Kudashov, 2015-2018.
|
2015-09-24 22:52:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DEFINES_H
|
|
|
|
#define DEFINES_H
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <list>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <sstream>
|
2018-07-30 04:07:02 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include "ByteArray.h"
|
2015-09-24 22:52:02 +02:00
|
|
|
|
|
|
|
#define USE_DEBUG_SESSION false
|
2021-09-20 07:54:41 +02:00
|
|
|
#define READ_BUFFER_SIZE 1024 * 1024 * 2
|
2023-04-27 23:47:58 +02:00
|
|
|
//#define DEBUG_VERSION
|
2019-12-31 14:08:08 +01:00
|
|
|
#define PFS_ENABLED 1
|
2015-09-24 22:52:02 +02:00
|
|
|
#define DEFAULT_DATACENTER_ID INT_MAX
|
|
|
|
#define DC_UPDATE_TIME 60 * 60
|
2019-12-31 14:08:08 +01:00
|
|
|
#define TEMP_AUTH_KEY_EXPIRE_TIME 24 * 60 * 60
|
2018-07-30 04:07:02 +02:00
|
|
|
#define PROXY_CONNECTIONS_COUNT 4
|
2015-09-24 22:52:02 +02:00
|
|
|
#define DOWNLOAD_CONNECTIONS_COUNT 2
|
2017-07-08 18:32:04 +02:00
|
|
|
#define UPLOAD_CONNECTIONS_COUNT 4
|
2016-04-22 15:49:00 +02:00
|
|
|
#define CONNECTION_BACKGROUND_KEEP_TIME 10000
|
2022-06-21 04:51:00 +02:00
|
|
|
#define MAX_ACCOUNT_COUNT 5
|
2019-05-14 14:08:05 +02:00
|
|
|
#define USE_DELEGATE_HOST_RESOLVE
|
2015-09-24 22:52:02 +02:00
|
|
|
|
2021-02-24 02:02:54 +01:00
|
|
|
#define USE_IPV4_ONLY 0
|
|
|
|
#define USE_IPV6_ONLY 1
|
|
|
|
#define USE_IPV4_IPV6_RANDOM 2
|
|
|
|
|
2017-03-31 01:58:05 +02:00
|
|
|
#define NETWORK_TYPE_MOBILE 0
|
|
|
|
#define NETWORK_TYPE_WIFI 1
|
|
|
|
#define NETWORK_TYPE_ROAMING 2
|
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
class TLObject;
|
|
|
|
class TL_error;
|
|
|
|
class Request;
|
|
|
|
class TL_message;
|
|
|
|
class TL_config;
|
|
|
|
class NativeByteBuffer;
|
2018-07-30 04:07:02 +02:00
|
|
|
class Handshake;
|
2019-05-14 14:08:05 +02:00
|
|
|
class ConnectionSocket;
|
2015-09-24 22:52:02 +02:00
|
|
|
|
2024-10-17 22:04:16 +02:00
|
|
|
typedef std::function<void(TLObject *response, TL_error *error, int32_t networkType, int64_t responseTime, int64_t msgId, int32_t dcId)> onCompleteFunc;
|
2015-09-24 22:52:02 +02:00
|
|
|
typedef std::function<void()> onQuickAckFunc;
|
2017-07-08 18:32:04 +02:00
|
|
|
typedef std::function<void()> onWriteToSocketFunc;
|
2024-03-08 15:32:16 +01:00
|
|
|
typedef std::function<void()> onRequestClearFunc;
|
|
|
|
typedef std::function<void()> onRequestCancelDoneFunc;
|
2018-07-30 04:07:02 +02:00
|
|
|
typedef std::function<void(int64_t messageId)> fillParamsFunc;
|
|
|
|
typedef std::function<void(int64_t requestTime)> onRequestTimeFunc;
|
2015-09-24 22:52:02 +02:00
|
|
|
typedef std::list<std::unique_ptr<Request>> requestsList;
|
|
|
|
typedef requestsList::iterator requestsIter;
|
|
|
|
|
|
|
|
typedef struct NetworkMessage {
|
|
|
|
std::unique_ptr<TL_message> message;
|
|
|
|
bool invokeAfter = false;
|
|
|
|
bool needQuickAck = false;
|
2021-07-15 16:24:57 +02:00
|
|
|
bool forceContainer = false;
|
2015-09-24 22:52:02 +02:00
|
|
|
int32_t requestId;
|
|
|
|
} NetworkMessage;
|
|
|
|
|
|
|
|
enum ConnectionType {
|
|
|
|
ConnectionTypeGeneric = 1,
|
|
|
|
ConnectionTypeDownload = 2,
|
|
|
|
ConnectionTypeUpload = 4,
|
|
|
|
ConnectionTypePush = 8,
|
2018-07-30 04:07:02 +02:00
|
|
|
ConnectionTypeTemp = 16,
|
|
|
|
ConnectionTypeProxy = 32,
|
|
|
|
ConnectionTypeGenericMedia = 64
|
2017-07-08 18:32:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum TcpAddressFlag {
|
|
|
|
TcpAddressFlagIpv6 = 1,
|
|
|
|
TcpAddressFlagDownload = 2,
|
|
|
|
TcpAddressFlagO = 4,
|
|
|
|
TcpAddressFlagCdn = 8,
|
|
|
|
TcpAddressFlagStatic = 16,
|
|
|
|
TcpAddressFlagTemp = 2048
|
2015-09-24 22:52:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ConnectionState {
|
|
|
|
ConnectionStateConnecting = 1,
|
|
|
|
ConnectionStateWaitingForNetwork = 2,
|
2017-07-08 18:32:04 +02:00
|
|
|
ConnectionStateConnected = 3,
|
|
|
|
ConnectionStateConnectingViaProxy = 4
|
2015-09-24 22:52:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum EventObjectType {
|
|
|
|
EventObjectTypeConnection,
|
2015-09-30 00:50:53 +02:00
|
|
|
EventObjectTypeTimer,
|
2016-10-11 13:57:01 +02:00
|
|
|
EventObjectTypePipe,
|
|
|
|
EventObjectTypeEvent
|
|
|
|
};
|
|
|
|
|
|
|
|
enum FileLoadState {
|
|
|
|
FileLoadStateIdle,
|
|
|
|
FileLoadStateDownloading,
|
|
|
|
FileLoadStateFailed,
|
|
|
|
FileLoadStateFinished
|
2015-09-24 22:52:02 +02:00
|
|
|
};
|
|
|
|
|
2016-10-11 13:57:01 +02:00
|
|
|
enum FileLoadFailReason {
|
|
|
|
FileLoadFailReasonError,
|
|
|
|
FileLoadFailReasonCanceled,
|
|
|
|
FileLoadFailReasonRetryLimit
|
|
|
|
};
|
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
enum HandshakeType {
|
|
|
|
HandshakeTypePerm,
|
|
|
|
HandshakeTypeTemp,
|
|
|
|
HandshakeTypeMediaTemp,
|
|
|
|
HandshakeTypeCurrent,
|
|
|
|
HandshakeTypeAll
|
|
|
|
};
|
|
|
|
|
2017-07-08 18:32:04 +02:00
|
|
|
class TcpAddress {
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::string address;
|
|
|
|
int32_t flags;
|
|
|
|
int32_t port;
|
2018-07-30 04:07:02 +02:00
|
|
|
std::string secret;
|
2017-07-08 18:32:04 +02:00
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
TcpAddress(std::string addr, int32_t p, int32_t f, std::string s) {
|
2017-07-08 18:32:04 +02:00
|
|
|
address = addr;
|
|
|
|
port = p;
|
|
|
|
flags = f;
|
2018-07-30 04:07:02 +02:00
|
|
|
secret = s;
|
2017-07-08 18:32:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-11 13:57:01 +02:00
|
|
|
typedef std::function<void(std::string path)> onFinishedFunc;
|
|
|
|
typedef std::function<void(FileLoadFailReason reason)> onFailedFunc;
|
|
|
|
typedef std::function<void(float progress)> onProgressChangedFunc;
|
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
typedef struct ConnectiosManagerDelegate {
|
2018-07-30 04:07:02 +02:00
|
|
|
virtual void onUpdate(int32_t instanceNum) = 0;
|
|
|
|
virtual void onSessionCreated(int32_t instanceNum) = 0;
|
|
|
|
virtual void onConnectionStateChanged(ConnectionState state, int32_t instanceNum) = 0;
|
|
|
|
virtual void onUnparsedMessageReceived(int64_t reqMessageId, NativeByteBuffer *buffer, ConnectionType connectionType, int32_t instanceNum) = 0;
|
|
|
|
virtual void onLogout(int32_t instanceNum) = 0;
|
|
|
|
virtual void onUpdateConfig(TL_config *config, int32_t instanceNum) = 0;
|
|
|
|
virtual void onInternalPushReceived(int32_t instanceNum) = 0;
|
|
|
|
virtual void onBytesSent(int32_t amount, int32_t networkType, int32_t instanceNum) = 0;
|
|
|
|
virtual void onBytesReceived(int32_t amount, int32_t networkType, int32_t instanceNum) = 0;
|
|
|
|
virtual void onRequestNewServerIpAndPort(int32_t second, int32_t instanceNum) = 0;
|
|
|
|
virtual void onProxyError(int32_t instanceNum) = 0;
|
2019-05-14 14:08:05 +02:00
|
|
|
virtual void getHostByName(std::string domain, int32_t instanceNum, ConnectionSocket *socket) = 0;
|
2018-07-30 04:07:02 +02:00
|
|
|
virtual int32_t getInitFlags(int32_t instanceNum) = 0;
|
2024-03-31 21:10:51 +02:00
|
|
|
virtual void onPremiumFloodWait(int32_t instanceNum, int32_t requestToken, bool isUpload) = 0;
|
2024-06-30 16:27:03 +02:00
|
|
|
virtual void onIntegrityCheckClassic(int32_t instanceNum, int32_t requestToken, std::string project, std::string nonce) = 0;
|
2015-09-24 22:52:02 +02:00
|
|
|
} ConnectiosManagerDelegate;
|
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
typedef struct HandshakeDelegate {
|
|
|
|
virtual void onHandshakeComplete(Handshake *handshake, int64_t keyId, ByteArray *authKey, int32_t timeDifference) = 0;
|
|
|
|
} HandshakeDelegate;
|
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
#define AllConnectionTypes ConnectionTypeGeneric | ConnectionTypeDownload | ConnectionTypeUpload
|
|
|
|
|
|
|
|
enum RequestFlag {
|
|
|
|
RequestFlagEnableUnauthorized = 1,
|
|
|
|
RequestFlagFailOnServerErrors = 2,
|
|
|
|
RequestFlagCanCompress = 4,
|
|
|
|
RequestFlagWithoutLogin = 8,
|
|
|
|
RequestFlagTryDifferentDc = 16,
|
|
|
|
RequestFlagForceDownload = 32,
|
|
|
|
RequestFlagInvokeAfter = 64,
|
2018-07-30 04:07:02 +02:00
|
|
|
RequestFlagNeedQuickAck = 128,
|
2021-07-15 16:24:57 +02:00
|
|
|
RequestFlagUseUnboundKey = 256,
|
2022-11-05 13:34:47 +01:00
|
|
|
RequestFlagResendAfter = 512,
|
2023-10-28 22:52:06 +02:00
|
|
|
RequestFlagIgnoreFloodWait = 1024,
|
2024-03-08 15:32:16 +01:00
|
|
|
RequestFlagListenAfterCancel = 2048,
|
2024-06-03 08:21:03 +02:00
|
|
|
RequestFlagIsCancel = 32768,
|
|
|
|
RequestFlagFailOnServerErrorsExceptFloodWait = 65536
|
2015-09-24 22:52:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
inline std::string to_string_int32(int32_t value) {
|
|
|
|
char buf[30];
|
|
|
|
int len = sprintf(buf, "%d", value);
|
2018-07-30 04:07:02 +02:00
|
|
|
return std::string(buf, (uint32_t) len);
|
2015-09-24 22:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string to_string_uint64(uint64_t value) {
|
|
|
|
char buf[30];
|
2018-07-30 04:07:02 +02:00
|
|
|
int len = sprintf(buf, "%" PRIu64, value);
|
|
|
|
return std::string(buf, (uint32_t) len);
|
2015-09-24 22:52:02 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 01:53:26 +02:00
|
|
|
inline int32_t char2int(char input) {
|
|
|
|
if (input >= '0' && input <= '9') {
|
|
|
|
return input - '0';
|
|
|
|
} else if (input >= 'A' && input <= 'F') {
|
|
|
|
return (char) (input - 'A' + 10);
|
|
|
|
} else if (input >= 'a' && input <= 'f') {
|
|
|
|
return (char) (input - 'a' + 10);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
#endif
|