|
@@ -3,55 +3,221 @@
|
3
|
3
|
|
4
|
4
|
PTSocket::PTSocket(QObject* p) : QTcpSocket(p)
|
5
|
5
|
{
|
6
|
|
- m_hasHandshaked = false;
|
7
|
|
- m_timeout = 5000;
|
8
|
|
- connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(m_handshakeError()));
|
9
|
|
- connect(this, SIGNAL(readyRead()), this, SLOT(m_readyRead()));
|
|
6
|
+ setSocketOption(QAbstractSocket::LowDelayOption, 1);
|
|
7
|
+ m_packetSize = 0;
|
|
8
|
+ setTimeout(5000);
|
|
9
|
+ setPingInterval(1000 * 7);
|
|
10
|
+ m_state = Disconnected;
|
|
11
|
+ m_isServerSide = false;
|
|
12
|
+ connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(m_timedout()));
|
|
13
|
+ connect(&m_pingTimer, SIGNAL(timeout()), this, SLOT(m_ping()));
|
|
14
|
+ connect(this, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(m_stateChanged(QAbstractSocket::SocketState)));
|
|
15
|
+ connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(m_socketError(QAbstractSocket::SocketError)));
|
|
16
|
+ connect(this, SIGNAL(readyRead()), this, SLOT(m_readyRead()));
|
10
|
17
|
}
|
11
|
18
|
|
12
|
19
|
int PTSocket::getTimeout() const
|
13
|
20
|
{
|
14
|
|
- return m_timeout;
|
|
21
|
+ return m_timeout;
|
15
|
22
|
}
|
16
|
23
|
|
17
|
|
-QByteArray PTSocket::handshakeData() const
|
|
24
|
+int PTSocket::getPingInterval() const
|
18
|
25
|
{
|
19
|
|
- return "LibPTSocket";
|
|
26
|
+ return m_pingInterval;
|
20
|
27
|
}
|
21
|
28
|
|
22
|
|
-void PTSocket::handshake()
|
|
29
|
+bool PTSocket::isServerSide() const
|
23
|
30
|
{
|
24
|
|
- write(handshakeData());
|
|
31
|
+ return m_isServerSide;
|
|
32
|
+}
|
|
33
|
+
|
|
34
|
+QByteArray PTSocket::handshakeData()
|
|
35
|
+{
|
|
36
|
+ return "LibPTSocketv1";
|
25
|
37
|
}
|
26
|
38
|
|
27
|
39
|
void PTSocket::setTimeout(int t)
|
28
|
40
|
{
|
29
|
|
- m_timeout = t;
|
|
41
|
+ m_timeout = t;
|
|
42
|
+ if(m_timeoutTimer.isActive())
|
|
43
|
+ m_timeoutTimer.start(m_timeout);
|
|
44
|
+ else
|
|
45
|
+ m_timeoutTimer.setInterval(m_timeout);
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+void PTSocket::setPingInterval(int p)
|
|
49
|
+{
|
|
50
|
+ m_pingInterval = p;
|
|
51
|
+ if(m_pingTimer.isActive())
|
|
52
|
+ m_pingTimer.start(m_pingInterval);
|
|
53
|
+ else
|
|
54
|
+ m_pingTimer.setInterval(m_pingInterval);
|
|
55
|
+}
|
|
56
|
+
|
|
57
|
+void PTSocket::setIsServerSide(bool s)
|
|
58
|
+{
|
|
59
|
+ m_isServerSide = s;
|
|
60
|
+}
|
|
61
|
+
|
|
62
|
+void PTSocket::send(int pktType, QByteArray data)
|
|
63
|
+{
|
|
64
|
+ send(false, pktType, data);
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+void PTSocket::send(bool sys, int pktType, QByteArray data)
|
|
68
|
+{
|
|
69
|
+ if(data.isEmpty())
|
|
70
|
+ data = "\0";
|
|
71
|
+ data.prepend(intToByteArray(pktType));
|
|
72
|
+ data.prepend((char)sys);
|
|
73
|
+ data.prepend(intToByteArray(data.size()));
|
|
74
|
+ write(data);
|
|
75
|
+}
|
|
76
|
+
|
|
77
|
+void PTSocket::m_ping()
|
|
78
|
+{
|
|
79
|
+ m_pingTimer.stop();
|
|
80
|
+ if(m_timeoutTimer.isActive())
|
|
81
|
+ return;
|
|
82
|
+ send(true, PingRequest);
|
|
83
|
+ m_timeoutTimer.start();
|
30
|
84
|
}
|
31
|
85
|
|
32
|
86
|
void PTSocket::m_readyRead()
|
33
|
87
|
{
|
34
|
|
- if(m_hasHandshaked)
|
35
|
|
- {
|
36
|
|
-
|
37
|
|
- }
|
38
|
|
- else
|
39
|
|
- {
|
40
|
|
- QByteArray handshake = handshakeData();
|
41
|
|
- if(bytesAvailable() >= handshake.size())
|
42
|
|
- {
|
43
|
|
- QByteArray data = read(handshake.size());
|
44
|
|
- if(data == handshake)
|
45
|
|
- {
|
46
|
|
- m_hasHandshaked = true;
|
47
|
|
- }
|
48
|
|
- else
|
49
|
|
- m_handshakeError();
|
50
|
|
- }
|
51
|
|
- }
|
|
88
|
+ m_timeoutTimer.stop();
|
|
89
|
+ m_pingTimer.start();
|
|
90
|
+ if(m_state == Handshaked)
|
|
91
|
+ {
|
|
92
|
+ if(m_packetSize == 0)
|
|
93
|
+ {
|
|
94
|
+ if((unsigned)bytesAvailable() >= sizeof(int))
|
|
95
|
+ m_packetSize = byteArrayToInt(read(sizeof(int)));
|
|
96
|
+ else if(bytesAvailable() != 0)
|
|
97
|
+ m_timeoutTimer.start();
|
|
98
|
+ }
|
|
99
|
+ if(m_packetSize != 0)
|
|
100
|
+ {
|
|
101
|
+ if((unsigned)bytesAvailable() >= m_packetSize)
|
|
102
|
+ {
|
|
103
|
+ bool sys = (bool)read(1).at(0);
|
|
104
|
+ QByteArray p = read(sizeof(int));
|
|
105
|
+ int pktType = byteArrayToInt(p);
|
|
106
|
+ QByteArray data = read(m_packetSize - 1 - p.size());
|
|
107
|
+ if(sys)
|
|
108
|
+ {
|
|
109
|
+ if(pktType == PingRequest)
|
|
110
|
+ send(true, PingAnswer);
|
|
111
|
+ //else if(pktType == PingAnswer)
|
|
112
|
+ }
|
|
113
|
+ else
|
|
114
|
+ emit packetReceived(pktType, data);
|
|
115
|
+ m_packetSize = 0;
|
|
116
|
+ m_readyRead();
|
|
117
|
+ }
|
|
118
|
+ else
|
|
119
|
+ m_timeoutTimer.start();
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+ else
|
|
123
|
+ {
|
|
124
|
+ QByteArray handshake = handshakeData();
|
|
125
|
+ if(bytesAvailable() >= handshake.size())
|
|
126
|
+ {
|
|
127
|
+ QByteArray data = read(handshake.size());
|
|
128
|
+ if(data == handshake)
|
|
129
|
+ {
|
|
130
|
+ setState(Handshaked);
|
|
131
|
+ m_readyRead();
|
|
132
|
+ }
|
|
133
|
+ else
|
|
134
|
+ m_handshakeError();
|
|
135
|
+ }
|
|
136
|
+ else
|
|
137
|
+ m_timeoutTimer.start();
|
|
138
|
+ }
|
|
139
|
+}
|
|
140
|
+
|
|
141
|
+void PTSocket::m_timedout()
|
|
142
|
+{
|
|
143
|
+ if(m_state == Disconnected)
|
|
144
|
+ return;
|
|
145
|
+ if(m_state == HostLookUp)
|
|
146
|
+ setError("Unable to find the host " + peerName() + " in the time (" + QString::number(m_timeout) + " ms)");
|
|
147
|
+ else if(m_state == Connecting)
|
|
148
|
+ setError("The " + QString(isServerSide()? "client " + peerAddress().toString() : "server " + peerName()) + " didn't answer to the connection request in the time (" + QString::number(m_timeout) + " ms)");
|
|
149
|
+ else if(m_state == Connected)
|
|
150
|
+ setError("The " + QString(isServerSide()? "client " + peerAddress().toString() : "server " + peerName()) + " didn't answer to the handshake in the time (" + QString::number(m_timeout) + " ms)");
|
|
151
|
+ else if(m_state == Handshaked)
|
|
152
|
+ setError("The " + QString(isServerSide()? "client " + peerAddress().toString() : "server " + peerName()) + " timed out (" + QString::number(m_timeout) + " ms)");
|
|
153
|
+ else if(m_state == Disconnecting)
|
|
154
|
+ abort();
|
52
|
155
|
}
|
53
|
156
|
|
54
|
157
|
void PTSocket::m_handshakeError()
|
55
|
158
|
{
|
|
159
|
+ setError("The " + QString(isServerSide()? "client " + peerAddress().toString() : "server " + peerName()) + " didn't answer to the handshake correclty");
|
|
160
|
+}
|
|
161
|
+
|
|
162
|
+void PTSocket::m_stateChanged(QAbstractSocket::SocketState s)
|
|
163
|
+{
|
|
164
|
+ if(s == QAbstractSocket::HostLookupState)
|
|
165
|
+ setState(HostLookUp);
|
|
166
|
+ else if(s == QAbstractSocket::ConnectingState)
|
|
167
|
+ setState(Connecting);
|
|
168
|
+ else if(s == QAbstractSocket::ConnectedState)
|
|
169
|
+ {
|
|
170
|
+ m_packetSize = 0;
|
|
171
|
+ setState(Connected);
|
|
172
|
+ write(handshakeData());
|
|
173
|
+ }
|
|
174
|
+ else if(s == QAbstractSocket::ClosingState)
|
|
175
|
+ setState(Disconnecting);
|
|
176
|
+ m_timeoutTimer.start();
|
|
177
|
+ if(s == QAbstractSocket::UnconnectedState)
|
|
178
|
+ {
|
|
179
|
+ m_timeoutTimer.stop();
|
|
180
|
+ setState(Disconnected);
|
|
181
|
+ }
|
|
182
|
+}
|
56
|
183
|
|
|
184
|
+void PTSocket::m_socketError(QAbstractSocket::SocketError)
|
|
185
|
+{
|
|
186
|
+ setState(Error);
|
|
187
|
+}
|
|
188
|
+
|
|
189
|
+void PTSocket::setState(PTSocket::State s)
|
|
190
|
+{
|
|
191
|
+ if(s == Handshaked)
|
|
192
|
+ m_pingTimer.start();
|
|
193
|
+ else
|
|
194
|
+ m_pingTimer.stop();
|
|
195
|
+ if(m_state == Error && (s != HostLookUp && s != Connecting))
|
|
196
|
+ return;
|
|
197
|
+ m_state = s;
|
|
198
|
+ emit stateChanged(m_state);
|
|
199
|
+}
|
|
200
|
+
|
|
201
|
+void PTSocket::setError(QString e)
|
|
202
|
+{
|
|
203
|
+ if(m_state == Error)
|
|
204
|
+ return;
|
|
205
|
+ setErrorString(e);
|
|
206
|
+ setState(Error);
|
|
207
|
+}
|
|
208
|
+
|
|
209
|
+QByteArray PTSocket::intToByteArray(int a)
|
|
210
|
+{
|
|
211
|
+ QByteArray data;
|
|
212
|
+ for(unsigned char i = 0; i < sizeof(a); ++i)
|
|
213
|
+ data.append((char)(a >> 8 * i));
|
|
214
|
+ return data;
|
|
215
|
+}
|
|
216
|
+
|
|
217
|
+int PTSocket::byteArrayToInt(QByteArray d)
|
|
218
|
+{
|
|
219
|
+ int a = 0;
|
|
220
|
+ for(unsigned char i = 0; i < d.size(); ++i)
|
|
221
|
+ a |= (unsigned int)((unsigned char)d.at(i) << i * 8);
|
|
222
|
+ return a;
|
57
|
223
|
}
|