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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "UDPConnection.h"
#include <memory>
#include <cinttypes>
#include "Socket.h"
#include "ProtocolDef.h"
#include "Exception.h"
#include "Net/Protocol/BaseNetProtocol.h"
#include "System/Config/ConfigHandler.h"
#include "System/CRC.h"
#include "System/GlobalConfig.h"
#include "System/Log/ILog.h"
#include "System/SpringFormat.h"
#include "System/SafeUtil.h"
#ifndef UNIT_TEST
CONFIG(bool, UDPConnectionLogDebugMessages).defaultValue(false);
#endif
namespace netcode {
using namespace asio;
static const unsigned udpMaxPacketSize = 4096;
static const int maxChunkSize = 254;
static const int chunksPerSec = 30;
#if NETWORK_TEST
static CGlobalUnsyncedRNG rng;
float RANDOM_NUMBER() { return (rng.NextFloat()); }
bool EMULATE_PACKET_LOSS(int& lossCtr) {
if (RANDOM_NUMBER() < (PACKET_LOSS_FACTOR / 100.0f))
return true;
const bool loss = RANDOM_NUMBER() < (SEVERE_PACKET_LOSS_FACTOR / 100.0f);
if (loss && lossCtr == 0)
lossCtr = SEVERE_PACKET_LOSS_MAX_COUNT * RANDOM_NUMBER();
return (lossCtr > 0 && lossCtr--);
}
void EMULATE_PACKET_CORRUPTION(uint8_t& crc) {
if ((RANDOM_NUMBER() < (PACKET_CORRUPTION_FACTOR / 100.0f)))
crc = (uint8_t)rng.NextInt();
}
#define LOSS_COUNTER lossCounter
#else
static int dummyLossCounter = 0;
inline bool EMULATE_PACKET_LOSS(int& lossCtr) { return false; }
inline void EMULATE_PACKET_CORRUPTION(std::uint8_t& crc) {}
#define LOSS_COUNTER dummyLossCounter
#endif
#if NETWORK_TEST && PACKET_MAX_LATENCY > 0 && PACKET_MAX_LATENCY >= PACKET_MIN_LATENCY
#define EMULATE_LATENCY(cond) \
for (auto di = delayed.begin(); di != delayed.end(); ) { \
spring_time curtime = spring_gettime(); \
if (curtime > di->first && (curtime - di->first) > spring_msecs(0)) { \
mySocket->send_to(buffer(di->second), addr, flags, err); \
di = delayed.erase(di); \
} else { ++di; } \
} \
if (cond) \
delayed[spring_gettime() + spring_msecs(PACKET_MIN_LATENCY + (PACKET_MAX_LATENCY - PACKET_MIN_LATENCY) * RANDOM_NUMBER())] = data; \
if (false)
#else
#define EMULATE_LATENCY(cond) if(cond)
#endif
class Unpacker
{
public:
Unpacker(const unsigned char* data, unsigned length)
: data(data)
, length(length)
, pos(0)
{
}
template<typename T>
void Unpack(T& t) {
assert(length >= pos + sizeof(t));
t = *reinterpret_cast<const T*>(data + pos);
pos += sizeof(t);
}
void Unpack(std::vector<std::uint8_t>& t, unsigned unpackLength) {
std::copy(data + pos, data + pos + unpackLength, std::back_inserter(t));
pos += unpackLength;
}
unsigned Remaining() const {
return length - std::min(pos, length);
}
private:
const unsigned char* data;
unsigned length;
unsigned pos;
};
class Packer
{
public:
Packer(std::vector<std::uint8_t>& data): data(data)
{
assert(data.empty());
}
template<typename T>
void Pack(T& t) {
const size_t pos = data.size();
data.resize(pos + sizeof(T));
*reinterpret_cast<T*>(&data[pos]) = t;
}
void Pack(std::vector<std::uint8_t>& _data) {
std::copy(_data.begin(), _data.end(), std::back_inserter(data));
}
private:
std::vector<std::uint8_t>& data;
};
void Chunk::UpdateChecksum(CRC& crc) const {
crc << chunkNumber;
crc << (unsigned int)chunkSize;
if (!data.empty()) {
crc.Update(&data[0], data.size());
}
}
Packet::Packet(const unsigned char* data, unsigned length)
{
Unpacker buf(data, length);
buf.Unpack(lastContinuous);
buf.Unpack(nakType);
buf.Unpack(checksum);
if (nakType > 0) {
naks.reserve(nakType);
for (int i = 0; i != nakType; ++i) {
if (buf.Remaining() >= sizeof(naks[i])) {
if (naks.size() <= i) {
naks.push_back(0);
}
buf.Unpack(naks[i]);
} else {
break;
}
}
}
while (buf.Remaining() > Chunk::headerSize) {
ChunkPtr temp(new Chunk);
buf.Unpack(temp->chunkNumber);
buf.Unpack(temp->chunkSize);
if (buf.Remaining() >= temp->chunkSize) {
buf.Unpack(temp->data, temp->chunkSize);
chunks.push_back(temp);
} else {
// defective, ignore
break;
}
}
}
Packet::Packet(int _lastContinuous, int _nak)
: lastContinuous(_lastContinuous)
, nakType(_nak)
{
}
unsigned Packet::GetSize() const {
unsigned size = headerSize + naks.size();
for (auto chk = chunks.begin(); chk != chunks.end(); ++chk)
size += (*chk)->GetSize();
return size;
}
std::uint8_t Packet::GetChecksum() const {
CRC crc;
crc << lastContinuous;
crc << (unsigned int)nakType;
if (!naks.empty())
crc.Update(&naks[0], naks.size());
for (auto chk = chunks.begin(); chk != chunks.end(); ++chk)
(*chk)->UpdateChecksum(crc);
return (std::uint8_t)crc.GetDigest();
}
void Packet::Serialize(std::vector<std::uint8_t>& data)
{
data.reserve(GetSize());
Packer buf(data);
buf.Pack(lastContinuous);
buf.Pack(nakType);
buf.Pack(checksum);
buf.Pack(naks);
for (auto ci = chunks.begin(); ci != chunks.end(); ++ci) {
buf.Pack((*ci)->chunkNumber);
buf.Pack((*ci)->chunkSize);
buf.Pack((*ci)->data);
}
}
UDPConnection::UDPConnection(std::shared_ptr<ip::udp::socket> netSocket, const ip::udp::endpoint& myAddr)
: addr(myAddr)
, sharedSocket(true)
, mySocket(netSocket)
{
Init();
}
UDPConnection::UDPConnection(int sourcePort, const std::string& address, const unsigned port)
: sharedSocket(false)
{
asio::error_code err;
addr = ResolveAddr(address, port, &err);
ip::address sourceAddr = GetAnyAddress(addr.address().is_v6());
std::shared_ptr<ip::udp::socket> tempSocket(new ip::udp::socket(
netcode::netservice, ip::udp::endpoint(sourceAddr, sourcePort)));
mySocket = tempSocket;
Init();
}
UDPConnection::UDPConnection(CConnection& conn)
: sharedSocket(true)
{
ReconnectTo(conn);
Init();
}
void UDPConnection::Init()
{
// make sure protocoldef is initialized
CBaseNetProtocol::Get();
lastNakTime = spring_gettime();
lastUnackResentTime = spring_gettime();
lastPacketSendTime = spring_gettime();
lastPacketRecvTime = spring_gettime();
lastChunkCreatedTime = spring_gettime();
#ifdef ENABLE_DEBUG_STATS
lastDebugMessageTime = spring_gettime();
lastFramePacketRecvTime = spring_gettime();
#endif
lastInOrder = -1;
waitingPackets.clear();
#ifdef ENABLE_DEBUG_STATS
sumDeltaFramePacketRecvTime = 0.0f;
minDeltaFramePacketRecvTime = 0.0f;
maxDeltaFramePacketRecvTime = 0.0f;
numReceivedFramePackets = 0;
numEnqueuedFramePackets = 0;
numEmptyGetDataCalls = 0;
numTotalGetDataCalls = 0;
#endif
currentPacketChunkNum = 0;
lastNak = -1;
sentOverhead = 0;
recvOverhead = 0;
fragmentBuffer = 0;
resentChunks = 0;
sentPackets = recvPackets = 0;
droppedChunks = 0;
mtu = globalConfig->mtu;
reconnectTime = globalConfig->reconnectTimeout;
muted = true;
closed = false;
resend = false;
#ifndef UNIT_TEST
logMessages = configHandler->GetBool("UDPConnectionLogDebugMessages");
#endif
netLossFactor = globalConfig->networkLossFactor;
lastMidChunk = -1;
#if NETWORK_TEST
lossCounter = 0;
#endif
}
void UDPConnection::ReconnectTo(CConnection& conn) {
dynamic_cast<UDPConnection &>(conn).CopyConnection(*this);
}
void UDPConnection::CopyConnection(UDPConnection &conn) {
conn.InitConnection(addr, mySocket);
}
void UDPConnection::InitConnection(ip::udp::endpoint address, std::shared_ptr<ip::udp::socket> socket) {
addr = address;
mySocket = socket;
}
UDPConnection::~UDPConnection()
{
delete fragmentBuffer;
for (auto &it: waitingPackets)
delete it.second;
fragmentBuffer = NULL;
Flush(true);
}
void UDPConnection::SendData(std::shared_ptr<const RawPacket> data)
{
assert(data->length > 0);
outgoingData.push_back(data);
}
std::shared_ptr<const RawPacket> UDPConnection::Peek(unsigned ahead) const
{
if (ahead < msgQueue.size())
return msgQueue[ahead];
std::shared_ptr<const RawPacket> empty;
return empty;
}
#ifdef ENABLE_DEBUG_STATS
std::shared_ptr<const RawPacket> UDPConnection::GetData()
{
numTotalGetDataCalls++;
if (!msgQueue.empty()) {
std::shared_ptr<const RawPacket> msg = msgQueue.front();
msgQueue.pop_front();
numEnqueuedFramePackets -= (msg->data[0] == NETMSG_NEWFRAME);
numEnqueuedFramePackets -= (msg->data[0] == NETMSG_KEYFRAME);
return msg;
}
numEmptyGetDataCalls++;
std::shared_ptr<const RawPacket> empty;
return empty;
}
#else
std::shared_ptr<const RawPacket> UDPConnection::GetData()
{
if (!msgQueue.empty()) {
std::shared_ptr<const RawPacket> msg = msgQueue.front();
msgQueue.pop_front();
return msg;
}
std::shared_ptr<const RawPacket> empty;
return empty;
}
#endif
void UDPConnection::DeleteBufferPacketAt(unsigned index)
{
if (index < msgQueue.size()) {
msgQueue.erase(msgQueue.begin() + index);
}
}
void UDPConnection::Update()
{
spring_time curTime = spring_gettime();
outgoing.UpdateTime(spring_tomsecs(curTime));
#ifdef ENABLE_DEBUG_STATS
{
const float debugMssgDeltaTime = (curTime - lastDebugMessageTime).toMilliSecsf();
const float avgFramePacketRate = numReceivedFramePackets / debugMssgDeltaTime;
if (debugMssgDeltaTime >= 1000.0f) {
if (logMessages) {
LOG_L(L_INFO,
"[UDPConnection::%s] %u NETMSG_*FRAME packets received (%fms : %fp/ms) during (empty=%u total=%u) GetData calls",
__FUNCTION__, numReceivedFramePackets, debugMssgDeltaTime, avgFramePacketRate, numEmptyGetDataCalls, numTotalGetDataCalls
);
}
lastDebugMessageTime = curTime;
sumDeltaFramePacketRecvTime = 0.0f;
minDeltaFramePacketRecvTime = 1e6f;
maxDeltaFramePacketRecvTime = 0.0f;
numReceivedFramePackets = 0;
// numEnqueuedFramePackets = 0;
numEmptyGetDataCalls = 0;
numTotalGetDataCalls = 0;
}
}
#endif
if (!sharedSocket && !closed) {
// duplicated code with UDPListener
netservice.poll();
size_t bytesAvail = 0;
while ((bytesAvail = mySocket->available()) > 0) {
std::vector<std::uint8_t> buffer(bytesAvail, 0);
ip::udp::endpoint sender_endpoint;
ip::udp::socket::message_flags flags = 0;
asio::error_code err;
const size_t bytesReceived = mySocket->receive_from(asio::buffer(buffer), sender_endpoint, flags, err);
if (CheckErrorCode(err))
break;
if (bytesReceived < Packet::headerSize)
continue;
Packet data(&buffer[0], bytesReceived);
if (IsUsingAddress(sender_endpoint))
ProcessRawPacket(data);
// not likely, but make sure we do not get stuck here
if ((spring_gettime() - curTime) > spring_msecs(10)) {
break;
}
}
}
Flush(false);
}
void UDPConnection::ProcessRawPacket(Packet& incoming)
{
#ifdef ENABLE_DEBUG_STATS
if (logMessages) {
LOG_L(L_INFO, "\t[%s] checksum=(%u : %u) mtu=%u", __FUNCTION__, incoming.GetChecksum(), incoming.checksum, mtu);
}
#endif
lastPacketRecvTime = spring_gettime();
dataRecv += incoming.GetSize();
recvOverhead += Packet::headerSize;
++recvPackets;
// if (EMULATE_PACKET_LOSS(lossCounter))
// return;
if (incoming.GetChecksum() != incoming.checksum) {
LOG_L(L_ERROR, "Discarding incoming corrupted packet: CRC %d, LEN %d", incoming.checksum, incoming.GetSize());
return;
}
if (incoming.lastContinuous < 0 && lastInOrder >= 0 &&
(unackedChunks.empty() || unackedChunks[0]->chunkNumber > 0)) {
LOG_L(L_WARNING, "Discarding superfluous reconnection attempt");
return;
}
AckChunks(incoming.lastContinuous);
if (!unackedChunks.empty()) {
const int nextCont = incoming.lastContinuous + 1;
const int unAckDiff = unackedChunks[0]->chunkNumber - nextCont;
if (-256 <= unAckDiff && unAckDiff <= 256) {
if (incoming.nakType < 0) {
for (int i = 0; i != -incoming.nakType; ++i) {
const int unAckPos = i + unAckDiff;
if (unAckPos >= 0 && unAckPos < unackedChunks.size()) {
assert(unackedChunks[unAckPos]->chunkNumber == nextCont + i);
RequestResend(unackedChunks[unAckPos]);
}
}
} else if (incoming.nakType > 0) {
int unAckPos = 0;
for (int i = 0; i != incoming.naks.size(); ++i) {
if (unAckDiff + incoming.naks[i] < 0)
continue;
while (unAckPos < unAckDiff + incoming.naks[i]) {
// if there are gaps in the array, assume that further resends are not needed
if (unAckPos < unackedChunks.size())
resendRequested.erase(unackedChunks[unAckPos]->chunkNumber);
++unAckPos;
}
if (unAckPos < unackedChunks.size()) {
assert(unackedChunks[unAckPos]->chunkNumber == nextCont + incoming.naks[i]);
RequestResend(unackedChunks[unAckPos]);
}
++unAckPos;
}
}
}
}
for (auto ci = incoming.chunks.begin(); ci != incoming.chunks.end(); ++ci) {
const std::shared_ptr<netcode::Chunk>& c = *ci;
if ((lastInOrder >= c->chunkNumber) || (waitingPackets.find(c->chunkNumber) != waitingPackets.end())) {
++droppedChunks;
continue;
}
waitingPackets.emplace(c->chunkNumber, new RawPacket(&c->data[0], c->data.size()));
}
packetMap::iterator wpi;
// process all in order packets that we have waiting
while ((wpi = waitingPackets.find(lastInOrder + 1)) != waitingPackets.end()) {
std::vector<std::uint8_t> buf;
if (fragmentBuffer != NULL) {
buf.resize(fragmentBuffer->length);
// combine with fragment buffer (packet reassembly)
std::copy(fragmentBuffer->data, fragmentBuffer->data + fragmentBuffer->length, buf.begin());
delete fragmentBuffer;
fragmentBuffer = NULL;
}
lastInOrder++;
std::copy(wpi->second->data, wpi->second->data + wpi->second->length, std::back_inserter(buf));
waitingPackets.erase(wpi);
for (unsigned pos = 0; pos < buf.size(); ) {
const unsigned char* bufp = &buf[pos];
const unsigned msglength = buf.size() - pos;
const int pktlength = ProtocolDef::GetInstance()->PacketLength(bufp, msglength);
// this returns false for zero/invalid pktlength
if (ProtocolDef::GetInstance()->IsValidLength(pktlength, msglength)) {
msgQueue.push_back(std::shared_ptr<const RawPacket>(new RawPacket(bufp, pktlength)));
#ifdef ENABLE_DEBUG_STATS
// server sends both of these, clients send only keyframe messages
// TODO: would be easy to feed this data into a Q3A-style lagometer
//
if ((msgQueue.back())->data[0] == NETMSG_NEWFRAME || (msgQueue.back())->data[0] == NETMSG_KEYFRAME) {
const spring_time dt = spring_gettime() - lastFramePacketRecvTime;
sumDeltaFramePacketRecvTime += dt.toMilliSecsf();
minDeltaFramePacketRecvTime = std::min(dt.toMilliSecsf(), minDeltaFramePacketRecvTime);
maxDeltaFramePacketRecvTime = std::max(dt.toMilliSecsf(), maxDeltaFramePacketRecvTime);
numReceivedFramePackets += 1;
numEnqueuedFramePackets += 1;
lastFramePacketRecvTime = spring_gettime();
if (logMessages) {
LOG_L(L_INFO,
"\t[%s] (received=%u enqueued=%u) packets (dt=%fms mindt=%fms maxdt=%fms sumdt=%fms)",
__FUNCTION__, numReceivedFramePackets, numEnqueuedFramePackets, dt.toMilliSecsf(),
minDeltaFramePacketRecvTime, maxDeltaFramePacketRecvTime, sumDeltaFramePacketRecvTime
);
}
}
#endif
pos += pktlength;
} else {
if (pktlength >= 0) {
// partial packet in buffer
fragmentBuffer = new RawPacket(bufp, msglength);
break;
}
LOG_L(L_ERROR, "Discarding incoming invalid packet: ID %d, LEN %d", (int)*bufp, pktlength);
// if the packet is invalid, skip a single byte
// until we encounter a good packet
++pos;
}
}
}
}
void UDPConnection::Flush(const bool forced)
{
if (muted)
return;
const spring_time curTime = spring_gettime();
// do not create chunks more than chunksPerSec times per second
const bool waitMore = (lastChunkCreatedTime >= (curTime - spring_msecs(1000 / chunksPerSec)));
// if the packet is tiny, reduce the send frequency further
const int requiredLength = ((200 >> netLossFactor) - spring_tomsecs(curTime - lastChunkCreatedTime)) / 10;
int outgoingLength = 0;
if (!waitMore) {
for (auto pi = outgoingData.begin(); (pi != outgoingData.end()) && (outgoingLength <= requiredLength); ++pi) {
outgoingLength += (*pi)->length;
}
}
if (forced || (!waitMore && outgoingLength > requiredLength)) {
std::uint8_t buffer[udpMaxPacketSize];
unsigned pos = 0;
// Manually fragment packets to respect configured UDP_MTU.
// This is an attempt to fix the bug where players drop out of the game if
// someone in the game gives a large order.
bool partialPacket = false;
bool sendMore = true;
do {
sendMore = (outgoing.GetAverage(true) <= globalConfig->linkOutgoingBandwidth);
sendMore |= ((globalConfig->linkOutgoingBandwidth <= 0) || partialPacket || forced);
if (!outgoingData.empty() && sendMore) {
std::shared_ptr<const RawPacket>& packet = *(outgoingData.begin());
if (!partialPacket && !ProtocolDef::GetInstance()->IsValidPacket(packet->data, packet->length)) {
LOG_L(L_ERROR,
"Discarding outgoing invalid packet: ID %d, LEN %d",
((packet->length > 0) ? (int)packet->data[0] : -1),
packet->length);
outgoingData.pop_front();
} else {
const unsigned numBytes = std::min((unsigned)maxChunkSize - pos, packet->length);
assert(packet->length > 0);
memcpy(buffer + pos, packet->data, numBytes);
pos += numBytes;
outgoing.DataSent(numBytes, true);
partialPacket = (numBytes != packet->length);
if (partialPacket) {
// partially transfered
packet.reset(new RawPacket(packet->data + numBytes, packet->length - numBytes));
} else {
// full packet copied
outgoingData.pop_front();
}
}
}
if ((pos > 0) && (outgoingData.empty() || (pos == maxChunkSize) || !sendMore)) {
CreateChunk(buffer, pos, currentPacketChunkNum++);
pos = 0;
}
} while (!outgoingData.empty() && sendMore);
}
SendIfNecessary(forced);
}
bool UDPConnection::CheckTimeout(int seconds, bool initial) const {
int timeout;
if (seconds == 0) {
timeout = (dataRecv && !initial)
? globalConfig->networkTimeout
: globalConfig->initialNetworkTimeout;
} else if (seconds > 0) {
timeout = seconds;
} else {
timeout = globalConfig->reconnectTimeout;
}
return (timeout > 0 && (spring_gettime() - lastPacketRecvTime) > spring_secs(timeout));
}
bool UDPConnection::NeedsReconnect() {
if (CanReconnect()) {
if (!CheckTimeout(-1)) {
reconnectTime = globalConfig->reconnectTimeout;
} else if (CheckTimeout(reconnectTime)) {
++reconnectTime;
return true;
}
}
return false;
}
bool UDPConnection::CanReconnect() const {
return (globalConfig->reconnectTimeout > 0);
}
std::string UDPConnection::Statistics() const
{
std::string msg = "[UDPConnection::Statistics]\n";
msg += spring::format("\tReceived: %u bytes in %u packets (%f bytes/package)\n",
dataRecv, recvPackets, spring::SafeDivide(dataRecv, recvPackets));
msg += spring::format("\tSent: %u bytes in %u packets (%f bytes/package)\n",
dataSent, sentPackets, spring::SafeDivide(dataSent, sentPackets));
msg += spring::format("\tRelative protocol overhead: %f up, %f down\n",
spring::SafeDivide(sentOverhead, dataSent), spring::SafeDivide(recvOverhead, dataRecv) );
msg += spring::format("\t%u incoming chunks dropped, %u outgoing chunks resent\n",
droppedChunks, resentChunks);
return msg;
}
bool UDPConnection::IsUsingAddress(const ip::udp::endpoint& from) const
{
return (addr == from);
}
std::string UDPConnection::GetFullAddress() const
{
return spring::format("[%s]:%u", addr.address().to_string().c_str(), addr.port());
}
void UDPConnection::SetMTU(unsigned mtu2)
{
if ((mtu2 > 300) && (mtu2 < udpMaxPacketSize)) {
mtu = mtu2;
}
}
void UDPConnection::CreateChunk(const unsigned char* data, const unsigned length, const int packetNum)
{
assert((length > 0) && (length < 255));
ChunkPtr buf(new Chunk);
buf->chunkNumber = packetNum;
buf->chunkSize = length;
std::copy(data, data+length, std::back_inserter(buf->data));
newChunks.push_back(buf);
lastChunkCreatedTime = spring_gettime();
}
void UDPConnection::SendIfNecessary(bool flushed)
{
const spring_time curTime = spring_gettime();
int nak = 0;
std::vector<int> dropped;
{
int packetNum = lastInOrder+1;
for (packetMap::iterator pi = waitingPackets.begin(); pi != waitingPackets.end(); ++pi)
{
const int diff = pi->first - packetNum;
if (diff > 0) {
for (int i = 0; i < diff; ++i) {
dropped.push_back(packetNum);
packetNum++;
}
}
packetNum++;
}
while (!dropped.empty() && (dropped.back() - (lastInOrder + 1)) > 255)
dropped.pop_back();
unsigned numContinuous = 0;
for (unsigned i = 0; i != dropped.size(); ++i) {
if (dropped[i] == (lastInOrder + i + 1)) {
numContinuous++;
} else {
break;
}
}
if ((numContinuous < 8) && (curTime - lastNakTime) > spring_msecs(200 >> netLossFactor)) {
nak = std::min(dropped.size(), (size_t)127);
// needs 1 byte per requested packet, so do not spam to often
lastNakTime = curTime;
} else {
nak = -(int)std::min((unsigned)127, numContinuous);
}
}
if (!unackedChunks.empty() &&
(curTime - lastChunkCreatedTime) > spring_msecs(400 >> netLossFactor) &&
(curTime - lastUnackResentTime) > spring_msecs(400 >> netLossFactor)) {
// resend last packet if we didn't get an ack within reasonable time
// and don't plan sending out a new chunk either
if (newChunks.empty())
RequestResend(*unackedChunks.rbegin());
lastUnackResentTime = curTime;
}
if (flushed || !newChunks.empty() || (netLossFactor == MIN_LOSS_FACTOR && !resendRequested.empty()) || (nak > 0) || (curTime - lastPacketSendTime) > spring_msecs(200 >> netLossFactor))
{
bool todo = true;
int maxResend = resendRequested.size();
int unackPrevSize = unackedChunks.size();
std::map<std::int32_t, ChunkPtr>::iterator resIter = resendRequested.begin();
std::map<std::int32_t, ChunkPtr>::iterator resMidIter, resMidIterStart, resMidIterEnd;
std::map<std::int32_t, ChunkPtr>::reverse_iterator resRevIter;
if (netLossFactor != MIN_LOSS_FACTOR) {
maxResend = std::min(maxResend, 20 * netLossFactor); // keep it reasonable, or it could cause a tremendous flood of packets
resMidIter = resendRequested.begin();
resMidIterStart = resendRequested.begin();
resMidIterEnd = resendRequested.end();
resRevIter = resendRequested.rbegin();
const int resMidStart = (maxResend + 3) / 4;
const int resMidEnd = (maxResend + 2) / 4;
for (int i = 0; i < resMidStart; ++i)
++resMidIterStart;
if (resMidIterStart != resendRequested.end() && lastMidChunk < resMidIterStart->first)
lastMidChunk = resMidIterStart->first - 1;
for (int i = 0; i < resMidEnd; ++i)
--resMidIterEnd;
while (resMidIter != resendRequested.end() && resMidIter->first <= lastMidChunk)
++resMidIter;
if (resMidIter == resendRequested.end() || resMidIterEnd == resendRequested.end() ||
resMidIter->first >= resMidIterEnd->first)
resMidIter = resMidIterStart;
}
int rev = 0;
while (todo && ((outgoing.GetAverage() <= globalConfig->linkOutgoingBandwidth) || (globalConfig->linkOutgoingBandwidth <= 0))) {
Packet buf(lastInOrder, nak);
if (nak > 0) {
buf.naks.resize(nak);
for (unsigned i = 0; i != buf.naks.size(); ++i) {
buf.naks[i] = dropped[i] - (lastInOrder + 1); // zero means request resend of lastInOrder + 1
}
if (netLossFactor == MIN_LOSS_FACTOR)
nak = 0; // 1 request is enough, unless high loss
}
bool sent = false;
while (true) {
bool canResend = maxResend > 0 &&
((buf.GetSize() +
(((netLossFactor == MIN_LOSS_FACTOR) || (rev == 0)) ? resIter->second->GetSize() : ((rev == 1) ? resRevIter->second->GetSize() : resMidIter->second->GetSize())) // resend chunk size
) <= mtu);
bool canSendNew = !newChunks.empty() && ((buf.GetSize() + newChunks[0]->GetSize()) <= mtu);
if (!canResend && !canSendNew)
break;
// alternate between send and resend to make sure none is starved
resend = !resend;
if (resend && canResend) {
if (netLossFactor == MIN_LOSS_FACTOR) {
buf.chunks.push_back(resIter->second);
resIter = resendRequested.erase(resIter);
} else {
// on a lossy connection, just keep resending until it is acked
switch(rev) {
case 0:
buf.chunks.push_back(resIter->second);
++resIter;
break;
// alternate between sending from front, middle and back of list of requested chunks,
case 1:
buf.chunks.push_back(resRevIter->second);
++resRevIter;
break;
// since this improves performance on high latency connections
case 2:
case 3:
buf.chunks.push_back(resMidIter->second);
lastMidChunk = resMidIter->first;
++resMidIter;
if (resMidIter == resMidIterEnd)
resMidIter = resMidIterStart;
break;
}
rev = (rev + 1) % 4;
}
++resentChunks;
--maxResend;
sent = true;
} else if (!resend && canSendNew) {
buf.chunks.push_back(newChunks[0]);
unackedChunks.push_back(newChunks[0]);
newChunks.pop_front();
sent = true;
}
}
if (!sent || (maxResend == 0 && newChunks.empty()))
todo = false;
buf.checksum = buf.GetChecksum();
EMULATE_PACKET_CORRUPTION(buf.checksum);
SendPacket(buf);
}
if (netLossFactor != MIN_LOSS_FACTOR) {
// on a lossy connection the packet will be sent multiple times
for (int i = unackPrevSize; i < unackedChunks.size(); ++i)
RequestResend(unackedChunks[i]);
}
}
}
void UDPConnection::SendPacket(Packet& pkt)
{
std::vector<std::uint8_t> data;
pkt.Serialize(data);
outgoing.DataSent(data.size());
lastPacketSendTime = spring_gettime();
ip::udp::socket::message_flags flags = 0;
asio::error_code err;
EMULATE_LATENCY( !EMULATE_PACKET_LOSS( LOSS_COUNTER ) ) {
mySocket->send_to(buffer(data), addr, flags, err);
}
if (CheckErrorCode(err))
return;
dataSent += data.size();
++sentPackets;
}
void UDPConnection::AckChunks(int lastAck)
{
while (!unackedChunks.empty() && (lastAck >= (*unackedChunks.begin())->chunkNumber))
unackedChunks.pop_front();
// resend requested and later acked, happens every now and then
while (!resendRequested.empty() && lastAck >= resendRequested.begin()->first)
resendRequested.erase(resendRequested.begin());
}
void UDPConnection::RequestResend(ChunkPtr ptr)
{
// filter out duplicates
if (resendRequested.find(ptr->chunkNumber) == resendRequested.end())
resendRequested[ptr->chunkNumber] = ptr;
}
UDPConnection::BandwidthUsage::BandwidthUsage()
: lastTime(0)
, trafficSinceLastTime(1)
, prelTrafficSinceLastTime(0)
, average(0.0)
{
}
void UDPConnection::BandwidthUsage::UpdateTime(unsigned newTime)
{
if (newTime > (lastTime + 100)) {
average = (average*9 + float(trafficSinceLastTime) / float(newTime-lastTime) * 1000.0f) / 10.0f;
trafficSinceLastTime = 0;
prelTrafficSinceLastTime = 0;
lastTime = newTime;
}
}
void UDPConnection::BandwidthUsage::DataSent(unsigned amount, bool prel)
{
if (prel) {
prelTrafficSinceLastTime += amount;
} else {
trafficSinceLastTime += amount;
}
}
float UDPConnection::BandwidthUsage::GetAverage(bool prel) const
{
// not exactly accurate, but does job
return average + (prel ? std::max(trafficSinceLastTime, prelTrafficSinceLastTime) : trafficSinceLastTime);
}
void UDPConnection::Close(bool flush) {
if (closed) {
return;
}
Flush(flush);
muted = true;
if (!sharedSocket) {
try {
mySocket->close();
} catch (const asio::system_error& ex) {
LOG_L(L_ERROR, "Failed closing UDP connection: %s", ex.what());
}
}
closed = true;
}
void UDPConnection::SetLossFactor(int factor) {
netLossFactor = std::max((int)MIN_LOSS_FACTOR, std::min(factor, (int)MAX_LOSS_FACTOR));
}
} // namespace netcode
|