mirror of
https://github.com/Linloir/Simple-TCP-Server.git
synced 2025-12-18 16:38:12 +08:00
Changed recept method to async!!
This commit is contained in:
parent
092a0cbb59
commit
bb13789271
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,6 +3,7 @@
|
|||||||
.packages
|
.packages
|
||||||
|
|
||||||
.tmp/
|
.tmp/
|
||||||
|
.data/**
|
||||||
|
|
||||||
# Conventional directory for build output.
|
# Conventional directory for build output.
|
||||||
build/
|
build/
|
||||||
|
|||||||
@ -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',
|
||||||
|
|||||||
@ -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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user