@@ -9,29 +9,20 @@ module MQTT
9
9
getter topic, payload, qos, packet_id, remaining_length
10
10
getter? dup, retain
11
11
12
- def initialize (pub : Publish , topic = pub.topic, qos = pub.qos, payload = pub.payload, packet_id = pub.packet_id, dup = pub.dup?, retain = pub.retain? )
13
- initialize (topic, payload, packet_id, dup, qos, retain)
12
+ def self.new (topic : String , payload : Bytes , packet_id : UInt16 ?, dup : Bool , qos : UInt8 , retain : Bool )
13
+ new (topic, BytesPayload .new( payload) , packet_id, dup, qos, retain)
14
14
end
15
15
16
- def initialize (@topic : String , @payload : Payload , @packet_id : UInt16 ?, @dup : Bool , @qos : UInt8 , @retain : Bool )
17
- raise ArgumentError .new(" QoS must be 0, 1 or 2" ) if @qos > 2
18
- raise ArgumentError .new(" Topic cannot contain wildcard" ) if @topic .matches?(/[#+] / )
19
- raise ArgumentError .new(" Topic must be between atleast 1 char long" ) if @topic .size < 1
20
- raise ArgumentError .new(" Topic cannot be larger than 65535 bytes" ) if @topic .bytesize > 65535
21
- raise ArgumentError .new(" DUP must be 0 for QoS 0 messages" ) if dup? && qos.zero?
22
- @remaining_length = 0
23
- @remaining_length += (2 + topic.bytesize) + payload.bytesize
24
- @remaining_length += 2 if qos.positive? # packet_id
16
+ def self.new (pub : Publish , topic = pub.topic, qos = pub.qos, payload = pub.payload, packet_id = pub.packet_id, dup = pub.dup?, retain = pub.retain?)
17
+ new(topic, payload, packet_id, dup, qos, retain)
25
18
end
26
19
27
- @[Deprecated (" Use Payload instead of Bytes for @payload" )]
28
- def initialize (@topic : String , payload : Bytes , @packet_id : UInt16 ?, @dup : Bool , @qos : UInt8 , @retain : Bool )
20
+ def initialize (@topic : String , @payload : Payload , @packet_id : UInt16 ?, @dup : Bool , @qos : UInt8 , @retain : Bool )
29
21
raise ArgumentError .new(" QoS must be 0, 1 or 2" ) if @qos > 2
30
22
raise ArgumentError .new(" Topic cannot contain wildcard" ) if @topic .matches?(/[#+] / )
31
23
raise ArgumentError .new(" Topic must be between atleast 1 char long" ) if @topic .size < 1
32
24
raise ArgumentError .new(" Topic cannot be larger than 65535 bytes" ) if @topic .bytesize > 65535
33
25
raise ArgumentError .new(" DUP must be 0 for QoS 0 messages" ) if dup? && qos.zero?
34
- @payload = BytesPayload .new(payload)
35
26
@remaining_length = 0
36
27
@remaining_length += (2 + topic.bytesize) + payload.bytesize
37
28
@remaining_length += 2 if qos.positive? # packet_id
0 commit comments