-
Notifications
You must be signed in to change notification settings - Fork 7
/
abstractedserver.cpp
executable file
·163 lines (135 loc) · 4.85 KB
/
abstractedserver.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
#include "abstractedserver.h"
#include <QEventLoop>
#include <QTime>
#include <QBluetoothSocket>
#include <QTcpSocket>
#include <QThread>
#include "fakeinput.h"
AbstractedServer::AbstractedServer()
: bluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this),
tcpServer(this),
eventLoop(this),
timeoutTimer(this),
pendingSocket(0)
{
tcpServer.setMaxPendingConnections(1);
bluetoothServer.setMaxPendingConnections(1);
QObject::connect(&tcpServer, SIGNAL(newConnection()), this, SLOT(newTcpConnection()));
QObject::connect(&bluetoothServer, SIGNAL(newConnection()), this, SLOT(newBluetoothConnection()));
QObject::connect(&timeoutTimer, SIGNAL(timeout()), this, SLOT(timeout()));
timeoutTimer.setSingleShot(true);
trySetupServers();
}
AbstractedServer::~AbstractedServer()
{
if(pendingSocket != 0)
delete pendingSocket;
}
void AbstractedServer::newTcpConnection()
{
eventLoop.quit();
QTcpSocket *tcpSocket = tcpServer.nextPendingConnection();
tcpSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
if(pendingSocket == 0) {
pendingSocket = tcpSocket;
pendingIsBluetooth = false;
QString clientIp = tcpSocket->peerAddress().toString();
int index = clientIp.lastIndexOf(':'); index = index==-1?0:index;
clientIp = clientIp.right(clientIp.length() - index - 1);
pendingSocketInfo = clientIp;
emit newConnection();
}
else
delete tcpSocket;
}
void AbstractedServer::newBluetoothConnection()
{
eventLoop.quit();
QBluetoothSocket *bluetoothSocket = bluetoothServer.nextPendingConnection();
if(pendingSocket == 0) {
pendingSocket = bluetoothSocket;
pendingIsBluetooth = true;
pendingSocketInfo = "Connected via Bluetooth";
emit newConnection();
}
else
delete bluetoothSocket;
}
AbstractedSocket *AbstractedServer::nextPendingConnection()
{
if(pendingSocket == 0)
return 0;
else if(!pendingSocket->isOpen()) {
delete pendingSocket;
return 0;
}
AbstractedSocket *ret = new AbstractedSocket(pendingSocket, pendingIsBluetooth);
pendingSocket = 0;
return ret;
}
bool AbstractedServer::registerBluetoothService()
{
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("WifiMouseServer"));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription, tr("WifiMouseServer"));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("wifi-mouse.xyz"));
static const QLatin1String serviceUuid("c05efa2b-5e9f-4a39-9705-72ccf47d2eb8");
serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));
QBluetoothServiceInfo::Sequence publicBrowse;
publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, publicBrowse);
QBluetoothServiceInfo::Sequence protocolDescriptorList;
QBluetoothServiceInfo::Sequence protocol;
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap));
protocolDescriptorList.append(QVariant::fromValue(protocol));
protocol.clear();
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
<< QVariant::fromValue(quint8(bluetoothServer.serverPort()));
protocolDescriptorList.append(QVariant::fromValue(protocol));
serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList);
return serviceInfo.registerService();
}
bool AbstractedServer::bluetoothServerListen()
{
if(!bluetoothServer.isListening() && !bluetoothServer.listen()) {
qInfo() << "Unable to start bluetooth server.\n";
return false;
}
if(serviceInfo.isRegistered() == false) {
if( !registerBluetoothService() ) {
qInfo() << "Couldn't register service";
return false;
}
}
return true;
}
bool AbstractedServer::tcpServerListen()
{
if(!tcpServer.isListening() && !tcpServer.listen(QHostAddress::Any, 9798)) {
qInfo() << "Unable to start TCP server on port 9798.\n";
return false;
}
return true;
}
void AbstractedServer::timeout() {
eventLoop.quit();
}
void AbstractedServer::listenWithTimeout(qint16 timeoutMs)
{
trySetupServers();
qint64 until = QDateTime::currentMSecsSinceEpoch() + timeoutMs;
while(!tcpServer.hasPendingConnections()
&& !bluetoothServer.hasPendingConnections()
&& !QThread::currentThread()->isInterruptionRequested()
&& QDateTime::currentMSecsSinceEpoch() < until) {
timeoutTimer.setInterval(until - QDateTime::currentMSecsSinceEpoch());
timeoutTimer.start();
eventLoop.exec();
timeoutTimer.stop();
}
}
void AbstractedServer::trySetupServers()
{
// these can be recalled if initial setup failed
bluetoothServerListen();
tcpServerListen();
}