mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
dgram: Implement binding an existing `fd`. Allow pass a `fd` property to `socket.bind()` in dgram. src: Add `UDPWrap::Open` PR-URL: https://github.com/nodejs/node/pull/21745 Fixes: https://github.com/nodejs/node/issues/14961 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
35 lines
838 B
JavaScript
35 lines
838 B
JavaScript
// Flags: --expose-internals
|
|
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { _createSocketHandle } = require('internal/dgram');
|
|
const UDP = process.binding('udp_wrap').UDP;
|
|
|
|
{
|
|
// Create a handle that is not bound.
|
|
const handle = _createSocketHandle(null, null, 'udp4');
|
|
|
|
assert(handle instanceof UDP);
|
|
assert.strictEqual(typeof handle.fd, 'number');
|
|
assert(handle.fd < 0);
|
|
}
|
|
|
|
{
|
|
// Create a bound handle.
|
|
const handle = _createSocketHandle(common.localhostIPv4, 0, 'udp4');
|
|
|
|
assert(handle instanceof UDP);
|
|
assert.strictEqual(typeof handle.fd, 'number');
|
|
|
|
if (!common.isWindows)
|
|
assert(handle.fd > 0);
|
|
}
|
|
|
|
{
|
|
// Return an error if binding fails.
|
|
const err = _createSocketHandle('localhost', 0, 'udp4');
|
|
|
|
assert.strictEqual(typeof err, 'number');
|
|
assert(err < 0);
|
|
}
|