|
21 | 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
22 | 22 | */
|
23 | 23 |
|
| 24 | +#include <assert.h> |
24 | 25 | #include <inttypes.h>
|
25 | 26 | #include <iterator>
|
26 | 27 | #include <stdio.h>
|
@@ -204,16 +205,30 @@ bool Client::close()
|
204 | 205 |
|
205 | 206 | setState(ClosingState);
|
206 | 207 |
|
207 |
| - uv_read_stop(reinterpret_cast<uv_stream_t*>(m_socket)); |
| 208 | + uv_stream_t *stream = reinterpret_cast<uv_stream_t*>(m_socket); |
208 | 209 |
|
209 |
| - uv_shutdown(new uv_shutdown_t, reinterpret_cast<uv_stream_t*>(m_socket), [](uv_shutdown_t* req, int status) { |
| 210 | + if (uv_is_readable(stream) == 1) { |
| 211 | + uv_read_stop(stream); |
| 212 | + } |
210 | 213 |
|
211 |
| - if (uv_is_closing(reinterpret_cast<uv_handle_t*>(req->handle)) == 0) { |
212 |
| - uv_close(reinterpret_cast<uv_handle_t*>(req->handle), Client::onClose); |
213 |
| - } |
| 214 | + if (uv_is_writable(stream) == 1) { |
| 215 | + const int rc = uv_shutdown(new uv_shutdown_t, stream, [](uv_shutdown_t* req, int status) { |
| 216 | + if (uv_is_closing(reinterpret_cast<uv_handle_t*>(req->handle)) == 0) { |
| 217 | + uv_close(reinterpret_cast<uv_handle_t*>(req->handle), Client::onClose); |
| 218 | + } |
214 | 219 |
|
215 |
| - delete req; |
216 |
| - }); |
| 220 | + delete req; |
| 221 | + }); |
| 222 | + |
| 223 | + assert(rc == 0); |
| 224 | + |
| 225 | + if (rc != 0) { |
| 226 | + onClose(); |
| 227 | + } |
| 228 | + } |
| 229 | + else { |
| 230 | + uv_close(reinterpret_cast<uv_handle_t*>(m_socket), Client::onClose); |
| 231 | + } |
217 | 232 |
|
218 | 233 | return true;
|
219 | 234 | }
|
@@ -442,6 +457,18 @@ void Client::login()
|
442 | 457 | }
|
443 | 458 |
|
444 | 459 |
|
| 460 | +void Client::onClose() |
| 461 | +{ |
| 462 | + delete m_socket; |
| 463 | + |
| 464 | + m_stream = nullptr; |
| 465 | + m_socket = nullptr; |
| 466 | + setState(UnconnectedState); |
| 467 | + |
| 468 | + reconnect(); |
| 469 | +} |
| 470 | + |
| 471 | + |
445 | 472 | void Client::parse(char *line, size_t len)
|
446 | 473 | {
|
447 | 474 | startTimeout();
|
@@ -655,13 +682,7 @@ void Client::onClose(uv_handle_t *handle)
|
655 | 682 | return;
|
656 | 683 | }
|
657 | 684 |
|
658 |
| - delete client->m_socket; |
659 |
| - |
660 |
| - client->m_stream = nullptr; |
661 |
| - client->m_socket = nullptr; |
662 |
| - client->setState(UnconnectedState); |
663 |
| - |
664 |
| - client->reconnect(); |
| 685 | + client->onClose(); |
665 | 686 | }
|
666 | 687 |
|
667 | 688 |
|
|
0 commit comments