Telegram-Android/TMessagesProj/jni/tgnet/ConnectionsManager.h

277 lines
12 KiB
C
Raw Normal View History

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 CONNECTIONSMANAGER_H
#define CONNECTIONSMANAGER_H
#include <pthread.h>
#include <queue>
#include <functional>
#include <sys/epoll.h>
#include <map>
2015-11-26 22:04:02 +01:00
#include <atomic>
2023-02-25 09:01:39 +01:00
#include <unordered_set>
2015-09-24 22:52:02 +02:00
#include "Defines.h"
#ifdef ANDROID
#include <jni.h>
#endif
class NativeByteBuffer;
class Connection;
class Datacenter;
class Request;
class DatacenterHandshake;
class TLObject;
class ConnectionSocket;
class TL_auth_exportedAuthorization;
class ByteArray;
class TL_config;
class EventObject;
class Config;
2018-07-30 04:07:02 +02:00
class ProxyCheckInfo;
2015-09-24 22:52:02 +02:00
class ConnectionsManager {
public:
2018-07-30 04:07:02 +02:00
ConnectionsManager(int32_t instance);
2015-09-24 22:52:02 +02:00
~ConnectionsManager();
2018-07-30 04:07:02 +02:00
static ConnectionsManager &getInstance(int32_t instanceNum);
2015-09-24 22:52:02 +02:00
int64_t getCurrentTimeMillis();
2017-03-31 01:58:05 +02:00
int64_t getCurrentTimeMonotonicMillis();
2015-09-24 22:52:02 +02:00
int32_t getCurrentTime();
2024-11-20 15:15:52 +01:00
int32_t getCurrentPingTime();
2020-10-30 11:26:29 +01:00
uint32_t getCurrentDatacenterId();
2017-07-08 18:32:04 +02:00
bool isTestBackend();
2015-09-24 22:52:02 +02:00
int32_t getTimeDifference();
2024-03-08 15:32:16 +01:00
int32_t sendRequest(TLObject *object, onCompleteFunc onComplete, onQuickAckFunc onQuickAck, onRequestClearFunc onClear, uint32_t flags, uint32_t datacenterId, ConnectionType connectionType, bool immediate);
int32_t sendRequest(TLObject *object, onCompleteFunc onComplete, onQuickAckFunc onQuickAck, onRequestClearFunc onClear, uint32_t flags, uint32_t datacenterId, ConnectionType connectionType, bool immediate, int32_t requestToken);
void cancelRequest(int32_t token, bool notifyServer, onRequestCancelDoneFunc onCancelled);
2020-09-30 15:48:47 +02:00
void cleanUp(bool resetKeys, int32_t datacenterId);
2015-09-24 22:52:02 +02:00
void cancelRequestsForGuid(int32_t guid);
void bindRequestToGuid(int32_t requestToken, int32_t guid);
void applyDatacenterAddress(uint32_t datacenterId, std::string ipAddress, uint32_t port);
void setDelegate(ConnectiosManagerDelegate *connectiosManagerDelegate);
ConnectionState getConnectionState();
2021-09-20 07:54:41 +02:00
void setUserId(int64_t userId);
2023-10-28 22:52:06 +02:00
void setUserPremium(bool premium);
2021-07-30 16:49:55 +02:00
void switchBackend(bool restart);
2015-09-24 22:52:02 +02:00
void resumeNetwork(bool partial);
void pauseNetwork();
2018-07-30 04:07:02 +02:00
void setNetworkAvailable(bool value, int32_t type, bool slow);
2021-02-24 02:02:54 +01:00
void setIpStrategy(uint8_t value);
2023-10-28 22:52:06 +02:00
void init(uint32_t version, int32_t layer, int32_t apiId, std::string deviceModel, std::string systemVersion, std::string appVersion, std::string langCode, std::string systemLangCode, std::string configPath, std::string logPath, std::string regId, std::string cFingerprint, std::string installerId, std::string packageId, int32_t timezoneOffset, int64_t userId, bool userPremium, bool isPaused, bool enablePushConnection, bool hasNetwork, int32_t networkType, int32_t performanceClass);
2018-07-30 04:07:02 +02:00
void setProxySettings(std::string address, uint16_t port, std::string username, std::string password, std::string secret);
2017-07-08 18:32:04 +02:00
void setLangCode(std::string langCode);
2019-07-18 15:01:39 +02:00
void setRegId(std::string regId);
2019-01-23 18:03:33 +01:00
void setSystemLangCode(std::string langCode);
2023-05-04 14:25:48 +02:00
void updateDcSettings(uint32_t datacenterId, bool workaround, bool ifLoadingTryAgain);
2016-04-22 15:49:00 +02:00
void setPushConnectionEnabled(bool value);
2019-08-22 01:53:26 +02:00
void applyDnsConfig(NativeByteBuffer *buffer, std::string phone, int32_t date);
2018-07-30 04:07:02 +02:00
int64_t checkProxy(std::string address, uint16_t port, std::string username, std::string password, std::string secret, onRequestTimeFunc requestTimeFunc, jobject ptr1);
2015-09-24 22:52:02 +02:00
#ifdef ANDROID
2024-03-08 15:32:16 +01:00
void sendRequest(TLObject *object, onCompleteFunc onComplete, onQuickAckFunc onQuickAck, onWriteToSocketFunc onWriteToSocket, onRequestClearFunc onClear, uint32_t flags, uint32_t datacenterId, ConnectionType connectionType, bool immediate, int32_t requestToken);
2015-09-24 22:52:02 +02:00
static void useJavaVM(JavaVM *vm, bool useJavaByteBuffers);
#endif
2023-07-20 22:15:36 +02:00
void reconnect(int32_t datacentrId, int32_t connectionType);
void failNotRunningRequest(int32_t token);
2024-06-03 08:21:03 +02:00
void receivedIntegrityCheckClassic(int32_t requestToken, std::string nonce, std::string token);
2023-07-20 22:15:36 +02:00
2015-09-24 22:52:02 +02:00
private:
static void *ThreadProc(void *data);
void initDatacenters();
void loadConfig();
void saveConfig();
2017-07-08 18:32:04 +02:00
void saveConfigInternal(NativeByteBuffer *buffer);
2015-09-24 22:52:02 +02:00
void select();
void wakeup();
void processServerResponse(TLObject *message, int64_t messageId, int32_t messageSeqNo, int64_t messageSalt, Connection *connection, int64_t innerMsgId, int64_t containerMessageId);
void sendPing(Datacenter *datacenter, bool usePushConnection);
void sendMessagesToConnection(std::vector<std::unique_ptr<NetworkMessage>> &messages, Connection *connection, bool reportAck);
void sendMessagesToConnectionWithConfirmation(std::vector<std::unique_ptr<NetworkMessage>> &messages, Connection *connection, bool reportAck);
2019-12-31 14:08:08 +01:00
void requestSaltsForDatacenter(Datacenter *datacenter, bool media, bool useTempConnection);
2018-07-30 04:07:02 +02:00
void clearRequestsForDatacenter(Datacenter *datacenter, HandshakeType type);
2015-09-24 22:52:02 +02:00
void registerForInternalPushUpdates();
void processRequestQueue(uint32_t connectionType, uint32_t datacenterId);
void moveToDatacenter(uint32_t datacenterId);
void authorizeOnMovingDatacenter();
void authorizedOnMovingDatacenter();
Datacenter *getDatacenterWithId(uint32_t datacenterId);
std::unique_ptr<TLObject> wrapInLayer(TLObject *object, Datacenter *datacenter, Request *baseRequest);
void removeRequestFromGuid(int32_t requestToken);
2024-03-08 15:32:16 +01:00
bool cancelRequestInternal(int32_t token, int64_t messageId, bool notifyServer, bool removeFromClass, onRequestCancelDoneFunc onCancelled);
2015-09-24 22:52:02 +02:00
int callEvents(int64_t now);
2024-03-08 15:32:16 +01:00
int32_t sendRequestInternal(TLObject *object, onCompleteFunc onComplete, onQuickAckFunc onQuickAck, onRequestClearFunc onClear, uint32_t flags, uint32_t datacenterId, ConnectionType connetionType, bool immediate);
2015-09-24 22:52:02 +02:00
void checkPendingTasks();
void scheduleTask(std::function<void()> task);
void scheduleEvent(EventObject *eventObject, uint32_t time);
void removeEvent(EventObject *eventObject);
2017-07-08 18:32:04 +02:00
void onConnectionClosed(Connection *connection, int reason);
2015-09-24 22:52:02 +02:00
void onConnectionConnected(Connection *connection);
void onConnectionQuickAckReceived(Connection *connection, int32_t ack);
void onConnectionDataReceived(Connection *connection, NativeByteBuffer *data, uint32_t length);
2019-01-23 18:03:33 +01:00
bool hasPendingRequestsForConnection(Connection *connection);
2015-09-24 22:52:02 +02:00
void attachConnection(ConnectionSocket *connection);
void detachConnection(ConnectionSocket *connection);
TLObject *TLdeserialize(TLObject *request, uint32_t bytes, NativeByteBuffer *data);
TLObject *getRequestWithMessageId(int64_t messageId);
2018-07-30 04:07:02 +02:00
void onDatacenterHandshakeComplete(Datacenter *datacenter, HandshakeType type, int32_t timeDiff);
2015-09-24 22:52:02 +02:00
void onDatacenterExportAuthorizationComplete(Datacenter *datacenter);
int64_t generateMessageId();
2021-02-24 02:02:54 +01:00
uint8_t getIpStratagy();
2015-09-24 22:52:02 +02:00
bool isNetworkAvailable();
2019-07-18 15:01:39 +02:00
void scheduleCheckProxyInternal(ProxyCheckInfo *proxyCheckInfo);
2018-07-30 04:07:02 +02:00
void checkProxyInternal(ProxyCheckInfo *proxyCheckInfo);
int32_t instanceNum = 0;
2019-12-31 14:08:08 +01:00
uint32_t configVersion = 5;
2015-09-24 22:52:02 +02:00
Config *config = nullptr;
std::list<EventObject *> events;
std::map<uint32_t, Datacenter *> datacenters;
std::map<int32_t, std::vector<std::int32_t>> quickAckIdToRequestIds;
int32_t pingTime;
2024-11-20 15:15:52 +01:00
int64_t pingTimeMs;
2015-09-24 22:52:02 +02:00
bool testBackend = false;
2018-07-30 04:07:02 +02:00
bool clientBlocked = true;
2019-01-23 18:03:33 +01:00
std::string lastInitSystemLangcode = "";
2018-07-30 04:07:02 +02:00
std::atomic<uint32_t> lastRequestToken{50000000};
2015-09-24 22:52:02 +02:00
uint32_t currentDatacenterId = 0;
uint32_t movingToDatacenterId = DEFAULT_DATACENTER_ID;
int64_t pushSessionId = 0;
int32_t currentPingTime = 0;
2024-11-20 15:15:52 +01:00
int32_t currentPingTimeLive = 0;
2015-09-24 22:52:02 +02:00
bool registeringForPush = false;
int64_t lastPushPingTime = 0;
2019-05-14 14:08:05 +02:00
int32_t nextPingTimeOffset = 60000 * 3;
2024-11-20 15:15:52 +01:00
int64_t sendingPushPingTime = 0;
2015-09-24 22:52:02 +02:00
bool sendingPushPing = false;
2019-01-23 18:03:33 +01:00
bool sendingPing = false;
2015-09-24 22:52:02 +02:00
bool updatingDcSettings = false;
2023-05-04 14:25:48 +02:00
bool updatingDcSettingsAgain = false;
uint32_t updatingDcSettingsAgainDcNum = 0;
2017-07-08 18:32:04 +02:00
bool updatingDcSettingsWorkaround = false;
int32_t disconnectTimeoutAmount = 0;
2019-08-22 01:53:26 +02:00
bool requestingSecondAddressByTlsHashMismatch = false;
2017-07-08 18:32:04 +02:00
int32_t requestingSecondAddress = 0;
2015-09-24 22:52:02 +02:00
int32_t updatingDcStartTime = 0;
int32_t lastDcUpdateTime = 0;
2017-03-31 01:58:05 +02:00
int64_t lastPingTime = getCurrentTimeMonotonicMillis();
2015-09-24 22:52:02 +02:00
bool networkPaused = false;
2016-04-22 15:49:00 +02:00
int32_t nextSleepTimeout = CONNECTION_BACKGROUND_KEEP_TIME;
2015-09-24 22:52:02 +02:00
int64_t lastPauseTime = 0;
2020-02-13 19:26:53 +01:00
int64_t lastMonotonicPauseTime = 0;
int32_t lastSystemPauseTime = 0;
2015-09-24 22:52:02 +02:00
ConnectionState connectionState = ConnectionStateConnecting;
std::unique_ptr<ByteArray> movingAuthorization;
std::vector<int64_t> sessionsToDestroy;
int32_t lastDestroySessionRequestTime;
std::map<int32_t, std::vector<int32_t>> requestsByGuids;
std::map<int32_t, int32_t> guidsByRequests;
2017-12-08 18:35:59 +01:00
std::map<int64_t, int64_t> resendRequests;
2018-07-30 04:07:02 +02:00
Datacenter *deserializingDatacenter;
2015-09-24 22:52:02 +02:00
2017-07-08 18:32:04 +02:00
std::string proxyUser = "";
std::string proxyPassword = "";
std::string proxyAddress = "";
2018-07-30 04:07:02 +02:00
std::string proxySecret = "";
uint16_t proxyPort = 1080;
int32_t lastPingProxyId = 2000000;
std::vector<std::unique_ptr<ProxyCheckInfo>> proxyCheckQueue;
std::vector<std::unique_ptr<ProxyCheckInfo>> proxyActiveChecks;
2017-07-08 18:32:04 +02:00
2015-09-24 22:52:02 +02:00
pthread_t networkThread;
pthread_mutex_t mutex;
std::queue<std::function<void()>> pendingTasks;
struct epoll_event *epollEvents;
timespec timeSpec;
2017-03-31 01:58:05 +02:00
timespec timeSpecMonotonic;
2015-09-24 22:52:02 +02:00
int32_t timeDifference = 0;
int64_t lastOutgoingMessageId = 0;
bool networkAvailable = true;
2018-07-30 04:07:02 +02:00
bool networkSlow = false;
2021-02-24 02:02:54 +01:00
uint8_t ipStrategy = USE_IPV4_ONLY;
2021-02-25 17:58:14 +01:00
bool lastProtocolIsIpv6 = false;
bool lastProtocolUsefullData = false;
2015-09-24 22:52:02 +02:00
std::vector<ConnectionSocket *> activeConnections;
2020-01-23 13:58:50 +01:00
std::vector<ConnectionSocket *> activeConnectionsCopy;
2015-09-24 22:52:02 +02:00
int epolFd;
2016-10-11 13:57:01 +02:00
int eventFd;
int *pipeFd = nullptr;
2015-09-24 22:52:02 +02:00
NativeByteBuffer *networkBuffer;
2023-07-20 22:15:36 +02:00
requestsList waitingLoginRequests;
2015-09-24 22:52:02 +02:00
requestsList requestsQueue;
requestsList runningRequests;
std::vector<uint32_t> requestingSaltsForDc;
2023-02-25 09:01:39 +01:00
std::unordered_set<int32_t> tokensToBeCancelled;
2015-09-24 22:52:02 +02:00
int32_t lastPingId = 0;
2021-04-14 03:44:46 +02:00
int64_t lastInvokeAfterMessageId = 0;
2015-09-24 22:52:02 +02:00
2017-03-31 01:58:05 +02:00
int32_t currentNetworkType = NETWORK_TYPE_WIFI;
2015-09-24 22:52:02 +02:00
uint32_t currentVersion = 1;
int32_t currentLayer = 34;
int32_t currentApiId = 6;
std::string currentDeviceModel;
std::string currentSystemVersion;
std::string currentAppVersion;
std::string currentLangCode;
2019-07-18 15:01:39 +02:00
std::string currentRegId;
2019-12-31 14:08:08 +01:00
std::string certFingerprint;
2020-07-26 10:03:38 +02:00
std::string installer;
2021-04-14 03:44:46 +02:00
std::string package;
2020-04-24 11:21:58 +02:00
int32_t currentDeviceTimezone = 0;
2017-07-08 18:32:04 +02:00
std::string currentSystemLangCode;
2015-09-24 22:52:02 +02:00
std::string currentConfigPath;
2015-10-29 18:10:07 +01:00
std::string currentLogPath;
2021-09-20 07:54:41 +02:00
int64_t currentUserId = 0;
2023-10-28 22:52:06 +02:00
bool currentUserPremium = false;
2015-09-24 22:52:02 +02:00
bool registeredForInternalPush = false;
2016-04-22 15:49:00 +02:00
bool pushConnectionEnabled = true;
2023-02-18 22:24:25 +01:00
int32_t currentPerformanceClass = -1;
2015-09-24 22:52:02 +02:00
2018-07-30 04:07:02 +02:00
std::map<uint32_t, std::vector<std::unique_ptr<NetworkMessage>>> genericMessagesToDatacenters;
std::map<uint32_t, std::vector<std::unique_ptr<NetworkMessage>>> genericMediaMessagesToDatacenters;
std::map<uint32_t, std::vector<std::unique_ptr<NetworkMessage>>> tempMessagesToDatacenters;
std::vector<uint32_t> unknownDatacenterIds;
std::vector<std::pair<Datacenter *, ConnectionType>> neededDatacenters;
std::map<uint32_t, uint32_t> downloadRunningRequestCount;
2023-10-28 22:52:06 +02:00
std::map<uint32_t, uint32_t> downloadCancelRunningRequestCount;
2018-07-30 04:07:02 +02:00
std::vector<Datacenter *> unauthorizedDatacenters;
NativeByteBuffer *sizeCalculator;
2015-09-24 22:52:02 +02:00
ConnectiosManagerDelegate *delegate;
friend class ConnectionSocket;
friend class ConnectionSession;
friend class Connection;
friend class Timer;
friend class Datacenter;
friend class TL_message;
friend class TL_rpc_result;
friend class Config;
2018-07-30 04:07:02 +02:00
friend class FileLog;
friend class Handshake;
2015-09-24 22:52:02 +02:00
};
#ifdef ANDROID
extern JavaVM *javaVm;
2018-07-30 04:07:02 +02:00
extern JNIEnv *jniEnv[MAX_ACCOUNT_COUNT];
2015-09-24 22:52:02 +02:00
extern jclass jclass_ByteBuffer;
extern jmethodID jclass_ByteBuffer_allocateDirect;
#endif
#endif