Skip to content

Commit

Permalink
test fix support apple ecosystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Apr 12, 2023
1 parent d464044 commit 3668f89
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 215 deletions.
132 changes: 1 addition & 131 deletions package/telegram_bot_api_flutter/lib/telegram_bot_api_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,131 +1 @@

import 'dart:async';
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';

import 'telegram_bot_api_flutter_bindings_generated.dart';

/// A very short-lived native function.
///
/// For very short-lived functions, it is fine to call them on the main isolate.
/// They will block the Dart execution while running the native function, so
/// only do this for native functions which are guaranteed to be short-lived.
int sum(int a, int b) => _bindings.sum(a, b);

/// A longer lived native function, which occupies the thread calling it.
///
/// Do not call these kind of native functions in the main isolate. They will
/// block Dart execution. This will cause dropped frames in Flutter applications.
/// Instead, call these native functions on a separate isolate.
///
/// Modify this to suit your own use case. Example use cases:
///
/// 1. Reuse a single isolate for various different kinds of requests.
/// 2. Use multiple helper isolates for parallel execution.
Future<int> sumAsync(int a, int b) async {
final SendPort helperIsolateSendPort = await _helperIsolateSendPort;
final int requestId = _nextSumRequestId++;
final _SumRequest request = _SumRequest(requestId, a, b);
final Completer<int> completer = Completer<int>();
_sumRequests[requestId] = completer;
helperIsolateSendPort.send(request);
return completer.future;
}

const String _libName = 'telegram_bot_api_flutter';

/// The dynamic library in which the symbols for [TelegramBotApiFlutterBindings] can be found.
final DynamicLibrary _dylib = () {
if (Platform.isMacOS || Platform.isIOS) {
return DynamicLibrary.open('$_libName.framework/$_libName');
}
if (Platform.isAndroid || Platform.isLinux) {
return DynamicLibrary.open('lib$_libName.so');
}
if (Platform.isWindows) {
return DynamicLibrary.open('$_libName.dll');
}
throw UnsupportedError('Unknown platform: ${Platform.operatingSystem}');
}();

/// The bindings to the native functions in [_dylib].
final TelegramBotApiFlutterBindings _bindings = TelegramBotApiFlutterBindings(_dylib);


/// A request to compute `sum`.
///
/// Typically sent from one isolate to another.
class _SumRequest {
final int id;
final int a;
final int b;

const _SumRequest(this.id, this.a, this.b);
}

/// A response with the result of `sum`.
///
/// Typically sent from one isolate to another.
class _SumResponse {
final int id;
final int result;

const _SumResponse(this.id, this.result);
}

/// Counter to identify [_SumRequest]s and [_SumResponse]s.
int _nextSumRequestId = 0;

/// Mapping from [_SumRequest] `id`s to the completers corresponding to the correct future of the pending request.
final Map<int, Completer<int>> _sumRequests = <int, Completer<int>>{};

/// The SendPort belonging to the helper isolate.
Future<SendPort> _helperIsolateSendPort = () async {
// The helper isolate is going to send us back a SendPort, which we want to
// wait for.
final Completer<SendPort> completer = Completer<SendPort>();

// Receive port on the main isolate to receive messages from the helper.
// We receive two types of messages:
// 1. A port to send messages on.
// 2. Responses to requests we sent.
final ReceivePort receivePort = ReceivePort()
..listen((dynamic data) {
if (data is SendPort) {
// The helper isolate sent us the port on which we can sent it requests.
completer.complete(data);
return;
}
if (data is _SumResponse) {
// The helper isolate sent us a response to a request we sent.
final Completer<int> completer = _sumRequests[data.id]!;
_sumRequests.remove(data.id);
completer.complete(data.result);
return;
}
throw UnsupportedError('Unsupported message type: ${data.runtimeType}');
});

// Start the helper isolate.
await Isolate.spawn((SendPort sendPort) async {
final ReceivePort helperReceivePort = ReceivePort()
..listen((dynamic data) {
// On the helper isolate listen to requests and respond to them.
if (data is _SumRequest) {
final int result = _bindings.sum_long_running(data.a, data.b);
final _SumResponse response = _SumResponse(data.id, result);
sendPort.send(response);
return;
}
throw UnsupportedError('Unsupported message type: ${data.runtimeType}');
});

// Send the the port to the main isolate on which we can receive requests.
sendPort.send(helperReceivePort.sendPort);
}, receivePort.sendPort);

// Wait until the helper isolate has sent us back the SendPort on which we
// can start sending requests.
return completer.future;
}();

