209 lines
8.4 KiB
C++
209 lines
8.4 KiB
C++
#include "datamanager.h"
|
|
DataManager *DataManager::p_this;
|
|
DataManager::DataManager(QObject *parent)
|
|
: QObject{parent}
|
|
{
|
|
p_this = this;
|
|
database = DataBase::getInstance();
|
|
protocol = Protocol::getInstance();
|
|
server = TCPServer::getInstance();
|
|
server->configAndrun();
|
|
server->setCallBack(disconnect_from_cln);
|
|
server->setCallBack(recv_from_cln);
|
|
}
|
|
|
|
void DataManager::disconnect_from_cln(QTcpSocket *sock)
|
|
{
|
|
qDebug() << "删除之前的数据" <<p_this->online_name_list;
|
|
for (int i = 0; i < p_this->online_socket_list.length(); i++)
|
|
{
|
|
if (p_this->online_socket_list.at(i) == sock)
|
|
{
|
|
p_this->online_name_list.removeAt(i);
|
|
p_this->online_socket_list.removeAt(i);
|
|
break;
|
|
}
|
|
}
|
|
qDebug() << "删除之后的数据" << p_this->online_name_list;
|
|
p_this->update_namelist(sock);
|
|
}
|
|
|
|
void DataManager::recv_from_cln(QByteArray &data, QTcpSocket *sock)
|
|
{
|
|
QByteArray data_decrypt = p_this->protocol->data_decrypt(data);
|
|
qDebug() << data_decrypt;
|
|
QList<QByteArray> list = data_decrypt.split('\n');
|
|
qDebug() << list;
|
|
for (int i = 0; i < list.count(); i++)
|
|
{
|
|
QJsonDocument json_data = QJsonDocument::fromJson(list[i]);
|
|
if (json_data["type"] == "message")
|
|
{
|
|
qDebug() << json_data;
|
|
if (json_data["to_user"].toString().isEmpty())
|
|
{
|
|
qDebug() << "收到群发类型的信息,将转发";
|
|
for (auto &one_socket : p_this->online_socket_list)
|
|
{
|
|
if (one_socket != sock)
|
|
{
|
|
p_this->server->sendToclient(one_socket, data);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "收到私发类型的信息,将转发";
|
|
QString to_user = json_data["to_user"].toString();
|
|
qDebug() << to_user;
|
|
for (int i = 0; i < p_this->online_name_list.length(); i++)
|
|
{
|
|
if (p_this->online_name_list.at(i) == to_user)
|
|
{
|
|
p_this->server->sendToclient(p_this->online_socket_list[i], data);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (json_data["type"] == "file")
|
|
{
|
|
if (json_data["to_user"].toString().isEmpty())
|
|
{
|
|
qDebug() << "收到群发类型的文件,将转发";
|
|
for (auto &one_socket : p_this->online_socket_list)
|
|
{
|
|
if (one_socket != sock)
|
|
{
|
|
p_this->server->sendToclient(one_socket, data);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "收到私发类型的文件,将转发";
|
|
QString to_user = json_data["to_user"].toString();
|
|
qDebug() << to_user;
|
|
for (int i = 0; i < p_this->online_name_list.length(); i++)
|
|
{
|
|
if (p_this->online_name_list.at(i) == to_user)
|
|
{
|
|
p_this->server->sendToclient(p_this->online_socket_list[i], data);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (json_data["type"] == "request")
|
|
{
|
|
if (json_data["action"] == "register")
|
|
{
|
|
qDebug() << "收到注册类型的信息,将进行处理";
|
|
QString username, password;
|
|
QJsonObject json = json_data["content"].toObject();
|
|
username = json["user"].toString();
|
|
password = json["password"].toString();
|
|
bool result = p_this->database->insertUsr(username, password);
|
|
qDebug() << result;
|
|
if (result == true)
|
|
{
|
|
QByteArray data_build, data_encrypt;
|
|
data_build = p_this->protocol->data_maker(Protocol::RESPONSE, QString("register"), QList<QString>{"result", "true"});
|
|
qDebug() << data_build;
|
|
data_encrypt = p_this->protocol->data_encrypt(data_build);
|
|
p_this->server->sendToclient(sock, data_encrypt);
|
|
}
|
|
else
|
|
{
|
|
QByteArray data_build, data_encrypt;
|
|
data_build = p_this->protocol->data_maker(Protocol::RESPONSE, QString("register"), QList<QString>{"result", "false"});
|
|
qDebug() << data_build;
|
|
data_encrypt = p_this->protocol->data_encrypt(data_build);
|
|
p_this->server->sendToclient(sock, data_encrypt);
|
|
}
|
|
}
|
|
else if (json_data["action"] == "signin")
|
|
{
|
|
qDebug() << "收到登录类型的信息,将进行处理";
|
|
QString username, password;
|
|
QJsonObject json = json_data["content"].toObject();
|
|
username = json["user"].toString();
|
|
password = json["password"].toString();
|
|
qDebug() << username <<password;
|
|
bool result = p_this->database->match(username, password);
|
|
qDebug() << result;
|
|
if (result == true)
|
|
{
|
|
p_this->online_socket_list.append(sock);
|
|
p_this->online_name_list.append(username);
|
|
QByteArray data_build, data_encrypt;
|
|
data_build = p_this->protocol->data_maker(Protocol::RESPONSE, QString("signin"), QList<QString>{"result", "true"});
|
|
qDebug() << data_build;
|
|
data_encrypt = p_this->protocol->data_encrypt(data_build);
|
|
p_this->server->sendToclient(sock, data_encrypt);
|
|
p_this->update_namelist(sock);
|
|
}
|
|
else
|
|
{
|
|
QByteArray data_build, data_encrypt;
|
|
data_build = p_this->protocol->data_maker(Protocol::RESPONSE, QString("signin"), QList<QString>{"result", "false"});
|
|
qDebug() << data_build;
|
|
data_encrypt = p_this->protocol->data_encrypt(data_build);
|
|
p_this->server->sendToclient(sock, data_encrypt);
|
|
}
|
|
}
|
|
else if (json_data["action"] == "cancellation")
|
|
{
|
|
qDebug() << "收到注销类型的数据,将进行处理";
|
|
QJsonObject json = json_data["content"].toObject();
|
|
QString username = json["name"].toString();
|
|
qDebug() << username;
|
|
if (!p_this->database->Delete(username))
|
|
{
|
|
qDebug() << "用户注销失败";
|
|
return;
|
|
}
|
|
|
|
}
|
|
else if (json_data["action"] == "update_namelist")
|
|
{
|
|
qDebug() << "接收到更新用户列表的请求";
|
|
QJsonObject json = json_data["content"].toObject();
|
|
QString username = json["name"].toString();
|
|
for (int i = 0; i < p_this->online_name_list.length(); i++)
|
|
{
|
|
if (p_this->online_name_list.at(i) == username)
|
|
{
|
|
p_this->online_name_list.removeAt(i);
|
|
p_this->online_socket_list.removeAt(i);
|
|
break;
|
|
}
|
|
}
|
|
p_this->update_namelist(sock);
|
|
/*QByteArray data_build, data_encrypt;
|
|
data_build = p_this->protocol->data_maker(Protocol::ONLINEUSERS, 4, p_this->online_name_list);
|
|
data_encrypt = p_this->protocol->data_encrypt(data_build);
|
|
p_this->server->sendToclient(sock, data_encrypt);*/
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "服务端收到未知类型的数据" << list[i];
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void DataManager::update_namelist(QTcpSocket *sock)
|
|
{
|
|
qDebug() << "发送更新用户在线列表信息";
|
|
QByteArray data_build, data_encrypt;
|
|
data_build = protocol->data_maker(Protocol::ONLINEUSERS, 4, online_name_list);
|
|
qDebug() << online_name_list;
|
|
data_encrypt = protocol->data_encrypt(data_build);
|
|
for (auto &one_socket : online_socket_list)
|
|
{
|
|
server->sendToclient(one_socket, data_encrypt);
|
|
}
|
|
}
|