File: netqueue.cpp

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (370 lines) | stat: -rw-r--r-- 12,086 bytes parent folder | download
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
	This file is part of Warzone 2100.
	Copyright (C) 2010-2020  Warzone 2100 Project

	Warzone 2100 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 2 of the License, or
	(at your option) any later version.

	Warzone 2100 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 Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
 * @file netqueue.cpp
 *
 * Basic netqueue.
 */
#include "lib/framework/frame.h"
#include "netqueue.h"
#include "netplay.h"

#include <limits>
#include <cstdint>
#include <cstring>

// See comments in netqueue.h.


// Byte n is the final byte, iff it is less than 256-a[n].

// Some possible values of table_uint32_t_a[n], such that the maximum encodable value is 0xFFFFFFFF, satisfying the assertion below:
// ASSERT(0xFFFFFFFF == 255*(1 + a[0]*(1 + a[1]*(1 + a[2]*(1 + a[3]*(1 + a[4]))))), "Maximum encodable value not 0xFFFFFFFF.");

//static const unsigned table_uint32_t_a[5] = {14, 127, 74, 127, 0}, table_uint32_t_m[5] = {1, 14, 1778, 131572, 16709644};  // <242: 1 byte, <2048: 2 bytes, <325644: 3 bytes, <17298432: 4 bytes, <4294967296: 5 bytes
constexpr unsigned table_uint32_t_a[5] = {78, 95, 32, 70, 0}, table_uint32_t_m[5] = {1, 78, 7410, 237120, 16598400};  // <178: 1 byte, <12736: 2 bytes, <1672576: 3 bytes, <45776896: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {78, 95, 71, 31, 0}, table_uint32_t_m[5] = {1, 78, 7410, 526110, 16309410};  // <178: 1 byte, <12736: 2 bytes, <1383586: 3 bytes, <119758336: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 19, 119, 0}, table_uint32_t_m[5] = {1, 104, 7384, 140296, 16695224};  // <152: 1 byte, <19392: 2 bytes, <1769400: 3 bytes, <20989952: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 20, 113, 0}, table_uint32_t_m[5] = {1, 104, 7384, 147680, 16687840};  // <152: 1 byte, <19392: 2 bytes, <1762016: 3 bytes, <22880256: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 24, 94, 0}, table_uint32_t_m[5] = {1, 104, 7384, 177216, 16658304};  // <152: 1 byte, <19392: 2 bytes, <1732480: 3 bytes, <30441472: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 30, 75, 0}, table_uint32_t_m[5] = {1, 104, 7384, 221520, 16614000};  // <152: 1 byte, <19392: 2 bytes, <1688176: 3 bytes, <41783296: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 38, 59, 0}, table_uint32_t_m[5] = {1, 104, 7384, 280592, 16554928};  // <152: 1 byte, <19392: 2 bytes, <1629104: 3 bytes, <56905728: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 40, 56, 0}, table_uint32_t_m[5] = {1, 104, 7384, 295360, 16540160};  // <152: 1 byte, <19392: 2 bytes, <1614336: 3 bytes, <60686336: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 57, 39, 0}, table_uint32_t_m[5] = {1, 104, 7384, 420888, 16414632};  // <152: 1 byte, <19392: 2 bytes, <1488808: 3 bytes, <92821504: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 60, 37, 0}, table_uint32_t_m[5] = {1, 104, 7384, 443040, 16392480};  // <152: 1 byte, <19392: 2 bytes, <1466656: 3 bytes, <98492416: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 76, 29, 0}, table_uint32_t_m[5] = {1, 104, 7384, 561184, 16274336};  // <152: 1 byte, <19392: 2 bytes, <1348512: 3 bytes, <128737280: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 95, 23, 0}, table_uint32_t_m[5] = {1, 104, 7384, 701480, 16134040};  // <152: 1 byte, <19392: 2 bytes, <1208216: 3 bytes, <164653056: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 114, 19, 0}, table_uint32_t_m[5] = {1, 104, 7384, 841776, 15993744};  // <152: 1 byte, <19392: 2 bytes, <1067920: 3 bytes, <200568832: 4 bytes, <4294967296: 5 bytes
//static const unsigned table_uint32_t_a[5] = {104, 71, 120, 18, 0}, table_uint32_t_m[5] = {1, 104, 7384, 886080, 15949440};  // <152: 1 byte, <19392: 2 bytes, <1023616: 3 bytes, <211910656: 4 bytes, <4294967296: 5 bytes

//static const unsigned table_uint32_t_m[5] = {1, a[0], a[0]*a[1], a[0]*a[1]*a[2], a[0]*a[1]*a[2]*a[3]};

