File: PrintRunner.cpp

package info (click to toggle)
herculesstudio 1.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,376 kB
  • sloc: cpp: 17,077; xml: 21; makefile: 13
file content (183 lines) | stat: -rw-r--r-- 4,990 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "PrintRunner.h"
#include "PrinterItem.h"

#include <QHostAddress>
#include <QElapsedTimer>
#include <QtGlobal>
#include <algorithm>

PrinterSocket::PrinterSocket() :
    QTcpSocket()
{
    mBuffPos = mBuff;
}

QByteArray PrinterSocket::returnLine(const char *end)
{
    end++;
    QByteArray line(mBuff, end-mBuff);

    memmove(mBuff, end, mBuffPos-end);

    mBuffPos -= (end - mBuff);
    return line;
}

char *PrinterSocket::nextPos()
{
    char * maybeEndF = std::find(mBuff, mBuffPos, '\f');
    char * maybeEndN = std::find(mBuff, mBuffPos, '\n');
    char * maybeEndR = std::find(mBuff, mBuffPos, '\r');

    char * ret = std::min(std::min(maybeEndF, maybeEndN), maybeEndR);
    if (ret < mBuffPos) return ret;

    return NULL;
}

QByteArray PrinterSocket::readLine()
{
    char *pos = nextPos();
    if (pos != NULL)
    {
        return returnLine(pos);
    }

    while (true)
    {
        waitForReadyRead(500);
        if (bytesAvailable() > 0)
        {
            qint64 len = read(mBuffPos, 255);
            if (len < 0) return "";
            mBuffPos += len;
            pos = nextPos();
            if (pos != NULL) return returnLine(pos);
        }
    }
}

qint64 PrinterSocket::bytesAvailable() const
{
    return QTcpSocket::bytesAvailable() + (mBuffPos - mBuff);
}

PrintRunner::PrintRunner(SynchronizedQueue& queue, PrinterItemConstPtr& printerItem, int maxQueueSize) :
    Runner(queue), mQueue(queue), mPrinterItem(printerItem), mMaxQueueSize(maxQueueSize)
{
}

void PrintRunner::readFromSocket()
{
    while((mSocket->state() == QAbstractSocket::ConnectedState && mRunning) || mSocket->bytesAvailable() > 0)
    {
        mSocket->waitForReadyRead(500);
        if (mSocket->bytesAvailable() > 0)
        {
            bool ff = false;
            QByteArray line = mSocket->readLine();
            int len = line.length();
            if ((line[len-1] == '\f') && (len > 1)) // in practice this is not likely to happen as all \f are preceded by \r or \n
            {
                line.data()[len-1] = '\n';
                ff = true;
            }

            if ( (len>0) &&  ((line[len-1] == '\n') || (line[len-1] == '\r')) )
                line.data()[len-1] = '\0';

            if (line.length() != 0)
            {
                hOutDebug(5,":" << line.data());
                if (ff) mQueue.push_back("\f"); // generate eject
#ifdef Q_OS_DARWIN
                else mQueue.push_back((line));
#else
                else mQueue.push_back(std::move(line));
#endif
                emit newData();
                while (mQueue.size() > mMaxQueueSize) // do not flood the queue
                    QThread::msleep(100);
            }
        }
    }
}

void PrintRunner::waitForConnected()
{
    while(mSocket->state() != QAbstractSocket::ConnectedState && mRunning)
    {
        hOutDebug(1,"not connected:" << mSocket->state());
        if (mSocket->state() == QAbstractSocket::UnconnectedState)
        {
            mSocket->connectToHost(QHostAddress(mPrinterItem->mIp), mPrinterItem->mPort);
            mSocket->waitForConnected(1000);
            if(mRunning && mSocket->state() != QAbstractSocket::ConnectedState)
                QThread::msleep(200);
            hOutDebug(1, "error:" << mSocket->errorString().toStdString());
        }
        else
        {
            QThread::sleep(1);
        }
    }
}

void PrintRunner::run()
{
    mSocket = QSharedPointer<PrinterSocket>(new PrinterSocket());
    mRunning = true;

    qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState");
    mSocket->moveToThread(this);
    connect(mSocket.data(), SIGNAL(stateChanged(QAbstractSocket::SocketState)),this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));

    while(mRunning)
    {
        QElapsedTimer timer;
        timer.start();
        emit waiting();
        waitForConnected();

        if (!mRunning) break;

        emit connected();
        hOutDebug(1,"appl connected");

        readFromSocket();
        hOutDebug(1, "state:" << mSocket->state() << " running: " << (mRunning? "y" : "n") );
        emit disconnected();
        if (timer.elapsed() < 1000)
        {
            QThread::msleep(1000);
        }
    }

    emit stoppedWaiting();
    mSocket->close();
}

void PrintRunner::socketStateChanged(QAbstractSocket::SocketState state)
{
    static QAbstractSocket::SocketState prevState = QAbstractSocket::UnconnectedState;
    if (state != prevState)
    {
        if (state == QAbstractSocket::ConnectedState && prevState != QAbstractSocket::ConnectedState)
        {
            hOutDebug(1,"Connected");
            emit connected();
        }
        if (state != QAbstractSocket::ConnectedState && prevState == QAbstractSocket::ConnectedState)
        {
            hOutDebug(1,"Disconnected");
            emit disconnected();
        }
        prevState = state;
    }
    hOutDebug(1, "Socket state: " << state);
}

void PrintRunner::stop()
{
    mRunning = false;
}