-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAbstractServer.cpp
executable file
·88 lines (68 loc) · 2.06 KB
/
AbstractServer.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
#include <httq/AbstractServer.h>
#include <httq/HttpRequest.h>
#include <httq/Logger.h>
#include <QTcpServer>
#include <QTcpSocket>
#include <QWebSocketServer>
namespace httq
{
AbstractServer::AbstractServer(QObject *parent)
: QObject(parent)
, mSvr(new QTcpServer(this))
, mWsSvr(new QWebSocketServer("", QWebSocketServer::SslMode::NonSecureMode, this)) // TODO: server name!
, mLoggerFactory(nullptr)
, mLogger(nullptr)//mLoggerFactory->createLogger(this))
{
connect(mWsSvr, &QWebSocketServer::newConnection,
this, [this]()
{
newWebSocketConnection(mWsSvr->nextPendingConnection());
});
}
LoggerFactory *AbstractServer::createLoggerFactory()
{
return new LoggerFactory(this);
}
LoggerFactory *AbstractServer::getLoggerFactory()
{
if (mLoggerFactory == nullptr)
mLoggerFactory = createLoggerFactory();
return mLoggerFactory;
}
void AbstractServer::slotNewConnection()
{
mLogger->debug(QStringLiteral("newConnection"));
while (QTcpSocket *cli = mSvr->nextPendingConnection())
{
mLogger->debug(QStringLiteral("nextPendingConnection"));
if (cli == nullptr)
{
mLogger->debug(QStringLiteral("invalid tcp client"));
return;
}
HttpRequest *req = new HttpRequest(cli, this);
connect(req, &HttpRequest::signalUpgrade,
this, [this, req](QTcpSocket *sock)
{
mLogger->debug(QStringLiteral("signalUpgrade"));
mWsSvr->handleConnection(sock);
emit sock->readyRead(); // rollbackTransaction doesn't re-emit the readyRead signal
});
connect(req, &HttpRequest::signalReady,
this, [this, req]()
{
mLogger->debug(QStringLiteral("signalReady"));
newHttpConnection(req); // TODO: who owns HttpRequest now?sss
});
}
}
bool AbstractServer::listen(qint16 port, const QHostAddress &host)
{
mLoggerFactory = createLoggerFactory();
mLogger = mLoggerFactory->createLogger(this);
mLogger->debug(QStringLiteral("listen"));
connect(mSvr, &QTcpServer::newConnection,
this, &AbstractServer::slotNewConnection);
return mSvr->listen(host, port);
}
}