Skip to content

Commit 9cd0574

Browse files
committed
Improved DS interop performance
The DB mode server socket doesn't run when auto resize is off
1 parent b1ff7c3 commit 9cd0574

File tree

4 files changed

+78
-29
lines changed

4 files changed

+78
-29
lines changed

lib/pages/dashboard/dashboard_page_settings.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,10 @@ mixin DashboardPageSettings on DashboardPageViewModel {
189189
}
190190

191191
Future<void> changeResizeToDS(bool value) async {
192-
if (value && ntConnection.dsClient.driverStationDocked) {
193-
onDriverStationDocked();
192+
if (value) {
193+
ntConnection.startDBModeServer();
194194
} else {
195+
ntConnection.stopDBModeServer;
195196
onDriverStationUndocked();
196197
}
197198

lib/pages/dashboard_page.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ abstract class DashboardPageViewModel extends ChangeNotifier {
193193
});
194194
robotNotificationListener.listen();
195195

196+
if (preferences.getBool(PrefKeys.autoResizeToDS) ??
197+
Defaults.autoResizeToDS) {
198+
ntConnection.startDBModeServer();
199+
}
200+
196201
ntConnection.dsClientConnect(
197202
onIPAnnounced: (ip) async {
198203
if (preferences.getInt(PrefKeys.ipAddressMode) !=

lib/services/ds_interop.dart

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:elastic_dashboard/services/log.dart';
99
class DSInteropClient {
1010
final String serverBaseAddress = '127.0.0.1';
1111
bool _serverConnectionActive = false;
12-
bool _dbModeConnectionActive = false;
12+
bool _dbModeServerRunning = false;
1313

1414
Function()? onConnect;
1515
Function()? onDisconnect;
@@ -20,6 +20,8 @@ class DSInteropClient {
2020
Socket? _socket;
2121
ServerSocket? _dbModeServer;
2222

23+
final List<Socket> _connectedSockets = [];
24+
2325
List<int> _tcpBuffer = [];
2426

2527
String? _lastAnnouncedIP;
@@ -28,21 +30,34 @@ class DSInteropClient {
2830
String? get lastAnnouncedIP => _lastAnnouncedIP;
2931
bool get driverStationDocked => _driverStationDocked;
3032

33+
bool _attemptServerStart = true;
34+
35+
DateTime serverStopTime = DateTime.now();
36+
3137
DSInteropClient({
3238
this.onNewIPAnnounced,
3339
this.onDriverStationDockChanged,
3440
this.onConnect,
3541
this.onDisconnect,
3642
}) {
37-
_connect();
43+
_tcpSocketConnect();
3844
}
3945

40-
void _connect() {
41-
if (_serverConnectionActive) {
42-
return;
46+
void startDBModeServer() {
47+
// For some reason if the server is quickly stopped then started there's a 5 second delay
48+
// The workaround is to use the cached value of the docked state
49+
if (DateTime.now().microsecondsSinceEpoch -
50+
serverStopTime.microsecondsSinceEpoch <=
51+
5 * 1e6) {
52+
onDriverStationDockChanged?.call(_driverStationDocked);
4353
}
44-
_tcpSocketConnect();
45-
_dbModeServerConnect();
54+
_attemptServerStart = true;
55+
_startDBModeServer();
56+
}
57+
58+
void stopDBModeServer() {
59+
serverStopTime = DateTime.now();
60+
_dbModeServerClose(false);
4661
}
4762

4863
void _tcpSocketConnect() async {
@@ -74,30 +89,36 @@ class DSInteropClient {
7489
);
7590
}
7691

77-
void _dbModeServerConnect() async {
78-
if (_dbModeConnectionActive) {
92+
Future<void> _startDBModeServer() async {
93+
if (_dbModeServerRunning || !_attemptServerStart) {
7994
return;
8095
}
8196
try {
8297
_dbModeServer = await ServerSocket.bind(serverBaseAddress, 1741);
8398
} catch (e) {
84-
logger.info(
85-
'Failed to start TCP server on port 1741, attempting to reconnect in 5 seconds');
86-
Future.delayed(const Duration(seconds: 5), _dbModeServerConnect);
99+
if (_attemptServerStart) {
100+
logger.info(
101+
'Failed to start TCP server on port 1741, attempting to restart in 5 seconds');
102+
Future.delayed(const Duration(seconds: 5), _startDBModeServer);
103+
} else {
104+
logger.info('Failed to start TCP server on port 1741');
105+
}
87106
return;
88107
}
89108

109+
_attemptServerStart = false;
110+
_dbModeServerRunning = true;
111+
90112
_dbModeServer!.listen(
91113
(socket) {
92114
logger.info('Received connection from Driver Station on TCP port 1741');
115+
_connectedSockets.add(socket);
93116
socket.listen(
94117
(data) {
95-
if (!_dbModeConnectionActive) {
96-
_dbModeConnectionActive = true;
97-
}
98118
_dbModeServerOnMessage(data);
99119
},
100120
onDone: () {
121+
_connectedSockets.remove(socket);
101122
logger.info('Lost connection from Driver Station on TCP port 1741');
102123
},
103124
);
@@ -174,39 +195,53 @@ class DSInteropClient {
174195
}
175196
}
176197

177-
void _socketClose() {
198+
void _socketClose() async {
178199
if (!_serverConnectionActive) {
179200
return;
180201
}
181202

182-
_socket?.close();
203+
await _socket?.close();
183204
_socket = null;
184205

185206
_serverConnectionActive = false;
186207

187-
_driverStationDocked = false;
188-
onDriverStationDockChanged?.call(false);
189208
onDisconnect?.call();
190209

191210
logger.info(
192211
'Driver Station connection on TCP port 1742 closed, attempting to reconnect in 5 seconds.');
193212

194-
Future.delayed(const Duration(seconds: 5), _connect);
213+
Future.delayed(const Duration(seconds: 5), _tcpSocketConnect);
195214
}
196215

197-
void _dbModeServerClose() {
198-
if (!_dbModeConnectionActive) {
216+
void _dbModeServerClose([bool reconnect = true]) async {
217+
if (!_dbModeServerRunning) {
199218
return;
200219
}
201220

202-
_dbModeServer?.close();
221+
_dbModeServerRunning = false;
222+
223+
// Cloning the list because closing each socket changes the size of the list
224+
for (Socket socket in _connectedSockets.toList()) {
225+
await socket.close();
226+
socket.destroy();
227+
}
228+
229+
await _dbModeServer?.close();
203230
_dbModeServer = null;
204231

205-
_dbModeConnectionActive = false;
232+
onDriverStationDockChanged?.call(false);
206233

207-
logger.info(
208-
'Driver Station TCP Server on Port 1741 closed, attempting to reconnect in 5 seconds.');
234+
_tcpBuffer.clear();
235+
236+
_attemptServerStart = reconnect;
209237

210-
Future.delayed(const Duration(seconds: 5), _dbModeServerConnect);
238+
if (reconnect) {
239+
logger.info(
240+
'Driver Station TCP Server on Port 1741 closed, attempting to reconnect in 5 seconds.');
241+
242+
Future.delayed(const Duration(seconds: 5), _startDBModeServer);
243+
} else {
244+
logger.info('Driver Station TCP Server on Port 1741 closed');
245+
}
211246
}
212247
}

lib/services/nt_connection.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ class NTConnection {
7676
);
7777
}
7878

79+
void startDBModeServer() {
80+
_dsClient.startDBModeServer();
81+
}
82+
83+
void stopDBModeServer() {
84+
_dsClient.stopDBModeServer();
85+
}
86+
7987
void addConnectedListener(VoidCallback callback) {
8088
onConnectedListeners.add(callback);
8189
}

0 commit comments

Comments
 (0)