static_assert(0xFFFFFFFF == 255*(1 + table_uint32_t_a[0]*(1 + table_uint32_t_a[1]*(1 + table_uint32_t_a[2]*(1 + table_uint32_t_a[3]*(1 + table_uint32_t_a[4]))))), "Maximum encodable value not 0xFFFFFFFF.");

unsigned encodedlength_uint32_t(uint32_t v)
{
	for (unsigned n = 0;; ++n)
	{
		const unsigned a = table_uint32_t_a[n];
		const unsigned m = table_uint32_t_m[n];
		const unsigned possibleValues = (256 - a) * m; // Number of values which encode to n + 1 bytes.
		if (v < possibleValues)
		{
			return n + 1;
		}
		v -= possibleValues;
	}
}

bool encode_uint32_t(uint8_t &b, uint32_t &v, unsigned n)
{
	const unsigned a = table_uint32_t_a[n];

	bool isLastByte = v < 256 - a;
	if (isLastByte)
	{
		b = v;
	}
	else
	{
		v -= 256 - a;
		b = 255 - v % a;
		v /= a;
	}
	return !isLastByte;
}

bool decode_uint32_t(uint8_t b, uint32_t &v, unsigned n)
{
	const unsigned a = table_uint32_t_a[n];
	const unsigned m = table_uint32_t_m[n];

	bool isLastByte = b < 256 - a;
	if (isLastByte)
	{
		v += b * m;
	}
	else
	{
		v += (256 - a + 255 - b) * m;
	}
	return !isLastByte;
}

NetMessage::NetMessage(NetMsgDataVector&& data)
	: data_(std::move(data))
{}

uint8_t NetMessage::type() const
{
	ASSERT_OR_RETURN(0, !data_.empty(), "Invalid message data");
	return data_[0];
}

const NetMsgDataVector& NetMessage::rawData() const
{
	return data_;
}

const uint8_t* NetMessage::payload() const
{
	ASSERT_OR_RETURN(nullptr, !data_.empty() && data_.size() >= HEADER_LENGTH, "Invalid message data");
	return &data_[HEADER_LENGTH];
}

size_t NetMessage::payloadSize() const
{
	ASSERT_OR_RETURN(0, data_.size() >= HEADER_LENGTH, "Invalid message data");
	return data_.size() - HEADER_LENGTH;
}

optional<NetMessage> NetMessage::tryFromRawData(const uint8_t* buffer, size_t bufferLen)
{
	if (bufferLen < NetMessage::HEADER_LENGTH) { return nullopt; }

	uint8_t type = buffer[0];
	uint16_t len = 0;
	// Load payload length from uint16_t (network byte order) starting at the second byte of the data buffer.
	wz_ntohs_load_unaligned(len, &buffer[1]);

	if (bufferLen - HEADER_LENGTH < len)
	{
		return nullopt;  // Don't have a whole message ready yet.
	}

	NetMessageBuilder msg(type, len);
	msg.append(&buffer[HEADER_LENGTH], len);
	return optional<NetMessage>{msg.build()};
}

void NetMessage::rawDataAppendToVector(std::vector<uint8_t>& output) const
{
	const size_t oldLen = output.size();
	output.resize(output.size() + data_.size());
	std::memcpy(&output[oldLen], data_.data(), data_.size());
}

NetMessageBuilder::NetMessageBuilder(uint8_t type, size_t reservedCapacity /* = 16 */)
	: data_(MsgDataAllocator(defaultMemoryPool()))
{
	data_.reserve(reservedCapacity + NetMessage::HEADER_LENGTH);
	data_.resize(NetMessage::HEADER_LENGTH);
	data_[0] = type;
}

NetMessageBuilder::NetMessageBuilder(NetMsgDataVector&& rawData)
	: data_(std::move(rawData))
{}

NetQueue::NetQueue()
	: canGetMessagesForNet(true)
	, canGetMessages(true)
	, messages(MsgAllocator(defaultMemoryPool()))
	, pendingGameTimeUpdateMessages(0)
	, bCurrentMessageWasDecrypted(false)
{
	dataPos = messages.end();
	messagePos = messages.end();
}

