@@ -15,8 +15,9 @@ function Connection(options) {
1515
1616 this . config = options . config ;
1717
18- this . _socket = options . socket ;
18+ this . _socket = null ;
1919 this . _protocol = new Protocol ( { config : this . config , connection : this } ) ;
20+ this . _protocolSetup = false ;
2021 this . _connectCalled = false ;
2122 this . state = 'disconnected' ;
2223 this . threadId = null ;
@@ -67,19 +68,29 @@ Connection.prototype.connect = function connect(options, callback) {
6768 options = { } ;
6869 }
6970
71+ this . _setupProtocolHandlers ( ) ;
72+ this . _protocol . handshake ( options , wrapCallbackInDomain ( this , callback ) ) ;
73+
7074 if ( ! this . _connectCalled ) {
7175 this . _connectCalled = true ;
76+ this . _socket = null ;
7277
73- // Connect either via a UNIX domain socket or a TCP socket.
74- this . _socket = ( this . config . socketPath )
75- ? Net . createConnection ( this . config . socketPath )
76- : Net . createConnection ( this . config . port , this . config . host ) ;
78+ try {
79+ // Connect either via a UNIX domain socket or a TCP socket.
80+ this . _socket = ( this . config . socketPath )
81+ ? Net . createConnection ( this . config . socketPath )
82+ : Net . createConnection ( this . config . port , this . config . host ) ;
83+ } catch ( err ) {
84+ this . _handleNetworkError ( err ) ;
85+ return ;
86+ }
7787
7888 // Connect socket to connection domain
7989 if ( Events . usingDomains ) {
8090 this . _socket . domain = this . domain ;
8191 }
8292
93+ // socket <-> protocol
8394 var connection = this ;
8495 this . _protocol . on ( 'data' , function ( data ) {
8596 connection . _socket . write ( data ) ;
@@ -94,15 +105,11 @@ Connection.prototype.connect = function connect(options, callback) {
94105 connection . _protocol . end ( ) ;
95106 } ) ) ;
96107
108+ // Set up socket handlers
97109 this . _socket . on ( 'error' , this . _handleNetworkError . bind ( this ) ) ;
98110 this . _socket . on ( 'connect' , this . _handleProtocolConnect . bind ( this ) ) ;
99- this . _protocol . on ( 'handshake' , this . _handleProtocolHandshake . bind ( this ) ) ;
100- this . _protocol . on ( 'initialize' , this . _handleProtocolInitialize . bind ( this ) ) ;
101- this . _protocol . on ( 'unhandledError' , this . _handleProtocolError . bind ( this ) ) ;
102- this . _protocol . on ( 'drain' , this . _handleProtocolDrain . bind ( this ) ) ;
103- this . _protocol . on ( 'end' , this . _handleProtocolEnd . bind ( this ) ) ;
104- this . _protocol . on ( 'enqueue' , this . _handleProtocolEnqueue . bind ( this ) ) ;
105111
112+ // Set connect timeout
106113 if ( this . config . connectTimeout ) {
107114 var handleConnectTimeout = this . _handleConnectTimeout . bind ( this ) ;
108115
@@ -112,8 +119,6 @@ Connection.prototype.connect = function connect(options, callback) {
112119 } ) ;
113120 }
114121 }
115-
116- this . _protocol . handshake ( options , wrapCallbackInDomain ( this , callback ) ) ;
117122} ;
118123
119124Connection . prototype . changeUser = function changeUser ( options , callback ) {
@@ -241,18 +246,28 @@ Connection.prototype.end = function end(options, callback) {
241246
242247Connection . prototype . destroy = function ( ) {
243248 this . state = 'disconnected' ;
244- this . _implyConnect ( ) ;
245- this . _socket . destroy ( ) ;
249+ this . _setupProtocolHandlers ( ) ;
250+
251+ if ( this . _socket ) {
252+ this . _socket . destroy ( ) ;
253+ }
254+
246255 this . _protocol . destroy ( ) ;
247256} ;
248257
249258Connection . prototype . pause = function ( ) {
250- this . _socket . pause ( ) ;
259+ if ( this . _socket ) {
260+ this . _socket . pause ( ) ;
261+ }
262+
251263 this . _protocol . pause ( ) ;
252264} ;
253265
254266Connection . prototype . resume = function ( ) {
255- this . _socket . resume ( ) ;
267+ if ( this . _socket ) {
268+ this . _socket . resume ( ) ;
269+ }
270+
256271 this . _protocol . resume ( ) ;
257272} ;
258273
@@ -450,11 +465,25 @@ Connection.prototype._handleProtocolEnqueue = function _handleProtocolEnqueue(se
450465} ;
451466
452467Connection . prototype . _implyConnect = function ( ) {
453- if ( ! this . _connectCalled ) {
468+ this . _setupProtocolHandlers ( ) ;
469+
470+ if ( ! this . _connectCalled && ! this . _protocol . _destroyed ) {
454471 this . connect ( ) ;
455472 }
456473} ;
457474
475+ Connection . prototype . _setupProtocolHandlers = function _setupProtocolHandlers ( ) {
476+ if ( ! this . _protocolSetup ) {
477+ this . _protocolSetup = true ;
478+ this . _protocol . on ( 'handshake' , this . _handleProtocolHandshake . bind ( this ) ) ;
479+ this . _protocol . on ( 'initialize' , this . _handleProtocolInitialize . bind ( this ) ) ;
480+ this . _protocol . on ( 'unhandledError' , this . _handleProtocolError . bind ( this ) ) ;
481+ this . _protocol . on ( 'drain' , this . _handleProtocolDrain . bind ( this ) ) ;
482+ this . _protocol . on ( 'end' , this . _handleProtocolEnd . bind ( this ) ) ;
483+ this . _protocol . on ( 'enqueue' , this . _handleProtocolEnqueue . bind ( this ) ) ;
484+ }
485+ } ;
486+
458487function createSecureContext ( config , cb ) {
459488 var context = null ;
460489 var error = null ;
0 commit comments