-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPubnub.cpp
More file actions
83 lines (71 loc) · 1.89 KB
/
Pubnub.cpp
File metadata and controls
83 lines (71 loc) · 1.89 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
#include "Pubnub.h"
Pubnub::Pubnub(const QString &_hostname, const QString &_path) {
hostname = _hostname;
s = new QUnixSocket(_path, this);
c = new QTcpSocket(this);
can_write = false;
connect(s, SIGNAL(message(const QByteArray&)), this, SLOT(message(const QByteArray&)));
connect(c, SIGNAL(connected()), this, SLOT(connected()));
connect(c, SIGNAL(readyRead()), this, SLOT(doRead()));
connect(c, SIGNAL(bytesWritten(qint64)), this, SLOT(flush()));
connect(c, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
connect(c, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(&t, SIGNAL(timeout()), this, SLOT(check()));
c->connectToHost(hostname, 80);
t.setSingleShot(false);
t.start(15000);
}
void Pubnub::check() {
// check status of connection to pubnub
switch(c->state()) {
case QAbstractSocket::ConnectedState:
break;
default:
// in any other state than ConnectedState: force disconnection/Reconnection
disconnected();
}
}
void Pubnub::message(const QByteArray &msg) {
queue.append(msg);
flush();
}
void Pubnub::connected() {
can_write = true;
buf.clear(); // avoid any half packet
flush();
}
void Pubnub::disconnected() {
c->close();
qDebug("lost connection");
can_write = false;
// try to connect
c->connectToHost(hostname, 80);
// reset timer to 15 secs
t.start(15000);
}
void Pubnub::doRead() {
while(c->bytesAvailable())
c->read(8192);
}
bool Pubnub::genPacket() {
if (queue.isEmpty()) return false;
QByteArray req = queue.takeFirst();
buf.append("GET "+req+" HTTP/1.1\r\nHost: "+hostname.toUtf8()+"\r\n\r\n");
return true;
}
void Pubnub::flush() {
if (!can_write) return;
while(true) {
if (buf.isEmpty()) {
if (!genPacket()) return; // nothing
}
// attempt to write
qint64 l = c->write(buf);
if (l < 0) {
disconnected();
return;
}
if (l == 0) break; // can't write anymore for now
buf = buf.mid(l);
}
}