@@ -146,7 +146,7 @@ size_t ConnectionImpl::parse(const Buffer &buffer)
146146 }
147147
148148 // leap out if the connection object no longer exists
149- if (!monitor.valid () || !_closed || _state = = state_connected) return processed;
149+ if (!monitor.valid () || !_closed || _state ! = state_connected) return processed;
150150
151151 // the close() function was called, but if the close frame was not yet sent
152152 // if there are no waiting channels, we can do that right now
@@ -189,9 +189,9 @@ bool ConnectionImpl::close()
189189 }
190190
191191 // if still busy with handshake, we delay closing for a while
192- if (waiters > 0 || _state == state_handshake || _state == state_protocol ) return true ;
192+ if (waiters > 0 || _state != state_connected ) return true ;
193193
194- // perform the close operation
194+ // perform the close frame
195195 sendClose ();
196196
197197 // done
@@ -229,11 +229,6 @@ void ConnectionImpl::setConnected()
229229 // store connected state
230230 _state = state_connected;
231231
232- // if the close method was called before, the frame was not
233- // sent. append it to the end of the queue to make sure we
234- // are correctly closed down.
235- if (_closed && !waiting () && !sendClose ()) return ;
236-
237232 // we're going to call the handler, which can destruct the connection,
238233 // so we must monitor if the queue object is still valid after calling
239234 Monitor monitor (this );
@@ -253,6 +248,13 @@ void ConnectionImpl::setConnected()
253248 // send it
254249 _handler->onData (_parent, buffer.data (), buffer.size ());
255250 }
251+
252+ // leap out if object is dead
253+ if (!monitor.valid ()) return ;
254+
255+ // if the close method was called before, and no channel is waiting
256+ // for an answer, we can now safely send out the close frame
257+ if (_closed && state == _state_connected && !waiting ()) sendClose ();
256258}
257259
258260/* *
0 commit comments