Cuberite
A lightweight, fast and extensible game server for Minecraft
ProtocolRecognizer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Protocol.h"
4 
5 
6 
7 
8 
9 // Adjust these if a new protocol is added or an old one is removed:
10 #define MCS_CLIENT_VERSIONS "1.8.x-1.14.4"
11 #define MCS_PROTOCOL_VERSIONS "47, 107, 108, 109, 110, 210, 315, 316, 335, 338, 340, 393, 401, 477, 480, 485, 490, 498"
12 #define MCS_LATEST_PROTOCOL_VERSION 498
13 
14 
15 
16 
17 
21 {
22 public:
23 
25 
27  static AString GetVersionTextFromInt(cProtocol::Version a_ProtocolVersion);
28 
31  {
32  return m_Protocol != nullptr;
33  }
34 
36  auto & operator->()
37  {
38  return m_Protocol;
39  }
40 
43  void HandleIncomingData(cClientHandle & a_Client, ContiguousByteBuffer & a_Data);
44 
47 
50  void SendDisconnect(cClientHandle & a_Client, const AString & a_Reason);
51 
52 private:
53 
58 
61 
62  /* Checks if incoming data is an HTTP request and handles it if it is. */
63  bool TryHandleHTTPRequest(cClientHandle & a_Client, ContiguousByteBuffer & a_Data);
64 
67  std::unique_ptr<cProtocol> TryRecognizeLengthedProtocol(cClientHandle & a_Client);
68 
71  static void SendPacket(cClientHandle & a_Client, cByteBuffer & a_OutPacketBuffer);
72 
74  static UInt32 GetPacketID(cProtocol::ePacketType a_PacketType);
75 
76  /* Status handler for unrecognised versions. */
78 
79  /* Ping handler for unrecognised versions. */
80  void HandlePacketStatusPing(cClientHandle & a_Client);
81 
84 
87  std::unique_ptr<cProtocol> m_Protocol;
88 
91 } ;
unsigned int UInt32
Definition: Globals.h:157
std::basic_string_view< std::byte > ContiguousByteBufferView
Definition: Globals.h:376
std::basic_string< std::byte > ContiguousByteBuffer
Definition: Globals.h:375
std::string AString
Definition: StringUtils.h:11
An object that can store incoming bytes and lets its clients read the bytes sequentially The bytes ar...
Definition: ByteBuffer.h:32
ePacketType
Logical types of outgoing packets.
Definition: Protocol.h:57
Version
The protocol version number, received from the client in the Handshake packet.
Definition: Protocol.h:335
Meta-protocol that recognizes multiple protocol versions, creates the specific protocol version insta...
bool TryHandleHTTPRequest(cClientHandle &a_Client, ContiguousByteBuffer &a_Data)
cByteBuffer m_Buffer
Buffer for received protocol data.
void HandlePacketStatusPing(cClientHandle &a_Client)
void HandleOutgoingData(ContiguousByteBuffer &a_Data)
Allows the protocol (if any) to do a final pass on outgiong data, possibly modifying the provided buf...
void HandleIncomingData(cClientHandle &a_Client, ContiguousByteBuffer &a_Data)
Directs incoming protocol data along the correct pathway, depending on the state of the version recog...
bool m_WaitingForData
If we're still waiting for data required for version recognition to arrive.
void HandleIncomingDataInOldPingResponseStage(cClientHandle &a_Client, ContiguousByteBufferView a_Data)
Handles and responds to unsupported clients sending pings.
static UInt32 GetPacketID(cProtocol::ePacketType a_PacketType)
Returns the protocol-specific packet ID given the protocol-agnostic packet enum.
static void SendPacket(cClientHandle &a_Client, cByteBuffer &a_OutPacketBuffer)
Sends one packet inside a cByteBuffer.
void SendDisconnect(cClientHandle &a_Client, const AString &a_Reason)
Sends a disconnect to the client as a result of a recognition error.
auto & operator->()
Convenience overload to enable redirecting sends to the underlying implementation.
void HandlePacketStatusRequest(cClientHandle &a_Client)
bool VersionRecognitionSuccessful()
Returns if we contain a concrete protocol corresponding to the client's protocol version.
static AString GetVersionTextFromInt(cProtocol::Version a_ProtocolVersion)
Translates protocol version number into protocol version text: 49 -> "1.4.4".
std::unique_ptr< cProtocol > m_Protocol
The actual protocol implementation.
std::unique_ptr< cProtocol > TryRecognizeLengthedProtocol(cClientHandle &a_Client)
Tries to recognize a protocol in the lengthed family (1.7+), based on m_Buffer.
void HandleIncomingDataInRecognitionStage(cClientHandle &a_Client, ContiguousByteBuffer &a_Data)
Handles data reception in a newly-created client handle that doesn't yet have a known protocol.