Torc  0.1
torcwebsocketpool.cpp
Go to the documentation of this file.
1 /* Class TorcWebSocketPool
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2013-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Torc
24 #include "torclogging.h"
25 #include "torcwebsocketpool.h"
26 
28  : QObject(),
29  m_webSockets(),
30  m_webSocketsLock(QMutex::Recursive)
31 {
32 }
33 
35 {
36  CloseSockets();
37 }
38 
40 {
41  QMutexLocker locker(&m_webSocketsLock);
42  if (!m_webSockets.isEmpty())
43  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Closing outstanding websockets"));
44  while (!m_webSockets.isEmpty())
45  {
46  TorcWebSocketThread* thread = m_webSockets.takeLast();
47  thread->quit();
48  thread->wait();
49  delete thread;
50  }
51 }
52 
54 {
55  QThread *thread = static_cast<QThread*>(sender());
56 
57  QMutexLocker locker(&m_webSocketsLock);
58 
59  for (int i = 0; i < m_webSockets.size(); ++i)
60  {
61  if (m_webSockets.value(i) == thread)
62  {
63  TorcWebSocketThread *socket = m_webSockets.takeAt(i);
64 
65  socket->quit();
66  socket->wait();
67  delete socket;
68  LOG(VB_NETWORK, LOG_INFO, QStringLiteral("WebSocket thread deleted"));
69  break;
70  }
71  }
72 }
73 
74 void TorcWebSocketPool::IncomingConnection(qintptr SocketDescriptor, bool Secure)
75 {
76  QMutexLocker locker(&m_webSocketsLock);
77  if (m_webSockets.size() <= MAX_SOCKET_THREADS)
78  {
79  TorcWebSocketThread *thread = new TorcWebSocketThread(SocketDescriptor, Secure);
80  m_webSockets.append(thread);
82  thread->start();
83  }
84  else
85  {
86  // Not sure whether this is the polite thing to do or not!
87  QTcpSocket *socket = new QTcpSocket();
88  socket->setSocketDescriptor(SocketDescriptor);
89  socket->close();
90  delete socket;
91  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("Ignoring incoming connection - too many connections"));
92  }
93 }
94 
96 {
97  if (!Socket)
98  return nullptr;
99 
100  QMutexLocker locker(&m_webSocketsLock);
101  for (int i = 0; i < m_webSockets.size(); ++i)
102  {
103  if (m_webSockets.value(i) == Socket)
104  return m_webSockets.takeAt(i);
105  }
106 
107  return nullptr;
108 }
void IncomingConnection(qintptr SocketDescriptor, bool Secure)
#define MAX_SOCKET_THREADS
void Finished(void)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
Wraps a TorcQThread around a TorcWebsocket.
TorcWebSocketThread * TakeSocket(TorcWebSocketThread *Socket)