From eeb63b8901a9c049f1bb594abb9ce9b4a9c47620 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 11 Jan 2021 16:39:43 +0000 Subject: zlib -> libdeflate (#5085) + Use libdeflate + Use std::byte * Fix passing temporary to string_view + Emulate make_unique_for_overwrite --- src/Protocol/ForgeHandshake.cpp | 77 +++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 38 deletions(-) (limited to 'src/Protocol/ForgeHandshake.cpp') diff --git a/src/Protocol/ForgeHandshake.cpp b/src/Protocol/ForgeHandshake.cpp index 8c16d4f0c..99b585894 100644 --- a/src/Protocol/ForgeHandshake.cpp +++ b/src/Protocol/ForgeHandshake.cpp @@ -36,8 +36,8 @@ namespace ClientPhase /** Server handshake state phases. */ namespace ServerPhase { - static const Int8 WAITINGCACK = 2; - static const Int8 COMPLETE = 3; + static const auto WAITINGCACK = std::byte(2); + static const auto COMPLETE = std::byte(3); } @@ -105,12 +105,12 @@ void cForgeHandshake::BeginForgeHandshake(const AString & a_Name, const cUUID & m_UUID = a_UUID; m_Properties = a_Properties; - static const std::array Channels{{ "FML|HS", "FML", "FML|MP", "FML", "FORGE" }}; - AString ChannelsString; + static const std::array Channels{{ "FML|HS", "FML", "FML|MP", "FML", "FORGE" }}; + ContiguousByteBuffer ChannelsString; for (auto & Channel: Channels) { - ChannelsString.append(Channel); - ChannelsString.push_back('\0'); + ChannelsString.append({ reinterpret_cast(Channel.data()), Channel.size() }); + ChannelsString.push_back(std::byte(0)); } m_Client->SendPluginMessage("REGISTER", ChannelsString); @@ -123,7 +123,6 @@ void cForgeHandshake::BeginForgeHandshake(const AString & a_Name, const cUUID & void cForgeHandshake::SendServerHello() { - AString Message; cByteBuffer Buf(6); // Discriminator | Byte | Always 0 for ServerHello Buf.WriteBEInt8(Discriminator::ServerHello); @@ -131,6 +130,8 @@ void cForgeHandshake::SendServerHello() Buf.WriteBEInt8(2); // Dimension TODO Buf.WriteBEInt32(0); + + ContiguousByteBuffer Message; Buf.ReadAll(Message); m_Client->SendPluginMessage("FML|HS", Message); @@ -140,18 +141,18 @@ void cForgeHandshake::SendServerHello() -AStringMap cForgeHandshake::ParseModList(const char * a_Data, size_t a_Size) +AStringMap cForgeHandshake::ParseModList(const ContiguousByteBufferView a_Data) { AStringMap Mods; - if (a_Size < 4) + if (a_Data.size() < 4) { - SetError(Printf("ParseModList invalid packet, missing length (size = %zu)", a_Size)); + SetError(Printf("ParseModList invalid packet, missing length (size = %zu)", a_Data.size())); return Mods; } - cByteBuffer Buf(a_Size); - Buf.Write(a_Data, a_Size); + cByteBuffer Buf(a_Data.size()); + Buf.Write(a_Data.data(), a_Data.size()); UInt32 NumMods; if (!Buf.ReadVarInt32(NumMods)) { @@ -182,11 +183,11 @@ AStringMap cForgeHandshake::ParseModList(const char * a_Data, size_t a_Size) -void cForgeHandshake::HandleClientHello(cClientHandle * a_Client, const char * a_Data, size_t a_Size) +void cForgeHandshake::HandleClientHello(cClientHandle * a_Client, const ContiguousByteBufferView a_Data) { - if (a_Size == 2) + if (a_Data.size() == 2) { - int FmlProtocolVersion = a_Data[1]; + const auto FmlProtocolVersion = static_cast(a_Data[1]); LOGD("Received ClientHello with FML protocol version %d", FmlProtocolVersion); if (FmlProtocolVersion != 2) { @@ -195,7 +196,7 @@ void cForgeHandshake::HandleClientHello(cClientHandle * a_Client, const char * a } else { - SetError(Printf("Received unexpected length of ClientHello: %zu", a_Size)); + SetError(Printf("Received unexpected length of ClientHello: %zu", a_Data.size())); } } @@ -203,11 +204,11 @@ void cForgeHandshake::HandleClientHello(cClientHandle * a_Client, const char * a -void cForgeHandshake::HandleModList(cClientHandle * a_Client, const char * a_Data, size_t a_Size) +void cForgeHandshake::HandleModList(cClientHandle * a_Client, const ContiguousByteBufferView a_Data) { LOGD("Received ModList"); - auto ClientMods = ParseModList(a_Data + 1, a_Size - 1); + auto ClientMods = ParseModList(a_Data.substr(1)); AString ClientModsString; for (auto & item: ClientMods) { @@ -241,7 +242,8 @@ void cForgeHandshake::HandleModList(cClientHandle * a_Client, const char * a_Dat Buf.WriteVarUTF8String(item.first); // name Buf.WriteVarUTF8String(item.second); // version } - AString ServerModList; + + ContiguousByteBuffer ServerModList; Buf.ReadAll(ServerModList); m_Client->SendPluginMessage("FML|HS", ServerModList); @@ -251,15 +253,15 @@ void cForgeHandshake::HandleModList(cClientHandle * a_Client, const char * a_Dat -void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const char * a_Data, size_t a_Size) +void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const ContiguousByteBufferView a_Data) { - if (a_Size != 2) + if (a_Data.size() != 2) { - SetError(Printf("Unexpected HandshakeAck packet length: %zu", a_Size)); + SetError(Printf("Unexpected HandshakeAck packet length: %zu", a_Data.size())); return; } - auto Phase = a_Data[1]; + const auto Phase = static_cast(a_Data[1]); LOGD("Received client HandshakeAck with phase = %d", Phase); switch (Phase) @@ -282,7 +284,7 @@ void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const char * Buf.WriteVarInt32(NumSubstitutions); Buf.WriteVarInt32(NumDummies); - AString RegistryData; + ContiguousByteBuffer RegistryData; Buf.ReadAll(RegistryData); m_Client->SendPluginMessage("FML|HS", RegistryData); break; @@ -292,8 +294,8 @@ void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const char * { LOGD("Client finished receiving registry data; acknowledging"); - AString Ack; - Ack.push_back(Discriminator::HandshakeAck); + ContiguousByteBuffer Ack; + Ack.push_back(std::byte(Discriminator::HandshakeAck)); Ack.push_back(ServerPhase::WAITINGCACK); m_Client->SendPluginMessage("FML|HS", Ack); break; @@ -303,8 +305,8 @@ void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const char * { LOGD("Client is pending completion; sending complete ack"); - AString Ack; - Ack.push_back(Discriminator::HandshakeAck); + ContiguousByteBuffer Ack; + Ack.push_back(std::byte(Discriminator::HandshakeAck)); Ack.push_back(ServerPhase::COMPLETE); m_Client->SendPluginMessage("FML|HS", Ack); @@ -320,7 +322,7 @@ void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const char * default: { - SetError(Printf("Received unknown phase in Forge handshake acknowledgement: %d", Phase)); + SetError(fmt::format("Received unknown phase in Forge handshake acknowledgement: {}", Phase)); break; } } @@ -330,11 +332,11 @@ void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const char * -void cForgeHandshake::DataReceived(cClientHandle * a_Client, const char * a_Data, size_t a_Size) +void cForgeHandshake::DataReceived(cClientHandle * a_Client, const ContiguousByteBufferView a_Data) { if (!m_IsForgeClient) { - SetError(Printf("Received unexpected Forge data from non-Forge client (%zu bytes)", a_Size)); + SetError(Printf("Received unexpected Forge data from non-Forge client (%zu bytes)", a_Data.size())); return; } if (m_Errored) @@ -343,19 +345,18 @@ void cForgeHandshake::DataReceived(cClientHandle * a_Client, const char * a_Data return; } - if (a_Size <= 1) + if (a_Data.size() <= 1) { - SetError(Printf("Received unexpectedly short Forge data (%zu bytes)", a_Size)); + SetError(Printf("Received unexpectedly short Forge data (%zu bytes)", a_Data.size())); return; } - auto Discriminator = a_Data[0]; - + const auto Discriminator = static_cast(a_Data[0]); switch (Discriminator) { - case Discriminator::ClientHello: HandleClientHello(a_Client, a_Data, a_Size); break; - case Discriminator::ModList: HandleModList(a_Client, a_Data, a_Size); break; - case Discriminator::HandshakeAck: HandleHandshakeAck(a_Client, a_Data, a_Size); break; + case Discriminator::ClientHello: HandleClientHello(a_Client, a_Data); break; + case Discriminator::ModList: HandleModList(a_Client, a_Data); break; + case Discriminator::HandshakeAck: HandleHandshakeAck(a_Client, a_Data); break; default: { -- cgit v1.2.3