diff --git a/CMakeLists.txt b/CMakeLists.txt
index a764f3d3d..d730861cd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -813,9 +813,9 @@ target_include_directories(tdcore SYSTEM PRIVATE ${OPENSSL_INCLUDE_DIR})
target_link_libraries(tdcore PUBLIC tdapi tdactor tdutils tdnet tddb PRIVATE ${OPENSSL_CRYPTO_LIBRARY} ${CMAKE_DL_LIBS} ${ZLIB_LIBRARIES})
if (WIN32)
if (MINGW)
- target_link_libraries(tdcore PRIVATE ws2_32 mswsock)
+ target_link_libraries(tdcore PRIVATE ws2_32 mswsock crypt32)
else()
- target_link_libraries(tdcore PRIVATE ws2_32 Mswsock)
+ target_link_libraries(tdcore PRIVATE ws2_32 Mswsock Crypt32)
endif()
endif()
diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt
index e175ee414..78bb649f5 100644
--- a/benchmark/CMakeLists.txt
+++ b/benchmark/CMakeLists.txt
@@ -10,9 +10,9 @@ add_executable(bench_crypto bench_crypto.cpp)
target_link_libraries(bench_crypto PRIVATE tdutils ${OPENSSL_CRYPTO_LIBRARY} ${CMAKE_DL_LIBS} ${ZLIB_LIBRARIES})
if (WIN32)
if (MINGW)
- target_link_libraries(bench_crypto PRIVATE ws2_32 mswsock)
+ target_link_libraries(bench_crypto PRIVATE ws2_32 mswsock crypt32)
else()
- target_link_libraries(bench_crypto PRIVATE ws2_32 Mswsock)
+ target_link_libraries(bench_crypto PRIVATE ws2_32 Mswsock Crypt32)
endif()
endif()
target_include_directories(bench_crypto SYSTEM PRIVATE ${OPENSSL_INCLUDE_DIR})
diff --git a/build.html b/build.html
index cc4910b6b..289fd2823 100644
--- a/build.html
+++ b/build.html
@@ -28,6 +28,7 @@ select.large { font-size: large; }
+
@@ -202,6 +203,8 @@ function getExampleAnchor(language) {
case 'Elixir':
case 'C':
return language.toLowerCase();
+ case 'Object Pascal':
+ return 'object-pascal';
case 'C#':
return 'csharp';
case 'C++':
diff --git a/example/README.md b/example/README.md
index 44090b565..d9081f9b8 100644
--- a/example/README.md
+++ b/example/README.md
@@ -18,6 +18,7 @@ Choose your preferred programming language to see examples of usage and a detail
- [C++](#cxx)
- [Swift](#swift)
- [Objective-C](#objective-c)
+- [Object Pascal](#object-pascal)
- [Dart](#dart)
- [Rust](#rust)
- [Erlang](#erlang)
@@ -135,6 +136,15 @@ TDLib can be used from the Objective-C programming language through [JSON](https
See [example/ios](https://github.com/tdlib/td/tree/master/example/ios) for an example of building TDLib for iOS, watchOS, tvOS, and macOS.
+
+## Using TDLib in Object Pascal projects with Delphi and Lazarus
+
+TDLib can be used from the Object Pascal programming language through the [JSON](https://github.com/tdlib/td#using-json).
+
+See [tdlib-delphi](https://github.com/dieletro/tdlib-delphi) for an example of TDLib usage from Delphi.
+
+See [tdlib-lazarus](https://github.com/dieletro/tdlib-lazarus) for an example of TDLib usage from Lazarus.
+
## Using TDLib in Dart projects
diff --git a/example/cpp/tdjson_example.cpp b/example/cpp/tdjson_example.cpp
index c22320ee8..5d69c4e21 100644
--- a/example/cpp/tdjson_example.cpp
+++ b/example/cpp/tdjson_example.cpp
@@ -13,32 +13,32 @@
int main() {
// disable TDLib logging
- td_json_client_execute(nullptr, "{\"@type\":\"setLogVerbosityLevel\", \"new_verbosity_level\":0}");
+ td_execute("{\"@type\":\"setLogVerbosityLevel\", \"new_verbosity_level\":0}");
- void *client = td_json_client_create();
- // somehow share the client with other threads, which will be able to send requests via td_json_client_send
+ int client_id = td_create_client();
+ // somehow share the client_id with other threads, which will be able to send requests via td_send
const bool test_incorrect_queries = false;
if (test_incorrect_queries) {
- td_json_client_execute(nullptr, "{\"@type\":\"setLogVerbosityLevel\", \"new_verbosity_level\":3}");
- td_json_client_execute(nullptr, "");
- td_json_client_execute(nullptr, "test");
- td_json_client_execute(nullptr, "\"test\"");
- td_json_client_execute(nullptr, "{\"@type\":\"test\", \"@extra\":1}");
+ td_execute("{\"@type\":\"setLogVerbosityLevel\", \"new_verbosity_level\":1}");
+ td_execute("");
+ td_execute("test");
+ td_execute("\"test\"");
+ td_execute("{\"@type\":\"test\", \"@extra\":1}");
- td_json_client_send(client, "{\"@type\":\"getFileMimeType\"}");
- td_json_client_send(client, "{\"@type\":\"getFileMimeType\", \"@extra\":1}");
- td_json_client_send(client, "{\"@type\":\"getFileMimeType\", \"@extra\":null}");
- td_json_client_send(client, "{\"@type\":\"test\"}");
- td_json_client_send(client, "[]");
- td_json_client_send(client, "{\"@type\":\"test\", \"@extra\":1}");
- td_json_client_send(client, "{\"@type\":\"sendMessage\", \"chat_id\":true, \"@extra\":1}");
- td_json_client_send(client, "test");
+ td_send(client_id, "{\"@type\":\"getFileMimeType\"}");
+ td_send(client_id, "{\"@type\":\"getFileMimeType\", \"@extra\":1}");
+ td_send(client_id, "{\"@type\":\"getFileMimeType\", \"@extra\":null}");
+ td_send(client_id, "{\"@type\":\"test\"}");
+ td_send(client_id, "[]");
+ td_send(client_id, "{\"@type\":\"test\", \"@extra\":1}");
+ td_send(client_id, "{\"@type\":\"sendMessage\", \"chat_id\":true, \"@extra\":1}");
+ td_send(client_id, "test");
}
const double WAIT_TIMEOUT = 10.0; // seconds
while (true) {
- const char *result = td_json_client_receive(client, WAIT_TIMEOUT);
+ const char *result = td_receive(WAIT_TIMEOUT);
if (result != nullptr) {
// parse the result as a JSON object and process it as an incoming update or an answer to a previously sent request
@@ -49,6 +49,4 @@ int main() {
std::cout << result << std::endl;
}
}
-
- td_json_client_destroy(client);
}
diff --git a/example/csharp/TdExample.cs b/example/csharp/TdExample.cs
index eb5c0c0f5..7c5b4d255 100644
--- a/example/csharp/TdExample.cs
+++ b/example/csharp/TdExample.cs
@@ -23,7 +23,8 @@ namespace TdExample
private static TdApi.AuthorizationState _authorizationState = null;
private static volatile bool _haveAuthorization = false;
- private static volatile bool _quiting = false;
+ private static volatile bool _needQuit = false;
+ private static volatile bool _canQuit = false;
private static volatile AutoResetEvent _gotAuthorization = new AutoResetEvent(false);
@@ -133,9 +134,11 @@ namespace TdExample
{
Print("Closed");
_client.Dispose(); // _client is closed and native resources can be disposed now
- if (!_quiting)
+ if (!_needQuit)
{
_client = CreateTdClient(); // recreate _client after previous has closed
+ } else {
+ _canQuit = true;
}
}
else
@@ -187,7 +190,7 @@ namespace TdExample
_client.Send(new TdApi.Close(), _defaultHandler);
break;
case "q":
- _quiting = true;
+ _needQuit = true;
_haveAuthorization = false;
_client.Send(new TdApi.Close(), _defaultHandler);
break;
@@ -228,7 +231,7 @@ namespace TdExample
_defaultHandler.OnResult(Td.Client.Execute(new TdApi.GetTextEntities("@telegram /test_command https://telegram.org telegram.me @gif @test")));
// main loop
- while (!_quiting)
+ while (!_needQuit)
{
// await authorization
_gotAuthorization.Reset();
@@ -240,6 +243,9 @@ namespace TdExample
GetCommand();
}
}
+ while (!_canQuit) {
+ Thread.Sleep(1);
+ }
}
private class DefaultHandler : Td.ClientResultHandler
diff --git a/example/java/org/drinkless/tdlib/Client.java b/example/java/org/drinkless/tdlib/Client.java
index 986d5b3c7..d95196f17 100644
--- a/example/java/org/drinkless/tdlib/Client.java
+++ b/example/java/org/drinkless/tdlib/Client.java
@@ -8,14 +8,11 @@ package org.drinkless.tdlib;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Main class for interaction with the TDLib.
*/
-public final class Client implements Runnable {
+public final class Client {
/**
* Interface for handler for results of queries to TDLib and incoming updates from TDLib.
*/
@@ -55,25 +52,11 @@ public final class Client implements Runnable {
* @throws NullPointerException if query is null.
*/
public void send(TdApi.Function query, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
- if (query == null) {
- throw new NullPointerException("query is null");
- }
-
- readLock.lock();
- try {
- if (isClientDestroyed) {
- if (resultHandler != null) {
- handleResult(new TdApi.Error(500, "Client is closed"), resultHandler, exceptionHandler);
- }
- return;
- }
-
- long queryId = currentQueryId.incrementAndGet();
+ long queryId = currentQueryId.incrementAndGet();
+ if (resultHandler != null) {
handlers.put(queryId, new Handler(resultHandler, exceptionHandler));
- nativeClientSend(nativeClientId, queryId, query);
- } finally {
- readLock.unlock();
}
+ nativeClientSend(nativeClientId, queryId, query);
}
/**
@@ -97,22 +80,9 @@ public final class Client implements Runnable {
* @throws NullPointerException if query is null.
*/
public static TdApi.Object execute(TdApi.Function query) {
- if (query == null) {
- throw new NullPointerException("query is null");
- }
return nativeClientExecute(query);
}
- /**
- * Overridden method from Runnable, do not call it directly.
- */
- @Override
- public void run() {
- while (!stopFlag) {
- receiveQueries(300.0 /*seconds*/);
- }
- }
-
/**
* Creates new Client.
*
@@ -123,54 +93,79 @@ public final class Client implements Runnable {
*/
public static Client create(ResultHandler updateHandler, ExceptionHandler updateExceptionHandler, ExceptionHandler defaultExceptionHandler) {
Client client = new Client(updateHandler, updateExceptionHandler, defaultExceptionHandler);
- new Thread(client, "TDLib thread").start();
+ synchronized (responseReceiver) {
+ if (!responseReceiver.isRun) {
+ responseReceiver.isRun = true;
+
+ Thread receiverThread = new Thread(responseReceiver, "TDLib thread");
+ receiverThread.setDaemon(true);
+ receiverThread.start();
+ }
+ }
return client;
}
- /**
- * Closes Client.
- */
- public void close() {
- writeLock.lock();
- try {
- if (isClientDestroyed) {
- return;
+ private static class ResponseReceiver implements Runnable {
+ public boolean isRun = false;
+
+ @Override
+ public void run() {
+ while (true) {
+ int resultN = nativeClientReceive(clientIds, eventIds, events, 100000.0 /*seconds*/);
+ for (int i = 0; i < resultN; i++) {
+ processResult(clientIds[i], eventIds[i], events[i]);
+ events[i] = null;
+ }
}
- if (!stopFlag) {
- send(new TdApi.Close(), null);
- }
- isClientDestroyed = true;
- while (!stopFlag) {
- Thread.yield();
- }
- while (!handlers.isEmpty()) {
- receiveQueries(300.0);
- }
- updateHandlers.remove(nativeClientId);
- defaultExceptionHandlers.remove(nativeClientId);
- destroyNativeClient(nativeClientId);
- } finally {
- writeLock.unlock();
}
+
+ private void processResult(int clientId, long id, TdApi.Object object) {
+ boolean isClosed = false;
+ if (id == 0 && object instanceof TdApi.UpdateAuthorizationState) {
+ TdApi.AuthorizationState authorizationState = ((TdApi.UpdateAuthorizationState) object).authorizationState;
+ if (authorizationState instanceof TdApi.AuthorizationStateClosed) {
+ isClosed = true;
+ }
+ }
+
+ Handler handler = id == 0 ? updateHandlers.get(clientId) : handlers.remove(id);
+ if (handler != null) {
+ try {
+ handler.resultHandler.onResult(object);
+ } catch (Throwable cause) {
+ ExceptionHandler exceptionHandler = handler.exceptionHandler;
+ if (exceptionHandler == null) {
+ exceptionHandler = defaultExceptionHandlers.get(clientId);
+ }
+ if (exceptionHandler != null) {
+ try {
+ exceptionHandler.onException(cause);
+ } catch (Throwable ignored) {
+ }
+ }
+ }
+ }
+
+ if (isClosed) {
+ updateHandlers.remove(clientId); // there will be no more updates
+ defaultExceptionHandlers.remove(clientId); // ignore further exceptions
+ }
+ }
+
+ private static final int MAX_EVENTS = 1000;
+ private final int[] clientIds = new int[MAX_EVENTS];
+ private final long[] eventIds = new long[MAX_EVENTS];
+ private final TdApi.Object[] events = new TdApi.Object[MAX_EVENTS];
}
- private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
- private final Lock readLock = readWriteLock.readLock();
- private final Lock writeLock = readWriteLock.writeLock();
+ private final int nativeClientId;
- private volatile boolean stopFlag = false;
- private volatile boolean isClientDestroyed = false;
- private final long nativeClientId;
+ private static final ConcurrentHashMap defaultExceptionHandlers = new ConcurrentHashMap();
+ private static final ConcurrentHashMap updateHandlers = new ConcurrentHashMap();
+ private static final ConcurrentHashMap handlers = new ConcurrentHashMap();
+ private static final AtomicLong currentQueryId = new AtomicLong();
- private static final ConcurrentHashMap defaultExceptionHandlers = new ConcurrentHashMap();
- private static final ConcurrentHashMap updateHandlers = new ConcurrentHashMap();
-
- private final ConcurrentHashMap handlers = new ConcurrentHashMap();
- private final AtomicLong currentQueryId = new AtomicLong();
-
- private static final int MAX_EVENTS = 1000;
- private final long[] eventIds = new long[MAX_EVENTS];
- private final TdApi.Object[] events = new TdApi.Object[MAX_EVENTS];
+ private static final ResponseReceiver responseReceiver = new ResponseReceiver();
private static class Handler {
final ResultHandler resultHandler;
@@ -184,76 +179,24 @@ public final class Client implements Runnable {
private Client(ResultHandler updateHandler, ExceptionHandler updateExceptionHandler, ExceptionHandler defaultExceptionHandler) {
nativeClientId = createNativeClient();
- updateHandlers.put(nativeClientId, new Handler(updateHandler, updateExceptionHandler));
+ if (updateHandler != null) {
+ updateHandlers.put(nativeClientId, new Handler(updateHandler, updateExceptionHandler));
+ }
if (defaultExceptionHandler != null) {
- defaultExceptionHandlers.put(nativeClientId, defaultExceptionHandler);
+ defaultExceptionHandlers.put(nativeClientId, defaultExceptionHandler);
}
}
@Override
protected void finalize() throws Throwable {
- try {
- close();
- } finally {
- super.finalize();
- }
+ send(new TdApi.Close(), null, null);
}
- private void processResult(long id, TdApi.Object object) {
- if (object instanceof TdApi.UpdateAuthorizationState) {
- if (((TdApi.UpdateAuthorizationState) object).authorizationState instanceof TdApi.AuthorizationStateClosed) {
- stopFlag = true;
- }
- }
- Handler handler;
- if (id == 0) {
- // update handler stays forever
- handler = updateHandlers.get(nativeClientId);
- } else {
- handler = handlers.remove(id);
- }
- if (handler == null) {
- return;
- }
+ private static native int createNativeClient();
- handleResult(object, handler.resultHandler, handler.exceptionHandler);
- }
+ private static native void nativeClientSend(int nativeClientId, long eventId, TdApi.Function function);
- private void handleResult(TdApi.Object object, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
- if (resultHandler == null) {
- return;
- }
-
- try {
- resultHandler.onResult(object);
- } catch (Throwable cause) {
- if (exceptionHandler == null) {
- exceptionHandler = defaultExceptionHandlers.get(nativeClientId);
- }
- if (exceptionHandler != null) {
- try {
- exceptionHandler.onException(cause);
- } catch (Throwable ignored) {
- }
- }
- }
- }
-
- private void receiveQueries(double timeout) {
- int resultN = nativeClientReceive(nativeClientId, eventIds, events, timeout);
- for (int i = 0; i < resultN; i++) {
- processResult(eventIds[i], events[i]);
- events[i] = null;
- }
- }
-
- private static native long createNativeClient();
-
- private static native void nativeClientSend(long nativeClientId, long eventId, TdApi.Function function);
-
- private static native int nativeClientReceive(long nativeClientId, long[] eventIds, TdApi.Object[] events, double timeout);
+ private static native int nativeClientReceive(int[] clientIds, long[] eventIds, TdApi.Object[] events, double timeout);
private static native TdApi.Object nativeClientExecute(TdApi.Function function);
-
- private static native void destroyNativeClient(long nativeClientId);
}
diff --git a/example/java/org/drinkless/tdlib/example/Example.java b/example/java/org/drinkless/tdlib/example/Example.java
index 8e52fda92..7c2e5649e 100644
--- a/example/java/org/drinkless/tdlib/example/Example.java
+++ b/example/java/org/drinkless/tdlib/example/Example.java
@@ -29,7 +29,8 @@ public final class Example {
private static TdApi.AuthorizationState authorizationState = null;
private static volatile boolean haveAuthorization = false;
- private static volatile boolean quiting = false;
+ private static volatile boolean needQuit = false;
+ private static volatile boolean canQuit = false;
private static final Client.ResultHandler defaultHandler = new DefaultHandler();
@@ -160,8 +161,10 @@ public final class Example {
break;
case TdApi.AuthorizationStateClosed.CONSTRUCTOR:
print("Closed");
- if (!quiting) {
+ if (!needQuit) {
client = Client.create(new UpdateHandler(), null, null); // recreate client after previous has closed
+ } else {
+ canQuit = true;
}
break;
default:
@@ -230,7 +233,7 @@ public final class Example {
client.send(new TdApi.LogOut(), defaultHandler);
break;
case "q":
- quiting = true;
+ needQuit = true;
haveAuthorization = false;
client.send(new TdApi.Close(), defaultHandler);
break;
@@ -316,7 +319,7 @@ public final class Example {
defaultHandler.onResult(Client.execute(new TdApi.GetTextEntities("@telegram /test_command https://telegram.org telegram.me @gif @test")));
// main loop
- while (!quiting) {
+ while (!needQuit) {
// await authorization
authorizationLock.lock();
try {
@@ -331,6 +334,9 @@ public final class Example {
getCommand();
}
}
+ while (!canQuit) {
+ Thread.sleep(1);
+ }
}
private static class OrderedChat implements Comparable {
diff --git a/example/java/td_jni.cpp b/example/java/td_jni.cpp
index 34ad9c836..e0106e258 100644
--- a/example/java/td_jni.cpp
+++ b/example/java/td_jni.cpp
@@ -26,31 +26,35 @@ static td::td_api::object_ptr fetch_function(JNIEnv *env,
return result;
}
-static td::Client *get_client(jlong client_id) {
- return reinterpret_cast(static_cast(client_id));
+static td::ClientManager *get_manager() {
+ return td::ClientManager::get_manager_singleton();
}
-static jlong Client_createNativeClient(JNIEnv *env, jclass clazz) {
- return static_cast(reinterpret_cast(new td::Client()));
+static jint Client_createNativeClient(JNIEnv *env, jclass clazz) {
+ return static_cast(get_manager()->create_client());
}
-static void Client_nativeClientSend(JNIEnv *env, jclass clazz, jlong client_id, jlong id, jobject function) {
- get_client(client_id)->send({static_cast(id), fetch_function(env, function)});
+static void Client_nativeClientSend(JNIEnv *env, jclass clazz, jint client_id, jlong id, jobject function) {
+ get_manager()->send(static_cast(client_id), static_cast(id),
+ fetch_function(env, function));
}
-static jint Client_nativeClientReceive(JNIEnv *env, jclass clazz, jlong client_id, jlongArray ids, jobjectArray events,
- jdouble timeout) {
- auto client = get_client(client_id);
- jsize events_size = env->GetArrayLength(ids); // ids and events size must be of equal size
+static jint Client_nativeClientReceive(JNIEnv *env, jclass clazz, jintArray client_ids, jlongArray ids,
+ jobjectArray events, jdouble timeout) {
+ jsize events_size = env->GetArrayLength(ids); // client_ids, ids and events must be of equal size
if (events_size == 0) {
return 0;
}
jsize result_size = 0;
- auto response = client->receive(timeout);
+ auto *manager = get_manager();
+ auto response = manager->receive(timeout);
while (response.object) {
- jlong result_id = static_cast(response.id);
- env->SetLongArrayRegion(ids, result_size, 1, &result_id);
+ jint client_id = static_cast(response.client_id);
+ env->SetIntArrayRegion(client_ids, result_size, 1, &client_id);
+
+ jlong request_id = static_cast(response.request_id);
+ env->SetLongArrayRegion(ids, result_size, 1, &request_id);
jobject object;
response.object->store(env, object);
@@ -62,21 +66,17 @@ static jint Client_nativeClientReceive(JNIEnv *env, jclass clazz, jlong client_i
break;
}
- response = client->receive(0);
+ response = manager->receive(0);
}
return result_size;
}
static jobject Client_nativeClientExecute(JNIEnv *env, jclass clazz, jobject function) {
jobject result;
- td::Client::execute({0, fetch_function(env, function)}).object->store(env, result);
+ td::ClientManager::execute(fetch_function(env, function))->store(env, result);
return result;
}
-static void Client_destroyNativeClient(JNIEnv *env, jclass clazz, jlong client_id) {
- delete get_client(client_id);
-}
-
static void Log_setVerbosityLevel(JNIEnv *env, jclass clazz, jint new_log_verbosity_level) {
td::Log::set_verbosity_level(static_cast(new_log_verbosity_level));
}
@@ -103,8 +103,11 @@ static jclass log_class;
static void on_fatal_error(const char *error_message) {
auto env = td::jni::get_jni_env(java_vm, JAVA_VERSION);
+ if (env == nullptr) {
+ return;
+ }
jmethodID on_fatal_error_method = env->GetStaticMethodID(log_class, "onFatalError", "(Ljava/lang/String;)V");
- if (env && on_fatal_error_method) {
+ if (on_fatal_error_method) {
jstring error_str = td::jni::to_jstring(env.get(), error_message);
env->CallStaticVoidMethod(log_class, on_fatal_error_method, error_str);
if (error_str) {
@@ -133,11 +136,10 @@ static jint register_native(JavaVM *vm) {
#define TD_OBJECT "L" PACKAGE_NAME "/TdApi$Object;"
#define TD_FUNCTION "L" PACKAGE_NAME "/TdApi$Function;"
- register_method(client_class, "createNativeClient", "()J", Client_createNativeClient);
- register_method(client_class, "nativeClientSend", "(JJ" TD_FUNCTION ")V", Client_nativeClientSend);
- register_method(client_class, "nativeClientReceive", "(J[J[" TD_OBJECT "D)I", Client_nativeClientReceive);
+ register_method(client_class, "createNativeClient", "()I", Client_createNativeClient);
+ register_method(client_class, "nativeClientSend", "(IJ" TD_FUNCTION ")V", Client_nativeClientSend);
+ register_method(client_class, "nativeClientReceive", "([I[J[" TD_OBJECT "D)I", Client_nativeClientReceive);
register_method(client_class, "nativeClientExecute", "(" TD_FUNCTION ")" TD_OBJECT, Client_nativeClientExecute);
- register_method(client_class, "destroyNativeClient", "(J)V", Client_destroyNativeClient);
register_method(log_class, "setVerbosityLevel", "(I)V", Log_setVerbosityLevel);
register_method(log_class, "setFilePath", "(Ljava/lang/String;)Z", Log_setFilePath);
diff --git a/example/web/README.md b/example/web/README.md
index 6f137ec9c..15a4964db 100644
--- a/example/web/README.md
+++ b/example/web/README.md
@@ -6,7 +6,7 @@ You need a Unix shell with `sed`, `tar` and `wget` utilities to run the provided
## Building tdweb NPM package
-* Install the 1.39.5 [emsdk](https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html), which is known to work. Do not use the system-provided `emscripten` package, because it contains a version that is too old.
+* Install the 2.0.6 [emsdk](https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html), which is known to work. Do not use the system-provided `emscripten` package, because it contains a too old emsdk version.
* Install all `TDLib` build dependencies as described in [Building](https://github.com/tdlib/td#building).
* Run `source ./emsdk_env.sh` from `emsdk` directory to set up the correct build environment.
* On `macOS`, install the `coreutils` [Homebrew](https://brew.sh) package and replace `realpath` in scripts with `grealpath`:
diff --git a/sqlite/CMakeLists.txt b/sqlite/CMakeLists.txt
index 0d90f3330..131facb48 100644
--- a/sqlite/CMakeLists.txt
+++ b/sqlite/CMakeLists.txt
@@ -22,9 +22,9 @@ target_include_directories(tdsqlite SYSTEM PRIVATE ${OPENSSL_INCLUDE_DIR})
target_link_libraries(tdsqlite PRIVATE ${OPENSSL_CRYPTO_LIBRARY} ${CMAKE_DL_LIBS} ${ZLIB_LIBRARIES})
if (WIN32)
if (MINGW)
- target_link_libraries(tdsqlite PRIVATE ws2_32 mswsock)
+ target_link_libraries(tdsqlite PRIVATE ws2_32 mswsock crypt32)
else()
- target_link_libraries(tdsqlite PRIVATE ws2_32 Mswsock)
+ target_link_libraries(tdsqlite PRIVATE ws2_32 Mswsock Crypt32)
endif()
endif()
diff --git a/td/telegram/Client.cpp b/td/telegram/Client.cpp
index 0de42e12f..474bb8868 100644
--- a/td/telegram/Client.cpp
+++ b/td/telegram/Client.cpp
@@ -13,6 +13,7 @@
#include "td/utils/common.h"
#include "td/utils/crypto.h"
+#include "td/utils/ExitGuard.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/MpscPollableQueue.h"
@@ -203,10 +204,12 @@ class ClientManager::Impl final {
td.second.reset();
}
}
- while (!tds_.empty()) {
- receive(10, false, true);
+ while (!tds_.empty() && !ExitGuard::is_exited()) {
+ receive(0.1, false, true);
+ }
+ if (!ExitGuard::is_exited()) { // prevent closing of schedulers from already killed by OS threads
+ concurrent_scheduler_->finish();
}
- concurrent_scheduler_->finish();
}
private:
@@ -475,7 +478,9 @@ class MultiImpl {
Scheduler::instance()->finish();
}
scheduler_thread_.join();
- concurrent_scheduler_->finish();
+ if (!ExitGuard::is_exited()) { // prevent closing of schedulers from already killed by OS threads
+ concurrent_scheduler_->finish();
+ }
}
private:
@@ -618,11 +623,14 @@ class ClientManager::Impl final {
Impl(Impl &&) = delete;
Impl &operator=(Impl &&) = delete;
~Impl() {
+ if (ExitGuard::is_exited()) {
+ return;
+ }
for (auto &it : impls_) {
close_impl(it.first);
}
- while (!impls_.empty()) {
- receive(10, false, true);
+ while (!impls_.empty() && !ExitGuard::is_exited()) {
+ receive(0.1, false, true);
}
}
@@ -673,8 +681,8 @@ class Client::Impl final {
Impl &operator=(Impl &&) = delete;
~Impl() {
multi_impl_->close(td_id_);
- while (true) {
- auto response = receiver_.receive(10.0, false, true);
+ while (!ExitGuard::is_exited()) {
+ auto response = receiver_.receive(0.1, false, true);
if (response.object == nullptr && response.client_id != 0 && response.request_id == 0) {
break;
}
@@ -742,4 +750,10 @@ ClientManager::~ClientManager() = default;
ClientManager::ClientManager(ClientManager &&other) = default;
ClientManager &ClientManager::operator=(ClientManager &&other) = default;
+ClientManager *ClientManager::get_manager_singleton() {
+ static ClientManager client_manager;
+ static ExitGuard exit_guard;
+ return &client_manager;
+}
+
} // namespace td
diff --git a/td/telegram/Client.h b/td/telegram/Client.h
index 8254d0cd8..34eecfb79 100644
--- a/td/telegram/Client.h
+++ b/td/telegram/Client.h
@@ -260,6 +260,12 @@ class ClientManager final {
*/
ClientManager &operator=(ClientManager &&other);
+ /**
+ * Returns a pointer to a singleton ClientManager instance.
+ * \return A unique singleton ClientManager instance.
+ */
+ static ClientManager *get_manager_singleton();
+
private:
friend class Client;
class Impl;
diff --git a/td/telegram/ClientJson.cpp b/td/telegram/ClientJson.cpp
index d5beab12d..80ef8450b 100644
--- a/td/telegram/ClientJson.cpp
+++ b/td/telegram/ClientJson.cpp
@@ -115,8 +115,7 @@ const char *ClientJson::execute(Slice request) {
}
static ClientManager *get_manager() {
- static ClientManager client_manager;
- return &client_manager;
+ return ClientManager::get_manager_singleton();
}
static std::mutex extra_mutex;
diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp
index a9849bd37..49709f40d 100644
--- a/td/telegram/ContactsManager.cpp
+++ b/td/telegram/ContactsManager.cpp
@@ -12825,6 +12825,14 @@ bool ContactsManager::get_chat_is_active(ChatId chat_id) const {
return c->is_active;
}
+ChannelId ContactsManager::get_chat_migrated_to_channel_id(ChatId chat_id) const {
+ auto c = get_chat(chat_id);
+ if (c == nullptr) {
+ return ChannelId();
+ }
+ return c->migrated_to_channel_id;
+}
+
DialogParticipantStatus ContactsManager::get_chat_status(ChatId chat_id) const {
auto c = get_chat(chat_id);
if (c == nullptr) {
diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h
index 665948167..74b075194 100644
--- a/td/telegram/ContactsManager.h
+++ b/td/telegram/ContactsManager.h
@@ -467,6 +467,7 @@ class ContactsManager : public Actor {
void reload_chat_full(ChatId chat_id, Promise &&promise);
bool get_chat_is_active(ChatId chat_id) const;
+ ChannelId get_chat_migrated_to_channel_id(ChatId chat_id) const;
DialogParticipantStatus get_chat_status(ChatId chat_id) const;
DialogParticipantStatus get_chat_permissions(ChatId chat_id) const;
bool is_appointed_chat_administrator(ChatId chat_id) const;
diff --git a/td/telegram/Logging.cpp b/td/telegram/Logging.cpp
index 8694ab498..131303a67 100644
--- a/td/telegram/Logging.cpp
+++ b/td/telegram/Logging.cpp
@@ -29,6 +29,7 @@
#include "td/actor/actor.h"
+#include "td/utils/ExitGuard.h"
#include "td/utils/FileLog.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
@@ -44,6 +45,7 @@ static std::mutex logging_mutex;
static FileLog file_log;
static TsLog ts_log(&file_log);
static NullLog null_log;
+static ExitGuard exit_guard;
#define ADD_TAG(tag) \
{ #tag, &VERBOSITY_NAME(tag) }
diff --git a/td/telegram/MessageReplyInfo.h b/td/telegram/MessageReplyInfo.h
index d1c3a75da..0c1b4e8d9 100644
--- a/td/telegram/MessageReplyInfo.h
+++ b/td/telegram/MessageReplyInfo.h
@@ -9,7 +9,7 @@
#include "td/telegram/ChannelId.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/MessageId.h"
-#include "Td/telegram/td_api.h"
+#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/utils/common.h"
diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp
index e6404c61c..4ba626ae1 100644
--- a/td/telegram/MessagesManager.cpp
+++ b/td/telegram/MessagesManager.cpp
@@ -15424,6 +15424,9 @@ std::pair> MessagesManager::search_dialogs(const string
}
promise.set_value(Unit());
+
+ update_recently_found_dialogs();
+
size_t result_size = min(static_cast(limit), recently_found_dialog_ids_.size());
return {narrow_cast(recently_found_dialog_ids_.size()),
vector(recently_found_dialog_ids_.begin(), recently_found_dialog_ids_.begin() + result_size)};
@@ -23734,6 +23737,26 @@ bool MessagesManager::is_broadcast_channel(DialogId dialog_id) const {
return td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == ChannelType::Broadcast;
}
+bool MessagesManager::is_deleted_secret_chat(const Dialog *d) const {
+ if (d == nullptr) {
+ return true;
+ }
+ if (d->dialog_id.get_type() != DialogType::SecretChat) {
+ return false;
+ }
+
+ if (d->order != DEFAULT_ORDER || d->messages != nullptr) {
+ return false;
+ }
+
+ auto state = td_->contacts_manager_->get_secret_chat_state(d->dialog_id.get_secret_chat_id());
+ if (state != SecretChatState::Closed) {
+ return false;
+ }
+
+ return true;
+}
+
int32 MessagesManager::get_message_schedule_date(const Message *m) {
if (!m->message_id.is_scheduled()) {
return 0;
@@ -28934,7 +28957,7 @@ void MessagesManager::send_dialog_action(DialogId dialog_id, MessageId top_threa
auto &query_ref = set_typing_query_[dialog_id];
if (!query_ref.empty() && !td_->auth_manager_->is_bot()) {
- LOG(INFO) << "Cancel previous set typing query";
+ LOG(INFO) << "Cancel previous send chat action query";
cancel_query(query_ref);
}
query_ref = td_->create_handler(std::move(promise))
@@ -28942,7 +28965,7 @@ void MessagesManager::send_dialog_action(DialogId dialog_id, MessageId top_threa
}
void MessagesManager::on_send_dialog_action_timeout(DialogId dialog_id) {
- LOG(INFO) << "Receive send_dialog_action timeout in " << dialog_id;
+ LOG(INFO) << "Receive send_chat_action timeout in " << dialog_id;
Dialog *d = get_dialog(dialog_id);
CHECK(d != nullptr);
@@ -30778,7 +30801,7 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq
if (queue_id & 1) {
LOG(INFO) << "Add " << message_id << " from " << source << " to queue " << queue_id;
yet_unsent_media_queues_[queue_id][message_id.get()]; // reserve place for promise
- if (!td_->auth_manager_->is_bot() && !is_dialog_action_unneeded(dialog_id)) {
+ if (!td_->auth_manager_->is_bot()) {
pending_send_dialog_action_timeout_.add_timeout_in(dialog_id.get(), 1.0);
}
}
@@ -32890,11 +32913,8 @@ void MessagesManager::update_dialog_pos(Dialog *d, const char *source, bool need
}
}
if (dialog_type == DialogType::SecretChat) {
- auto secret_chat_id = d->dialog_id.get_secret_chat_id();
- auto date = td_->contacts_manager_->get_secret_chat_date(secret_chat_id);
- auto state = td_->contacts_manager_->get_secret_chat_state(secret_chat_id);
- // do not return removed from the chat list closed secret chats
- if (date != 0 && (d->order != DEFAULT_ORDER || state != SecretChatState::Closed || d->messages != nullptr)) {
+ auto date = td_->contacts_manager_->get_secret_chat_date(d->dialog_id.get_secret_chat_id());
+ if (date != 0 && !is_deleted_secret_chat(d)) {
LOG(INFO) << "Creation of secret chat at " << date << " found";
int64 creation_order = get_dialog_order(MessageId(), date);
if (creation_order > new_order) {
@@ -35195,6 +35215,7 @@ void MessagesManager::save_recently_found_dialogs() {
}
value += to_string(dialog_id.get());
}
+ LOG(DEBUG) << "Save recently found chats " << value;
G()->td_db()->get_binlog_pmc()->set("recently_found_dialog_usernames_and_ids", value);
}
@@ -35213,6 +35234,7 @@ bool MessagesManager::load_recently_found_dialogs(Promise &promise) {
return true;
}
+ LOG(DEBUG) << "Loaded recently found chats " << found_dialogs_str;
auto found_dialogs = full_split(found_dialogs_str, ',');
if (recently_found_dialogs_loaded_ == 1 && resolve_recently_found_dialogs_multipromise_.promise_count() == 0) {
// queries was sent and have already been finished
@@ -35318,7 +35340,7 @@ bool MessagesManager::add_recently_found_dialog_internal(DialogId dialog_id) {
// TODO create function
auto it = std::find(recently_found_dialog_ids_.begin(), recently_found_dialog_ids_.end(), dialog_id);
if (it == recently_found_dialog_ids_.end()) {
- if (narrow_cast(recently_found_dialog_ids_.size()) == MAX_RECENT_FOUND_DIALOGS) {
+ if (narrow_cast(recently_found_dialog_ids_.size()) == MAX_RECENTLY_FOUND_DIALOGS) {
CHECK(!recently_found_dialog_ids_.empty());
recently_found_dialog_ids_.back() = dialog_id;
} else {
@@ -35335,6 +35357,48 @@ bool MessagesManager::remove_recently_found_dialog_internal(DialogId dialog_id)
return td::remove(recently_found_dialog_ids_, dialog_id);
}
+void MessagesManager::update_recently_found_dialogs() {
+ vector dialog_ids;
+ for (auto dialog_id : recently_found_dialog_ids_) {
+ const Dialog *d = get_dialog(dialog_id);
+ if (d == nullptr) {
+ continue;
+ }
+ switch (dialog_id.get_type()) {
+ case DialogType::User:
+ // always keep
+ break;
+ case DialogType::Chat: {
+ auto channel_id = td_->contacts_manager_->get_chat_migrated_to_channel_id(dialog_id.get_chat_id());
+ if (channel_id.is_valid() && get_dialog(DialogId(channel_id)) != nullptr) {
+ dialog_id = DialogId(channel_id);
+ }
+ break;
+ }
+ case DialogType::Channel:
+ // always keep
+ break;
+ case DialogType::SecretChat:
+ if (is_deleted_secret_chat(d)) {
+ dialog_id = DialogId();
+ }
+ break;
+ case DialogType::None:
+ default:
+ UNREACHABLE();
+ break;
+ }
+ if (dialog_id.is_valid()) {
+ dialog_ids.push_back(dialog_id);
+ }
+ }
+
+ if (dialog_ids != recently_found_dialog_ids_) {
+ recently_found_dialog_ids_ = std::move(dialog_ids);
+ save_recently_found_dialogs();
+ }
+}
+
void MessagesManager::suffix_load_loop(Dialog *d) {
if (d->suffix_load_has_query_) {
return;
diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h
index 2b2fbb28e..6ee4299a4 100644
--- a/td/telegram/MessagesManager.h
+++ b/td/telegram/MessagesManager.h
@@ -1644,7 +1644,7 @@ class MessagesManager : public Actor {
static constexpr int32 MIN_CHANNEL_DIFFERENCE = 10;
static constexpr int32 MAX_CHANNEL_DIFFERENCE = 100;
static constexpr int32 MAX_BOT_CHANNEL_DIFFERENCE = 100000; // server side limit
- static constexpr int32 MAX_RECENT_FOUND_DIALOGS = 20; // some reasonable value
+ static constexpr int32 MAX_RECENTLY_FOUND_DIALOGS = 30; // some reasonable value
static constexpr size_t MAX_TITLE_LENGTH = 128; // server side limit for chat title
static constexpr size_t MAX_DESCRIPTION_LENGTH = 255; // server side limit for chat description
static constexpr size_t MAX_DIALOG_FILTER_TITLE_LENGTH = 12; // server side limit for dialog filter title
@@ -2831,7 +2831,10 @@ class MessagesManager : public Actor {
bool remove_recently_found_dialog_internal(DialogId dialog_id);
+ void update_recently_found_dialogs();
+
void save_recently_found_dialogs();
+
bool load_recently_found_dialogs(Promise &promise);
void reget_message_from_server_if_needed(DialogId dialog_id, const Message *m);
@@ -2916,6 +2919,8 @@ class MessagesManager : public Actor {
bool is_broadcast_channel(DialogId dialog_id) const;
+ bool is_deleted_secret_chat(const Dialog *d) const;
+
static int32 get_message_schedule_date(const Message *m);
int32 recently_found_dialogs_loaded_ = 0; // 0 - not loaded, 1 - load request was sent, 2 - loaded
diff --git a/td/telegram/Photo.cpp b/td/telegram/Photo.cpp
index 98a7dd30f..5672aa5d5 100644
--- a/td/telegram/Photo.cpp
+++ b/td/telegram/Photo.cpp
@@ -581,13 +581,16 @@ static vector> get_photo_sizes_object(File
auto sizes = transform(photo_sizes, [file_manager](const PhotoSize &photo_size) {
return get_photo_size_object(file_manager, &photo_size);
});
- std::sort(sizes.begin(), sizes.end(), [](const auto &lhs, const auto &rhs) {
+ std::stable_sort(sizes.begin(), sizes.end(), [](const auto &lhs, const auto &rhs) {
if (lhs->photo_->expected_size_ != rhs->photo_->expected_size_) {
return lhs->photo_->expected_size_ < rhs->photo_->expected_size_;
}
return static_cast(lhs->width_) * static_cast(lhs->height_) <
static_cast(rhs->width_) * static_cast(rhs->height_);
});
+ td::remove_if(sizes, [](const auto &size) {
+ return !size->photo_->local_->can_be_downloaded_ && !size->photo_->local_->is_downloading_completed_;
+ });
return sizes;
}
diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp
index 288232e45..cdd0b06b2 100644
--- a/td/telegram/cli.cpp
+++ b/td/telegram/cli.cpp
@@ -20,6 +20,7 @@
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/crypto.h"
+#include "td/utils/ExitGuard.h"
#include "td/utils/FileLog.h"
#include "td/utils/format.h"
#include "td/utils/JsonBuilder.h"
@@ -4450,6 +4451,8 @@ void main(int argc, char **argv) {
#ifndef _WIN32
Debug::DeathHandler dh;
#endif
+
+ ExitGuard exit_guard;
ignore_signal(SignalType::HangUp).ensure();
ignore_signal(SignalType::Pipe).ensure();
set_signal_handler(SignalType::Error, fail_signal).ensure();
diff --git a/td/tl/tl_jni_object.cpp b/td/tl/tl_jni_object.cpp
index 640635c32..31af7508d 100644
--- a/td/tl/tl_jni_object.cpp
+++ b/td/tl/tl_jni_object.cpp
@@ -85,7 +85,10 @@ std::unique_ptr get_jni_env(JavaVM *java_vm, jint jni
#else
auto p_env = &env;
#endif
- java_vm->AttachCurrentThread(p_env, nullptr);
+ if (java_vm->AttachCurrentThread(p_env, nullptr) != JNI_OK) {
+ java_vm = nullptr;
+ env = nullptr;
+ }
} else {
java_vm = nullptr;
}
diff --git a/tdactor/td/actor/impl/Scheduler.cpp b/tdactor/td/actor/impl/Scheduler.cpp
index 39cb4661e..ca8f58fb6 100644
--- a/tdactor/td/actor/impl/Scheduler.cpp
+++ b/tdactor/td/actor/impl/Scheduler.cpp
@@ -111,6 +111,7 @@ void Scheduler::ServiceActor::tear_down() {
/*** SchedlerGuard ***/
SchedulerGuard::SchedulerGuard(Scheduler *scheduler, bool lock) : scheduler_(scheduler) {
if (lock) {
+ // the next check can fail if OS killed the scheduler's thread without releasing the guard
CHECK(!scheduler_->has_guard_);
scheduler_->has_guard_ = true;
}
diff --git a/tdutils/CMakeLists.txt b/tdutils/CMakeLists.txt
index 067237508..728a6a0cb 100644
--- a/tdutils/CMakeLists.txt
+++ b/tdutils/CMakeLists.txt
@@ -318,6 +318,12 @@ target_include_directories(tdutils PUBLIC $ ExitGuard::is_exited_{false};
+ExitGuard::~ExitGuard() {
+ is_exited_.store(true, std::memory_order_relaxed);
+ set_verbosity_level(VERBOSITY_NAME(FATAL));
+}
+
} // namespace td
diff --git a/tdutils/td/utils/ExitGuard.h b/tdutils/td/utils/ExitGuard.h
index 1a91a731f..62a5bdaa9 100644
--- a/tdutils/td/utils/ExitGuard.h
+++ b/tdutils/td/utils/ExitGuard.h
@@ -17,9 +17,7 @@ class ExitGuard {
ExitGuard &operator=(ExitGuard &&) = delete;
ExitGuard(const ExitGuard &) = delete;
ExitGuard &operator=(const ExitGuard &) = delete;
- ~ExitGuard() {
- is_exited_.store(true, std::memory_order_relaxed);
- }
+ ~ExitGuard();
static bool is_exited() {
return is_exited_.load(std::memory_order_relaxed);
diff --git a/tdutils/td/utils/format.h b/tdutils/td/utils/format.h
index a3b5b3a46..8798d0e49 100644
--- a/tdutils/td/utils/format.h
+++ b/tdutils/td/utils/format.h
@@ -11,7 +11,6 @@
#include "td/utils/Slice.h"
#include "td/utils/StringBuilder.h"
-#include
#include
#include
@@ -335,9 +334,5 @@ template
StringBuilder &operator<<(StringBuilder &stream, const vector &vec) {
return stream << format::as_array(vec);
}
-template
-StringBuilder &operator<<(StringBuilder &stream, const std::set &vec) {
- return stream << format::as_array(vec);
-}
} // namespace td
diff --git a/tdutils/td/utils/logging.cpp b/tdutils/td/utils/logging.cpp
index 255b2da27..937cc279c 100644
--- a/tdutils/td/utils/logging.cpp
+++ b/tdutils/td/utils/logging.cpp
@@ -6,6 +6,7 @@
//
#include "td/utils/logging.h"
+#include "td/utils/ExitGuard.h"
#include "td/utils/port/Clocks.h"
#include "td/utils/port/StdStreams.h"
#include "td/utils/port/thread_local.h"
@@ -48,6 +49,9 @@ Logger::Logger(LogInterface &log, const LogOptions &options, int log_level, Slic
if (!options_.add_info) {
return;
}
+ if (ExitGuard::is_exited()) {
+ return;
+ }
// log level
sb_ << '[';
@@ -109,6 +113,9 @@ Logger::Logger(LogInterface &log, const LogOptions &options, int log_level, Slic
}
Logger::~Logger() {
+ if (ExitGuard::is_exited()) {
+ return;
+ }
if (options_.fix_newlines) {
sb_ << '\n';
auto slice = as_cslice();
@@ -157,7 +164,7 @@ TsCerr &TsCerr::operator<<(Slice slice) {
}
void TsCerr::enterCritical() {
- while (lock_.test_and_set(std::memory_order_acquire)) {
+ while (lock_.test_and_set(std::memory_order_acquire) && !ExitGuard::is_exited()) {
// spin
}
}
@@ -167,6 +174,16 @@ void TsCerr::exitCritical() {
}
TsCerr::Lock TsCerr::lock_ = ATOMIC_FLAG_INIT;
+void TsLog::enter_critical() {
+ while (lock_.test_and_set(std::memory_order_acquire) && !ExitGuard::is_exited()) {
+ // spin
+ }
+}
+
+void TsLog::exit_critical() {
+ lock_.clear(std::memory_order_release);
+}
+
class DefaultLog : public LogInterface {
public:
void append(CSlice slice, int log_level) override {
@@ -304,4 +321,6 @@ ScopedDisableLog::~ScopedDisableLog() {
}
}
+static ExitGuard exit_guard;
+
} // namespace td
diff --git a/tdutils/td/utils/logging.h b/tdutils/td/utils/logging.h
index 1202f0083..00c62169b 100644
--- a/tdutils/td/utils/logging.h
+++ b/tdutils/td/utils/logging.h
@@ -321,14 +321,8 @@ class TsLog : public LogInterface {
private:
LogInterface *log_ = nullptr;
std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
- void enter_critical() {
- while (lock_.test_and_set(std::memory_order_acquire)) {
- // spin
- }
- }
- void exit_critical() {
- lock_.clear(std::memory_order_release);
- }
+ void enter_critical();
+ void exit_critical();
};
} // namespace td
diff --git a/tdutils/td/utils/port/FileFd.cpp b/tdutils/td/utils/port/FileFd.cpp
index 1a6965eca..634dacb64 100644
--- a/tdutils/td/utils/port/FileFd.cpp
+++ b/tdutils/td/utils/port/FileFd.cpp
@@ -12,6 +12,7 @@
#endif
#include "td/utils/common.h"
+#include "td/utils/ExitGuard.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/port/detail/PollableFd.h"
@@ -356,6 +357,7 @@ Result FileFd::pread(MutableSlice slice, int64 offset) const {
static std::mutex in_process_lock_mutex;
static std::unordered_set locked_files;
+static ExitGuard exit_guard;
static Status create_local_lock(const string &path, int32 &max_tries) {
while (true) {
@@ -469,12 +471,13 @@ Status FileFd::lock(const LockFlags flags, const string &path, int32 max_tries)
}
void FileFd::remove_local_lock(const string &path) {
- if (!path.empty()) {
- VLOG(fd) << "Unlock file \"" << path << '"';
- std::unique_lock lock(in_process_lock_mutex);
- auto erased_count = locked_files.erase(path);
- CHECK(erased_count > 0);
+ if (path.empty() || ExitGuard::is_exited()) {
+ return;
}
+ VLOG(fd) << "Unlock file \"" << path << '"';
+ std::unique_lock lock(in_process_lock_mutex);
+ auto erased_count = locked_files.erase(path);
+ CHECK(erased_count > 0 || ExitGuard::is_exited());
}
void FileFd::close() {
diff --git a/tdutils/test/bitmask.cpp b/tdutils/test/bitmask.cpp
index b3fa109c7..3729a2a46 100644
--- a/tdutils/test/bitmask.cpp
+++ b/tdutils/test/bitmask.cpp
@@ -4,11 +4,17 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
-#include "td/utils/tests.h"
+#include "td/utils/common.h"
#include "td/utils/misc.h"
+#include "td/utils/Slice.h"
+#include "td/utils/Status.h"
+#include "td/utils/tests.h"
#include "td/utils/utf8.h"
+#include
+
namespace td {
+
class RangeSet {
template
static auto find(T &ranges, int64 begin) {
@@ -28,17 +34,14 @@ class RangeSet {
int64 end;
};
- static constexpr int64 BitSize = 1024;
- static constexpr int64 MaxPartSize = 16 * 1024 * 1024;
-
- RangeSet() = default;
+ static constexpr int64 BIT_SIZE = 1024;
static RangeSet create_one_range(int64 end, int64 begin = 0) {
RangeSet res;
res.ranges_.push_back({begin, end});
return res;
}
- static td::Result decode(CSlice data) {
+ static Result decode(CSlice data) {
if (!check_utf8(data)) {
return Status::Error("Invalid encoding");
}
@@ -50,7 +53,7 @@ class RangeSet {
begin = next_utf8_unsafe(begin, &size, "RangeSet");
if (!is_empty && size != 0) {
- res.ranges_.push_back({curr * BitSize, (curr + size) * BitSize});
+ res.ranges_.push_back({curr * BIT_SIZE, (curr + size) * BIT_SIZE});
}
curr += size;
is_empty = !is_empty;
@@ -58,12 +61,12 @@ class RangeSet {
return res;
}
- std::string encode(int64 prefix_size = -1) const {
- std::vector sizes;
+ string encode(int64 prefix_size = -1) const {
+ vector sizes;
uint32 all_end = 0;
if (prefix_size != -1) {
- prefix_size = (prefix_size + BitSize - 1) / BitSize * BitSize;
+ prefix_size = (prefix_size + BIT_SIZE - 1) / BIT_SIZE * BIT_SIZE;
}
for (auto it : ranges_) {
if (prefix_size != -1 && it.begin >= prefix_size) {
@@ -73,10 +76,10 @@ class RangeSet {
it.end = prefix_size;
}
- CHECK(it.begin % BitSize == 0);
- CHECK(it.end % BitSize == 0);
- uint32 begin = narrow_cast(it.begin / BitSize);
- uint32 end = narrow_cast(it.end / BitSize);
+ CHECK(it.begin % BIT_SIZE == 0);
+ CHECK(it.end % BIT_SIZE == 0);
+ uint32 begin = narrow_cast(it.begin / BIT_SIZE);
+ uint32 end = narrow_cast(it.end / BIT_SIZE);
if (sizes.empty()) {
if (begin != 0) {
sizes.push_back(0);
@@ -89,7 +92,7 @@ class RangeSet {
all_end = end;
}
- std::string res;
+ string res;
for (auto c : sizes) {
append_utf8_character(res, c);
}
@@ -149,8 +152,8 @@ class RangeSet {
}
void set(int64 begin, int64 end) {
- CHECK(begin % BitSize == 0);
- CHECK(end % BitSize == 0);
+ CHECK(begin % BIT_SIZE == 0);
+ CHECK(end % BIT_SIZE == 0);
// 1. skip all with r.end < begin
auto it_begin = find(begin);
@@ -162,16 +165,16 @@ class RangeSet {
if (it_begin == it_end) {
ranges_.insert(it_begin, Range{begin, end});
} else {
- begin = std::min(begin, it_begin->begin);
+ begin = td::min(begin, it_begin->begin);
--it_end;
- end = std::max(end, it_end->end);
+ end = td::max(end, it_end->end);
*it_end = Range{begin, end};
ranges_.erase(it_begin, it_end);
}
}
- std::vector as_vector(int32 part_size) const {
- std::vector res;
+ vector as_vector(int32 part_size) const {
+ vector res;
for (auto it : ranges_) {
auto begin = narrow_cast((it.begin + part_size - 1) / part_size);
auto end = narrow_cast(it.end / part_size);
@@ -183,13 +186,12 @@ class RangeSet {
}
private:
- std::vector ranges_;
+ vector ranges_;
};
TEST(Bitmask, simple) {
auto validate_encoding = [](auto &rs) {
auto str = rs.encode();
- LOG(ERROR) << str.size();
RangeSet rs2 = RangeSet::decode(str).move_as_ok();
auto str2 = rs2.encode();
rs = std::move(rs2);
@@ -238,9 +240,10 @@ TEST(Bitmask, simple) {
ASSERT_EQ(8, get(3));
ASSERT_EQ(10, rs.get_ready_prefix_size(S * 3, S * 3 + 10));
- ASSERT_TRUE(!rs.is_ready(S*11, S *12));
+ ASSERT_TRUE(!rs.is_ready(S * 11, S * 12));
ASSERT_EQ(3, rs.get_ready_parts(2, S * 2));
- ASSERT_EQ(std::vector({2, 3, 4, 7}), rs.as_vector(S * 2) );
+ ASSERT_EQ(vector({2, 3, 4, 7}), rs.as_vector(S * 2));
}
}
+
} // namespace td
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index dce6e85f6..6fab03211 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -26,13 +26,13 @@ set(TESTS_MAIN
main.cpp
)
-add_library(all_tests STATIC ${TD_TEST_SOURCE})
-target_include_directories(all_tests PUBLIC $)
-target_link_libraries(all_tests PRIVATE tdcore tdclient)
+#add_library(all_tests STATIC ${TD_TEST_SOURCE})
+#target_include_directories(all_tests PUBLIC $)
+#target_link_libraries(all_tests PRIVATE tdcore tdclient)
if (NOT CMAKE_CROSSCOMPILING OR EMSCRIPTEN)
#Tests
- add_executable(test-tdutils ${TESTS_MAIN} ${TDUTILS_TEST_SOURCE})
+ add_executable(test-tdutils EXCLUDE_FROM_ALL ${TESTS_MAIN} ${TDUTILS_TEST_SOURCE})
add_executable(run_all_tests ${TESTS_MAIN} ${TD_TEST_SOURCE})
if (CLANG AND NOT CYGWIN AND NOT EMSCRIPTEN AND NOT (CMAKE_HOST_SYSTEM_NAME MATCHES "OpenBSD"))
target_compile_options(test-tdutils PUBLIC -fsanitize=undefined -fno-sanitize=vptr)