Skip to content

Commit 9d1eaa8

Browse files
committed
Fix discovery and connection issues on Windows
1 parent b142b52 commit 9d1eaa8

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

XTOOL_PROTOCOL.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ $ echo '{"requestId":123456}' | socat - UDP-DATAGRAM:255.255.255.255:20000,broad
290290
}
291291
```
292292

293+
After receiving the UDP request on port 20000, the device will first try to reply on TCP port 20001 and only then send the same response on UDP port 20000. If TCP port 20001 is blocked by a firewall the device will wait until the connections timed out before using UDP, creating a 20 second delay.
294+
293295

294296
#### GCODE
295297
Incomplete right now.

main/device-controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module.exports.connect = async (address, callback, config_) => {
6767
lastHeartbeat = Date.now();
6868
heartbeatTimer = setInterval(() => {
6969
websocket.ping();
70-
if(lastHeartbeat + 4100 < Date.now()) {
70+
if(lastHeartbeat + 6100 < Date.now()) {
7171
wsCallback('err:TIMEOUT');
7272
console.log('Connection to device timed out.');
7373
websocket.terminate();

main/discovery.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
11
const dgram = require('dgram');
2+
const net = require('net');
23

34
const DISCOVERY_PORT = 20000;
5+
const DISCOVERY_TCP_PORT = 20001;
46

57
module.exports.scanDevices = () => {
68
return new Promise(resolve => {
79
const requestId = Date.now();
810
const devices = [];
9-
const socket = dgram.createSocket('udp4');
10-
11-
socket.on('listening', () => {
12-
socket.setBroadcast(true);
13-
socket.send(JSON.stringify({requestId}), DISCOVERY_PORT, '255.255.255.255');
14-
console.log('Starting device scan.');
15-
});
16-
17-
socket.on('message', (message, remote) => {
11+
const handleResponse = message => {
1812
try {
1913
message = JSON.parse(message);
2014
if(message.requestId == requestId && !!message.ip && !!message.name) {
15+
if(devices.find(dev => dev.ip == message.ip)) return;
2116
delete message.requestId;
2217
devices.push(message);
2318
console.log('Discovered device: '+JSON.stringify(message));
2419
}
2520
} catch(e) {}
21+
};
22+
const socket = dgram.createSocket('udp4');
23+
24+
// Request
25+
socket.on('listening', () => {
26+
socket.setBroadcast(true);
27+
socket.send(JSON.stringify({requestId}), DISCOVERY_PORT, '255.255.255.255');
28+
console.log('Starting device scan.');
29+
});
30+
31+
// UDP Response
32+
socket.on('message', handleResponse);
33+
34+
// TCP Response
35+
const tcpServer = net.createServer(socket => {
36+
socket.on('data', handleResponse);
2637
});
38+
tcpServer.listen(DISCOVERY_TCP_PORT);
2739

2840
setTimeout(() => {
41+
tcpServer.close();
2942
socket.close();
3043
resolve(devices);
3144
}, 2000);

0 commit comments

Comments
 (0)