/// <summary>
 /// Constructs a PublishArrivedArgs object
 /// </summary>
 /// <param name="topic">Source of message</param>
 /// <param name="payload">Message body</param>
 /// <param name="retained">Whether or not the message is retained</param>
 public PublishArrivedArgs(string topic, MqttPayload payload, bool retained, QoS qos)
 {
   _topic = topic;
   _payload = payload;
   _retained = retained;
   _qos = qos;
 }
示例#2
0
        // TODO: Add a constructor containing WillTopic and WillMessage
        public MqttConnectMessage(
            string clientID, string username, string password, ushort keepAlive,
            string willTopic, byte[] willPayload,
            QoS willQos, bool willRetained, bool cleanStart
            )
            : base(MessageType.CONNECT)
        {
            SetConnectVariableHeaderCommon(clientID, username, password, keepAlive);

            _containsWill = true;
            _willTopic = willTopic;
            _willPayload = willPayload;

            _connectFlags = (byte)(
              0x04 | // LWT enabled
              (willRetained ? 0x20 : 0) | // LWT is retained?
              (cleanStart ? 0x02 : 0) | // Clean Start
              (_containsPassword ? 0x40 : 0) |
                (_containsUsername ? 0x80 : 0) |
              ((byte)willQos) << 3        // LWT QoS
            );

            base.variableHeaderLength += (
              _willTopic.Length +
              _willPayload.Length +
              4
            );
        }
示例#3
0
        public async Task <PostResult> PostMessage(string topic, byte[] data, QoS qos)
        {
            FrameBuilder bld = new FrameBuilder();

            bld.WriteByte((byte)(qos == QoS.BestEffort ? 0x00 : 0x01));
            // ToDo: Generate Frame Number to receive ACK.
            currentPacketId++;
            bld.WriteArray(BitConverter.GetBytes(currentPacketId));
            bld.WriteString(topic);
            bld.WriteMultiByte(data.Length);
            bld.WriteArray(data);

            if (qos == QoS.GuaranteedDelivery)
            {
                if (!syncSendInProcess.WaitOne(3000))
                {
                    return(PostResult.DispatchError);
                }
                waitForPacketId = currentPacketId;
            }

            socket.Send(bld.Build(FrameType.ChannelEvent));

            if (qos == QoS.GuaranteedDelivery)
            {
                var replyReceived = waitQosEvent.WaitOne(3000);

                syncSendInProcess.ReleaseMutex();
                return(replyReceived ? PostResult.Delivered
                                     : PostResult.DeliveryError);
            }

            return(PostResult.Dispatched);
        }
示例#4
0
        public void subscribe(String topic, QoS qos, Callback cb) //throws IOException
        {
            SubscribeMessage msg = new SubscribeMessage(topic, qos, this);

            msg.setMessageId(getNextMessageId());
            sendCallbackMessage(msg, cb);
        }
示例#5
0
 private void timerQuality_Elapsed(object sender, ElapsedEventArgs e)
 {
     try
     {
         if (this.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
         {
             this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                                         new EventHandler <ElapsedEventArgs>(this.timerQuality_Elapsed), sender, new object[] { e });
             return;
         }
         if (this.AVSession != null && this.AVSession.IsActive)
         {
             QoS qos = this.AVSession.GetVideoQualityInfo();
             if (qos != null)
             {
                 this.labelQuality.Content = string.Format("Quality: {0}%", (uint)(qos.getQavg() * 100));
                 if (this.qosWindow != null && this.qosWindow.IsVisible)
                 {
                     this.qosWindow.Info = qos;
                 }
             }
         }
     }
     catch (Exception ex)
     {
         LOG.Error(ex);
     }
 }
示例#6
0
        public Subscription Add(string topic, QoS qos)
        {
            var s = new Subscription(topic, qos);

            Add(s);
            return(s);
        }
