| 104 | } |
| 105 | |
| 106 | bool DapSession::start() |
| 107 | { |
| 108 | auto onClientConnected = [this](const std::shared_ptr<ReaderWriter> &socket) { |
| 109 | QMetaObject::invokeMethod(this, "initialize", Qt::BlockingQueuedConnection, Q_ARG(std::shared_ptr<dap::ReaderWriter>, socket)); |
| 110 | }; |
| 111 | |
| 112 | auto onError = [](const char *errMessage) { |
| 113 | Log(errMessage) |
| 114 | }; |
| 115 | |
| 116 | // Create the network server |
| 117 | d->server = dap::net::Server::create(); |
| 118 | |
| 119 | auto checkPortFree = [](int port) { |
| 120 | QProcess process; |
| 121 | QString cmd = QString("fuser %1/tcp").arg(port); |
| 122 | process.start(cmd); |
| 123 | process.waitForFinished(); |
| 124 | QString ret = process.readAll(); |
| 125 | if (ret.isEmpty()) |
| 126 | return true; |
| 127 | return false; |
| 128 | }; |
| 129 | |
| 130 | bool ret = false; |
| 131 | int port = kPort; |
| 132 | while (port - kPort < maxTryNum) { |
| 133 | if (checkPortFree(port)) { |
| 134 | // Start listening on kPort. |
| 135 | // onClientConnected will be called when a client wants to connect. |
| 136 | // onError will be called on any connection errors. |
| 137 | ret = d->server->start(port, onClientConnected, onError); |
| 138 | if (ret) { |
| 139 | d->serverInfo.setPort(port); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | port++; |
| 144 | } |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | void DapSession::stop() |
| 149 | { |
no test coverage detected