blob: 8eb37501713daec13fb162895fd01ea031de5a40 (
plain) (
blame)
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
|
#include "listenserver.h"
#include "client.h"
#include <QSslSocket>
#define MAX_CLIENTS 50
/**
* Initialize listenServer to listen on specific port.
* And connect Signal newConnection() with Slot newClientConnection().
* @param port
*/
ListenServer::ListenServer(quint16 port)
{
if (!_server.listen(QHostAddress::Any, port) || !_server.isListening())
qFatal("Cannot bind to TCP port %d (incoming SSL clients)", (int)port);
connect(&_server, SIGNAL(newConnection()), this, SLOT(newClientConnection()));
}
ListenServer::~ListenServer()
{
_server.close();
for (int i = 0; i < _clients.size(); ++i)
_clients[i]->deleteLater(); // TODO are there nullptrs?
}
/*
* Slots
*/
/**
* Handle new client connection.
* If there are not already to much clients, emit signal newClient().
*/
void ListenServer::newClientConnection()
{
QSslSocket* sock;
while ((sock = (QSslSocket*)_server.nextPendingConnection()) != NULL)
{
if (_clients.size() >= MAX_CLIENTS)
{
sock->abort();
sock->deleteLater();
continue;
}
Client* client = new Client(sock); // TODO: what happens with disconnected clients
_clients.append(client); // create new client class and add to list
emit newClient(client);
}
}
|