示例#7
0
 public MqttParcel(string topic, byte[] payload, QoS qos, bool retained)
 {
     this.topic    = topic;
     this.payload  = new MqttPayload(payload, 0);
     this.qos      = qos;
     this.retained = retained;
 }
示例#8
0
 public MqttParcel(string topic, MqttPayload payload, QoS qos, bool retained)
 {
     this.topic    = topic;
     this.payload  = payload;
     this.qos      = qos;
     this.retained = retained;
 }
        // TODO: Add a constructor containing WillTopic and WillMessage
        public MqttConnectMessage(
            string clientID, string username, string password, ushort keepAlive,
            string willTopic, byte[] willPayload,
            QoS willQos, bool willRetained, bool cleanStart
            )
            : base(MessageType.CONNECT)
        {
            SetConnectVariableHeaderCommon(clientID, username, password, keepAlive);

            _containsWill = true;
            _willTopic    = willTopic;
            _willPayload  = willPayload;

            _connectFlags = (byte)(
                0x04 |                      // LWT enabled
                (willRetained ? 0x20 : 0) | // LWT is retained?
                (cleanStart ? 0x02 : 0) |   // Clean Start
                (_containsPassword ? 0x40 : 0) |
                (_containsUsername ? 0x80 : 0) |
                ((byte)willQos) << 3      // LWT QoS
                );

            base.variableHeaderLength += (
                _willTopic.Length +
                _willPayload.Length +
                4
                );
        }
示例#10
0
 private void ConstructHeader(byte header)
 {
     msgType     = (MessageType)((header & 0xf0) >> 4);
     isDuplicate = (header & 0x08) != 0;
     msgQos      = (QoS)((header & 0x06) >> 1);
     isRetained  = (header & 0x01) != 0;
 }
 public MqttPublishMessage(string topic, MqttPayload payload,bool retained, QoS qos)
 {
     _topic = topic;
     _payload = payload;
     _retained = retained;
     _qos = qos;
 }
示例#12
0
        private static async Task PublishMessages(IQueueOf<string> queue, QoS qos, int passes)
        {
            var watch = new Stopwatch();
            var counters = new List<long>();

            for (var i = 1; i <= passes; i++)
            {
                Write("Publishing 1000 messages ({0} QoS), pass {1}/{2} -> ", qos.ToString(), i, passes);
                watch.Restart();

                for (var j = 0; j < 1000; j++)
                {
                    await queue.PublishAsync(j.ToString(), qos: qos);
                }
                watch.Stop();
                counters.Add(watch.ElapsedMilliseconds);
                WriteLine(watch.ElapsedMilliseconds);
            }

            var average = counters.Sum() / passes;

            WriteLine("Average time (ms): {0}", average);
            WriteLine("Average rate (msg/s): {0}", Math.Truncate(decimal.Divide(1000, average) * 1000));
            WriteLine();            
        }
示例#13
0
        public void publish(String topic, byte[] message, QoS qos, Callback cb) //throws IOException
        {
            PublishMessage msg = new PublishMessage(topic, message, qos, this);

            msg.setMessageId(getNextMessageId());
            sendCallbackMessage(msg, cb);
        }
示例#14
0
 public void Connect(string willTopic, QoS willQoS, MqttPayload willMsg, bool willRetain, bool cleanStart)
 {
     _keepAlive = 60;
     DoConnect(new MqttConnectMessage(
                   _clientID, _username, _password, _keepAlive, willTopic, willMsg.TrimmedBuffer, willQoS, willRetain, cleanStart
                   ));
 }
示例#15
0
 public MqttParcel(string topic, byte[] payload, QoS qos, bool retained)
 {
   this.topic = topic;
   this.payload = new MqttPayload(payload,0);
   this.qos = qos;
   this.retained = retained;
 }
示例#16
0
 public MqttParcel(string topic, MqttPayload payload, QoS qos, bool retained )
 {
   this.topic = topic;
   this.payload = payload;
   this.qos = qos;
   this.retained = retained;
 }
