9 #pragma mark - Basic message channel
18 NSCAssert(newSize >= 0,
@"Channel buffer size must be non-negative");
22 NSArray* args = @[ channel, @(
static_cast<int>(newSize)) ];
26 NSData* message = [codec encodeMethodCall:resizeMethodCall];
44 arguments:@[ channel, @(!warns) ]];
46 NSData* message = [codec encodeMethodCall:overflowMethodCall];
51 NSObject<FlutterBinaryMessenger>* messenger,
54 NSObject<FlutterTaskQueue>* taskQueue) {
56 NSCAssert([messenger respondsToSelector:
@selector(setMessageHandlerOnChannel:
57 binaryMessageHandler:taskQueue:)],
59 return [messenger setMessageHandlerOnChannel:name
60 binaryMessageHandler:handler
63 return [messenger setMessageHandlerOnChannel:name binaryMessageHandler:handler];
69 NSObject<FlutterBinaryMessenger>* _messenger;
71 NSObject<FlutterMessageCodec>*
_codec;
75 + (instancetype)messageChannelWithName:(NSString*)name
82 + (instancetype)messageChannelWithName:(NSString*)name
86 binaryMessenger:messenger
90 - (instancetype)initWithName:(NSString*)name
97 - (instancetype)initWithName:(NSString*)name
102 NSAssert(
self,
@"Super init cannot be nil");
104 _messenger = messenger;
110 - (void)sendMessage:(
id)message {
111 [_messenger sendOnChannel:_name message:[_codec encode:message]];
114 - (void)sendMessage:(
id)message reply:(
FlutterReply)callback {
117 callback([
_codec decode:data]);
120 [_messenger sendOnChannel:_name message:[_codec encode:message] binaryReply:reply];
126 [_messenger cleanUpConnection:_connection];
129 [_messenger setMessageHandlerOnChannel:_name binaryMessageHandler:nil];
137 NSObject<FlutterMessageCodec>* codec =
_codec;
139 handler([codec decode:message], ^(
id reply) {
140 callback([codec encode:reply]);
146 + (void)resizeChannelWithName:(NSString*)name
148 size:(NSInteger)newSize {
152 - (void)resizeChannelBuffer:(NSInteger)newSize {
156 + (void)setWarnsOnOverflow:(BOOL)warns
157 forChannelWithName:(NSString*)name
162 - (void)setWarnsOnOverflow:(BOOL)warns {
168 #pragma mark - Method channel
172 + (instancetype)errorWithCode:(NSString*)code message:(NSString*)message details:(
id)details {
173 return [[
FlutterError alloc] initWithCode:code message:message details:details];
176 - (instancetype)initWithCode:(NSString*)code message:(NSString*)message details:(
id)details {
177 NSAssert(
code,
@"Code cannot be nil");
179 NSAssert(
self,
@"Super init cannot be nil");
181 _message = [message copy];
186 - (BOOL)isEqual:(
id)object {
187 if (
self ==
object) {
194 return [
self.code isEqual:other.code] &&
200 return [
self.code hash] ^ [
self.message hash] ^ [
self.details hash];
206 + (instancetype)methodCallWithMethodName:(NSString*)method arguments:(
id)arguments {
207 return [[
FlutterMethodCall alloc] initWithMethodName:method arguments:arguments];
210 - (instancetype)initWithMethodName:(NSString*)method arguments:(
id)arguments {
211 NSAssert(
method,
@"Method name cannot be nil");
213 NSAssert(
self,
@"Super init cannot be nil");
214 _method = [method copy];
219 - (BOOL)isEqual:(
id)object {
220 if (
self ==
object) {
227 return [
self.method isEqual:[other
method]] &&
232 return [
self.method hash] ^ [
self.arguments hash];
240 NSObject<FlutterBinaryMessenger>* _messenger;
242 NSObject<FlutterMethodCodec>*
_codec;
247 + (instancetype)methodChannelWithName:(NSString*)name
253 + (instancetype)methodChannelWithName:(NSString*)name
255 codec:(NSObject<FlutterMethodCodec>*)codec {
259 - (instancetype)initWithName:(NSString*)name
261 codec:(NSObject<FlutterMethodCodec>*)codec {
262 self = [
self initWithName:name binaryMessenger:messenger codec:codec taskQueue:nil];
265 - (instancetype)initWithName:(NSString*)name
267 codec:(NSObject<FlutterMethodCodec>*)codec
270 NSAssert(
self,
@"Super init cannot be nil");
272 _messenger = messenger;
278 - (void)invokeMethod:(NSString*)method arguments:(
id)arguments {
281 NSData* message = [_codec encodeMethodCall:methodCall];
282 [_messenger sendOnChannel:_name message:message];
285 - (void)invokeMethod:(NSString*)method arguments:(
id)arguments result:(
FlutterResult)callback {
288 NSData* message = [_codec encodeMethodCall:methodCall];
294 [_messenger sendOnChannel:_name message:message binaryReply:reply];
300 [_messenger cleanUpConnection:_connection];
303 [_messenger setMessageHandlerOnChannel:_name binaryMessageHandler:nil];
310 NSObject<FlutterMethodCodec>* codec =
_codec;
313 handler(call, ^(
id result) {
316 }
else if ([result isKindOfClass:[
FlutterError class]]) {
317 callback([codec encodeErrorEnvelope:(
FlutterError*)result]);
319 callback([codec encodeSuccessEnvelope:result]);
326 - (void)resizeChannelBuffer:(NSInteger)newSize {
332 #pragma mark - Event channel
338 NSObject<FlutterBinaryMessenger>* _messenger;
340 NSObject<FlutterMethodCodec>*
_codec;
344 + (instancetype)eventChannelWithName:(NSString*)name
350 + (instancetype)eventChannelWithName:(NSString*)name
352 codec:(NSObject<FlutterMethodCodec>*)codec {
353 return [[
FlutterEventChannel alloc] initWithName:name binaryMessenger:messenger codec:codec];
356 - (instancetype)initWithName:(NSString*)name
358 codec:(NSObject<FlutterMethodCodec>*)codec {
359 return [
self initWithName:name binaryMessenger:messenger codec:codec taskQueue:nil];
362 - (instancetype)initWithName:(NSString*)name
364 codec:(NSObject<FlutterMethodCodec>*)codec
367 NSAssert(
self,
@"Super init cannot be nil");
369 _messenger = messenger;
376 NSObject<FlutterStreamHandler>* handler,
378 NSObject<FlutterBinaryMessenger>* messenger,
379 NSObject<FlutterMethodCodec>* codec,
380 NSObject<FlutterTaskQueue>* taskQueue) {
384 if ([call.
method isEqual:
@"listen"]) {
386 FlutterError* error = [handler onCancelWithArguments:nil];
388 NSLog(
@"Failed to cancel existing stream: %@. %@ (%@)", error.
code, error.
message,
392 currentSink = ^(
id event) {
394 [messenger sendOnChannel:name message:nil];
395 }
else if ([event isKindOfClass:[
FlutterError class]]) {
396 [messenger sendOnChannel:name message:[codec encodeErrorEnvelope:(FlutterError*)event]];
398 [messenger sendOnChannel:name message:[codec encodeSuccessEnvelope:event]];
401 FlutterError* error = [handler onListenWithArguments:call.arguments eventSink:currentSink];
403 callback([codec encodeErrorEnvelope:error]);
405 callback([codec encodeSuccessEnvelope:nil]);
407 }
else if ([call.
method isEqual:
@"cancel"]) {
410 [codec encodeErrorEnvelope:[
FlutterError errorWithCode:
@"error"
411 message:
@"No active stream to cancel"
416 FlutterError* error = [handler onCancelWithArguments:call.arguments];
418 callback([codec encodeErrorEnvelope:error]);
420 callback([codec encodeSuccessEnvelope:nil]);
431 [_messenger cleanUpConnection:_connection];