This file was deleted.

2 changes: 1 addition & 1 deletion package/telegram_bot_api_flutter/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ cmake_minimum_required(VERSION 3.10)

set(PROJECT_NAME "telegram_bot_api_flutter")

install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/telegram-bot-api/telegram-bot-api-cli" DESTINATION "bin" COMPONENT Runtime)
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/telegram-bot-api/telegram-bot-api" DESTINATION "bin" COMPONENT Runtime)
6 changes: 3 additions & 3 deletions package/telegram_bot_api_flutter/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ project(${PROJECT_NAME} LANGUAGES CXX)

# begin td
set(dll_path "${CMAKE_CURRENT_SOURCE_DIR}/telegram-bot-api")
install(FILES "${dll_path}/libcrypto-1_1-x64.dll" DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)
install(FILES "${dll_path}/libssl-1_1-x64.dll" DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)
install(FILES "${dll_path}/tdjson.dll" DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)
install(FILES "${dll_path}/libcrypto-3.dll" DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)
install(FILES "${dll_path}/libssl-3.dll" DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)
install(FILES "${dll_path}/telegram-bot-api.exe" DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)
install(FILES "${dll_path}/zlib1.dll" DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)
# end td
2 changes: 1 addition & 1 deletion package/telegram_bot_api_flutter/windows/telegram-bot-api/.gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*
*
!.gitignore
4 changes: 4 additions & 0 deletions package/telegram_client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.6

- Test fix support cli macos

## 0.4.5

- Menambahkan Support Send File di telegram bot api
Expand Down
8 changes: 7 additions & 1 deletion package/telegram_client/lib/tdlib/ffi/tdlib_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class LibTdJson {
"start": true,
};
late final String path_tdlib;
bool is_cli;
bool is_android = Platform.isAndroid;
List<TdlibClient> clients = [];
int client_id = 0;
Expand All @@ -80,6 +81,7 @@ class LibTdJson {
LibTdJson({
String? pathTdl,
Map? clientOption,
this.is_cli = false,
this.event_invoke = "invoke",
this.event_update = "update",
EventEmitter? eventEmitter,
Expand Down Expand Up @@ -143,7 +145,11 @@ class LibTdJson {

ffi.DynamicLibrary get tdLib {
if (Platform.isIOS || Platform.isMacOS) {
return ffi.DynamicLibrary.process();
if (is_cli) {
return ffi.DynamicLibrary.open(path_tdlib);
} else {
return ffi.DynamicLibrary.process();
}
} else {
return ffi.DynamicLibrary.open(path_tdlib);
}
Expand Down
2 changes: 2 additions & 0 deletions package/telegram_client/lib/tdlib/ffi/tdlib_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class LibTdJson {
"database_key": "",
"start": true,
};
bool is_cli;
late final String path_tdlib;
bool is_android = Platform.isAndroid;
List<TdlibClient> clients = [];
Expand All @@ -80,6 +81,7 @@ class LibTdJson {
LibTdJson({
String? pathTdl,
Map? clientOption,
this.is_cli = false,
this.event_invoke = "invoke",
this.event_update = "update",
EventEmitter? eventEmitter,
Expand Down
1 change: 1 addition & 0 deletions package/telegram_client/lib/tdlib/tdlib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Tdlib extends LibTdJson {
Tdlib({
super.pathTdl,
super.clientOption,
super.is_cli,
int? clientId,
Duration? invokeTimeOut,
super.event_invoke = "invoke",
Expand Down
2 changes: 1 addition & 1 deletion package/telegram_client/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: telegram_client
description: Telegram Client Lightweight, blazing and Highly customizable for make application telegram based tdlib, mtproto, or bot api and support server side.
version: 0.4.5
version: 0.4.6
homepage: https://youtube.com/@azkadev
repository: https://github.com/azkadev/telegram_client
issue_tracker: https://github.com/azkadev/telegram_client/issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,8 @@ void main(List<String> arguments) async {
parameters: {
"chat_id": 5308590964,
"media": [
{
"type": "document",
"media":file
},
{
"type": "document",
"media": file
}
{"type": "document", "media": file},
{"type": "document", "media": file}
],
},
is_form: false,
Expand Down

0 comments on commit 3668f89

Please sign in to comment.