/
Shymer123
/
Messenger
Обзор
Документация
Войти
/
Shymer123
/
Messenger
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
UI_Client/SourceFiles/RequestResponseHandler/communicationHandler.h
149 строк
5 KB
Shymer123
The code has been refactored, now there are no incomprehensible UserRole, etc.
19 май 2025, 13:22
19 май 2025, 13:22
f8b3525
Код
Авторство
О чём код?
#ifndef COMMUNICATIONHANDLER_H #define COMMUNICATIONHANDLER_H #include <QAbstractSocket> #include <QObject> #include <QMutex> #include <QTimer> #include <QQueue> #include <QWaitCondition> #include <QFile> class QTcpSocket; class MessageData; struct FileTransferInfo { QFile file; uint32_t totalChunks = 0; uint32_t receivedChunks = 0; bool shouldDecompress = false; bool isDataSet = false; FileTransferInfo(const QString& fileName) : file(fileName) {} }; class CommunicationHandler : public QObject { Q_OBJECT public: CommunicationHandler(QTcpSocket* clientSocket); ~CommunicationHandler(); bool isOnline() const; public: void getIsNameAlreadyUse(const QString& userName); void getIsPhoneNumberAlreadyUse(const QString& phoneNumber); void addUserInDatabase(const QString& userName, const QString& hashedPassword, const QString& salt, const QString& phoneNumber); void addUserInServerCache(const int userID); void getStoredPasswordData(const QString& userName); void getUserID(const QString& userName); void getPhoneNumber(const int userID); void getChats(const int userID); void getSingleChat(const int chatID); void getMessages(const int chatID); void sendMessage(const MessageData& data, const std::vector<int>& participantsID); void getContacts(const int userID); void addContact(const int currentUserID, const QString& currentUserName, const int user2ID, const QString& user2Name, const QString& user2PhoneNumber); void downloadFile(const int fileID, const QString& filePath, bool shouldDecompress); void addFavourite(const int userID, const int tempChatID); void addDialog(const int currentUserID, const QString& contactName, const int tempChatID); void getUserData(const int chatID, const int currentUserID); void getParticipantIDInDialogUsePhoneNumber(const QString& user2PhoneNumber); void addGroup(const int userID, const QString& groupName, const QStringList& selectedMembers, const int tempChatID); void getGroupData(const int chatID); void getMembersIDInGroup(const int userID, const int chatID); void addMembersToGroup(const int groupID, const std::vector<int>& membersID, bool isCreated, const int creatorID); void getIsCombinedNameExists(const int userID, const QString& combinedName); void getIsPhoneNumberValid(const int userID, const QString& phoneNumber); signals: void getIsNameAlreadyUseReceived(const QJsonObject& response); void getIsPhoneNumberAlreadyUseReceived(const QJsonObject& response); void getStoredPasswordDataReceived(const QJsonObject& response); void getUserIDReceived(const QJsonObject& response); void getPhoneNumberReceived(const QJsonObject& response); void getChatsReceived(const QJsonObject& response); void getSingleChatReceived(const QJsonObject& response); void getMessagesReceived(const QJsonObject& response); void getContactsReceived(const QJsonObject& response); void getUserDataReceived(const QJsonObject& response); void getParticipantIDInDialogUsePhoneNumberReceived(const QJsonObject& response); void getGroupDataReceived(const QJsonObject& response); void getMembersIDInGroupReceived(const QJsonObject& response); void getIsCombinedNameExistsReceived(const QJsonObject& response); void getIsPhoneNumberValidReceived(const QJsonObject& response); void updateChatListReceived(const QJsonObject& response); void updateMessagesReceived(const QJsonObject& response); void messageDeliveredReceived(const QJsonObject& response); void chatDeliveredReceived(const QJsonObject& response); private: enum class Action { getIsNameAlreadyUse, getIsPhoneNumberAlreadyUse, getStoredPasswordData, getUserID, getPhoneNumber, getChats, getSingleChat, getMessages, getContacts, getUserData, getParticipantIDInDialogUsePhoneNumber, getGroupData, getMembersIDInGroup, getIsCombinedNameExists, getIsPhoneNumberValid, updateChatList, updateMessages, messageDelivered, chatDelivered, Unknown }; private: std::vector<char> compressData(const QByteArray& inputData); QByteArray decompressData(const QByteArray& compressedData); void sendRequest(const QJsonObject& jsonRequest, bool isFile = false); void sendFileMessage(const QJsonObject& jsonRequest, const QList<QString>& filePaths); void processNextResponse(); void handleResponse(const QJsonObject& response); Action stringToAction(const QString& actionStr); private slots: void onReadyRead(); bool receiveJson(); bool receiveFile(); void onErrorOccurred(QAbstractSocket::SocketError socketError); private: QTcpSocket* m_clientSocket = nullptr; QByteArray m_buffer; bool m_isProcessingResponse = false; QMutex m_queueMutex; QQueue<QByteArray> m_responseQueue; QHash<uint32_t, FileTransferInfo*> m_fileTransfers; }; #endif // COMMUNICATIONHANDLER_H