/// <summary> /// 初始化MQTT数据包 /// </summary> /// <param name="dataPackage"></param> public MQTTDataPackage(MQTTDataPackage dataPackage) { this.Data = dataPackage.Data; this.Header = dataPackage.Header; ClientId = dataPackage.ClientId; RemoteEndPoint = dataPackage.RemoteEndPoint; }
/// <summary> /// 将消息转换成<see cref="MQTTDataPackage"/>标准协议包。 /// </summary> /// <returns></returns> public virtual MQTTDataPackage ToPackage(bool subscribe) { MQTTDataPackage package = new MQTTDataPackage() { MessageType = subscribe ? MessageType.Subscribe : MessageType.UnSubscribe, QoS = QoS }; List <byte> bytes = new List <byte>(); bytes.Add((byte)(this.Identifier >> 8)); bytes.Add((byte)(Identifier & 255)); var topicBytes = this.Topic.ToBytes(Encoding.ASCII); bytes.Add((byte)(topicBytes.Length >> 8)); bytes.Add((byte)(topicBytes.Length & 255)); bytes.AddRange(topicBytes); bytes.Add((byte)(QoS)); package.Data = bytes.ToArray(); bytes.Clear(); return(package); }
/// <summary> /// 将消息转换成<see cref="MQTTDataPackage"/>标准协议包。 /// </summary> /// <returns></returns> public virtual MQTTDataPackage ToPackage() { MQTTDataPackage package = new MQTTDataPackage() { MessageType = MessageType.Publish, QoS = QoS }; List <byte> bytes = new List <byte>(); var topicBytes = this.Topic.ToBytes(Encoding.ASCII); bytes.Add((byte)(topicBytes.Length >> 8)); bytes.Add((byte)(topicBytes.Length & 255)); bytes.AddRange(topicBytes); if (QoS > 0) { bytes.Add((byte)(this.Identifier >> 8)); bytes.Add((byte)(Identifier & 255)); } bytes.AddRange(Data); package.Data = bytes.ToArray(); bytes.Clear(); return(package); }
/// <summary> /// 初始化mqtt连接包 /// </summary> /// <param name="package"></param> public PublishDataPackage(MQTTDataPackage package) : base(package) { }
/// <summary> /// 初始化mqtt连接包 /// </summary> /// <param name="package"></param> public SubscribeAckDataPackage(MQTTDataPackage package) : base(package) { }
/// <summary> /// 初始化mqtt连接包 /// </summary> /// <param name="package"></param> public TopicDataPackage(MQTTDataPackage package) : base(package) { Init();// }
/// <summary> /// 初始化mqtt连接包 /// </summary> /// <param name="package"></param> public IdentifierAckDataPackage(MQTTDataPackage package) : base(package) { Identifier = (ushort)(Data[0] << 8 | Data[1]); }
/// <summary> /// 初始化连接确认包 /// </summary> /// <param name="dataPackage"></param> public ConnectAckDataPackage(MQTTDataPackage dataPackage) { this.Data = dataPackage.Data; this.Header = dataPackage.Header; }