diff options
| author | qwasdrizzel <145519042+qwasdrizzel@users.noreply.github.com> | 2026-03-16 21:44:26 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-16 21:44:26 -0500 |
| commit | ce739f6045ec72127491286ea3f3f21e537c1b55 (patch) | |
| tree | f33bd42a47c1b4a7b2153a7fb77127ee3b407db9 /Minecraft.World/Packet.cpp | |
| parent | 255a18fe8e9b57377975f82e2b227afe2a12eda0 (diff) | |
| parent | 5a59f5d146b43811dde6a5a0245ee9875d7b5cd1 (diff) | |
Merge branch 'smartcmd:main' into main
Diffstat (limited to 'Minecraft.World/Packet.cpp')
| -rw-r--r-- | Minecraft.World/Packet.cpp | 76 |
1 files changed, 45 insertions, 31 deletions
diff --git a/Minecraft.World/Packet.cpp b/Minecraft.World/Packet.cpp index a37cb2ad..05bf932d 100644 --- a/Minecraft.World/Packet.cpp +++ b/Minecraft.World/Packet.cpp @@ -252,11 +252,11 @@ void Packet::updatePacketStatsPIX() for( auto it = outgoingStatistics.begin(); it != outgoingStatistics.end(); it++ ) { Packet::PacketStatistics *stat = it->second; - __int64 count = stat->getRunningCount(); + int64_t count = stat->getRunningCount(); wchar_t pixName[256]; swprintf_s(pixName,L"Packet count %d",stat->id); // PIXReportCounter(pixName,(float)count); - __int64 total = stat->getRunningTotal(); + int64_t total = stat->getRunningTotal(); swprintf_s(pixName,L"Packet bytes %d",stat->id); PIXReportCounter(pixName,(float)total); stat->IncrementPos(); @@ -267,8 +267,12 @@ void Packet::updatePacketStatsPIX() shared_ptr<Packet> Packet::getPacket(int id) { - // 4J: Removed try/catch - return idToCreateMap[id](); + auto it = idToCreateMap.find(id); + if (it == idToCreateMap.end()) + { + return nullptr; + } + return it->second(); } void Packet::writeBytes(DataOutputStream *dataoutputstream, byteArray bytes) @@ -327,25 +331,30 @@ shared_ptr<Packet> Packet::readPacket(DataInputStream *dis, bool isServer) // th id = dis->read(); if (id == -1) return nullptr; + // Track last few good packets for diagnosing TCP desync + static thread_local int s_lastIds[8] = {}; + static thread_local int s_lastIdPos = 0; + static thread_local int s_packetCount = 0; + if ((isServer && serverReceivedPackets.find(id) == serverReceivedPackets.end()) || (!isServer && clientReceivedPackets.find(id) == clientReceivedPackets.end())) { - //app.DebugPrintf("Bad packet id %d\n", id); - __debugbreak(); - assert(false); - // throw new IOException(wstring(L"Bad packet id ") + std::to_wstring(id)); + return nullptr; } packet = getPacket(id); - if (packet == NULL) assert(false);//throw new IOException(wstring(L"Bad packet id ") + std::to_wstring(id)); + if (packet == nullptr) return nullptr;//throw new IOException(wstring(L"Bad packet id ") + std::to_wstring(id)); + + s_lastIds[s_lastIdPos] = id; + s_lastIdPos = (s_lastIdPos + 1) % 8; + s_packetCount++; - //app.DebugPrintf("%s reading packet %d\n", isServer ? "Server" : "Client", packet->getId()); packet->read(dis); // } // catch (EOFException e) // { // // reached end of stream // OutputDebugString("Reached end of stream"); - // return NULL; + // return nullptr; // } // 4J - Don't bother tracking stats in a content package @@ -372,7 +381,7 @@ shared_ptr<Packet> Packet::readPacket(DataInputStream *dis, bool isServer) // th void Packet::writePacket(shared_ptr<Packet> packet, DataOutputStream *dos) // throws IOException TODO 4J JEV, should this declare a throws? { - //app.DebugPrintf("Writing packet %d\n", packet->getId()); + //app.DebugPrintf("NET WRITE: packet id=%d (0x%02X) estSize=%d\n", packet->getId(), packet->getId(), packet->getEstimatedSize()); dos->write(packet->getId()); packet->write(dos); } @@ -386,7 +395,7 @@ void Packet::writeUtf(const wstring& value, DataOutputStream *dos) // throws IOE } #endif - dos->writeShort((short)value.length()); + dos->writeShort(static_cast<short>(value.length())); dos->writeChars(value); } @@ -394,11 +403,9 @@ wstring Packet::readUtf(DataInputStream *dis, int maxLength) // throws IOExcepti { short stringLength = dis->readShort(); - if (stringLength > maxLength) + if (stringLength > maxLength || stringLength <= 0) { - wstringstream stream; - stream << L"Received string length longer than maximum allowed (" << stringLength << " > " << maxLength << ")"; - assert(false); + return L""; // throw new IOException( stream.str() ); } if (stringLength < 0) @@ -443,7 +450,7 @@ double Packet::PacketStatistics::getAverageSize() { return 0; } - return (double) totalSize / count; + return static_cast<double>(totalSize) / count; } int Packet::PacketStatistics::getTotalSize() @@ -451,10 +458,10 @@ int Packet::PacketStatistics::getTotalSize() return totalSize; } -__int64 Packet::PacketStatistics::getRunningTotal() +int64_t Packet::PacketStatistics::getRunningTotal() { - __int64 total = 0; - __int64 currentTime = System::currentTimeMillis(); + int64_t total = 0; + int64_t currentTime = System::currentTimeMillis(); for( int i = 0; i < TOTAL_TICKS; i++ ) { if( currentTime - timeSamples[i] <= 1000 ) @@ -465,10 +472,10 @@ __int64 Packet::PacketStatistics::getRunningTotal() return total; } -__int64 Packet::PacketStatistics::getRunningCount() +int64_t Packet::PacketStatistics::getRunningCount() { - __int64 total = 0; - __int64 currentTime = System::currentTimeMillis(); + int64_t total = 0; + int64_t currentTime = System::currentTimeMillis(); for( int i = 0; i < TOTAL_TICKS; i++ ) { if( currentTime - timeSamples[i] <= 1000 ) @@ -507,12 +514,12 @@ shared_ptr<ItemInstance> Packet::readItem(DataInputStream *dis) { shared_ptr<ItemInstance> item = nullptr; int id = dis->readShort(); - if (id >= 0) + if (id >= 0 && id < 32000) // todo: should turn Item::ITEM_NUM_COUNT into a global define { int count = dis->readByte(); int damage = dis->readShort(); - item = shared_ptr<ItemInstance>( new ItemInstance(id, count, damage) ); + item = std::make_shared<ItemInstance>(id, count, damage); // 4J Stu - Always read/write the tag //if (Item.items[id].canBeDepleted() || Item.items[id].shouldOverrideMultiplayerNBT()) { @@ -525,7 +532,7 @@ shared_ptr<ItemInstance> Packet::readItem(DataInputStream *dis) void Packet::writeItem(shared_ptr<ItemInstance> item, DataOutputStream *dos) { - if (item == NULL) + if (item == nullptr) { dos->writeShort(-1); } @@ -545,9 +552,16 @@ void Packet::writeItem(shared_ptr<ItemInstance> item, DataOutputStream *dos) CompoundTag *Packet::readNbt(DataInputStream *dis) { int size = dis->readShort(); - if (size < 0) return NULL; + if (size <= 0) return nullptr; + + const int MAX_NBT_SIZE = 32767; + if (size > MAX_NBT_SIZE) return nullptr; byteArray buff(size); - dis->readFully(buff); + if (!dis->readFully(buff)) + { + delete [] buff.data; + return nullptr; + } CompoundTag *result = (CompoundTag *) NbtIo::decompress(buff); delete [] buff.data; return result; @@ -555,14 +569,14 @@ CompoundTag *Packet::readNbt(DataInputStream *dis) void Packet::writeNbt(CompoundTag *tag, DataOutputStream *dos) { - if (tag == NULL) + if (tag == nullptr) { dos->writeShort(-1); } else { byteArray buff = NbtIo::compress(tag); - dos->writeShort((short) buff.length); + dos->writeShort(static_cast<short>(buff.length)); dos->write(buff); delete [] buff.data; } |
