Changed recept method to async!!

This commit is contained in:
Linloir 2022-10-09 20:30:53 +08:00
parent 092a0cbb59
commit bb13789271
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
3 changed files with 11 additions and 17 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@
.packages .packages
.tmp/ .tmp/
.data/**
# Conventional directory for build output. # Conventional directory for build output.
build/ build/

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-06 16:15:01 * @Date : 2022-10-06 16:15:01
* @LastEditTime : 2022-10-09 20:14:06 * @LastEditTime : 2022-10-09 20:28:49
* @Description : * @Description :
*/ */
@ -482,7 +482,6 @@ class DataBaseHelper {
} }
var filePath = '${Directory.current.path}/.data/files/$fileMd5'; var filePath = '${Directory.current.path}/.data/files/$fileMd5';
await tempFile.copy(filePath); await tempFile.copy(filePath);
tempFile.delete();
try { try {
await _database.insert( await _database.insert(
'files', 'files',

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-08 15:10:04 * @Date : 2022-10-08 15:10:04
* @LastEditTime : 2022-10-09 17:55:26 * @LastEditTime : 2022-10-09 20:21:53
* @Description : * @Description :
*/ */
@ -17,8 +17,6 @@ class TCPController {
//Stores the incoming bytes of the TCP connection temporarily //Stores the incoming bytes of the TCP connection temporarily
final List<int> buffer = []; final List<int> buffer = [];
//Stores the fetched require temporarily
List<int> _requestBytes = [];
//Byte length for json object //Byte length for json object
int requestLength = 0; int requestLength = 0;
@ -66,23 +64,19 @@ class TCPController {
var tempFile = File('${Directory.current.path}/.tmp/${DateTime.now().microsecondsSinceEpoch}')..createSync(); var tempFile = File('${Directory.current.path}/.tmp/${DateTime.now().microsecondsSinceEpoch}')..createSync();
//Initialize payload transmission controller //Initialize payload transmission controller
_payloadStreamController = StreamController(); _payloadStreamController = StreamController();
//Bind file to stream //Create a future that listens to the status of the payload transmission
_payloadStreamController.stream.listen( var payloadTransmission = Future(() async {
(data) { await for(var data in _payloadStreamController.stream) {
tempFile.writeAsBytesSync(data, mode: FileMode.append, flush: true); await tempFile.writeAsBytes(data, mode: FileMode.append, flush: true);
},
onDone: () {
//Payload definetely ends after request is buffered
//Therefore transmit the request and payload to stream here
_inStreamController.add(TCPRequest(_requestBytes, tempFile));
} }
); });
//Bind request construction on stream //Bind request construction on stream
_requestStreamController = StreamController(); _requestStreamController = StreamController();
_requestStreamController.stream.listen((requestBytes) { _requestStreamController.stream.listen((requestBytes) {
//When request stream is closed by controller //When request stream is closed by controller
//Request is intact, save to _request temporarily payloadTransmission.then((_) {
_requestBytes = requestBytes; _inStreamController.add(TCPRequest(requestBytes, tempFile));
});
}); });
} }
else { else {