work save: DAP protocol:

Create / parse request message
This commit is contained in:
Roy Qu 2024-03-09 13:58:32 +08:00
parent 23e918304e
commit 8d1cda13b3
3 changed files with 92 additions and 0 deletions

View File

@ -129,6 +129,7 @@ SOURCES += \
debugger/debugger.cpp \
debugger/gdbmidebugger.cpp \
debugger/gdbmiresultparser.cpp \
debugger/dapprotocol.cpp \
cpprefacter.cpp \
parser/cppparser.cpp \
parser/cpppreprocessor.cpp \
@ -259,6 +260,7 @@ HEADERS += \
debugger/debugger.h \
debugger/gdbmidebugger.h \
debugger/gdbmiresultparser.h \
debugger/dapprotocol.h \
cpprefacter.h \
customfileiconprovider.h \
parser/cppparser.h \

View File

@ -0,0 +1,47 @@
#include "dapprotocol.h"
#include <QJsonDocument>
#include <QJsonObject>
QString createDAPRequestMessage(qint64 seq, const QString &command, const QVariantMap &arguments)
{
QJsonObject obj;
obj["seq"]=seq;
obj["type"]="request";
obj["command"]=command;
if (!arguments.isEmpty()) {
obj["arguments"]= QJsonObject::fromVariantMap(arguments);
}
QJsonDocument doc;
doc.setObject(obj);
QString contentPart = doc.toJson(QJsonDocument::JsonFormat::Indented);
QString message = QString("Content-Length: %1\r\n").arg(contentPart.length())+contentPart;
return message;
}
DAPRequest parseDAPRequestMessage(const QByteArray &contentPart)
{
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(contentPart, &error);
if (error.error != QJsonParseError::NoError) {
throw DAPMessageError(QObject::tr("Failed to parse json content: %1").arg(QString::fromUtf8(contentPart)));
}
QJsonObject obj = doc.object();
DAPRequest req;
if (!obj.contains("seq")) {
throw DAPMessageError(QObject::tr("The request message don't have '%1' field!").arg("seq"));
}
req.seq = obj["seq"].toDouble();
req.type = "request";
if (!obj.contains("command")) {
throw DAPMessageError(QObject::tr("The request message don't have '%1' field!").arg("command"));
}
req.command = obj["command"].toString();
if (obj.contains("arguments"))
req.arguments = obj["arguments"].toObject().toVariantMap();
return req;
}
DAPMessageError::DAPMessageError(const QString &reason) : BaseError{reason}
{
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (C) 2020-2024 Roy Qu (royqh1979@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef DAP_PROTOCAL_H
#define DAP_PROTOCAL_H
#include <QString>
#include <QMap>
#include <QVariant>
#include <qt_utils/utils.h>
class DAPMessageError : public BaseError {
public:
explicit FileError(const QString& reason);
};
struct DAPProtocolMessage{
qint64 seq;
QString type;
};
struct DAPRequest: public DAPProtocolMessage {
QString command;
QVariantMap arguments;
};
QString createDAPRequestMessage(
qint64 seq, const QString &command, const QVariantMap& arguments);
DAPRequest parseDAPRequestMessage(const QByteArray& contentPart);
#endif