1
0

Use cMultiVersionProtocol's buffer

This commit is contained in:
Tiger Wang 2020-07-18 19:22:28 +01:00
parent 5141d05ba6
commit bc1b70dcde
4 changed files with 33 additions and 37 deletions

View File

@ -343,7 +343,7 @@ public:
}; };
/** Called when client sends some data */ /** Called when client sends some data */
virtual void DataReceived(const char * a_Data, size_t a_Size) = 0; virtual void DataReceived(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size) = 0;
// Sending stuff to clients (alphabetically sorted): // Sending stuff to clients (alphabetically sorted):
virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity & a_Vehicle) = 0; virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity & a_Vehicle) = 0;

View File

@ -50,7 +50,7 @@ struct sTriedToJoinWithUnsupportedProtocolException : public std::runtime_error
cMultiVersionProtocol::cMultiVersionProtocol() : cMultiVersionProtocol::cMultiVersionProtocol() :
HandleIncomingData(std::bind(&cMultiVersionProtocol::HandleIncomingDataInRecognitionStage, this, std::placeholders::_1, std::placeholders::_2)), HandleIncomingData(std::bind(&cMultiVersionProtocol::HandleIncomingDataInRecognitionStage, this, std::placeholders::_1, std::placeholders::_2)),
m_Buffer(8 KiB) // We need a larger buffer to support BungeeCord - it sends one huge packet at the start m_Buffer(32 KiB)
{ {
} }
@ -107,7 +107,7 @@ void cMultiVersionProtocol::HandleIncomingDataInRecognitionStage(cClientHandle &
HandleIncomingData = [this](cClientHandle &, const std::string_view a_In) HandleIncomingData = [this](cClientHandle &, const std::string_view a_In)
{ {
// TODO: make it take our a_ReceivedData // TODO: make it take our a_ReceivedData
m_Protocol->DataReceived(a_In.data(), a_In.size()); m_Protocol->DataReceived(m_Buffer, a_In.data(), a_In.size());
}; };
} }
catch (const sUnsupportedButPingableProtocolException &) catch (const sUnsupportedButPingableProtocolException &)

View File

