Пример #1
0
        public void Publish(string topic, byte[] data, QoSLevel qosLevel, bool retain, ushort packetId)
        {
            if (packetId == 0)
            {
                throw new ArgumentException(nameof(packetId));
            }

            var publish =
                new MqttPacket(PacketType.Publish)
            {
                LastWillTopic    = topic,
                Data             = data,
                IsDuplicated     = false,
                LastWillQosLevel = qosLevel,
                LastWillRetain   = retain,
                PacketId         = packetId
            };


            var enqueue = this.PushPacketToQueue(publish, PacketDirection.ToServer);

            if (!enqueue)
            {
                throw new Exception("Packet full.");
            }
        }
Пример #2
0
 private void ReadFixedHeader(byte fixedHeader)
 {
     _retain      = (fixedHeader & 0x01) != 0;
     _qosLevel    = (QoSLevel)((fixedHeader & 0x06) >> 1);
     _dup         = (fixedHeader & 0x08) != 0;
     _messageType = (MqttMessageType)((fixedHeader & 0xf0) >> 4);
 }
Пример #3
0
 public MqttMessage(MqttMessageType messageType, QoSLevel qosLevel, bool retain, bool dup)
 {
     _qosLevel    = qosLevel;
     _messageType = messageType;
     _retain      = retain;
     _dup         = dup;
 }
Пример #4
0
 private void ReadFixedHeader(byte fixedHeader)
 {
     _retain = (fixedHeader & 0x01) != 0;
     _qosLevel = (QoSLevel)((fixedHeader & 0x06) >> 1);
     _dup = (fixedHeader & 0x08) != 0;
     _messageType = (MqttMessageType)((fixedHeader & 0xf0) >> 4);
 }
Пример #5
0
 public MqttMessage(MqttMessageType messageType, QoSLevel qosLevel, bool retain, bool dup)
 {
     _qosLevel = qosLevel;
     _messageType = messageType;
     _retain = retain;
     _dup = dup;
 }
Пример #6
0
 public Will(QoSLevel qos, bool retain, string topic, EncodedDataField message)
 {
     QoS     = qos;
     Retain  = retain;
     Topic   = topic;
     Message = message;
 }
Пример #7
0
 public MqttConnectMessage(string clientId, bool cleanSession, ushort keepAliveTimer, string willTopic, byte[] willMessage, QoSLevel willQos, bool willRetain)
     : this(clientId, cleanSession, keepAliveTimer)
 {
     _willTopic = willTopic;
     if (!string.IsNullOrEmpty(_willTopic))
     {
         _willQos = willQos;
         _willRetain = willRetain;
         _willMessage = willMessage;
         _hasWill = true;
     }
 }
Пример #8
0
 public MqttConnectMessage(string clientId, bool cleanSession, ushort keepAliveTimer, string willTopic, byte[] willMessage, QoSLevel willQos, bool willRetain)
     : this(clientId, cleanSession, keepAliveTimer)
 {
     _willTopic = willTopic;
     if (!string.IsNullOrEmpty(_willTopic))
     {
         _willQos     = willQos;
         _willRetain  = willRetain;
         _willMessage = willMessage;
         _hasWill     = true;
     }
 }
Пример #9
0
        private void QoSTest(QoSLevel Level)
        {
            ManualResetEvent Received  = new ManualResetEvent(false);
            ManualResetEvent Delivered = new ManualResetEvent(false);

            this.client2.OnNormalMessage += (sender, e) => Received.Set();

            this.client1.SendMessage(Level, MessageType.Normal, this.client2.FullJID, string.Empty, "Hello", string.Empty, "en",
                                     string.Empty, string.Empty, (sender, e) => Delivered.Set(), null);

            Assert.IsTrue(Delivered.WaitOne(10000), "Message not delivered properly.");
            Assert.IsTrue(Received.WaitOne(10000), "Message not received properly.");
        }
Пример #10
0
        private MqttQualityOfServiceLevel ConvertQosLevel(QoSLevel qosLvl)
        {
            switch (qosLvl)
            {
            case QoSLevel.AtMostOnce: return(MqttQualityOfServiceLevel.AtMostOnce);

            case QoSLevel.AtLeastOnce: return(MqttQualityOfServiceLevel.AtLeastOnce);

            case QoSLevel.ExactlyOnce: return(MqttQualityOfServiceLevel.ExactlyOnce);

            default:
                throw new ArgumentOutOfRangeException(nameof(qosLvl), qosLvl, null);
            }
        }
Пример #11
0
        public MqttConnectionInfo(string clientId, string host, int port)
        {
            if (string.IsNullOrEmpty(clientId))
                throw new ArgumentNullException("clientId");
            if (string.IsNullOrEmpty(host))
                throw new ArgumentNullException("host");
            if (port <= 0)
                throw new ArgumentException("Invalid port");

            _host = host;
            _port = port;
            _clientId = clientId;
            _willQoS = QoSLevel.AtMostOnce;
            _keepAliveInterval = 10;
        }
Пример #12
0
        public MqttConnectionInfo(string clientId, string host, int port)
        {
            if (string.IsNullOrEmpty(clientId))
            {
                throw new ArgumentNullException("clientId");
            }
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentNullException("host");
            }
            if (port <= 0)
            {
                throw new ArgumentException("Invalid port");
            }

            _host              = host;
            _port              = port;
            _clientId          = clientId;
            _willQoS           = QoSLevel.AtMostOnce;
            _keepAliveInterval = 10;
        }
Пример #13
0
        private static void ConvertToQualityOfServiceLevel(MqttApplicationMessageBuilder builder, QoSLevel qos)
        {
            switch (qos)
            {
            case QoSLevel.AtMostOnce:
                builder.WithAtMostOnceQoS();
                break;

            case QoSLevel.AtLeastOnce:
                builder.WithAtLeastOnceQoS();
                break;

            case QoSLevel.ExactlyOnce:
                builder.WithExactlyOnceQoS();
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(qos), qos, null);
            }
        }
Пример #14
0
 internal static ControlPacketFlags GetPublishFlags(bool duplicate, QoSLevel qos, bool retain)
 {
     return(new ControlPacketFlags(duplicate, qos, retain));
 }
Пример #15
0
 protected ControlPacketFlags(bool duplicate, QoSLevel qos, bool retain)
 {
     Duplicate = duplicate;
     QoS       = qos;
     Retain    = retain;
 }
Пример #16
0
 public static FixedHeader CreatePublishHeader(bool duplicate, QoSLevel qos, bool retain, UInt32 remainingLength)
 {
     return(new FixedHeader(ControlPacketType.PUBLISH, ControlPacketFlags.GetPublishFlags(duplicate, qos, retain), remainingLength));
 }