| 48 | } |
| 49 | |
| 50 | void connect(const std::string& address, int32_t port, const Shared<ITCPClientListener>& listener) { |
| 51 | auto strongSelf = strongRef(this); |
| 52 | auto connection = makeShared<TCPConnectionImpl>(_ioService); |
| 53 | connection->getSocket().async_connect( |
| 54 | boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(address), static_cast<uint16_t>(port)), |
| 55 | [strongSelf, connection, listener](const boost::system::error_code& errorCode) { |
| 56 | if (errorCode.failed()) { |
| 57 | strongSelf->stopServices(); |
| 58 | listener->onDisconnected(errorFromBoostError(errorCode)); |
| 59 | } else { |
| 60 | { |
| 61 | std::lock_guard<Mutex> lock(strongSelf->_mutex); |
| 62 | strongSelf->_connection = connection; |
| 63 | } |
| 64 | connection->setDisconnectListener( |
| 65 | makeShared<TCPClientDisconnectListenerImpl>(strongSelf, listener).toShared()); |
| 66 | connection->onReady(); |
| 67 | listener->onConnected(connection); |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | auto result = startServices(); |
| 72 | if (!result) { |
| 73 | listener->onDisconnected(result.error()); |
| 74 | return; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void stopServices() { |
| 79 | _started = false; |
nothing calls this directly
no test coverage detected