diff --git a/TMessagesProj/jni/tgnet/ConnectionsManager.cpp b/TMessagesProj/jni/tgnet/ConnectionsManager.cpp index eb9d2f5fc..6980109c2 100644 --- a/TMessagesProj/jni/tgnet/ConnectionsManager.cpp +++ b/TMessagesProj/jni/tgnet/ConnectionsManager.cpp @@ -34,7 +34,8 @@ #ifdef ANDROID #include JavaVM *javaVm = nullptr; -JNIEnv *jniEnv[MAX_ACCOUNT_COUNT]; +//JNIEnv *jniEnv[MAX_ACCOUNT_COUNT]; +std::vector jniEnv(MAX_ACCOUNT_COUNT); jclass jclass_ByteBuffer = nullptr; jmethodID jclass_ByteBuffer_allocateDirect = 0; #endif @@ -132,58 +133,18 @@ ConnectionsManager::~ConnectionsManager() { pthread_mutex_destroy(&mutex); } +std::vector ConnectionsManager::_instances = std::vector(MAX_ACCOUNT_COUNT); + ConnectionsManager& ConnectionsManager::getInstance(int32_t instanceNum) { - switch (instanceNum) { - case 0: - static ConnectionsManager instance0(0); - return instance0; - case 1: - static ConnectionsManager instance1(1); - return instance1; - case 2: - static ConnectionsManager instance2(2); - return instance2; - case 3: - static ConnectionsManager instance3(3); - return instance3; - case 4: - static ConnectionsManager instance4(4); - return instance4; - case 5: - static ConnectionsManager instance5(5); - return instance5; - case 6: - static ConnectionsManager instance6(6); - return instance6; - case 7: - static ConnectionsManager instance7(7); - return instance7; - case 8: - static ConnectionsManager instance8(8); - return instance8; - case 9: - static ConnectionsManager instance9(9); - return instance9; - case 10: - static ConnectionsManager instance10(10); - return instance10; - case 11: - static ConnectionsManager instance11(11); - return instance11; - case 12: - static ConnectionsManager instance12(12); - return instance12; - case 13: - static ConnectionsManager instance13(13); - return instance13; - case 14: - static ConnectionsManager instance14(14); - return instance14; - case 15: - default: - static ConnectionsManager instance15(15); - return instance15; + static std::mutex _new_mutex; + + if(_instances[instanceNum] == nullptr) { + _new_mutex.lock(); + if(_instances[instanceNum] == nullptr) + _instances[instanceNum] = new ConnectionsManager(instanceNum); + _new_mutex.unlock(); } + return *_instances[instanceNum]; } int ConnectionsManager::callEvents(int64_t now) { diff --git a/TMessagesProj/jni/tgnet/ConnectionsManager.h b/TMessagesProj/jni/tgnet/ConnectionsManager.h index 50dccad4e..d5f14a62c 100644 --- a/TMessagesProj/jni/tgnet/ConnectionsManager.h +++ b/TMessagesProj/jni/tgnet/ConnectionsManager.h @@ -91,6 +91,7 @@ public: private: static void *ThreadProc(void *data); + static std::vector _instances; void initDatacenters(); void loadConfig(); @@ -264,7 +265,8 @@ private: #ifdef ANDROID extern JavaVM *javaVm; -extern JNIEnv *jniEnv[MAX_ACCOUNT_COUNT]; +//extern JNIEnv *jniEnv[MAX_ACCOUNT_COUNT]; +extern std::vector jniEnv; extern jclass jclass_ByteBuffer; extern jmethodID jclass_ByteBuffer_allocateDirect; #endif