summaryrefslogtreecommitdiffstats
path: root/src/server/net/listenserver.cpp
blob: 27a1412c9a443282cf2309489b29a1c7b68ea411 (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
#include "listenserver.h"
#include "client.h"

#include <QSslSocket>

/**
 * Initialize listenServer to listen on specific port.
 * And connect Signal newConnection() with Slot newClientConnection().
 * @param port
 */
ListenServer::ListenServer(quint16 port, QObject *parent)
		: QObject(parent)
		, _server(this)
{
	if (!_server.listen(QHostAddress::AnyIPv4, port) || !_server.isListening())
		qFatal("Cannot bind to TCP port %d (incoming SSL clients)", int(port));
	connect(&_server, &SslServer::newConnection, this, &ListenServer::newClientConnection);
}

ListenServer::~ListenServer()
{
	_server.close();
}

/*
 * Slots
 */

/**
 * Handle new client connection.
 * If there are not already to much clients, emit signal newClient().
 */
void ListenServer::newClientConnection()
{
	QTcpSocket* sock;
	while ((sock = _server.nextPendingConnection()) != nullptr) {
		auto* client = new Client(sock); // TODO: what happens with disconnected clients
		emit newClient(client);
	}
}