Exemple #1
0
QChar TextReaderBase::getNextCharacter()
{
    bool couldRead=true;
    if(buffer.isEmpty()){
        couldRead=readIntoBuffer();
    }else if(bufferPos==buffer.size()){
        couldRead=readIntoBuffer();
        if(couldRead){
            ++lineNo;
        }
    }

    if(!couldRead){
        return QChar();
    }

    QChar nextChar=buffer.at(bufferPos);
    ++bufferPos;
    ++pos;
    ++linePos;

    return nextChar;
}
QByteArray AndroidRfComm::receive(const int maxNumOfBytes, const int waitMilliSeconds) {
    qint64 maxTime=QDateTime::currentMSecsSinceEpoch()+waitMilliSeconds;
    timeout=false;
    while (buffer.length()<maxNumOfBytes) {
        if (QDateTime::currentMSecsSinceEpoch()>maxTime) {
            timeout=true;
            break;
        }
        QThread::msleep(10);
        QCoreApplication::processEvents();
        readIntoBuffer();
    }
    // Truncate the result and keep only the remainder in the buffer
    QByteArray result=buffer.left(maxNumOfBytes);
    buffer.remove(0,result.length());
    return result;
}
QString AndroidRfComm::receiveLine(const int maxCharacters, const int waitMilliSeconds) {
    qint64 maxTime=QDateTime::currentMSecsSinceEpoch()+waitMilliSeconds;
    // Receive repeatedly
    timeout=false;
    QString line=useLatin1?QString::fromLatin1(buffer):QString::fromUtf8(buffer);
    int posi=line.indexOf(receiveTerminator);
    while (posi<0 && line.length()<maxCharacters+receiveTerminator.length() && !timeout) {
        int numRead=0;
        while (numRead==0) {
            if (QDateTime::currentMSecsSinceEpoch()>maxTime) {
                timeout=true;
                break;
            }
            QThread::msleep(10);
            QCoreApplication::processEvents();
            numRead=readIntoBuffer();
        }
        line=useLatin1?QString::fromLatin1(buffer):QString::fromUtf8(buffer);
        posi=line.indexOf(receiveTerminator);
    }
    //  Truncate the line
    if (posi>=0 && posi<=maxCharacters) {
        // The line is terminated
        line=line.left(posi+receiveTerminator.length());
    }
    else {
        // The line is not terminated or longer than allowed
        line=line.left(maxCharacters);
    }
    // Remove the line from the buffer
    buffer.remove(0,line.toUtf8().length());
    // Remove the terminator from the line, if present
    if (posi>=0 && posi<=maxCharacters) {
        line=line.left(posi);
    }
    return line;
}
int ServerSocket::processEventThread()
{
    epoll_event ev;
    ev.events = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP;
    epoll_event events[EPOLL_SIZE];
    sockaddr clientAddr;
    socklen_t addrlen = sizeof(sockaddr);
    while (true)
    {
        int eventCount = epoll_wait(epfd, events, EPOLL_SIZE, -1);
        if (eventCount == -1)
        {
            if (errno == EINTR)
            {
                continue;
            }
            Log::e("epoll wait error=%s", strerror(errno));
            return -1;
        }
        for (int i = 0; i < eventCount; ++i)
        {
            epoll_event& evt(events[i]);
            if (evt.events & EPOLLERR || evt.events & EPOLLHUP)
            {
                // handle errors
            }
            if (evt.events & EPOLLIN)
            {
                if (evt.data.fd == listenSocket)
                {
                    while (true)
                    {
                        Socket acceptedSocket = accept4(listenSocket, &clientAddr, &addrlen, SOCK_NONBLOCK);
                        if (acceptedSocket == -1)
                        {
                            if (errno != EAGAIN && errno != EWOULDBLOCK)
                            {
                                Log::e("accept error: %s", strerror(errno));
                            }
                            break;
                        }
                        if (numClients < config.maxConnection)
                        {
                            ev.data.ptr = addClient(acceptedSocket, (sockaddr_in&)clientAddr);
                            epoll_ctl(epfd, EPOLL_CTL_ADD, acceptedSocket, &ev);
                        }
                        else
                        {
                            close(acceptedSocket);
                        }
                    }
                }
                else
                {
                    ClientSocket* client = (ClientSocket*)evt.data.ptr;
                    readIntoBuffer(client);
                }
            }
            if (evt.events & EPOLLOUT)
            {
                ClientSocket* client = (ClientSocket*)evt.data.ptr;
                writeFromBuffer(client);
            }
            if (evt.events & EPOLLRDHUP)
            {
                ClientSocket* client = (ClientSocket*)evt.data.ptr;
                client->isClosing = true;
            }
        }
    }
    return 0;
}	//end of processEvent
Exemple #5
0
void QSerial::canReadNotification( int )
{
	readIntoBuffer();
	emit readyRead();
}