This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunkBuffer.h
138 lines (118 loc) · 4.39 KB
/
ChunkBuffer.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/******************************************************************************
* This file is part of plNetLog. *
* *
* plNetLog 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. *
* *
* plNetLog 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 plNetLog. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************/
#ifndef _CHUNK_BUFFER_H
#define _CHUNK_BUFFER_H
#include <QLinkedList>
#include <QString>
#include <QMutex>
class ChunkBuffer
{
private:
ChunkBuffer(const ChunkBuffer&) { }
ChunkBuffer& operator=(const ChunkBuffer&) { }
public:
ChunkBuffer() { }
~ChunkBuffer() { clear(); }
void chomp(void* out, size_t size);
void skip(size_t size);
bool isEmpty() const { return m_chunks.isEmpty(); }
size_t size() const;
void clear()
{
while (!isEmpty()) {
delete[] m_chunks.front().m_data;
m_chunks.pop_front();
}
}
void append(const unsigned char* data, size_t size, unsigned time);
unsigned currentTime();
template<typename _Tp>
_Tp read()
{
_Tp temp;
chomp(&temp, sizeof(temp));
return temp;
}
template<size_t _Length>
QString readHex()
{
char buffer[_Length];
chomp(buffer, _Length);
return QString(QByteArray(buffer, _Length).toHex());
}
QString readString()
{
unsigned short length = read<unsigned short>();
unsigned short* utf16 = new unsigned short[length + 1];
chomp(utf16, length * sizeof(unsigned short));
utf16[length] = 0;
QString temp = QString::fromUtf16(utf16, length);
delete[] utf16;
return temp;
}
template <typename _Tp>
QString readPString()
{
_Tp length = read<_Tp>();
char* str = new char[length + 1];
chomp(str, length);
str[length] = 0;
QString temp = QString::fromUtf8(str, length);
delete[] str;
return temp;
}
template <typename _Tp>
QString readWPString()
{
_Tp length = read<_Tp>();
unsigned short* str = new unsigned short[length + 1];
chomp(str, length * sizeof(unsigned short));
str[length] = 0;
QString temp = QString::fromUtf16(str, length);
delete[] str;
return temp;
}
QString readUuid()
{
unsigned int u1 = read<unsigned int>();
unsigned short u2 = read<unsigned short>();
unsigned short u3 = read<unsigned short>();
unsigned char u4[8];
chomp(u4, 8);
return QString("{%1-%2-%3-%4%5-%6%7%8%9%10%11}").arg(u1, 8, 16, QChar('0'))
.arg(u2, 4, 16, QChar('0')).arg(u3, 4, 16, QChar('0'))
.arg(u4[0], 2, 16, QChar('0')).arg(u4[1], 2, 16, QChar('0'))
.arg(u4[2], 2, 16, QChar('0')).arg(u4[3], 2, 16, QChar('0'))
.arg(u4[4], 2, 16, QChar('0')).arg(u4[5], 2, 16, QChar('0'))
.arg(u4[6], 2, 16, QChar('0')).arg(u4[7], 2, 16, QChar('0'));
}
QString readResultCode();
QString readSafeString();
QString readSafeWString();
private:
struct Buffer
{
const unsigned char* m_data;
size_t m_size;
size_t m_cur;
unsigned m_time;
};
QMutex m_mutex;
QLinkedList<Buffer> m_chunks;
void waitOnData();
};
#endif