@@ -193,9 +193,9 @@ protected void decode(
193193    }
194194
195195    private  void  handlePacketTooLarge (
196-             final  @ com . hivemq . extension . sdk . api . annotations . NotNull  ByteBuf  buf ,
197-             final  @ com . hivemq . extension . sdk . api . annotations . NotNull  MessageType  messageType ,
198-             final  @ com . hivemq . extension . sdk . api . annotations . NotNull  ClientConnection  clientConnectionContext ) {
196+             final  @ NotNull  ByteBuf  buf ,
197+             final  @ NotNull  MessageType  messageType ,
198+             final  @ NotNull  ClientConnection  clientConnectionContext ) {
199199        //connack with PACKET_TOO_LARGE for Mqtt5 
200200        if  (messageType  == MessageType .CONNECT ) {
201201            // Theoretically, remaining length could be too short to read the protocol version. 
@@ -236,18 +236,6 @@ private void handlePacketTooLarge(
236236            final  int  packetSize ,
237237            final  int  remainingLength ) {
238238
239-         //this is the message size HiveMQ allows for incoming messages 
240-         if  (packetSize  > maxPacketSize ) {
241-             connectDecoder .decodeProtocolVersion (clientConnection , buf );
242-             mqttConnacker .connackError (clientConnection .getChannel (),
243-                     "A client (IP: {}) connect packet exceeded the maximum permissible size." ,
244-                     "Sent CONNECT exceeded the maximum permissible size" ,
245-                     Mqtt5ConnAckReasonCode .PACKET_TOO_LARGE ,
246-                     ReasonStrings .CONNACK_PACKET_TOO_LARGE );
247- 
248-             return  null ;
249-         }
250- 
251239        // Check if the client is already connected 
252240        if  (clientConnection .getProtocolVersion () != null ) {
253241            mqttServerDisconnector .disconnect (clientConnection .getChannel (),
@@ -283,22 +271,6 @@ private void handlePacketTooLarge(
283271
284272        final  ProtocolVersion  protocolVersion  = clientConnection .getProtocolVersion ();
285273
286-         //this is the message size HiveMQ allows for incoming messages 
287-         if  (packetSize  > maxPacketSize ) {
288- 
289-             //force channel close for Mqtt3.1, Mqtt3.1.1 and null (before connect) 
290-             final  boolean  forceClose  = protocolVersion  != ProtocolVersion .MQTTv5 ;
291-             mqttServerDisconnector .disconnect (clientConnection .getChannel (),
292-                     "A client (IP: {}) sent a message, that was bigger than the maximum message size. Disconnecting client." ,
293-                     "Sent a message that was bigger than the maximum size" ,
294-                     Mqtt5DisconnectReasonCode .PACKET_TOO_LARGE ,
295-                     ReasonStrings .DISCONNECT_PACKET_TOO_LARGE_MESSAGE ,
296-                     Mqtt5UserProperties .NO_USER_PROPERTIES ,
297-                     false ,
298-                     forceClose );
299-             return  null ;
300-         }
301- 
302274        // Check if client is connected 
303275        if  (protocolVersion  == null ) {
304276            mqttServerDisconnector .logAndClose (clientConnection .getChannel (),
@@ -317,58 +289,45 @@ private void handlePacketTooLarge(
317289        final  MqttDecoder <?> decoder  = mqttDecoders .decoder (messageType , protocolVersion );
318290        if  (decoder  != null ) {
319291            return  decoder .decode (clientConnection , messageBuffer , fixedHeader );
320-         } else  {
321-             switch  (messageType ) {
322-                 case  RESERVED_ZERO :
323-                     mqttServerDisconnector .disconnect (clientConnection .getChannel (),
324-                             "A client (IP: {}) sent a message with an invalid message type '0'. This message type is reserved. Disconnecting client." ,
325-                             "Sent a message with message type '0'" ,
326-                             Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
327-                             ReasonStrings .DISCONNECT_MESSAGE_TYPE_ZERO );
328-                     return  null ;
329-                 case  CONNACK :
330-                     mqttServerDisconnector .disconnect (clientConnection .getChannel (),
331-                             "A client (IP: {}) sent a CONNACK message. This is invalid because clients are not allowed to send CONNACKs. Disconnecting client." ,
332-                             "Sent a CONNACK message" ,
333-                             Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
334-                             ReasonStrings .DISCONNECT_CONNACK_RECEIVED );
335-                     return  null ;
336-                 case  SUBACK :
337-                     mqttServerDisconnector .disconnect (clientConnection .getChannel (),
338-                             "A client (IP: {}) sent a SUBACK message. This is invalid because clients are not allowed to send SUBACKs. Disconnecting client." ,
339-                             "Sent a SUBACK message" ,
340-                             Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
341-                             ReasonStrings .DISCONNECT_SUBACK_RECEIVED );
342-                     return  null ;
343-                 case  UNSUBACK :
344-                     mqttServerDisconnector .disconnect (clientConnection .getChannel (),
345-                             "A client (IP: {}) sent a UNSUBACK message. This is invalid because clients are not allowed to send UNSUBACKs. Disconnecting client." ,
346-                             "Sent a UNSUBACK message" ,
347-                             Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
348-                             ReasonStrings .DISCONNECT_UNSUBACK_RECEIVED );
349-                     return  null ;
350-                 case  PINGRESP :
351-                     mqttServerDisconnector .disconnect (clientConnection .getChannel (),
352-                             "A client (IP: {}) sent a PINGRESP message. This is invalid because clients are not allowed to send PINGRESPs. Disconnecting client." ,
353-                             "Sent a PINGRESP message" ,
354-                             Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
355-                             ReasonStrings .DISCONNECT_PINGRESP_RECEIVED );
356-                     return  null ;
357-                 case  AUTH :
358-                     mqttServerDisconnector .disconnect (clientConnection .getChannel (),
359-                             "A client (IP: {}) sent a message with an invalid message type '15'. This message type is reserved. Disconnecting client." ,
360-                             "Sent a message with message type '15'" ,
361-                             Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
362-                             ReasonStrings .DISCONNECT_MESSAGE_TYPE_FIFTEEN );
363-                     return  null ;
364-                 default :
365-                     mqttServerDisconnector .disconnect (clientConnection .getChannel (),
366-                             "A client (IP: {}) connected but the message type could not get determined. Disconnecting client." ,
367-                             "Sent a message with invalid message type" ,
368-                             Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
369-                             ReasonStrings .DISCONNECT_MESSAGE_TYPE_INVALID );
370-                     return  null ;
371-             }
372292        }
293+ 
294+         switch  (messageType ) {
295+             case  RESERVED_ZERO  -> mqttServerDisconnector .disconnect (clientConnection .getChannel (),
296+                     "A client (IP: {}) sent a message with an invalid message type '0'. This message type is reserved. Disconnecting client." ,
297+                     "Sent a message with message type '0'" ,
298+                     Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
299+                     ReasonStrings .DISCONNECT_MESSAGE_TYPE_ZERO );
300+             case  CONNACK  -> mqttServerDisconnector .disconnect (clientConnection .getChannel (),
301+                     "A client (IP: {}) sent a CONNACK message. This is invalid because clients are not allowed to send CONNACKs. Disconnecting client." ,
302+                     "Sent a CONNACK message" ,
303+                     Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
304+                     ReasonStrings .DISCONNECT_CONNACK_RECEIVED );
305+             case  SUBACK  -> mqttServerDisconnector .disconnect (clientConnection .getChannel (),
306+                     "A client (IP: {}) sent a SUBACK message. This is invalid because clients are not allowed to send SUBACKs. Disconnecting client." ,
307+                     "Sent a SUBACK message" ,
308+                     Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
309+                     ReasonStrings .DISCONNECT_SUBACK_RECEIVED );
310+             case  UNSUBACK  -> mqttServerDisconnector .disconnect (clientConnection .getChannel (),
311+                     "A client (IP: {}) sent a UNSUBACK message. This is invalid because clients are not allowed to send UNSUBACKs. Disconnecting client." ,
312+                     "Sent a UNSUBACK message" ,
313+                     Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
314+                     ReasonStrings .DISCONNECT_UNSUBACK_RECEIVED );
315+             case  PINGRESP  -> mqttServerDisconnector .disconnect (clientConnection .getChannel (),
316+                     "A client (IP: {}) sent a PINGRESP message. This is invalid because clients are not allowed to send PINGRESPs. Disconnecting client." ,
317+                     "Sent a PINGRESP message" ,
318+                     Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
319+                     ReasonStrings .DISCONNECT_PINGRESP_RECEIVED );
320+             case  AUTH  -> mqttServerDisconnector .disconnect (clientConnection .getChannel (),
321+                     "A client (IP: {}) sent a message with an invalid message type '15'. This message type is reserved. Disconnecting client." ,
322+                     "Sent a message with message type '15'" ,
323+                     Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
324+                     ReasonStrings .DISCONNECT_MESSAGE_TYPE_FIFTEEN );
325+             default  -> mqttServerDisconnector .disconnect (clientConnection .getChannel (),
326+                     "A client (IP: {}) connected but the message type could not get determined. Disconnecting client." ,
327+                     "Sent a message with invalid message type" ,
328+                     Mqtt5DisconnectReasonCode .PROTOCOL_ERROR ,
329+                     ReasonStrings .DISCONNECT_MESSAGE_TYPE_INVALID );
330+         }
331+         return  null ;
373332    }
374333}
0 commit comments