* Connects a socket and sends the startup protocol messages. * Note that when open() callbacks in error, the caller should immediately call Connection#close.
(callback)
| 170 | * Note that when open() callbacks in error, the caller should immediately call {@link Connection#close}. |
| 171 | */ |
| 172 | open(callback) { |
| 173 | const self = this; |
| 174 | this.log('info', `Connecting to ${this.endpointFriendlyName}`); |
| 175 | |
| 176 | if (!this.options.sslOptions) { |
| 177 | this.netClient = new net.Socket({ highWaterMark: this.options.socketOptions.coalescingThreshold }); |
| 178 | this.netClient.connect(this.port, this.address, function connectCallback() { |
| 179 | self.log('verbose', `Socket connected to ${self.endpointFriendlyName}`); |
| 180 | self.bindSocketListeners(); |
| 181 | self.startup(callback); |
| 182 | }); |
| 183 | } |
| 184 | else { |
| 185 | // Use TLS |
| 186 | const sslOptions = utils.extend({ rejectUnauthorized: false }, this.options.sslOptions); |
| 187 | |
| 188 | if (this.options.sni) { |
| 189 | sslOptions.servername = this._serverName; |
| 190 | } |
| 191 | |
| 192 | this.netClient = tls.connect(this.port, this.address, sslOptions, function tlsConnectCallback() { |
| 193 | self.log('verbose', `Secure socket connected to ${self.endpointFriendlyName} with protocol ${self.netClient.getProtocol()}`); |
| 194 | self.bindSocketListeners(); |
| 195 | self.startup(callback); |
| 196 | }); |
| 197 | |
| 198 | // TLSSocket will validate for values from 512 to 16K (depending on the SSL protocol version) |
| 199 | this.netClient.setMaxSendFragment(this.options.socketOptions.coalescingThreshold); |
| 200 | } |
| 201 | |
| 202 | this.netClient.once('error', function socketError(err) { |
| 203 | self.errorConnecting(err, false, callback); |
| 204 | }); |
| 205 | |
| 206 | this.netClient.once('timeout', function connectTimedOut() { |
| 207 | const err = new types.DriverError('Connection timeout'); |
| 208 | self.errorConnecting(err, true, callback); |
| 209 | }); |
| 210 | |
| 211 | this.netClient.setTimeout(this.options.socketOptions.connectTimeout); |
| 212 | |
| 213 | // Improve failure detection with TCP keep-alives |
| 214 | if (this.options.socketOptions.keepAlive) { |
| 215 | this.netClient.setKeepAlive(true, this.options.socketOptions.keepAliveDelay); |
| 216 | } |
| 217 | |
| 218 | this.netClient.setNoDelay(!!this.options.socketOptions.tcpNoDelay); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Determines the protocol version to use and sends the STARTUP request |
no test coverage detected