-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectaccesmatildaservice.cpp
165 lines (152 loc) · 5.65 KB
/
directaccesmatildaservice.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
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
#include "directaccesmatildaservice.h"
#include <QTimer>
#include <QHostAddress>
#include <QNetworkInterface>
//============================================================================================
DirectAccesMatildaService::DirectAccesMatildaService(QObject *parent) : QTcpServer(parent)
{
clientCount = 0;
}
//============================================================================================
void DirectAccesMatildaService::startServerNow(qint8 mode, quint16 port)
{
QHostAddress hAddr;
switch(mode){
case 1: hAddr = QHostAddress::LocalHost; break;
case 2: hAddr = QHostAddress::LocalHostIPv6; break;
default: hAddr = QHostAddress::Any; break;
}
if(isListening()){
emit stopAllNow();
this->close();
}
if(!listen(hAddr, port)){
emit showMessage(tr("Can't start server. Error: %1").arg(errorString()));
}
QString str;
switch(mode){
case 1: str = QHostAddress(QHostAddress::LocalHost).toString(); break;
case 2: str = QHostAddress(QHostAddress::LocalHostIPv6).toString(); break;
default:{
QStringList list;
QList<QHostAddress> netIface = QNetworkInterface::allAddresses();
for(int i = 0, iMax = netIface.size(); i < iMax; i++){
if(netIface.at(i).isNull())
continue;
list.append( netIface.at(i).toString() );
}
str = list.join("\n");
break;}
}
emit onDasStarted(str);
updateStatus();
lastState = isListening();
emit onStateChanged(lastState);
}
//============================================================================================
void DirectAccesMatildaService::onSocketKickOff()
{
clientCount--;
if(clientCount < 0)
clientCount = 0;
updateStatus();
}
//============================================================================================
void DirectAccesMatildaService::updateStatus()
{
if(isListening()){
emit onStateChanged(tr("State: listening, Port: %1, Clients: %2").arg(serverPort()).arg(clientCount));
}else{
emit onStateChanged(tr("State: closed"));
}
}
//============================================================================================
void DirectAccesMatildaService::stopServerNow()
{
emit stopAllNow();
close();
updateStatus();
if(lastState == isListening())
return;
lastState = isListening();
emit onStateChanged(lastState);
emit onDasStopped();
}
//============================================================================================
void DirectAccesMatildaService::onMatildaConnectedStateChanged(bool isConnUp)
{
if(!isConnUp)
stopServerNow();
}
//============================================================================================
void DirectAccesMatildaService::setDaForwardNI(QByteArray ni)
{
modemNI = ni;
emit setDaForwardNISign(ni);
}
//============================================================================================
void DirectAccesMatildaService::incomingConnection(qintptr socketDescr)
{
DirectAccessSocket *socket = new DirectAccessSocket(this);
if(!socket->setSocketDescriptor(socketDescr)){
socket->deleteLater();
return;
}
socket->setDaForwardNI(modemNI);
clientCount++;
connect(this, SIGNAL(dataFromCoordinator(QByteArray)), socket, SLOT(mWrite2Local(QByteArray)) );
connect(socket, SIGNAL(data2coordiantor(QByteArray)), this, SIGNAL(data2coordiantor(QByteArray)) );
connect(socket, SIGNAL(onSocketKickOff()), this, SLOT(onSocketKickOff()) );
connect(this, SIGNAL(stopAllNow()), socket, SLOT(onDisconn()) );
connect(this, SIGNAL(setDaForwardNISign(QByteArray)), socket, SLOT(setDaForwardNI(QByteArray)) );
updateStatus();
}
//============================================================================================
DirectAccessSocket::DirectAccessSocket(QObject *parent) : QTcpSocket(parent)
{
QTimer::singleShot(12 * 60 * 60 * 1000, this, SLOT(onDisconn()) );
connect(this, SIGNAL(disconnected()), SLOT(onDisconn()) );
connect(this, SIGNAL(readyRead()), this, SLOT(mReadyRead()) );
isKickOff = false;
}
//============================================================================================
void DirectAccessSocket::mWrite2Local(QByteArray writeStr)
{
if(state() == QAbstractSocket::ConnectedState){
if(!modemNI.isEmpty() && (writeStr.left(modemNI.length()) == modemNI || writeStr.left(4) == "OK\r\n" || writeStr.left(7) == "ERROR\r\n")){
if(writeStr.left(modemNI.length()) == modemNI){
write( writeStr.mid(modemNI.length()));
}else{
write( (writeStr.left(4) == "OK\r\n") ? writeStr.mid(4) : writeStr.mid(7) );
}
}else
write( writeStr );
}else{
if(state() != QAbstractSocket::ConnectingState)
QTimer::singleShot(11, this, SLOT(onDisconn()) );
}
}
//============================================================================================
void DirectAccessSocket::setDaForwardNI(QByteArray ni)
{
modemNI = ni;
}
//============================================================================================
void DirectAccessSocket::mReadyRead()
{
QByteArray readArr = readAll();
if(waitForReadyRead(5))
readArr.append(readAll());
emit data2coordiantor(modemNI + readArr);
}
//============================================================================================
void DirectAccessSocket::onDisconn()
{
if(!isKickOff){
isKickOff = true;
emit onSocketKickOff();
close();
}
deleteLater();
}
//============================================================================================