From e9265b1d0129e56b9b665019697055bd63229fde Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 10 Jul 2021 21:04:49 +0100 Subject: Symmetry in MultiVersionProtocol to fix a crash (#5261) + Add HandleOutgoingData, which tests for m_Protocol before calling DataPrepared. * Change std::function to bool + if/else to handle incoming data; it's almost certainly faster. * Fixes #5260 --- src/Protocol/ProtocolRecognizer.h | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/Protocol/ProtocolRecognizer.h') diff --git a/src/Protocol/ProtocolRecognizer.h b/src/Protocol/ProtocolRecognizer.h index 03b379f17..0a923e78f 100644 --- a/src/Protocol/ProtocolRecognizer.h +++ b/src/Protocol/ProtocolRecognizer.h @@ -19,9 +19,6 @@ protocol version instance and redirects everything to it. */ class cMultiVersionProtocol { - // Work around the style checker complaining about && in template. - using OwnedContiguousByteBuffer = ContiguousByteBuffer &&; - public: cMultiVersionProtocol(); @@ -41,8 +38,12 @@ public: return m_Protocol; } - /** The function that's responsible for processing incoming protocol data. */ - std::function HandleIncomingData; + /** Directs incoming protocol data along the correct pathway, depending on the state of the version recognition process. + The protocol modifies the provided buffer in-place. */ + void HandleIncomingData(cClientHandle & a_Client, ContiguousByteBuffer & a_Data); + + /** Allows the protocol (if any) to do a final pass on outgiong data, possibly modifying the provided buffer in-place. */ + void HandleOutgoingData(ContiguousByteBuffer & a_Data); /** Sends a disconnect to the client as a result of a recognition error. This function can be used to disconnect before any protocol has been recognised. */ @@ -53,7 +54,7 @@ private: /** Handles data reception in a newly-created client handle that doesn't yet have a known protocol. a_Data contains a view of data that were just received. Tries to recognize a protocol, populate m_Protocol, and transitions to another mode depending on success. */ - void HandleIncomingDataInRecognitionStage(cClientHandle & a_Client, ContiguousByteBuffer && a_Data); + void HandleIncomingDataInRecognitionStage(cClientHandle & a_Client, ContiguousByteBuffer & a_Data); /** Handles and responds to unsupported clients sending pings. */ void HandleIncomingDataInOldPingResponseStage(cClientHandle & a_Client, ContiguousByteBufferView a_Data); @@ -75,11 +76,13 @@ private: /* Ping handler for unrecognised versions. */ void HandlePacketStatusPing(cClientHandle & a_Client, cByteBuffer & a_Out); + /** Buffer for received protocol data. */ + cByteBuffer m_Buffer; + /** The actual protocol implementation. Created when recognition of the client version succeeds with a version we support. */ std::unique_ptr m_Protocol; - /** Buffer for received protocol data. */ - cByteBuffer m_Buffer; - + /** If we're still waiting for data required for version recognition to arrive. */ + bool m_WaitingForData; } ; -- cgit v1.2.3