void SyncNetworkRequest::get(const QNetworkRequest &request, const std::function<void (QNetworkReply *)> &callback) { m_requestAborted = false; if (m_timer->isActive()) { m_timer->stop(); } m_reply = m_networkMgr->get(request); if (!m_reply) { callback(Q_NULLPTR); return; } connect(m_reply, &QNetworkReply::finished, [&](){ if (m_timer->isActive()) { m_timer->stop(); } if (m_eventLoop->isRunning()) { m_eventLoop->quit(); } }); if (m_timeoutInterval > 0) { m_timer->start(); } m_eventLoop->exec(); if (!m_requestAborted) { callback(m_reply); } else if (m_timeoutCallback) { m_timeoutCallback(); } }
//#define ____DEBUG_SERIAL_ //#define _DEBUG_SERIAL_ int SerialClass::Read(void) { int fd; if (m_commHandle == INVALID_COMMS_HANDLE) OpenPort(); fd = CommGetDescriptor(m_commHandle); if (fd == -1) { CsErrx("SerialClass::Read: file handle %d is not valid, cannot select/read", m_commHandle); return COMMSERIAL_FAILURE; } // Call timeout if(m_timeoutCallback) m_timeoutCallback(m_noCommsTimer); while (1) { struct timeval tv; fd_set fdset; int ret; FD_ZERO(&fdset); FD_SET(fd, &fdset); tv.tv_sec = 0; tv.tv_usec = 5* 1000 * 1000; // MAGIC 5 seconds #if defined(____DEBUG_SERIAL_) CsDebug(5, (5, "SerialClass::Read: begin select on h:(%d[%d])", m_commHandle, fd)); #endif ret = select(fd + 1, &fdset, NULL, NULL, &tv); switch (ret) { case -1: /* error */ if (errno != EAGAIN && errno != EINTR) { CsDebug(1, (1, "SerialClass::Read: error %d on select", errno)); CsDebug(1, (1, "SerialClass::Read: closing port and reopen after wait")); ClosePort(); usleep(1000000); // Reopen in 1 sec, TODO set in #define // re-open it CsDebug(1, (1, "SerialClass::Read: re-opening port")); OpenPort(); } else { #if defined(_DEBUG_SERIAL_) CsDebug(5, (5, "SerialClass::Read: an expected early return %d on select", errno)); #endif } continue; case 0: /* no data, timed out */ #if defined(____DEBUG_SERIAL_) CsDebug(1, (9, "SerialClass::Read: timeout on select")); #endif continue; default: /* got data, read it */ break; } if (FD_ISSET(fd, &fdset)) { int BytesRead; #define READBUFFERSIZE 64 char buf[ IPC_GENERIC_SIZE(READBUFFERSIZE) ]; IPC_Generic_t *pMsg = (IPC_Generic_t *)buf; BytesRead = read(fd, pMsg->data, READBUFFERSIZE); CsDebug(8, (8, "SerialClass::Read(%d):%d", fd, BytesRead)); if (BytesRead <=0) { if(BytesRead ==0) CsDebug(7, (7, "SerialClass::Read: EOF returned, closing port;")); else CsDebug(7, (7, "SerialClass::Read: fatal error returned, closing port;")); ClosePort(); // Report timeout to running application if(m_timeoutCallback) m_timeoutCallback(m_noCommsTimer); usleep(100*1000); // re-open it CsDebug(7, (7, "SerialClass::Read: re-opening port")); OpenPort(); continue; } else { pMsg->hdr.type = m_reportRecieveCharacter_MessageId; pMsg->hdr.source = m_reportRecieveCharacter_SourceQueueId; pMsg->len = BytesRead; CsDebug(5, (5, "SerialClass::Read: got %u characters", BytesRead)); if ((ret = IpcSend(m_queueId, pMsg, IPC_GENERIC_SIZE(BytesRead))) != 0) CsErrx("SerialClass::Read: failed to report rx-data-available to main thread"); } } else // if (FD_ISSET(pipefd, &fdset)) { CsErrx("SerialClass::Read: unexpected unblock of select()"); } } /*NOTREACHED*/ return 0; }