-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbackserver.cpp
More file actions
98 lines (78 loc) · 2.05 KB
/
callbackserver.cpp
File metadata and controls
98 lines (78 loc) · 2.05 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
#include <QtCore/QCoreApplication>
#include <QtCore/QThreadPool>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QHostAddress>
#include <QtQml/qqml.h>
#include <QDebug>
#include "connection.h"
#include "callbackserver.h"
#include "enums.h"
namespace com { namespace cutehacks { namespace auctor {
CallbackServer::CallbackServer(QObject *parent) :
QTcpServer(parent),
m_port(9100),
m_address(QHostAddress::LocalHost)
{
start();
}
QString CallbackServer::address() const {
if (m_address == QHostAddress::LocalHost)
return QStringLiteral("localhost");
return m_address.toString();
}
void CallbackServer::start()
{
bool success = listen(m_address, (quint16)m_port);
if (!success)
qWarning("Could not bind to port: %s", qUtf8Printable(errorString()));
}
void CallbackServer::stop()
{
if (isListening())
close();
}
void CallbackServer::restartServer()
{
stop();
start();
}
void CallbackServer::incomingConnection(qintptr socketDescriptor)
{
// Hand off the handling to a separate thread
Connection *conn = new Connection(this, socketDescriptor);
connect(conn, SIGNAL(receivedParams(QVariantMap)), this, SLOT(emitCallback(QVariantMap)));
QThreadPool::globalInstance()->start(conn);
}
void CallbackServer::setPort(int port)
{
if (m_port == port)
return;
m_port = port;
emit portChanged(port);
restartServer();
}
void CallbackServer::setAddress(QString address)
{
QHostAddress newAddress(address);
if (m_address == newAddress)
return;
m_address = newAddress;
emit addressChanged(m_address.toString());
restartServer();
}
void CallbackServer::emitCallback(QVariantMap params)
{
emit callbackCalled(params);
}
static void registerTypes() {
qmlRegisterType<CallbackServer>(
"com.cutehacks.auctor",
1, 0,
"CallbackServer");
qmlRegisterUncreatableType<GrantType>(
"com.cutehacks.auctor",
1, 0,
"GrantType", "Class just contains an enumeration");
}
Q_COREAPP_STARTUP_FUNCTION(registerTypes)
} } }