diff --git a/js/main/main.js b/js/main/main.js index 407bc8e8c..8e1cbc735 100644 --- a/js/main/main.js +++ b/js/main/main.js @@ -376,8 +376,8 @@ app.whenReady().then(() => { return udp.send(data); }); - ipcMain.on('udpClose', (_event) => { - udp.close(); + ipcMain.handle('udpClose', (_event) => { + return udp.close(); }); ipcMain.handle('writeFile', (_event, filename, data) => { diff --git a/js/main/udp.js b/js/main/udp.js index d746793a3..ae7061edd 100644 --- a/js/main/udp.js +++ b/js/main/udp.js @@ -1,60 +1,108 @@ // Runs in main thread import dgram from 'dgram'; -const socket = dgram.createSocket('udp4'); const udp = { + _socket: null, _id: 1, _ip: null, _port: null, connect: function(ip, port, window = true) { - return new Promise(resolve => { + return new Promise(resolve => { + let socket = null; try { - socket.bind(port, () => { - this._ip = ip; - this._port = port; - resolve({ error: false, id: this._id++ }); - }); + // Drop a socket left over from a previous connection + this._release(); + + socket = dgram.createSocket('udp4'); + this._socket = socket; + let bound = false; socket.on('error', error => { - if (window && typeof window.isDestroyed === 'function' && !window.isDestroyed()) { - window.webContents.send('udpError', error); + if (!bound) { + // Failed before the socket was ready: report it as the result of connect() + this._release(socket); + resolve({error: true, msg: error}); + return; + } + + if (this._socket === socket && window && typeof window.isDestroyed === 'function' && !window.isDestroyed()) { + window.webContents.send('udpError', error); } }); socket.on('message', (message, _rinfo) => { - if (window && typeof window.isDestroyed === 'function' && !window.isDestroyed()) { + if (this._socket === socket && window && typeof window.isDestroyed === 'function' && !window.isDestroyed()) { window.webContents.send('udpMessage', message); } - }); + }); + + // Let the OS pick a free local port. Binding to the remote port would collide + // with a peer listening on the same machine. + socket.bind(0, () => { + bound = true; + this._ip = ip; + this._port = port; + resolve({ error: false, id: this._id++ }); + }); } catch (err) { - resolve({error: true, errorMsg: err}); + this._release(socket); + resolve({error: true, msg: err}); } }); }, close: function() { return new Promise(resolve => { - try { - socket.disconnect(); + const socket = this._socket; + this._socket = null; + this._ip = null; + this._port = null; + + if (!socket) { resolve({error: false}); + return; + } + + try { + socket.close(() => resolve({error: false})); } catch (err) { resolve({error: true, msg: err}); } }); }, send: function(data) { - return new Promise(resolve => { - if (this._ip && this._port) { - socket.send(Buffer.from(data), this._port, this._ip, (error) => { + return new Promise(resolve => { + if (this._socket && this._ip && this._port) { + this._socket.send(Buffer.from(data), this._port, this._ip, (error) => { if (!error) { resolve({error: false, bytesWritten: data.byteLength}); } else { resolve({error: true, msg: error}); } }); + } else { + resolve({error: true, msg: "Socket closed or invalid"}); } }); + }, + // Closes the given socket (the current one by default) without reporting anything + _release: function(socket = this._socket) { + if (!socket) { + return; + } + + if (this._socket === socket) { + this._socket = null; + this._ip = null; + this._port = null; + } + + try { + socket.close(); + } catch (err) { + // Already closed + } } }; -export default udp; \ No newline at end of file +export default udp;