hybridconnector.js 2.9 KB

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