JAVASCRIPT   10

socket_list.js

Guest on 15th May 2021 10:16:19 AM

  1. 'use strict';
  2.  
  3. const EventEmitter = require('events');
  4.  
  5. // This object keeps track of the sockets that are sent
  6. class SocketListSend extends EventEmitter {
  7.   constructor(child, key) {
  8.     super();
  9.     this.key = key;
  10.     this.child = child;
  11.     child.once('exit', () => this.emit('exit', this));
  12.   }
  13.  
  14.   _request(msg, cmd, callback) {
  15.     var self = this;
  16.  
  17.     if (!this.child.connected) return onclose();
  18.     this.child.send(msg);
  19.  
  20.     function onclose() {
  21.       self.child.removeListener('internalMessage', onreply);
  22.       callback(new Error('child closed before reply'));
  23.     }
  24.  
  25.     function onreply(msg) {
  26.       if (!(msg.cmd === cmd && msg.key === self.key)) return;
  27.       self.child.removeListener('disconnect', onclose);
  28.       self.child.removeListener('internalMessage', onreply);
  29.  
  30.       callback(null, msg);
  31.     }
  32.  
  33.     this.child.once('disconnect', onclose);
  34.     this.child.on('internalMessage', onreply);
  35.   }
  36.  
  37.   close(callback) {
  38.     this._request({
  39.       cmd: 'NODE_SOCKET_NOTIFY_CLOSE',
  40.       key: this.key
  41.     }, 'NODE_SOCKET_ALL_CLOSED', callback);
  42.   }
  43.  
  44.   getConnections(callback) {
  45.     this._request({
  46.       cmd: 'NODE_SOCKET_GET_COUNT',
  47.       key: this.key
  48.     }, 'NODE_SOCKET_COUNT', function(err, msg) {
  49.       if (err) return callback(err);
  50.       callback(null, msg.count);
  51.     });
  52.   }
  53. }
  54.  
  55.  
  56. // This object keeps track of the sockets that are received
  57. class SocketListReceive extends EventEmitter {
  58.   constructor(child, key) {
  59.     super();
  60.  
  61.     this.connections = 0;
  62.     this.key = key;
  63.     this.child = child;
  64.  
  65.     function onempty(self) {
  66.       if (!self.child.connected) return;
  67.  
  68.       self.child.send({
  69.         cmd: 'NODE_SOCKET_ALL_CLOSED',
  70.         key: self.key
  71.       });
  72.     }
  73.  
  74.     this.child.on('internalMessage', (msg) => {
  75.       if (msg.key !== this.key) return;
  76.  
  77.       if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') {
  78.         // Already empty
  79.         if (this.connections === 0) return onempty(this);
  80.  
  81.         // Wait for sockets to get closed
  82.         this.once('empty', onempty);
  83.       } else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') {
  84.         if (!this.child.connected) return;
  85.         this.child.send({
  86.           cmd: 'NODE_SOCKET_COUNT',
  87.           key: this.key,
  88.           count: this.connections
  89.         });
  90.       }
  91.     });
  92.   }
  93.  
  94.   add(obj) {
  95.     this.connections++;
  96.  
  97.     // Notify the previous owner of the socket about its state change
  98.     obj.socket.once('close', () => {
  99.       this.connections--;
  100.  
  101.       if (this.connections === 0) this.emit('empty', this);
  102.     });
  103.   }
  104. }
  105.  
  106. module.exports = { SocketListSend, SocketListReceive };

Raw Paste


Login or Register to edit or fork this paste. It's free.