void NetQueue::writeRawData(const uint8_t *netData, size_t netLen)
{
	size_t used = 0;
	std::vector<uint8_t> &buffer = incompleteReceivedMessageData;  // Short alias.

	// Insert the data.
	buffer.insert(buffer.end(), netData, netData + netLen);

	// Extract the messages.
	while (buffer.size() - used >= NetMessage::HEADER_LENGTH)
	{
		uint8_t type = buffer[used];
		uint16_t len = 0;
		// Load payload length from uint16_t (network byte order) starting at the second byte of the data buffer.
		wz_ntohs_load_unaligned(len, &buffer[used + 1]);

		if (buffer.size() - used - NetMessage::HEADER_LENGTH < len)
		{
			break;  // Don't have a whole message ready yet.
		}

		NetMessageBuilder msg(type, len);
		msg.append(buffer.data() + used + NetMessage::HEADER_LENGTH, len);
		messages.emplace_front(msg.build());

		if (type == GAME_GAME_TIME)
		{
			++pendingGameTimeUpdateMessages;
		}
		used += NetMessage::HEADER_LENGTH + len;
	}

	// Recycle old data.
	buffer.erase(buffer.begin(), buffer.begin() + used);
}

size_t NetQueue::currentIncompleteDataBuffered() const
{
	return incompleteReceivedMessageData.size();
}

void NetQueue::setWillNeverGetMessagesForNet()
{
	canGetMessagesForNet = false;
}

unsigned NetQueue::numMessagesForNet() const
{
	unsigned count = 0;
	if (canGetMessagesForNet)
	{
		for (List::iterator i = dataPos; i != messages.begin(); --i)
		{
			++count;
		}
	}

	return count;
}

const NetMessage &NetQueue::getMessageForNet() const
{
	ASSERT(canGetMessagesForNet, "Wrong NetQueue type for getMessageForNet.");
	ASSERT(dataPos != messages.begin(), "No message to get!");

	// Return the message.
	return internal_getMessageForNet();
}

void NetQueue::popMessageForNet()
{
	ASSERT(canGetMessagesForNet, "Wrong NetQueue type for popMessageForNet.");
	ASSERT(dataPos != messages.begin(), "No message to pop!");

	if (messagePos != messages.begin() && internal_getMessageForNet().type() == GAME_GAME_TIME)
	{
		if (pendingGameTimeUpdateMessages > 0)
		{
			--pendingGameTimeUpdateMessages;
		}
	}

	// Pop the message.
	--dataPos;

	// Recycle old data.
	popOldMessages();
}

void NetQueue::pushMessage(NetMessage&& message)
{
	if (message.type() == GAME_GAME_TIME)
	{
		++pendingGameTimeUpdateMessages;
	}
	messages.emplace_front(std::move(message));
}

void NetQueue::setWillNeverGetMessages()
{
	canGetMessages = false;
}

bool NetQueue::haveMessage() const
{
	ASSERT(canGetMessages, "Wrong NetQueue type for haveMessage.");
	return messagePos != messages.begin();
}

const NetMessage &NetQueue::getMessage() const
{
	ASSERT(canGetMessages, "Wrong NetQueue type for getMessage.");
	ASSERT(messagePos != messages.begin(), "No message to get!");

	// Return the message.
	return internal_getConstMessage();
}

bool NetQueue::currentMessageWasDecrypted() const
{
	return bCurrentMessageWasDecrypted;
}

bool NetQueue::replaceCurrentWithDecrypted(NetMessage &&decryptedMessage)
{
	ASSERT_OR_RETURN(false, canGetMessages, "Wrong NetQueue type for getMessage.");
	ASSERT_OR_RETURN(false, messagePos != messages.begin(), "No message to get!");

	NetMessage& currentMessage = internal_getMessage();
	ASSERT_OR_RETURN(false, currentMessage.type() == NET_SECURED_NET_MESSAGE, "Current message is not a secured message!");

	currentMessage = std::move(decryptedMessage);
	bCurrentMessageWasDecrypted = true;
	return true;
}

void NetQueue::popMessage()
{
	ASSERT(canGetMessages, "Wrong NetQueue type for popMessage.");
	ASSERT(messagePos != messages.begin(), "No message to pop!");

	if (messagePos != messages.begin() && internal_getConstMessage().type() == GAME_GAME_TIME)
	{
		if (pendingGameTimeUpdateMessages > 0)
		{
			--pendingGameTimeUpdateMessages;
		}
	}

	// Pop the message.
	--messagePos;
	bCurrentMessageWasDecrypted = false;

	// Recycle old data.
	popOldMessages();
}

void NetQueue::popOldMessages()
{
	if (!canGetMessagesForNet)
	{
		dataPos = messages.begin();
	}
	if (!canGetMessages)
	{
		messagePos = messages.begin();
	}

	List::iterator i = messages.end();
	while (i != dataPos && i != messagePos)
	{
		--i;
	}

	if (i == dataPos)
	{
		dataPos = messages.end();  // Old iterator will become invalid.
	}
	if (i == messagePos)
	{
		messagePos = messages.end();  // Old iterator will become invalid.
	}

	messages.erase(i, messages.end());
}