mirror of
https://github.com/nodejs/node.git
synced 2025-05-05 22:29:54 +00:00

PR-URL: https://github.com/nodejs/node/pull/26732 Refs: https://github.com/nodejs/node/issues/26568 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
'use strict';
|
|
const assert = require('internal/assert');
|
|
const dgram = require('internal/dgram');
|
|
const net = require('net');
|
|
|
|
module.exports = SharedHandle;
|
|
|
|
function SharedHandle(key, address, port, addressType, fd, flags) {
|
|
this.key = key;
|
|
this.workers = [];
|
|
this.handle = null;
|
|
this.errno = 0;
|
|
|
|
var rval;
|
|
if (addressType === 'udp4' || addressType === 'udp6')
|
|
rval = dgram._createSocketHandle(address, port, addressType, fd, flags);
|
|
else
|
|
rval = net._createServerHandle(address, port, addressType, fd, flags);
|
|
|
|
if (typeof rval === 'number')
|
|
this.errno = rval;
|
|
else
|
|
this.handle = rval;
|
|
}
|
|
|
|
SharedHandle.prototype.add = function(worker, send) {
|
|
assert(!this.workers.includes(worker));
|
|
this.workers.push(worker);
|
|
send(this.errno, null, this.handle);
|
|
};
|
|
|
|
SharedHandle.prototype.remove = function(worker) {
|
|
const index = this.workers.indexOf(worker);
|
|
|
|
if (index === -1)
|
|
return false; // The worker wasn't sharing this handle.
|
|
|
|
this.workers.splice(index, 1);
|
|
|
|
if (this.workers.length !== 0)
|
|
return false;
|
|
|
|
this.handle.close();
|
|
this.handle = null;
|
|
return true;
|
|
};
|