示例#17
0
 public SubscribeMessage(String topic, QoS topicQos, MqttConnection conn)
     : base(MessageType.SUBSCRIBE, conn)
 {
     setQos(QoS.AT_LEAST_ONCE);
     topics.Add(topic);
     topicQoSs.Add(topicQos);
 }
示例#18
0
 public Header(MessageType type, bool retain, QoS qos, bool dup)
 {
     this.type   = type;
     this.retain = retain;
     this.qos    = qos;
     this.dup    = dup;
 }
示例#19
0
 public Header(byte flags)
 {
     retain = (flags & 1) > 0;
     qos    = (QoS)((Convert.ToInt32(flags) & 0x6) >> 1);
     dup    = (flags & 8) > 0;
     type   = (MessageType)((Convert.ToInt32(flags) >> 4) & 0xF);
 }
示例#20
0
 public PublishMessage(String topic, byte[] data, QoS qos, MqttConnection conn)
     : base(MessageType.PUBLISH, conn)
 {
     this.topic = topic;
     this.data  = data;
     setQos(qos);
 }
 /// <summary>
 /// Constructs a PublishArrivedArgs object
 /// </summary>
 /// <param name="topic">Source of message</param>
 /// <param name="payload">Message body</param>
 /// <param name="retained">Whether or not the message is retained</param>
 public PublishArrivedArgs(string topic, MqttPayload payload, bool retained, QoS qos)
 {
     _topic    = topic;
     _payload  = payload;
     _retained = retained;
     _qos      = qos;
 }
示例#22
0
 public void setWill(String willTopic, String will, QoS willQoS,
                     bool retainWill)
 {
     this.willTopic  = willTopic;
     this.will       = will;
     this.willQoS    = willQoS;
     this.retainWill = retainWill;
 }
示例#23
0
 public MsSuback(QoS qualityOfService, ushort topicId, ushort msgId, MsReturnCode code)
     : base(MsMessageType.SUBACK)
 {
     _qualityOfService = qualityOfService;
     _topicId          = topicId;
     _msgId            = msgId;
     _retCode          = code;
 }
示例#24
0
 /// <summary>Creates an MqttMessage from a data stream</summary>
 /// <param name="header">The first byte of the fixed header of the message</param>
 /// <param name="len">Variable header length</param>
 /// <param name="str">Input stream</param>
 protected MqMessage(byte header, uint len, Stream str) //-V3117
 {
     this.MsgType          = (MessageType)((header & 0xf0) >> 4);
     this.Duplicate        = (header & 0x08) != 0;
     this.QualityOfService = (QoS)((header & 0x06) >> 1);
     this.Retained         = (header & 0x01) != 0;
     variableHeaderLength  = len;
 }
示例#25
0
 protected MqMessage(MessageType msgType)
 {
     this.MsgType          = msgType;
     this.Duplicate        = false;
     this.QualityOfService = QoS.AtMostOnce;
     this.Retained         = false;
     this.MessageID        = 0;
 }
示例#26
0
 private void addQoS(QoS qos)
 {
     if (grantedQoSs == null)
     {
         grantedQoSs = new List <QoS>();
     }
     grantedQoSs.Add(qos);
 }
示例#27
0
 /// <summary>
 /// Creates a new context.
 /// </summary>
 /// <param name="version">The version of MQTT.</param>
 /// <param name="isEmitter">Whether this is our special implementation.</param>
 /// <param name="id">The client id specified in the MQTT connect packet.</param>
 public MqttContext(MqttProtocolVersion version, QoS qos, bool isEmitter, string id, string username)
 {
     this.Version   = version;
     this.QoS       = qos;
     this.IsEmitter = isEmitter;
     this.ClientId  = id;
     this.Username  = username;
 }
