-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathsocket_utils.cpp
476 lines (414 loc) · 11.8 KB
/
socket_utils.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include <string.h>
#include "../include/coroutine_utils.h"
#include "../include/socket_utils.h"
QTNETWORKNG_NAMESPACE_BEGIN
SocketLike::SocketLike() { }
SocketLike::~SocketLike() { }
qint32 SocketLike::read(char *data, qint32 size)
{
return recv(data, size);
}
qint32 SocketLike::write(const char *data, qint32 size)
{
return sendall(data, size);
}
qint64 SocketLike::size()
{
return -1;
}
namespace {
class SocketLikeImpl : public SocketLike
{
public:
SocketLikeImpl(QSharedPointer<Socket> s);
public:
virtual Socket::SocketError error() const override;
virtual QString errorString() const override;
virtual bool isValid() const override;
virtual HostAddress localAddress() const override;
virtual quint16 localPort() const override;
virtual HostAddress peerAddress() const override;
virtual QString peerName() const override;
virtual quint16 peerPort() const override;
virtual qintptr fileno() const override;
virtual Socket::SocketType type() const override;
virtual Socket::SocketState state() const override;
virtual HostAddress::NetworkLayerProtocol protocol() const override;
virtual QString localAddressURI() const override;
virtual QString peerAddressURI() const override;
virtual Socket *acceptRaw() override;
virtual QSharedPointer<SocketLike> accept() override;
virtual bool bind(const HostAddress &address, quint16 port, Socket::BindMode mode) override;
virtual bool bind(quint16 port, Socket::BindMode mode) override;
virtual bool connect(const HostAddress &addr, quint16 port) override;
virtual bool connect(const QString &hostName, quint16 port,
QSharedPointer<SocketDnsCache> dnsCache = QSharedPointer<SocketDnsCache>()) override;
virtual void close() override;
virtual void abort() override;
virtual bool listen(int backlog) override;
virtual bool setOption(Socket::SocketOption option, const QVariant &value) override;
virtual QVariant option(Socket::SocketOption option) const override;
virtual qint32 peek(char *data, qint32 size) override;
virtual qint32 peekRaw(char *data, qint32 size) override;
virtual qint32 recv(char *data, qint32 size) override;
virtual qint32 recvall(char *data, qint32 size) override;
virtual qint32 send(const char *data, qint32 size) override;
virtual qint32 sendall(const char *data, qint32 size) override;
virtual QByteArray recv(qint32 size) override;
virtual QByteArray recvall(qint32 size) override;
virtual qint32 send(const QByteArray &data) override;
virtual qint32 sendall(const QByteArray &data) override;
public:
QSharedPointer<Socket> s;
};
SocketLikeImpl::SocketLikeImpl(QSharedPointer<Socket> s)
: s(s)
{
}
Socket::SocketError SocketLikeImpl::error() const
{
return s->error();
}
QString SocketLikeImpl::errorString() const
{
return s->errorString();
}
bool SocketLikeImpl::isValid() const
{
return s->isValid();
}
HostAddress SocketLikeImpl::localAddress() const
{
return s->localAddress();
}
quint16 SocketLikeImpl::localPort() const
{
return s->localPort();
}
HostAddress SocketLikeImpl::peerAddress() const
{
return s->peerAddress();
}
QString SocketLikeImpl::peerName() const
{
return s->peerName();
}
quint16 SocketLikeImpl::peerPort() const
{
return s->peerPort();
}
qintptr SocketLikeImpl::fileno() const
{
return s->fileno();
}
Socket::SocketType SocketLikeImpl::type() const
{
return s->type();
}
Socket::SocketState SocketLikeImpl::state() const
{
return s->state();
}
HostAddress::NetworkLayerProtocol SocketLikeImpl::protocol() const
{
return s->protocol();
}
QString SocketLikeImpl::localAddressURI() const
{
return s->localAddressURI();
}
QString SocketLikeImpl::peerAddressURI() const
{
return s->peerAddressURI();
}
Socket *SocketLikeImpl::acceptRaw()
{
return s->accept();
}
QSharedPointer<SocketLike> SocketLikeImpl::accept()
{
Socket *r = s->accept();
if (r) {
return asSocketLike(r);
} else {
return QSharedPointer<SocketLike>();
}
}
bool SocketLikeImpl::bind(const HostAddress &address, quint16 port, Socket::BindMode mode)
{
return s->bind(address, port, mode);
}
bool SocketLikeImpl::bind(quint16 port, Socket::BindMode mode)
{
return s->bind(port, mode);
}
bool SocketLikeImpl::connect(const HostAddress &addr, quint16 port)
{
return s->connect(addr, port);
}
bool SocketLikeImpl::connect(const QString &hostName, quint16 port, QSharedPointer<SocketDnsCache> dnsCache)
{
return s->connect(hostName, port, dnsCache);
}
void SocketLikeImpl::close()
{
s->close();
}
void SocketLikeImpl::abort()
{
s->abort();
}
bool SocketLikeImpl::listen(int backlog)
{
return s->listen(backlog);
}
bool SocketLikeImpl::setOption(Socket::SocketOption option, const QVariant &value)
{
return s->setOption(option, value);
}
QVariant SocketLikeImpl::option(Socket::SocketOption option) const
{
return s->option(option);
}
qint32 SocketLikeImpl::peek(char *data, qint32 size)
{
return s->peek(data, size);
}
qint32 SocketLikeImpl::peekRaw(char *data, qint32 size)
{
return s->peek(data, size);
}
qint32 SocketLikeImpl::recv(char *data, qint32 size)
{
return s->recv(data, size);
}
qint32 SocketLikeImpl::recvall(char *data, qint32 size)
{
return s->recvall(data, size);
}
qint32 SocketLikeImpl::send(const char *data, qint32 size)
{
return s->send(data, size);
}
qint32 SocketLikeImpl::sendall(const char *data, qint32 size)
{
return s->sendall(data, size);
}
QByteArray SocketLikeImpl::recv(qint32 size)
{
return s->recv(size);
}
QByteArray SocketLikeImpl::recvall(qint32 size)
{
return s->recvall(size);
}
qint32 SocketLikeImpl::send(const QByteArray &data)
{
return s->send(data);
}
qint32 SocketLikeImpl::sendall(const QByteArray &data)
{
return s->sendall(data);
}
} // anonymous namespace
QSharedPointer<SocketLike> asSocketLike(QSharedPointer<Socket> s)
{
if (!s) {
return QSharedPointer<SocketLike>();
}
return QSharedPointer<SocketLikeImpl>::create(s).dynamicCast<SocketLike>();
}
QSharedPointer<Socket> convertSocketLikeToSocket(QSharedPointer<SocketLike> socket)
{
QSharedPointer<SocketLikeImpl> impl = socket.dynamicCast<SocketLikeImpl>();
if (impl.isNull()) {
return QSharedPointer<Socket>();
} else {
return impl->s;
}
}
class ExchangerPrivate
{
public:
ExchangerPrivate(QSharedPointer<SocketLike> request, QSharedPointer<SocketLike> forward, quint32 maxBufferSize,
float timeout);
~ExchangerPrivate();
public:
void receiveOutgoing();
void receiveIncoming();
void sendOutgoing();
void sendIncoming();
void in2out();
void out2in();
public:
QSharedPointer<SocketLike> request;
QSharedPointer<SocketLike> forward;
CoroutineGroup *operations;
Queue<QByteArray> incoming;
Queue<QByteArray> outgoing;
float timeout;
};
#define EXCHANGER_PACKET_SIZE (1024 * 8)
ExchangerPrivate::ExchangerPrivate(QSharedPointer<SocketLike> request, QSharedPointer<SocketLike> forward,
quint32 maxBufferSize, float timeout)
: request(request)
, forward(forward)
, operations(new CoroutineGroup)
, incoming(maxBufferSize / EXCHANGER_PACKET_SIZE)
, outgoing(maxBufferSize / EXCHANGER_PACKET_SIZE)
, timeout(timeout)
{
}
ExchangerPrivate::~ExchangerPrivate()
{
delete operations;
}
void ExchangerPrivate::receiveOutgoing()
{
QByteArray buf(EXCHANGER_PACKET_SIZE, Qt::Uninitialized);
while (true) {
qint32 len = forward->recv(buf.data(), buf.size());
if (len <= 0) {
operations->kill(QString::fromLatin1("receive_incoming"), false);
operations->kill(QString::fromLatin1("send_outgoing"), false);
incoming.put(QByteArray());
return;
}
incoming.put(QByteArray(buf.constData(), len));
}
}
void ExchangerPrivate::receiveIncoming()
{
QByteArray buf(EXCHANGER_PACKET_SIZE, Qt::Uninitialized);
while (true) {
qint32 len = request->recv(buf.data(), buf.size());
if (len <= 0) {
operations->kill(QString::fromLatin1("receive_outgoing"), false);
operations->kill(QString::fromLatin1("sending_incoming"), false);
outgoing.put(QByteArray());
return;
}
outgoing.put(QByteArray(buf.constData(), len));
}
}
void ExchangerPrivate::sendOutgoing()
{
while (true) {
QByteArray buf = outgoing.get();
if (buf.isEmpty()) {
return;
} else {
while (!outgoing.isEmpty()) {
buf.append(outgoing.get());
}
}
qint32 len;
try {
Timeout timeout(this->timeout);
Q_UNUSED(timeout);
len = forward->sendall(buf);
} catch (TimeoutException &) {
len = -1;
}
if (len != buf.size()) {
operations->killall(false);
return;
}
}
}
void ExchangerPrivate::sendIncoming()
{
while (true) {
QByteArray buf = incoming.get();
if (buf.isEmpty()) {
return;
} else {
while (!incoming.isEmpty()) {
buf.append(incoming.get());
}
}
qint32 len;
try {
Timeout timeout(this->timeout);
Q_UNUSED(timeout);
len = request->sendall(buf);
} catch (TimeoutException &) {
len = -1;
}
if (len != buf.size()) {
operations->killall(false);
return;
}
}
}
void ExchangerPrivate::in2out()
{
QByteArray buf(EXCHANGER_PACKET_SIZE, Qt::Uninitialized);
while (true) {
qint32 len = request->recv(buf.data(), buf.size());
if (len <= 0) {
forward->abort();
operations->kill(QString::fromLatin1("out2in"));
return;
}
qint32 sentBytes;
try {
Timeout timeout(this->timeout);
Q_UNUSED(timeout);
sentBytes = forward->sendall(buf.data(), len);
} catch (TimeoutException &) {
sentBytes = -1;
}
if (sentBytes != len) {
forward->abort();
operations->kill(QString::fromLatin1("out2in"));
return;
}
}
}
void ExchangerPrivate::out2in()
{
QByteArray buf(EXCHANGER_PACKET_SIZE, Qt::Uninitialized);
while (true) {
qint32 len = forward->recv(buf.data(), buf.size());
if (len <= 0) {
request->abort();
operations->kill(QString::fromLatin1("in2out"));
return;
}
qint32 sentBytes;
try {
Timeout timeout(this->timeout);
Q_UNUSED(timeout);
sentBytes = request->sendall(buf.data(), len);
} catch (TimeoutException &) {
sentBytes = -1;
}
if (sentBytes != len) {
request->abort();
operations->kill(QString::fromLatin1("in2out"));
return;
}
}
}
Exchanger::Exchanger(QSharedPointer<SocketLike> request, QSharedPointer<SocketLike> forward, quint32 maxBufferSize,
float timeout)
: d_ptr(new ExchangerPrivate(request, forward, maxBufferSize, timeout))
{
}
Exchanger::~Exchanger()
{
delete d_ptr;
}
void Exchanger::exchange()
{
Q_D(Exchanger);
// d->operations->spawnWithName("receive_outgoing", [d] { d->receiveOutgoing(); });
// d->operations->spawnWithName("receive_incoming", [d] { d->receiveIncoming(); });
// d->operations->spawnWithName("send_outgoing", [d] { d->sendOutgoing(); });
// d->operations->spawnWithName("send_incoming", [d] { d->sendIncoming(); });
d->operations->spawnWithName(QString::fromLatin1("in2out"), [d] { d->in2out(); });
d->operations->spawnWithName(QString::fromLatin1("out2in"), [d] { d->out2in(); });
d->operations->joinall();
}
QTNETWORKNG_NAMESPACE_END