| 512 | } |
| 513 | |
| 514 | void ConnectAsync(const std::string& address, uint16_t port) override |
| 515 | { |
| 516 | if (_status != SocketStatus::closed) |
| 517 | { |
| 518 | throw std::runtime_error("Socket not closed."); |
| 519 | } |
| 520 | |
| 521 | // When connect is called, the status is set to resolving, but we want to make sure |
| 522 | // the status is changed before this async method exits. Otherwise, the consumer |
| 523 | // might think the status has closed before it started to connect. |
| 524 | _status = SocketStatus::waiting; |
| 525 | |
| 526 | auto saddress = std::string(address); |
| 527 | std::promise<void> barrier; |
| 528 | _connectFuture = barrier.get_future(); |
| 529 | auto thread = std::thread( |
| 530 | [this, saddress, port](std::promise<void> barrier2) -> void { |
| 531 | try |
| 532 | { |
| 533 | Connect(saddress.c_str(), port); |
| 534 | } |
| 535 | catch (const std::exception& ex) |
| 536 | { |
| 537 | _error = std::string(ex.what()); |
| 538 | } |
| 539 | barrier2.set_value(); |
| 540 | }, |
| 541 | std::move(barrier)); |
| 542 | thread.detach(); |
| 543 | } |
| 544 | |
| 545 | void Finish() override |
| 546 | { |
no test coverage detected