| 1 | #ifndef OPENMSXCONNECTION_HH |
| 2 | #define OPENMSXCONNECTION_HH |
| 3 | |
| 4 | #include <QObject> |
| 5 | #include <QAbstractSocket> |
| 6 | #include <QXmlStreamAttributes> |
| 7 | #include <QQueue> |
| 8 | #include <memory> |
| 9 | #include <functional> |
| 10 | |
| 11 | class QXmlStreamReader; |
| 12 | |
| 13 | class CommandBase |
| 14 | { |
| 15 | public: |
| 16 | virtual ~CommandBase() = default; |
| 17 | |
| 18 | virtual QString getCommand() const = 0; |
| 19 | virtual void replyOk (const QString& message) = 0; |
| 20 | virtual void replyNok(const QString& message) = 0; |
| 21 | virtual void cancel() = 0; |
| 22 | }; |
| 23 | |
| 24 | class SimpleCommand : public CommandBase |
| 25 | { |
| 26 | public: |
| 27 | SimpleCommand(QString command_) |
| 28 | : command(std::move(command_)) {} |
| 29 | QString getCommand() const override { return command; } |
| 30 | void replyOk (const QString& message) override; |
| 31 | void replyNok(const QString& message) override; |
| 32 | void cancel() override; |
| 33 | |
| 34 | private: |
| 35 | QString command; |
| 36 | }; |
| 37 | |
| 38 | class Command : public CommandBase |
| 39 | { |
| 40 | public: |
| 41 | Command(QString command, |
| 42 | std::function<void(const QString&)> okCallback, |
| 43 | std::function<void(const QString&)> errorCallback = {}); |
| 44 | |
| 45 | QString getCommand() const override; |
| 46 | |
| 47 | void replyOk (const QString& message) override; |
| 48 | void replyNok(const QString& message) override; |
| 49 | void cancel() override; |
| 50 | |
| 51 | private: |
| 52 | QString command; |
| 53 | std::function <void (const QString&)> okCallback; |
| 54 | std::function <void (const QString&)> errorCallback; |
| 55 | }; |
| 56 | |
| 57 | class ReadDebugBlockCommand : public SimpleCommand |
| 58 | { |
| 59 | public: |
| 60 | ReadDebugBlockCommand(const QString& commandString, unsigned size, |
| 61 | unsigned char* target); |
| 62 | ReadDebugBlockCommand(const QString& debuggable, unsigned offset, unsigned size, |
| 63 | unsigned char* target); |
| 64 | |
| 65 | protected: |
| 66 | void copyData(const QString& message); |
| 67 | |
| 68 | private: |
| 69 | unsigned size; |
| 70 | unsigned char* target; |
| 71 | }; |
| 72 | |
| 73 | class WriteDebugBlockCommand : public SimpleCommand |
| 74 | { |
| 75 | public: |
| 76 | WriteDebugBlockCommand(const QString& debuggable, unsigned offset, unsigned size, |
| 77 | unsigned char* source); |
| 78 | }; |
| 79 | |
| 80 | class OpenMSXConnection : public QObject |
| 81 | { |
| 82 | Q_OBJECT |
| 83 | public: |
| 84 | /** require: socket must be in connected state */ |
| 85 | OpenMSXConnection(QAbstractSocket* socket); |
| 86 | ~OpenMSXConnection() override; |
| 87 | |
| 88 | void sendCommand(CommandBase* command); |
| 89 | |
| 90 | signals: |
| 91 | void disconnected(); |
| 92 | void logParsed(const QString& level, const QString& message); |
| 93 | void updateParsed(const QString& type, const QString& name, const QString& message); |
| 94 | |
| 95 | private: |
| 96 | void processData(); |
| 97 | void socketStateChanged(QAbstractSocket::SocketState state); |
| 98 | void socketError(QAbstractSocket::SocketError state); |
| 99 | |
| 100 | void cleanup(); |
| 101 | void cancelPending(); |
| 102 | |
| 103 | bool startElement(const QStringRef& qName, const QXmlStreamAttributes& atts); |
| 104 | bool endElement(const QStringRef& qName); |
| 105 | bool characters(const QStringRef& ch); |
| 106 | |
| 107 | private: |
| 108 | //std::unique_ptr<QAbstractSocket> socket; |
| 109 | QAbstractSocket* socket; |
| 110 | std::unique_ptr<QXmlStreamReader> reader; |
| 111 | |
| 112 | QString xmlData; |
| 113 | QXmlStreamAttributes xmlAttrs; |
| 114 | QQueue<CommandBase*> commands; |
| 115 | bool connected; |
| 116 | }; |
| 117 | |
| 118 | #endif |