| 5 | #include "tgnet/MTProtoScheme.h" |
| 6 | #include "tgnet/FileLog.h" |
| 7 |
| 8 | JavaVM *java; |
| 9 | jclass jclass_RequestDelegateInternal; |
| 10 | jmethodID jclass_RequestDelegateInternal_run; |
| 11 |
| 12 | jclass jclass_QuickAckDelegate; |
| 13 | jmethodID jclass_QuickAckDelegate_run; |
| 14 |
| 15 | jclass jclass_ConnectionsManager; |
| 16 | jmethodID jclass_ConnectionsManager_onUnparsedMessageReceived; |
| 17 | jmethodID jclass_ConnectionsManager_onUpdate; |
| 18 | jmethodID jclass_ConnectionsManager_onSessionCreated; |
| 19 | jmethodID jclass_ConnectionsManager_onLogout; |
| 20 | jmethodID jclass_ConnectionsManager_onConnectionStateChanged; |
| 21 | jmethodID jclass_ConnectionsManager_onInternalPushReceived; |
| 22 | jmethodID jclass_ConnectionsManager_onUpdateConfig; |
| 23 |
| 24 | jint getFreeBuffer(JNIEnv *env, jclass c, jint length) { |
| 25 | return (jint) BuffersStorage::getInstance().getFreeBuffer(length); |
| 26 | } |
| 27 |
| 28 | jint limit(JNIEnv *env, jclass c, jint address) { |
| 29 | NativeByteBuffer *buffer = (NativeByteBuffer *) address; |
| 30 | return buffer->limit(); |
| 31 | } |
| 32 |
| 33 | jint position(JNIEnv *env, jclass c, jint address) { |
| 34 | NativeByteBuffer *buffer = (NativeByteBuffer *) address; |
| 35 | return buffer->position(); |
| 36 | } |
| 37 |
| 38 | void reuse(JNIEnv *env, jclass c, jint address) { |
| 39 | NativeByteBuffer *buffer = (NativeByteBuffer *) address; |
| 40 | buffer->reuse(); |
| 41 | } |
| 42 |
| 43 | jobject getJavaByteBuffer(JNIEnv *env, jclass c, jint address) { |
| 44 | NativeByteBuffer *buffer = (NativeByteBuffer *) address; |
| 45 | return buffer->getJavaByteBuffer(); |
| 46 | } |
| 47 |
| 48 | static const char *NativeByteBufferClassPathName = "org/telegram/tgnet/NativeByteBuffer"; |
| 49 | static JNINativeMethod NativeByteBufferMethods[] = { |
| 50 | {"native_getFreeBuffer", "(I)I", (void *) getFreeBuffer}, |
| 51 | {"native_limit", "(I)I", (void *) limit}, |
| 52 | {"native_position", "(I)I", (void *) position}, |
| 53 | {"native_reuse", "(I)V", (void *) reuse}, |
| 54 | {"native_getJavaByteBuffer", "(I)Ljava/nio/ByteBuffer;", (void *) getJavaByteBuffer} |
| 55 | }; |
| 56 |
| 57 | jlong getCurrentTimeMillis(JNIEnv *env, jclass c) { |
| 58 | return ConnectionsManager::getInstance().getCurrentTimeMillis(); |
| 59 | } |
| 60 |
| 61 | jint getCurrentTime(JNIEnv *env, jclass c) { |
| 62 | return ConnectionsManager::getInstance().getCurrentTime(); |
| 63 | } |
| 64 |
| 65 | jint getTimeDifference(JNIEnv *env, jclass c) { |
| 66 | return ConnectionsManager::getInstance().getTimeDifference(); |
| 67 | } |
| 68 |
| 69 | void sendRequest(JNIEnv *env, jclass c, jint object, jobject onComplete, jobject onQuickAck, jint flags, jint datacenterId, jint connetionType, jboolean immediate, jint token) { |
| 70 | TL_api_request *request = new TL_api_request(); |
| 71 | request->request = (NativeByteBuffer *) object; |
| 72 | if (onComplete != nullptr) { |
| 73 | onComplete = env->NewGlobalRef(onComplete); |
| 74 | } |
| 75 | if (onQuickAck != nullptr) { |
| 76 | onQuickAck = env->NewGlobalRef(onQuickAck); |
| 77 | } |
| 78 | ConnectionsManager::getInstance().sendRequest(request, ([onComplete](TLObject *response, TL_error *error) { |
| 79 | TL_api_response *resp = (TL_api_response *) response; |
| 80 | jint ptr = 0; |
| 81 | jint errorCode = 0; |
| 82 | jstring errorText = nullptr; |
| 83 | if (resp != nullptr) { |
| 84 | ptr = (jint) resp->response.get(); |
| 85 | } else if (error != nullptr) { |
| 86 | errorCode = error->code; |
| 87 | errorText = jniEnv->NewStringUTF(error->text.c_str()); |
| 88 | } |
| 89 | if (onComplete != nullptr) { |
| 90 | jniEnv->CallVoidMethod(onComplete, jclass_RequestDelegateInternal_run, ptr, errorCode, errorText); |
| 91 | } |
| 92 | if (errorText != nullptr) { |
| 93 | jniEnv->DeleteLocalRef(errorText); |
| 94 | } |
| 95 | }), ([onQuickAck] { |
| 96 | if (onQuickAck != nullptr) { |
| 97 | jniEnv->CallVoidMethod(onQuickAck, jclass_QuickAckDelegate_run); |
| 98 | } |
| 99 | }), flags, datacenterId, (ConnectionType) connetionType, immediate, token, onComplete, onQuickAck); |
| 100 | } |
| 101 |
| 102 | void cancelRequest(JNIEnv *env, jclass c, jint token, jboolean notifyServer) { |
| 103 | return ConnectionsManager::getInstance().cancelRequest(token, notifyServer); |
| 104 | } |
| 105 |
| 106 | void cleanUp(JNIEnv *env, jclass c) { |
| 107 | return ConnectionsManager::getInstance().cleanUp(); |
| 108 | } |
| 109 |
| 110 | void cancelRequestsForGuid(JNIEnv *env, jclass c, jint guid) { |
| 111 | return ConnectionsManager::getInstance().cancelRequestsForGuid(guid); |
| 112 | } |
| 113 |
| 114 | void bindRequestToGuid(JNIEnv *env, jclass c, jint requestToken, jint guid) { |
| 115 | return ConnectionsManager::getInstance().bindRequestToGuid(requestToken, guid); |
| 116 | } |
| 117 |
| 118 | void applyDatacenterAddress(JNIEnv *env, jclass c, jint datacenterId, jstring ipAddress, jint port) { |
| 119 | const char *valueStr = env->GetStringUTFChars(ipAddress, 0); |
| 120 |
| 121 | ConnectionsManager::getInstance().applyDatacenterAddress(datacenterId, std::string(valueStr), port); |
| 122 |
| 123 | if (valueStr != 0) { |
| 124 | env->ReleaseStringUTFChars(ipAddress, valueStr); |
| 125 | } |
| 126 | } |
| 127 |
| 128 | jint getConnectionState(JNIEnv *env, jclass c) { |
| 129 | return ConnectionsManager::getInstance().getConnectionState(); |
| 130 | } |
| 131 |
| 132 | void setUserId(JNIEnv *env, jclass c, int32_t id) { |
| 133 | ConnectionsManager::getInstance().setUserId(id); |
| 134 | } |
| 135 |
| 136 | void switchBackend(JNIEnv *env, jclass c) { |
| 137 | ConnectionsManager::getInstance().switchBackend(); |
| 138 | } |
| 139 |
| 140 | void pauseNetwork(JNIEnv *env, jclass c) { |
| 141 | ConnectionsManager::getInstance().pauseNetwork(); |
| 142 | } |
| 143 |
| 144 | void resumeNetwork(JNIEnv *env, jclass c, jboolean partial) { |
| 145 | ConnectionsManager::getInstance().resumeNetwork(partial); |
| 146 | } |
| 147 |
| 148 | void updateDcSettings(JNIEnv *env, jclass c) { |
| 149 | ConnectionsManager::getInstance().updateDcSettings(0); |
| 150 | } |
| 151 |
| 152 | void setUseIpv6(JNIEnv *env, jclass c, bool value) { |
| 153 | ConnectionsManager::getInstance().setUseIpv6(value); |
| 154 | } |
| 155 |
| 156 | void setNetworkAvailable(JNIEnv *env, jclass c, jboolean value) { |
| 157 | ConnectionsManager::getInstance().setNetworkAvailable(value); |
| 158 | } |
| 159 |
| 160 | void setPushConnectionEnabled(JNIEnv *env, jclass c, jboolean value) { |
| 161 | ConnectionsManager::getInstance().setPushConnectionEnabled(value); |
| 162 | } |
| 163 |
| 164 | class Delegate : public ConnectiosManagerDelegate { |
| 165 |
| 166 | void onUpdate() { |
| 167 | jniEnv->CallStaticVoidMethod(jclass_ConnectionsManager, jclass_ConnectionsManager_onUpdate); |
| 168 | } |
| 169 |
| 170 | void onSessionCreated() { |
| 171 | jniEnv->CallStaticVoidMethod(jclass_ConnectionsManager, jclass_ConnectionsManager_onSessionCreated); |
| 172 | } |
| 173 |
| 174 | void onConnectionStateChanged(ConnectionState state) { |
| 175 | jniEnv->CallStaticVoidMethod(jclass_ConnectionsManager, jclass_ConnectionsManager_onConnectionStateChanged, state); |
| 176 | } |
| 177 |
| 178 | void onUnparsedMessageReceived(int64_t reqMessageId, NativeByteBuffer *buffer, ConnectionType connectionType) { |
| 179 | if (connectionType == ConnectionTypeGeneric) { |
| 180 | jniEnv->CallStaticVoidMethod(jclass_ConnectionsManager, jclass_ConnectionsManager_onUnparsedMessageReceived, buffer); |
| 181 | } |
| 182 | } |
| 183 |
| 184 | void onLogout() { |
| 185 | jniEnv->CallStaticVoidMethod(jclass_ConnectionsManager, jclass_ConnectionsManager_onLogout); |
| 186 | } |
| 187 |
| 188 | void onUpdateConfig(TL_config *config) { |
| 189 | NativeByteBuffer *buffer = BuffersStorage::getInstance().getFreeBuffer(config->getObjectSize()); |
| 190 | config->serializeToStream(buffer); |
| 191 | buffer->position(0); |
| 192 | jniEnv->CallStaticVoidMethod(jclass_ConnectionsManager, jclass_ConnectionsManager_onUpdateConfig, buffer); |
| 193 | buffer->reuse(); |
| 194 | } |
| 195 |
| 196 | void onInternalPushReceived() { |
| 197 | jniEnv->CallStaticVoidMethod(jclass_ConnectionsManager, jclass_ConnectionsManager_onInternalPushReceived); |
| 198 | } |
| 199 | }; |
| 200 |
| 201 | void init(JNIEnv *env, jclass c, jint version, jint layer, jint apiId, jstring deviceModel, jstring systemVersion, jstring appVersion, jstring langCode, jstring configPath, jstring logPath, jint userId, jboolean enablePushConnection) { |
| 202 | const char *deviceModelStr = env->GetStringUTFChars(deviceModel, 0); |
| 203 | const char *systemVersionStr = env->GetStringUTFChars(systemVersion, 0); |
| 204 | const char *appVersionStr = env->GetStringUTFChars(appVersion, 0); |
| 205 | const char *langCodeStr = env->GetStringUTFChars(langCode, 0); |
| 206 | const char *configPathStr = env->GetStringUTFChars(configPath, 0); |
| 207 | const char *logPathStr = env->GetStringUTFChars(logPath, 0); |
| 208 |
| 209 | ConnectionsManager::getInstance().init(version, layer, apiId, std::string(deviceModelStr), std::string(systemVersionStr), std::string(appVersionStr), std::string(langCodeStr), std::string(configPathStr), std::string(logPathStr), userId, true, enablePushConnection); |
| 210 |
| 211 | if (deviceModelStr != 0) { |
| 212 | env->ReleaseStringUTFChars(deviceModel, deviceModelStr); |
| 213 | } |
| 214 | if (systemVersionStr != 0) { |
| 215 | env->ReleaseStringUTFChars(systemVersion, systemVersionStr); |
| 216 | } |
| 217 | if (appVersionStr != 0) { |
| 218 | env->ReleaseStringUTFChars(appVersion, appVersionStr); |
| 219 | } |
| 220 | if (langCodeStr != 0) { |
| 221 | env->ReleaseStringUTFChars(langCode, langCodeStr); |
| 222 | } |
| 223 | if (configPathStr != 0) { |
| 224 | env->ReleaseStringUTFChars(configPath, configPathStr); |
| 225 | } |
| 226 | if (logPathStr != 0) { |
| 227 | env->ReleaseStringUTFChars(logPath, logPathStr); |
| 228 | } |
| 229 | } |
| 230 |
| 231 | void setJava(JNIEnv *env, jclass c, jboolean useJavaByteBuffers) { |
| 232 | ConnectionsManager::useJavaVM(java, useJavaByteBuffers); |
| 233 | ConnectionsManager::getInstance().setDelegate(new Delegate()); |
| 234 | } |
| 235 |
| 236 | static const char *ConnectionsManagerClassPathName = "org/telegram/tgnet/ConnectionsManager"; |
| 237 | static JNINativeMethod ConnectionsManagerMethods[] = { |
| 238 | {"native_getCurrentTimeMillis", "()J", (void *) getCurrentTimeMillis}, |
| 239 | {"native_getCurrentTime", "()I", (void *) getCurrentTime}, |
| 240 | {"native_getTimeDifference", "()I", (void *) getTimeDifference}, |
| 241 | {"native_sendRequest", "(ILorg/telegram/tgnet/RequestDelegateInternal;Lorg/telegram/tgnet/QuickAckDelegate;IIIZI)V", (void *) sendRequest}, |
| 242 | {"native_cancelRequest", "(IZ)V", (void *) cancelRequest}, |
| 243 | {"native_cleanUp", "()V", (void *) cleanUp}, |
| 244 | {"native_cancelRequestsForGuid", "(I)V", (void *) cancelRequestsForGuid}, |
| 245 | {"native_bindRequestToGuid", "(II)V", (void *) bindRequestToGuid}, |
| 246 | {"native_applyDatacenterAddress", "(ILjava/lang/String;I)V", (void *) applyDatacenterAddress}, |
| 247 | {"native_getConnectionState", "()I", (void *) getConnectionState}, |
| 248 | {"native_setUserId", "(I)V", (void *) setUserId}, |
| 249 | {"native_init", "(IIILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IZ)V", (void *) init}, |
| 250 | {"native_switchBackend", "()V", (void *) switchBackend}, |
| 251 | {"native_pauseNetwork", "()V", (void *) pauseNetwork}, |
| 252 | {"native_resumeNetwork", "(Z)V", (void *) resumeNetwork}, |
| 253 | {"native_updateDcSettings", "()V", (void *) updateDcSettings}, |
| 254 | {"native_setUseIpv6", "(Z)V", (void *) setUseIpv6}, |
| 255 | {"native_setNetworkAvailable", "(Z)V", (void *) setNetworkAvailable}, |
| 256 | {"native_setPushConnectionEnabled", "(Z)V", (void *) setPushConnectionEnabled}, |
| 257 | {"native_setJava", "(Z)V", (void *) setJava} |
| 258 | }; |
| 259 |
| 260 | inline int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int methodsCount) { |
| 261 | jclass clazz; |
| 262 | clazz = env->FindClass(className); |
| 263 | if (clazz == NULL) { |
| 264 | return JNI_FALSE; |
| 265 | } |
| 266 | if (env->RegisterNatives(clazz, methods, methodsCount) < 0) { |
| 267 | return JNI_FALSE; |
| 268 | } |
| 269 | return JNI_TRUE; |
| 270 | } |
| 271 |
| 272 | extern "C" int registerNativeTgNetFunctions(JavaVM *vm, JNIEnv *env) { |
| 273 | java = vm; |
| 274 |
| 275 | if (!registerNativeMethods(env, NativeByteBufferClassPathName, NativeByteBufferMethods, sizeof(NativeByteBufferMethods) / sizeof(NativeByteBufferMethods[0]))) { |
| 276 | return JNI_FALSE; |
| 277 | } |
| 278 |
| 279 | if (!registerNativeMethods(env, ConnectionsManagerClassPathName, ConnectionsManagerMethods, sizeof(ConnectionsManagerMethods) / sizeof(ConnectionsManagerMethods[0]))) { |
| 280 | return JNI_FALSE; |
| 281 | } |
| 282 |
| 283 | jclass_RequestDelegateInternal = (jclass) env->NewGlobalRef(env->FindClass("org/telegram/tgnet/RequestDelegateInternal")); |
| 284 | if (jclass_RequestDelegateInternal == 0) { |
| 285 | return JNI_FALSE; |
| 286 | } |
| 287 | jclass_RequestDelegateInternal_run = env->GetMethodID(jclass_RequestDelegateInternal, "run", "(IILjava/lang/String;)V"); |
| 288 | if (jclass_RequestDelegateInternal_run == 0) { |
| 289 | return JNI_FALSE; |
| 290 | } |
| 291 |
| 292 | jclass_QuickAckDelegate = (jclass) env->NewGlobalRef(env->FindClass("org/telegram/tgnet/QuickAckDelegate")); |
| 293 | if (jclass_RequestDelegateInternal == 0) { |
| 294 | return JNI_FALSE; |
| 295 | } |
| 296 | jclass_QuickAckDelegate_run = env->GetMethodID(jclass_QuickAckDelegate, "run", "()V"); |
| 297 | if (jclass_QuickAckDelegate_run == 0) { |
| 298 | return JNI_FALSE; |
| 299 | } |
| 300 |
| 301 | jclass_ConnectionsManager = (jclass) env->NewGlobalRef(env->FindClass("org/telegram/tgnet/ConnectionsManager")); |
| 302 | if (jclass_ConnectionsManager == 0) { |
| 303 | return JNI_FALSE; |
| 304 | } |
| 305 | jclass_ConnectionsManager_onUnparsedMessageReceived = env->GetStaticMethodID(jclass_ConnectionsManager, "onUnparsedMessageReceived", "(I)V"); |
| 306 | if (jclass_ConnectionsManager_onUnparsedMessageReceived == 0) { |
| 307 | return JNI_FALSE; |
| 308 | } |
| 309 | jclass_ConnectionsManager_onUpdate = env->GetStaticMethodID(jclass_ConnectionsManager, "onUpdate", "()V"); |
| 310 | if (jclass_ConnectionsManager_onUpdate == 0) { |
| 311 | return JNI_FALSE; |
| 312 | } |
| 313 | jclass_ConnectionsManager_onSessionCreated = env->GetStaticMethodID(jclass_ConnectionsManager, "onSessionCreated", "()V"); |
| 314 | if (jclass_ConnectionsManager_onSessionCreated == 0) { |
| 315 | return JNI_FALSE; |
| 316 | } |
| 317 | jclass_ConnectionsManager_onLogout = env->GetStaticMethodID(jclass_ConnectionsManager, "onLogout", "()V"); |
| 318 | if (jclass_ConnectionsManager_onLogout == 0) { |
| 319 | return JNI_FALSE; |
| 320 | } |
| 321 | jclass_ConnectionsManager_onConnectionStateChanged = env->GetStaticMethodID(jclass_ConnectionsManager, "onConnectionStateChanged", "(I)V"); |
| 322 | if (jclass_ConnectionsManager_onConnectionStateChanged == 0) { |
| 323 | return JNI_FALSE; |
| 324 | } |
| 325 | jclass_ConnectionsManager_onInternalPushReceived = env->GetStaticMethodID(jclass_ConnectionsManager, "onInternalPushReceived", "()V"); |
| 326 | if (jclass_ConnectionsManager_onInternalPushReceived == 0) { |
| 327 | return JNI_FALSE; |
| 328 | } |
| 329 | jclass_ConnectionsManager_onUpdateConfig = env->GetStaticMethodID(jclass_ConnectionsManager, "onUpdateConfig", "(I)V"); |
| 330 | if (jclass_ConnectionsManager_onUpdateConfig == 0) { |
| 331 | return JNI_FALSE; |
| 332 | } |
| 333 | ConnectionsManager::getInstance().setDelegate(new Delegate()); |
| 334 |
| 335 | return JNI_TRUE; |
| 336 | } |
| 337 |