Changeset View
Changeset View
Standalone View
Standalone View
host/connection.cpp
Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | |||||
68 | void Connection::readData() | 68 | void Connection::readData() | ||
69 | { | 69 | { | ||
70 | /* Qt does not recognize POLLHUP as an error and | 70 | /* Qt does not recognize POLLHUP as an error and | ||
71 | * as that flag never gets cleared, we enter an | 71 | * as that flag never gets cleared, we enter an | ||
72 | * infinite busy loop polling STDIN. | 72 | * infinite busy loop polling STDIN. | ||
73 | * So we need to check for this condition ourselves | 73 | * So we need to check for this condition ourselves | ||
74 | * and exit. */ | 74 | * and exit. */ | ||
75 | 75 | | |||
76 | struct pollfd poll_stdin = { | 76 | struct pollfd poll_stdin = {}; | ||
77 | .fd = STDIN_FILENO, | 77 | poll_stdin.fd = STDIN_FILENO; | ||
78 | .events = POLLHUP, | 78 | poll_stdin.events = POLLHUP; | ||
79 | .revents = 0 | 79 | poll_stdin.revents = 0; | ||
80 | }; | 80 | | ||
81 | if (poll (&poll_stdin, 1, 0) != 0) { | 81 | if (poll (&poll_stdin, 1, 0) != 0) { | ||
82 | // STDIN has HUP/ERR/NVAL condition | 82 | // STDIN has HUP/ERR/NVAL condition | ||
83 | qApp->exit(0); | 83 | qApp->exit(0); | ||
84 | return; | 84 | return; | ||
85 | } | 85 | } | ||
86 | 86 | | |||
87 | m_stdIn.startTransaction(); | 87 | m_stdIn.startTransaction(); | ||
88 | quint32 length = 0; | 88 | quint32 length = 0; | ||
89 | auto rc = m_stdIn.read((char*)(&length), sizeof(quint32)); | 89 | auto rc = m_stdIn.read((char*)(&length), sizeof(quint32)); | ||
90 | if (rc == -1) { | 90 | if (rc == -1) { | ||
91 | m_stdIn.rollbackTransaction(); | 91 | m_stdIn.rollbackTransaction(); | ||
92 | return; | 92 | return; | ||
93 | } | 93 | } | ||
94 | 94 | | |||
95 | QByteArray data = m_stdIn.read(length); | 95 | QByteArray data = m_stdIn.read(length); | ||
96 | if (data.length() != length) { | 96 | if (data.length() != int(length)) { | ||
97 | m_stdIn.rollbackTransaction(); | 97 | m_stdIn.rollbackTransaction(); | ||
98 | return; | 98 | return; | ||
99 | } | 99 | } | ||
100 | 100 | | |||
101 | if (data.isEmpty()) { | 101 | if (data.isEmpty()) { | ||
102 | m_stdIn.rollbackTransaction(); | 102 | m_stdIn.rollbackTransaction(); | ||
103 | return; | 103 | return; | ||
104 | } | 104 | } | ||
105 | 105 | | |||
106 | m_stdIn.commitTransaction(); | 106 | m_stdIn.commitTransaction(); | ||
107 | const QJsonObject json = QJsonDocument::fromJson(data).object(); | 107 | const QJsonObject json = QJsonDocument::fromJson(data).object(); | ||
108 | emit dataReceived(json); | 108 | emit dataReceived(json); | ||
109 | } | 109 | } |