-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringserver.cpp
117 lines (92 loc) · 2.7 KB
/
stringserver.cpp
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
#include <QDebug>
#include <QTimer>
#include "stringserver.h"
StringServer::StringServer(QObject *parent, int port, bool parseJson, bool translate, QString translateID, bool primaryConnection) : QObject(parent)
,m_server(new QTcpServer)
{
m_port = port;
m_parseJson = parseJson;
m_translate = translate;
m_translateID = translateID;
m_primaryConnection = primaryConnection;
}
StringServer::~StringServer()
{
if (m_server)
{
m_server->close();
delete m_server;
}
}
bool StringServer::Start()
{
if (this->m_server->listen(QHostAddress::Any, m_port))
{
qDebug() << "[QMLVIEWER] TCP Server listening on port" << m_port;
connect(m_server, SIGNAL(newConnection()), this, SLOT(onClientConnected()));
if (m_primaryConnection)
emit PrimaryConnectionAvailable();
return true;
}
else
{
qDebug() << "[QMLVIEWER] Error: TCP Server cannot listen on port" << m_port;
return false;
}
}
bool StringServer::Send(QString msg)
{
int count = m_clients.size();
for(int i = 0; i < count; i++) {
if(m_clients[i]->state() == QAbstractSocket::ConnectedState) {
m_clients[i]->write(msg.append("\r\n").toLatin1());
}
}
return true;
}
int StringServer::getPort()
{
return m_port;
}
bool StringServer::getTranslate()
{
return m_translate;
}
QString StringServer::getTranslateID()
{
return m_translateID;
}
void StringServer::onClientConnected()
{
qDebug() << "[QMLVIEWER] Handling new connection.";
QTcpSocket *s = m_server->nextPendingConnection();
connect(s, SIGNAL(readyRead()), this,SLOT(onClientReadyRead()));
connect(s, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
m_clients.append(s);
emit ClientConnected();
}
void StringServer::onClientReadyRead()
{
int count = m_clients.size();
for(int i = 0; i < count; i++) {
while (m_clients[i]->bytesAvailable() && m_clients[i]->canReadLine()) {
QByteArray ba = m_clients[i]->readLine();
emit MessageAvailable(ba, m_parseJson, m_translate, m_translateID);
}
}
}
void StringServer::onClientDisconnected()
{
int count = m_clients.size();
for(int i = 0; i < count; i++) {
if(m_clients[i]->state() == QAbstractSocket::UnconnectedState) {
qDebug() << "[QMLVIEWER] Removing client:" << i;
disconnect(m_clients[i], SIGNAL(readyRead()), this,SLOT(onClientReadyRead()));
disconnect(m_clients[i], SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
m_clients[i]->deleteLater();
m_clients.removeAt(i);
emit ClientDisconnected();
break;
}
}
}