1
0
Fork 0

Add State enum to protocol

This commit is contained in:
Tiger Wang 2020-09-12 21:08:23 +01:00
parent bf253caab5
commit 7f372b12cb
8 changed files with 55 additions and 62 deletions

View File

@ -345,6 +345,14 @@ public:
v1_14 = 477 v1_14 = 477
}; };
enum State
{
Status = 1,
Login = 2,
Game = 3,
Invalid = 255
};
/** Called when client sends some data */ /** Called when client sends some data */
virtual void DataReceived(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size) = 0; virtual void DataReceived(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size) = 0;

View File

@ -244,7 +244,7 @@ std::unique_ptr<cProtocol> cMultiVersionProtocol::TryRecognizeLengthedProtocol(c
UInt32 ProtocolVersion; UInt32 ProtocolVersion;
AString ServerAddress; AString ServerAddress;
UInt16 ServerPort; UInt16 ServerPort;
UInt32 NextState; cProtocol::State NextState;
if (!m_Buffer.ReadVarInt(PacketType) || (PacketType != 0x00)) if (!m_Buffer.ReadVarInt(PacketType) || (PacketType != 0x00))
{ {
@ -308,7 +308,7 @@ std::unique_ptr<cProtocol> cMultiVersionProtocol::TryRecognizeLengthedProtocol(c
a_Client.GetIPString().c_str(), ProtocolVersion, ProtocolVersion a_Client.GetIPString().c_str(), ProtocolVersion, ProtocolVersion
); );
if (NextState != 1) if (NextState != cProtocol::State::Status)
{ {
throw sTriedToJoinWithUnsupportedProtocolException( throw sTriedToJoinWithUnsupportedProtocolException(
Printf("Unsupported protocol version %u.\nTry connecting with Minecraft " MCS_CLIENT_VERSIONS, ProtocolVersion) Printf("Unsupported protocol version %u.\nTry connecting with Minecraft " MCS_CLIENT_VERSIONS, ProtocolVersion)

View File

@ -1022,9 +1022,8 @@ bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTyp
{ {
switch (m_State) switch (m_State)
{ {
case 1: case State::Status:
{ {
// Status
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true; case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true;
@ -1033,9 +1032,8 @@ bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTyp
break; break;
} }
case 2: case State::Login:
{ {
// Login
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandlePacketLoginStart(a_ByteBuffer); return true; case 0x00: HandlePacketLoginStart(a_ByteBuffer); return true;
@ -1044,9 +1042,8 @@ bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTyp
break; break;
} }
case 3: case State::Game:
{ {
// Game
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandleConfirmTeleport(a_ByteBuffer); return true; case 0x00: HandleConfirmTeleport(a_ByteBuffer); return true;
@ -1093,10 +1090,10 @@ bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTyp
// Cannot kick the client - we don't know this state and thus the packet number for the kick packet // Cannot kick the client - we don't know this state and thus the packet number for the kick packet
// Switch to a state when all further packets are silently ignored: // Switch to a state when all further packets are silently ignored:
m_State = 255; m_State = State::Invalid;
return false; return false;
} }
case 255: case State::Invalid:
{ {
// This is the state used for "not processing packets anymore" when we receive a bad packet from a client. // This is the state used for "not processing packets anymore" when we receive a bad packet from a client.
// Do not output anything (the caller will do that for us), just return failure // Do not output anything (the caller will do that for us), just return failure
@ -1172,9 +1169,8 @@ bool cProtocol_1_12_1::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketT
{ {
switch (m_State) switch (m_State)
{ {
case 1: case State::Status:
{ {
// Status
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true; case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true;
@ -1183,9 +1179,8 @@ bool cProtocol_1_12_1::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketT
break; break;
} }
case 2: case State::Login:
{ {
// Login
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandlePacketLoginStart(a_ByteBuffer); return true; case 0x00: HandlePacketLoginStart(a_ByteBuffer); return true;
@ -1194,9 +1189,8 @@ bool cProtocol_1_12_1::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketT
break; break;
} }
case 3: case State::Game:
{ {
// Game
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandleConfirmTeleport(a_ByteBuffer); return true; case 0x00: HandleConfirmTeleport(a_ByteBuffer); return true;
@ -1243,10 +1237,10 @@ bool cProtocol_1_12_1::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketT
// Cannot kick the client - we don't know this state and thus the packet number for the kick packet // Cannot kick the client - we don't know this state and thus the packet number for the kick packet
// Switch to a state when all further packets are silently ignored: // Switch to a state when all further packets are silently ignored:
m_State = 255; m_State = State::Invalid;
return false; return false;
} }
case 255: case State::Invalid:
{ {
// This is the state used for "not processing packets anymore" when we receive a bad packet from a client. // This is the state used for "not processing packets anymore" when we receive a bad packet from a client.
// Do not output anything (the caller will do that for us), just return failure // Do not output anything (the caller will do that for us), just return failure

View File

@ -226,7 +226,7 @@ std::pair<short, short> cProtocol_1_14::GetItemFromProtocolID(UInt32 a_ProtocolI
UInt32 cProtocol_1_14::GetProtocolBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) UInt32 cProtocol_1_14::GetProtocolBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta)
{ {
return Palette_1_14::From(PaletteUpgrade::FromBlock(a_BlockType, a_Meta)); return Palette_1_14::FromBlock(PaletteUpgrade::FromBlock(a_BlockType, a_Meta));
} }
@ -235,7 +235,7 @@ UInt32 cProtocol_1_14::GetProtocolBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_
UInt32 cProtocol_1_14::GetProtocolItemType(short a_ItemID, short a_ItemDamage) UInt32 cProtocol_1_14::GetProtocolItemType(short a_ItemID, short a_ItemDamage)
{ {
return Palette_1_14::From(PaletteUpgrade::FromItem(a_ItemID, a_ItemDamage)); return Palette_1_14::FromItem(PaletteUpgrade::FromItem(a_ItemID, a_ItemDamage));
} }
@ -253,7 +253,7 @@ UInt32 cProtocol_1_14::GetProtocolStatisticType(Statistic a_Statistic)
bool cProtocol_1_14::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) bool cProtocol_1_14::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType)
{ {
if (m_State != 3) if (m_State != State::Game)
{ {
return Super::HandlePacket(a_ByteBuffer, a_PacketType); return Super::HandlePacket(a_ByteBuffer, a_PacketType);
} }

View File

@ -100,7 +100,7 @@ extern bool g_ShouldLogCommIn, g_ShouldLogCommOut;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// cProtocol_1_8_0: // cProtocol_1_8_0:
cProtocol_1_8_0::cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : cProtocol_1_8_0::cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, State a_State) :
Super(a_Client), Super(a_Client),
m_ServerAddress(a_ServerAddress), m_ServerAddress(a_ServerAddress),
// Note: a_ServerPort is unused // Note: a_ServerPort is unused
@ -383,16 +383,14 @@ void cProtocol_1_8_0::SendDisconnect(const AString & a_Reason)
{ {
switch (m_State) switch (m_State)
{ {
case 2: case State::Login:
{ {
// During login:
cPacketizer Pkt(*this, pktDisconnectDuringLogin); cPacketizer Pkt(*this, pktDisconnectDuringLogin);
Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str())); Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str()));
break; break;
} }
case 3: case State::Game:
{ {
// In-game:
cPacketizer Pkt(*this, pktDisconnectDuringGame); cPacketizer Pkt(*this, pktDisconnectDuringGame);
Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str())); Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str()));
break; break;
@ -796,7 +794,7 @@ void cProtocol_1_8_0::SendLoginSuccess(void)
Pkt.WriteVarInt32(CompressionThreshold); Pkt.WriteVarInt32(CompressionThreshold);
} }
m_State = 3; // State = Game m_State = State::Game;
{ {
cPacketizer Pkt(*this, pktLoginSuccess); cPacketizer Pkt(*this, pktLoginSuccess);
@ -2217,9 +2215,8 @@ bool cProtocol_1_8_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy
{ {
switch (m_State) switch (m_State)
{ {
case 1: case State::Status:
{ {
// Status
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true; case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true;
@ -2228,9 +2225,8 @@ bool cProtocol_1_8_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy
break; break;
} }
case 2: case State::Login:
{ {
// Login
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandlePacketLoginStart (a_ByteBuffer); return true; case 0x00: HandlePacketLoginStart (a_ByteBuffer); return true;
@ -2239,9 +2235,8 @@ bool cProtocol_1_8_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy
break; break;
} }
case 3: case State::Game:
{ {
// Game
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandlePacketKeepAlive (a_ByteBuffer); return true; case 0x00: HandlePacketKeepAlive (a_ByteBuffer); return true;
@ -2281,10 +2276,10 @@ bool cProtocol_1_8_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy
// Cannot kick the client - we don't know this state and thus the packet number for the kick packet // Cannot kick the client - we don't know this state and thus the packet number for the kick packet
// Switch to a state when all further packets are silently ignored: // Switch to a state when all further packets are silently ignored:
m_State = 255; m_State = State::Invalid;
return false; return false;
} }
case 255: case State::Invalid:
{ {
// This is the state used for "not processing packets anymore" when we receive a bad packet from a client. // This is the state used for "not processing packets anymore" when we receive a bad packet from a client.
// Do not output anything (the caller will do that for us), just return failure // Do not output anything (the caller will do that for us), just return failure

View File

@ -31,7 +31,7 @@ class cProtocol_1_8_0:
public: 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, State a_State);
/** Called when client sends some data: */ /** Called when client sends some data: */
virtual void DataReceived(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size) override; virtual void DataReceived(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size) override;
@ -134,6 +134,9 @@ public:
protected: protected:
/** State of the protocol. */
State m_State;
/** 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(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size); virtual void AddReceivedData(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size);
@ -190,7 +193,6 @@ protected:
The message payload is still in the bytebuffer, the handler reads it specifically for each handled channel */ The message payload is still in the bytebuffer, the handler reads it specifically for each handled channel */
virtual void HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const AString & a_Channel); virtual void HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const AString & a_Channel);
/** Sends the data to the client, encrypting them if needed. */ /** Sends the data to the client, encrypting them if needed. */
virtual void SendData(const char * a_Data, size_t a_Size) override; virtual void SendData(const char * a_Data, size_t a_Size) override;
@ -229,11 +231,20 @@ protected:
/** Writes the block entity data for the specified block entity into the packet. */ /** Writes the block entity data for the specified block entity into the packet. */
virtual void WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & a_BlockEntity); virtual void WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & a_BlockEntity);
/** State of the protocol. 1 = status, 2 = login, 3 = game */
UInt32 m_State;
private: private:
AString m_ServerAddress;
AString m_AuthServerID;
bool m_IsEncrypted;
cAesCfb128Decryptor m_Decryptor;
cAesCfb128Encryptor m_Encryptor;
/** The logfile where the comm is logged, when g_ShouldLogComm is true */
cFile m_CommLogFile;
/** Sends an entity teleport packet. /** Sends an entity teleport packet.
Mitigates a 1.8 bug where the position in the entity spawn packet is ignored, Mitigates a 1.8 bug where the position in the entity spawn packet is ignored,
and so entities don't show up until a teleport is sent. */ and so entities don't show up until a teleport is sent. */
@ -247,16 +258,4 @@ private:
Protocols <= 1.12 use strings, hence this is a static as the string-mapping was append-only for the versions that used it. Protocols <= 1.12 use strings, hence this is a static as the string-mapping was append-only for the versions that used it.
Returns an empty string, handled correctly by the client, for newer, unsupported statistics. */ Returns an empty string, handled correctly by the client, for newer, unsupported statistics. */
static const char * GetProtocolStatisticName(Statistic a_Statistic); static const char * GetProtocolStatisticName(Statistic a_Statistic);
AString m_ServerAddress;
AString m_AuthServerID;
bool m_IsEncrypted;
cAesCfb128Decryptor m_Decryptor;
cAesCfb128Encryptor m_Encryptor;
/** The logfile where the comm is logged, when g_ShouldLogComm is true */
cFile m_CommLogFile;
} ; } ;

View File

@ -79,7 +79,7 @@ static const UInt32 OFF_HAND = 1;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// cProtocol_1_9_0: // cProtocol_1_9_0:
cProtocol_1_9_0::cProtocol_1_9_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : cProtocol_1_9_0::cProtocol_1_9_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, State a_State) :
Super(a_Client, a_ServerAddress, a_ServerPort, a_State), Super(a_Client, a_ServerAddress, a_ServerPort, a_State),
m_IsTeleportIdConfirmed(true), m_IsTeleportIdConfirmed(true),
m_OutstandingTeleportId(0) m_OutstandingTeleportId(0)
@ -575,9 +575,8 @@ bool cProtocol_1_9_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy
{ {
switch (m_State) switch (m_State)
{ {
case 1: case State::Status:
{ {
// Status
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true; case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true;
@ -586,9 +585,8 @@ bool cProtocol_1_9_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy
break; break;
} }
case 2: case State::Login:
{ {
// Login
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandlePacketLoginStart (a_ByteBuffer); return true; case 0x00: HandlePacketLoginStart (a_ByteBuffer); return true;
@ -597,9 +595,8 @@ bool cProtocol_1_9_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy
break; break;
} }
case 3: case State::Game:
{ {
// Game
switch (a_PacketType) switch (a_PacketType)
{ {
case 0x00: HandleConfirmTeleport (a_ByteBuffer); return true; case 0x00: HandleConfirmTeleport (a_ByteBuffer); return true;
@ -643,10 +640,10 @@ bool cProtocol_1_9_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy
// Cannot kick the client - we don't know this state and thus the packet number for the kick packet // Cannot kick the client - we don't know this state and thus the packet number for the kick packet
// Switch to a state when all further packets are silently ignored: // Switch to a state when all further packets are silently ignored:
m_State = 255; m_State = State::Invalid;
return false; return false;
} }
case 255: case State::Invalid:
{ {
// This is the state used for "not processing packets anymore" when we receive a bad packet from a client. // This is the state used for "not processing packets anymore" when we receive a bad packet from a client.
// Do not output anything (the caller will do that for us), just return failure // Do not output anything (the caller will do that for us), just return failure

View File

@ -37,7 +37,7 @@ class cProtocol_1_9_0:
public: public:
cProtocol_1_9_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); cProtocol_1_9_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, State a_State);
/** 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;