| 30 | ~Impl() { stop(); } |
| 31 | |
| 32 | bool start(int port, |
| 33 | const OnConnect& onConnect, |
| 34 | const OnError& onError) override { |
| 35 | std::unique_lock<std::mutex> lock(mutex); |
| 36 | stopWithLock(); |
| 37 | // modified by ZhangTingAn:‘localhost‘ -> ‘0.0.0.0‘ receive network connections from other machines |
| 38 | // used to remote debug |
| 39 | socket = std::unique_ptr<dap::Socket>( |
| 40 | new dap::Socket("0.0.0.0", std::to_string(port).c_str())); |
| 41 | |
| 42 | if (!socket->isOpen()) { |
| 43 | onError("Failed to open socket"); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | stopped = false; |
| 48 | thread = std::thread([=] { |
| 49 | while (true) { |
| 50 | if (auto rw = socket->accept()) { |
| 51 | onConnect(rw); |
| 52 | continue; |
| 53 | } |
| 54 | if (!stopped) { |
| 55 | onError("Failed to accept connection"); |
| 56 | } |
| 57 | break; |
| 58 | }; |
| 59 | }); |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | void stop() override { |
| 65 | std::unique_lock<std::mutex> lock(mutex); |