未验证 提交 6f3a2eb1 编写于 作者: F Francisco Magdaleno 提交者: GitHub

[macos] Add reply to binary messenger (#9953)

* Add reply to binary messenger

* Address comments

* Formatting

* Address comments
上级 a7ef5086
......@@ -61,10 +61,7 @@ FLUTTER_EXPORT
*/
- (void)sendOnChannel:(NSString*)channel
message:(NSData* _Nullable)message
binaryReply:(FlutterBinaryReply _Nullable)callback
// TODO: Add macOS support for replies once
// https://github.com/flutter/flutter/issues/18852 is fixed.
API_UNAVAILABLE(macos);
binaryReply:(FlutterBinaryReply _Nullable)callback;
/**
* Registers a message handler for incoming binary messages from the Flutter side
......
......@@ -103,11 +103,7 @@ FLUTTER_EXPORT
* @param message The message. Must be supported by the codec of this channel.
* @param callback A callback to be invoked with the message reply from Flutter.
*/
- (void)sendMessage:(id _Nullable)message
reply:(FlutterReply _Nullable)callback
// TODO: Add macOS support for replies once
// https://github.com/flutter/flutter/issues/18852 is fixed.
API_UNAVAILABLE(macos);
- (void)sendMessage:(id _Nullable)message reply:(FlutterReply _Nullable)callback;
/**
* Registers a message handler with this channel.
......
......@@ -73,7 +73,7 @@
}
- (id)decode:(NSData*)message {
if (message == nil)
if (message.length == 0)
return nil;
BOOL isSimpleValue = NO;
id decoded = nil;
......
......@@ -345,6 +345,49 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FLEEngine*
}
}
- (void)sendOnChannel:(NSString*)channel
message:(NSData* _Nullable)message
binaryReply:(FlutterBinaryReply _Nullable)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:(void*)data length:data_size];
captures->reply(reply_data);
delete captures;
};
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),
.channel = [channel UTF8String],
.message = static_cast<const uint8_t*>(message.bytes),
.message_size = message.length,
.response_handle = response_handle,
};
result = FlutterEngineSendPlatformMessage(_engine, &platformMessage);
if (result != kSuccess) {
NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result);
}
result = FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle);
if (result != kSuccess) {
NSLog(@"Failed to release the response handle");
};
}
- (void)setMessageHandlerOnChannel:(nonnull NSString*)channel
binaryMessageHandler:(nullable FlutterBinaryMessageHandler)handler {
_messageHandlers[channel] = [handler copy];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册