| 1 | #include "PrintRunner.h" |
| 2 | #include "PrinterItem.h" |
| 3 | |
| 4 | #include <QHostAddress> |
| 5 | #include <QElapsedTimer> |
| 6 | #include <QtGlobal> |
| 7 | #include <algorithm> |
| 8 | |
| 9 | PrinterSocket::PrinterSocket() : |
| 10 | QTcpSocket() |
| 11 | { |
| 12 | mBuffPos = mBuff; |
| 13 | } |
| 14 | |
| 15 | QByteArray PrinterSocket::returnLine(const char *end) |
| 16 | { |
| 17 | end++; |
| 18 | QByteArray line(mBuff, end-mBuff); |
| 19 | |
| 20 | memmove(mBuff, end, mBuffPos-end); |
| 21 | |
| 22 | mBuffPos -= (end - mBuff); |
| 23 | return line; |
| 24 | } |
| 25 | |
| 26 | char *PrinterSocket::nextPos() |
| 27 | { |
| 28 | char * maybeEndF = std::find(mBuff, mBuffPos, '\f'); |
| 29 | char * maybeEndN = std::find(mBuff, mBuffPos, '\n'); |
| 30 | char * maybeEndR = std::find(mBuff, mBuffPos, '\r'); |
| 31 | |
| 32 | char * ret = std::min(std::min(maybeEndF, maybeEndN), maybeEndR); |
| 33 | if (ret < mBuffPos) return ret; |
| 34 | |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | QByteArray PrinterSocket::readLine() |
| 39 | { |
| 40 | char *pos = nextPos(); |
| 41 | if (pos != NULL) |
| 42 | { |
| 43 | return returnLine(pos); |
| 44 | } |
| 45 | |
| 46 | while (true) |
| 47 | { |
| 48 | waitForReadyRead(500); |
| 49 | if (bytesAvailable() > 0) |
| 50 | { |
| 51 | qint64 len = read(mBuffPos, 255); |
| 52 | if (len < 0) return ""; |
| 53 | mBuffPos += len; |
| 54 | pos = nextPos(); |
| 55 | if (pos != NULL) return returnLine(pos); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | qint64 PrinterSocket::bytesAvailable() const |
| 61 | { |
| 62 | return QTcpSocket::bytesAvailable() + (mBuffPos - mBuff); |
| 63 | } |
| 64 | |
| 65 | PrintRunner::PrintRunner(SynchronizedQueue& queue, PrinterItemConstPtr& printerItem, int maxQueueSize) : |
| 66 | Runner(queue), mQueue(queue), mPrinterItem(printerItem), mMaxQueueSize(maxQueueSize) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | void PrintRunner::readFromSocket() |
| 71 | { |
| 72 | while((mSocket->state() == QAbstractSocket::ConnectedState && mRunning) || mSocket->bytesAvailable() > 0) |
| 73 | { |
| 74 | mSocket->waitForReadyRead(500); |
| 75 | if (mSocket->bytesAvailable() > 0) |
| 76 | { |
| 77 | bool ff = false; |
| 78 | QByteArray line = mSocket->readLine(); |
| 79 | int len = line.length(); |
| 80 | if ((line[len-1] == '\f') && (len > 1)) // in practice this is not likely to happen as all \f are preceded by \r or \n |
| 81 | { |
| 82 | line.data()[len-1] = '\n'; |
| 83 | ff = true; |
| 84 | } |
| 85 | |
| 86 | if ( (len>0) && ((line[len-1] == '\n') || (line[len-1] == '\r')) ) |
| 87 | line.data()[len-1] = '\0'; |
| 88 | |
| 89 | if (line.length() != 0) |
| 90 | { |
| 91 | hOutDebug(5,":" << line.data()); |
| 92 | if (ff) mQueue.push_back("\f"); // generate eject |
| 93 | #ifdef Q_OS_DARWIN |
| 94 | else mQueue.push_back((line)); |
| 95 | #else |
| 96 | else mQueue.push_back(std::move(line)); |
| 97 | #endif |
| 98 | emit newData(); |
| 99 | while (mQueue.size() > mMaxQueueSize) // do not flood the queue |
| 100 | QThread::msleep(100); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void PrintRunner::waitForConnected() |
| 107 | { |
| 108 | while(mSocket->state() != QAbstractSocket::ConnectedState && mRunning) |
| 109 | { |
| 110 | hOutDebug(1,"not connected:" << mSocket->state()); |
| 111 | if (mSocket->state() == QAbstractSocket::UnconnectedState) |
| 112 | { |
| 113 | mSocket->connectToHost(QHostAddress(mPrinterItem->mIp), mPrinterItem->mPort); |
| 114 | mSocket->waitForConnected(1000); |
| 115 | if(mRunning && mSocket->state() != QAbstractSocket::ConnectedState) |
| 116 | QThread::msleep(200); |
| 117 | hOutDebug(1, "error:" << mSocket->errorString().toStdString()); |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | QThread::sleep(1); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void PrintRunner::run() |
| 127 | { |
| 128 | mSocket = QSharedPointer<PrinterSocket>(new PrinterSocket()); |
| 129 | mRunning = true; |
| 130 | |
| 131 | qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState"); |
| 132 | mSocket->moveToThread(this); |
| 133 | connect(mSocket.data(), SIGNAL(stateChanged(QAbstractSocket::SocketState)),this, SLOT(socketStateChanged(QAbstractSocket::SocketState))); |
| 134 | |
| 135 | while(mRunning) |
| 136 | { |
| 137 | QElapsedTimer timer; |
| 138 | timer.start(); |
| 139 | emit waiting(); |
| 140 | waitForConnected(); |
| 141 | |
| 142 | if (!mRunning) break; |
| 143 | |
| 144 | emit connected(); |
| 145 | hOutDebug(1,"appl connected"); |
| 146 | |
| 147 | readFromSocket(); |
| 148 | hOutDebug(1, "state:" << mSocket->state() << " running: " << (mRunning? "y" : "n") ); |
| 149 | emit disconnected(); |
| 150 | if (timer.elapsed() < 1000) |
| 151 | { |
| 152 | QThread::msleep(1000); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | emit stoppedWaiting(); |
| 157 | mSocket->close(); |
| 158 | } |
| 159 | |
| 160 | void PrintRunner::socketStateChanged(QAbstractSocket::SocketState state) |
| 161 | { |
| 162 | static QAbstractSocket::SocketState prevState = QAbstractSocket::UnconnectedState; |
| 163 | if (state != prevState) |
| 164 | { |
| 165 | if (state == QAbstractSocket::ConnectedState && prevState != QAbstractSocket::ConnectedState) |
| 166 | { |
| 167 | hOutDebug(1,"Connected"); |
| 168 | emit connected(); |
| 169 | } |
| 170 | if (state != QAbstractSocket::ConnectedState && prevState == QAbstractSocket::ConnectedState) |
| 171 | { |
| 172 | hOutDebug(1,"Disconnected"); |
| 173 | emit disconnected(); |
| 174 | } |
| 175 | prevState = state; |
| 176 | } |
| 177 | hOutDebug(1, "Socket state: " << state); |
| 178 | } |
| 179 | |
| 180 | void PrintRunner::stop() |
| 181 | { |
| 182 | mRunning = false; |
| 183 | } |