mirror of
https://github.com/Linloir/Simple-TCP-Server.git
synced 2025-12-16 23:48:11 +08:00
Improvements
- add await in file receiving
This commit is contained in:
parent
522e0712e7
commit
4504c196f6
23
Dockerfile
Normal file
23
Dockerfile
Normal file
@ -0,0 +1,23 @@
|
||||
# Specify the Dart SDK base image version using dart:<version> (ex: dart:2.12)
|
||||
FROM dart:stable AS build
|
||||
|
||||
# Resolve app dependencies.
|
||||
WORKDIR /app
|
||||
COPY pubspec.* ./
|
||||
RUN dart pub get
|
||||
|
||||
# Copy app source code and AOT compile it.
|
||||
COPY . .
|
||||
# Ensure packages are still up-to-date if anything has changed
|
||||
RUN dart pub get --offline
|
||||
RUN dart compile exe bin/server.dart -o bin/server
|
||||
|
||||
# Build minimal serving image from AOT-compiled `/server` and required system
|
||||
# libraries and configuration files stored in `/runtime/` from the build stage.
|
||||
FROM scratch
|
||||
COPY --from=build /runtime/ /
|
||||
COPY --from=build /app/bin/server /app/bin/
|
||||
|
||||
# Start server.
|
||||
EXPOSE 20706
|
||||
CMD ["/app/bin/server"]
|
||||
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* @Author : Linloir
|
||||
* @Date : 2022-10-08 15:10:04
|
||||
* @LastEditTime : 2022-10-18 14:50:17
|
||||
* @LastEditTime : 2022-10-19 00:54:03
|
||||
* @Description :
|
||||
*/
|
||||
|
||||
@ -57,19 +57,32 @@ class TCPController {
|
||||
print('[L] Connection Established');
|
||||
print('[L] Remote: ${socket.remoteAddress}:${socket.remotePort}');
|
||||
print('[L] Local: ${socket.address}:${socket.port}');
|
||||
socket.listen(
|
||||
_pullRequest,
|
||||
onError: (e) {
|
||||
print(e);
|
||||
_requestStreamController.addError(e);
|
||||
},
|
||||
onDone: () {
|
||||
print('[L] [CLOSED ]-----------------------');
|
||||
print('[L] Connection closed: ${socket.address}:${socket.port}<-${socket.remoteAddress}:${socket.remotePort}');
|
||||
_requestStreamController.close();
|
||||
},
|
||||
cancelOnError: true,
|
||||
);
|
||||
Future(() async {
|
||||
await for(var request in socket) {
|
||||
_pullRequest(request);
|
||||
await Future.delayed(const Duration(microseconds: 0));
|
||||
}
|
||||
}).then((_) {
|
||||
print('[L] [CLOSED ]-----------------------');
|
||||
print('[L] Connection closed: ${socket.address}:${socket.port}<-${socket.remoteAddress}:${socket.remotePort}');
|
||||
_requestStreamController.close();
|
||||
}).onError((error, stackTrace) {
|
||||
print(error);
|
||||
_requestStreamController.addError(error ?? Error());
|
||||
},);
|
||||
// socket.listen(
|
||||
// _pullRequest,
|
||||
// onError: (e) {
|
||||
// print(e);
|
||||
// _requestStreamController.addError(e);
|
||||
// },
|
||||
// onDone: () {
|
||||
// print('[L] [CLOSED ]-----------------------');
|
||||
// print('[L] Connection closed: ${socket.address}:${socket.port}<-${socket.remoteAddress}:${socket.remotePort}');
|
||||
// _requestStreamController.close();
|
||||
// },
|
||||
// cancelOnError: true,
|
||||
// );
|
||||
//This future never ends, would that be bothersome?
|
||||
Future(() async {
|
||||
try{
|
||||
@ -124,7 +137,7 @@ class TCPController {
|
||||
_fileCounter %= 1000;
|
||||
Future(() async {
|
||||
await for(var data in payloadPullStream) {
|
||||
await tempFile.writeAsBytes(data, mode: FileMode.append, flush: true);
|
||||
await tempFile.writeAsBytes(data, mode: FileMode.writeOnlyAppend);
|
||||
}
|
||||
_payloadRawStreamController.add(tempFile);
|
||||
});
|
||||
@ -160,7 +173,7 @@ class TCPController {
|
||||
if(buffer.length >= payloadLength) {
|
||||
//Last few bytes to emit
|
||||
//Send the last few bytes to stream
|
||||
_payloadPullStreamController.add(Uint8List.fromList(buffer.sublist(0, payloadLength)));
|
||||
_payloadPullStreamController.add(buffer.sublist(0, payloadLength));
|
||||
//Clear buffer
|
||||
buffer.removeRange(0, payloadLength);
|
||||
//Set payload length to zero
|
||||
@ -171,7 +184,7 @@ class TCPController {
|
||||
else {
|
||||
//Part of payload
|
||||
//Transmit all to stream
|
||||
_payloadPullStreamController.add(Uint8List.fromList(buffer));
|
||||
_payloadPullStreamController.add(buffer);
|
||||
//Reduce payload bytes left
|
||||
payloadLength -= buffer.length;
|
||||
//Clear buffer
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user