Skip to content

Commit e0d3572

Browse files
committed
Fix unable find-device-server bug and clean up network_wifi code
1 parent 2c377c4 commit e0d3572

1 file changed

Lines changed: 11 additions & 23 deletions

File tree

lib/network/interface/network_wifi.dart

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@ class WiFiNetworkAgent extends NetworkAgent {
2121
}
2222

2323
@override
24-
void sendData(NetworkData networkData) {
25-
this._socket.add(utf8.encode(networkData.toString()));
26-
}
24+
void sendData(NetworkData networkData) => this._socket.add(utf8.encode(networkData.toString()));
2725

2826
@override
29-
void removeConnection() {
30-
this._socket.close();
31-
}
27+
void removeConnection() => this._socket.close();
3228
}
3329

3430
class WifiNetworkManager extends NetworkManager {
@@ -49,29 +45,21 @@ class WifiNetworkManager extends NetworkManager {
4945
return NetworkAnalyzer.discover2(
5046
ip.substring(0, ip.lastIndexOf(".")), PORT,
5147
timeout: Duration(milliseconds: AppSettings().settingsMap[SettingsType.NETWORK_TIMEOUT].value),
52-
).map((value) => value.ip).toList();
48+
).transform(StreamTransformer<NetworkAddress, String>
49+
.fromHandlers(handleData: (data, sink) {if (data.exists) sink.add(data.ip);})).toList();
5350
}
5451

5552
@override
56-
Future<void> connectToTarget(String deviceAddress,
57-
Function() onSessionFail) async {
53+
Future<void> connectToTarget(String deviceAddress, Function() onSessionFail) async {
5854
if (this.isConnected) return this.disconnectCurrentTarget();
5955

60-
Completer<void> completer = Completer<void>();
61-
62-
Function() onSessionKilled = this.setDisconnectedState;
63-
64-
Socket.connect(deviceAddress, PORT).then((socket) {
65-
this.targetNetworkAgent = WiFiNetworkAgent(socket, deviceAddress, onSessionKilled);
56+
return Socket.connect(deviceAddress, PORT).then((socket) {
57+
this.targetNetworkAgent = WiFiNetworkAgent(socket, deviceAddress, this.setDisconnectedState);
6658
this.setConnectedState();
67-
completer.complete();
68-
}).catchError((e) {
69-
onSessionFail();
70-
}).timeout(Duration(milliseconds: AppSettings().settingsMap[SettingsType.NETWORK_TIMEOUT].value), onTimeout: () {
71-
onSessionFail();
72-
});
73-
74-
return completer.future;
59+
}).catchError((e) => onSessionFail())
60+
.timeout(Duration(milliseconds: AppSettings().settingsMap[SettingsType.NETWORK_TIMEOUT].value),
61+
onTimeout: () => onSessionFail(),
62+
);
7563
}
7664

7765
@override

0 commit comments

Comments
 (0)