feat: update
This commit is contained in:
@@ -2,6 +2,12 @@ import 'dart:convert';
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'config.dart';
|
import 'config.dart';
|
||||||
|
|
||||||
|
int createdConnectionCount = 0;
|
||||||
|
int runningConnectionCount = 0;
|
||||||
|
int upstreamBytes = 0;
|
||||||
|
int downstreamBytes = 0;
|
||||||
|
Map<int, bool> createdConnectionMap = <int, bool>{};
|
||||||
|
|
||||||
void main(List<String> arguments) {
|
void main(List<String> arguments) {
|
||||||
final listenAddress = "127.0.0.1";
|
final listenAddress = "127.0.0.1";
|
||||||
final listenPort = 8801;
|
final listenPort = 8801;
|
||||||
@@ -34,9 +40,54 @@ void main(List<String> arguments) {
|
|||||||
|
|
||||||
void startListen(HostAndPort listen, HostAndPort target) {
|
void startListen(HostAndPort listen, HostAndPort target) {
|
||||||
ServerSocket.bind(listen.host, listen.port).then((serverSocket) {
|
ServerSocket.bind(listen.host, listen.port).then((serverSocket) {
|
||||||
|
serverSocket.listen((Socket socket) {
|
||||||
Socket.connect(target.host, target.port).then((clientSocket) {
|
Socket.connect(target.host, target.port).then((clientSocket) {
|
||||||
// TODO ...
|
final createdConnectionIndex = createdConnectionCount;
|
||||||
|
createdConnectionCount++;
|
||||||
|
runningConnectionCount++;
|
||||||
|
createdConnectionMap[createdConnectionIndex] = true;
|
||||||
|
socket.listen(
|
||||||
|
(event) {
|
||||||
|
upstreamBytes += event.lengthInBytes;
|
||||||
|
clientSocket.write(event);
|
||||||
|
},
|
||||||
|
onError: (error, StackTrace stackTrace) {
|
||||||
|
if (createdConnectionMap.remove(createdConnectionIndex) != null) {
|
||||||
|
runningConnectionCount--;
|
||||||
|
}
|
||||||
|
print(
|
||||||
|
'Error in connection: ' + error + " " + stackTrace.toString());
|
||||||
|
},
|
||||||
|
onDone: () {
|
||||||
|
if (createdConnectionMap.remove(createdConnectionIndex) != null) {
|
||||||
|
runningConnectionCount--;
|
||||||
|
}
|
||||||
|
clientSocket.destroy();
|
||||||
|
},
|
||||||
|
cancelOnError: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
clientSocket.listen(
|
||||||
|
(event) {
|
||||||
|
downstreamBytes += event.lengthInBytes;
|
||||||
|
socket.write(event);
|
||||||
|
},
|
||||||
|
onError: (error, StackTrace stackTrace) {
|
||||||
|
if (createdConnectionMap.remove(createdConnectionIndex) != null) {
|
||||||
|
runningConnectionCount--;
|
||||||
|
}
|
||||||
|
print(
|
||||||
|
'Error in connection: ' + error + " " + stackTrace.toString());
|
||||||
|
},
|
||||||
|
onDone: () {
|
||||||
|
if (createdConnectionMap.remove(createdConnectionIndex) != null) {
|
||||||
|
runningConnectionCount--;
|
||||||
|
}
|
||||||
|
socket.destroy();
|
||||||
|
},
|
||||||
|
cancelOnError: true,
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
// serverSocket.listen((event) { })
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user