| 366 | } |
| 367 | |
| 368 | void Client::threadEntry() { |
| 369 | SI_LOG_INFO("Setting up DVBAPI client"); |
| 370 | |
| 371 | struct pollfd pfd[1]; |
| 372 | |
| 373 | // set time to try to connect |
| 374 | std::time_t retryTime = std::time(nullptr) + 2; |
| 375 | |
| 376 | for (;; ) { |
| 377 | // try to connect to server |
| 378 | if (!_connected) { |
| 379 | pfd[0].events = POLLIN | POLLHUP | POLLRDNORM | POLLERR; |
| 380 | pfd[0].revents = 0; |
| 381 | pfd[0].fd = -1; |
| 382 | if (_enabled) { |
| 383 | const std::time_t currTime = std::time(nullptr); |
| 384 | if (retryTime < currTime) { |
| 385 | if (initClientSocket(_client, _serverIPAddr, _serverPort)) { |
| 386 | sendClientInfo(); |
| 387 | pfd[0].fd = _client.getFD(); |
| 388 | } else { |
| 389 | _client.closeFD(); |
| 390 | retryTime = currTime + 5; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | // call poll with a timeout of 500 ms |
| 396 | const int pollRet = poll(pfd, 1, 500); |
| 397 | if (pollRet > 0) { |
| 398 | if (pfd[0].revents != 0) { |
| 399 | char tmpData[2048]; |
| 400 | auto i = 0; |
| 401 | const ssize_t size = _client.recvDatafrom(tmpData, sizeof(tmpData) - 1, MSG_DONTWAIT); |
| 402 | const unsigned char *buf = reinterpret_cast<unsigned char *>(&tmpData); |
| 403 | if (size > 0) { |
| 404 | while (i < size) { |
| 405 | // get command |
| 406 | const uint32_t cmd = (buf[i + 0] << 24) | (buf[i + 1] << 16) | (buf[i + 2] << 8) | buf[i + 3]; |
| 407 | SI_LOG_DEBUG("Stream: %d, Receive data total size %u - cmd: 0x%X", buf[i + 4] - _adapterOffset, size, cmd); |
| 408 | |
| 409 | switch (cmd) { |
| 410 | case DVBAPI_SERVER_INFO: { |
| 411 | _serverName.assign(reinterpret_cast<const char *>(&buf[i + 7]), buf[i + 6]); |
| 412 | SI_LOG_INFO("Connected to %s", _serverName.c_str()); |
| 413 | _connected = true; |
| 414 | |
| 415 | // Goto next cmd |
| 416 | i += 7 + _serverName.size(); |
| 417 | break; |
| 418 | } |
| 419 | case DVBAPI_DMX_SET_FILTER: { |
| 420 | const int adapter = buf[i + 4] - _adapterOffset; |
| 421 | const int demux = buf[i + 5]; |
| 422 | const int filter = buf[i + 6]; |
| 423 | const int pid = (buf[i + 7] << 8) | buf[i + 8]; |
| 424 | const unsigned char *filterData = &buf[i + 9]; |
| 425 | const unsigned char *filterMask = &buf[i + 25]; |
nothing calls this directly
no test coverage detected