-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_server.cpp
152 lines (135 loc) · 4.63 KB
/
websocket_server.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
/*
* Copyright (c) 2017-2018 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#ifdef USE_WEBSOCKETS
#include "deconz/dbg_trace.h"
#include "websocket_server.h"
/*! Constructor.
*/
WebSocketServer::WebSocketServer(QObject *parent, quint16 port) :
QObject(parent)
{
srv = new QWebSocketServer("deconz", QWebSocketServer::NonSecureMode, this);
quint16 p = 0;
quint16 ports[] = { 443, 443, 8080, 8088, 20877, 0 }; // start with proxy frinedly ports first, use random port as fallback
if (port > 0)
{
ports[0] = port;
}
while (!srv->listen(QHostAddress::AnyIPv4, ports[p]))
{
if (ports[p] == 0)
{
DBG_Printf(DBG_ERROR, "giveup starting websocket server on port %u. error: %s\n", ports[p], qPrintable(srv->errorString()));
break;
}
DBG_Printf(DBG_ERROR, "failed to start websocket server on port %u. error: %s\n", ports[p], qPrintable(srv->errorString()));
p++;
}
if (srv->isListening())
{
DBG_Printf(DBG_INFO, "started websocket server at port %u\n", srv->serverPort());
connect(srv, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
}
}
/*! Returns the websocket server port.
\return the active server port, or 0 if not active
*/
quint16 WebSocketServer::port() const
{
return srv->isListening() ? srv->serverPort() : 0;
}
/*! Handler for new client connections.
*/
void WebSocketServer::onNewConnection()
{
while (srv->hasPendingConnections())
{
QWebSocket *sock = srv->nextPendingConnection();
DBG_Printf(DBG_INFO, "New websocket %s:%u (state: %d) \n", qPrintable(sock->peerAddress().toString()), sock->peerPort(), sock->state());
connect(sock, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
clients.push_back(sock);
}
}
/*! Handle websocket disconnected signal.
*/
void WebSocketServer::onSocketDisconnected()
{
for (size_t i = 0; i < clients.size(); i++)
{
QWebSocket *sock = qobject_cast<QWebSocket*>(sender());
DBG_Assert(sock);
if (sock && clients[i] == sock)
{
DBG_Printf(DBG_INFO, "Websocket disconnected %s:%u (state: %d) \n", qPrintable(sock->peerAddress().toString()), sock->peerPort(), sock->state());
sock->deleteLater();
clients[i] = clients.back();
clients.pop_back();
}
}
}
/*! Handle websocket error signal.
\param err - the error which occured
*/
void WebSocketServer::onSocketError(QAbstractSocket::SocketError err)
{
Q_UNUSED(err);
for (size_t i = 0; i < clients.size(); i++)
{
QWebSocket *sock = qobject_cast<QWebSocket*>(sender());
DBG_Assert(sock);
if (sock && clients[i] == sock)
{
DBG_Printf(DBG_INFO, "Remove websocket %s:%u after error %s\n",
qPrintable(sock->peerAddress().toString()), sock->peerPort(), qPrintable(sock->errorString()));
sock->deleteLater();
clients[i] = clients.back();
clients.pop_back();
}
}
}
/*! Broadcasts a message to all connected clients.
\param msg the message as JSON string
*/
void WebSocketServer::broadcastTextMessage(const QString &msg)
{
for (size_t i = 0; i < clients.size(); i++)
{
QWebSocket *sock = clients[i];
if (sock->state() != QAbstractSocket::ConnectedState)
{
DBG_Printf(DBG_INFO, "Websocket %s:%u unexpected state: %d\n", qPrintable(sock->peerAddress().toString()), sock->peerPort(), sock->state());
}
qint64 ret = sock->sendTextMessage(msg);
DBG_Printf(DBG_INFO_L2, "Websocket %s:%u send message: %s (ret = %d)\n", qPrintable(sock->peerAddress().toString()), sock->peerPort(), qPrintable(msg), ret);
sock->flush();
}
}
/*! Flush the sockets of all connected clients.
*/
void WebSocketServer::flush()
{
for (size_t i = 0; i < clients.size(); i++)
{
QWebSocket *sock = clients[i];
if (sock->state() == QAbstractSocket::ConnectedState)
{
sock->flush();
}
}
}
#else // no websockets
WebSocketServer::WebSocketServer(QObject *parent) :
QObject(parent)
{ }
void WebSocketServer::onNewConnection() { }
void WebSocketServer::broadcastTextMessage(const QString &) { }
quint16 WebSocketServer::port() const { return 0; }
#endif