public async Task PublishAsync(string topic, QualityOfServiceLevelType qos, bool retain, bool dup, byte[] data) { ushort id = session.NewId(); PublishMessage msg = new PublishMessage(dup, qos, retain, id, topic, data); if (qos != QualityOfServiceLevelType.AtMostOnce) { session.Quarantine(msg, DirectionType.In); } await channel.SendAsync(msg.Encode()); }
/// <summary> /// MQTT publish to a topic. /// </summary> /// <param name="qos"></param> /// <param name="topicUriString"></param> /// <param name="contentType"></param> /// <param name="data"></param> /// <param name="indexes"></param> /// <param name="messageId"></param> /// <returns></returns> public async Task PublishAsync(QualityOfServiceLevelType qos, string topicUriString, string contentType, byte[] data, string cacheKey = null, IEnumerable <KeyValuePair <string, string> > indexes = null, string messageId = null) { try { string indexString = GetIndexString(indexes); UriBuilder builder = new UriBuilder(topicUriString); string queryString = messageId == null?String.Format("{0}={1}", SkunkLab.Protocols.Utilities.QueryStringConstants.CONTENT_TYPE, contentType) : String.Format("{0}={1}&{2}={3}", QueryStringConstants.CONTENT_TYPE, contentType, QueryStringConstants.MESSAGE_ID, messageId); if (!string.IsNullOrEmpty(cacheKey)) { queryString = queryString + String.Format("&{0}={1}", QueryStringConstants.CACHE_KEY, cacheKey); } if (!string.IsNullOrEmpty(indexString)) { queryString = queryString + "&" + indexString; } builder.Query = queryString; PublishMessage msg = new PublishMessage(false, qos, false, 0, builder.ToString().ToLowerInvariant(), data); if (qos != QualityOfServiceLevelType.AtMostOnce) { msg.MessageId = session.NewId(); session.Quarantine(msg, DirectionType.Out); } queue.Enqueue(msg.Encode()); while (queue.Count > 0) { byte[] message = queue.Dequeue(); //if (channel.RequireBlocking) //{ // //Task t = channel.SendAsync(message); // //Task.WaitAll(t); //} //else //{ await channel.SendAsync(message); //} } } catch (Exception ex) { OnChannelError?.Invoke(this, new ChannelErrorEventArgs(channel.Id, ex)); } }
private static byte[] MqttConversion(MqttSession session, byte[] message, string contentType = null) { PublishMessage msg = MqttMessage.DecodeMessage(message) as PublishMessage; MqttUri uri = new MqttUri(msg.Topic); QualityOfServiceLevelType?qos = session.GetQoS(uri.Resource); PublishMessage pm = new PublishMessage(false, qos.HasValue ? qos.Value : QualityOfServiceLevelType.AtMostOnce, false, session.NewId(), uri.Resource, msg.Payload); if (pm.QualityOfService != QualityOfServiceLevelType.AtMostOnce) { session.Quarantine(pm, SkunkLab.Protocols.Mqtt.Handlers.DirectionType.Out); } return(pm.Encode()); }
private static byte[] MqttConversion(MqttSession session, byte[] message) { PublishMessage msg = MqttMessage.DecodeMessage(message) as PublishMessage; MqttUri uri = new MqttUri(msg.Topic); QualityOfServiceLevelType?qos = session.GetQoS(uri.Resource); PublishMessage pm = new PublishMessage(false, qos ?? QualityOfServiceLevelType.AtMostOnce, false, session.NewId(), uri.Resource, msg.Payload); if (pm.QualityOfService != QualityOfServiceLevelType.AtMostOnce) { session.Quarantine(pm, DirectionType.Out); } return(pm.Encode()); }