Пример #1
0
        /// <summary>
        /// 取消订阅多个主题
        /// </summary>
        /// <param name="topic">主题</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="MqttException"></exception>
        /// <returns></returns>
        private ITaskSetter <bool> UnsubscribeManyInternal(string[] topic)
        {
            if (topic == null)
            {
                throw new ArgumentNullException();
            }
            if (topic.Length == 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            var taskId = this.GenerateTaskId();
            var setter = this.taskSetterTable.Create <bool>(taskId);

            var topicArray = topic.Select(item => item.ToUnmanagedPointer()).ToArray();

            fixed(IntPtr *ptrtopic = &topicArray[0])
            {
                var opt = this.InitResponseOptions(taskId);
                var er  = MQTTAsync.MQTTAsync_unsubscribeMany(this.handle, topic.Length, ptrtopic, ref opt);

                this.EnsureSuccessCode(er);
            }

            return(setter);
        }
Пример #2
0
        /// <summary>
        /// 订阅多个主题
        /// </summary>
        /// <param name="topicQos">主题与消息</param>
        /// <param name="taskId">任务id</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="MqttException"></exception>
        /// <returns></returns>
        private ITaskSetter <bool> SubscribeManyInternal(MqttTopicQoS[] topicQos)
        {
            if (topicQos == null)
            {
                throw new ArgumentNullException();
            }
            if (topicQos.Length == 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            var taskId = this.GenerateTaskId();
            var setter = this.taskSetterTable.Create <bool>(taskId);

            var opt    = this.InitResponseOptions(taskId);
            var qoss   = topicQos.Select(item => (int)item.QoS).ToArray();
            var topics = topicQos.Select(item => item.Topic.ToUnmanagedPointer()).ToArray();

            fixed(IntPtr *ptrtopic = &topics[0])
            {
                fixed(int *ptrqos = &qoss[0])
                {
                    var er = MQTTAsync.MQTTAsync_subscribeMany(this.handle, qoss.Length, ptrtopic, ptrqos, ref opt);

                    this.EnsureSuccessCode(er);
                }
            }

            return(setter);
        }
Пример #3
0
        /// <summary>
        /// 设置客户端相关回调
        /// </summary>
        private void InitClientCallbacks()
        {
            var lost = new MQTTAsync_connectionLost((context, cause) =>
            {
                var e = this.OnConnectionLost;
                if (e != null)
                {
                    e.Invoke(this);
                }
            });

            var arrvied = new MQTTAsync_messageArrived((context, topicName, topicLen, msg) =>
            {
                var e = this.OnMessageArrived;
                if (e != null)
                {
                    var topic   = Marshal.PtrToStringAnsi(topicName, topicLen);
                    var message = MqttMessage.From(msg);
                    e.Invoke(this, topic, message);
                }

                MQTTAsync.MQTTAsync_free(topicName);
                MQTTAsync.MQTTAsync_freeMessage(ref msg);
                return(1);
            });

            var er = MQTTAsync.MQTTAsync_setCallbacks(this.handle, IntPtr.Zero, lost, arrvied, null);

            this.EnsureSuccessCode(er);
            this.AutoRef(lost, arrvied);
        }
Пример #4
0
        /// <summary>
        /// mqtt客户端
        /// </summary>
        /// <param name="serverUri">mqtt://mymqtt.com</param>
        /// <param name="clientId">客户端id</param>
        /// <param name="persistence">持久化方式</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="UriFormatException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        /// <exception cref="MqttException"></exception>
        public MqttClient(string serverUri, string clientId, MqttPersistence persistence)
            : base(IntPtr.Zero, true)
        {
            if (string.IsNullOrEmpty(serverUri))
            {
                throw new ArgumentNullException("serverUri");
            }

            if (string.IsNullOrEmpty(clientId))
            {
                throw new ArgumentNullException("clientId");
            }

            if (persistence == MqttPersistence.User)
            {
                var message = string.Format("不支持的持久化方式:{0}.{1}", typeof(MqttPersistence).Name, persistence);
                throw new NotSupportedException(message);
            }

            var       uri             = new Uri(serverUri);
            const int mqttDefaultPort = 1883;
            var       tcpUri          = string.Format("tcp://{0}:{1}", uri.Host, uri.Port > 0 ? uri.Port : mqttDefaultPort);

            var er = MQTTAsync.MQTTAsync_create(ref this.handle, tcpUri, clientId, persistence, IntPtr.Zero);

            this.EnsureSuccessCode(er);

            this.InitClientCallbacks();
        }
Пример #5
0
        /// <summary>
        /// 释放句柄
        /// </summary>
        protected sealed override bool ReleaseHandle()
        {
            if (this.IsInvalid == true)
            {
                return(false);
            }

            MQTTAsync.MQTTAsync_destroy(ref this.handle);
            return(this.IsInvalid);
        }
Пример #6
0
        static MQTTAsync()
        {
            var dllFile = Path.Combine(Environment.Is64BitProcess ? "x64" : "x86", mqtt3a_dll);

            if (HttpContext.Current != null)
            {
                dllFile = Path.Combine("~\\bin", dllFile);
                dllFile = HttpContext.Current.Server.MapPath(dllFile);
            }
            MQTTAsync.LoadLibraryA(dllFile);
        }
Пример #7
0
 /// <summary>
 /// 设置追踪回调
 /// </summary>
 /// <param name="traceCallback">追踪回调,null则清除追踪</param>
 public static void SetTraceCallback(Action <MqttTraceLevels, string> traceCallback)
 {
     if (traceCallback == null)
     {
         MQTTAsync.MQTTAsync_setTraceCallback(null);
     }
     else
     {
         MqttClient.trackCallback = new MQTTAsync_traceCallback((level, msg) => traceCallback(level, msg));
         MQTTAsync.MQTTAsync_setTraceCallback(MqttClient.trackCallback);
     }
 }
Пример #8
0
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <exception cref="MqttException"></exception>
        /// <returns></returns>
        private ITaskSetter <bool> DisconnectInternal()
        {
            var taskId = this.GenerateTaskId();
            var setter = this.taskSetterTable.Create <bool>(taskId);

            var mqttOptions = this.AutoRef <MqttDisconnecOptions>();
            var opt         = mqttOptions.ToStruct(taskId);
            var er          = MQTTAsync.MQTTAsync_disconnect(this.handle, ref opt);

            this.EnsureSuccessCode(er);

            return(setter);
        }
Пример #9
0
        /// <summary>
        /// 取消订阅
        /// </summary>
        /// <param name="topic">主题</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="MqttException"></exception>
        /// <returns></returns>
        private ITaskSetter <bool> UnsubscribeInternal(string topic)
        {
            if (topic == null)
            {
                throw new ArgumentNullException();
            }

            var taskId = this.GenerateTaskId();
            var setter = this.taskSetterTable.Create <bool>(taskId);

            var opt = this.InitResponseOptions(taskId);
            var er  = MQTTAsync.MQTTAsync_unsubscribe(this.handle, topic, ref opt);

            this.EnsureSuccessCode(er);

            return(setter);
        }
Пример #10
0
        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="topic">主题</param>
        /// <param name="qos">质量</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="MqttException"></exception>
        /// <returns></returns>
        private ITaskSetter <bool> SubscribeInternal(string topic, MqttQoS qos)
        {
            if (string.IsNullOrEmpty(topic))
            {
                throw new ArgumentNullException("topic");
            }

            var taskId = this.GenerateTaskId();
            var setter = this.taskSetterTable.Create <bool>(taskId);

            var opt = this.InitResponseOptions(taskId);
            var er  = MQTTAsync.MQTTAsync_subscribe(this.handle, topic, qos, ref opt);

            this.EnsureSuccessCode(er);

            return(setter);
        }
Пример #11
0
        /// <summary>
        /// 连接到服务器
        /// </summary>
        /// <param name="option">选项</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="MqttException"></exception>
        /// <returns></returns>
        private ITaskSetter <ConnectError> ConnectInternal(ConnectOption option)
        {
            if (option == null)
            {
                throw new ArgumentNullException();
            }

            this.connectOpt = option;

            var taskId = this.GenerateTaskId();
            var setter = this.taskSetterTable.Create <ConnectError>(taskId);

            var mqttOptions = this.AutoRef <MqttConnectOptions>();
            var opt         = mqttOptions.ToStruct(taskId, option.ToStruct());
            var er          = MQTTAsync.MQTTAsync_connect(this.handle, ref opt);

            this.EnsureSuccessCode(er);

            return(setter);
        }
Пример #12
0
        /// <summary>
        /// 发送消息
        /// 不跟踪服务端的消息处理结果
        /// </summary>
        /// <param name="topic">主题</param>
        /// <param name="message">消息</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns></returns>
        public MqttError SendMessageAsync(string topic, MqttMessage message)
        {
            if (string.IsNullOrEmpty(topic))
            {
                throw new ArgumentNullException("topic");
            }
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var msg = message.ToStruct();
            var opt = new MQTTAsync_responseOptions();

            opt.Init();

            var er = MQTTAsync.MQTTAsync_sendMessage(this.handle, topic, ref msg, ref opt);

            msg.Dispose();
            return(er);
        }
Пример #13
0
        /// <summary>
        /// 发送消息
        /// 并跟踪服务端对消息回复结果
        /// </summary>
        /// <param name="topic">主题</param>
        /// <param name="message">消息</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="MqttException"></exception>
        /// <returns></returns>
        public Task <bool> SendMessageTaskAsync(string topic, MqttMessage message)
        {
            if (string.IsNullOrEmpty(topic))
            {
                throw new ArgumentNullException("topic");
            }
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var taskId = this.GenerateTaskId();
            var setter = this.taskSetterTable.Create <bool>(taskId);

            var msg = message.ToStruct();
            var opt = this.InitResponseOptions(taskId);
            var er  = MQTTAsync.MQTTAsync_sendMessage(this.handle, topic, ref msg, ref opt);

            msg.Dispose();

            this.EnsureSuccessCode(er);
            return(setter.GetTask());
        }
Пример #14
0
 /// <summary>
 /// 设置追踪级别
 /// </summary>
 /// <param name="level">级别</param>
 public static void SetTraceLevel(MqttTraceLevels level)
 {
     MQTTAsync.MQTTAsync_setTraceLevel(level);
 }