first commit
This commit is contained in:
111
socketclient.cpp
Normal file
111
socketclient.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include "socketclient.h"
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <errno.h>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QDebug>
|
||||
|
||||
SocketClient::SocketClient(const QString& socketPath, QObject *parent)
|
||||
: QObject(parent), m_socketPath(socketPath)
|
||||
{
|
||||
}
|
||||
|
||||
void SocketClient::sendCommand(const QString& command, const QJsonObject& params,
|
||||
ResponseCallback onSuccess, ErrorCallback onError)
|
||||
{
|
||||
qDebug() << "[SocketClient] Sending command:" << command;
|
||||
qDebug() << "[SocketClient] Parameters:" << params;
|
||||
|
||||
QJsonObject response = executeCommand(command, params);
|
||||
|
||||
qDebug() << "[SocketClient] Response:" << response;
|
||||
|
||||
if (response.isEmpty()) {
|
||||
QString errorMsg = "Failed to connect to socket: " + m_socketPath;
|
||||
qDebug() << "[SocketClient] ERROR:" << errorMsg;
|
||||
emit connectionError(errorMsg);
|
||||
if (onError) {
|
||||
onError(errorMsg);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
QString status = response["status"].toString();
|
||||
if (status == "success") {
|
||||
qDebug() << "[SocketClient] Command successful";
|
||||
emit commandSuccess(response);
|
||||
if (onSuccess) {
|
||||
onSuccess(response);
|
||||
}
|
||||
} else {
|
||||
QString errorMsg = response["message"].toString("Unknown error");
|
||||
qDebug() << "[SocketClient] Command error:" << errorMsg;
|
||||
emit commandError(errorMsg);
|
||||
if (onError) {
|
||||
onError(errorMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject SocketClient::executeCommand(const QString& command, const QJsonObject& params)
|
||||
{
|
||||
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
qDebug() << "[SocketClient] Failed to create socket";
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, m_socketPath.toUtf8().constData(), sizeof(addr.sun_path) - 1);
|
||||
|
||||
if (::connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||
qDebug() << "[SocketClient] Failed to connect to socket:" << m_socketPath;
|
||||
qDebug() << "[SocketClient] Error:" << strerror(errno);
|
||||
close(sock);
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
QJsonObject cmdObj;
|
||||
cmdObj["command"] = command;
|
||||
if (!params.isEmpty()) {
|
||||
cmdObj["params"] = params;
|
||||
}
|
||||
|
||||
QJsonDocument cmdDoc(cmdObj);
|
||||
QByteArray cmdData = cmdDoc.toJson(QJsonDocument::Compact);
|
||||
|
||||
qDebug() << "[SocketClient] Sending:" << cmdData;
|
||||
|
||||
ssize_t sent = send(sock, cmdData.constData(), cmdData.size(), 0);
|
||||
if (sent < 0) {
|
||||
qDebug() << "[SocketClient] Failed to send data:" << strerror(errno);
|
||||
close(sock);
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
char buffer[4096];
|
||||
int bytesRead = recv(sock, buffer, sizeof(buffer) - 1, 0);
|
||||
|
||||
if (bytesRead < 0) {
|
||||
qDebug() << "[SocketClient] Failed to receive data:" << strerror(errno);
|
||||
close(sock);
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
close(sock);
|
||||
|
||||
if (bytesRead > 0) {
|
||||
buffer[bytesRead] = '\0';
|
||||
qDebug() << "[SocketClient] Received:" << QByteArray(buffer, bytesRead);
|
||||
QJsonDocument responseDoc = QJsonDocument::fromJson(QByteArray(buffer, bytesRead));
|
||||
return responseDoc.object();
|
||||
}
|
||||
|
||||
qDebug() << "[SocketClient] No data received";
|
||||
return QJsonObject();
|
||||
}
|
||||
Reference in New Issue
Block a user