-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce0a87d
commit b07f965
Showing
5 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
'use strict'; | ||
|
||
const dgram = require('dgram'); | ||
const { isIP, isIPv4 } = require('net'); | ||
const nanoresource = require('nanoresource/emitter'); | ||
|
||
const isLegalPort = port => typeof port === 'number' && port > 0 && port < 0xffff; | ||
|
||
/** | ||
* @typedef {Object} UDPSocketOptions | ||
* @property {number} port Target port. | ||
* @property {string} address Target IP address. | ||
* @property {string} [bindAddress] Source IP address. | ||
* @property {number} [bindPort] Source port. | ||
*/ | ||
/** | ||
* UDP socket abstraction. | ||
*/ | ||
module.exports = class UDPSocket extends nanoresource { | ||
/** | ||
* @class {UDPSocket} | ||
* @param {UDPSocketOptions} opts | ||
*/ | ||
constructor(opts = {}) { | ||
super(); | ||
|
||
const { port, address, bindAddress, bindPort } = opts; | ||
|
||
if (!isIP(address)) { | ||
throw new Error('Invalid ip address'); | ||
} | ||
|
||
if (!isLegalPort(port)) { | ||
throw new Error('Invalid port'); | ||
} | ||
|
||
this.port = port; | ||
this.address = address; | ||
|
||
this.bindPort = isLegalPort(bindPort) ? bindPort : 0; | ||
this.bindAddress = isIP(bindAddress) ? bindAddress : 'localhost'; | ||
|
||
this.socket = null; | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {Function} callback | ||
*/ | ||
_open(callback) { | ||
const type = isIPv4(this.address) ? 'udp4' : 'udp6'; | ||
const socket = dgram.createSocket(type); | ||
|
||
this.socket = socket; | ||
|
||
const connectHandler = () => { | ||
this.socket.removeListener('error', errorHandler); | ||
this.socket.on('error', err => this.emit('error', err)); | ||
this.socket.on('message', message => this.emit('data', message)); | ||
|
||
callback(null); | ||
}; | ||
|
||
const listeningHandler = () => { | ||
this.socket.connect(this.port, this.address); | ||
}; | ||
|
||
/** | ||
* @param {Error} error | ||
*/ | ||
function errorHandler(error) { | ||
socket.removeListener('connect', connectHandler); | ||
socket.removeListener('listening', listeningHandler); | ||
|
||
socket.close(() => callback(error)); | ||
} | ||
|
||
this.socket.once('error', errorHandler); | ||
this.socket.once('listening', listeningHandler); | ||
this.socket.once('connect', connectHandler); | ||
|
||
try { | ||
this.socket.bind(this.bindPort, this.bindAddress); | ||
} catch (error) { | ||
errorHandler(error); | ||
} | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {Function} callback | ||
*/ | ||
_close(callback) { | ||
this.socket.close(callback); | ||
this.socket = null; | ||
} | ||
|
||
/** | ||
* Send data. | ||
* @param {Buffer} data Data to send. | ||
* @param {Function} callback | ||
*/ | ||
write(data, callback) { | ||
this.open(error => { | ||
if (error) { | ||
return callback(error); | ||
} | ||
|
||
if (!this.active(callback)) { | ||
return; | ||
} | ||
|
||
this.socket.send(data, 0, data.length, (err, bytes) => { | ||
if (err) { | ||
return this.inactive(callback, err); | ||
} | ||
|
||
this.inactive(callback, null, bytes); | ||
}); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
const dgram = require('dgram'); | ||
const UDPSocket = require('../src/transport/udp-socket'); | ||
|
||
describe('udp socket', () => { | ||
const target = dgram.createSocket('udp4'); | ||
|
||
beforeAll(done => { | ||
target.once('error', done); | ||
target.bind(0, 'localhost', done); | ||
}); | ||
|
||
afterAll(done => { | ||
target.close(done); | ||
}); | ||
|
||
it('should work', done => { | ||
expect.assertions(3); | ||
|
||
const socket = new UDPSocket(target.address()); | ||
const data = Buffer.allocUnsafe(10); | ||
|
||
socket.write(data, (err, bytes) => { | ||
expect(err).toBeFalsy(); | ||
expect(bytes).toEqual(data.length); | ||
|
||
socket.close(); | ||
}); | ||
|
||
target.once('message', message => { | ||
expect(message).toEqual(data); | ||
done(); | ||
}); | ||
}); | ||
}); |