示例#28
0
        public Message(MessageType type, QoS qos, bool retain, bool duplicateDelivery)
        {
            FixedHeader = new FixedHeader();

            FixedHeader.MessageType       = type;
            FixedHeader.QoS               = qos;
            FixedHeader.Retain            = retain;
            FixedHeader.DuplicateDelivery = duplicateDelivery;
        }
示例#29
0
        public void Publish(string topic, string data, QoS qos, bool retain)
        {
            //var encoded = Encoding.ASCII.GetBytes(data);
            //Publish(topic, data, qos, retain);
            var encoded = Encoding.UTF8.GetBytes(data);

            Publish(topic, encoded, qos, retain);
            TracingDebug("Send Publish: topic=" + topic + ",content=" + data);
        }
示例#30
0
 /// <summary>
 /// Recycles the packet.
 /// </summary>
 public override void Recycle()
 {
     this.Origin          = null;
     this.Lifetime        = PacketLifetime.Automatic;
     this.ProtocolVersion = MqttProtocolVersion.Unknown;
     this.DupFlag         = false;
     this.QoS             = QoS.AtMostOnce;
     this.Retain          = false;
 }
 private static void Mqtt_MessageReceived(string topic, QoS qos, byte[] payload)
 {
     Debug.WriteLine("<-" + System.Text.Encoding.UTF8.GetString(payload));
     MensajeRecibido(null, new Tools.MensajeRecibido()
     {
         Mensaje = System.Text.Encoding.UTF8.GetString(payload),
         Topico  = topic
     });
 }
示例#32
0
 public override void setQos(QoS qos)
 {
     if (qos != QoS.AT_LEAST_ONCE)
     {
         //throw new IllegalArgumentException(
         //        "SUBSCRIBE is always using QoS-level AT LEAST ONCE. Requested level: "
         //                + qos);
     }
     base.setQos(qos);
 }
示例#33
0
        public Subscription(string topic, QoS qos)
        {
            Validate
            .Begin()
            .IsNotNullOrEmpty(topic)
            .Check();

            TopicName = topic;
            QoS       = qos;
        }
示例#34
0
 public PublishMessage(bool duplicate, bool retain, QoS qos, String topic, byte[] message, short?packetId) :
     base(MessageType.Publish, (packetId.HasValue ? 2: 0) + GetSize(topic) + GetSize(message, false), (byte)((duplicate ? 0x08 : 0) | (retain ? 0x01 : 0) | (byte)qos << 1))
 {
     Duplicate = duplicate;
     Retain    = retain;
     QoS       = qos;
     Topic     = topic;
     payload   = message;
     PacketId  = packetId;
 }
示例#35
0
        public override PostResult DispatchMessage(string topic, byte[] data, QoS qos)
        {
            Console.WriteLine("A message was posted in: " + topic);
            Console.WriteLine(Encoding.UTF8.GetString(data));
            var res = base.DispatchMessage(topic, data, qos);

            base.DispatchMessage("SomeChannel", Encoding.UTF8.GetBytes("This is yet another message."), QoS.BestEffort);

            return(res);
        }
示例#36
0
 public void publish(String topic, byte[][] message, QoS qos, Callback[] cb) //throws IOException
 {
     PublishMessage[] messagesToPublish = new PublishMessage[cb.Length];
     for (int i = 0; i < message.Length; i++)
     {
         messagesToPublish[i] = new PublishMessage(topic, message[i], qos, this);
         messagesToPublish[i].setMessageId(getNextMessageId());
     }
     sendCallbackMessage(messagesToPublish, cb);
 }
