| 301 | } |
| 302 | |
| 303 | void HarnessServer::HandleClient(SOCKET_T clientSock) |
| 304 | { |
| 305 | std::string buffer; |
| 306 | char recvBuf[4096]; |
| 307 | |
| 308 | while (!_stopRequested.load()) |
| 309 | { |
| 310 | // First, drain any pending events and send to client |
| 311 | auto events = _queue.DrainEvents(); |
| 312 | for (const auto& evt : events) |
| 313 | { |
| 314 | int sent = send(clientSock, evt.json.c_str(), static_cast<int>(evt.json.size()), 0); |
| 315 | if (sent == HARNESS_SOCKET_ERROR) |
| 316 | { |
| 317 | LOG_DEBUG(Core, "[Harness] Send event failed, client gone"); |
| 318 | return; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // Use select() to check for data with timeout |
| 323 | fd_set readSet; |
| 324 | FD_ZERO(&readSet); |
| 325 | FD_SET(clientSock, &readSet); |
| 326 | |
| 327 | timeval tv = {}; |
| 328 | tv.tv_sec = 0; |
| 329 | tv.tv_usec = 50000; // 50ms — check events frequently |
| 330 | |
| 331 | int sel = select(static_cast<int>(clientSock) + 1, &readSet, nullptr, nullptr, &tv); |
| 332 | if (sel < 0) |
| 333 | { |
| 334 | LOG_DEBUG(Core, "[Harness] select() error on client socket"); |
| 335 | return; |
| 336 | } |
| 337 | if (sel == 0) |
| 338 | continue; // timeout — loop back to drain events |
| 339 | |
| 340 | int bytes = recv(clientSock, recvBuf, sizeof(recvBuf) - 1, 0); |
| 341 | if (bytes <= 0) |
| 342 | { |
| 343 | LOG_DEBUG(Core, "[Harness] Client connection closed (recv={})", bytes); |
| 344 | return; |
| 345 | } |
| 346 | recvBuf[bytes] = '\0'; |
| 347 | buffer += recvBuf; |
| 348 | |
| 349 | // Process complete lines |
| 350 | size_t pos; |
| 351 | while ((pos = buffer.find('\n')) != std::string::npos) |
| 352 | { |
| 353 | std::string line = buffer.substr(0, pos); |
| 354 | buffer.erase(0, pos + 1); |
| 355 | |
| 356 | while (!line.empty() && (line.back() == '\r' || line.back() == ' ')) |
| 357 | line.pop_back(); |
| 358 | if (line.empty()) |
| 359 | continue; |
| 360 |
nothing calls this directly
no test coverage detected