/
mythhttpcommon.h
90 lines (73 loc) · 2.24 KB
/
mythhttpcommon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef MYTHHTTPCOMMON_H
#define MYTHHTTPCOMMON_H
// Qt
#include <QString>
#include <QMetaType>
// MythTV
#include "mythbaseexp.h"
// Std
#include <memory>
/// A group of functions shared between HTTP and WebSocket code.
#define HTTP_CHUNKSIZE 65536L // 64k
#define JSONRPC QStringLiteral("jsonrpc")
#define XMLRPC QStringLiteral("xmlrpc")
#define PLISTRPC QStringLiteral("plistrpc")
#define CBORRPC QStringLiteral("cborrpc")
enum MythSocketProtocol
{
ProtHTTP = 0, // Socket has not been upgraded
ProtFrame, // Default WebSocket text and binary frame transmission
ProtJSONRPC,
ProtXMLRPC,
ProtPLISTRPC,
ProtCBORRPC
};
class MythSharedData;
using DataPayload = std::shared_ptr<MythSharedData>;
using DataPayloads = std::vector<DataPayload>;
class MythSharedData : public QByteArray
{
public:
static DataPayload CreatePayload(size_t Size);
static DataPayload CreatePayload(const QString& Text);
static DataPayload CreatePayload(const QByteArray& Other);
int64_t m_readPosition { 0 };
int64_t m_writePosition { 0 };
protected:
explicit MythSharedData(uint64_t Size);
explicit MythSharedData(const QString& Text);
explicit MythSharedData(const QByteArray& Other);
};
class MythSharedString;
using StringPayload = std::shared_ptr<MythSharedString>;
class MythSharedString : public QString
{
public:
static StringPayload CreateString();
protected:
MythSharedString() = default;
};
Q_DECLARE_METATYPE(DataPayload)
Q_DECLARE_METATYPE(DataPayloads)
Q_DECLARE_METATYPE(StringPayload)
class MythHTTPWS
{
public:
static QString ProtocolToString(MythSocketProtocol Protocol);
static MythSocketProtocol ProtocolFromString(const QString& Protocols);
static inline QString BitrateToString(uint64_t Rate)
{
if (Rate < 1)
return "-";
else if (Rate > (1073741824L * 1024))
return ">1TBps";
else if (Rate >= 1073741824)
return QStringLiteral("%1GBps").arg(Rate / 1073741824.0);
else if (Rate >= 1048576)
return QStringLiteral("%1MBps").arg(Rate / 1048576.0);
else if (Rate >= 1024)
return QStringLiteral("%1kBps").arg(Rate / 1024.0);
return QStringLiteral("%1Bps").arg(Rate);
}
};
#endif