MQTTnet常见问题(一) - ReturnCode与ReasonCode

目录

文章目录

    • 目录
    • 什么是 “ReturnCode 已过时”
      • 解决方案
      • 附:源码
        • 枚举类型MqttConnectReasonCode的源码
        • 枚举类型MqttConnectReturnCode的源码
      • 用于转换MqttConnectReasonCodeConverter的源码

什么是 “ReturnCode 已过时”

警告:“MqttConnectionValidatorContext.ReturnCode”已过时:“Use ReasonCode instead. It is MQTTv5 only but will be converted to a valid ReturnCode.”

出现这个提示的原因是MQTT V5.0 版协议更新了标记返回状态的标识符。
MQTTnet中的表现则是弃用了MQTT连接时标志连接状态(返回码)的一个枚举类型
MqttConnectReturnCode,改由描述更加全面的枚举类型MqttConnectReasonCode代替,新的返回原因码类型中包含了更加具体的状态标记。

我们可以看一下MQTT V5.0版协议中对返回原因码的定义
MQTTnet常见问题(一) - ReturnCode与ReasonCode_第1张图片

官方文档链接: https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html
返回原因码的定义位于2.4章节

解决方案

虽然此警告置之不理并不会导致程序的异常(MQTTnet已内置了不同版本下返回标识的转换),但若是能获取到更加详尽的状态信息为什么要拒绝呢?
在服务端后返回状态码时采用ReasonCode,参考附录中的MqttConnectReasonCode源码注释,返回对应的状态信息。

附:源码

枚举类型MqttConnectReasonCode的源码

https://github.com/chkr1011/MQTTnet/blob/master/Source/MQTTnet/Protocol/MqttConnectReasonCode.cs

namespace MQTTnet.Protocol
{
    public enum MqttConnectReasonCode
    {
        Success = 0,                        // 成功
        UnspecifiedError = 128,             // 未知的错误
        MalformedPacket = 129,              // 数据包缺失
        ProtocolError = 130,                // 协议有误
        ImplementationSpecificError = 131,  // 实现细节有误
        UnsupportedProtocolVersion = 132,   // 协议版本不支持
        ClientIdentifierNotValid = 133,     // 客户端标识码无效
        BadUserNameOrPassword = 134,        // 错误的用户名或密码
        NotAuthorized = 135,                // 未授权
        ServerUnavailable = 136,            // 服务器不可用
        ServerBusy = 137,                   // 服务器正忙
        Banned = 138,                       // 已禁用
        BadAuthenticationMethod = 140,      // 验证方法有误
        TopicNameInvalid = 144,             // 无效的Topic名称
        PacketTooLarge = 149,               // 数据包过大
        QuotaExceeded = 151,                // 超出配额
        PayloadFormatInvalid = 153,         // Payload格式有误
        RetainNotSupported = 154,           // 不支持保留
        QoSNotSupported = 155,              // 不支持QoS
        UseAnotherServer = 156,             // 使用其他服务器
        ServerMoved = 157,                  // 服务器移动
        ConnectionRateExceeded = 159        // 连接速率超出限定
    }
}

枚举类型MqttConnectReturnCode的源码

https://github.com/chkr1011/MQTTnet/blob/master/Source/MQTTnet/Protocol/MqttConnectReturnCode.cs

namespace MQTTnet.Protocol
{
    public enum MqttConnectReturnCode
    {
        ConnectionAccepted = 0x00,
        ConnectionRefusedUnacceptableProtocolVersion = 0x01,
        ConnectionRefusedIdentifierRejected = 0x02,
        ConnectionRefusedServerUnavailable = 0x03,
        ConnectionRefusedBadUsernameOrPassword = 0x04,
        ConnectionRefusedNotAuthorized = 0x05
    }
}

用于转换MqttConnectReasonCodeConverter的源码

https://github.com/chkr1011/MQTTnet/blob/master/Source/MQTTnet/Protocol/MqttConnectReasonCodeConverter.cs

public class MqttConnectReasonCodeConverter
    {
        public MqttConnectReturnCode ToConnectReturnCode(MqttConnectReasonCode reasonCode)
        {
            switch (reasonCode)
            {
                case MqttConnectReasonCode.Success:
                    {
                        return MqttConnectReturnCode.ConnectionAccepted;
                    }

                case MqttConnectReasonCode.NotAuthorized:
                    {
                        return MqttConnectReturnCode.ConnectionRefusedNotAuthorized;
                    }

                case MqttConnectReasonCode.BadUserNameOrPassword:
                    {
                        return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
                    }

                case MqttConnectReasonCode.ClientIdentifierNotValid:
                    {
                        return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
                    }

                case MqttConnectReasonCode.UnsupportedProtocolVersion:
                    {
                        return MqttConnectReturnCode.ConnectionRefusedUnacceptableProtocolVersion;
                    }

                case MqttConnectReasonCode.ServerUnavailable:
                case MqttConnectReasonCode.ServerBusy:
                case MqttConnectReasonCode.ServerMoved:
                    {
                        return MqttConnectReturnCode.ConnectionRefusedServerUnavailable;
                    }

                default:
                    {
                        throw new MqttProtocolViolationException("Unable to convert connect reason code (MQTTv5) to return code (MQTTv3).");
                    }
            }
        }

        public MqttConnectReasonCode ToConnectReasonCode(MqttConnectReturnCode returnCode)
        {
            switch (returnCode)
            {
                case MqttConnectReturnCode.ConnectionAccepted:
                    {
                        return MqttConnectReasonCode.Success;
                    }

                case MqttConnectReturnCode.ConnectionRefusedUnacceptableProtocolVersion:
                    {
                        return MqttConnectReasonCode.UnsupportedProtocolVersion;
                    }

                case MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword:
                    {
                        return MqttConnectReasonCode.BadUserNameOrPassword;
                    }

                case MqttConnectReturnCode.ConnectionRefusedIdentifierRejected:
                    {
                        return MqttConnectReasonCode.ClientIdentifierNotValid;
                    }

                case MqttConnectReturnCode.ConnectionRefusedServerUnavailable:
                    {
                        return MqttConnectReasonCode.ServerUnavailable;
                    }

                case MqttConnectReturnCode.ConnectionRefusedNotAuthorized:
                    {
                        return MqttConnectReasonCode.NotAuthorized;
                    }

                default:
                    {
                        throw new MqttProtocolViolationException("Unable to convert connect reason code (MQTTv5) to return code (MQTTv3).");
                    }
            }
        }
    }

你可能感兴趣的:(c#,MQTT,MQTT,c#,MQTTnet,.NET,.NETCore)