-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQTCPIP.cpp
More file actions
346 lines (306 loc) · 8.67 KB
/
QTCPIP.cpp
File metadata and controls
346 lines (306 loc) · 8.67 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "QTCPIP.h"
#include <QHostAddress>
#include <QElapsedTimer>
#include <QMessageBox>
#include <QApplication>
#include <QThread>
#include <QTime>
#include "ControlAPI.h"
QTelnet::QTelnet(QObject *parent) :
QTcpSocket(parent), m_actualSB(0)
{
NumberReconnects=0;
#ifndef USE_CA_DLL
connect( this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)) );
#endif
connect( this, SIGNAL(readyRead()), this, SLOT(onReadyRead()) );
connect(this, &QTcpSocket::disconnected, this, &QTelnet::handleDisconnected);
standard_timeout_in_seconds = 3;
m_lastHost = "";
m_lastPort = 0;
m_reconnectTimer = new QTimer(this);
m_reconnectTimer->setInterval(3000); // Try every 3 seconds
connect(m_reconnectTimer, &QTimer::timeout, this, &QTelnet::attemptReconnect);
}
QString QTelnet::peerInfo() const
{
return QString("%1 (%2):%3").arg(peerName()).arg(peerAddress().toString()).arg(peerPort());
}
bool QTelnet::isConnected(bool no_error_message) const
{
bool ok = state() == QAbstractSocket::ConnectedState;
if ((!ok) && (!no_error_message)) {
QMessageBox msgBox;
msgBox.setText("QTelnet::isConnected() not connected");
msgBox.exec();
}
return ok;
}
void QTelnet::connectToHost(const QString &host, quint16 port)
{
m_lastHost = host;
m_lastPort = port;
if (!isConnected()) {
abort();
QTcpSocket::connectToHost(host, port);
}
}
void QTelnet::disconnectFromRemote()
{
qDebug() << "Disconnecting from remote host manually.";
disconnectFromHost();
}
void QTelnet::handleDisconnected()
{
qDebug() << "Disconnected. Starting reconnection attempts.";
m_reconnectTimer->start();
}
void Sleep_ms_and_process_Qt_events(int delay_in_milli_seconds)
{
QTime dieTime= QTime::currentTime().addMSecs(delay_in_milli_seconds);
int remaining = 0;
constexpr unsigned int MaxNrLoops = 500;
unsigned int NrLoops = 0;
do {
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
remaining = QTime::currentTime().msecsTo(dieTime);
if (remaining>0) {
QThread::msleep( (remaining>10) ? 10 : remaining);
remaining = QTime::currentTime().msecsTo(dieTime);
}
NrLoops++;
} while ((remaining > 0) && (NrLoops < MaxNrLoops));
}
bool QTelnet::verifyConnected() {
if (state() == QAbstractSocket::UnconnectedState) {
if (m_lastHost=="") {
//TelnetTester->setStatusText("Reconnection not successfull", true);
qDebug() << "Reconnection not successfull";
return false;
}
unsigned int attempt = 0;
qDebug() << "Trying to reconnect to" << m_lastHost << ":" << m_lastPort;
while ((state() != QAbstractSocket::ConnectedState) && (attempt<30)) {
Sleep_ms_and_process_Qt_events(2000);//QThread::msleep(2000);
connectToHost(m_lastHost, m_lastPort);
attempt++;
}
if (state() == QAbstractSocket::ConnectedState) {
qDebug() << "Reconnected successfully!";
NumberReconnects++;
return true;
} else {
qDebug() << "Could not reconnect, even during 1 minute. Stopping any attempts.";
return false;
}
} else return true;
}
void QTelnet::attemptReconnect()
{
if (state() == QAbstractSocket::UnconnectedState) {
if (m_lastHost=="") {
m_reconnectTimer->stop();
//TelnetTester->setStatusText("Reconnection not successfull", true);
qDebug() << "Reconnection not successfull";
return;
}
qDebug() << "Trying to reconnect to" << m_lastHost << ":" << m_lastPort;
connectToHost(m_lastHost, m_lastPort);
if (state() == QAbstractSocket::ConnectedState) {
m_reconnectTimer->stop();
NumberReconnects++;
qDebug() << "Reconnected successfully!";
}
} else if (state() == QAbstractSocket::ConnectedState) {
m_reconnectTimer->stop();
}
}
void QTelnet::sendData(const QByteArray &ba)
{
if( verifyConnected() )
QTcpSocket::write( ba.constData(), ba.size() );
}
void QTelnet::socketError(QAbstractSocket::SocketError err)
{
Q_UNUSED(err);
qWarning() << "Socket error:" << err;
disconnectFromHost();
}
void QTelnet::write(const char c)
{
if( verifyConnected() ) {
QTcpSocket::write( (char*)&c, 1 );
flush();
}
}
void QTelnet::writeBuffer(const char* buffer, qint64 buffer_length)
{
if( verifyConnected() ) {
QTcpSocket::write( buffer, buffer_length );
flush();
}
}
void QTelnet::write(const QString &msg)
{
if( verifyConnected() ) {
QTcpSocket::write(msg.toLatin1(), msg.size());
flush();
}
}
void QTelnet::writeString(const QString &msg)
{
if( verifyConnected() ) {
QString cmd = "*" + msg + "#";
QTcpSocket::write(cmd.toLatin1(), cmd.size());
flush();
}
}
//convert double to string and write it
void QTelnet::writeDouble(double d)
{
writeString(QString::number(d));
}
//convert long to string and write it
void QTelnet::writeLong(long l)
{
writeString(QString::number(l));
}
//convert bool to string and write it
void QTelnet::writeBool(bool b)
{
writeString(b ? "1" : "0");
}
bool QTelnet::readBuffer(unsigned char* buffer, qint64 buffer_length, double timeout_in_seconds) {
if( !verifyConnected() )
return false;
if (timeout_in_seconds < 0.01) timeout_in_seconds = standard_timeout_in_seconds;
char c;
flush();
unsigned long buffer_position = 0;
QElapsedTimer timer;
timer.start();
while ((!timer.hasExpired(timeout_in_seconds * 1000)) && (buffer_position < buffer_length))
{
QApplication::processEvents();
if( read(&c, 1) == 1 ) //for optimization one could read more than one char here
{
if (buffer_position < buffer_length) {
buffer[buffer_position] = c;
buffer_position++;
}
} else QThread::msleep(1);
}
if (buffer_position < buffer_length)
{
QMessageBox msgBox;
//msgBox.setText("QTelnet.cpp: readBuffer timeout");
//msgBox.exec();
return false;
}
return true;
}
//read and discard input buffer till '*' is encountered
//then read and place characters in buffer till '#' is encountered or till timeout
//return true if success, false otherwise
bool QTelnet::readString(QString &msg, double timeout_in_seconds)
{
if( !verifyConnected() )
return false;
if (timeout_in_seconds == 0) timeout_in_seconds = standard_timeout_in_seconds;
char c;
bool bStart = false;
bool started = false;
QElapsedTimer timer;
timer.start();
flush();
msg = "";
while (!timer.hasExpired(timeout_in_seconds * 1000))
{
QApplication::processEvents();
if( read(&c, 1) == 1 )
{
if( c == '*' )
{
bStart = true;
started = true;
}
else if( c == '#' )
{
bStart = false;
break;
}
else if( bStart )
{
msg += c;
}
} else {
verifyConnected();
QThread::msleep(1);
}
}
if (timer.hasExpired(timeout_in_seconds * 1000))
{
//QMessageBox msgBox;
//msgBox.setText("QTelnet.cpp: readString timeout");
//msgBox.exec();
qDebug() << "QTelnet.cpp: readString timeout";
return false;
}
return started && (!bStart);
}
//read string and convert it to double if successful
//if successful, return true and place the double in d
//if failed, return false
bool QTelnet::readDouble(double &d, double timeout_in_seconds)
{
QString msg;
if( readString(msg, timeout_in_seconds) )
{
d = msg.toDouble();
return true;
}
return false;
}
//read string and convert it to long if successful
//if successful, return true and place the double in d
//if failed, return false
bool QTelnet::readLong(long &l, double timeout_in_seconds)
{
QString msg;
if( readString(msg, timeout_in_seconds) )
{
l = msg.toLong();
return true;
}
return false;
}
//read string and convert it to bool if successful, where string == "1" means true and everything else means false
//if successful, return true and place the double in d
//if failed, return false
bool QTelnet::readBool(bool &b, double timeout_in_seconds)
{
QString msg;
if( readString(msg, timeout_in_seconds) )
{
b = (msg == "1");
return true;
}
return false;
}
void QTelnet::onReadyRead()
{
/* qint64 readlength;
while( (readlength = read(m_buffIncoming, IncommingBufferSize)) != 0 )
{
switch( readlength )
{
case -1:
disconnectFromHost();
break;
default:
if( readlength > 0 )
Q_EMIT(newData(m_buffProcessed, readlength));
break;
}
} */
}