decodePayload static method
dynamic
decodePayload(
- dynamic data, {
- bool binaryType = false,
- required dynamic callback(
- dynamic err, [
- dynamic foo,
- dynamic bar
Decodes data when a payload is maybe expected. Possible binary contents are decoded from their base64 representation
Implementation
static decodePayload(data,
{bool binaryType = false,
required Function(dynamic err, [dynamic foo, dynamic bar]) callback}) {
if (data is! String) {
return decodePayloadAsBinary(data,
binaryType: binaryType, callback: callback);
}
if (data == '') {
// parser error - ignoring payload
return callback(Error, 0, 1);
}
dynamic packet = '', msg = '';
dynamic length = 0, n = 0;
for (int i = 0, l = data.length; i < l; i++) {
var chr = data[i];
if (chr != ':') {
length += chr;
continue;
}
if (length.isEmpty || (length != '${(n = num.tryParse(length))}')) {
// parser error - ignoring payload
return callback(error, 0, 1);
}
int nv = n!;
msg = data.substring(i + 1, i + 1 + nv);
if (length != '${msg.length}') {
// parser error - ignoring payload
return callback(error, 0, 1);
}
if (msg.isNotEmpty) {
packet = decodePacket(msg, binaryType: binaryType, utf8decode: false);
if (error['type'] == packet['type'] &&
error['data'] == packet['data']) {
// parser error in individual packet - ignoring payload
return callback(error, 0, 1);
}
var more = callback(packet, i + nv, l);
if (false == more) return null;
}
// advance cursor
i += nv;
length = '';
}
if (length.isNotEmpty) {
// parser error - ignoring payload
return callback(error, 0, 1);
}
}