summaryrefslogtreecommitdiffstats
path: root/src/shared/networkmessage.cpp
blob: 3c6a6879282d15f72b4f8ebaaefa877733598997 (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * NetworkMessage.cpp
 *
 *  Created on: 18.01.2013
 *      Author: sr
 */

#include <QtCore>
#include <QtNetwork>
#include "networkmessage.h"

#define HEADER_LEN 8
#define MAX_MSG_LEN 60000

#define BYTE_SWAP4(x) \
    ((((x) & 0xFF000000u) >> 24) | \
     (((x) & 0x00FF0000u) >> 8)  | \
     (((x) & 0x0000FF00u) << 8)  | \
     (((x) & 0x000000FFu) << 24))

#define BYTE_SWAP2(x) \
    ((((x) & 0xFF00u) >> 8) | \
     (((x) & 0x00FFu) << 8))

static quint16 _htons(const quint16 x)
{
	if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
		return x;
	return quint16(BYTE_SWAP2(x));
}

static quint32 _htonl(const quint32 x)
{
	if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
		return x;
	return quint32(BYTE_SWAP4(x));
}

static quint16 _ntohs(const char *in)
{
	return quint16(quint8(in[0]) << 8
			| quint8(in[1]));
}

static quint32 _ntohl(const char *in)
{
	return quint32(quint8(in[0])) << 24
			| quint32(quint8(in[1])) << 16
			| quint32(quint8(in[2])) << 8
			| quint32(quint8(in[3]));
}

/*
 // ####################################################### \\ ° . °
 \\ ####################################################### // \___/
 */

NetworkMessage::NetworkMessage() :
	_buffer(nullptr), _bufferSize(0), _bufferPos(0), _lastBufferSize(0), _mode(0)
{
	//
}

NetworkMessage::~NetworkMessage()
{
	if (_buffer)
		delete[] _buffer;
}

inline void NetworkMessage::allocBuffer()
{
	if (_lastBufferSize < _bufferSize || _buffer == nullptr) {
		if (_buffer)
			delete[] _buffer;
		_lastBufferSize = _bufferSize;
		_buffer = new char[_lastBufferSize];
	}
}

int NetworkMessage::readMessage(QAbstractSocket* socket)
{
	// Check/Set the _mode variable, so read and write calls are not mixed
	if (_mode != 1) {
		if (_mode != 0) {
			qDebug("NetworkMessage::readMessage(TCP) called when class was in mode %d!", _mode);
			return NM_READ_FAILED;
		}
		_mode = 1;
	}
	// buffer size == 0 means the header hasn't been received yet. do so and set things up
	if (_bufferSize == 0) {
		if (socket->bytesAvailable() < HEADER_LEN)
			return NM_READ_INCOMPLETE;
		char header[HEADER_LEN];
		if (socket->read(header, HEADER_LEN) != HEADER_LEN) {
			qDebug("FIXME: Socket said 8 bytes available, but could not read 8...");
			return NM_READ_FAILED;
		}
		if (!this->parseHeader(header))
			return NM_READ_FAILED;
		//qDebug() << "Expecting message of " << _bufferSize << " bytes";
		allocBuffer();
	}
	if (_bufferSize > _bufferPos) {
		while (_bufferSize > _bufferPos && socket->bytesAvailable() > 0) {
			const qint64 ret = socket->read(_buffer + _bufferPos, _bufferSize - _bufferPos);
			//qDebug() << "Read " << ret << " bytes";
			if (ret < 0 || ret > (_bufferSize - _bufferPos)) {
				qDebug("Socket read failed (TCP), return code %d", int(ret));
				return NM_READ_FAILED;
			}
			_bufferPos += quint32(ret);
			//qDebug() << "Buffer has now " << _bufferPos << " of " << _bufferSize << " bytes";
		}
		if (_bufferSize == _bufferPos) {
			if (!this->parseMessage(_buffer))
				return NM_READ_FAILED;
		}
	}
	return NM_READ_OK;
}

int NetworkMessage::readMessage(char* data, quint32 len)
{
	// Check/Set the _mode variable, so read and write calls are not mixed
	if (_mode != 1) {
		if (_mode != 0) {
			qDebug("NetworkMessage::readMessage(UDP) called when class was in mode %d!", _mode);
			return NM_READ_FAILED;
		}
		_mode = 1;
	}
	if (len < HEADER_LEN) {
		qDebug("UDP message shorter than 8 bytes. ignored.");
		return NM_READ_FAILED;
	}
	if (!this->parseHeader(data))
		return NM_READ_FAILED;
	if (len != _bufferSize + HEADER_LEN) {
		qDebug("UDP packet has wrong size. Is %d, expected %d", int(_bufferSize), len - HEADER_LEN);
		return NM_READ_FAILED;
	}
	return this->parseMessage(data + HEADER_LEN) ? NM_READ_OK : NM_READ_FAILED;
}

bool NetworkMessage::parseHeader(char *header)
{
	if (header[0] != 'P' || header[1] != 'V' || header[2] != 'S' || header[3] != '2') {
		qDebug("Protocol magic wrong.");
		return false;
	}
	_bufferPos = 0;
	_bufferSize = _ntohl(header + 4);
	if (_bufferSize > MAX_MSG_LEN) {
		qDebug("Disconnecting Client: MAX_MSG_LEN exceeded.");
		return false;
	}
	if (_bufferSize < 4) { // TODO: magic number. msg needs to be at least 4 bytes for 1 key/value pair of length 0 each.
		qDebug("A Client sent an empty message.");
		return false;
	}
	return true;
}

bool NetworkMessage::parseMessage(char *buffer)
{
	char *ptr = buffer;
	while (_bufferSize - (ptr - buffer) >= 4) {
		const quint16 keyLen = _ntohs(ptr);
		ptr += 2;
		const quint16 valLen = _ntohs(ptr);
		ptr += 2;
		if (_bufferSize - (ptr - buffer) < keyLen + valLen) {
			qDebug() << "Warning: Error parsing message. key(" << keyLen << ")+value(" << valLen
			         << ") length > total remaining bytes (" << (_bufferSize - (ptr - buffer)) << ")";
			return false;
		}
		_fields.insert(QByteArray(ptr, keyLen), QByteArray(ptr + keyLen, valLen));
		//qDebug() << "Got " << QString::fromUtf8(ptr, keyLen) << " -> " << QString::fromUtf8(ptr + keyLen, valLen);
		ptr += keyLen + valLen;
	}
	_mode = 3;
	return true;
}

bool NetworkMessage::writeMessage(QAbstractSocket * const socket)
{
	if (_mode != 2) {
		if (_mode == 1) {
			qDebug("NetworkMessage::writeMessage called when class was in mode %d!", _mode);
			return false;
		}
		_mode = 1;
		_bufferPos = 0;
	}
	// key/value pairs have not been serialized yet...
	if (_bufferSize == 0) {
		this->serializeMessage();
	}
	const qint64 ret = socket->write(_buffer + _bufferPos, _bufferSize - _bufferPos);
	if (ret == 0)
		return true;
	if (ret < 0 || ret > (_bufferSize - _bufferPos))
		return false;
	_bufferPos += quint32(ret);
	if (_bufferPos == _bufferSize) {
		_bufferPos = 0;
		_mode = 4;
	}
	return true;
}

bool NetworkMessage::writeMessage(QUdpSocket* socket, const QHostAddress& address, quint16 port)
{
	if (_mode != 4) {
		if (_mode == 1) {
			qDebug("NetworkMessage::writeMessage called when class was in mode %d!", _mode);
			return false;
		}
		_mode = 4;
		_bufferPos = 0;
	}
	// key/value pairs have not been serialized yet...
	if (_bufferSize == 0) {
		this->serializeMessage();
	}
	const qint64 ret = socket->writeDatagram(_buffer, _bufferSize, address, port);
	if (ret != _bufferSize)
		return false;
	if (ret < 0)
		return false;
	_bufferPos = 0;
	return true;
}

void NetworkMessage::serializeMessage()
{
	QByteArray buf;
	//qDebug() << "Default size: " << buf.capacity();
	buf.reserve(_lastBufferSize > 0 ? int(_lastBufferSize) : 200);
	for (QHash<QByteArray, QByteArray>::const_iterator it = _fields.begin(); it != _fields.end(); ++it) {
		const QByteArray &ba = it.key();
		const QByteArray &val = it.value();
		quint16 keyLen = _htons(quint16(ba.size()));
		quint16 valLen = _htons(quint16(val.size()));
		//qDebug() << "Adding to msg(" << ba.size() << "/" << val.size() << "): " << ba << " -> " << val;
		buf.append(reinterpret_cast<const char*>(&keyLen), 2);
		buf.append(reinterpret_cast<const char*>(&valLen), 2);
		buf.append(ba);
		buf.append(val);
	}
	_bufferSize = quint32(buf.length() + HEADER_LEN);
	allocBuffer();
	memcpy(_buffer + HEADER_LEN, buf.data(), size_t(buf.size()));
	_buffer[0] = 'P';
	_buffer[1] = 'V';
	_buffer[2] = 'S';
	_buffer[3] = '2';
	const quint32 flipped = _htonl(_bufferSize - HEADER_LEN);
	memcpy(_buffer + 4, &flipped, 4);
}

void NetworkMessage::buildErrorMessage(const QString& error)
{
	this->reset();
	this->setField(_ID, _ERROR);
	this->setField(_ERROR, error);
}