hybridconnector.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. var net = require('net');
  2. var tls = require('tls');
  3. var util = require('util');
  4. var EventEmitter = require('events').EventEmitter;
  5. var HybridSocket = require('./hybridsocket');
  6. var Switcher = require('./hybrid/switcher');
  7. var Handshake = require('./commands/handshake');
  8. var Heartbeat = require('./commands/heartbeat');
  9. var Kick = require('./commands/kick');
  10. var coder = require('./common/coder');
  11. var curId = 1;
  12. /**
  13. * Connector that manager low level connection and protocol bewteen server and client.
  14. * Develper can provide their own connector to switch the low level prototol, such as tcp or probuf.
  15. */
  16. var Connector = function(port, host, opts) {
  17. if (!(this instanceof Connector)) {
  18. return new Connector(port, host, opts);
  19. }
  20. EventEmitter.call(this);
  21. this.opts = opts || {};
  22. this.port = port;
  23. this.host = host;
  24. this.useDict = opts.useDict;
  25. this.useProtobuf = opts.useProtobuf;
  26. this.handshake = new Handshake(opts);
  27. this.heartbeat = new Heartbeat(opts);
  28. this.distinctHost = opts.distinctHost;
  29. this.ssl = opts.ssl;
  30. this.switcher = null;
  31. };
  32. util.inherits(Connector, EventEmitter);
  33. module.exports = Connector;
  34. /**
  35. * Start connector to listen the specified port
  36. */
  37. Connector.prototype.start = function(cb) {
  38. var app = require('../pomelo').app;
  39. var self = this;
  40. var gensocket = function(socket) {
  41. var hybridsocket = new HybridSocket(curId++, socket);
  42. hybridsocket.on('handshake', self.handshake.handle.bind(self.handshake, hybridsocket));
  43. hybridsocket.on('heartbeat', self.heartbeat.handle.bind(self.heartbeat, hybridsocket));
  44. hybridsocket.on('disconnect', self.heartbeat.clear.bind(self.heartbeat, hybridsocket.id));
  45. hybridsocket.on('closing', Kick.handle.bind(null, hybridsocket));
  46. self.emit('connection', hybridsocket);
  47. };
  48. this.connector = app.components.__connector__.connector;
  49. this.dictionary = app.components.__dictionary__;
  50. this.protobuf = app.components.__protobuf__;
  51. this.decodeIO_protobuf = app.components.__decodeIO__protobuf__;
  52. if(!this.ssl) {
  53. this.listeningServer = net.createServer();
  54. } else {
  55. this.listeningServer = tls.createServer(this.ssl);
  56. }
  57. this.switcher = new Switcher(this.listeningServer, self.opts);
  58. this.switcher.on('connection', function(socket) {
  59. gensocket(socket);
  60. });
  61. if(!!this.distinctHost) {
  62. this.listeningServer.listen(this.port, this.host);
  63. } else {
  64. this.listeningServer.listen(this.port);
  65. }
  66. process.nextTick(cb);
  67. };
  68. Connector.prototype.stop = function(force, cb) {
  69. this.switcher.close();
  70. this.listeningServer.close();
  71. process.nextTick(cb);
  72. };
  73. Connector.decode = Connector.prototype.decode = coder.decode;
  74. Connector.encode = Connector.prototype.encode = coder.encode;