• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const { SafeMap } = primordials;
3const assert = require('internal/assert');
4const dgram = require('internal/dgram');
5const net = require('net');
6
7module.exports = SharedHandle;
8
9function SharedHandle(key, address, { port, addressType, fd, flags }) {
10  this.key = key;
11  this.workers = new SafeMap();
12  this.handle = null;
13  this.errno = 0;
14
15  let rval;
16  if (addressType === 'udp4' || addressType === 'udp6')
17    rval = dgram._createSocketHandle(address, port, addressType, fd, flags);
18  else
19    rval = net._createServerHandle(address, port, addressType, fd, flags);
20
21  if (typeof rval === 'number')
22    this.errno = rval;
23  else
24    this.handle = rval;
25}
26
27SharedHandle.prototype.add = function(worker, send) {
28  assert(!this.workers.has(worker.id));
29  this.workers.set(worker.id, worker);
30  send(this.errno, null, this.handle);
31};
32
33SharedHandle.prototype.remove = function(worker) {
34  if (!this.workers.has(worker.id))
35    return false;
36
37  this.workers.delete(worker.id);
38
39  if (this.workers.size !== 0)
40    return false;
41
42  this.handle.close();
43  this.handle = null;
44  return true;
45};
46