forked from treefrogframework/treefrog-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtepollwebsocket.cpp
261 lines (208 loc) · 6.26 KB
/
tepollwebsocket.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* Copyright (c) 2015-2017, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include <QDataStream>
#include <QCryptographicHash>
#include <TWebApplication>
#include <TSystemGlobal>
#include <TAppSettings>
#include <THttpRequestHeader>
#include <THttpUtility>
#include "tepollwebsocket.h"
#include "tepoll.h"
#include "twebsocketframe.h"
#include "twebsocketworker.h"
#include "turlroute.h"
#include "tdispatcher.h"
const int BUFFER_RESERVE_SIZE = 127;
TEpollWebSocket::TEpollWebSocket(int socketDescriptor, const QHostAddress &address, const THttpRequestHeader &header)
: TEpollSocket(socketDescriptor, address), TAbstractWebSocket(header),
recvBuffer(), frames()
{
tSystemDebug("TEpollWebSocket [%p]", this);
recvBuffer.reserve(BUFFER_RESERVE_SIZE);
}
TEpollWebSocket::~TEpollWebSocket()
{
tSystemDebug("~TEpollWebSocket [%p]", this);
}
bool TEpollWebSocket::canReadRequest()
{
for (auto &frm : (const QList<TWebSocketFrame>&)frames) {
if (frm.isFinalFrame() && frm.state() == TWebSocketFrame::Completed) {
return true;
}
}
return false;
}
bool TEpollWebSocket::isTextRequest() const
{
if (!frames.isEmpty()) {
const TWebSocketFrame &frm = frames.first();
return (frm.opCode() == TWebSocketFrame::TextFrame);
}
return false;
}
bool TEpollWebSocket::isBinaryRequest() const
{
if (!frames.isEmpty()) {
const TWebSocketFrame &frm = frames.first();
return (frm.opCode() == TWebSocketFrame::BinaryFrame);
}
return false;
}
void TEpollWebSocket::sendTextForPublish(const QString &text, const QObject *except)
{
tSystemDebug("sendText text len:%d (pid:%d)", text.length(), (int)QCoreApplication::applicationPid());
if (except != this) {
TAbstractWebSocket::sendText(text);
}
}
void TEpollWebSocket::sendBinaryForPublish(const QByteArray &binary, const QObject *except)
{
tSystemDebug("sendBinary binary len:%d (pid:%d)", binary.length(), (int)QCoreApplication::applicationPid());
if (except != this) {
TAbstractWebSocket::sendBinary(binary);
}
}
void TEpollWebSocket::sendPong(const QByteArray &data)
{
tSystemDebug("sendPong data len:%d (pid:%d)", data.length(), (int)QCoreApplication::applicationPid());
TAbstractWebSocket::sendPong(data);
}
QList<QPair<int, QByteArray>> TEpollWebSocket::readAllBinaryRequest()
{
Q_ASSERT(canReadRequest());
QList<QPair<int, QByteArray>> ret;
QByteArray payload;
while (canReadRequest()) {
int opcode = frames.first().opCode();
payload.resize(0);
while (!frames.isEmpty()) {
TWebSocketFrame frm = frames.takeFirst();
payload += frm.payload();
if (frm.isFinalFrame() && frm.state() == TWebSocketFrame::Completed) {
ret << qMakePair(opcode, payload);
break;
}
}
}
return ret;
}
void *TEpollWebSocket::getRecvBuffer(int size)
{
int len = recvBuffer.size();
recvBuffer.reserve(len + size);
return recvBuffer.data() + len;
}
bool TEpollWebSocket::seekRecvBuffer(int pos)
{
int size = recvBuffer.size();
if (Q_UNLIKELY(pos <= 0 || size + pos > recvBuffer.capacity())) {
Q_ASSERT(0);
return false;
}
size += pos;
recvBuffer.resize(size);
int len = parse(recvBuffer);
tSystemDebug("WebSocket parse len : %d", len);
if (len < 0) {
tSystemError("WebSocket parse error [%s:%d]", __FILE__, __LINE__);
close();
return false;
}
return true;
}
void TEpollWebSocket::startWorker()
{
tSystemDebug("TEpollWebSocket::startWorker");
Q_ASSERT(canReadRequest());
auto payloads = readAllBinaryRequest();
if (!payloads.isEmpty()) {
TWebSocketWorker *worker = new TWebSocketWorker(TWebSocketWorker::Receiving, this, reqHeader.path());
worker->setPayloads(payloads);
startWorker(worker);
}
}
void TEpollWebSocket::startWorker(TWebSocketWorker *worker)
{
worker->moveToThread(thread());
connect(worker, SIGNAL(finished()), this, SLOT(releaseWorker()));
++myWorkerCounter; // count-up
worker->start();
}
void TEpollWebSocket::releaseWorker()
{
tSystemDebug("TEpollWebSocket::releaseWorker");
TWebSocketWorker *worker = qobject_cast<TWebSocketWorker *>(sender());
if (worker) {
worker->deleteLater();
--myWorkerCounter; // count-down
if (deleting.load()) {
TEpollWebSocket::deleteLater();
} else {
if (pollIn.exchange(false)) {
TEpoll::instance()->modifyPoll(this, (EPOLLIN | EPOLLOUT | EPOLLET)); // reset
}
}
}
}
void TEpollWebSocket::deleteLater()
{
tSystemDebug("TEpollWebSocket::deleteLater countWorkers:%d deleting:%d", (int)myWorkerCounter, (bool)deleting);
if (!deleting.exchange(true)) {
startWorkerForClosing();
return;
}
if ((int)myWorkerCounter == 0) {
QObject::deleteLater();
}
}
void TEpollWebSocket::startWorkerForOpening(const TSession &session)
{
TWebSocketWorker *worker = new TWebSocketWorker(TWebSocketWorker::Opening, this, reqHeader.path());
worker->setSession(session);
startWorker(worker);
}
void TEpollWebSocket::startWorkerForClosing()
{
if (!closing.load()) {
TWebSocketWorker *worker = new TWebSocketWorker(TWebSocketWorker::Closing, this, reqHeader.path());
startWorker(worker);
} else {
deleteLater();
}
}
void TEpollWebSocket::clear()
{
recvBuffer.resize(BUFFER_RESERVE_SIZE);
recvBuffer.squeeze();
recvBuffer.truncate(0);
frames.clear();
}
qint64 TEpollWebSocket::writeRawData(const QByteArray &data)
{
sendData(data);
return data.length();
}
void TEpollWebSocket::disconnect()
{
TEpollSocket::disconnect();
stopKeepAlive();
}
void TEpollWebSocket::timerEvent(QTimerEvent *event)
{
if (event->timerId() == keepAliveTimer->timerId()) {
sendPing();
} else {
TEpollSocket::timerEvent(event);
}
}
TEpollWebSocket *TEpollWebSocket::searchSocket(int sid)
{
TEpollSocket *sock = TEpollSocket::searchSocket(sid);
return dynamic_cast<TEpollWebSocket*>(sock);
}