未验证 提交 330b0f02 编写于 作者: F Francisco Magdaleno 提交者: GitHub

Revert "[macos] Revert check on FlutterCodecs and refactor message function] (#10009)" (#10141)

This reverts commit bd387021.
上级 250ee316
......@@ -51,7 +51,7 @@
- (void)sendMessage:(id)message reply:(FlutterReply)callback {
FlutterBinaryReply reply = ^(NSData* data) {
if (callback)
callback(data.length > 0 ? [_codec decode:data] : nil);
callback([_codec decode:data]);
};
[_messenger sendOnChannel:_name message:[_codec encode:message] binaryReply:reply];
}
......
......@@ -73,7 +73,7 @@
}
- (id)decode:(NSData*)message {
if (message == nil)
if (message.length == 0)
return nil;
BOOL isSimpleValue = NO;
id decoded = nil;
......
......@@ -332,35 +332,42 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FLEEngine*
#pragma mark - FlutterBinaryMessenger
- (void)sendOnChannel:(nonnull NSString*)channel message:(nullable NSData*)message {
[self sendOnChannel:channel message:message binaryReply:nil];
FlutterPlatformMessage platformMessage = {
.struct_size = sizeof(FlutterPlatformMessage),
.channel = [channel UTF8String],
.message = static_cast<const uint8_t*>(message.bytes),
.message_size = message.length,
};
FlutterEngineResult result = FlutterEngineSendPlatformMessage(_engine, &platformMessage);
if (result != kSuccess) {
NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result);
}
}
- (void)sendOnChannel:(NSString*)channel
message:(NSData* _Nullable)message
binaryReply:(FlutterBinaryReply _Nullable)callback {
FlutterPlatformMessageResponseHandle* response_handle = nullptr;
if (callback) {
struct Captures {
FlutterBinaryReply reply;
};
auto captures = std::make_unique<Captures>();
captures->reply = callback;
auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) {
auto captures = reinterpret_cast<Captures*>(user_data);
NSData* reply_data = [NSData dataWithBytes:static_cast<const void*>(data) length:data_size];
captures->reply(reply_data);
delete captures;
};
struct Captures {
FlutterBinaryReply reply;
};
auto captures = std::make_unique<Captures>();
captures->reply = callback;
auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) {
auto captures = reinterpret_cast<Captures*>(user_data);
NSData* reply_data = [NSData dataWithBytes:(void*)data length:data_size];
captures->reply(reply_data);
delete captures;
};
FlutterEngineResult create_result = FlutterPlatformMessageCreateResponseHandle(
_engine, message_reply, captures.get(), &response_handle);
if (create_result != kSuccess) {
NSLog(@"Failed to create a FlutterPlatformMessageResponseHandle (%d)", create_result);
return;
}
captures.release();
FlutterPlatformMessageResponseHandle* response_handle = nullptr;
FlutterEngineResult result = FlutterPlatformMessageCreateResponseHandle(
_engine, message_reply, captures.get(), &response_handle);
if (result != kSuccess) {
NSLog(@"Failed to create a FlutterPlatformMessageResponseHandle");
return;
}
captures.release();
FlutterPlatformMessage platformMessage = {
.struct_size = sizeof(FlutterPlatformMessage),
......@@ -370,19 +377,15 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FLEEngine*
.response_handle = response_handle,
};
FlutterEngineResult message_result = FlutterEngineSendPlatformMessage(_engine, &platformMessage);
if (message_result != kSuccess) {
NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel,
message_result);
result = FlutterEngineSendPlatformMessage(_engine, &platformMessage);
if (result != kSuccess) {
NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result);
}
if (response_handle != nullptr) {
FlutterEngineResult release_result =
FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle);
if (release_result != kSuccess) {
NSLog(@"Failed to release the response handle (%d).", release_result);
};
}
result = FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle);
if (result != kSuccess) {
NSLog(@"Failed to release the response handle");
};
}
- (void)setMessageHandlerOnChannel:(nonnull NSString*)channel
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册