3 #include "ActiveSessionSocket.h"
4 #include "ActiveSessionSocket.moc"
9 ActiveSessionSocket::ActiveSessionSocket(QString host
, int port
, Platform::Memory
*memory
) : memory(memory
), host(host
), port(port
) {
10 socket
= new QTcpSocket();
12 socket
->connectToHost(host
, port
);
13 connect(socket
, SIGNAL(readyRead()), this, SLOT(handle_data()));
14 connect(socket
, SIGNAL(connected()), this, SLOT(reemit_connected()));
15 connect(socket
, SIGNAL(disconnected()), this, SLOT(reemit_disconnected()));
16 connect(socket
, SIGNAL(error(QAbstractSocket::SocketError
)), this, SLOT(error_caught(QAbstractSocket::SocketError
)));
19 ActiveSessionSocket::~ActiveSessionSocket() {
22 void ActiveSessionSocket::handle_data() {
23 QByteArray data
= socket
->readAll();
25 while(data
.length()) {
26 QString string
= data
;
27 data
.remove(0, string
.length()+1);
28 Platform::Event
*event
= Platform::Event::deserialize(string
.toStdString());
30 memory
->handle_event(event
);
31 emit
event_received(event
);
37 void ActiveSessionSocket::error_caught(QAbstractSocket::SocketError error
) {
38 /* if it's a timeout or connection-refused, try reconnecting in 500ms . . . */
39 if(error
== QAbstractSocket::SocketTimeoutError
) {
40 QTimer::singleShot(500, this, SLOT(try_connecting()));
42 else if(error
== QAbstractSocket::ConnectionRefusedError
) {
43 QTimer::singleShot(500, this, SLOT(try_connecting()));
47 void ActiveSessionSocket::try_connecting() {
48 socket
->connectToHost(host
, port
);
52 } // namespace Aesalon