示例#37
0
        public MqttPublishMessage( ushort id, string topic, byte[] payload, QoS qos, bool retained )
            : base(MessageType.PUBLISH)
        {
            _topic = topic;
            _payload = payload;
            _messageID = id;

            base.msgQos = qos;
            base.isRetained = retained;

            base.variableHeaderLength =
              2 + GetUTF8StringLength(topic) +    // Topic + length
              (qos == QoS.BestEfforts ? 0 : 2) +  // Message ID for QoS > 0
              payload.Length;                     // Message Payload
        }
        public MqttExtendedackMessage(ulong msgID, string topic, byte[] payload, QoS qos, int ttl, string apn_json)
            : base(MessageType.EXTENDEDACK, 8, msgID)
        {
            _commondId = 7;
            base._messageID = msgID;
            base.msgQos = QoS.AtLeastOnce;

            MemoryStream pay = new MemoryStream();

            pay.WriteByte(0);
            WriteToStream(pay, topic);

            pay.WriteByte(1);
            WriteToStream(pay, payload);

            if (ttl > 0)
            {
                pay.WriteByte(3);
                WriteToStream(pay, ttl.ToString());
            }

            string[] qos2str = {"0", "1", "2"};
            pay.WriteByte(6);
            WriteToStream(pay, qos2str[(int)qos]);

            if (apn_json != null)
            {
                pay.WriteByte(7);
                WriteToStream(pay, apn_json);
            }

            byte[] paybytes = pay.ToArray();
            if (paybytes.Length <= 65535)
            {
                _leftLength = (ushort)paybytes.Length;
                _payload = paybytes;
            }
            else
                throw new ArgumentOutOfRangeException("payload length is longer then 65535.");

            base.variableHeaderLength = 11 + _payload.Length;
        }
示例#39
0
 public ulong Publish2Alias(string alias, MqttPayload payload, QoS qos, int ttl, string apn_json)
 {
     return Publish2(",yta/" + alias, payload, qos, ttl, apn_json);
 }
示例#40
0
        public ulong Publish2(string topic, MqttPayload payload, QoS qos, int ttl, string apn_json)
        {
            if (manager.IsConnected)
            {
                ulong messID = MessageID;

                manager.SendMessage(new MqttExtendedackMessage(messID, topic, payload.TrimmedBuffer, qos, ttl, apn_json));
                return messID;
            }
            else
            {
                throw new MqttNotConnectedException("You need to connect to a broker before trying to Publish");
            }
        }
示例#41
0
 public int Subscribe(string topic, QoS qos)
 {
     return Subscribe(new Subscription(topic, qos));
 }
示例#42
0
 public int Publish(string topic, MqttPayload payload, QoS qos, bool retained)
 {
     if (manager.IsConnected)
     {
         ushort messID = MessageID;
         manager.SendMessage(new MqttPublishMessage(messID, topic, payload.TrimmedBuffer, qos, retained));
         return messID;
     }
     else
     {
         throw new MqttNotConnectedException("You need to connect to a broker before trying to Publish");
     }
 }
示例#43
0
 public void Add(QoS gQoS) {
   grantedQos.Add(gQoS);
 }
示例#44
0
 private void ConstructHeader(byte header)
 {
     msgType = (MessageType)((header & 0xf0) >> 4);
     isDuplicate = (header & 0x08) != 0;
     msgQos = (QoS)((header & 0x06) >> 1);
     isRetained = (header & 0x01) != 0;
 }
示例#45
0
 public ulong PublishToAlias(string alias, MqttPayload payload, QoS qos, bool retained)
 {
     return Publish(",yta/" + alias, payload, qos, retained);
 }
示例#46
0
 public ulong Publish(string topic, MqttPayload payload, QoS qos, bool retained)
 {
     if (manager.IsConnected)
     {
         // Reset the PINGREQ timer as this publish will reset the server's counter
         if (keepAliveTimer != null)
         {
             int kmillis = 1000 * _keepAlive;
             keepAliveTimer.Change(kmillis, kmillis);
         }
         ulong messID = MessageID;
         manager.SendMessage(new MqttPublishMessage(messID, topic, payload.TrimmedBuffer, qos, retained));
         return messID;
     }
     else
     {
         throw new MqttNotConnectedException("You need to connect to a broker before trying to Publish");
     }
 }
 public Subscription(string topic, QoS qos)
 {
     _topic = topic;
     _qos = qos;
 }
