mirror of
https://github.com/Linloir/Simple-TCP-Server.git
synced 2025-12-19 00:48:12 +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
|
* @Author : Linloir
|
||||||
* @Date : 2022-10-08 15:10:04
|
* @Date : 2022-10-08 15:10:04
|
||||||
* @LastEditTime : 2022-10-18 14:50:17
|
* @LastEditTime : 2022-10-19 00:54:03
|
||||||
* @Description :
|
* @Description :
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -57,19 +57,32 @@ class TCPController {
|
|||||||
print('[L] Connection Established');
|
print('[L] Connection Established');
|
||||||
print('[L] Remote: ${socket.remoteAddress}:${socket.remotePort}');
|
print('[L] Remote: ${socket.remoteAddress}:${socket.remotePort}');
|
||||||
print('[L] Local: ${socket.address}:${socket.port}');
|
print('[L] Local: ${socket.address}:${socket.port}');
|
||||||
socket.listen(
|
Future(() async {
|
||||||
_pullRequest,
|
await for(var request in socket) {
|
||||||
onError: (e) {
|
_pullRequest(request);
|
||||||
print(e);
|
await Future.delayed(const Duration(microseconds: 0));
|
||||||
_requestStreamController.addError(e);
|
}
|
||||||
},
|
}).then((_) {
|
||||||
onDone: () {
|
|
||||||
print('[L] [CLOSED ]-----------------------');
|
print('[L] [CLOSED ]-----------------------');
|
||||||
print('[L] Connection closed: ${socket.address}:${socket.port}<-${socket.remoteAddress}:${socket.remotePort}');
|
print('[L] Connection closed: ${socket.address}:${socket.port}<-${socket.remoteAddress}:${socket.remotePort}');
|
||||||
_requestStreamController.close();
|
_requestStreamController.close();
|
||||||
},
|
}).onError((error, stackTrace) {
|
||||||
cancelOnError: true,
|
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?
|
//This future never ends, would that be bothersome?
|
||||||
Future(() async {
|
Future(() async {
|
||||||
try{
|
try{
|
||||||
@ -124,7 +137,7 @@ class TCPController {
|
|||||||
_fileCounter %= 1000;
|
_fileCounter %= 1000;
|
||||||
Future(() async {
|
Future(() async {
|
||||||
await for(var data in payloadPullStream) {
|
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);
|
_payloadRawStreamController.add(tempFile);
|
||||||
});
|
});
|
||||||
@ -160,7 +173,7 @@ class TCPController {
|
|||||||
if(buffer.length >= payloadLength) {
|
if(buffer.length >= payloadLength) {
|
||||||
//Last few bytes to emit
|
//Last few bytes to emit
|
||||||
//Send the last few bytes to stream
|
//Send the last few bytes to stream
|
||||||
_payloadPullStreamController.add(Uint8List.fromList(buffer.sublist(0, payloadLength)));
|
_payloadPullStreamController.add(buffer.sublist(0, payloadLength));
|
||||||
//Clear buffer
|
//Clear buffer
|
||||||
buffer.removeRange(0, payloadLength);
|
buffer.removeRange(0, payloadLength);
|
||||||
//Set payload length to zero
|
//Set payload length to zero
|
||||||
@ -171,7 +184,7 @@ class TCPController {
|
|||||||
else {
|
else {
|
||||||
//Part of payload
|
//Part of payload
|
||||||
//Transmit all to stream
|
//Transmit all to stream
|
||||||
_payloadPullStreamController.add(Uint8List.fromList(buffer));
|
_payloadPullStreamController.add(buffer);
|
||||||
//Reduce payload bytes left
|
//Reduce payload bytes left
|
||||||
payloadLength -= buffer.length;
|
payloadLength -= buffer.length;
|
||||||
//Clear buffer
|
//Clear buffer
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user