-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChattingServer.cpp
48 lines (36 loc) · 1.12 KB
/
ChattingServer.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
#include "ChattingServer.h"
#include "ServerThread.h"
#include <QTcpSocket>
#include <QHostAddress>
#include <QTcpServer>
ChattingServer::ChattingServer(QObject *parent)
: QTcpServer(parent)
{
}
void ChattingServer::startServer() {
if (!this->listen(QHostAddress::Any, 1234)) {
qDebug() << "Server was unable to start listening";
} else {
qDebug() << "Waiting for clients";
}
}
void ChattingServer::incomingConnection(qintptr handle) {
qDebug() << handle << " is connecting...";
ServerThread* thread = new ServerThread(handle, this);
this->threads.append(thread);
(void)connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
void ChattingServer::spreadMessage(Message* message, QString name) {
qDebug() << "Socket descriptor: " << message->sender;
for (int i = 0; i < this->threads.size(); i++) {
ServerThread* thread = this->threads.at(i);
qDebug() << "Thread descriptor: " << thread->getSocketDescriptor();
if (thread->getSocketDescriptor() == message->sender) {
qDebug() << "Skip";
continue;
}
qDebug() << "Sending message";
thread->sendMessage(message, name);
}
}