示例#48
0
 protected MqMessage(MessageType msgType) {
   this.MsgType = msgType;
   this.Duplicate=false;
   this.QualityOfService=QoS.AtMostOnce;
   this.Retained=false;
   this.MessageID=0;
 }
示例#49
0
 public MsSuback(QoS qualityOfService, ushort topicId, ushort msgId, MsReturnCode code)
   : base(MsMessageType.SUBACK) {
   _qualityOfService=qualityOfService;
   _topicId=topicId;
   _msgId=msgId;
   _retCode=code;
 }
示例#50
0
 public int Subscribe(string topic, QoS qos)
 {
     throw new NotImplementedException();
 }
示例#51
0
 public void Add(string topic, QoS sQoS) {
     _list.Add(new KeyValuePair<string,QoS>(topic, sQoS));
 }
示例#52
0
 public static void MqttPublish(this IXSocketController controller, string topic, object obj, QoS qosLevel = QoS.FireAndForget, bool retain = false)
 {
     var data = Encoding.UTF8.GetBytes(controller.JsonSerializer.SerializeToString(obj));
     Composable.GetExport<IMqttBridge>().PublishToMqttClients(new MqttMsgPublish(topic, data, false, (byte)qosLevel, retain));
 }
示例#53
0
 public static void MqttPublish(this IXSocketController controller, string topic, byte[] data, QoS qosLevel = QoS.FireAndForget, bool retain = false)
 {
     Composable.GetExport<IMqttBridge>().PublishToMqttClients(new MqttMsgPublish(topic, data, false, (byte)qosLevel, retain));
 }
示例#54
0
 public void Subscribe(string topic, QoS sQoS) {
   MqSubscribe msg=new MqSubscribe();
   msg.Add(topic, sQoS);
   Send(msg);
 }
示例#55
0
 public void Connect(string willTopic, QoS willQoS, MqttPayload willMsg, bool willRetain)
 {
     DoConnect(new MqttConnectMessage(
     _clientID, _keepAlive, willTopic, willMsg.TrimmedBuffer, willQoS, willRetain, false
       ));
 }
示例#56
0
 public void Connect(string username, string password,string willTopic, QoS willQoS, MqttPayload willMsg, bool willRetain, bool cleanStart)
 {
     DoConnect(new MqttConnectMessage(
       _clientID, _keepAlive,username,password, willTopic, willMsg.TrimmedBuffer, willQoS, willRetain, cleanStart
     ));
 }
示例#57
0
 /// <summary>Creates an MqttMessage from a data stream</summary>
 /// <param name="header">The first byte of the fixed header of the message</param>
 /// <param name="len">Variable header length</param>
 /// <param name="str">Input stream</param>
 protected MqMessage(byte header, uint len, Stream str) {
   this.MsgType = (MessageType)((header & 0xf0) >> 4);
   this.Duplicate = (header & 0x08) != 0;
   this.QualityOfService = (QoS)((header & 0x06)>>1);
   this.Retained = (header & 0x01) != 0;
   variableHeaderLength = len;
 }
示例#58
0
 public MsPublish(Topic val, ushort topicId, QoS qualityOfService)
   : base(MsMessageType.PUBLISH) {
   this.IsRequest=qualityOfService!=QoS.AtMostOnce;
   this.qualityOfService=qualityOfService;
   this.TopicId=topicId;
   this._val=val;
   if(MsDevice.PredefinedTopics.ContainsValue(topicId) && (_val==null || MsDevice.PredefinedTopics.ContainsKey(_val.name) || MsDevice.PredefinedTopics.ContainsKey(".cfg/"+_val.name))) {
     this.topicIdType=TopicIdType.PreDefined;
   }
 }