ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
jsonrpcserver.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // Qt
11 #include <QMap>
12 #include <QObject>
13 #include <QWebSocket>
14 #include <QWebSocketServer>
15 // STL
16 #include <functional>
17 
18 struct JsonRPCResult {
19  static JsonRPCResult error(int code, QString message) {
21  {
22  result.isError = true;
23  result.error_code = code;
24  result.error_message = message;
25  }
26  return result;
27  }
28 
29  static JsonRPCResult success(QVariant value) {
31  {
32  result.isError = false;
33  result.result = value;
34  }
35  return result;
36  }
37 
38  bool isError{true};
39  int error_code{-32601};
40  QString error_message = "Method not found";
41  QVariant result;
42 };
43 
44 class JsonRPCServer : public QObject {
45  Q_OBJECT
46 public:
47  explicit JsonRPCServer(QObject *parent = nullptr);
49 
50  void listen(unsigned int port);
51  void close();
52 
53 signals:
54  JsonRPCResult execute(QString method, QMap<QString, QVariant> params);
55 private slots:
56  void onNewConnection();
57  void onClosed();
58  void processTextMessage(QString message);
59  void processBinaryMessage(QByteArray message);
60  void socketDisconnected();
61 
62 private:
63  QWebSocketServer *ws_server{nullptr};
64  QList<QWebSocket *> connections;
65 };
#define slots
#define signals
cmdLineReadable * params[]
JsonRPCServer(QObject *parent=nullptr)
JsonRPCResult execute(QString method, QMap< QString, QVariant > params)
void listen(unsigned int port)
static JsonRPCResult success(QVariant value)
Definition: jsonrpcserver.h:29
QVariant result
Definition: jsonrpcserver.h:41
static JsonRPCResult error(int code, QString message)
Definition: jsonrpcserver.h:19
QString error_message
Definition: jsonrpcserver.h:40