Line data Source code
1 : /*
2 : * Package : mqtt_client
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 31/05/2017
5 : * Copyright : S.Hamblett
6 : */
7 :
8 : part of mqtt_client;
9 :
10 : /// Represents the connect flags part of the MQTT Variable Header
11 : class MqttConnectFlags {
12 : bool reserved1 = false;
13 : bool cleanStart = false;
14 : bool willFlag = false;
15 : MqttQos willQos = MqttQos.atLeastOnce;
16 : bool willRetain = false;
17 : bool passwordFlag = false;
18 : bool usernameFlag = false;
19 :
20 : /// Initializes a new instance of the <see cref="MqttConnectFlags" /> class.
21 6 : MqttConnectFlags();
22 :
23 : /// Initializes a new instance of the <see cref="MqttConnectFlags" /> class configured as per the supplied stream.
24 4 : MqttConnectFlags.fromByteBuffer(MqttByteBuffer connectFlagsStream) {
25 4 : readFrom(connectFlagsStream);
26 : }
27 :
28 : int connectFlagByte() {
29 8 : return ((reserved1 ? 1 : 0) |
30 12 : (cleanStart ? 1 : 0) << 1 |
31 12 : (willFlag ? 1 : 0) << 2 |
32 16 : (willQos.index) << 3 |
33 12 : (willRetain ? 1 : 0) << 5 |
34 12 : (passwordFlag ? 1 : 0) << 6 |
35 8 : (usernameFlag ? 1 : 0) << 7);
36 : }
37 :
38 : /// Writes the connect flag byte to the supplied stream.
39 : void writeTo(MqttByteBuffer connectFlagsStream) {
40 8 : connectFlagsStream.writeByte(connectFlagByte());
41 : }
42 :
43 : /// Reads the connect flags from the underlying stream.
44 : void readFrom(MqttByteBuffer stream) {
45 4 : final int connectFlagsByte = stream.readByte();
46 :
47 12 : reserved1 = (connectFlagsByte & 1) == 1;
48 12 : cleanStart = (connectFlagsByte & 2) == 2;
49 12 : willFlag = (connectFlagsByte & 4) == 4;
50 16 : willQos = MqttQos.values[((connectFlagsByte >> 3) & 3)];
51 12 : willRetain = (connectFlagsByte & 32) == 32;
52 12 : passwordFlag = (connectFlagsByte & 64) == 64;
53 12 : usernameFlag = (connectFlagsByte & 128) == 128;
54 : }
55 :
56 : /// Gets the length of data written when WriteTo is called.
57 : static int getWriteLength() {
58 : return 1;
59 : }
60 :
61 : /// Returns a String that represents the current connect flag settings
62 : String toString() {
63 20 : return "Connect Flags: Reserved1=$reserved1, CleanStart=$cleanStart, WillFlag=$willFlag, WillQos=$willQos, " +
64 16 : "WillRetain=$willRetain, PasswordFlag=$passwordFlag, UserNameFlag=$usernameFlag";
65 : }
66 : }
|