From 8d1cda13b31501d7f54558feba74ac39c7e2cb26 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Sat, 9 Mar 2024 13:58:32 +0800 Subject: [PATCH] work save: DAP protocol: Create / parse request message --- RedPandaIDE/RedPandaIDE.pro | 2 ++ RedPandaIDE/debugger/dapprotocol.cpp | 47 ++++++++++++++++++++++++++++ RedPandaIDE/debugger/dapprotocol.h | 43 +++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 RedPandaIDE/debugger/dapprotocol.cpp create mode 100644 RedPandaIDE/debugger/dapprotocol.h diff --git a/RedPandaIDE/RedPandaIDE.pro b/RedPandaIDE/RedPandaIDE.pro index b52481a9..aca74fd6 100644 --- a/RedPandaIDE/RedPandaIDE.pro +++ b/RedPandaIDE/RedPandaIDE.pro @@ -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 \ diff --git a/RedPandaIDE/debugger/dapprotocol.cpp b/RedPandaIDE/debugger/dapprotocol.cpp new file mode 100644 index 00000000..626ed48c --- /dev/null +++ b/RedPandaIDE/debugger/dapprotocol.cpp @@ -0,0 +1,47 @@ +#include "dapprotocol.h" +#include +#include + +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} +{ + +} diff --git a/RedPandaIDE/debugger/dapprotocol.h b/RedPandaIDE/debugger/dapprotocol.h new file mode 100644 index 00000000..811d5d2b --- /dev/null +++ b/RedPandaIDE/debugger/dapprotocol.h @@ -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 . + */ +#ifndef DAP_PROTOCAL_H +#define DAP_PROTOCAL_H +#include +#include +#include +#include + +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