* Creates a new instance of Connection. * @param {String} endpoint An string containing ip address and port of the host * @param {Number|null} protocolVersion * @param {ClientOptions} options
(endpoint, protocolVersion, options)
| 47 | * @param {ClientOptions} options |
| 48 | */ |
| 49 | constructor(endpoint, protocolVersion, options) { |
| 50 | super(); |
| 51 | |
| 52 | this.setMaxListeners(0); |
| 53 | |
| 54 | if (!options) { |
| 55 | throw new Error('options is not defined'); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Gets the ip and port of the server endpoint. |
| 60 | * @type {String} |
| 61 | */ |
| 62 | this.endpoint = endpoint; |
| 63 | |
| 64 | /** |
| 65 | * Gets the friendly name of the host, used to identify the connection in log messages. |
| 66 | * With direct connect, this is the address and port. |
| 67 | * With SNI, this will be the address and port of the proxy, plus the server name. |
| 68 | * @type {String} |
| 69 | */ |
| 70 | this.endpointFriendlyName = this.endpoint; |
| 71 | |
| 72 | if (options.sni) { |
| 73 | this._serverName = endpoint; |
| 74 | this.endpoint = `${options.sni.addressResolver.getIp()}:${options.sni.port}`; |
| 75 | this.endpointFriendlyName = `${this.endpoint} (${this._serverName})`; |
| 76 | } |
| 77 | |
| 78 | if (!this.endpoint || this.endpoint.indexOf(':') < 0) { |
| 79 | throw new Error('EndPoint must contain the ip address and port separated by : symbol'); |
| 80 | } |
| 81 | |
| 82 | const portSeparatorIndex = this.endpoint.lastIndexOf(':'); |
| 83 | this.address = this.endpoint.substr(0, portSeparatorIndex); |
| 84 | this.port = this.endpoint.substr(portSeparatorIndex + 1); |
| 85 | |
| 86 | Object.defineProperty(this, "options", { value: options, enumerable: false, writable: false}); |
| 87 | |
| 88 | if (protocolVersion === null) { |
| 89 | // Set initial protocol version |
| 90 | protocolVersion = types.protocolVersion.maxSupported; |
| 91 | if (options.protocolOptions.maxVersion) { |
| 92 | // User provided the protocol version |
| 93 | protocolVersion = options.protocolOptions.maxVersion; |
| 94 | } |
| 95 | // Allow to check version using this connection instance |
| 96 | this._checkingVersion = true; |
| 97 | } |
| 98 | |
| 99 | this.log = utils.log; |
| 100 | this.protocolVersion = protocolVersion; |
| 101 | this._operations = new Map(); |
| 102 | this._pendingWrites = []; |
| 103 | this._preparing = new Map(); |
| 104 | |
| 105 | /** |
| 106 | * The timeout state for the idle request (heartbeat) |