Implementation
void start() async {
Map<String, String> reportParams = {};
DateTime startTime = DateTime.now();
_state = SMHTaskState.processing;
stateStream.add(_state);
receivePort = ReceivePort();
receivePort.listen((data) {
SendPort? subSencPort;
if (data['code'] == 'finish') {
if (finishCalBack != null) {
SMHError? error = data['error'];
reportParams['took_time'] =
DateTime.now().difference(startTime).inMilliseconds.toString();
reportParams['space_id'] = getTaskInfo().spaceId;
reportParams['host'] = SMHServicesManager
.servicesManager.serviceMap[SMHAPIServiceKey]?.baseUrl ??
'';
reportParams['smh_key'] = SMHServicesManager
.servicesManager.serviceMap[SMHAPIServiceKey]?.baseUrl +
getTaskInfo().filePath;
if (error != null) {
reportParams['error_service_name'] = SMHRequest.getServiceName();
reportParams['error_status_code'] = error.statusCode.toString();
reportParams['error_message'] = error.statusMessage.toString() +
':' +
error.smhMessage.toString();
reportParams['error_code'] = error.smhCode.toString();
reportParams['error_type'] = error.error.toString();
reportParams['cos_request_id'] = error.cosRequestId ?? '';
reportParams['request_id'] = error.requestId ?? '';
_state = SMHTaskState.error;
if (getTaskInfo().option == SMHTaskOption.upload) {
reportParams['size'] = getTaskInfo().length.toString();
SMHBeaconManager.manager
.reportFail(params: reportParams, eventCode: 'smh_upload');
} else {
reportParams['size'] =
File(getTaskInfo().localPath).lengthSync().toString();
SMHBeaconManager.manager
.reportFail(params: reportParams, eventCode: 'smh_download');
}
} else {
_state = SMHTaskState.success;
if (getTaskInfo().option == SMHTaskOption.upload) {
reportParams['name'] = 'SMHTransferService.upload';
reportParams['size'] = getTaskInfo().length.toString();
SMHBeaconManager.manager.reportSuccess(
params: reportParams,
eventCode: 'smh_upload',
);
} else {
reportParams['name'] = 'SMHTransferService.download';
reportParams['size'] =
File(getTaskInfo().localPath).lengthSync().toString();
SMHBeaconManager.manager.reportSuccess(
params: reportParams,
eventCode: 'smh_download',
);
}
}
stateStream.add(_state);
finishCalBack!(data['result'], error);
}
finish();
return;
}
if (data['code'] == 'confirmKey') {
String confirmKey = data['result'];
if (confirmKeyCalBack != null) {
confirmKeyCalBack!(confirmKey);
}
return;
}
if (data['code'] == 'processing') {
Map value = data['result'];
if (onSendProgress != null) {
onSendProgress!(value['count'], value['total']);
}
if (onReceiveProgress != null) {
onReceiveProgress!(value['count'], value['total']);
}
return;
}
if (data['code'] == 'init') {
subSencPort = data['result'];
SMHServicesManager servicesManager = SMHServicesManager.servicesManager;
Map<String, dynamic> envMap = {
'currentHost': servicesManager.currentHost,
'currentType': servicesManager.currentType,
};
final info = getTaskInfo();
subSencPort!.send({'data': info, 'env': envMap});
return;
}
});
isolate = await createIsolate(receivePort.sendPort);
}