@ -107,7 +107,6 @@ cProtocol_1_8_0::cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_Ser
m_ServerAddress(a_ServerAddress), m_ServerAddress(a_ServerAddress),
m_ServerPort(a_ServerPort), m_ServerPort(a_ServerPort),
m_State(a_State), m_State(a_State),
m_ReceivedData(32 KiB),
m_IsEncrypted(false) m_IsEncrypted(false)
{ {
AStringVector Params; AStringVector Params;
@ -183,7 +182,7 @@ cProtocol_1_8_0::cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_Ser
void cProtocol_1_8_0::DataReceived(const char * a_Data, size_t a_Size) void cProtocol_1_8_0::DataReceived(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size)
{ {
if (m_IsEncrypted) if (m_IsEncrypted)
{ {
@ -192,14 +191,14 @@ void cProtocol_1_8_0::DataReceived(const char * a_Data, size_t a_Size)
{ {
size_t NumBytes = (a_Size > sizeof(Decrypted)) ? sizeof(Decrypted) : a_Size; size_t NumBytes = (a_Size > sizeof(Decrypted)) ? sizeof(Decrypted) : a_Size;
m_Decryptor.ProcessData(Decrypted, reinterpret_cast<const Byte *>(a_Data), NumBytes); m_Decryptor.ProcessData(Decrypted, reinterpret_cast<const Byte *>(a_Data), NumBytes);
AddReceivedData(reinterpret_cast<const char *>(Decrypted), NumBytes); AddReceivedData(a_Buffer, reinterpret_cast<const char *>(Decrypted), NumBytes);
a_Size -= NumBytes; a_Size -= NumBytes;
a_Data += NumBytes; a_Data += NumBytes;
} }
} }
else else
{ {
AddReceivedData(a_Data, a_Size); AddReceivedData(a_Buffer, a_Data, a_Size);
} }
} }
@ -1873,19 +1872,19 @@ UInt32 cProtocol_1_8_0::GetProtocolMobType(eMonsterType a_MobType)
void cProtocol_1_8_0::AddReceivedData(const char * a_Data, size_t a_Size) void cProtocol_1_8_0::AddReceivedData(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size)
{ {
// Write the incoming data into the comm log file: // Write the incoming data into the comm log file:
if (g_ShouldLogCommIn && m_CommLogFile.IsOpen()) if (g_ShouldLogCommIn && m_CommLogFile.IsOpen())
{ {
if (m_ReceivedData.GetReadableSpace() > 0) if (a_Buffer.GetReadableSpace() > 0)
{ {
AString AllData; AString AllData;
size_t OldReadableSpace = m_ReceivedData.GetReadableSpace(); size_t OldReadableSpace = a_Buffer.GetReadableSpace();
m_ReceivedData.ReadAll(AllData); a_Buffer.ReadAll(AllData);
m_ReceivedData.ResetRead(); a_Buffer.ResetRead();
m_ReceivedData.SkipRead(m_ReceivedData.GetReadableSpace() - OldReadableSpace); a_Buffer.SkipRead(a_Buffer.GetReadableSpace() - OldReadableSpace);
ASSERT(m_ReceivedData.GetReadableSpace() == OldReadableSpace); ASSERT(a_Buffer.GetReadableSpace() == OldReadableSpace);
AString Hex; AString Hex;
CreateHexDump(Hex, AllData.data(), AllData.size(), 16); CreateHexDump(Hex, AllData.data(), AllData.size(), 16);
m_CommLogFile.Printf("Incoming data, %zu (0x%zx) unparsed bytes already present in buffer:\n%s\n", m_CommLogFile.Printf("Incoming data, %zu (0x%zx) unparsed bytes already present in buffer:\n%s\n",
@ -1900,7 +1899,7 @@ void cProtocol_1_8_0::AddReceivedData(const char * a_Data, size_t a_Size)
m_CommLogFile.Flush(); m_CommLogFile.Flush();
} }
if (!m_ReceivedData.Write(a_Data, a_Size)) if (!a_Buffer.Write(a_Data, a_Size))
{ {
// Too much data in the incoming queue, report to caller: // Too much data in the incoming queue, report to caller:
m_Client->PacketBufferFull(); m_Client->PacketBufferFull();
@ -1911,16 +1910,16 @@ void cProtocol_1_8_0::AddReceivedData(const char * a_Data, size_t a_Size)
for (;;) for (;;)
{ {
UInt32 PacketLen; UInt32 PacketLen;
if (!m_ReceivedData.ReadVarInt(PacketLen)) if (!a_Buffer.ReadVarInt(PacketLen))
{ {
// Not enough data // Not enough data
m_ReceivedData.ResetRead(); a_Buffer.ResetRead();
break; break;
} }
if (!m_ReceivedData.CanReadBytes(PacketLen)) if (!a_Buffer.CanReadBytes(PacketLen))
{ {
// The full packet hasn't been received yet // The full packet hasn't been received yet
m_ReceivedData.ResetRead(); a_Buffer.ResetRead();
break; break;
} }
@ -1929,15 +1928,15 @@ void cProtocol_1_8_0::AddReceivedData(const char * a_Data, size_t a_Size)
AString UncompressedData; AString UncompressedData;
if (m_State == 3) if (m_State == 3)
{ {
UInt32 NumBytesRead = static_cast<UInt32>(m_ReceivedData.GetReadableSpace()); UInt32 NumBytesRead = static_cast<UInt32>(a_Buffer.GetReadableSpace());
if (!m_ReceivedData.ReadVarInt(UncompressedSize)) if (!a_Buffer.ReadVarInt(UncompressedSize))
{ {
m_Client->Kick("Compression packet incomplete"); m_Client->Kick("Compression packet incomplete");
return; return;
} }
NumBytesRead -= static_cast<UInt32>(m_ReceivedData.GetReadableSpace()); // How many bytes has the UncompressedSize taken up? NumBytesRead -= static_cast<UInt32>(a_Buffer.GetReadableSpace()); // How many bytes has the UncompressedSize taken up?
ASSERT(PacketLen > NumBytesRead); ASSERT(PacketLen > NumBytesRead);
PacketLen -= NumBytesRead; PacketLen -= NumBytesRead;
@ -1945,7 +1944,7 @@ void cProtocol_1_8_0::AddReceivedData(const char * a_Data, size_t a_Size)
{ {
// Decompress the data: // Decompress the data:
AString CompressedData; AString CompressedData;
VERIFY(m_ReceivedData.ReadString(CompressedData, PacketLen)); VERIFY(a_Buffer.ReadString(CompressedData, PacketLen));
if (InflateString(CompressedData.data(), PacketLen, UncompressedData) != Z_OK) if (InflateString(CompressedData.data(), PacketLen, UncompressedData) != Z_OK)
{ {
m_Client->Kick("Compression failure"); m_Client->Kick("Compression failure");
@ -1965,14 +1964,14 @@ void cProtocol_1_8_0::AddReceivedData(const char * a_Data, size_t a_Size)
if (UncompressedSize == 0) if (UncompressedSize == 0)
{ {
// No compression was used, move directly // No compression was used, move directly
VERIFY(m_ReceivedData.ReadToByteBuffer(bb, static_cast<size_t>(PacketLen))); VERIFY(a_Buffer.ReadToByteBuffer(bb, static_cast<size_t>(PacketLen)));
} }
else else
{ {
// Compression was used, move the uncompressed data: // Compression was used, move the uncompressed data:
VERIFY(bb.Write(UncompressedData.data(), UncompressedData.size())); VERIFY(bb.Write(UncompressedData.data(), UncompressedData.size()));
} }
m_ReceivedData.CommitRead(); a_Buffer.CommitRead();
UInt32 PacketType; UInt32 PacketType;
if (!bb.ReadVarInt(PacketType)) if (!bb.ReadVarInt(PacketType))
@ -2048,18 +2047,18 @@ void cProtocol_1_8_0::AddReceivedData(const char * a_Data, size_t a_Size)
} // for (ever) } // for (ever)
// Log any leftover bytes into the logfile: // Log any leftover bytes into the logfile:
if (g_ShouldLogCommIn && (m_ReceivedData.GetReadableSpace() > 0) && m_CommLogFile.IsOpen()) if (g_ShouldLogCommIn && (a_Buffer.GetReadableSpace() > 0) && m_CommLogFile.IsOpen())
{ {
AString AllData; AString AllData;
size_t OldReadableSpace = m_ReceivedData.GetReadableSpace(); size_t OldReadableSpace = a_Buffer.GetReadableSpace();
m_ReceivedData.ReadAll(AllData); a_Buffer.ReadAll(AllData);
m_ReceivedData.ResetRead(); a_Buffer.ResetRead();
m_ReceivedData.SkipRead(m_ReceivedData.GetReadableSpace() - OldReadableSpace); a_Buffer.SkipRead(a_Buffer.GetReadableSpace() - OldReadableSpace);
ASSERT(m_ReceivedData.GetReadableSpace() == OldReadableSpace); ASSERT(a_Buffer.GetReadableSpace() == OldReadableSpace);
AString Hex; AString Hex;
CreateHexDump(Hex, AllData.data(), AllData.size(), 16); CreateHexDump(Hex, AllData.data(), AllData.size(), 16);
m_CommLogFile.Printf("There are %zu (0x%zx) bytes of non-parse-able data left in the buffer:\n%s", m_CommLogFile.Printf("There are %zu (0x%zx) bytes of non-parse-able data left in the buffer:\n%s",
m_ReceivedData.GetReadableSpace(), m_ReceivedData.GetReadableSpace(), Hex.c_str() a_Buffer.GetReadableSpace(), a_Buffer.GetReadableSpace(), Hex.c_str()
); );
m_CommLogFile.Flush(); m_CommLogFile.Flush();
} }

View File

@ -34,7 +34,7 @@ public:
cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State);
/** Called when client sends some data: */ /** Called when client sends some data: */
virtual void DataReceived(const char * a_Data, size_t a_Size) override; virtual void DataReceived(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size) override;
/** Sending stuff to clients (alphabetically sorted): */ /** Sending stuff to clients (alphabetically sorted): */
virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity & a_Vehicle) override; virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity & a_Vehicle) override;
@ -143,9 +143,6 @@ protected:
/** State of the protocol. 1 = status, 2 = login, 3 = game */ /** State of the protocol. 1 = status, 2 = login, 3 = game */
UInt32 m_State; UInt32 m_State;
/** Buffer for the received data */
cByteBuffer m_ReceivedData;
bool m_IsEncrypted; bool m_IsEncrypted;
cAesCfb128Decryptor m_Decryptor; cAesCfb128Decryptor m_Decryptor;
@ -155,7 +152,7 @@ protected:
cFile m_CommLogFile; cFile m_CommLogFile;
/** Adds the received (unencrypted) data to m_ReceivedData, parses complete packets */ /** Adds the received (unencrypted) data to m_ReceivedData, parses complete packets */
virtual void AddReceivedData(const char * a_Data, size_t a_Size); virtual void AddReceivedData(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size);
/** Nobody inherits 1.8, so it doesn't use this method */ /** Nobody inherits 1.8, so it doesn't use this method */
virtual UInt32 GetPacketID(ePacketType a_Packet) override; virtual UInt32 GetPacketID(ePacketType a_Packet) override;