-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpserver.cpp
More file actions
142 lines (131 loc) · 3.93 KB
/
tcpserver.cpp
File metadata and controls
142 lines (131 loc) · 3.93 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
#include "tcpserver.h"
#include <QDebug>
#include <QRegExp>
TcpServer::TcpServer(QObject *parent) :
QTcpServer(parent)
{
clientNum = 0;
linkList.clear();
setMaxPendingConnections(50);
}
/**
* @brief TcpServer::incomingConnection
* when incomming connection
* if the pending socket > maxpending connection, do not connect
* if it is repeated socket, abort the socket
* else add it into linklist whose type is QTcpSocket*
* connect two slots
* emit one incomming connection signal, which wants system option receive
* @param socketDescriptor
*/
void TcpServer::incomingConnection(qintptr socketDescriptor)
{
#ifdef QT_DEBUG
qDebug() << "success";
#endif
if(linkList.size() <= maxPendingConnections())
{
QTcpSocket *socket = new QTcpSocket(this);
socket->setSocketDescriptor(socketDescriptor);
if(getLinkListSize(socketDescriptor) != -1)
{
linkList.insert(socketDescriptor, socket);
//stringList.insert(socketDescriptor, QString(""));
clientNum++;
QObject::connect(socket, &QTcpSocket::stateChanged, [=](){disconnectSocket(socketDescriptor);});
QObject::connect(socket, &QTcpSocket::readyRead, [=](){readData(socketDescriptor);});
#ifdef QT_DEBUG
qDebug() << "success";
#endif
emit newSocketConnected(socketDescriptor);
//to prevent repeated socker linked
}
else
socket->abort();
}
}
/**
* @brief TcpServer::readData
* use the given socket descriptor, return the buffer of the socket
* @param socketDescriptor
*/
void TcpServer::readData(qintptr socketDescriptor)
{
qDebug() << "already in";
QString buffer = QString(linkList[socketDescriptor]->readAll());
qDebug() << buffer;
/*QRegExp handle("Data.*:Rx");
handle.setMinimal(true);
int pos = 0;
int handlePos = handle.indexIn(buffer);
if(handlePos != -1)
pos = handlePos + handle.matchedLength() - 2;*/
QRegExp reg("Rx:NWKID:.*Finish");
reg.setMinimal(true);
while(buffer != "")
{
int pos = reg.indexIn(buffer);
if(pos != -1)
{
int len = reg.matchedLength();
QString data = buffer.left(len);
buffer = buffer.mid(len, buffer.length() - len + 1);
emit sendData(socketDescriptor, data);
}
}
/*
* qDebug() << temp[temp.length() - 1];
if(temp[temp.length() - 1] == '\n')
{
emit sendData(socketDescriptor, stringList[socketDescriptor]);
qDebug() << "already emit";
stringList[socketDescriptor] = "";
}
qDebug() << linkList[socketDescriptor]->readAll();
while(true)
{
int index = reg.indexIn(buffer, pos);
if(index == -1)
break;
emit sendData(socketDescriptor, reg.cap(0));
qDebug() << reg.cap(0);
pos = reg.matchedLength() + index;
}
if(reg.indexIn(buffer) >= 0)
emit sendData(socketDescriptor, reg.cap(0));*/
}
void TcpServer::disconnectSocket(qintptr socketDescriptor)
{
/*
* disconnect all slots of the socket
* remove the socket from the linklist
* emit a disconnect socket signal, which wants system option received
*/
QTcpSocket *tempsocket = linkList[socketDescriptor];
if(tempsocket->state() == QTcpSocket::UnconnectedState)
{
//linkList[socketDescriptor]->disconnectFromHost();
QObject::disconnect(tempsocket);
linkList.remove(socketDescriptor);
qDebug()<< "Disconnect Succeed";
clientNum--;
emit socketDisconnected(socketDescriptor);
}
}
int TcpServer::getLinkListSize(qintptr socketDescriptor)
{
/*
*if the given socket descriptor is existed in the listsize return -1;
*else return the size of the linklist
*/
if(linkList.contains(socketDescriptor))
return -1;
return linkList.size();
}
int TcpServer::getClientNum()
{
return clientNum;
}
TcpServer::~TcpServer()
{
}