10
#pragma mark - Codec for basic message channel
15
+ (instancetype)sharedInstance {
16
static
id
_sharedInstance = nil;
17
if
(!_sharedInstance) {
21
return
_sharedInstance;
30
NSAssert(
self
,
@"Super init cannot be nil"
);
31
_readerWriter = readerWriter;
35
- (NSData*)encode:(
id
)message {
39
NSMutableData* data = [NSMutableData dataWithCapacity:32];
45
- (id)decode:(NSData*)message {
46
if
([message length] == 0) {
51
NSAssert(![reader hasMore],
@"Corrupted standard message"
);
56
#pragma mark - Codec for method channel
61
+ (instancetype)sharedInstance {
62
static
id
_sharedInstance = nil;
63
if
(!_sharedInstance) {
67
return
_sharedInstance;
76
NSAssert(
self
,
@"Super init cannot be nil"
);
77
_readerWriter = readerWriter;
82
NSMutableData* data = [NSMutableData dataWithCapacity:32];
89
- (NSData*)encodeSuccessEnvelope:(
id
)result {
90
NSMutableData* data = [NSMutableData dataWithCapacity:32];
98
NSMutableData* data = [NSMutableData dataWithCapacity:32];
111
NSAssert(![reader hasMore],
@"Corrupted standard method call"
);
112
NSAssert([value1 isKindOfClass:[NSString
class
]],
@"Corrupted standard method call"
);
116
- (id)decodeEnvelope:(NSData*)envelope {
119
NSAssert(flag <= 1,
@"Corrupted standard envelope"
);
124
NSAssert(![reader hasMore],
@"Corrupted standard envelope"
);
130
NSAssert(![reader hasMore],
@"Corrupted standard envelope"
);
131
NSAssert([code isKindOfClass:[NSString
class
]],
@"Invalid standard envelope"
);
132
NSAssert(message == nil || [message isKindOfClass:[NSString
class
]],
133
@"Invalid standard envelope"
);
143
#pragma mark - Standard serializable types
146
+ (instancetype)typedDataWithBytes:(NSData*)data {
150
+ (instancetype)typedDataWithInt32:(NSData*)data {
154
+ (instancetype)typedDataWithInt64:(NSData*)data {
158
+ (instancetype)typedDataWithFloat32:(NSData*)data {
162
+ (instancetype)typedDataWithFloat64:(NSData*)data {
166
+ (instancetype)typedDataWithData:(NSData*)data type:(FlutterStandardDataType)type {
170
- (instancetype)initWithData:(NSData*)data type:(FlutterStandardDataType)type {
172
NSAssert(
data
,
@"Data cannot be nil"
);
173
NSAssert(
data
.length %
elementSize
== 0,
@"Data must contain integral number of elements"
);
174
self
= [
super
init];
175
NSAssert(
self
,
@"Super init cannot be nil"
);
183
- (BOOL)isEqual:(
id
)object {
184
if
(
self
==
object
) {
192
[
self
.data isEqual:other.data];
196
return
[
self
.data hash] ^
self
.type;
200
#pragma mark - Writer and reader of standard codec
203
NSMutableData* _data;
206
- (instancetype)initWithData:(NSMutableData*)data {
207
self
= [
super
init];
208
NSAssert(
self
,
@"Super init cannot be nil"
);
213
- (void)writeByte:(UInt8)value {
217
- (void)writeBytes:(const
void
*)bytes length:(NSUInteger)length {
221
- (void)writeData:(NSData*)data {
225
- (void)writeSize:(UInt32)size {
229
- (void)writeAlignment:(UInt8)alignment {
233
- (void)writeUTF8:(NSString*)value {
235
(__bridge CFStringRef)value);
239
if
(value == nil || (__bridge CFNullRef)value == kCFNull) {
241
}
else
if
([value isKindOfClass:[NSNumber
class
]]) {
243
}
else
if
([value isKindOfClass:[NSString
class
]]) {
247
}
else
if
([value isKindOfClass:[NSData
class
]]) {
249
}
else
if
([value isKindOfClass:[NSArray
class
]]) {
251
}
else
if
([value isKindOfClass:[NSDictionary
class
]]) {
263
static
void
WriteKeyValues(CFTypeRef key, CFTypeRef value,
void
* context) {
265
FastWriteValueOfType(info->
writer
, info->
data
, key);
266
FastWriteValueOfType(info->
writer
, info->
data
, value);
271
static
void
FastWriteValueOfType(CFTypeRef writer, CFMutableDataRef data, CFTypeRef value) {
274
WriteValueOfType(writer, data, type, value);
280
static
void
WriteValueOfType(CFTypeRef writer,
281
CFMutableDataRef data,
289
CFNumberRef number = (CFNumberRef)value;
292
NSLog(
@"Unsupported value: %@ of number type %ld"
, value, CFNumberGetType(number));
293
NSCAssert(NO,
@"Unsupported value for standard codec"
);
298
CFStringRef
string
= (CFStringRef)value;
317
CFArrayRef array = (CFArrayRef)value;
319
CFIndex count = CFArrayGetCount(array);
321
for
(CFIndex i = 0; i < count; ++i) {
322
FastWriteValueOfType(writer, data, CFArrayGetValueAtIndex(array, i));
327
CFDictionaryRef dict = (CFDictionaryRef)value;
329
CFIndex count = CFDictionaryGetCount(dict);
335
CFDictionaryApplyFunction(dict, WriteKeyValues, (
void
*)&info);
339
id
objc_value = (__bridge id)value;
340
NSLog(
@"Unsupported value: %@ of type %@"
, objc_value, [objc_value
class
]);
341
NSCAssert(NO,
@"Unsupported value for standard codec"
);
347
- (void)writeValue:(
id
)value {
349
WriteValueOfType((__bridge CFTypeRef)
self
, (__bridge CFMutableDataRef)
self
->_data, type,
350
(__bridge CFTypeRef)value);
359
- (instancetype)initWithData:(NSData*)data {
360
self
= [
super
init];
361
NSAssert(
self
,
@"Super init cannot be nil"
);
363
_range
= NSMakeRange(0, 0);
368
return
_range
.location < _data.length;
371
- (void)readBytes:(
void
*)destination length:(NSUInteger)length {
373
(__bridge CFDataRef)_data);
384
- (NSData*)readData:(NSUInteger)length {
386
NSData* data = [_data subdataWithRange:_range];
391
- (NSString*)readUTF8 {
393
(__bridge CFDataRef)_data);
396
- (void)readAlignment:(UInt8)alignment {
400
- (nullable id)readValue {
401
return
(__bridge
id
)ReadValue((__bridge CFTypeRef)
self
);
404
static
CFTypeRef ReadValue(CFTypeRef
user_data
) {
407
(__bridge CFDataRef)reader->_data);
413
unsigned
long
* location = &reader->_range.location;
414
CFDataRef data = (__bridge CFDataRef)reader->_data;
420
UInt64 length = elementCount * elementSize;
421
NSRange range = NSMakeRange(*location, length);
424
NSData* bytes = [(__bridge NSData*)data subdataWithRange:range];
429
- (nullable id)readValueOfType:(UInt8)type {
431
&
_range
.location, (__bridge CFDataRef)_data, type, ReadValue, ReadTypedDataOfType,
432
(__bridge CFTypeRef)
self
);
FlutterStandardField FlutterStandardFieldForDataType(FlutterStandardDataType type)
CFStringRef FlutterStandardCodecHelperReadUTF8(unsigned long *location, CFDataRef data)
void writeValue:(id value)
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
uint32_t FlutterStandardCodecHelperReadSize(unsigned long *location, CFDataRef data)
FlutterStandardDataType type
@ FlutterStandardFieldList
void writeByte:(UInt8 value)
void FlutterStandardCodecHelperWriteUTF8(CFMutableDataRef data, CFStringRef value)
FlutterStandardDataType FlutterStandardDataTypeForField(FlutterStandardField field)
void FlutterStandardCodecHelperWriteAlignment(CFMutableDataRef data, uint8_t alignment)
uint8_t FlutterStandardCodecHelperReadByte(unsigned long *location, CFDataRef data)
FlutterStandardCodecObjcType
instancetype errorWithCode:message:details:(NSString *code,[message] NSString *_Nullable message,[details] id _Nullable details)
CFTypeRef FlutterStandardCodecHelperReadValueOfType(unsigned long *location, CFDataRef data, uint8_t type, CFTypeRef(*ReadValue)(CFTypeRef), CFTypeRef(*ReadTypedDataOfType)(FlutterStandardField, CFTypeRef), CFTypeRef user_data)
@ FlutterStandardCodecObjcTypeFlutterStandardTypedData
@ FlutterStandardFieldString
@ FlutterStandardCodecObjcTypeNSString
UInt8 elementSizeForFlutterStandardDataType(FlutterStandardDataType type)
@ FlutterStandardCodecObjcTypeNSArray
void FlutterStandardCodecHelperReadBytes(unsigned long *location, unsigned long length, void *destination, CFDataRef data)
void FlutterStandardCodecHelperWriteSize(CFMutableDataRef data, uint32_t size)
bool FlutterStandardCodecHelperWriteNumber(CFMutableDataRef data, CFNumberRef number)
void FlutterStandardCodecHelperWriteData(CFMutableDataRef data, CFDataRef value)
void FlutterStandardCodecHelperWriteBytes(CFMutableDataRef data, const void *bytes, unsigned long length)
void FlutterStandardCodecHelperReadAlignment(unsigned long *location, uint8_t alignment)
@ FlutterStandardCodecObjcTypeNSNumber
nullable id readValueOfType:(UInt8 type)
@ FlutterStandardFieldNil
@ FlutterStandardFieldMap
void FlutterStandardCodecHelperWriteByte(CFMutableDataRef data, uint8_t value)
@ FlutterStandardCodecObjcTypeUnknown
@ FlutterStandardCodecObjcTypeNil
@ FlutterStandardCodecObjcTypeNSDictionary
@ FlutterStandardCodecObjcTypeNSData
shell
platform
darwin
common
framework
Source
FlutterStandardCodec.mm