forked from CPP-Final-Project/Chat_Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserController.cpp
76 lines (63 loc) · 3.06 KB
/
UserController.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
#include "UserController.h"
UserController::UserController(QObject* object_) : QObject(object_) {}
UserController::~UserController() {}
QSharedPointer<UserController> UserController::instance()
{
if (!shp_instance)
{
shp_instance = QSharedPointer<UserController>(new UserController());
}
return shp_instance;
}
void UserController::addConnection(qintptr socket_descriptor_)
{
QSharedPointer<SrvUser> user_connection = QSharedPointer<SrvUser>(new SrvUser());
if (!user_connection->setSocketDescriptor(socket_descriptor_)) {
user_connection->deleteLater(); //if the socket descriptor could not be set, delete the socket
PLOGE << "Socket descriptor could not be set";
return;
}
connect(user_connection.get(), &SrvUser::disconnectedFromClient, this, std::bind(&UserController::userDisconnected, this, user_connection));
connect(user_connection.get(), &SrvUser::errorSignal, this, std::bind(&UserController::userError, this, user_connection));
connect(user_connection.get(), &SrvUser::jsonReceived, MessageController::instance().get(),
[this, user_connection](const QJsonObject& doc) {
MessageController::instance()->jsonReceived(user_connection, doc);
}); //connect with MessageController (это нужно перенести в MessageController)
connect(user_connection.get(), &SrvUser::jsonWMedia, MessageController::instance().get(),
[this, user_connection](const QJsonObject& doc, const QByteArray& data) {
MessageController::instance()->jsonWMediaReceived(user_connection, doc, data);
}); //connect with MessageController (это нужно перенести в MessageController)
connect(user_connection.get(), &SrvUser::mediaRequest, MessageController::instance().get(),
[this, user_connection](const QJsonObject& doc) {
MessageController::instance()->jsonReceived(user_connection, doc);
});
connected_users.insert(user_connection);
PLOGI << "New client Connected! Now users: " + QString::number(connected_users.size());
}
void UserController::disableUsers()
{
QtConcurrent::map(connected_users, [](QSharedPointer<SrvUser> user) {
user->disconnectFromClient();
}).waitForFinished();
}
void UserController::userDisconnected(QSharedPointer<SrvUser> sender_)
{
connected_users.remove(sender_);
const quint32 userRoom = sender_->getRoomId();
if (userRoom != 0) {
RoomController::instance()->userLeave(sender_);
//добавить connection() и заменить на emit user leave
}
PLOGI << sender_->getUserName() + QLatin1String(" disconnected, total users left: ") + QString::number(connected_users.size());
//sender_->deleteLater();
PLOGI << "User disconected from server. Id: " + sender_->getUserName();
}
void UserController::userError(const QSharedPointer<SrvUser> sender_)
{
Q_UNUSED(sender_)
//PLOGE << QLatin1String("Error from ")/* + sender_->getUserName()*/;
}
QSet<QSharedPointer<SrvUser>> UserController::getUsersList() const
{
return connected_users;
}