| 29 | ~Impl() { stop(); } |
| 30 | |
| 31 | bool start(int port, |
| 32 | const OnConnect& onConnect, |
| 33 | const OnError& onError) override { |
| 34 | std::unique_lock<std::mutex> lock(mutex); |
| 35 | stopWithLock(); |
| 36 | socket = std::unique_ptr<dap::Socket>( |
| 37 | new dap::Socket("localhost", std::to_string(port).c_str())); |
| 38 | |
| 39 | if (!socket->isOpen()) { |
| 40 | onError("Failed to open socket"); |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | running = true; |
| 45 | thread = std::thread([=] { |
| 46 | do { |
| 47 | if (auto rw = socket->accept()) { |
| 48 | onConnect(rw); |
| 49 | continue; |
| 50 | } |
| 51 | if (!isRunning()) { |
| 52 | onError("Failed to accept connection"); |
| 53 | } |
| 54 | } while (false); |
| 55 | }); |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | void stop() override { |
| 61 | std::unique_lock<std::mutex> lock(mutex); |