Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(mqtt): add MQTT broker connection support

jinmo8cfe3807843f
diff --git a/.github/scripts/database-issue-catalog.mjs b/.github/scripts/database-issue-catalog.mjs
index 402b4c1c4c..350d17d7ce 100644
--- a/.github/scripts/database-issue-catalog.mjs
+++ b/.github/scripts/database-issue-catalog.mjs
@@ -84,6 +84,7 @@ const supplementalDrivers = [
{ dbType: "kafka", label: "Apache Kafka" },
{ dbType: "rocketmq", label: "Apache RocketMQ" },
{ dbType: "rabbitmq", label: "RabbitMQ" },
+ { dbType: "mqtt", label: "MQTT", aliases: ["emqx", "hivemq", "mosquitto"] },
];
const manifestUrl = new URL("../../crates/dbx-core/assets/database-drivers.manifest.json", import.meta.url);
diff --git a/.github/scripts/label-issue-database.test.mjs b/.github/scripts/label-issue-database.test.mjs
index 74f48a2d16..782eccf7c1 100644
--- a/.github/scripts/label-issue-database.test.mjs
+++ b/.github/scripts/label-issue-database.test.mjs
@@ -57,6 +57,8 @@ test("labels native and compatibility database products with their families", ()
["Dremio", ["db/dremio", "db/jdbc"]],
["Apache Kafka", ["db/kafka", "db/mq"]],
["RabbitMQ", ["db/mq", "db/rabbitmq"]],
+ ["MQTT 5.0", ["db/mqtt"]],
+ ["EMQX 5.8", ["db/mqtt"]],
];
for (const [database, expected] of cases) {
diff --git a/Cargo.lock b/Cargo.lock
index 36dc614c3f..14987fa8de 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -41,9 +41,9 @@ dependencies = [
[[package]]
name = "aes"
-version = "0.9.1"
+version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f1fc76eaeac4c9164506c466d4ffdd8ec9d0c5bf57ee97177c4d8eceb3a0e138"
+checksum = "f8eb277bec05f56a0e0591f155a484cbd0f4f07ff2905051a48c72f004f7ed58"
dependencies = [
"cipher 0.5.2",
"cpubits",
@@ -71,7 +71,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e22c0c90bbe8d4f77c3ca9ddabe41a1f8382d6fc1f7cea89459d0f320371f972"
dependencies = [
"aead 0.6.0-rc.10",
- "aes 0.9.1",
+ "aes 0.9.2",
"cipher 0.5.2",
"ctr 0.10.1",
"ghash 0.6.0",
@@ -256,9 +256,9 @@ dependencies = [
[[package]]
name = "async-compression"
-version = "0.4.42"
+version = "0.4.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e79b3f8a79cccc2898f31920fc69f304859b3bd567490f75ebf51ae1c792a9ac"
+checksum = "3976abdc8fe7d1133d43d304afd42abdf5bc3e1319d263d223bde07b5efc4be8"
dependencies = [
"compression-codecs",
"compression-core",
@@ -395,6 +395,35 @@ dependencies = [
"syn 3.0.3",
]
+[[package]]
+name = "async-tungstenite"
+version = "0.25.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2cca750b12e02c389c1694d35c16539f88b8bbaa5945934fdc1b41a776688589"
+dependencies = [
+ "futures-io",
+ "futures-util",
+ "log",
+ "pin-project-lite",
+ "rustls-native-certs 0.7.3",
+ "rustls-pki-types",
+ "tokio",
+ "tokio-rustls 0.25.0",
+ "tungstenite 0.21.0",
+]
+
+[[package]]
+name = "async_io_stream"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6d7b9decdf35d8908a7e3ef02f64c5e9b1695e230154c0e8de3969142d9b94c"
+dependencies = [
+ "futures",
+ "pharos",
+ "rustc_version",
+ "tokio",
+]
+
[[package]]
name = "asynchronous-codec"
version = "0.6.2"
@@ -897,9 +926,9 @@ dependencies = [
[[package]]
name = "camino"
-version = "1.2.4"
+version = "1.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5f2d30e4173c4026932d51d31d6b0613b1fd3014bf3f9f8943d4ba139c437ba0"
+checksum = "bb1307f12aa967b5a58416e87b3653360e0fd614a016b6e970db08fecbb1b80d"
dependencies = [
"serde_core",
]
@@ -1646,7 +1675,7 @@ dependencies = [
"reqwest 0.12.28",
"russh",
"rust_decimal",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"semver",
"serde",
"serde_json",
@@ -1726,10 +1755,11 @@ dependencies = [
"redis",
"regex",
"reqwest 0.12.28",
+ "rumqttc",
"rusqlite",
"russh",
"rust_decimal",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"rustls-pemfile 2.2.0",
"serde",
"serde_json",
@@ -1767,7 +1797,7 @@ dependencies = [
"dirs",
"reqwest 0.12.28",
"rmcp",
- "schemars 1.2.1",
+ "schemars 1.2.2",
"serde",
"serde_json",
"tempfile",
@@ -1792,7 +1822,7 @@ dependencies = [
"pbkdf2 0.12.2",
"redis",
"reqwest 0.12.28",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"serde",
"serde_json",
"sha2 0.10.9",
@@ -2021,13 +2051,13 @@ dependencies = [
[[package]]
name = "displaydoc"
-version = "0.2.6"
+version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1ac70aa55017e108007fbaf5aa0f54b021c98f92ff8af59d42eda9da96e3dd4f"
+checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.119",
+ "syn 3.0.3",
]
[[package]]
@@ -2234,7 +2264,7 @@ dependencies = [
"cc",
"memchr",
"rustc_version",
- "toml 1.1.3+spec-1.1.0",
+ "toml 1.1.4+spec-1.1.0",
"vswhom",
"winreg",
]
@@ -2344,11 +2374,10 @@ checksum = "dea2df4cf52843e0452895c455a1a2cfbb842a1e7329671acf418fdc53ed4c59"
[[package]]
name = "event-listener"
-version = "5.4.1"
+version = "5.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e13b66accf52311f30a0db42147dadea9850cb48cd070028831ae5f5d4b856ab"
+checksum = "5a23add41df1562121a9393cb065eab5146a1242410f23a644851e90cfd669d2"
dependencies = [
- "concurrent-queue",
"parking",
"pin-project-lite",
]
@@ -2484,6 +2513,17 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce81f49ae8a0482e4c55ea62ebbd7e5a686af544c00b9d090bba3ff9be97b3d"
+[[package]]
+name = "flume"
+version = "0.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da0e4dd2a88388a1f4ccc7c9ce104604dab68d9f408dc34cd45823d5a9069095"
+dependencies = [
+ "futures-core",
+ "futures-sink",
+ "spin",
+]
+
[[package]]
name = "fnv"
version = "1.0.7"
@@ -3280,9 +3320,9 @@ dependencies = [
[[package]]
name = "http"
-version = "1.4.2"
+version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6970f50e31d6fc17d3fa27329444bfa74e196cf62e95052a3f6fee181dba6425"
+checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"
dependencies = [
"bytes",
"itoa",
@@ -3372,7 +3412,7 @@ dependencies = [
"http",
"hyper",
"hyper-util",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"tokio",
"tokio-rustls 0.26.4",
"tower-service",
@@ -4442,7 +4482,7 @@ dependencies = [
"percent-encoding",
"rand 0.9.5",
"rustc_version_runtime",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"serde",
"serde_bytes",
"serde_with",
@@ -4555,7 +4595,7 @@ dependencies = [
"mysql_common",
"percent-encoding",
"rand 0.10.2",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"serde",
"socket2",
"thiserror 2.0.19",
@@ -5438,6 +5478,16 @@ dependencies = [
"indexmap 2.14.0",
]
+[[package]]
+name = "pharos"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e9567389417feee6ce15dd6527a8a1ecac205ef62c2932bcf3d9f6fc5b78b414"
+dependencies = [
+ "futures",
+ "rustc_version",
+]
+
[[package]]
name = "phf"
version = "0.13.1"
@@ -5524,7 +5574,7 @@ version = "0.8.0-rc.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5a777c6e26664bc9504b3ce3f6133f8f20d9071f130a4f9fcbd3186959d8dd6"
dependencies = [
- "aes 0.9.1",
+ "aes 0.9.2",
"aes-gcm 0.11.0-rc.3",
"cbc 0.2.1",
"der 0.8.1",
@@ -5922,7 +5972,7 @@ dependencies = [
"quinn-proto",
"quinn-udp",
"rustc-hash",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"socket2",
"thiserror 2.0.19",
"tokio",
@@ -5943,7 +5993,7 @@ dependencies = [
"rand_pcg",
"ring",
"rustc-hash",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"rustls-pki-types",
"slab",
"thiserror 2.0.19",
@@ -6142,7 +6192,7 @@ dependencies = [
"percent-encoding",
"pin-project-lite",
"rand 0.9.5",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"rustls-native-certs 0.8.4",
"ryu",
"sha1_smol",
@@ -6257,7 +6307,7 @@ dependencies = [
"percent-encoding",
"pin-project-lite",
"quinn",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"rustls-pki-types",
"serde",
"serde_json",
@@ -6298,7 +6348,7 @@ dependencies = [
"log",
"percent-encoding",
"pin-project-lite",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"rustls-pki-types",
"rustls-platform-verifier",
"serde",
@@ -6413,7 +6463,7 @@ dependencies = [
"pastey",
"pin-project-lite",
"rmcp-macros",
- "schemars 1.2.1",
+ "schemars 1.2.2",
"serde",
"serde_json",
"thiserror 2.0.19",
@@ -6455,6 +6505,25 @@ dependencies = [
"zeroize",
]
+[[package]]
+name = "rumqttc"
+version = "0.24.0"
+dependencies = [
+ "async-tungstenite",
+ "bytes",
+ "flume",
+ "futures-util",
+ "http",
+ "log",
+ "rustls-native-certs 0.7.3",
+ "rustls-pemfile 2.2.0",
+ "rustls-webpki 0.102.8",
+ "thiserror 1.0.69",
+ "tokio",
+ "tokio-rustls 0.25.0",
+ "ws_stream_tungstenite",
+]
+
[[package]]
name = "rusqlite"
version = "0.32.1"
@@ -6477,7 +6546,7 @@ checksum = "324b92f459d3e42da294e14e8eb150d2215fcfb7c966838bc1127cd68bc05a0d"
dependencies = [
"aead 0.6.0-rc.10",
"aes 0.8.4",
- "aes 0.9.1",
+ "aes 0.9.2",
"aes-gcm 0.11.0-rc.3",
"aws-lc-rs",
"bitflags 2.13.1",
@@ -6676,9 +6745,23 @@ dependencies = [
[[package]]
name = "rustls"
-version = "0.23.42"
+version = "0.22.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3c54fcab019b409d04215d3a17cb438fd7fbf192ee61461f20f4fe18704bc138"
+checksum = "bf4ef73721ac7bcd79b2b315da7779d8fc09718c6b3d2d1b2d94850eb8c18432"
+dependencies = [
+ "log",
+ "ring",
+ "rustls-pki-types",
+ "rustls-webpki 0.102.8",
+ "subtle",
+ "zeroize",
+]
+
+[[package]]
+name = "rustls"
+version = "0.23.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0283386ce02abc0151e1761d08802dfe86c173b0b494af5cbc086574e453da06"
dependencies = [
"aws-lc-rs",
"log",
@@ -6702,6 +6785,19 @@ dependencies = [
"security-framework 2.11.1",
]
+[[package]]
+name = "rustls-native-certs"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e5bfb394eeed242e909609f56089eecfe5fda225042e8b171791b9c95f5931e5"
+dependencies = [
+ "openssl-probe 0.1.6",
+ "rustls-pemfile 2.2.0",
+ "rustls-pki-types",
+ "schannel",
+ "security-framework 2.11.1",
+]
+
[[package]]
name = "rustls-native-certs"
version = "0.8.4"
@@ -6753,7 +6849,7 @@ dependencies = [
"jni 0.22.4",
"log",
"once_cell",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"rustls-native-certs 0.8.4",
"rustls-platform-verifier-android",
"rustls-webpki 0.103.13",
@@ -6779,6 +6875,17 @@ dependencies = [
"untrusted 0.9.0",
]
+[[package]]
+name = "rustls-webpki"
+version = "0.102.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9"
+dependencies = [
+ "ring",
+ "rustls-pki-types",
+ "untrusted 0.9.0",
+]
+
[[package]]
name = "rustls-webpki"
version = "0.103.13"
@@ -6866,14 +6973,14 @@ dependencies = [
[[package]]
name = "schemars"
-version = "1.2.1"
+version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a2b42f36aa1cd011945615b92222f6bf73c599a102a300334cd7f8dbeec726cc"
+checksum = "687274d293b6cdc6e73e0fee520bf2049650090d7164f87672d212a3c530cf4a"
dependencies = [
"chrono",
"dyn-clone",
"ref-cast",
- "schemars_derive 1.2.1",
+ "schemars_derive 1.2.2",
"serde",
"serde_json",
]
@@ -6886,20 +6993,20 @@ checksum = "32e265784ad618884abaea0600a9adf15393368d840e0222d101a072f3f7534d"
dependencies = [
"proc-macro2",
"quote",
- "serde_derive_internals",
+ "serde_derive_internals 0.29.1",
"syn 2.0.119",
]
[[package]]
name = "schemars_derive"
-version = "1.2.1"
+version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7d115b50f4aaeea07e79c1912f645c7513d81715d0420f8bc77a18c6260b307f"
+checksum = "d98c67716b46af2f0b8cf752abc930f6f9aecfbf671ecfb531db8a31dbe4e2ba"
dependencies = [
"proc-macro2",
"quote",
- "serde_derive_internals",
- "syn 2.0.119",
+ "serde_derive_internals 0.30.0",
+ "syn 3.0.3",
]
[[package]]
@@ -7078,6 +7185,17 @@ dependencies = [
"syn 2.0.119",
]
+[[package]]
+name = "serde_derive_internals"
+version = "0.30.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f852137cce035d6a4df67ccce505ff6b3e9fd3a10e3e52b24dc71e650bb1a9bd"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 3.0.3",
+]
+
[[package]]
name = "serde_json"
version = "1.0.151"
@@ -7157,7 +7275,7 @@ dependencies = [
"indexmap 1.9.3",
"indexmap 2.14.0",
"schemars 0.9.0",
- "schemars 1.2.1",
+ "schemars 1.2.2",
"serde_core",
"serde_json",
"serde_with_macros",
@@ -7491,6 +7609,9 @@ name = "spin"
version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e"
+dependencies = [
+ "lock_api",
+]
[[package]]
name = "spki"
@@ -7814,9 +7935,9 @@ dependencies = [
[[package]]
name = "tao-macros"
-version = "0.1.3"
+version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f4e16beb8b2ac17db28eab8bca40e62dbfbb34c0fcdc6d9826b11b7b5d047dfd"
+checksum = "5f7eeb6d99155545da6150a1795945f16ac9c178deb2a5f2e74d776107bd5849"
dependencies = [
"proc-macro2",
"quote",
@@ -8049,7 +8170,7 @@ dependencies = [
"tauri-plugin",
"tauri-utils",
"thiserror 2.0.19",
- "toml 1.1.3+spec-1.1.0",
+ "toml 1.1.4+spec-1.1.0",
"url",
]
@@ -8138,7 +8259,7 @@ dependencies = [
"osakit",
"percent-encoding",
"reqwest 0.13.4",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"semver",
"serde",
"serde_json",
@@ -8251,7 +8372,7 @@ dependencies = [
"serde_with",
"swift-rs",
"thiserror 2.0.19",
- "toml 1.1.3+spec-1.1.0",
+ "toml 1.1.4+spec-1.1.0",
"url",
"urlpattern",
"uuid",
@@ -8266,7 +8387,7 @@ checksum = "cc65d45c68858bfe420dd29e834b5d15dbecf8a07a8a16cf4d532c7b1f69d4b6"
dependencies = [
"dunce",
"embed-resource",
- "toml 1.1.3+spec-1.1.0",
+ "toml 1.1.4+spec-1.1.0",
]
[[package]]
@@ -8499,13 +8620,13 @@ dependencies = [
[[package]]
name = "tokio-macros"
-version = "2.7.1"
+version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6328af13490e73a9b4694030fafd93f8c8c6a9dede33e821c3fc63eddf8042ba"
+checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.119",
+ "syn 3.0.3",
]
[[package]]
@@ -8551,7 +8672,7 @@ checksum = "27d684bad428a0f2481f42241f821db42c54e2dc81d8c00db8536c506b0a0144"
dependencies = [
"const-oid 0.9.6",
"ring",
- "rustls 0.23.42",
+ "rustls 0.23.43",
"tokio",
"tokio-postgres",
"tokio-rustls 0.26.4",
@@ -8568,13 +8689,24 @@ dependencies = [
"tokio",
]
+[[package]]
+name = "tokio-rustls"
+version = "0.25.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f"
+dependencies = [
+ "rustls 0.22.4",
+ "rustls-pki-types",
+ "tokio",
+]
+
[[package]]
name = "tokio-rustls"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61"
dependencies = [
- "rustls 0.23.42",
+ "rustls 0.23.43",
"tokio",
]
@@ -8598,7 +8730,7 @@ dependencies = [
"futures-util",
"log",
"tokio",
- "tungstenite",
+ "tungstenite 0.29.0",
]
[[package]]
@@ -8646,9 +8778,9 @@ dependencies = [
[[package]]
name = "toml"
-version = "1.1.3+spec-1.1.0"
+version = "1.1.4+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "53c96ecdfa941c8fc4fcaed14f99ada8ebed502eef533015095a07e3301d4c3c"
+checksum = "3aace63f4bbcdfc2c965b059de67119c89c4017a70d633be6c104910f67056f5"
dependencies = [
"indexmap 2.14.0",
"serde_core",
@@ -8724,9 +8856,9 @@ dependencies = [
[[package]]
name = "toml_parser"
-version = "1.1.2+spec-1.1.0"
+version = "1.1.3+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a2abe9b86193656635d2411dc43050282ca48aa31c2451210f4202550afb7526"
+checksum = "1d38ac1cf9b95face32296c0a3ede1fdc270627c9d9c02a7274dd6d960dc4d56"
dependencies = [
"winnow 1.0.4",
]
@@ -8858,9 +8990,9 @@ dependencies = [
[[package]]
name = "tray-icon"
-version = "0.24.1"
+version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "65ba1e5f6b9ef9fd87e21b9c6f351554dbd717960089168fcfdef854686961dc"
+checksum = "045979e3f037cd18ad1cb2a419dfda133c5c29c9f3453370079f2255d46c257e"
dependencies = [
"crossbeam-channel",
"dirs",
@@ -8895,6 +9027,27 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
+[[package]]
+name = "tungstenite"
+version = "0.21.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1"
+dependencies = [
+ "byteorder",
+ "bytes",
+ "data-encoding",
+ "http",
+ "httparse",
+ "log",
+ "rand 0.8.7",
+ "rustls 0.22.4",
+ "rustls-pki-types",
+ "sha1 0.10.7",
+ "thiserror 1.0.69",
+ "url",
+ "utf-8",
+]
+
[[package]]
name = "tungstenite"
version = "0.29.0"
@@ -9121,6 +9274,12 @@ dependencies = [
"url",
]
+[[package]]
+name = "utf-8"
+version = "0.7.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
+
[[package]]
name = "utf8_iter"
version = "1.0.4"
@@ -10252,6 +10411,26 @@ dependencies = [
"x11-dl",
]
+[[package]]
+name = "ws_stream_tungstenite"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a198f414f083fb19fcc1bffcb0fa0cf46d33ccfa229adf248cac12c180e91609"
+dependencies = [
+ "async-tungstenite",
+ "async_io_stream",
+ "bitflags 2.13.1",
+ "futures-core",
+ "futures-io",
+ "futures-sink",
+ "futures-util",
+ "pharos",
+ "rustc_version",
+ "tokio",
+ "tracing",
+ "tungstenite 0.21.0",
+]
+
[[package]]
name = "wyz"
version = "0.5.1"
diff --git a/Cargo.toml b/Cargo.toml
index d6672554f0..0b37b46816 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,12 +1,14 @@
[workspace]
resolver = "2"
members = ["src-tauri", "crates/dbx-core", "crates/dbx-web", "crates/dbx-mcp", "crates/dbx-cli"]
-exclude = ["vendor/ctor"]
+exclude = ["vendor/ctor", "vendor/rumqttc"]
[patch.crates-io]
# Tauri 2.11 uses ctor 0.8, which excludes Rust's win7 vendor. This vendors
# upstream rust-ctor#443 until tauri-utils moves to ctor 1.x.
ctor = { path = "vendor/ctor" }
+# rumqttc 0.24 hardcodes MQTT 3.1.1; this patch adds MQTT 3.1 CONNECT encoding.
+rumqttc = { path = "vendor/rumqttc" }
tokio-postgres = { git = "https://github.com/t8y2/tokio-postgres-gaussdb.git", rev = "115f9fef10f0fc3669b5337955e4eb461fc349a6" }
postgres-types = { git = "https://github.com/t8y2/tokio-postgres-gaussdb.git", rev = "115f9fef10f0fc3669b5337955e4eb461fc349a6" }
postgres-protocol = { git = "https://github.com/t8y2/tokio-postgres-gaussdb.git", rev = "115f9fef10f0fc3669b5337955e4eb461fc349a6" }
diff --git a/apps/desktop/public/icons/database/mqtt.png b/apps/desktop/public/icons/database/mqtt.png
new file mode 100644
index 0000000000..28b7223e14
Binary files /dev/null and b/apps/desktop/public/icons/database/mqtt.png differ
diff --git a/apps/desktop/public/icons/database/mqtt.svg b/apps/desktop/public/icons/database/mqtt.svg
new file mode 100644
index 0000000000..237b47c7d4
--- /dev/null
+++ b/apps/desktop/public/icons/database/mqtt.svg
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128" width="128" height="128">
+ <path d="M7.67 0.06C45.49 0.06 83.31 0.06 121.13 0.06C121.21 0.65 121.28 1.24 121.36 1.83C123.56 2.62 125.38 4.57 126.5 6.59C126.98 6.61 127.46 6.63 127.94 6.64C127.94 45.1 127.94 83.55 127.94 122C127.57 122 127.2 122 126.83 122C125.22 123.61 123.61 125.22 122 126.83C122 127.2 122 127.57 122 127.94C83.55 127.94 45.1 127.94 6.64 127.94C6.63 127.46 6.61 126.98 6.59 126.5C4.57 125.38 2.62 123.56 1.83 121.36C1.24 121.28 0.65 121.21 0.06 121.13C0.06 83.31 0.06 45.49 0.06 7.67C1.16 7.37 7.37 1.16 7.67 0.06ZM118.5 38.86C119.69 37.17 119.01 34.16 119.01 32.17C119.03 27.6 120.61 12.95 116.73 10.44C113.28 8.21 106.45 9.56 102.5 9.56C99.97 9.56 90.27 8.72 89 10.5C89.59 13.46 93.44 14.75 95.58 16.6C101.24 21.51 106.68 26.56 111.61 32.23C113.5 34.42 115.3 38.62 118.5 38.86ZM12.88 9.77C9.16 10.61 6.76 17.86 10.52 20C12.98 21.4 17.36 20.79 20.17 21.06C25.86 21.6 31.3 22.93 36.83 24.33C52.33 28.24 67.23 37.86 78.64 48.86C91.78 61.53 100.31 77.22 105.68 94.5C107.2 99.41 108.08 105.06 108.64 110.17C108.91 112.61 108.38 115.84 109.42 118.09C110.53 120.49 115.28 119.91 116.92 118.42C120.43 115.22 119.03 108.8 119.03 104.5C119.03 97.35 120.23 82.19 118.84 75.98C118.17 72.99 116.45 70.36 115.19 67.64C106.3 48.39 90.25 31.92 72.45 20.72C65.69 16.46 58.12 11.75 50.25 9.87C43.71 8.3 35.56 9.56 28.83 9.56C23.79 9.56 17.81 8.67 12.88 9.77ZM11.87 37.15C10.91 37.22 9.79 37.55 9.27 38.44C8.35 40.05 8.71 53.63 10.16 54.67C12.05 56.04 15.62 55.62 17.85 55.88C22.4 56.4 26.73 57.72 31.07 59.13C42.46 62.79 53.62 71.59 60.69 81.14C65.41 87.51 69.66 95.11 71.59 102.85C72.34 105.85 73.31 116.89 74.59 118.58C75.71 120.04 90.94 119.93 92.08 118.59C93.64 116.75 92.43 112.32 92.08 110.16C90.8 102.39 89.02 95.14 85.92 87.9C75.52 63.56 53.88 45.48 28.13 39.43C22.89 38.19 17.32 36.76 11.87 37.15ZM10.92 72.14C8.42 72.75 8.98 75.53 8.98 77.5C8.97 83.06 8.97 88.61 8.97 94.17C8.97 99.01 7.49 114.27 10.25 117.58C12.62 120.43 16.86 119.51 20.17 119.52C28.83 119.57 37.5 119.53 46.17 119.53C48.68 119.53 54.93 120.73 56.75 118.6C59 115.97 54.81 104.62 53.63 101.52C48.6 88.35 36.17 77.91 22.81 74.06C19.24 73.03 14.69 71.22 10.92 72.14Z" fill="#fefefe" fill-rule="evenodd" stroke="#fefefe" stroke-width="0.25" stroke-linejoin="round"/>
+ <path d="M118.5 38.86C115.3 38.62 113.5 34.42 111.61 32.23C106.68 26.56 101.24 21.51 95.58 16.6C93.44 14.75 89.59 13.46 89 10.5C90.27 8.72 99.97 9.56 102.5 9.56C106.45 9.56 113.28 8.21 116.73 10.44C120.61 12.95 119.03 27.6 119.01 32.17C119.01 34.16 119.69 37.17 118.5 38.86ZM12.88 9.77C17.81 8.67 23.79 9.56 28.83 9.56C35.56 9.56 43.71 8.3 50.25 9.87C58.12 11.75 65.69 16.46 72.45 20.72C90.25 31.92 106.3 48.39 115.19 67.64C116.45 70.36 118.17 72.99 118.84 75.98C120.23 82.19 119.03 97.35 119.03 104.5C119.03 108.8 120.43 115.22 116.92 118.42C115.28 119.91 110.53 120.49 109.42 118.09C108.38 115.84 108.91 112.61 108.64 110.17C108.08 105.06 107.2 99.41 105.68 94.5C100.31 77.22 91.78 61.53 78.64 48.86C67.23 37.86 52.33 28.24 36.83 24.33C31.3 22.93 25.86 21.6 20.17 21.06C17.36 20.79 12.98 21.4 10.52 20C6.76 17.86 9.16 10.61 12.88 9.77ZM11.87 37.15C17.32 36.76 22.89 38.19 28.13 39.43C53.88 45.48 75.52 63.56 85.92 87.9C89.02 95.14 90.8 102.39 92.08 110.16C92.43 112.32 93.64 116.75 92.08 118.59C90.94 119.93 75.71 120.04 74.59 118.58C73.31 116.89 72.34 105.85 71.59 102.85C69.66 95.11 65.41 87.51 60.69 81.14C53.62 71.59 42.46 62.79 31.07 59.13C26.73 57.72 22.4 56.4 17.85 55.88C15.62 55.62 12.05 56.04 10.16 54.67C8.71 53.63 8.35 40.05 9.27 38.44C9.79 37.55 10.91 37.22 11.87 37.15ZM10.92 72.14C14.69 71.22 19.24 73.03 22.81 74.06C36.17 77.91 48.6 88.35 53.63 101.52C54.81 104.62 59 115.97 56.75 118.6C54.93 120.73 48.68 119.53 46.17 119.53C37.5 119.53 28.83 119.57 20.17 119.52C16.86 119.51 12.62 120.43 10.25 117.58C7.49 114.27 8.97 99.01 8.97 94.17C8.97 88.61 8.97 83.06 8.98 77.5C8.98 75.53 8.42 72.75 10.92 72.14Z" fill="#670266" fill-rule="evenodd" stroke="#670266" stroke-width="0.25" stroke-linejoin="round"/>
+</svg>
\ No newline at end of file
diff --git a/apps/desktop/src/components/connection/ConnectionDialog.vue b/apps/desktop/src/components/connection/ConnectionDialog.vue
index 684d18acf4..526461d358 100644
--- a/apps/desktop/src/components/connection/ConnectionDialog.vue
+++ b/apps/desktop/src/components/connection/ConnectionDialog.vue
@@ -17,6 +17,7 @@ import { Switch } from "@/components/ui/switch";
import type { ConnectionConfig, ConnectionTestResult, DatabaseConnectionInfo, DatabaseType, HttpTunnelConfig, IdentifierCase, JdbcDriverInfo, JdbcLocalBundleInfo, JdbcMavenBundleInfo, ProxyTunnelConfig, SshConfigHostEntry, SshTunnelConfig, TransportLayerConfig } from "@/types/database";
import type { InfluxDbExternalConfig, InfluxDbVersion } from "@/types/influxdb";
import type { MqAdminConfig, MqAuth, MqSystemKind } from "@/types/mq";
+import type { MqttConnectionConfig } from "@/types/mqtt";
import type { NacosAdminConfig, NacosAuthConfig, NacosImplementation, NacosMetricsMode, NacosRNacosConsoleAuth, NacosVersionMode } from "@/types/nacos";
import { CONNECTION_ATTEMPT_CANCELLED_MESSAGE, useConnectionStore } from "@/stores/connectionStore";
import { useTunnelProfileStore } from "@/stores/tunnelProfileStore";
@@ -657,6 +658,23 @@ const nacosTlsSkipVerify = ref(false);
const nacosMetricsMode = ref<NacosMetricsMode>("auto");
const nacosMetricsUrl = ref("");
const nacosPageSize = ref(20);
+
+// --- MQTT-specific form fields ---
+const mqttHost = ref("127.0.0.1");
+const mqttPort = ref(1883);
+const mqttClientId = ref("");
+const mqttProtocolVersion = ref<"v3" | "v4" | "v5">("v5");
+const mqttTransportMode = ref<"tcp" | "websocket">("tcp");
+const mqttWsPath = ref("/mqtt");
+const mqttAuthKind = ref<"none" | "password">("none");
+const mqttUsername = ref("");
+const mqttPassword = ref("");
+const mqttTls = ref(false);
+const mqttTlsSkipVerify = ref(false);
+const mqttKeepAliveSecs = ref(60);
+const mqttConnectTimeoutSecs = ref(30);
+const mqttMaxPacketSizeBytes = ref(16 * 1024 * 1024);
+
const nacosPrimaryAddressLabel = computed(() => {
if (nacosImplementation.value === "rnacos") return t("connection.nacosPrimaryAddressRNacos");
if (nacosVersionMode.value === "v2") return t("connection.nacosPrimaryAddressV2");
@@ -973,6 +991,7 @@ const driverProfiles: Record<
rocketmq: { type: "mq", port: 9876, user: "", label: "Apache RocketMQ", icon: "rocketmq", host: "127.0.0.1" },
rabbitmq: { type: "mq", port: 5672, user: "", label: "RabbitMQ", icon: "rabbitmq", host: "127.0.0.1" },
nacos: { type: "nacos", port: 8848, user: "nacos", label: "Nacos", icon: "nacos", host: "127.0.0.1" },
+ mqtt: { type: "mqtt", port: 1883, user: "", label: "MQTT", icon: "mqtt", host: "127.0.0.1" },
iris: { type: "iris", port: 1972, user: "_SYSTEM", label: "IRIS", icon: "iris" },
influxdb: { type: "influxdb", port: 8086, user: "", label: "InfluxDB", icon: "InfluxDB" },
custom_mysql: {
@@ -1188,6 +1207,57 @@ function hydrateNacosFields(value: unknown) {
resetNacosFields(value as Partial<NacosAdminConfig>);
}
+function resetMqttFields(config?: Partial<MqttConnectionConfig>) {
+ mqttHost.value = config?.host?.trim() || "127.0.0.1";
+ mqttPort.value = config?.port || 1883;
+ mqttClientId.value = config?.clientId || "";
+ mqttProtocolVersion.value = config?.protocolVersion || "v5";
+ mqttTransportMode.value = config?.transport || "tcp";
+ mqttWsPath.value = config?.wsPath || "/mqtt";
+ mqttTls.value = config?.tls || false;
+ mqttTlsSkipVerify.value = config?.tlsSkipVerify || false;
+ mqttKeepAliveSecs.value = Math.max(1, config?.keepAliveSecs || 60);
+ mqttConnectTimeoutSecs.value = Math.max(1, config?.connectTimeoutSecs || 30);
+ mqttMaxPacketSizeBytes.value = Math.min(268435455, Math.max(1024, config?.maxPacketSizeBytes || 16 * 1024 * 1024));
+ const auth = config?.auth;
+ if (auth && auth.kind === "password") {
+ mqttAuthKind.value = "password";
+ mqttUsername.value = auth.username || "";
+ mqttPassword.value = auth.password || "";
+ } else {
+ mqttAuthKind.value = "none";
+ mqttUsername.value = "";
+ mqttPassword.value = "";
+ }
+}
+
+function hydrateMqttFields(value: unknown) {
+ if (!value || typeof value !== "object") {
+ resetMqttFields();
+ return;
+ }
+ resetMqttFields(value as Partial<MqttConnectionConfig>);
+}
+
+function buildMqttExternalConfig(): MqttConnectionConfig {
+ const auth: MqttConnectionConfig["auth"] = mqttAuthKind.value === "password" ? { kind: "password", username: mqttUsername.value, password: mqttPassword.value } : { kind: "none" };
+
+ return {
+ host: mqttHost.value.trim(),
+ port: mqttPort.value,
+ clientId: mqttClientId.value.trim() || `dbx-${Math.random().toString(36).slice(2, 10)}`,
+ protocolVersion: mqttProtocolVersion.value,
+ transport: mqttTransportMode.value,
+ tls: mqttTls.value,
+ tlsSkipVerify: mqttTlsSkipVerify.value,
+ auth,
+ keepAliveSecs: Math.max(1, mqttKeepAliveSecs.value),
+ connectTimeoutSecs: Math.max(1, mqttConnectTimeoutSecs.value),
+ maxPacketSizeBytes: Math.min(268435455, Math.max(1024, mqttMaxPacketSizeBytes.value)),
+ wsPath: mqttTransportMode.value === "websocket" ? mqttWsPath.value || "/mqtt" : undefined,
+ };
+}
+
const influxDbVersion = ref<InfluxDbVersion>("1");
const influxDbOrg = ref("");
@@ -1957,6 +2027,12 @@ function applyProfile(val: string, preserveConnectionFields = false) {
form.value.connection_string = undefined;
form.value.url_params = "";
}
+ if (profile.type === "mqtt") {
+ resetMqttFields();
+ form.value.database = undefined;
+ form.value.connection_string = undefined;
+ form.value.url_params = "";
+ }
if (profile.type === "influxdb") {
resetInfluxDbFields();
form.value.database = undefined;
@@ -2060,6 +2136,11 @@ watch(
} else {
resetNacosFields();
}
+ if (config.db_type === "mqtt") {
+ hydrateMqttFields(config.external_config);
+ } else {
+ resetMqttFields();
+ }
if (config.db_type === "influxdb") {
hydrateInfluxDbFields(config.external_config);
} else {
@@ -2304,6 +2385,7 @@ const iconTypeMap: Record<string, string> = {
rocketmq: "rocketmq",
rabbitmq: "rabbitmq",
nacos: "nacos",
+ mqtt: "mqtt",
dm: "dm",
h2: "h2",
snowflake: "snowflake",
@@ -2403,6 +2485,7 @@ const dbOptions: DbOption[] = [
{ value: "kafka", label: "Apache Kafka" },
{ value: "rocketmq", label: "Apache RocketMQ" },
{ value: "rabbitmq", label: "RabbitMQ" },
+ { value: "mqtt", label: "MQTT" },
{ value: "nacos", label: "Nacos" },
{ value: "influxdb", label: "InfluxDB" },
{ value: "iris", label: "IRIS" },
@@ -2456,7 +2539,7 @@ const dbCategoryDefinitions: Array<{
{
key: "mq",
titleKey: "connection.databaseCategoryMq",
- optionValues: ["mq", "kafka", "rocketmq", "rabbitmq"],
+ optionValues: ["mq", "kafka", "rocketmq", "rabbitmq", "mqtt"],
},
{
key: "registry_config",
@@ -2864,6 +2947,7 @@ const hasRequiredConnectionTarget = computed(() => {
return !!mqAdminUrl.value.trim();
}
if (form.value.db_type === "zookeeper") return !!(form.value.host || form.value.connection_string || connectionUrlInput.value.trim());
+ if (form.value.db_type === "mqtt") return !!mqttHost.value.trim() && mqttPort.value > 0;
if (form.value.db_type === "nacos") return !!nacosServerAddr.value.trim();
if (isCloudflareD1Connection(form.value)) return hasCloudflareD1Credentials(form.value);
if (isH2FileMode.value) return !!(form.value.host.trim() || h2FilePathFromJdbcUrl(form.value.connection_string));
@@ -3264,6 +3348,19 @@ function connectionConfigForSubmit(id: string, generatedName = ""): ConnectionCo
config.database = nacosConfig.namespace || undefined;
config.connection_string = undefined;
config.url_params = "";
+ } else if (config.db_type === "mqtt") {
+ const mqttConfig = buildMqttExternalConfig();
+ config.external_config = mqttConfig;
+ config.driver_profile = "mqtt";
+ config.driver_label = "MQTT";
+ config.host = mqttConfig.host;
+ config.port = mqttConfig.port;
+ config.ssl = mqttConfig.tls;
+ config.username = "";
+ config.password = "";
+ config.database = undefined;
+ config.connection_string = undefined;
+ config.url_params = "";
} else if (config.db_type === "influxdb") {
config.external_config = buildInfluxDbExternalConfig();
config.connection_string = undefined;
@@ -5851,6 +5948,81 @@ function openExternalUrl(url: string) {
</template>
</template>
+ <!-- MQTT: broker address, client ID, protocol version, auth, TLS -->
+ <template v-else-if="form.db_type === 'mqtt'">
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttBrokerAddress") }}</Label>
+ <Input v-model="mqttHost" class="col-span-3" :placeholder="t('connection.mqttBrokerAddressPlaceholder')" />
+ </div>
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttBrokerPort") }}</Label>
+ <Input v-model.number="mqttPort" type="number" class="col-span-3 w-24" min="1" max="65535" />
+ </div>
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttClientId") }}</Label>
+ <Input v-model="mqttClientId" class="col-span-3" :placeholder="t('connection.mqttClientIdPlaceholder')" />
+ </div>
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttProtocolVersion") }}</Label>
+ <select v-model="mqttProtocolVersion" class="col-span-3 flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring">
+ <option value="v5">MQTT 5.0</option>
+ <option value="v4">MQTT 3.1.1</option>
+ <option value="v3">MQTT 3.1</option>
+ </select>
+ </div>
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttTransport") }}</Label>
+ <div class="col-span-3 flex gap-2">
+ <Button size="sm" :variant="mqttTransportMode === 'tcp' ? 'default' : 'outline'" @click="mqttTransportMode = 'tcp'">{{ t("connection.mqttTransportTcp") }}</Button>
+ <Button size="sm" :variant="mqttTransportMode === 'websocket' ? 'default' : 'outline'" @click="mqttTransportMode = 'websocket'">{{ t("connection.mqttTransportWebSocket") }}</Button>
+ </div>
+ </div>
+ <div v-if="mqttTransportMode === 'websocket'" class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttWsPath") }}</Label>
+ <Input v-model="mqttWsPath" class="col-span-3" :placeholder="t('connection.mqttWsPathPlaceholder')" />
+ </div>
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqAuth") }}</Label>
+ <div class="col-span-3 flex gap-2">
+ <Button size="sm" :variant="mqttAuthKind === 'none' ? 'default' : 'outline'" @click="mqttAuthKind = 'none'">{{ t("connection.mqAuthNone") }}</Button>
+ <Button size="sm" :variant="mqttAuthKind === 'password' ? 'default' : 'outline'" @click="mqttAuthKind = 'password'">{{ t("connection.mqAuthBasic") }}</Button>
+ </div>
+ </div>
+ <template v-if="mqttAuthKind === 'password'">
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttUsername") }}</Label>
+ <Input v-model="mqttUsername" class="col-span-3" :placeholder="t('connection.mqttUsernamePlaceholder')" />
+ </div>
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttPassword") }}</Label>
+ <Input v-model="mqttPassword" type="password" class="col-span-3" :placeholder="t('connection.mqttPasswordPlaceholder')" />
+ </div>
+ </template>
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttTls") }}</Label>
+ <div class="col-span-3 flex items-center gap-2">
+ <Switch :checked="mqttTls" @update:checked="mqttTls = $event" />
+ <Label class="text-sm" :class="mqttTls ? '' : 'text-muted-foreground'">TLS</Label>
+ <template v-if="mqttTls">
+ <Switch :checked="mqttTlsSkipVerify" @update:checked="mqttTlsSkipVerify = $event" class="ml-4" />
+ <Label class="text-sm" :class="mqttTlsSkipVerify ? '' : 'text-muted-foreground'">{{ t("connection.mqttTlsSkipVerify") }}</Label>
+ </template>
+ </div>
+ </div>
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttKeepAlive") }}</Label>
+ <Input v-model.number="mqttKeepAliveSecs" type="number" class="col-span-3 w-32" min="1" max="65535" />
+ </div>
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">{{ t("connection.mqttConnectTimeout") }}</Label>
+ <Input v-model.number="mqttConnectTimeoutSecs" type="number" class="col-span-3 w-32" min="1" max="300" />
+ </div>
+ <div class="grid grid-cols-4 items-center gap-4">
+ <Label :class="connectionLabelClass">最大报文(字节)</Label>
+ <Input v-model.number="mqttMaxPacketSizeBytes" type="number" class="col-span-3 w-40" min="1024" max="268435455" />
+ </div>
+ </template>
+
<!-- InfluxDB: v1 username/password or v2 token/org/bucket -->
<template v-else-if="form.db_type === 'influxdb'">
<div class="grid grid-cols-4 items-center gap-4">
diff --git a/apps/desktop/src/components/icons/DatabaseIcon.vue b/apps/desktop/src/components/icons/DatabaseIcon.vue
index 1a481017b2..c53af496f8 100644
--- a/apps/desktop/src/components/icons/DatabaseIcon.vue
+++ b/apps/desktop/src/components/icons/DatabaseIcon.vue
@@ -103,6 +103,7 @@ const assetIcons: Record<string, string> = {
zookeeper: "zookeeper",
oscar: "oscar.png",
jdbcx: "jdbcx",
+ mqtt: "mqtt",
};
const normalizedType = computed(() => props.dbType.toLowerCase().replace(/[\s-]+/g, "_"));
diff --git a/apps/desktop/src/components/layout/ContentArea.vue b/apps/desktop/src/components/layout/ContentArea.vue
index c4434f8aca..249591d516 100644
--- a/apps/desktop/src/components/layout/ContentArea.vue
+++ b/apps/desktop/src/components/layout/ContentArea.vue
@@ -58,6 +58,7 @@ const VectorBrowser = defineAsyncComponent(() => import("@/components/vector/Vec
const HBaseBrowser = defineAsyncComponent(() => import("@/components/hbase/HBaseBrowser.vue"));
const ElasticsearchJsonResponsePanel = defineAsyncComponent(() => import("@/components/common/ElasticsearchJsonResponsePanel.vue"));
const MqAdminConsole = defineAsyncComponent(() => import("@/components/mq/MqAdminConsole.vue"));
+const MqttAdminConsole = defineAsyncComponent(() => import("@/components/mqtt/MqttAdminConsole.vue"));
const NacosAdminConsole = defineAsyncComponent(() => import("@/components/nacos/NacosAdminConsole.vue"));
const NacosDashboard = defineAsyncComponent(() => import("@/components/nacos/NacosDashboard.vue"));
const ObjectBrowser = defineAsyncComponent(() => import("@/components/objects/ObjectBrowser.vue"));
@@ -1878,6 +1879,12 @@ defineExpose({ focusSearch, refreshData, refreshQueryEditorCompletionCache, hand
</div>
</template>
+ <template v-else-if="activeTab.mode === 'mqtt'">
+ <div class="flex-1 min-h-0">
+ <MqttAdminConsole :key="activeTab.id" :connection-id="activeTab.connectionId" :initial-topic="activeTab.mqttInitialTopic" />
+ </div>
+ </template>
+
<template v-else-if="activeTab.mode === 'nacos'">
<div class="flex-1 min-h-0">
<NacosAdminConsole
diff --git a/apps/desktop/src/components/mqtt/MqttAdminConsole.vue b/apps/desktop/src/components/mqtt/MqttAdminConsole.vue
new file mode 100644
index 0000000000..a11e163e16
--- /dev/null
+++ b/apps/desktop/src/components/mqtt/MqttAdminConsole.vue
@@ -0,0 +1,246 @@
+<script setup lang="ts">
+import { ref, computed, onMounted, onUnmounted } from "vue";
+import { useI18n } from "vue-i18n";
+import { mqttGetBrokerInfo, mqttListTopics, mqttGetTopicTree, mqttGetMessages, mqttSubscribe, mqttUnsubscribe, mqttClearMessages } from "@/lib/backend/api";
+import type { MqttBrokerInfo, MqttTopicNode, MqttMessage } from "@/types/mqtt";
+import { Button } from "@/components/ui/button";
+import TopicTreeNode from "./TopicTreeNode.vue";
+import MqttPublishPanel from "./MqttPublishDialog.vue";
+import { decodePayload, PAYLOAD_ENCODINGS, PAYLOAD_ENCODING_LABELS, type PayloadEncoding } from "@/lib/mqtt/mqttPayloadCodec";
+
+interface Props {
+ connectionId: string;
+ initialTopic?: string;
+}
+const props = defineProps<Props>();
+const { t } = useI18n();
+
+/* ========== State ========== */
+const brokerInfo = ref<MqttBrokerInfo | null>(null);
+const topicTree = ref<MqttTopicNode | null>(null);
+const messages = ref<MqttMessage[]>([]);
+const subscribedTopics = ref<[string, string][]>([]);
+const selectedTopic = ref<string>(props.initialTopic ?? "");
+const loading = ref(true);
+const error = ref<string | null>(null);
+const pollingTimer = ref<ReturnType<typeof setInterval> | null>(null);
+
+/* Payload 显示编码格式 */
+const displayEncoding = ref<PayloadEncoding>("plaintext");
+
+/* ========== Computed ========== */
+const connected = computed(() => brokerInfo.value?.connected ?? false);
+
+/* ========== 数据刷新 ========== */
+async function refreshData() {
+ try {
+ const [info, topics, tree, msgs] = await Promise.all([
+ mqttGetBrokerInfo(props.connectionId) as Promise<MqttBrokerInfo>,
+ mqttListTopics(props.connectionId) as Promise<[string, string][]>,
+ mqttGetTopicTree(props.connectionId) as Promise<MqttTopicNode>,
+ mqttGetMessages(props.connectionId, selectedTopic.value || undefined, 50) as Promise<MqttMessage[]>,
+ ]);
+ brokerInfo.value = info;
+ subscribedTopics.value = topics;
+ topicTree.value = tree;
+ messages.value = msgs;
+ error.value = null;
+ } catch (e) {
+ error.value = String(e);
+ } finally {
+ loading.value = false;
+ }
+}
+
+async function handleSubscribe(topic: string) {
+ try {
+ await mqttSubscribe(props.connectionId, topic, "atmostonce");
+ await refreshData();
+ } catch (e) {
+ error.value = String(e);
+ }
+}
+
+async function handleUnsubscribe(topics: string[]) {
+ const subscribed = new Set(subscribedTopics.value.map(([topic]) => topic));
+ const targets = [...new Set(topics)].filter((topic) => subscribed.has(topic));
+ if (targets.length === 0) {
+ error.value = t("connection.mqttUnsubscribeMissing");
+ return;
+ }
+
+ const prompt = targets.length === 1 ? t("connection.mqttUnsubscribeConfirm", { topic: targets[0] }) : t("connection.mqttUnsubscribeGroupConfirm", { count: targets.length });
+ if (!window.confirm(prompt)) return;
+
+ const failures: string[] = [];
+ for (const topic of targets) {
+ try {
+ await mqttUnsubscribe(props.connectionId, topic);
+ } catch (e) {
+ failures.push(`${topic}:${String(e)}`);
+ }
+ }
+
+ if (targets.includes(selectedTopic.value)) selectedTopic.value = "";
+ await refreshData();
+ if (failures.length > 0) {
+ error.value = t("connection.mqttUnsubscribeFailed", { count: failures.length, errors: failures.join("; ") });
+ }
+}
+
+function handleTopicClick(topic: string) {
+ selectedTopic.value = topic;
+ refreshData();
+}
+
+function handleMessagePublished() {
+ refreshData();
+}
+
+async function handleClearMessages() {
+ try {
+ await mqttClearMessages(props.connectionId);
+ messages.value = [];
+ } catch (e) {
+ error.value = String(e);
+ }
+}
+
+/* ========== 消息轮询 ========== */
+function startPolling() {
+ stopPolling();
+ pollingTimer.value = setInterval(async () => {
+ try {
+ const msgs = (await mqttGetMessages(props.connectionId, selectedTopic.value || undefined, 50)) as MqttMessage[];
+ messages.value = msgs;
+ } catch {
+ /* 忽略轮询错误 */
+ }
+ }, 3000);
+}
+
+function stopPolling() {
+ if (pollingTimer.value) {
+ clearInterval(pollingTimer.value);
+ pollingTimer.value = null;
+ }
+}
+
+/* ========== 格式化消息 Payload ========== */
+function formatMessagePayload(msg: MqttMessage): string {
+ if (displayEncoding.value === "plaintext" && msg.payloadText != null) {
+ return msg.payloadText;
+ }
+ return decodePayload(msg.payloadBase64, displayEncoding.value);
+}
+
+/* ========== 生命周期 ========== */
+onMounted(async () => {
+ await refreshData();
+ startPolling();
+});
+
+onUnmounted(() => {
+ stopPolling();
+});
+</script>
+
+<template>
+ <div class="flex flex-col h-full bg-card text-card-foreground">
+ <!-- Header -->
+ <div class="flex items-center justify-between px-4 py-2 border-b shrink-0">
+ <div class="flex items-center gap-3">
+ <span class="text-lg font-semibold">{{ t("connection.mqttConsoleTitle") }}</span>
+ <span v-if="brokerInfo" class="text-sm text-muted-foreground">{{ brokerInfo.brokerUrl }}</span>
+ <span v-if="brokerInfo" class="px-1.5 py-0.5 rounded text-xs font-medium" :class="connected ? 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400' : 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400'">
+ {{ connected ? t("connection.mqttConnected") : t("connection.mqttDisconnected") }}
+ </span>
+ </div>
+ <div class="flex items-center gap-2">
+ <Button size="sm" variant="outline" @click="refreshData">{{ t("connection.mqttRefresh") }}</Button>
+ <Button size="sm" variant="outline" @click="handleClearMessages">{{ t("connection.mqttClearMessages") }}</Button>
+ </div>
+ </div>
+
+ <!-- Error banner -->
+ <div v-if="error" class="px-4 py-2 bg-red-50 dark:bg-red-950/30 text-red-700 dark:text-red-400 text-sm border-b">
+ {{ error }}
+ <button class="ml-2 underline" @click="error = null">{{ t("common.close") }}</button>
+ </div>
+
+ <!-- Body: 双栏布局 -->
+ <div class="flex-1 flex min-h-0">
+ <!-- 左侧:主题树 -->
+ <div class="w-72 border-r flex flex-col shrink-0">
+ <div class="px-3 py-2 text-xs font-semibold text-muted-foreground uppercase border-b">{{ t("connection.mqttTopics") }}</div>
+ <div class="flex-1 overflow-auto p-2">
+ <div v-if="loading" class="text-xs text-muted-foreground p-2">{{ t("common.loading") }}</div>
+ <div v-else-if="!topicTree?.children?.length" class="text-xs text-muted-foreground p-2">{{ t("connection.mqttNoTopicsHint") }}</div>
+ <TopicTreeNode v-for="child in topicTree?.children ?? []" :key="child.fullPath" :node="child" :depth="0" :selected-topic="selectedTopic" :subscribed-topics="subscribedTopics" @select="handleTopicClick" @subscribe="handleSubscribe" @unsubscribe="handleUnsubscribe" />
+ </div>
+ <!-- 快速订阅 -->
+ <div class="border-t p-2">
+ <form
+ class="flex gap-1"
+ @submit.prevent="
+ (e) => {
+ const input = (e.target as HTMLFormElement).querySelector('input');
+ const topic = input?.value.trim();
+ if (topic && input) {
+ handleSubscribe(topic);
+ input.value = '';
+ }
+ }
+ "
+ >
+ <input class="flex-1 h-7 px-2 text-xs rounded border bg-transparent" :placeholder="t('connection.mqttSubscribePlaceholder')" />
+ <Button size="sm" variant="ghost" class="h-7 text-xs" type="submit">{{ t("connection.mqttSubscribe") }}</Button>
+ </form>
+ </div>
+ </div>
+
+ <!-- 右侧:消息列表 + 发布面板 -->
+ <div class="flex-1 flex flex-col min-w-0">
+ <!-- 消息区域 -->
+ <div class="flex-1 flex flex-col min-h-0">
+ <div class="px-3 py-2 text-xs font-semibold text-muted-foreground uppercase border-b flex items-center justify-between shrink-0">
+ <span v-if="selectedTopic">{{ t("connection.mqttMessagesForTopic", { topic: selectedTopic }) }}</span>
+ <span v-else>{{ t("connection.mqttAllMessages") }}</span>
+ <div class="flex items-center gap-2">
+ <span class="font-normal">{{ messages.length }}</span>
+ <!-- Payload 编码格式选择 -->
+ <select v-model="displayEncoding" class="h-6 px-1.5 text-[11px] rounded border bg-transparent text-muted-foreground cursor-pointer outline-none">
+ <option v-for="enc in PAYLOAD_ENCODINGS" :key="enc" :value="enc">
+ {{ PAYLOAD_ENCODING_LABELS[enc] }}
+ </option>
+ </select>
+ </div>
+ </div>
+ <div class="flex-1 overflow-auto">
+ <div v-if="messages.length === 0" class="text-xs text-muted-foreground p-4 text-center">{{ t("connection.mqttNoMessages") }}</div>
+ <div
+ v-for="(msg, i) in messages"
+ :key="i"
+ class="px-3 py-2 border-b last:border-b-0 cursor-pointer text-xs"
+ :class="msg.direction === 'sent' ? 'bg-emerald-50/60 dark:bg-emerald-950/20 hover:bg-emerald-100/60 dark:hover:bg-emerald-950/40' : 'hover:bg-muted/50'"
+ @click="handleTopicClick(msg.topic)"
+ >
+ <div class="flex items-center gap-2 mb-0.5">
+ <span v-if="msg.direction === 'sent'" class="text-emerald-600 dark:text-emerald-400 text-[10px] font-bold shrink-0">{{ t("connection.mqttSent") }}</span>
+ <span v-else class="text-blue-600 dark:text-blue-400 text-[10px] font-bold shrink-0">{{ t("connection.mqttReceived") }}</span>
+ <span class="font-mono text-blue-600 dark:text-blue-400 font-medium truncate">{{ msg.topic }}</span>
+ <span class="text-muted-foreground/60 shrink-0">QoS{{ msg.qos }}</span>
+ <span v-if="msg.retain" class="text-amber-600 dark:text-amber-400 text-[10px] font-medium shrink-0">{{ t("connection.mqttRetained") }}</span>
+ </div>
+ <div class="text-muted-foreground font-mono whitespace-pre-wrap break-all">{{ formatMessagePayload(msg) }}</div>
+ <div class="text-muted-foreground/50 mt-0.5 text-[10px]">{{ new Date(msg.receivedAtMs).toLocaleTimeString() }}</div>
+ </div>
+ </div>
+ </div>
+
+ <!-- 底部:内联发布面板 -->
+ <MqttPublishPanel v-if="connected" :connection-id="connectionId" :initial-topic="selectedTopic" @published="handleMessagePublished" />
+ </div>
+ </div>
+ </div>
+</template>
diff --git a/apps/desktop/src/components/mqtt/MqttPublishDialog.vue b/apps/desktop/src/components/mqtt/MqttPublishDialog.vue
new file mode 100644
index 0000000000..ddfcece43c
--- /dev/null
+++ b/apps/desktop/src/components/mqtt/MqttPublishDialog.vue
@@ -0,0 +1,352 @@
+<script setup lang="ts">
+import { computed, ref, watch } from "vue";
+import { useI18n } from "vue-i18n";
+import { mqttPublish } from "@/lib/backend/api";
+import { Button } from "@/components/ui/button";
+import { PAYLOAD_ENCODINGS, PAYLOAD_ENCODING_LABELS, encodePayload, type PayloadEncoding } from "@/lib/mqtt/mqttPayloadCodec";
+
+interface Props {
+ connectionId: string;
+ initialTopic?: string;
+}
+
+const props = defineProps<Props>();
+const emit = defineEmits<{
+ published: [];
+}>();
+const { t } = useI18n();
+
+const topic = ref(props.initialTopic || "");
+const qos = ref<0 | 1 | 2>(0);
+const retain = ref(false);
+const payloadText = ref("");
+const encoding = ref<PayloadEncoding>("plaintext");
+const loading = ref(false);
+const error = ref<string | null>(null);
+const success = ref(false);
+
+const qosLabels = ["QoS 0", "QoS 1", "QoS 2"];
+const qosHints = computed(() => [t("connection.mqttQosAtMostOnce"), t("connection.mqttQosAtLeastOnce"), t("connection.mqttQosExactlyOnce")]);
+
+/* 根据编码格式调整输入提示和占位符 */
+const payloadPlaceholder = computed(() => {
+ switch (encoding.value) {
+ case "json":
+ return t("connection.mqttPayloadPlaceholderJson");
+ case "base64":
+ return t("connection.mqttPayloadPlaceholderBase64");
+ case "hex":
+ return t("connection.mqttPayloadPlaceholderHex");
+ case "cbor":
+ case "msgpack":
+ return t("connection.mqttPayloadPlaceholderStructured", { encoding: encoding.value.toUpperCase() });
+ default:
+ return t("connection.mqttPayloadPlaceholderPlaintext");
+ }
+});
+
+/* 监听 topic prop 变化 */
+watch(
+ () => props.initialTopic,
+ (val) => {
+ if (val !== undefined && val !== topic.value) {
+ topic.value = val;
+ }
+ },
+);
+
+async function publish() {
+ const publishTopic = topic.value.trim();
+ if (!publishTopic) {
+ error.value = t("connection.mqttPublishTopicRequired");
+ return;
+ }
+ if (publishTopic.includes("#") || publishTopic.includes("+")) {
+ error.value = t("connection.mqttPublishTopicWildcard");
+ return;
+ }
+ loading.value = true;
+ error.value = null;
+ success.value = false;
+ try {
+ const payloadBase64 = encodePayload(payloadText.value, encoding.value);
+ await mqttPublish(props.connectionId, {
+ topic: publishTopic,
+ payloadBase64,
+ payloadText: encoding.value === "plaintext" ? payloadText.value : null,
+ qos: qos.value === 0 ? "atmostonce" : qos.value === 1 ? "atleastonce" : "exactlyonce",
+ retain: retain.value,
+ });
+ success.value = true;
+ emit("published");
+ /* 成功后保留表单内容便于连续测试,1.5 秒后清除成功提示 */
+ setTimeout(() => {
+ success.value = false;
+ }, 1500);
+ } catch (e) {
+ error.value = String(e);
+ } finally {
+ loading.value = false;
+ }
+}
+
+function clearForm() {
+ topic.value = "";
+ payloadText.value = "";
+ qos.value = 0;
+ retain.value = false;
+ encoding.value = "plaintext";
+ error.value = null;
+ success.value = false;
+}
+</script>
+
+<template>
+ <div class="publish-panel">
+ <div class="panel-header">
+ <span class="panel-title">{{ t("connection.mqttPublish") }}</span>
+ <Button size="sm" variant="ghost" class="h-6 text-xs" @click="clearForm">{{ t("common.clear") }}</Button>
+ </div>
+
+ <!-- Topic + QoS + Retain 行 -->
+ <div class="form-row">
+ <div class="form-group flex-1">
+ <label class="form-label">{{ t("connection.mqttTopic") }}</label>
+ <input v-model="topic" class="form-input font-mono text-xs" :placeholder="t('connection.mqttTopicExample')" @keydown.enter="publish()" />
+ </div>
+ </div>
+
+ <div class="form-row form-row-wrap">
+ <!-- QoS -->
+ <div class="form-group">
+ <label class="form-label">QoS</label>
+ <div class="segmented-control">
+ <button v-for="(label, i) in qosLabels" :key="i" class="segment-item" :class="{ active: qos === i }" :title="qosHints[i]" @click="qos = i as 0 | 1 | 2">
+ {{ label }}
+ </button>
+ </div>
+ </div>
+
+ <!-- Retain -->
+ <div class="form-group form-group-check">
+ <label class="form-label">&nbsp;</label>
+ <label class="checkbox-label">
+ <input type="checkbox" :checked="retain" @change="retain = !retain" class="form-checkbox" />
+ <span class="text-xs">{{ t("connection.mqttRetainMessage") }}</span>
+ </label>
+ </div>
+
+ <!-- 编码格式 -->
+ <div class="form-group">
+ <label class="form-label">{{ t("connection.mqttPayloadEncoding") }}</label>
+ <select v-model="encoding" class="form-select text-xs">
+ <option v-for="enc in PAYLOAD_ENCODINGS" :key="enc" :value="enc">
+ {{ PAYLOAD_ENCODING_LABELS[enc] }}
+ </option>
+ </select>
+ </div>
+ </div>
+
+ <!-- Payload -->
+ <div class="form-group flex-1 flex flex-col min-h-0">
+ <label class="form-label">{{ t("connection.mqttPayload") }}</label>
+ <textarea v-model="payloadText" class="form-textarea flex-1 font-mono text-xs" :placeholder="payloadPlaceholder" rows="4" @keydown.ctrl.enter="publish()" />
+ <span class="form-hint">{{ t("connection.mqttPublishShortcut") }}</span>
+ </div>
+
+ <!-- 状态提示 -->
+ <div v-if="error" class="publish-error">{{ error }}</div>
+ <div v-if="success" class="publish-success">✓ {{ t("connection.mqttPublishSuccess") }}</div>
+
+ <!-- 操作按钮 -->
+ <div class="form-actions">
+ <Button size="sm" class="publish-btn" :disabled="loading || !topic.trim()" @click="publish">
+ {{ loading ? t("connection.mqttPublishing") : t("connection.mqttPublish") }}
+ </Button>
+ </div>
+ </div>
+</template>
+
+<style scoped>
+.publish-panel {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+ padding: 10px 12px;
+ border-top: 1px solid var(--color-border);
+ background: var(--color-background);
+}
+
+.panel-header {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+}
+
+.panel-title {
+ font-size: 12px;
+ font-weight: 600;
+ text-transform: uppercase;
+ color: var(--color-text-secondary);
+ letter-spacing: 0.5px;
+}
+
+.form-row {
+ display: flex;
+ gap: 8px;
+ align-items: flex-end;
+}
+
+.form-row-wrap {
+ flex-wrap: wrap;
+}
+
+.form-group {
+ display: flex;
+ flex-direction: column;
+ gap: 3px;
+}
+
+.form-group-check {
+ justify-content: flex-end;
+}
+
+.form-label {
+ font-size: 11px;
+ font-weight: 500;
+ color: var(--color-text-secondary);
+}
+
+.form-input {
+ height: 28px;
+ padding: 0 8px;
+ border: 1px solid var(--color-border);
+ border-radius: 4px;
+ background: var(--color-background);
+ color: var(--color-text);
+ outline: none;
+ box-sizing: border-box;
+}
+
+.form-input:focus {
+ border-color: var(--color-primary);
+ box-shadow: 0 0 0 1px var(--color-primary-alpha);
+}
+
+.form-textarea {
+ padding: 6px 8px;
+ border: 1px solid var(--color-border);
+ border-radius: 4px;
+ background: var(--color-background);
+ color: var(--color-text);
+ outline: none;
+ resize: vertical;
+ min-height: 60px;
+ box-sizing: border-box;
+ line-height: 1.4;
+}
+
+.form-textarea:focus {
+ border-color: var(--color-primary);
+ box-shadow: 0 0 0 1px var(--color-primary-alpha);
+}
+
+.form-select {
+ height: 28px;
+ padding: 0 6px;
+ border: 1px solid var(--color-border);
+ border-radius: 4px;
+ background: var(--color-background);
+ color: var(--color-text);
+ font-size: 12px;
+ outline: none;
+ cursor: pointer;
+}
+
+.form-select:focus {
+ border-color: var(--color-primary);
+}
+
+.form-checkbox {
+ width: 14px;
+ height: 14px;
+ cursor: pointer;
+ accent-color: var(--color-primary);
+}
+
+.checkbox-label {
+ display: flex;
+ align-items: center;
+ gap: 4px;
+ cursor: pointer;
+ user-select: none;
+}
+
+.form-hint {
+ font-size: 10px;
+ color: var(--color-text-tertiary);
+ margin-top: 2px;
+}
+
+.segmented-control {
+ display: flex;
+ border: 1px solid var(--color-border);
+ border-radius: 4px;
+ overflow: hidden;
+}
+
+.segment-item {
+ padding: 2px 10px;
+ font-size: 11px;
+ font-weight: 500;
+ border: none;
+ background: transparent;
+ color: var(--color-text-secondary);
+ cursor: pointer;
+ transition: all 0.15s;
+ border-right: 1px solid var(--color-border);
+ white-space: nowrap;
+}
+
+.segment-item:last-child {
+ border-right: none;
+}
+
+.segment-item:hover {
+ background: var(--color-background-secondary);
+}
+
+.segment-item.active {
+ background: var(--color-primary);
+ color: white;
+}
+
+.publish-error {
+ font-size: 12px;
+ color: var(--color-error);
+ padding: 4px 8px;
+ background: var(--color-error-bg);
+ border-radius: 4px;
+}
+
+.publish-success {
+ font-size: 12px;
+ color: var(--color-success);
+ padding: 4px 8px;
+ background: color-mix(in srgb, var(--color-success) 12%, transparent);
+ border-radius: 4px;
+ font-weight: 500;
+}
+
+.form-actions {
+ display: flex;
+ gap: 8px;
+}
+
+.publish-btn {
+ flex: 1;
+ height: 30px;
+ font-size: 13px;
+ font-weight: 500;
+}
+</style>
diff --git a/apps/desktop/src/components/mqtt/TopicTreeNode.vue b/apps/desktop/src/components/mqtt/TopicTreeNode.vue
new file mode 100644
index 0000000000..1ca6be6c54
--- /dev/null
+++ b/apps/desktop/src/components/mqtt/TopicTreeNode.vue
@@ -0,0 +1,96 @@
+<script setup lang="ts">
+import type { MqttTopicNode } from "@/types/mqtt";
+import { ChevronRight, ChevronDown, Trash2 } from "@lucide/vue";
+import { computed, ref } from "vue";
+import { useI18n } from "vue-i18n";
+
+interface Props {
+ node: MqttTopicNode;
+ depth: number;
+ selectedTopic: string;
+ subscribedTopics: [string, string][];
+}
+
+const props = defineProps<Props>();
+const { t } = useI18n();
+const emit = defineEmits<{
+ select: [topic: string];
+ subscribe: [topic: string];
+ unsubscribe: [topics: string[]];
+}>();
+
+const expanded = ref(props.depth < 2);
+
+function isSubscribed(topic: string): boolean {
+ return props.subscribedTopics.some(([t]) => t === topic);
+}
+
+function collectSubscribedTopics(node: MqttTopicNode): string[] {
+ const topics = node.isLeaf && isSubscribed(node.fullPath) ? [node.fullPath] : [];
+ for (const child of node.children ?? []) {
+ topics.push(...collectSubscribedTopics(child));
+ }
+ return topics;
+}
+
+const topicsToUnsubscribe = computed(() => collectSubscribedTopics(props.node));
+const unsubscribeLabel = computed(() => (topicsToUnsubscribe.value.length === 1 ? t("connection.mqttUnsubscribeTopic", { topic: topicsToUnsubscribe.value[0] }) : t("connection.mqttUnsubscribeGroup", { count: topicsToUnsubscribe.value.length })));
+
+function toggle() {
+ expanded.value = !expanded.value;
+}
+</script>
+
+<template>
+ <div>
+ <div
+ class="flex items-center gap-1 py-0.5 pl-2 pr-1 rounded cursor-pointer text-xs group border-l-2 border-transparent"
+ :class="selectedTopic === node.fullPath ? 'bg-primary/25 text-foreground font-semibold border-l-primary shadow-sm ring-1 ring-primary/20' : 'hover:bg-muted/70 text-muted-foreground'"
+ :style="{ paddingLeft: `${depth * 12 + 4}px` }"
+ @click="node.isLeaf ? emit('select', node.fullPath) : toggle()"
+ >
+ <!-- Expand/collapse toggle -->
+ <button v-if="node.children?.length" class="shrink-0 w-4 h-4 flex items-center justify-center text-muted-foreground" @click.stop="toggle">
+ <ChevronRight v-if="!expanded" class="h-3 w-3" />
+ <ChevronDown v-else class="h-3 w-3" />
+ </button>
+ <span v-else class="w-4 shrink-0" />
+
+ <!-- Topic name -->
+ <span class="truncate flex-1 font-mono" :class="selectedTopic === node.fullPath ? 'text-primary' : node.isLeaf ? 'font-medium text-blue-600 dark:text-blue-400' : ''">
+ {{ node.name }}
+ </span>
+
+ <!-- 分组节点可一次取消其下全部订阅 -->
+ <button
+ v-if="topicsToUnsubscribe.length"
+ type="button"
+ class="flex h-5 w-5 shrink-0 items-center justify-center rounded text-red-500 opacity-0 transition-opacity hover:bg-accent group-hover:opacity-100 focus:opacity-100"
+ :title="unsubscribeLabel"
+ :aria-label="unsubscribeLabel"
+ @click.stop="emit('unsubscribe', topicsToUnsubscribe)"
+ >
+ <Trash2 class="h-3 w-3" />
+ </button>
+ </div>
+
+ <!-- Children -->
+ <TopicTreeNode
+ v-if="expanded"
+ v-for="child in node.children"
+ :key="child.fullPath"
+ :node="child"
+ :depth="depth + 1"
+ :selected-topic="selectedTopic"
+ :subscribed-topics="subscribedTopics"
+ @select="emit('select', $event)"
+ @subscribe="emit('subscribe', $event)"
+ @unsubscribe="emit('unsubscribe', $event)"
+ />
+ </div>
+</template>
+
+<script lang="ts">
+// Recursive component needs explicit name in non-setup script block
+export default { name: "TopicTreeNode" };
+</script>
diff --git a/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue b/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue
index bba6d265b0..04303248f8 100644
--- a/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue
+++ b/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue
@@ -603,6 +603,8 @@ async function toggle() {
await connectionStore.loadMqTenants(node.connectionId);
} else if (config?.db_type === "nacos") {
await connectionStore.loadNacosNamespaces(node.connectionId);
+ } else if (config?.db_type === "mqtt") {
+ await connectionStore.loadMqttTopics(node.connectionId);
} else {
await connectionStore.loadDatabases(node.connectionId);
}
@@ -615,6 +617,10 @@ async function toggle() {
} else if (node.type === "mq-tenant" && node.connectionId) {
await connectionStore.ensureConnected(node.connectionId);
queryStore.openMqAdmin(node.connectionId, { tenant: node.mqTenant || node.label, initialTab: node.mqInitialTab });
+ } else if (node.type === "mqtt-topic" && node.connectionId) {
+ await connectionStore.ensureConnected(node.connectionId);
+ const topicFromId = node.id.endsWith(":mqtt-topic:__console__") ? undefined : node.id.split(":mqtt-topic:")[1] || node.label;
+ queryStore.openMqttAdmin(node.connectionId, topicFromId ? { initialTopic: topicFromId } : undefined);
} else if (node.type === "nacos-namespace" && node.connectionId) {
await connectionStore.ensureConnected(node.connectionId);
queryStore.openNacosAdmin(node.connectionId, { namespace: node.nacosNamespace || "", namespaceName: node.nacosNamespaceName || node.label });
diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts
index 1d0bece0d4..22352f724b 100644
--- a/apps/desktop/src/i18n/locales/en.ts
+++ b/apps/desktop/src/i18n/locales/en.ts
@@ -488,6 +488,71 @@ export default {
mqTokenSigningKeyPlaceholderHs256: "Broker SECRET",
mqTokenSigningKeyPlaceholderRs256: "-----BEGIN PRIVATE KEY-----",
mqTokenSigningHint: "Choose based on the broker jwt.broker.token.mode: SECRET uses HS256, PRIVATE uses RS256. The key is stored with connection secrets.",
+ mqttBrokerAddress: "Broker Address",
+ mqttBrokerAddressPlaceholder: "127.0.0.1",
+ mqttBrokerPort: "Port",
+ mqttClientId: "Client ID",
+ mqttClientIdPlaceholder: "Auto-generated",
+ mqttUsername: "Username",
+ mqttUsernamePlaceholder: "Optional",
+ mqttPassword: "Password",
+ mqttPasswordPlaceholder: "Optional",
+ mqttTls: "TLS",
+ mqttTlsSkipVerify: "Skip certificate verification",
+ mqttKeepAlive: "Keep Alive (seconds)",
+ mqttProtocolVersion: "Protocol Version",
+ mqttTransport: "Transport",
+ mqttTransportTcp: "TCP",
+ mqttTransportWebSocket: "WebSocket",
+ mqttWsPath: "WebSocket Path",
+ mqttWsPathPlaceholder: "/mqtt",
+ mqttConnectTimeout: "Connect Timeout (seconds)",
+ mqttConsoleTitle: "MQTT Console",
+ mqttTopics: "Topics",
+ mqttMessages: "Messages",
+ mqttPublish: "Publish Message",
+ mqttSubscribe: "Subscribe",
+ mqttUnsubscribe: "Unsubscribe",
+ mqttNoMessages: "No messages",
+ mqttNoTopics: "No subscribed topics",
+ mqttQuickSubscribe: "Quick Subscribe",
+ mqttTopicPlaceholder: "Topic name",
+ mqttConnected: "Connected",
+ mqttDisconnected: "Disconnected",
+ mqttRefresh: "Refresh",
+ mqttPublishDialogTitle: "Publish MQTT Message",
+ mqttClearMessages: "Clear messages",
+ mqttNoTopicsHint: "No subscriptions. Use the field below to subscribe to a topic.",
+ mqttSubscribePlaceholder: "Enter a topic filter...",
+ mqttMessagesForTopic: "Messages: {topic}",
+ mqttAllMessages: "Messages (all topics)",
+ mqttSent: "Sent",
+ mqttReceived: "Received",
+ mqttRetained: "Retained",
+ mqttUnsubscribeMissing: "No active subscriptions found. Refresh topics and try again.",
+ mqttUnsubscribeConfirm: 'Unsubscribe from "{topic}"?',
+ mqttUnsubscribeGroupConfirm: "Unsubscribe from {count} topics in this group?",
+ mqttUnsubscribeFailed: "Failed to unsubscribe from {count} topic(s): {errors}",
+ mqttUnsubscribeTopic: "Unsubscribe from {topic}",
+ mqttUnsubscribeGroup: "Unsubscribe from {count} topics in this group",
+ mqttTopic: "Topic",
+ mqttTopicExample: "e.g. sensors/temperature",
+ mqttRetainMessage: "Retain message",
+ mqttPayloadEncoding: "Payload encoding",
+ mqttPayload: "Payload",
+ mqttPayloadPlaceholderPlaintext: "Enter message content...",
+ mqttPayloadPlaceholderJson: 'Enter JSON, for example {"key": "value"}',
+ mqttPayloadPlaceholderBase64: "Enter Base64-encoded data",
+ mqttPayloadPlaceholderHex: "Enter hexadecimal data, for example 48 65 6C 6C 6F",
+ mqttPayloadPlaceholderStructured: "Enter a JSON value; it will be encoded as {encoding}",
+ mqttPublishShortcut: "Ctrl+Enter to publish",
+ mqttPublishTopicRequired: "Topic is required",
+ mqttPublishTopicWildcard: "Publish topics cannot contain # or + wildcards. Replace the subscription filter with a concrete topic.",
+ mqttPublishSuccess: "Message published",
+ mqttPublishing: "Publishing...",
+ mqttQosAtMostOnce: "At most once",
+ mqttQosAtLeastOnce: "At least once",
+ mqttQosExactlyOnce: "Exactly once",
searchDatabasePlaceholder: "Search database types",
searchResults: "Search results",
databaseCategories: "Categories",
diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.ts
index 8370ff2595..5241057483 100644
--- a/apps/desktop/src/i18n/locales/es.ts
+++ b/apps/desktop/src/i18n/locales/es.ts
@@ -378,6 +378,25 @@ export default withEnglishFallback({
nacosTls: "TLS",
nacosTlsSkipVerify: "Omitir verificación de certificado",
nacosPageSize: "Tamaño de página",
+ mqttBrokerAddress: "Broker Address",
+ mqttBrokerAddressPlaceholder: "127.0.0.1",
+ mqttBrokerPort: "Port",
+ mqttClientId: "Client ID",
+ mqttClientIdPlaceholder: "Auto-generated",
+ mqttUsername: "Username",
+ mqttUsernamePlaceholder: "Optional",
+ mqttPassword: "Password",
+ mqttPasswordPlaceholder: "Optional",
+ mqttTls: "TLS",
+ mqttTlsSkipVerify: "Skip certificate verification",
+ mqttKeepAlive: "Keep Alive (seconds)",
+ mqttProtocolVersion: "Protocol Version",
+ mqttTransport: "Transport",
+ mqttTransportTcp: "TCP",
+ mqttTransportWebSocket: "WebSocket",
+ mqttWsPath: "WebSocket Path",
+ mqttWsPathPlaceholder: "/mqtt",
+ mqttConnectTimeout: "Connect Timeout (seconds)",
nacosMetrics: "Métricas Prometheus",
nacosMetricsAuto: "Detección automática",
nacosMetricsDisabled: "Desactivado",
@@ -709,6 +728,52 @@ export default withEnglishFallback({
sshHostKeyNoticeRejected: "Ha rechazado la verificación de la clave del host {host}:{port}. La conexión se ha abortado, no se enviaron credenciales.",
sshHostKeyNoticeGeneric: "Falló la verificación de la clave del host SSH {host}:{port}.",
sshHostKeyNoticeLearnFailed: "No se pudo guardar la clave de host de {host}:{port} — este host solo es de confianza en esta sesión, la próxima conexión requerirá confirmación nuevamente.",
+ mqttConsoleTitle: "Consola MQTT",
+ mqttTopics: "Topics",
+ mqttMessages: "Mensajes",
+ mqttPublish: "Publicar mensaje",
+ mqttSubscribe: "Suscribir",
+ mqttUnsubscribe: "Cancelar suscripción",
+ mqttNoMessages: "No hay mensajes",
+ mqttNoTopics: "No hay Topics suscritos",
+ mqttQuickSubscribe: "Suscripción rápida",
+ mqttTopicPlaceholder: "Nombre del Topic",
+ mqttConnected: "Conectado",
+ mqttDisconnected: "Desconectado",
+ mqttRefresh: "Actualizar",
+ mqttPublishDialogTitle: "Publicar mensaje MQTT",
+ mqttClearMessages: "Borrar mensajes",
+ mqttNoTopicsHint: "No hay suscripciones. Usa el campo inferior para suscribirte a un Topic.",
+ mqttSubscribePlaceholder: "Introduce un filtro de Topic...",
+ mqttMessagesForTopic: "Mensajes: {topic}",
+ mqttAllMessages: "Mensajes (todos los Topics)",
+ mqttSent: "Enviado",
+ mqttReceived: "Recibido",
+ mqttRetained: "Retenido",
+ mqttUnsubscribeMissing: "No se encontraron suscripciones activas. Actualiza los Topics e inténtalo de nuevo.",
+ mqttUnsubscribeConfirm: '¿Cancelar la suscripción a "{topic}"?',
+ mqttUnsubscribeGroupConfirm: "¿Cancelar la suscripción a {count} Topics de este grupo?",
+ mqttUnsubscribeFailed: "No se pudo cancelar la suscripción a {count} Topic(s): {errors}",
+ mqttUnsubscribeTopic: "Cancelar la suscripción a {topic}",
+ mqttUnsubscribeGroup: "Cancelar la suscripción a {count} Topics de este grupo",
+ mqttTopic: "Topic",
+ mqttTopicExample: "p. ej. sensors/temperature",
+ mqttRetainMessage: "Retener mensaje",
+ mqttPayloadEncoding: "Codificación del Payload",
+ mqttPayload: "Payload",
+ mqttPayloadPlaceholderPlaintext: "Introduce el contenido del mensaje...",
+ mqttPayloadPlaceholderJson: 'Introduce JSON, por ejemplo {"key": "value"}',
+ mqttPayloadPlaceholderBase64: "Introduce datos codificados en Base64",
+ mqttPayloadPlaceholderHex: "Introduce datos hexadecimales, por ejemplo 48 65 6C 6C 6F",
+ mqttPayloadPlaceholderStructured: "Introduce un valor JSON; se codificará como {encoding}",
+ mqttPublishShortcut: "Ctrl+Enter para publicar",
+ mqttPublishTopicRequired: "El Topic es obligatorio",
+ mqttPublishTopicWildcard: "Los Topics de publicación no pueden contener los comodines # o +. Sustituye el filtro de suscripción por un Topic concreto.",
+ mqttPublishSuccess: "Mensaje publicado",
+ mqttPublishing: "Publicando...",
+ mqttQosAtMostOnce: "Como máximo una vez",
+ mqttQosAtLeastOnce: "Al menos una vez",
+ mqttQosExactlyOnce: "Exactamente una vez",
damengJvmOptions: "Propiedades del sistema JVM",
damengJvmOptionsPlaceholder: "-Djava.net.preferIPv4Stack=true",
damengJvmOptionsHint: "Una propiedad -Dkey o -Dkey=value por línea. Los valores con espacios no necesitan comillas del shell.",
diff --git a/apps/desktop/src/i18n/locales/it.ts b/apps/desktop/src/i18n/locales/it.ts
index 9176c2af6f..ad86edae7e 100644
--- a/apps/desktop/src/i18n/locales/it.ts
+++ b/apps/desktop/src/i18n/locales/it.ts
@@ -377,6 +377,25 @@ export default withEnglishFallback({
nacosTls: "TLS",
nacosTlsSkipVerify: "Salta verifica certificato",
nacosPageSize: "Dimensione pagina",
+ mqttBrokerAddress: "Broker Address",
+ mqttBrokerAddressPlaceholder: "127.0.0.1",
+ mqttBrokerPort: "Port",
+ mqttClientId: "Client ID",
+ mqttClientIdPlaceholder: "Auto-generated",
+ mqttUsername: "Username",
+ mqttUsernamePlaceholder: "Optional",
+ mqttPassword: "Password",
+ mqttPasswordPlaceholder: "Optional",
+ mqttTls: "TLS",
+ mqttTlsSkipVerify: "Skip certificate verification",
+ mqttKeepAlive: "Keep Alive (seconds)",
+ mqttProtocolVersion: "Protocol Version",
+ mqttTransport: "Transport",
+ mqttTransportTcp: "TCP",
+ mqttTransportWebSocket: "WebSocket",
+ mqttWsPath: "WebSocket Path",
+ mqttWsPathPlaceholder: "/mqtt",
+ mqttConnectTimeout: "Connect Timeout (seconds)",
nacosMetrics: "Metriche Prometheus",
nacosMetricsAuto: "Rilevamento automatico",
nacosMetricsDisabled: "Disabilitato",
@@ -707,6 +726,52 @@ export default withEnglishFallback({
sshHostKeyNoticeRejected: "Hai rifiutato la verifica della chiave host di {host}:{port}, la connessione è stata interrotta e le credenziali non sono state inviate.",
sshHostKeyNoticeGeneric: "Verifica della chiave host SSH {host}:{port} fallita.",
sshHostKeyNoticeLearnFailed: "Impossibile salvare la chiave host di {host}:{port} — Questo host è considerato affidabile solo per questa sessione, alla prossima connessione sarà necessaria una nuova conferma.",
+ mqttConsoleTitle: "Console MQTT",
+ mqttTopics: "Topics",
+ mqttMessages: "Messaggi",
+ mqttPublish: "Pubblica messaggio",
+ mqttSubscribe: "Sottoscrivi",
+ mqttUnsubscribe: "Annulla sottoscrizione",
+ mqttNoMessages: "Nessun messaggio",
+ mqttNoTopics: "Nessun Topic sottoscritto",
+ mqttQuickSubscribe: "Sottoscrizione rapida",
+ mqttTopicPlaceholder: "Nome Topic",
+ mqttConnected: "Connesso",
+ mqttDisconnected: "Disconnesso",
+ mqttRefresh: "Aggiorna",
+ mqttPublishDialogTitle: "Pubblica messaggio MQTT",
+ mqttClearMessages: "Cancella messaggi",
+ mqttNoTopicsHint: "Nessuna sottoscrizione. Usa il campo sottostante per sottoscrivere un Topic.",
+ mqttSubscribePlaceholder: "Inserisci un filtro Topic...",
+ mqttMessagesForTopic: "Messaggi: {topic}",
+ mqttAllMessages: "Messaggi (tutti i Topic)",
+ mqttSent: "Inviato",
+ mqttReceived: "Ricevuto",
+ mqttRetained: "Mantenuto",
+ mqttUnsubscribeMissing: "Nessuna sottoscrizione attiva trovata. Aggiorna i Topic e riprova.",
+ mqttUnsubscribeConfirm: 'Annullare la sottoscrizione a "{topic}"?',
+ mqttUnsubscribeGroupConfirm: "Annullare la sottoscrizione a {count} Topic in questo gruppo?",
+ mqttUnsubscribeFailed: "Impossibile annullare la sottoscrizione a {count} Topic: {errors}",
+ mqttUnsubscribeTopic: "Annulla la sottoscrizione a {topic}",
+ mqttUnsubscribeGroup: "Annulla la sottoscrizione a {count} Topic in questo gruppo",
+ mqttTopic: "Topic",
+ mqttTopicExample: "ad es. sensors/temperature",
+ mqttRetainMessage: "Mantieni messaggio",
+ mqttPayloadEncoding: "Codifica del Payload",
+ mqttPayload: "Payload",
+ mqttPayloadPlaceholderPlaintext: "Inserisci il contenuto del messaggio...",
+ mqttPayloadPlaceholderJson: 'Inserisci JSON, ad esempio {"key": "value"}',
+ mqttPayloadPlaceholderBase64: "Inserisci dati codificati in Base64",
+ mqttPayloadPlaceholderHex: "Inserisci dati esadecimali, ad esempio 48 65 6C 6C 6F",
+ mqttPayloadPlaceholderStructured: "Inserisci un valore JSON; verrà codificato come {encoding}",
+ mqttPublishShortcut: "Ctrl+Invio per pubblicare",
+ mqttPublishTopicRequired: "Il Topic è obbligatorio",
+ mqttPublishTopicWildcard: "I Topic di pubblicazione non possono contenere i caratteri jolly # o +. Sostituisci il filtro di sottoscrizione con un Topic specifico.",
+ mqttPublishSuccess: "Messaggio pubblicato",
+ mqttPublishing: "Pubblicazione...",
+ mqttQosAtMostOnce: "Al massimo una volta",
+ mqttQosAtLeastOnce: "Almeno una volta",
+ mqttQosExactlyOnce: "Esattamente una volta",
damengJvmOptions: "Proprietà di sistema JVM",
damengJvmOptionsPlaceholder: "-Djava.net.preferIPv4Stack=true",
damengJvmOptionsHint: "Una proprietà -Dkey o -Dkey=value per riga. I valori con spazi non richiedono virgolette della shell.",
diff --git a/apps/desktop/src/i18n/locales/ja.ts b/apps/desktop/src/i18n/locales/ja.ts
index ac1430e2bd..74b20e95b4 100644
--- a/apps/desktop/src/i18n/locales/ja.ts
+++ b/apps/desktop/src/i18n/locales/ja.ts
@@ -371,6 +371,25 @@ export default withEnglishFallback({
nacosTls: "TLS",
nacosTlsSkipVerify: "証明書検証をスキップ",
nacosPageSize: "ページサイズ",
+ mqttBrokerAddress: "Broker Address",
+ mqttBrokerAddressPlaceholder: "127.0.0.1",
+ mqttBrokerPort: "Port",
+ mqttClientId: "Client ID",
+ mqttClientIdPlaceholder: "Auto-generated",
+ mqttUsername: "Username",
+ mqttUsernamePlaceholder: "Optional",
+ mqttPassword: "Password",
+ mqttPasswordPlaceholder: "Optional",
+ mqttTls: "TLS",
+ mqttTlsSkipVerify: "Skip certificate verification",
+ mqttKeepAlive: "Keep Alive (seconds)",
+ mqttProtocolVersion: "Protocol Version",
+ mqttTransport: "Transport",
+ mqttTransportTcp: "TCP",
+ mqttTransportWebSocket: "WebSocket",
+ mqttWsPath: "WebSocket Path",
+ mqttWsPathPlaceholder: "/mqtt",
+ mqttConnectTimeout: "Connect Timeout (seconds)",
nacosMetrics: "Prometheus メトリクス",
nacosMetricsAuto: "自動検出",
nacosMetricsDisabled: "無効",
@@ -707,6 +726,52 @@ export default withEnglishFallback({
sshHostKeyNoticeRejected: "ホスト {host}:{port} のホストキー検証を拒否しました。接続は中止され、資格情報は送信されませんでした。",
sshHostKeyNoticeGeneric: "SSHホスト {host}:{port} のキー検証に失敗しました。",
sshHostKeyNoticeLearnFailed: "{host}:{port} のホストキーを保存できません — このホストは現在のセッションでのみ信頼され、次回の接続時に再確認が必要です。",
+ mqttConsoleTitle: "MQTT コンソール",
+ mqttTopics: "トピック",
+ mqttMessages: "メッセージ",
+ mqttPublish: "メッセージを公開",
+ mqttSubscribe: "購読",
+ mqttUnsubscribe: "購読解除",
+ mqttNoMessages: "メッセージはありません",
+ mqttNoTopics: "購読中のトピックはありません",
+ mqttQuickSubscribe: "クイック購読",
+ mqttTopicPlaceholder: "トピック名",
+ mqttConnected: "接続済み",
+ mqttDisconnected: "切断済み",
+ mqttRefresh: "更新",
+ mqttPublishDialogTitle: "MQTT メッセージを公開",
+ mqttClearMessages: "メッセージを消去",
+ mqttNoTopicsHint: "購読はありません。下の入力欄からトピックを購読してください。",
+ mqttSubscribePlaceholder: "トピックフィルターを入力...",
+ mqttMessagesForTopic: "メッセージ: {topic}",
+ mqttAllMessages: "メッセージ(すべてのトピック)",
+ mqttSent: "送信",
+ mqttReceived: "受信",
+ mqttRetained: "保持",
+ mqttUnsubscribeMissing: "解除できる購読が見つかりません。トピックを更新して、もう一度お試しください。",
+ mqttUnsubscribeConfirm: "「{topic}」の購読を解除しますか?",
+ mqttUnsubscribeGroupConfirm: "このグループの {count} 件のトピック購読を解除しますか?",
+ mqttUnsubscribeFailed: "{count} 件のトピック購読を解除できませんでした: {errors}",
+ mqttUnsubscribeTopic: "{topic} の購読を解除",
+ mqttUnsubscribeGroup: "このグループの {count} 件のトピック購読を解除",
+ mqttTopic: "トピック",
+ mqttTopicExample: "例: sensors/temperature",
+ mqttRetainMessage: "メッセージを保持",
+ mqttPayloadEncoding: "ペイロードのエンコード",
+ mqttPayload: "ペイロード",
+ mqttPayloadPlaceholderPlaintext: "メッセージ内容を入力...",
+ mqttPayloadPlaceholderJson: 'JSON を入力(例: {"key": "value"})',
+ mqttPayloadPlaceholderBase64: "Base64 エンコード済みデータを入力",
+ mqttPayloadPlaceholderHex: "16 進数データを入力(例: 48 65 6C 6C 6F)",
+ mqttPayloadPlaceholderStructured: "JSON 値を入力してください。{encoding} としてエンコードされます",
+ mqttPublishShortcut: "Ctrl+Enter で公開",
+ mqttPublishTopicRequired: "トピックは必須です",
+ mqttPublishTopicWildcard: "公開トピックには # または + のワイルドカードを使用できません。購読フィルターを具体的なトピックに置き換えてください。",
+ mqttPublishSuccess: "メッセージを公開しました",
+ mqttPublishing: "公開中...",
+ mqttQosAtMostOnce: "最大 1 回",
+ mqttQosAtLeastOnce: "最低 1 回",
+ mqttQosExactlyOnce: "正確に 1 回",
damengJvmOptions: "JVM システムプロパティ",
damengJvmOptionsPlaceholder: "-Djava.net.preferIPv4Stack=true",
damengJvmOptionsHint: "1 行に 1 つの -Dkey または -Dkey=value を入力します。空白を含む値にもシェル引用符は不要です。",
diff --git a/apps/desktop/src/i18n/locales/pt-BR.ts b/apps/desktop/src/i18n/locales/pt-BR.ts
index 1fc7495298..08d1e378aa 100644
--- a/apps/desktop/src/i18n/locales/pt-BR.ts
+++ b/apps/desktop/src/i18n/locales/pt-BR.ts
@@ -378,6 +378,25 @@ export default withEnglishFallback({
nacosTls: "TLS",
nacosTlsSkipVerify: "Ignorar verificação do certificado",
nacosPageSize: "Tamanho da página",
+ mqttBrokerAddress: "Broker Address",
+ mqttBrokerAddressPlaceholder: "127.0.0.1",
+ mqttBrokerPort: "Port",
+ mqttClientId: "Client ID",
+ mqttClientIdPlaceholder: "Auto-generated",
+ mqttUsername: "Username",
+ mqttUsernamePlaceholder: "Optional",
+ mqttPassword: "Password",
+ mqttPasswordPlaceholder: "Optional",
+ mqttTls: "TLS",
+ mqttTlsSkipVerify: "Skip certificate verification",
+ mqttKeepAlive: "Keep Alive (seconds)",
+ mqttProtocolVersion: "Protocol Version",
+ mqttTransport: "Transport",
+ mqttTransportTcp: "TCP",
+ mqttTransportWebSocket: "WebSocket",
+ mqttWsPath: "WebSocket Path",
+ mqttWsPathPlaceholder: "/mqtt",
+ mqttConnectTimeout: "Connect Timeout (seconds)",
nacosMetrics: "Métricas Prometheus",
nacosMetricsAuto: "Detecção automática",
nacosMetricsDisabled: "Desativado",
@@ -708,6 +727,52 @@ export default withEnglishFallback({
sshHostKeyNoticeRejected: "Você rejeitou a verificação da chave do host {host}:{port}. A conexão foi abortada e as credenciais não foram enviadas.",
sshHostKeyNoticeGeneric: "Falha na verificação da chave do host SSH {host}:{port}.",
sshHostKeyNoticeLearnFailed: "Não foi possível salvar a chave do host {host}:{port} — o host é confiável apenas nesta sessão, necessitando reconfirmação na próxima conexão.",
+ mqttConsoleTitle: "MQTT Console",
+ mqttTopics: "Tópicos",
+ mqttMessages: "Mensagens",
+ mqttPublish: "Publicar mensagem",
+ mqttSubscribe: "Inscrever",
+ mqttUnsubscribe: "Cancelar inscrição",
+ mqttNoMessages: "Nenhuma mensagem",
+ mqttNoTopics: "Nenhum Topic inscrito",
+ mqttQuickSubscribe: "Inscrição rápida",
+ mqttTopicPlaceholder: "Nome do Topic",
+ mqttConnected: "Conectado",
+ mqttDisconnected: "Desconectado",
+ mqttRefresh: "Atualizar",
+ mqttPublishDialogTitle: "Publicar mensagem MQTT",
+ mqttClearMessages: "Limpar mensagens",
+ mqttNoTopicsHint: "Nenhuma inscrição. Use o campo abaixo para assinar um Topic.",
+ mqttSubscribePlaceholder: "Digite um filtro de Topic...",
+ mqttMessagesForTopic: "Mensagens: {topic}",
+ mqttAllMessages: "Mensagens (todos os Topics)",
+ mqttSent: "Enviado",
+ mqttReceived: "Recebido",
+ mqttRetained: "Retido",
+ mqttUnsubscribeMissing: "Nenhuma inscrição ativa encontrada. Atualize os Topics e tente novamente.",
+ mqttUnsubscribeConfirm: 'Cancelar a inscrição em "{topic}"?',
+ mqttUnsubscribeGroupConfirm: "Cancelar a inscrição em {count} Topics deste grupo?",
+ mqttUnsubscribeFailed: "Falha ao cancelar a inscrição em {count} Topic(s): {errors}",
+ mqttUnsubscribeTopic: "Cancelar a inscrição em {topic}",
+ mqttUnsubscribeGroup: "Cancelar a inscrição em {count} Topics deste grupo",
+ mqttTopic: "Topic",
+ mqttTopicExample: "ex.: sensors/temperature",
+ mqttRetainMessage: "Reter mensagem",
+ mqttPayloadEncoding: "Codificação do Payload",
+ mqttPayload: "Payload",
+ mqttPayloadPlaceholderPlaintext: "Digite o conteúdo da mensagem...",
+ mqttPayloadPlaceholderJson: 'Digite JSON, por exemplo {"key": "value"}',
+ mqttPayloadPlaceholderBase64: "Digite dados codificados em Base64",
+ mqttPayloadPlaceholderHex: "Digite dados hexadecimais, por exemplo 48 65 6C 6C 6F",
+ mqttPayloadPlaceholderStructured: "Digite um valor JSON; ele será codificado como {encoding}",
+ mqttPublishShortcut: "Ctrl+Enter para publicar",
+ mqttPublishTopicRequired: "O Topic é obrigatório",
+ mqttPublishTopicWildcard: "Topics de publicação não podem conter os curingas # ou +. Substitua o filtro de inscrição por um Topic específico.",
+ mqttPublishSuccess: "Mensagem publicada",
+ mqttPublishing: "Publicando...",
+ mqttQosAtMostOnce: "No máximo uma vez",
+ mqttQosAtLeastOnce: "Pelo menos uma vez",
+ mqttQosExactlyOnce: "Exatamente uma vez",
damengJvmOptions: "Propriedades do sistema da JVM",
damengJvmOptionsPlaceholder: "-Djava.net.preferIPv4Stack=true",
damengJvmOptionsHint: "Uma propriedade -Dkey ou -Dkey=value por linha. Valores com espaços não precisam de aspas do shell.",
diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts
index f823137d74..21ac5c939b 100644
--- a/apps/desktop/src/i18n/locales/zh-CN.ts
+++ b/apps/desktop/src/i18n/locales/zh-CN.ts
@@ -489,6 +489,71 @@ export default withEnglishFallback({
mqTokenSigningKeyPlaceholderHs256: "Broker SECRET",
mqTokenSigningKeyPlaceholderRs256: "-----BEGIN PRIVATE KEY-----",
mqTokenSigningHint: "按 Broker 的 jwt.broker.token.mode 选择:SECRET 使用 HS256,PRIVATE 使用 RS256。密钥会走连接 secret 存储。",
+ mqttBrokerAddress: "Broker 地址",
+ mqttBrokerAddressPlaceholder: "127.0.0.1",
+ mqttBrokerPort: "端口",
+ mqttClientId: "Client ID",
+ mqttClientIdPlaceholder: "自动生成",
+ mqttUsername: "用户名",
+ mqttUsernamePlaceholder: "选填",
+ mqttPassword: "密码",
+ mqttPasswordPlaceholder: "选填",
+ mqttTls: "TLS",
+ mqttTlsSkipVerify: "跳过证书校验",
+ mqttKeepAlive: "Keep Alive(秒)",
+ mqttProtocolVersion: "协议版本",
+ mqttTransport: "传输层",
+ mqttTransportTcp: "TCP",
+ mqttTransportWebSocket: "WebSocket",
+ mqttWsPath: "WebSocket 路径",
+ mqttWsPathPlaceholder: "/mqtt",
+ mqttConnectTimeout: "连接超时(秒)",
+ mqttConsoleTitle: "MQTT 控制台",
+ mqttTopics: "Topics",
+ mqttMessages: "消息",
+ mqttPublish: "发布消息",
+ mqttSubscribe: "订阅",
+ mqttUnsubscribe: "取消订阅",
+ mqttNoMessages: "暂无消息",
+ mqttNoTopics: "暂无已订阅的 Topic",
+ mqttQuickSubscribe: "快速订阅",
+ mqttTopicPlaceholder: "Topic 名称",
+ mqttConnected: "已连接",
+ mqttDisconnected: "已断开",
+ mqttRefresh: "刷新",
+ mqttPublishDialogTitle: "发布 MQTT 消息",
+ mqttClearMessages: "清空消息",
+ mqttNoTopicsHint: "暂无订阅。使用下方输入框订阅 Topic。",
+ mqttSubscribePlaceholder: "输入要订阅的 Topic...",
+ mqttMessagesForTopic: "消息:{topic}",
+ mqttAllMessages: "消息(所有 Topic)",
+ mqttSent: "发送",
+ mqttReceived: "接收",
+ mqttRetained: "保留",
+ mqttUnsubscribeMissing: "未找到可取消的订阅,Topic 列表可能已经刷新,请重试。",
+ mqttUnsubscribeConfirm: "确定要取消订阅“{topic}”吗?",
+ mqttUnsubscribeGroupConfirm: "确定要取消此分组下的 {count} 个订阅吗?",
+ mqttUnsubscribeFailed: "有 {count} 个订阅未能取消:{errors}",
+ mqttUnsubscribeTopic: "取消订阅 {topic}",
+ mqttUnsubscribeGroup: "取消此分组下的 {count} 个订阅",
+ mqttTopic: "Topic",
+ mqttTopicExample: "例如 sensors/temperature",
+ mqttRetainMessage: "保留消息",
+ mqttPayloadEncoding: "Payload 编码",
+ mqttPayload: "消息内容 (Payload)",
+ mqttPayloadPlaceholderPlaintext: "输入消息内容...",
+ mqttPayloadPlaceholderJson: '输入 JSON,例如 {"key": "value"}',
+ mqttPayloadPlaceholderBase64: "输入 Base64 编码的数据",
+ mqttPayloadPlaceholderHex: "输入十六进制数据,例如 48 65 6C 6C 6F",
+ mqttPayloadPlaceholderStructured: "输入 JSON 值,发送时将自动编码为 {encoding}",
+ mqttPublishShortcut: "Ctrl+Enter 发送",
+ mqttPublishTopicRequired: "Topic 不能为空",
+ mqttPublishTopicWildcard: "发布 Topic 不能包含 # 或 + 通配符,请将订阅过滤器改为具体 Topic 后再发送。",
+ mqttPublishSuccess: "消息已发送",
+ mqttPublishing: "发送中...",
+ mqttQosAtMostOnce: "最多一次",
+ mqttQosAtLeastOnce: "至少一次",
+ mqttQosExactlyOnce: "精确一次",
searchDatabasePlaceholder: "搜索数据库类型",
searchResults: "搜索结果",
databaseCategories: "分类",
diff --git a/apps/desktop/src/i18n/locales/zh-TW.ts b/apps/desktop/src/i18n/locales/zh-TW.ts
index 2fac4787f9..1e60e34ac1 100644
--- a/apps/desktop/src/i18n/locales/zh-TW.ts
+++ b/apps/desktop/src/i18n/locales/zh-TW.ts
@@ -377,6 +377,25 @@ export default withEnglishFallback({
nacosTls: "TLS",
nacosTlsSkipVerify: "跳過憑證驗證",
nacosPageSize: "分頁大小",
+ mqttBrokerAddress: "Broker Address",
+ mqttBrokerAddressPlaceholder: "127.0.0.1",
+ mqttBrokerPort: "Port",
+ mqttClientId: "Client ID",
+ mqttClientIdPlaceholder: "Auto-generated",
+ mqttUsername: "Username",
+ mqttUsernamePlaceholder: "Optional",
+ mqttPassword: "Password",
+ mqttPasswordPlaceholder: "Optional",
+ mqttTls: "TLS",
+ mqttTlsSkipVerify: "Skip certificate verification",
+ mqttKeepAlive: "Keep Alive (seconds)",
+ mqttProtocolVersion: "Protocol Version",
+ mqttTransport: "Transport",
+ mqttTransportTcp: "TCP",
+ mqttTransportWebSocket: "WebSocket",
+ mqttWsPath: "WebSocket Path",
+ mqttWsPathPlaceholder: "/mqtt",
+ mqttConnectTimeout: "Connect Timeout (seconds)",
nacosMetrics: "Prometheus 指標",
nacosMetricsAuto: "自動偵測",
nacosMetricsDisabled: "關閉",
@@ -707,6 +726,52 @@ export default withEnglishFallback({
sshHostKeyNoticeRejected: "您已拒絕 {host}:{port} 的主機金鑰驗證,連線已中止,憑證未發送。",
sshHostKeyNoticeGeneric: "SSH 主機 {host}:{port} 的金鑰驗證失敗。",
sshHostKeyNoticeLearnFailed: "無法儲存 {host}:{port} 的主機金鑰 — 該主機僅在本次會話內受信任,下次連線仍需重新確認。",
+ mqttConsoleTitle: "MQTT 控制台",
+ mqttTopics: "Topics",
+ mqttMessages: "訊息",
+ mqttPublish: "發布訊息",
+ mqttSubscribe: "訂閱",
+ mqttUnsubscribe: "取消訂閱",
+ mqttNoMessages: "暫無訊息",
+ mqttNoTopics: "暫無已訂閱的 Topic",
+ mqttQuickSubscribe: "快速訂閱",
+ mqttTopicPlaceholder: "Topic 名稱",
+ mqttConnected: "已連線",
+ mqttDisconnected: "已斷線",
+ mqttRefresh: "重新整理",
+ mqttPublishDialogTitle: "發布 MQTT 訊息",
+ mqttClearMessages: "清除訊息",
+ mqttNoTopicsHint: "目前沒有訂閱。請使用下方輸入框訂閱 Topic。",
+ mqttSubscribePlaceholder: "輸入要訂閱的 Topic 篩選器...",
+ mqttMessagesForTopic: "訊息:{topic}",
+ mqttAllMessages: "訊息(所有 Topic)",
+ mqttSent: "傳送",
+ mqttReceived: "接收",
+ mqttRetained: "保留",
+ mqttUnsubscribeMissing: "找不到可取消的訂閱。Topic 清單可能已重新整理,請再試一次。",
+ mqttUnsubscribeConfirm: "確定要取消訂閱「{topic}」嗎?",
+ mqttUnsubscribeGroupConfirm: "確定要取消此群組下的 {count} 個訂閱嗎?",
+ mqttUnsubscribeFailed: "有 {count} 個訂閱無法取消:{errors}",
+ mqttUnsubscribeTopic: "取消訂閱 {topic}",
+ mqttUnsubscribeGroup: "取消此群組下的 {count} 個訂閱",
+ mqttTopic: "Topic",
+ mqttTopicExample: "例如 sensors/temperature",
+ mqttRetainMessage: "保留訊息",
+ mqttPayloadEncoding: "Payload 編碼",
+ mqttPayload: "訊息內容 (Payload)",
+ mqttPayloadPlaceholderPlaintext: "輸入訊息內容...",
+ mqttPayloadPlaceholderJson: '輸入 JSON,例如 {"key": "value"}',
+ mqttPayloadPlaceholderBase64: "輸入 Base64 編碼的資料",
+ mqttPayloadPlaceholderHex: "輸入十六進位資料,例如 48 65 6C 6C 6F",
+ mqttPayloadPlaceholderStructured: "輸入 JSON 值,傳送時將自動編碼為 {encoding}",
+ mqttPublishShortcut: "Ctrl+Enter 傳送",
+ mqttPublishTopicRequired: "Topic 不可為空",
+ mqttPublishTopicWildcard: "發布 Topic 不可包含 # 或 + 萬用字元,請將訂閱篩選器改為具體 Topic 後再傳送。",
+ mqttPublishSuccess: "訊息已傳送",
+ mqttPublishing: "傳送中...",
+ mqttQosAtMostOnce: "最多一次",
+ mqttQosAtLeastOnce: "至少一次",
+ mqttQosExactlyOnce: "恰好一次",
damengJvmOptions: "JVM 系統屬性",
damengJvmOptionsPlaceholder: "-Djava.net.preferIPv4Stack=true",
damengJvmOptionsHint: "每行一個 -Dkey 或 -Dkey=value;值包含空格時也不要使用 shell 引號。",
diff --git a/apps/desktop/src/lib/backend/api.ts b/apps/desktop/src/lib/backend/api.ts
index e5eabff26b..e1aabc038b 100644
--- a/apps/desktop/src/lib/backend/api.ts
+++ b/apps/desktop/src/lib/backend/api.ts
@@ -748,3 +748,13 @@ export type {
AgentEvent,
SqlFileEntry,
} from "@/lib/backend/tauri";
+
+// MQTT
+export const mqttGetBrokerInfo = forward("mqttGetBrokerInfo");
+export const mqttSubscribe = forward("mqttSubscribe");
+export const mqttUnsubscribe = forward("mqttUnsubscribe");
+export const mqttPublish = forward("mqttPublish");
+export const mqttListTopics = forward("mqttListTopics");
+export const mqttGetTopicTree = forward("mqttGetTopicTree");
+export const mqttGetMessages = forward("mqttGetMessages");
+export const mqttClearMessages = forward("mqttClearMessages");
diff --git a/apps/desktop/src/lib/backend/http.ts b/apps/desktop/src/lib/backend/http.ts
index ad0837511c..be53e4e218 100644
--- a/apps/desktop/src/lib/backend/http.ts
+++ b/apps/desktop/src/lib/backend/http.ts
@@ -3358,3 +3358,4 @@ export async function refreshConnections(): Promise<void> {
}
export * from "@/lib/backend/mq-http";
+export * from "@/lib/backend/mqtt-http";
diff --git a/apps/desktop/src/lib/backend/mqtt-http.ts b/apps/desktop/src/lib/backend/mqtt-http.ts
new file mode 100644
index 0000000000..d678f75b41
--- /dev/null
+++ b/apps/desktop/src/lib/backend/mqtt-http.ts
@@ -0,0 +1,38 @@
+// MQTT HTTP backend stubs (MQTT is not yet supported in web/Docker mode).
+// Throw descriptive errors so the user knows this isn't available.
+
+function mqttWebNotAvailable(name: string): never {
+ throw new Error(`MQTT ${name}: MQTT 功能暂不支持 Web/Docker 模式。请使用桌面版。`);
+}
+
+export async function mqttGetBrokerInfo(_connectionId: string) {
+ mqttWebNotAvailable("mqttGetBrokerInfo");
+}
+
+export async function mqttSubscribe(_connectionId: string, _topic: string, _qos?: string | null) {
+ mqttWebNotAvailable("mqttSubscribe");
+}
+
+export async function mqttUnsubscribe(_connectionId: string, _topic: string) {
+ mqttWebNotAvailable("mqttUnsubscribe");
+}
+
+export async function mqttPublish(_connectionId: string, _request: unknown) {
+ mqttWebNotAvailable("mqttPublish");
+}
+
+export async function mqttListTopics(_connectionId: string) {
+ mqttWebNotAvailable("mqttListTopics");
+}
+
+export async function mqttGetTopicTree(_connectionId: string) {
+ mqttWebNotAvailable("mqttGetTopicTree");
+}
+
+export async function mqttGetMessages(_connectionId: string, _topicFilter?: string | null, _limit?: number | null) {
+ mqttWebNotAvailable("mqttGetMessages");
+}
+
+export async function mqttClearMessages(_connectionId: string) {
+ mqttWebNotAvailable("mqttClearMessages");
+}
diff --git a/apps/desktop/src/lib/backend/mqtt-tauri.ts b/apps/desktop/src/lib/backend/mqtt-tauri.ts
new file mode 100644
index 0000000000..5dba9ab037
--- /dev/null
+++ b/apps/desktop/src/lib/backend/mqtt-tauri.ts
@@ -0,0 +1,33 @@
+import { invoke } from "@tauri-apps/api/core";
+
+export async function mqttGetBrokerInfo(connectionId: string) {
+ return invoke("mqtt_get_broker_info", { connectionId });
+}
+
+export async function mqttSubscribe(connectionId: string, topic: string, qos?: string | null) {
+ return invoke("mqtt_subscribe", { connectionId, topic, qos: qos ?? null });
+}
+
+export async function mqttUnsubscribe(connectionId: string, topic: string) {
+ return invoke("mqtt_unsubscribe", { connectionId, topic });
+}
+
+export async function mqttPublish(connectionId: string, request: { topic: string; payloadBase64: string; payloadText?: string | null; qos: string; retain: boolean }) {
+ return invoke("mqtt_publish", { connectionId, request });
+}
+
+export async function mqttListTopics(connectionId: string) {
+ return invoke("mqtt_list_topics", { connectionId });
+}
+
+export async function mqttGetTopicTree(connectionId: string) {
+ return invoke("mqtt_get_topic_tree", { connectionId });
+}
+
+export async function mqttGetMessages(connectionId: string, topicFilter?: string | null, limit?: number | null) {
+ return invoke("mqtt_get_messages", { connectionId, topicFilter: topicFilter ?? null, limit: limit ?? null });
+}
+
+export async function mqttClearMessages(connectionId: string) {
+ return invoke("mqtt_clear_messages", { connectionId });
+}
diff --git a/apps/desktop/src/lib/backend/tauri.ts b/apps/desktop/src/lib/backend/tauri.ts
index c7e400c488..a6201b2a6e 100644
--- a/apps/desktop/src/lib/backend/tauri.ts
+++ b/apps/desktop/src/lib/backend/tauri.ts
@@ -3864,4 +3864,5 @@ export async function exportQueryResultMarkdown(filePath: string, columns: strin
}
export * from "@/lib/backend/mq-tauri";
+export * from "@/lib/backend/mqtt-tauri";
export * from "@/lib/backend/nacos-tauri";
diff --git a/apps/desktop/src/lib/database/databaseNamespaceCreation.ts b/apps/desktop/src/lib/database/databaseNamespaceCreation.ts
index 44556d09d3..f8e5557e7f 100644
--- a/apps/desktop/src/lib/database/databaseNamespaceCreation.ts
+++ b/apps/desktop/src/lib/database/databaseNamespaceCreation.ts
@@ -84,6 +84,7 @@ export const DATABASE_NAMESPACE_CREATION_MATRIX = {
jdbc: { deferred: "generic JDBC does not expose a reliable dialect-specific create target" },
mq: { deferred: "message queue namespaces are handled by MQ admin panels" },
nacos: { deferred: "Nacos namespace creation already uses the Nacos admin flow" },
+ mqtt: { deferred: "MQTT topics are managed via the MQTT console" },
} satisfies Record<DatabaseType, DatabaseNamespaceCreationMatrixEntry>;
export function connectionNamespaceCreationTarget(connection: CreationConnection): ConnectionCreationTarget | null {
diff --git a/apps/desktop/src/lib/database/databasePropertyEditing.ts b/apps/desktop/src/lib/database/databasePropertyEditing.ts
index 37b0598b1d..489ead728c 100644
--- a/apps/desktop/src/lib/database/databasePropertyEditing.ts
+++ b/apps/desktop/src/lib/database/databasePropertyEditing.ts
@@ -84,6 +84,7 @@ export const DATABASE_PROPERTY_EDITING_MATRIX = {
jdbc: { deferred: "generic JDBC does not expose reliable dialect-specific properties" },
mq: { deferred: "message queue namespaces are handled by MQ admin panels" },
nacos: { deferred: "Nacos namespace editing already uses the Nacos admin flow" },
+ mqtt: { deferred: "MQTT topics are managed via the MQTT console" },
} satisfies Record<DatabaseType, DatabasePropertyEditingEntry>;
function entryFor(connection: PropertyEditConnection): DatabasePropertyEditingEntry | null {
diff --git a/apps/desktop/src/lib/mqtt/mqttPayloadCodec.ts b/apps/desktop/src/lib/mqtt/mqttPayloadCodec.ts
new file mode 100644
index 0000000000..5fc111c438
--- /dev/null
+++ b/apps/desktop/src/lib/mqtt/mqttPayloadCodec.ts
@@ -0,0 +1,180 @@
+/**
+ * MQTT Payload 编解码工具
+ * 支持 Plaintext / JSON / Base64 / Hex / CBOR / MsgPack 六种格式
+ */
+import { decode as cborDecode, encode as cborEncode } from "cbor-x";
+import { decode as msgpackDecode, encode as msgpackEncode } from "@msgpack/msgpack";
+
+/** 支持的编码格式 */
+export type PayloadEncoding = "plaintext" | "json" | "base64" | "hex" | "cbor" | "msgpack";
+
+export const PAYLOAD_ENCODING_LABELS: Record<PayloadEncoding, string> = {
+ plaintext: "Plaintext",
+ json: "JSON",
+ base64: "Base64",
+ hex: "Hex",
+ cbor: "CBOR",
+ msgpack: "MsgPack",
+};
+
+export const PAYLOAD_ENCODINGS: PayloadEncoding[] = ["plaintext", "json", "base64", "hex", "cbor", "msgpack"];
+
+/**
+ * 将 base64 编码的 payload 按指定格式解码为可展示的字符串
+ */
+export function decodePayload(base64: string, encoding: PayloadEncoding): string {
+ if (!base64) return "";
+
+ try {
+ const bytes = base64ToBytes(base64);
+
+ switch (encoding) {
+ case "plaintext":
+ return bytesToUtf8(bytes);
+
+ case "json": {
+ const text = bytesToUtf8(bytes);
+ const parsed = JSON.parse(text);
+ return JSON.stringify(parsed, null, 2);
+ }
+
+ case "base64":
+ return base64;
+
+ case "hex":
+ return bytesToHex(bytes);
+
+ case "cbor": {
+ const decoded = cborDecode(bytes);
+ return formatDecodedValue(decoded);
+ }
+
+ case "msgpack": {
+ const decoded = msgpackDecode(bytes);
+ return formatDecodedValue(decoded);
+ }
+
+ default:
+ return bytesToUtf8(bytes);
+ }
+ } catch {
+ /* 解码失败时回退为纯文本显示 */
+ try {
+ return bytesToUtf8(base64ToBytes(base64));
+ } catch {
+ return `[解码失败] ${base64}`;
+ }
+ }
+}
+
+/**
+ * 将用户输入的文本按指定格式编码为 base64(用于发布消息)
+ */
+export function encodePayload(input: string, encoding: PayloadEncoding): string {
+ if (!input) return "";
+
+ try {
+ switch (encoding) {
+ case "plaintext":
+ return bytesToBase64(utf8ToBytes(input));
+
+ case "json": {
+ /* 确保输入是合法 JSON 后再编码 */
+ const parsed = JSON.parse(input);
+ return bytesToBase64(utf8ToBytes(JSON.stringify(parsed)));
+ }
+
+ case "base64":
+ /* 浏览器 atob 接受缺失 padding 和空白;规范化后再交给 Rust 严格解码 */
+ return bytesToBase64(base64ToBytes(input));
+
+ case "hex": {
+ const cleaned = input.replace(/\s/g, "");
+ if (!/^([0-9A-Fa-f]{2})*$/.test(cleaned)) {
+ throw new Error("无效的十六进制字符串");
+ }
+ const bytes = hexToBytes(cleaned);
+ return bytesToBase64(bytes);
+ }
+
+ case "cbor": {
+ /* 将 JSON 文本编码为 CBOR */
+ const parsed = JSON.parse(input);
+ const cborBytes = cborEncode(parsed);
+ return bytesToBase64(cborBytes);
+ }
+
+ case "msgpack": {
+ /* 将 JSON 文本编码为 MsgPack */
+ const parsed = JSON.parse(input);
+ const msgpackBytes = msgpackEncode(parsed);
+ return bytesToBase64(msgpackBytes);
+ }
+
+ default:
+ return bytesToBase64(utf8ToBytes(input));
+ }
+ } catch (e) {
+ throw new Error(`编码失败 (${PAYLOAD_ENCODING_LABELS[encoding]}): ${String(e)}`);
+ }
+}
+
+/* ========== 基础转换工具 ========== */
+
+function base64ToBytes(base64: string): Uint8Array {
+ const binary = atob(base64);
+ const bytes = new Uint8Array(binary.length);
+ for (let i = 0; i < binary.length; i++) {
+ bytes[i] = binary.charCodeAt(i);
+ }
+ return bytes;
+}
+
+function bytesToBase64(bytes: Uint8Array): string {
+ let binary = "";
+ for (let i = 0; i < bytes.length; i++) {
+ binary += String.fromCharCode(bytes[i]);
+ }
+ return btoa(binary);
+}
+
+function utf8ToBytes(text: string): Uint8Array {
+ return new TextEncoder().encode(text);
+}
+
+function bytesToUtf8(bytes: Uint8Array): string {
+ return new TextDecoder("utf-8", { fatal: false }).decode(bytes);
+}
+
+function bytesToHex(bytes: Uint8Array): string {
+ return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(" ");
+}
+
+function hexToBytes(hex: string): Uint8Array {
+ const bytes = new Uint8Array(hex.length / 2);
+ for (let i = 0; i < hex.length; i += 2) {
+ bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
+ }
+ return bytes;
+}
+
+function formatDecodedValue(value: unknown): string {
+ if (value === null || value === undefined) return String(value);
+ if (typeof value === "string") return value;
+ if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
+ return String(value);
+ }
+ /* Buffer / Uint8Array 等二进制类型 */
+ if (value instanceof Uint8Array || (ArrayBuffer.isView(value) && (value as ArrayBufferView).byteLength !== undefined)) {
+ return `[二进制数据, ${(value as Uint8Array).byteLength ?? (value as Uint8Array).length} bytes]`;
+ }
+ if (value instanceof ArrayBuffer) {
+ return `[二进制数据, ${value.byteLength} bytes]`;
+ }
+ /* 对象/数组 → JSON 格式化 */
+ try {
+ return JSON.stringify(value, null, 2);
+ } catch {
+ return String(value);
+ }
+}
diff --git a/apps/desktop/src/lib/sidebar/treeNodeIcon.ts b/apps/desktop/src/lib/sidebar/treeNodeIcon.ts
index 4d19a17f1a..f33f82ef0b 100644
--- a/apps/desktop/src/lib/sidebar/treeNodeIcon.ts
+++ b/apps/desktop/src/lib/sidebar/treeNodeIcon.ts
@@ -29,6 +29,7 @@ export function getTreeNodeIconInfo(node: TreeNode): TreeNodeIconInfo | null {
return { icon: FolderOpen, colorClass: "text-emerald-500" };
case "nacos-namespace":
case "etcd-root":
+ case "mqtt-topic":
return { icon: FolderOpen, colorClass: "text-sky-500" };
case "etcd-dashboard":
return { icon: Gauge, colorClass: "text-sky-500" };
diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.ts
index ae323886ba..2d2585b62d 100644
--- a/apps/desktop/src/stores/connectionStore.ts
+++ b/apps/desktop/src/stores/connectionStore.ts
@@ -2658,6 +2658,8 @@ export const useConnectionStore = defineStore("connection", () => {
await loadVectorCollections(connectionId);
} else if (config.db_type === "mq") {
await loadMqTenants(connectionId, { force: true });
+ } else if (config.db_type === "mqtt") {
+ await loadMqttTopics(connectionId);
} else if (config.db_type === "nacos") {
await loadNacosNamespaces(connectionId, { force: true });
} else {
@@ -3324,6 +3326,48 @@ export const useConnectionStore = defineStore("connection", () => {
}
}
+ async function loadMqttTopics(connectionId: string) {
+ const node = findConnectionNode(connectionId);
+ if (!node) return;
+
+ node.isLoading = true;
+ try {
+ await ensureConnected(connectionId);
+ const { mqttGetTopicTree } = await import("@/lib/backend/api");
+ const topicTree = (await mqttGetTopicTree(connectionId)) as { name: string; fullPath: string; children?: unknown[]; isLeaf: boolean };
+ const topicNodes = mqttTopicTreeToSidebarNodes(connectionId, topicTree);
+ // Always prepend a synthetic console entry so users can open the MQTT admin
+ // even when no topics are subscribed yet.
+ const consoleNode: TreeNode = {
+ id: `${connectionId}:mqtt-topic:__console__`,
+ label: "MQTT 控制台",
+ type: "mqtt-topic" as const,
+ connectionId,
+ children: topicNodes.length > 0 ? topicNodes : [],
+ isExpanded: topicNodes.length > 0,
+ };
+ setChildren(node, [consoleNode]);
+ node.isExpanded = true;
+ } catch (e) {
+ recordMetadataLoadError(connectionId, e);
+ throw e;
+ } finally {
+ node.isLoading = false;
+ }
+ }
+
+ function mqttTopicTreeToSidebarNodes(connectionId: string, tree: { name: string; fullPath: string; children?: unknown[]; isLeaf: boolean }): TreeNode[] {
+ const children = tree.children ?? [];
+ return children.map((child: any) => ({
+ id: `${connectionId}:mqtt-topic:${child.fullPath}`,
+ label: child.isLeaf ? child.name : `${child.name}/`,
+ type: "mqtt-topic",
+ connectionId,
+ children: child.children?.length > 0 ? mqttTopicTreeToSidebarNodes(connectionId, child) : [],
+ isExpanded: false,
+ })) as TreeNode[];
+ }
+
async function loadMqTenants(connectionId: string, options?: LoadTreeOptions) {
const node = findConnectionNode(connectionId);
if (!node) return;
@@ -5089,6 +5133,8 @@ export const useConnectionStore = defineStore("connection", () => {
await loadVectorCollections(node.connectionId);
} else if (config?.db_type === "mq") {
await loadMqTenants(node.connectionId, options);
+ } else if (config?.db_type === "mqtt") {
+ await loadMqttTopics(node.connectionId);
} else if (config?.db_type === "nacos") {
await loadNacosNamespaces(node.connectionId, options);
} else {
@@ -6863,6 +6909,7 @@ export const useConnectionStore = defineStore("connection", () => {
loadEtcdRoot,
loadZooKeeperRoot,
loadMqTenants,
+ loadMqttTopics,
loadNacosNamespaces,
updateRedisDbKeyStats,
loadMongoDatabases,
diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts
index df875e34c6..6e2f07f6c1 100644
--- a/apps/desktop/src/stores/queryStore.ts
+++ b/apps/desktop/src/stores/queryStore.ts
@@ -1735,6 +1735,33 @@ export const useQueryStore = defineStore("query", () => {
return id;
}
+ function openMqttAdmin(connectionId: string, target?: { initialTopic?: string }) {
+ const existing = tabs.value.find((tab) => tab.mode === "mqtt" && tab.connectionId === connectionId);
+ if (existing) {
+ if (target?.initialTopic) existing.mqttInitialTopic = target.initialTopic;
+ switchTab(existing.id);
+ return existing.id;
+ }
+
+ const conn = useConnectionStore().getConfig(connectionId);
+ const id = uuid();
+ const tab: QueryTab = {
+ id,
+ title: `${conn?.name || "MQTT"} Console`,
+ connectionId,
+ database: conn?.database || "",
+ sql: "",
+ isExecuting: false,
+ isCancelling: false,
+ isExplaining: false,
+ mode: "mqtt",
+ mqttInitialTopic: target?.initialTopic,
+ };
+ tabs.value.push(tab);
+ activeTabId.value = id;
+ return id;
+ }
+
function clearNacosNavigationTarget(connectionId: string, namespace: string, requestId?: number) {
const tab = tabs.value.find((candidate) => candidate.mode === "nacos" && candidate.connectionId === connectionId && (candidate.nacosNamespace || "") === namespace);
if (!tab || (requestId !== undefined && tab.nacosTargetRequestId !== requestId)) return;
@@ -5312,6 +5339,7 @@ export const useQueryStore = defineStore("query", () => {
openNacosDashboard,
openDamengJobAdmin,
openMqAdmin,
+ openMqttAdmin,
openNacosAdmin,
clearNacosNavigationTarget,
openTableStructure,
diff --git a/apps/desktop/src/types/database.ts b/apps/desktop/src/types/database.ts
index 98bc1b639a..f91edae25a 100644
--- a/apps/desktop/src/types/database.ts
+++ b/apps/desktop/src/types/database.ts
@@ -68,6 +68,7 @@ export type DatabaseType =
| "influxdb"
| "jdbc"
| "mq"
+ | "mqtt"
| "nacos";
export function isElasticsearchCompatibleDatabaseType(dbType?: DatabaseType): boolean {
@@ -821,7 +822,8 @@ export type TreeNodeType =
| "mongo-collection"
| "vector-database"
| "vector-collection"
- | "elasticsearch-index";
+ | "elasticsearch-index"
+ | "mqtt-topic";
export interface ConnectionGroup {
id: string;
@@ -1008,6 +1010,7 @@ export interface QueryTab {
| "etcd-access-control"
| "zookeeper"
| "mq"
+ | "mqtt"
| "nacos"
| "nacos-dashboard"
| "objects"
@@ -1021,6 +1024,7 @@ export interface QueryTab {
hbaseCreateTableOnOpen?: boolean;
mqTenant?: string;
mqInitialTab?: "topics";
+ mqttInitialTopic?: string;
nacosNamespace?: string;
nacosNamespaceName?: string;
nacosTargetDataId?: string;
diff --git a/apps/desktop/src/types/mqtt.ts b/apps/desktop/src/types/mqtt.ts
new file mode 100644
index 0000000000..56d0283bfd
--- /dev/null
+++ b/apps/desktop/src/types/mqtt.ts
@@ -0,0 +1,75 @@
+// MQTT broker connection types, matching dbx-core/src/mqtt/types.rs
+
+export type MqttProtocolVersion = "v3" | "v4" | "v5";
+export type MqttTransport = "tcp" | "websocket";
+export type MqttQoS = "atmostonce" | "atleastonce" | "exactlyonce";
+
+export interface MqttAuth {
+ kind: "none" | "password" | "certificate";
+ username?: string;
+ password?: string;
+ caCertPath?: string;
+ clientCertPath?: string;
+ clientKeyPath?: string;
+}
+
+export interface MqttConnectionConfig {
+ host: string;
+ port: number;
+ clientId: string;
+ protocolVersion: MqttProtocolVersion;
+ transport: MqttTransport;
+ tls: boolean;
+ tlsSkipVerify: boolean;
+ auth: MqttAuth;
+ keepAliveSecs: number;
+ connectTimeoutSecs: number;
+ maxPacketSizeBytes: number;
+ wsPath?: string;
+}
+
+export interface MqttBrokerInfo {
+ brokerUrl: string;
+ clientId: string;
+ connected: boolean;
+ protocolVersion: string;
+ subscriptionCount: number;
+}
+
+export type MqttMessageDirection = "sent" | "received";
+
+export interface MqttMessage {
+ topic: string;
+ payloadBase64: string;
+ payloadText?: string | null;
+ qos: number;
+ retain: boolean;
+ receivedAtMs: number;
+ /** 消息方向:sent(发出的)或 received(接收的) */
+ direction?: MqttMessageDirection;
+}
+
+export interface MqttTopicNode {
+ name: string;
+ fullPath: string;
+ children: MqttTopicNode[];
+ messageCount?: number | null;
+ isLeaf: boolean;
+}
+
+export interface MqttPublishRequest {
+ topic: string;
+ payloadBase64: string;
+ payloadText?: string | null;
+ qos: MqttQoS;
+ retain: boolean;
+}
+
+export interface MqttSubscribeRequest {
+ topic: string;
+ qos: MqttQoS;
+}
+
+export interface MqttUnsubscribeRequest {
+ topic: string;
+}
diff --git a/crates/dbx-core/Cargo.toml b/crates/dbx-core/Cargo.toml
index 84a0afa3d6..de32b57dff 100644
--- a/crates/dbx-core/Cargo.toml
+++ b/crates/dbx-core/Cargo.toml
@@ -7,7 +7,7 @@ license = "Apache-2.0"
[features]
default = ["duckdb-sidecar", "mq-admin", "sqlite-sqlcipher", "system-fonts"]
duckdb-sidecar = []
-mq-admin = []
+mq-admin = ["dep:rumqttc"]
sqlite-sqlcipher = ["rusqlite/bundled-sqlcipher-vendored-openssl"]
openapi = ["dep:utoipa"]
system-fonts = ["font-kit"]
@@ -51,6 +51,7 @@ prometheus-parse = "=0.2.5"
futures = "0.3"
iana-time-zone = "0.1"
mongodb = "3.2.5"
+rumqttc = { version = "0.24", features = ["websocket"], optional = true }
russh = "0.60"
pageant = "0.2"
portpicker = "0.1.1"
diff --git a/crates/dbx-core/src/connection.rs b/crates/dbx-core/src/connection.rs
index c10f61230b..5dc075d282 100644
--- a/crates/dbx-core/src/connection.rs
+++ b/crates/dbx-core/src/connection.rs
@@ -117,6 +117,9 @@ pub enum PoolKind {
MessageQueue,
/// Nacos admin connection marker.
Nacos,
+ /// MQTT broker connection with an active client.
+ #[cfg(feature = "mq-admin")]
+ Mqtt(Arc<super::mqtt::client::MqttClient>),
}
impl PoolKind {
@@ -1418,7 +1421,6 @@ impl AppState {
pool: PoolKind,
config: &ConnectionConfig,
) {
- // mq_registry only exists in mq-admin builds; other builds reject MQ connects before a pool is created.
#[cfg(feature = "mq-admin")]
if matches!(pool, PoolKind::MessageQueue) {
self.mq_registry.drop_connection(connection_id).await;
@@ -2209,6 +2211,18 @@ impl AppState {
.to_string(),
);
}
+ #[cfg(feature = "mq-admin")]
+ DatabaseType::Mqtt => {
+ let mqtt_config = crate::mqtt::types::MqttConnectionConfig::from_connection(&config)?;
+ let client = crate::mqtt::client::MqttClient::connect(mqtt_config).await?;
+ PoolKind::Mqtt(client)
+ }
+ #[cfg(not(feature = "mq-admin"))]
+ DatabaseType::Mqtt => {
+ return Err(
+ "MQTT support is not compiled in this build. Rebuild with the 'mq-admin' feature.".to_string()
+ );
+ }
};
if let Err(err) = self.ensure_current_connection_attempt(connection_id, connection_attempt).await {
@@ -2949,6 +2963,8 @@ impl AppState {
| PoolKind::ExternalDriver { .. }
| PoolKind::MessageQueue
| PoolKind::Nacos => false,
+ #[cfg(feature = "mq-admin")]
+ PoolKind::Mqtt(_) => false,
}
};
@@ -3824,6 +3840,8 @@ impl AppState {
| PoolKind::ExternalDriver { .. }
| PoolKind::MessageQueue
| PoolKind::Nacos => true,
+ #[cfg(feature = "mq-admin")]
+ PoolKind::Mqtt(_) => true,
PoolKind::Redis(_) => unreachable!("Redis handled separately"),
};
if !healthy && !matches!(pool, PoolKind::Agent(_)) {
@@ -4216,6 +4234,8 @@ fn connection_remote_endpoint(config: &ConnectionConfig) -> (String, u16) {
.unwrap_or_else(|| (config.host.clone(), config.port))
} else if config.db_type == DatabaseType::MessageQueue {
parse_mq_admin_host_port(config).unwrap_or_else(|| (config.host.clone(), config.port))
+ } else if config.db_type == DatabaseType::Mqtt {
+ parse_mqtt_broker_host_port(config).unwrap_or_else(|| (config.host.clone(), config.port))
} else if config.db_type == DatabaseType::Nacos {
parse_nacos_server_host_port(config).unwrap_or_else(|| (config.host.clone(), config.port))
} else {
@@ -4265,6 +4285,27 @@ fn parse_nacos_server_host_port(config: &ConnectionConfig) -> Option<(String, u1
Some((host, port))
}
+fn parse_mqtt_broker_host_port(config: &ConnectionConfig) -> Option<(String, u16)> {
+ let value = config
+ .external_config
+ .as_ref()?
+ .get("host")
+ .or_else(|| config.external_config.as_ref()?.get("host"))?
+ .as_str()?
+ .trim();
+ if value.is_empty() {
+ return None;
+ }
+ let port = config
+ .external_config
+ .as_ref()?
+ .get("port")
+ .or_else(|| config.external_config.as_ref()?.get("port"))
+ .and_then(|v| v.as_u64())
+ .unwrap_or(1883) as u16;
+ Some((value.to_string(), port))
+}
+
fn normalize_client_session_id(client_session_id: Option<&str>) -> Option<String> {
client_session_id.map(str::trim).filter(|session| !session.is_empty()).map(|session| session.replace(':', "_"))
}
@@ -4394,6 +4435,8 @@ fn clone_pool_kind(pool: &PoolKind) -> PoolKind {
}
PoolKind::MessageQueue => PoolKind::MessageQueue,
PoolKind::Nacos => PoolKind::Nacos,
+ #[cfg(feature = "mq-admin")]
+ PoolKind::Mqtt(client) => PoolKind::Mqtt(Arc::clone(client)),
PoolKind::Redis(_) => panic!("clone_pool_kind not supported for Redis — handled separately"),
}
}
@@ -4450,6 +4493,11 @@ async fn close_pool_kind(pool: PoolKind) -> Result<(), String> {
}
PoolKind::MessageQueue => {}
PoolKind::Nacos => {}
+ #[cfg(feature = "mq-admin")]
+ PoolKind::Mqtt(client) => {
+ // 发送 DISCONNECT 并等待事件循环任务结束
+ client.disconnect().await;
+ }
}
Ok(())
}
diff --git a/crates/dbx-core/src/connection_secrets.rs b/crates/dbx-core/src/connection_secrets.rs
index 89e993ce80..eca355c1f2 100644
--- a/crates/dbx-core/src/connection_secrets.rs
+++ b/crates/dbx-core/src/connection_secrets.rs
@@ -22,6 +22,8 @@ pub const MQ_TOKEN_SIGNING_KEY: &str = "mq.token_signing.key";
pub const NACOS_AUTH_SECRET_PREFIX: &str = "nacos.auth.";
pub const NACOS_AUTH_PASSWORD_KEY: &str = "nacos.auth.password";
pub const NACOS_RNACOS_CONSOLE_PASSWORD_KEY: &str = "nacos.auth.rnacos_console_password";
+pub const MQTT_AUTH_SECRET_PREFIX: &str = "mqtt.auth.";
+pub const MQTT_AUTH_PASSWORD_KEY: &str = "mqtt.auth.password";
pub trait ConnectionSecretStore {
fn set_secret(&self, connection_id: &str, key: &str, secret: &str) -> Result<(), String>;
@@ -109,6 +111,7 @@ pub fn save_connections_to_file(
persist_optional_secret(store, &config.id, INIT_SCRIPT_KEY, config.init_script.as_deref())?;
persist_mq_auth_secrets(store, config)?;
persist_mq_token_signing_secret(store, config)?;
+ persist_mqtt_auth_secrets(store, config)?;
// New configs persist transport-layer secrets only. Remove legacy transport secret slots after the
// migrated layer values have been written so old configs do not keep two sources of truth.
@@ -177,6 +180,7 @@ pub fn load_connections_from_file(
}
hydrate_mq_auth_secrets(store, config, &mut needs_rewrite)?;
hydrate_mq_token_signing_secret(store, config, &mut needs_rewrite)?;
+ hydrate_mqtt_auth_secrets(store, config, &mut needs_rewrite)?;
}
if needs_rewrite {
@@ -355,6 +359,7 @@ fn delete_removed_connection_secrets(
store.delete_secret(&config.id, INIT_SCRIPT_KEY)?;
delete_secret_prefix(store, &config.id, MQ_AUTH_SECRET_PREFIX)?;
delete_secret_prefix(store, &config.id, MQ_TOKEN_SIGNING_SECRET_PREFIX)?;
+ delete_secret_prefix(store, &config.id, MQTT_AUTH_SECRET_PREFIX)?;
}
Ok(())
}
@@ -549,6 +554,89 @@ fn scrub_mq_token_signing_secret(config: &mut ConnectionConfig) {
scrub_json_secret(signing, "key");
}
+// ── MQTT 密钥持久化 ──────────────────────────────────────────────
+
+fn persist_mqtt_auth_secrets(store: &dyn ConnectionSecretStore, config: &ConnectionConfig) -> Result<(), String> {
+ if config.db_type != DatabaseType::Mqtt {
+ delete_secret_prefix(store, &config.id, MQTT_AUTH_SECRET_PREFIX)?;
+ return Ok(());
+ }
+
+ let Some(auth) = mqtt_auth_object(config.external_config.as_ref()) else {
+ delete_secret_prefix(store, &config.id, MQTT_AUTH_SECRET_PREFIX)?;
+ return Ok(());
+ };
+
+ match mqtt_auth_kind(auth).as_deref() {
+ Some("password") => replace_mqtt_auth_secret(store, &config.id, MQTT_AUTH_PASSWORD_KEY, auth, "password")?,
+ _ => delete_secret_prefix(store, &config.id, MQTT_AUTH_SECRET_PREFIX)?,
+ }
+
+ Ok(())
+}
+
+fn hydrate_mqtt_auth_secrets(
+ store: &dyn ConnectionSecretStore,
+ config: &mut ConnectionConfig,
+ needs_rewrite: &mut bool,
+) -> Result<(), String> {
+ if config.db_type != DatabaseType::Mqtt {
+ return Ok(());
+ }
+
+ let Some(auth) = mqtt_auth_object_mut(config.external_config.as_mut()) else {
+ return Ok(());
+ };
+
+ if let Some("password") = mqtt_auth_kind(auth).as_deref() {
+ hydrate_json_secret(store, &config.id, MQTT_AUTH_PASSWORD_KEY, auth, "password", needs_rewrite)?
+ }
+
+ Ok(())
+}
+
+fn scrub_mqtt_auth_secrets(config: &mut ConnectionConfig) {
+ let Some(auth) = mqtt_auth_object_mut(config.external_config.as_mut()) else {
+ return;
+ };
+ if let Some("password") = mqtt_auth_kind(auth).as_deref() {
+ scrub_json_secret(auth, "password")
+ }
+}
+
+fn replace_mqtt_auth_secret(
+ store: &dyn ConnectionSecretStore,
+ connection_id: &str,
+ key: &str,
+ auth: &serde_json::Map<String, serde_json::Value>,
+ field: &str,
+) -> Result<(), String> {
+ let current = auth.get(field).and_then(serde_json::Value::as_str).filter(|secret| !secret.is_empty());
+ let existing = if current.is_none() { store.get_secret(connection_id, key)? } else { None };
+ delete_secret_prefix(store, connection_id, MQTT_AUTH_SECRET_PREFIX)?;
+ match current {
+ Some(secret) => store.set_secret(connection_id, key, secret),
+ None => match existing {
+ Some(secret) => store.set_secret(connection_id, key, &secret),
+ None => Ok(()),
+ },
+ }
+}
+
+fn mqtt_auth_kind(auth: &serde_json::Map<String, serde_json::Value>) -> Option<String> {
+ auth.get("kind").and_then(serde_json::Value::as_str).map(ToString::to_string)
+}
+
+fn mqtt_auth_object(value: Option<&serde_json::Value>) -> Option<&serde_json::Map<String, serde_json::Value>> {
+ value?.get("auth")?.as_object()
+}
+
+fn mqtt_auth_object_mut(
+ value: Option<&mut serde_json::Value>,
+) -> Option<&mut serde_json::Map<String, serde_json::Value>> {
+ value?.get_mut("auth")?.as_object_mut()
+}
+
fn scrub_json_secret(auth: &mut serde_json::Map<String, serde_json::Value>, field: &str) {
if auth.contains_key(field) {
auth.insert(field.to_string(), serde_json::Value::String(String::new()));
@@ -656,6 +744,7 @@ fn sanitize_connections(configs: &[ConnectionConfig]) -> Vec<ConnectionConfig> {
config.init_script = None;
scrub_mq_auth_secrets(&mut config);
scrub_mq_token_signing_secret(&mut config);
+ scrub_mqtt_auth_secrets(&mut config);
config
})
.collect()
@@ -669,8 +758,8 @@ pub fn secret_account(connection_id: &str, key: &str) -> String {
mod tests {
use super::{
load_connections_from_file, save_connections_to_file, ConnectionSecretStore, CONNECTION_STRING_KEY,
- INIT_SCRIPT_KEY, MAIN_PASSWORD_KEY, MQ_AUTH_PASSWORD_KEY, MQ_AUTH_TOKEN_KEY, MQ_TOKEN_SIGNING_KEY,
- REDIS_SENTINEL_PASSWORD_KEY, SSH_PASSWORD_KEY,
+ INIT_SCRIPT_KEY, MAIN_PASSWORD_KEY, MQTT_AUTH_PASSWORD_KEY, MQ_AUTH_PASSWORD_KEY, MQ_AUTH_TOKEN_KEY,
+ MQ_TOKEN_SIGNING_KEY, REDIS_SENTINEL_PASSWORD_KEY, SSH_PASSWORD_KEY,
};
use crate::models::connection::{
ConnectionConfig, DatabaseType, HttpTunnelConfig, SshTunnelConfig, TransportLayerConfig,
@@ -1148,4 +1237,197 @@ mod tests {
let signing = loaded[0].external_config.as_ref().and_then(|value| value.get("tokenSigning")).unwrap();
assert_eq!(signing.get("key").and_then(serde_json::Value::as_str), Some("broker-signing-secret"));
}
+
+ // ── MQTT 密钥持久化测试 ────────────────────────────────────────
+
+ #[test]
+ fn save_connections_moves_mqtt_password_to_secret_store_and_redacts_file() {
+ let path = temp_connections_file("mqtt-auth");
+ let store = MemorySecretStore::default();
+ let mut config = connection("mqtt-broker", "", "");
+ config.db_type = DatabaseType::Mqtt;
+ config.external_config = Some(serde_json::json!({
+ "host": "localhost",
+ "port": 1883,
+ "clientId": "dbx-test",
+ "protocolVersion": "v5",
+ "transport": "tcp",
+ "tls": false,
+ "tlsSkipVerify": false,
+ "auth": {
+ "kind": "password",
+ "username": "admin",
+ "password": "mqtt-secret"
+ },
+ "keepAliveSecs": 60,
+ "connectTimeoutSecs": 30
+ }));
+
+ save_connections_to_file(&path, &[config], &store).unwrap();
+
+ assert_eq!(store.get_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY).as_deref(), Some("mqtt-secret"));
+ let persisted_json = std::fs::read_to_string(&path).unwrap();
+ assert!(!persisted_json.contains("mqtt-secret"));
+ }
+
+ #[test]
+ fn load_connections_restores_mqtt_password_from_secret_store() {
+ let path = temp_connections_file("mqtt-load");
+ let store = MemorySecretStore::default();
+ store.set_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY, "mqtt-secret");
+ let sanitized = vec![{
+ let mut c = connection("mqtt-broker", "", "");
+ c.db_type = DatabaseType::Mqtt;
+ c.external_config = Some(serde_json::json!({
+ "host": "localhost",
+ "port": 1883,
+ "clientId": "dbx-test",
+ "protocolVersion": "v5",
+ "transport": "tcp",
+ "tls": false,
+ "tlsSkipVerify": false,
+ "auth": {
+ "kind": "password",
+ "username": "admin",
+ "password": ""
+ },
+ "keepAliveSecs": 60,
+ "connectTimeoutSecs": 30
+ }));
+ c
+ }];
+ std::fs::write(&path, serde_json::to_string_pretty(&sanitized).unwrap()).unwrap();
+
+ let loaded = load_connections_from_file(&path, &store).unwrap();
+ let auth = loaded[0].external_config.as_ref().and_then(|v| v.get("auth")).unwrap();
+ assert_eq!(auth.get("password").and_then(serde_json::Value::as_str), Some("mqtt-secret"));
+ }
+
+ #[test]
+ fn load_connections_migrates_plaintext_mqtt_password_and_rewrites_sanitized_file() {
+ let path = temp_connections_file("mqtt-migrate");
+ let store = MemorySecretStore::default();
+ let legacy = serde_json::json!([{
+ "id": "mqtt-legacy",
+ "name": "legacy mqtt",
+ "db_type": "mqtt",
+ "host": "localhost",
+ "port": 1883,
+ "username": "",
+ "password": "",
+ "external_config": {
+ "host": "localhost",
+ "port": 1883,
+ "clientId": "dbx-legacy",
+ "protocolVersion": "v5",
+ "transport": "tcp",
+ "tls": false,
+ "tlsSkipVerify": false,
+ "auth": {
+ "kind": "password",
+ "username": "admin",
+ "password": "plain-mqtt-password"
+ },
+ "keepAliveSecs": 60,
+ "connectTimeoutSecs": 30
+ }
+ }]);
+ std::fs::write(&path, serde_json::to_string_pretty(&legacy).unwrap()).unwrap();
+
+ let loaded = load_connections_from_file(&path, &store).unwrap();
+ let auth = loaded[0].external_config.as_ref().and_then(|v| v.get("auth")).unwrap();
+ assert_eq!(auth.get("password").and_then(serde_json::Value::as_str), Some("plain-mqtt-password"));
+ assert_eq!(store.get_existing("mqtt-legacy", MQTT_AUTH_PASSWORD_KEY).as_deref(), Some("plain-mqtt-password"));
+
+ let persisted_json = std::fs::read_to_string(&path).unwrap();
+ assert!(!persisted_json.contains("plain-mqtt-password"));
+ }
+
+ #[test]
+ fn save_connections_preserves_existing_mqtt_secret_when_config_is_sanitized() {
+ let path = temp_connections_file("mqtt-preserve");
+ let store = MemorySecretStore::default();
+ store.set_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY, "existing-mqtt-password");
+ let mut config = connection("mqtt-broker", "", "");
+ config.db_type = DatabaseType::Mqtt;
+ config.external_config = Some(serde_json::json!({
+ "host": "localhost",
+ "port": 1883,
+ "clientId": "dbx-test",
+ "protocolVersion": "v5",
+ "transport": "tcp",
+ "tls": false,
+ "tlsSkipVerify": false,
+ "auth": {
+ "kind": "password",
+ "username": "admin",
+ "password": ""
+ },
+ "keepAliveSecs": 60,
+ "connectTimeoutSecs": 30
+ }));
+
+ save_connections_to_file(&path, &[config], &store).unwrap();
+
+ assert_eq!(
+ store.get_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY).as_deref(),
+ Some("existing-mqtt-password")
+ );
+ let loaded = load_connections_from_file(&path, &store).unwrap();
+ let auth = loaded[0].external_config.as_ref().and_then(|v| v.get("auth")).unwrap();
+ assert_eq!(auth.get("password").and_then(serde_json::Value::as_str), Some("existing-mqtt-password"));
+ }
+
+ #[test]
+ fn mqtt_none_auth_does_not_store_password() {
+ let path = temp_connections_file("mqtt-none-auth");
+ let store = MemorySecretStore::default();
+ let mut config = connection("mqtt-none", "", "");
+ config.db_type = DatabaseType::Mqtt;
+ config.external_config = Some(serde_json::json!({
+ "host": "localhost",
+ "port": 1883,
+ "clientId": "dbx-test",
+ "protocolVersion": "v5",
+ "transport": "tcp",
+ "tls": false,
+ "tlsSkipVerify": false,
+ "auth": {
+ "kind": "none"
+ },
+ "keepAliveSecs": 60,
+ "connectTimeoutSecs": 30
+ }));
+
+ save_connections_to_file(&path, &[config], &store).unwrap();
+
+ assert_eq!(store.get_existing("mqtt-none", MQTT_AUTH_PASSWORD_KEY), None);
+ }
+
+ #[test]
+ fn mqtt_auth_kind_change_from_password_to_none_clears_stored_secret() {
+ let path = temp_connections_file("mqtt-kind-change");
+ let store = MemorySecretStore::default();
+ store.set_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY, "old-mqtt-password");
+ let mut config = connection("mqtt-broker", "", "");
+ config.db_type = DatabaseType::Mqtt;
+ config.external_config = Some(serde_json::json!({
+ "host": "localhost",
+ "port": 1883,
+ "clientId": "dbx-test",
+ "protocolVersion": "v5",
+ "transport": "tcp",
+ "tls": false,
+ "tlsSkipVerify": false,
+ "auth": {
+ "kind": "none"
+ },
+ "keepAliveSecs": 60,
+ "connectTimeoutSecs": 30
+ }));
+
+ save_connections_to_file(&path, &[config], &store).unwrap();
+
+ assert_eq!(store.get_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY), None);
+ }
}
diff --git a/crates/dbx-core/src/database_capabilities.rs b/crates/dbx-core/src/database_capabilities.rs
index 0391fd8e82..ca8332201a 100644
--- a/crates/dbx-core/src/database_capabilities.rs
+++ b/crates/dbx-core/src/database_capabilities.rs
@@ -56,6 +56,7 @@ pub fn skips_tcp_probe(db_type: &DatabaseType) -> bool {
| DatabaseType::CloudflareD1
| DatabaseType::Jdbc
| DatabaseType::MessageQueue
+ | DatabaseType::Mqtt
) || is_agent_type(db_type)
}
diff --git a/crates/dbx-core/src/lib.rs b/crates/dbx-core/src/lib.rs
index da9e309d6d..64cd0ba63e 100644
--- a/crates/dbx-core/src/lib.rs
+++ b/crates/dbx-core/src/lib.rs
@@ -44,8 +44,12 @@ pub mod mongo_ops;
pub mod mongo_shell;
#[cfg(feature = "mq-admin")]
pub mod mq;
+#[cfg(feature = "mq-admin")]
+pub mod mqtt;
pub(crate) mod mysql_ddl_normalize;
pub mod nacos;
+#[cfg(all(target_os = "windows", target_env = "gnu"))]
+mod nanosleep_stub;
pub mod object_source_sql;
pub mod path_utils;
pub mod plugins;
diff --git a/crates/dbx-core/src/models/connection.rs b/crates/dbx-core/src/models/connection.rs
index e257a31a86..5396d28f37 100644
--- a/crates/dbx-core/src/models/connection.rs
+++ b/crates/dbx-core/src/models/connection.rs
@@ -545,6 +545,10 @@ pub enum DatabaseType {
/// system is determined by `external_config.systemKind`.
#[serde(rename = "mq")]
MessageQueue,
+ /// MQTT broker connection. The broker address, client ID, and authentication
+ /// are stored in `external_config`.
+ #[serde(rename = "mqtt")]
+ Mqtt,
}
#[derive(Deserialize)]
@@ -1091,6 +1095,7 @@ impl ConnectionConfig {
}
DatabaseType::Jdbc => "jdbc:<redacted>".to_string(),
DatabaseType::MessageQueue => self.message_queue_admin_url(),
+ DatabaseType::Mqtt => self.mqtt_broker_url(),
DatabaseType::Nacos => self.nacos_admin_url(),
}
}
@@ -1321,6 +1326,7 @@ impl ConnectionConfig {
self.connection_string.as_deref().filter(|value| !value.is_empty()).unwrap_or("jdbc:").to_string()
}
DatabaseType::MessageQueue => self.message_queue_admin_url(),
+ DatabaseType::Mqtt => self.mqtt_broker_url(),
DatabaseType::Nacos => self.nacos_admin_url(),
}
}
@@ -1358,6 +1364,18 @@ impl ConnectionConfig {
.to_string()
}
+ fn mqtt_broker_url(&self) -> String {
+ #[cfg(feature = "mq-admin")]
+ {
+ use crate::mqtt::types::MqttConnectionConfig;
+ if let Ok(config) = MqttConnectionConfig::from_connection(self) {
+ return config.broker_url();
+ }
+ }
+ let scheme = if self.ssl { "mqtts" } else { "mqtt" };
+ format!("{}://{}:{}", scheme, self.host, self.port)
+ }
+
fn normalized_url_params(&self) -> String {
let value = self.url_params.as_deref().unwrap_or("").trim();
if self.needs_bare_mysql() {
diff --git a/crates/dbx-core/src/mqtt/client.rs b/crates/dbx-core/src/mqtt/client.rs
new file mode 100644
index 0000000000..af7e9e79ae
--- /dev/null
+++ b/crates/dbx-core/src/mqtt/client.rs
@@ -0,0 +1,2295 @@
+//! MQTT 客户端封装(基于 rumqttc),管理 MQTT broker 的长连接和消息收发。
+
+use std::collections::{HashMap, HashSet, VecDeque};
+use std::sync::atomic::{AtomicBool, Ordering};
+use std::sync::{Arc, Weak};
+use std::time::{Duration, SystemTime, UNIX_EPOCH};
+
+use rumqttc::tokio_rustls::rustls::{
+ self,
+ client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier},
+ crypto::{verify_tls12_signature, verify_tls13_signature, CryptoProvider},
+ pki_types::{CertificateDer, ServerName, UnixTime},
+};
+use rumqttc::v5::{
+ mqttbytes::QoS as QoSV5, AsyncClient as AsyncClientV5, Event as EventV5, Incoming as IncomingV5,
+ MqttOptions as MqttOptionsV5,
+};
+use rumqttc::{
+ mqttbytes::Protocol, valid_filter, valid_topic, AsyncClient as AsyncClientV4, Event as EventV4,
+ Incoming as IncomingV4, MqttOptions as MqttOptionsV4, Outgoing, QoS as QoSV4, TlsConfiguration, Transport,
+};
+use sha2::{Digest, Sha256};
+use tokio::sync::{oneshot, Mutex, Notify, RwLock};
+use tokio::task::JoinHandle;
+
+use super::types::{
+ MqttAuth, MqttBrokerInfo, MqttConnectionConfig, MqttMessage, MqttMessageDirection, MqttPublishRequest, MqttQoS,
+ MqttTlsVerificationMode, MqttTopicNode, MqttTransport,
+};
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum MqttBackendKind {
+ V4,
+ V5,
+}
+
+enum MqttBackendClient {
+ V4(AsyncClientV4),
+ V5(AsyncClientV5),
+}
+
+struct MqttConnectPlan {
+ backend: MqttBackendKind,
+ broker_addr: String,
+ transport: Transport,
+ protocol: Option<Protocol>,
+}
+
+const MQTT_RECONNECT_DELAY: Duration = Duration::from_secs(1);
+const RETAINED_DEDUP_CAPACITY: usize = 512;
+
+type SubscriptionCompletion = oneshot::Sender<Result<(), String>>;
+type PublishCompletion = oneshot::Sender<Result<(), String>>;
+
+struct PendingSubscribe {
+ sequence: u64,
+ topic: String,
+ qos: MqttQoS,
+ add_to_desired: bool,
+ completion: Option<SubscriptionCompletion>,
+}
+
+struct PendingUnsubscribe {
+ sequence: u64,
+ topic: String,
+ completion: Option<SubscriptionCompletion>,
+}
+
+struct PendingPublish {
+ sequence: u64,
+ qos: QoSV4,
+ message: MqttMessage,
+ completion: Option<PublishCompletion>,
+}
+
+impl PendingPublish {
+ fn fail(&mut self, error: &str) {
+ if let Some(completion) = self.completion.take() {
+ let _ = completion.send(Err(error.to_string()));
+ }
+ }
+}
+
+#[derive(Default)]
+struct PublishRequestTracker {
+ next_sequence: u64,
+ queued: VecDeque<PendingPublish>,
+ inflight: HashMap<u16, PendingPublish>,
+ orphaned_queued: usize,
+ orphaned_inflight: HashSet<u16>,
+}
+
+impl PublishRequestTracker {
+ fn queue(&mut self, qos: QoSV4, message: MqttMessage, completion: PublishCompletion) -> u64 {
+ self.next_sequence = self.next_sequence.wrapping_add(1);
+ let sequence = self.next_sequence;
+ self.queued.push_back(PendingPublish { sequence, qos, message, completion: Some(completion) });
+ sequence
+ }
+
+ fn remove_queued(&mut self, sequence: u64) {
+ self.queued.retain(|pending| pending.sequence != sequence);
+ }
+
+ fn mark_outgoing(&mut self, pkid: u16) -> Option<PendingPublish> {
+ if pkid != 0 && (self.inflight.contains_key(&pkid) || self.orphaned_inflight.contains(&pkid)) {
+ return None;
+ }
+ if self.orphaned_queued > 0 {
+ self.orphaned_queued -= 1;
+ if pkid != 0 {
+ self.orphaned_inflight.insert(pkid);
+ }
+ return None;
+ }
+ let Some(pending) = self.queued.pop_front() else {
+ log::warn!("MQTT PUBLISH 已发送,但未找到对应的本地待确认请求: pkid={pkid}");
+ return None;
+ };
+ if pending.qos == QoSV4::AtMostOnce {
+ return Some(pending);
+ }
+ self.inflight.insert(pkid, pending);
+ None
+ }
+
+ fn take_acknowledged(&mut self, pkid: u16) -> Option<PendingPublish> {
+ if self.orphaned_inflight.remove(&pkid) {
+ return None;
+ }
+ self.inflight.remove(&pkid)
+ }
+
+ fn fail_next_queued(&mut self, error: &str) {
+ if let Some(mut pending) = self.queued.pop_front() {
+ pending.fail(error);
+ } else {
+ log::warn!("MQTT 事件循环拒绝了 PUBLISH,但没有找到对应的本地请求: {error}");
+ }
+ }
+
+ fn take_for_connection_loss(&mut self) -> Vec<PendingPublish> {
+ self.orphaned_queued = self.orphaned_queued.saturating_add(self.queued.len());
+ let mut pending = self.queued.drain(..).collect::<Vec<_>>();
+ for (pkid, request) in self.inflight.drain() {
+ self.orphaned_inflight.insert(pkid);
+ pending.push(request);
+ }
+ pending.sort_by_key(|request| request.sequence);
+ pending
+ }
+ fn take_all(&mut self) -> Vec<PendingPublish> {
+ let mut pending = self.queued.drain(..).collect::<Vec<_>>();
+ pending.extend(self.inflight.drain().map(|(_, request)| request));
+ pending.sort_by_key(|request| request.sequence);
+ pending
+ }
+}
+
+struct RetainedDedup {
+ capacity: usize,
+ order: VecDeque<(String, [u8; 32])>,
+ entries: HashSet<(String, [u8; 32])>,
+}
+
+impl RetainedDedup {
+ fn new(capacity: usize) -> Self {
+ Self { capacity, order: VecDeque::with_capacity(capacity), entries: HashSet::with_capacity(capacity) }
+ }
+
+ fn insert(&mut self, topic: &str, payload: &[u8]) -> bool {
+ let digest: [u8; 32] = Sha256::digest(payload).into();
+ let key = (topic.to_string(), digest);
+ if !self.entries.insert(key.clone()) {
+ return false;
+ }
+ self.order.push_back(key);
+ while self.order.len() > self.capacity {
+ if let Some(oldest) = self.order.pop_front() {
+ self.entries.remove(&oldest);
+ }
+ }
+ true
+ }
+
+ fn clear_filter(&mut self, filter: &str) {
+ self.order.retain(|(topic, _)| !topic_matches_filter(topic, filter));
+ self.entries.retain(|(topic, _)| !topic_matches_filter(topic, filter));
+ }
+
+ fn clear(&mut self) {
+ self.order.clear();
+ self.entries.clear();
+ }
+}
+
+enum PendingSubscriptionAction {
+ Subscribe(PendingSubscribe),
+ Unsubscribe(PendingUnsubscribe),
+}
+
+impl PendingSubscriptionAction {
+ fn sequence(&self) -> u64 {
+ match self {
+ Self::Subscribe(pending) => pending.sequence,
+ Self::Unsubscribe(pending) => pending.sequence,
+ }
+ }
+
+ fn fail(self, error: &str) {
+ let completion = match self {
+ Self::Subscribe(pending) => pending.completion,
+ Self::Unsubscribe(pending) => pending.completion,
+ };
+ if let Some(completion) = completion {
+ let _ = completion.send(Err(error.to_string()));
+ }
+ }
+}
+
+#[derive(Default)]
+struct SubscriptionRequestTracker {
+ next_sequence: u64,
+ queued_subscribes: VecDeque<PendingSubscribe>,
+ queued_unsubscribes: VecDeque<PendingUnsubscribe>,
+ inflight_subscribes: HashMap<u16, PendingSubscribe>,
+ inflight_unsubscribes: HashMap<u16, PendingUnsubscribe>,
+}
+
+impl SubscriptionRequestTracker {
+ fn next_sequence(&mut self) -> u64 {
+ self.next_sequence = self.next_sequence.wrapping_add(1);
+ self.next_sequence
+ }
+
+ fn mark_subscribe_outgoing(&mut self, pkid: u16) {
+ if self.inflight_subscribes.contains_key(&pkid) {
+ return;
+ }
+ if let Some(pending) = self.queued_subscribes.pop_front() {
+ self.inflight_subscribes.insert(pkid, pending);
+ } else {
+ log::warn!("MQTT SUBSCRIBE 已发送,但未找到对应的本地待确认请求: pkid={pkid}");
+ }
+ }
+
+ fn mark_unsubscribe_outgoing(&mut self, pkid: u16) {
+ if self.inflight_unsubscribes.contains_key(&pkid) {
+ return;
+ }
+ if let Some(pending) = self.queued_unsubscribes.pop_front() {
+ self.inflight_unsubscribes.insert(pkid, pending);
+ } else {
+ log::warn!("MQTT UNSUBSCRIBE 已发送,但未找到对应的本地待确认请求: pkid={pkid}");
+ }
+ }
+
+ fn take_subscribe(&mut self, pkid: u16) -> Option<PendingSubscribe> {
+ self.inflight_subscribes.remove(&pkid)
+ }
+
+ fn take_unsubscribe(&mut self, pkid: u16) -> Option<PendingUnsubscribe> {
+ self.inflight_unsubscribes.remove(&pkid)
+ }
+
+ fn take_inflight(&mut self) -> Vec<PendingSubscriptionAction> {
+ let mut pending = self
+ .inflight_subscribes
+ .drain()
+ .map(|(_, request)| PendingSubscriptionAction::Subscribe(request))
+ .chain(
+ self.inflight_unsubscribes.drain().map(|(_, request)| PendingSubscriptionAction::Unsubscribe(request)),
+ )
+ .collect::<Vec<_>>();
+ pending.sort_by_key(PendingSubscriptionAction::sequence);
+ pending
+ }
+
+ fn take_all(&mut self) -> Vec<PendingSubscriptionAction> {
+ let mut pending = self
+ .queued_subscribes
+ .drain(..)
+ .map(PendingSubscriptionAction::Subscribe)
+ .chain(self.queued_unsubscribes.drain(..).map(PendingSubscriptionAction::Unsubscribe))
+ .collect::<Vec<_>>();
+ pending.extend(self.take_inflight());
+ pending.sort_by_key(PendingSubscriptionAction::sequence);
+ pending
+ }
+
+ fn has_pending_topic(&self, topic: &str) -> bool {
+ self.queued_subscribes.iter().any(|pending| pending.topic == topic)
+ || self.queued_unsubscribes.iter().any(|pending| pending.topic == topic)
+ || self.inflight_subscribes.values().any(|pending| pending.topic == topic)
+ || self.inflight_unsubscribes.values().any(|pending| pending.topic == topic)
+ }
+}
+
+/// 持久化的 MQTT 异步客户端
+pub struct MqttClient {
+ /// rumqttc 异步客户端
+ backend: MqttBackendClient,
+ /// 连接配置
+ config: MqttConnectionConfig,
+ /// 当前 broker 已通过 ACK 确认的 topic 集合
+ subscriptions: RwLock<Vec<(String, MqttQoS)>>,
+ /// broker 最近一次实际授予的 QoS,用于恢复持久会话的显示状态
+ granted_subscriptions: RwLock<Vec<(String, MqttQoS)>>,
+ /// 需要在无会话重连后恢复的 topic 集合
+ desired_subscriptions: RwLock<Vec<(String, MqttQoS)>>,
+ /// 当前连接是否已收到有效 CONNACK
+ connected: AtomicBool,
+ /// 订阅请求与 packet id 的关联状态
+ subscription_requests: Mutex<SubscriptionRequestTracker>,
+ /// 保证本地排队顺序与 rumqttc 请求通道顺序一致
+ subscription_send_lock: Mutex<()>,
+ /// 发布请求与 PUBLISH/PUBACK/PUBCOMP 的关联状态
+ publish_requests: Mutex<PublishRequestTracker>,
+ /// 保证本地发布排队顺序与 rumqttc 请求通道顺序一致
+ publish_send_lock: Mutex<()>,
+ /// 最近接收的消息缓冲区(保留最近 N 条)
+ message_buffer: RwLock<Vec<MqttMessage>>,
+ /// 最大缓冲消息数
+ max_buffer_size: usize,
+ /// 当前订阅生命周期内已见过的保留消息指纹
+ seen_retained: RwLock<RetainedDedup>,
+ /// 事件循环后台任务的 JoinHandle,用于优雅关闭时等待任务结束
+ event_loop_handle: RwLock<Option<JoinHandle<()>>>,
+ /// 通知事件循环强制退出的信号
+ shutdown_notify: Arc<Notify>,
+}
+
+impl MqttClient {
+ /// 创建新的 MQTT 客户端并建立连接。
+ /// 等待 CONNACK 确认连接成功,超时时间由 `config.connect_timeout_secs` 指定。
+ pub async fn connect(config: MqttConnectionConfig) -> Result<Arc<Self>, String> {
+ if !(1024..=268_435_455).contains(&config.max_packet_size_bytes) {
+ return Err("MQTT 最大报文大小必须在 1024 到 268435455 字节之间".to_string());
+ }
+ let plan = build_connect_plan(&config);
+ match plan.backend {
+ MqttBackendKind::V4 => Self::connect_v4(config, plan).await,
+ MqttBackendKind::V5 => Self::connect_v5(config, plan).await,
+ }
+ }
+
+ async fn connect_v4(config: MqttConnectionConfig, plan: MqttConnectPlan) -> Result<Arc<Self>, String> {
+ let client_id = config.client_id.clone();
+ let connect_timeout = Duration::from_secs(config.connect_timeout_secs);
+ let mut mqtt_options = MqttOptionsV4::new(&client_id, &plan.broker_addr, config.port);
+ mqtt_options.set_keep_alive(Duration::from_secs(config.keep_alive_secs));
+ mqtt_options.set_max_packet_size(config.max_packet_size_bytes, config.max_packet_size_bytes);
+ mqtt_options.set_transport(plan.transport);
+ if let Some(protocol) = plan.protocol {
+ mqtt_options.set_protocol(protocol);
+ }
+ apply_credentials_v4(&mut mqtt_options, &config.auth);
+
+ let (client, event_loop) = AsyncClientV4::new(mqtt_options, 100);
+ let shutdown_notify = Arc::new(Notify::new());
+ let instance = Self::new_with_backend(config, MqttBackendClient::V4(client), Arc::clone(&shutdown_notify));
+
+ let (connack_tx, connack_rx) = oneshot::channel();
+ let handle = Self::spawn_v4_event_loop(Arc::downgrade(&instance), event_loop, shutdown_notify, connack_tx);
+ Self::wait_for_connack(instance, handle, connack_rx, connect_timeout).await
+ }
+
+ async fn connect_v5(config: MqttConnectionConfig, plan: MqttConnectPlan) -> Result<Arc<Self>, String> {
+ let client_id = config.client_id.clone();
+ let connect_timeout = Duration::from_secs(config.connect_timeout_secs);
+ let mut mqtt_options = MqttOptionsV5::new(&client_id, &plan.broker_addr, config.port);
+ mqtt_options.set_keep_alive(Duration::from_secs(config.keep_alive_secs));
+ mqtt_options.set_max_packet_size(Some(config.max_packet_size_bytes as u32));
+ mqtt_options.set_transport(plan.transport);
+ apply_credentials_v5(&mut mqtt_options, &config.auth);
+
+ let (client, event_loop) = AsyncClientV5::new(mqtt_options, 100);
+ let shutdown_notify = Arc::new(Notify::new());
+ let instance = Self::new_with_backend(config, MqttBackendClient::V5(client), Arc::clone(&shutdown_notify));
+
+ let (connack_tx, connack_rx) = oneshot::channel();
+ let handle = Self::spawn_v5_event_loop(Arc::downgrade(&instance), event_loop, shutdown_notify, connack_tx);
+ Self::wait_for_connack(instance, handle, connack_rx, connect_timeout).await
+ }
+
+ fn new_with_backend(
+ config: MqttConnectionConfig,
+ backend: MqttBackendClient,
+ shutdown_notify: Arc<Notify>,
+ ) -> Arc<Self> {
+ Arc::new(Self {
+ backend,
+ config,
+ subscriptions: RwLock::new(Vec::new()),
+ granted_subscriptions: RwLock::new(Vec::new()),
+ desired_subscriptions: RwLock::new(Vec::new()),
+ connected: AtomicBool::new(false),
+ subscription_requests: Mutex::new(SubscriptionRequestTracker::default()),
+ subscription_send_lock: Mutex::new(()),
+ publish_requests: Mutex::new(PublishRequestTracker::default()),
+ publish_send_lock: Mutex::new(()),
+ message_buffer: RwLock::new(Vec::new()),
+ max_buffer_size: 200,
+ seen_retained: RwLock::new(RetainedDedup::new(RETAINED_DEDUP_CAPACITY)),
+ event_loop_handle: RwLock::new(None),
+ shutdown_notify,
+ })
+ }
+
+ fn spawn_v4_event_loop(
+ instance: Weak<Self>,
+ mut event_loop: rumqttc::EventLoop,
+ shutdown_notify: Arc<Notify>,
+ connack_tx: oneshot::Sender<Result<(), String>>,
+ ) -> JoinHandle<()> {
+ tokio::spawn(async move {
+ let mut connack_tx = Some(connack_tx);
+ loop {
+ tokio::select! {
+ event = event_loop.poll() => {
+ match event {
+ Ok(EventV4::Incoming(IncomingV4::ConnAck(connack))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.handle_connack(connack.session_present).await;
+ log::info!(
+ "MQTT CONNACK 已收到: session_present={}, code={:?}",
+ connack.session_present,
+ connack.code
+ );
+ if let Some(tx) = connack_tx.take() {
+ let _ = tx.send(Ok(()));
+ }
+ }
+ Ok(EventV4::Incoming(IncomingV4::Publish(publish))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ Self::record_received_publish(
+ &instance,
+ publish.topic.as_bytes(),
+ publish.payload.as_ref(),
+ qos_to_u8(publish.qos),
+ publish.retain,
+ ).await;
+ }
+ Ok(EventV4::Incoming(IncomingV4::SubAck(suback))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ let result = match suback.return_codes.as_slice() {
+ [rumqttc::SubscribeReasonCode::Success(qos)] => Ok(mqtt_qos_from_v4(*qos)),
+ [rumqttc::SubscribeReasonCode::Failure] => {
+ Err("MQTT broker 拒绝了订阅请求".to_string())
+ }
+ _ => Err("MQTT broker 返回的 SUBACK 数量与订阅请求不一致".to_string()),
+ };
+ instance.complete_subscribe_ack(suback.pkid, result).await;
+ }
+ Ok(EventV4::Incoming(IncomingV4::PubAck(puback))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.complete_publish_ack(puback.pkid).await;
+ }
+ Ok(EventV4::Incoming(IncomingV4::PubComp(pubcomp))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.complete_publish_ack(pubcomp.pkid).await;
+ }
+ Ok(EventV4::Incoming(IncomingV4::UnsubAck(unsuback))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.complete_unsubscribe_ack(unsuback.pkid, Ok(())).await;
+ }
+ Ok(EventV4::Incoming(IncomingV4::Disconnect)) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.handle_connection_lost("broker 主动断开连接").await;
+ if let Some(tx) = connack_tx.take() {
+ let _ = tx.send(Err("MQTT broker 在 CONNACK 之前断开了连接".to_string()));
+ break;
+ }
+ log::warn!("MQTT broker 断开了连接,将在 1 秒后重新连接");
+ event_loop.clean();
+ tokio::time::sleep(MQTT_RECONNECT_DELAY).await;
+ }
+ Ok(EventV4::Outgoing(Outgoing::Publish(pkid))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.mark_publish_outgoing(pkid).await;
+ }
+ Ok(EventV4::Outgoing(Outgoing::Subscribe(pkid))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.mark_subscribe_outgoing(pkid).await;
+ }
+ Ok(EventV4::Outgoing(Outgoing::Unsubscribe(pkid))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.mark_unsubscribe_outgoing(pkid).await;
+ }
+ Ok(EventV4::Outgoing(Outgoing::Disconnect)) => {
+ log::info!("MQTT DISCONNECT 已发送,事件循环退出");
+ break;
+ }
+ Ok(_) => {}
+ Err(e) => {
+ if let Some(instance) = instance.upgrade() {
+ if matches!(
+ &e,
+ rumqttc::ConnectionError::MqttState(
+ rumqttc::StateError::OutgoingPacketTooLarge { .. }
+ )
+ ) {
+ instance.fail_next_queued_publish(&format!("发布消息失败:{e}")).await;
+ }
+ instance.handle_connection_lost(&e.to_string()).await;
+ }
+ if let Some(tx) = connack_tx.take() {
+ let _ = tx.send(Err(format!("MQTT 连接失败(未收到 CONNACK): {e}")));
+ break;
+ }
+ log::warn!("MQTT 连接发生异常,将在 1 秒后重新连接: {e}");
+ tokio::time::sleep(MQTT_RECONNECT_DELAY).await;
+ }
+ }
+ }
+ _ = shutdown_notify.notified() => {
+ log::info!("MQTT 事件循环收到强制关闭信号,正在退出");
+ if let Some(tx) = connack_tx.take() {
+ let _ = tx.send(Err("MQTT 连接已取消".to_string()));
+ }
+ break;
+ }
+ }
+ }
+ })
+ }
+
+ fn spawn_v5_event_loop(
+ instance: Weak<Self>,
+ mut event_loop: rumqttc::v5::EventLoop,
+ shutdown_notify: Arc<Notify>,
+ connack_tx: oneshot::Sender<Result<(), String>>,
+ ) -> JoinHandle<()> {
+ tokio::spawn(async move {
+ let mut connack_tx = Some(connack_tx);
+ loop {
+ tokio::select! {
+ event = event_loop.poll() => {
+ match event {
+ Ok(EventV5::Incoming(IncomingV5::ConnAck(connack))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.handle_connack(connack.session_present).await;
+ log::info!(
+ "MQTT v5 CONNACK 已收到: session_present={}, code={:?}",
+ connack.session_present,
+ connack.code
+ );
+ if let Some(tx) = connack_tx.take() {
+ let _ = tx.send(Ok(()));
+ }
+ }
+ Ok(EventV5::Incoming(IncomingV5::Publish(publish))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ Self::record_received_publish(
+ &instance,
+ publish.topic.as_ref(),
+ publish.payload.as_ref(),
+ qos_v5_to_u8(publish.qos),
+ publish.retain,
+ ).await;
+ }
+ Ok(EventV5::Incoming(IncomingV5::SubAck(suback))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ let result = match suback.return_codes.as_slice() {
+ [rumqttc::v5::mqttbytes::v5::SubscribeReasonCode::Success(qos)] => {
+ Ok(mqtt_qos_from_v5(*qos))
+ }
+ [reason] => {
+ let detail = suback
+ .properties
+ .as_ref()
+ .and_then(|properties| properties.reason_string.as_deref())
+ .map(|message| format!(",broker 提示:{message}"))
+ .unwrap_or_default();
+ Err(format!("MQTT v5 broker 拒绝了订阅请求:{reason:?}{detail}"))
+ }
+ _ => Err("MQTT v5 broker 返回的 SUBACK 数量与订阅请求不一致".to_string()),
+ };
+ instance.complete_subscribe_ack(suback.pkid, result).await;
+ }
+ Ok(EventV5::Incoming(IncomingV5::PubAck(puback))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.complete_publish_ack(puback.pkid).await;
+ }
+ Ok(EventV5::Incoming(IncomingV5::PubComp(pubcomp))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.complete_publish_ack(pubcomp.pkid).await;
+ }
+ Ok(EventV5::Incoming(IncomingV5::UnsubAck(unsuback))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ let rejected = unsuback.reasons.iter().find(|reason| {
+ !matches!(
+ reason,
+ rumqttc::v5::mqttbytes::v5::UnsubAckReason::Success
+ | rumqttc::v5::mqttbytes::v5::UnsubAckReason::NoSubscriptionExisted
+ )
+ });
+ let result = rejected.map_or(Ok(()), |reason| {
+ let detail = unsuback
+ .properties
+ .as_ref()
+ .and_then(|properties| properties.reason_string.as_deref())
+ .map(|message| format!(",broker 提示:{message}"))
+ .unwrap_or_default();
+ Err(format!("MQTT v5 broker 拒绝了取消订阅请求:{reason:?}{detail}"))
+ });
+ instance.complete_unsubscribe_ack(unsuback.pkid, result).await;
+ }
+ Ok(EventV5::Incoming(IncomingV5::Disconnect(disconnect))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.handle_connection_lost("broker 主动断开连接").await;
+ if let Some(tx) = connack_tx.take() {
+ let _ = tx.send(Err("MQTT broker 在 CONNACK 之前断开了连接".to_string()));
+ break;
+ }
+ log::warn!("MQTT v5 broker 断开了连接,将在 1 秒后重新连接: {disconnect:?}");
+ event_loop.clean();
+ tokio::time::sleep(MQTT_RECONNECT_DELAY).await;
+ }
+ Ok(EventV5::Outgoing(Outgoing::Publish(pkid))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.mark_publish_outgoing(pkid).await;
+ }
+ Ok(EventV5::Outgoing(Outgoing::Subscribe(pkid))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.mark_subscribe_outgoing(pkid).await;
+ }
+ Ok(EventV5::Outgoing(Outgoing::Unsubscribe(pkid))) => {
+ let Some(instance) = instance.upgrade() else { break };
+ instance.mark_unsubscribe_outgoing(pkid).await;
+ }
+ Ok(EventV5::Outgoing(Outgoing::Disconnect)) => {
+ log::info!("MQTT v5 DISCONNECT 已发送,事件循环退出");
+ break;
+ }
+ Ok(_) => {}
+ Err(e) => {
+ if let Some(instance) = instance.upgrade() {
+ if matches!(
+ &e,
+ rumqttc::v5::ConnectionError::MqttState(
+ rumqttc::v5::StateError::OutgoingPacketTooLarge { .. }
+ )
+ ) {
+ instance.fail_next_queued_publish(&format!("发布消息失败:{e}")).await;
+ }
+ instance.handle_connection_lost(&e.to_string()).await;
+ }
+ if let Some(tx) = connack_tx.take() {
+ let _ = tx.send(Err(format!("MQTT 连接失败(未收到 CONNACK): {e}")));
+ break;
+ }
+ log::warn!("MQTT v5 连接发生异常,将在 1 秒后重新连接: {e}");
+ tokio::time::sleep(MQTT_RECONNECT_DELAY).await;
+ }
+ }
+ }
+ _ = shutdown_notify.notified() => {
+ log::info!("MQTT v5 事件循环收到强制关闭信号,正在退出");
+ if let Some(tx) = connack_tx.take() {
+ let _ = tx.send(Err("MQTT 连接已取消".to_string()));
+ }
+ break;
+ }
+ }
+ }
+ })
+ }
+
+ async fn wait_for_connack(
+ instance: Arc<Self>,
+ handle: JoinHandle<()>,
+ connack_rx: oneshot::Receiver<Result<(), String>>,
+ connect_timeout: Duration,
+ ) -> Result<Arc<Self>, String> {
+ match tokio::time::timeout(connect_timeout, connack_rx).await {
+ Ok(Ok(Ok(()))) => {
+ {
+ let mut handle_lock = instance.event_loop_handle.write().await;
+ *handle_lock = Some(handle);
+ }
+ log::info!("MQTT 客户端已成功连接到 {}", instance.config.broker_url());
+ Ok(instance)
+ }
+ Ok(Ok(Err(e))) => {
+ handle.abort();
+ let _ = handle.await;
+ Err(e)
+ }
+ Ok(Err(_)) => {
+ handle.abort();
+ let _ = handle.await;
+ Err("MQTT 事件循环意外退出,未收到 CONNACK".to_string())
+ }
+ Err(_) => {
+ handle.abort();
+ let _ = handle.await;
+ Err(format!("MQTT 连接超时:{} 秒内未收到 CONNACK", connect_timeout.as_secs()))
+ }
+ }
+ }
+
+ async fn record_received_publish(instance: &Self, topic: &[u8], payload: &[u8], qos: u8, retain: bool) {
+ let now_ms = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis() as u64;
+ let topic = decode_utf8_lossy(topic);
+ let payload_base64 = {
+ use base64::Engine;
+ base64::engine::general_purpose::STANDARD.encode(payload)
+ };
+
+ if retain {
+ let mut seen = instance.seen_retained.write().await;
+ if !seen.insert(&topic, payload) {
+ return;
+ }
+ }
+
+ let payload_text = String::from_utf8(payload.to_vec()).ok();
+ let msg = MqttMessage {
+ topic,
+ payload_base64,
+ payload_text,
+ qos,
+ retain,
+ received_at_ms: now_ms,
+ direction: MqttMessageDirection::Received,
+ };
+
+ let mut buffer = instance.message_buffer.write().await;
+ buffer.push(msg);
+ if buffer.len() > instance.max_buffer_size {
+ buffer.remove(0);
+ }
+ }
+
+ async fn handle_connack(self: &Arc<Self>, session_present: bool) {
+ self.connected.store(true, Ordering::Release);
+
+ if session_present {
+ let granted = self.granted_subscriptions.read().await.clone();
+ *self.subscriptions.write().await = granted;
+ return;
+ }
+
+ self.subscriptions.write().await.clear();
+ let instance = Arc::downgrade(self);
+ tokio::spawn(async move {
+ let Some(instance) = instance.upgrade() else {
+ return;
+ };
+ instance.restore_desired_subscriptions().await;
+ });
+ }
+
+ async fn handle_connection_lost(&self, reason: &str) {
+ if self.connected.swap(false, Ordering::AcqRel) {
+ log::warn!("MQTT 连接已断开: {reason}");
+ }
+ self.subscriptions.write().await.clear();
+
+ let pending = self.subscription_requests.lock().await.take_inflight();
+ for request in pending {
+ request.fail("MQTT 连接在收到 broker 确认前中断,请重新执行该操作");
+ }
+ let mut pending_publishes = self.publish_requests.lock().await.take_for_connection_loss();
+ for publish in &mut pending_publishes {
+ publish.fail(&format!("发布消息未完成:MQTT 协议处理或连接失败({reason}),请确认 broker 状态后重试"));
+ }
+ }
+
+ async fn restore_desired_subscriptions(&self) {
+ let desired = self.desired_subscriptions.read().await.clone();
+ for (topic, qos) in desired {
+ let has_pending = self.subscription_requests.lock().await.has_pending_topic(&topic);
+ if has_pending {
+ continue;
+ }
+ if let Err(error) = self.queue_subscribe_request(topic.clone(), qos, false, None).await {
+ log::warn!("MQTT 重连后恢复订阅 {topic} 失败: {error}");
+ }
+ }
+ }
+
+ async fn mark_subscribe_outgoing(&self, pkid: u16) {
+ self.subscription_requests.lock().await.mark_subscribe_outgoing(pkid);
+ }
+
+ async fn mark_unsubscribe_outgoing(&self, pkid: u16) {
+ self.subscription_requests.lock().await.mark_unsubscribe_outgoing(pkid);
+ }
+
+ async fn mark_publish_outgoing(&self, pkid: u16) {
+ let pending = self.publish_requests.lock().await.mark_outgoing(pkid);
+ if let Some(pending) = pending {
+ self.complete_publish(pending).await;
+ }
+ }
+
+ async fn complete_publish_ack(&self, pkid: u16) {
+ let pending = self.publish_requests.lock().await.take_acknowledged(pkid);
+ if let Some(pending) = pending {
+ self.complete_publish(pending).await;
+ } else {
+ log::warn!("收到无法关联的 MQTT 发布确认: pkid={pkid}");
+ }
+ }
+
+ async fn complete_publish(&self, mut pending: PendingPublish) {
+ let mut buffer = self.message_buffer.write().await;
+ buffer.push(pending.message);
+ if buffer.len() > self.max_buffer_size {
+ buffer.remove(0);
+ }
+ if let Some(completion) = pending.completion.take() {
+ let _ = completion.send(Ok(()));
+ }
+ }
+
+ async fn fail_next_queued_publish(&self, error: &str) {
+ self.publish_requests.lock().await.fail_next_queued(error);
+ }
+
+ async fn complete_subscribe_ack(&self, pkid: u16, result: Result<MqttQoS, String>) {
+ let Some(pending) = self.subscription_requests.lock().await.take_subscribe(pkid) else {
+ log::warn!("收到无法关联的 MQTT SUBACK: pkid={pkid}");
+ return;
+ };
+
+ let granted_qos = result.map_err(|error| format!("订阅主题“{}”失败:{error}", pending.topic));
+ let completion_result = match granted_qos {
+ Ok(granted_qos) => {
+ upsert_subscription(&self.subscriptions, &pending.topic, granted_qos).await;
+ upsert_subscription(&self.granted_subscriptions, &pending.topic, granted_qos).await;
+ self.seen_retained.write().await.clear_filter(&pending.topic);
+ if pending.add_to_desired {
+ upsert_subscription(&self.desired_subscriptions, &pending.topic, pending.qos).await;
+ }
+ Ok(())
+ }
+ Err(error) => {
+ log::warn!("{error}");
+ Err(error)
+ }
+ };
+
+ if let Some(completion) = pending.completion {
+ let _ = completion.send(completion_result);
+ }
+ }
+
+ async fn complete_unsubscribe_ack(&self, pkid: u16, result: Result<(), String>) {
+ let Some(pending) = self.subscription_requests.lock().await.take_unsubscribe(pkid) else {
+ log::warn!("收到无法关联的 MQTT UNSUBACK: pkid={pkid}");
+ return;
+ };
+
+ let result = result.map_err(|error| format!("取消订阅主题“{}”失败:{error}", pending.topic));
+ if result.is_ok() {
+ remove_subscription(&self.subscriptions, &pending.topic).await;
+ remove_subscription(&self.granted_subscriptions, &pending.topic).await;
+ remove_subscription(&self.desired_subscriptions, &pending.topic).await;
+ self.seen_retained.write().await.clear_filter(&pending.topic);
+ } else if let Err(error) = &result {
+ log::warn!("{error}");
+ }
+
+ if let Some(completion) = pending.completion {
+ let _ = completion.send(result);
+ }
+ }
+
+ async fn queue_subscribe_request(
+ &self,
+ topic: String,
+ qos: MqttQoS,
+ add_to_desired: bool,
+ completion: Option<SubscriptionCompletion>,
+ ) -> Result<(), String> {
+ let _send_guard = self.subscription_send_lock.lock().await;
+ let sequence = {
+ let mut requests = self.subscription_requests.lock().await;
+ if requests.has_pending_topic(&topic) {
+ return Err(format!("主题“{topic}”的订阅操作正在处理中,请稍后再试"));
+ }
+ let sequence = requests.next_sequence();
+ requests.queued_subscribes.push_back(PendingSubscribe {
+ sequence,
+ topic: topic.clone(),
+ qos,
+ add_to_desired,
+ completion,
+ });
+ sequence
+ };
+
+ if let Err(error) = self.backend.subscribe(&topic, mqtt_qos(qos)).await {
+ self.subscription_requests.lock().await.queued_subscribes.retain(|pending| pending.sequence != sequence);
+ return Err(error);
+ }
+ Ok(())
+ }
+
+ async fn queue_unsubscribe_request(
+ &self,
+ topic: String,
+ completion: Option<SubscriptionCompletion>,
+ ) -> Result<(), String> {
+ let _send_guard = self.subscription_send_lock.lock().await;
+ let sequence = {
+ let mut requests = self.subscription_requests.lock().await;
+ if requests.has_pending_topic(&topic) {
+ return Err(format!("主题“{topic}”的订阅操作正在处理中,请稍后再试"));
+ }
+ let sequence = requests.next_sequence();
+ requests.queued_unsubscribes.push_back(PendingUnsubscribe { sequence, topic: topic.clone(), completion });
+ sequence
+ };
+
+ if let Err(error) = self.backend.unsubscribe(&topic).await {
+ self.subscription_requests.lock().await.queued_unsubscribes.retain(|pending| pending.sequence != sequence);
+ return Err(error);
+ }
+ Ok(())
+ }
+
+ /// 获取 broker 基本信息
+ pub async fn broker_info(&self) -> MqttBrokerInfo {
+ let subscriptions = self.subscriptions.read().await;
+ let protocol_version = match self.config.protocol_version {
+ super::types::MqttProtocolVersion::V3 => "MQTT 3.1".to_string(),
+ super::types::MqttProtocolVersion::V4 => "MQTT 3.1.1".to_string(),
+ super::types::MqttProtocolVersion::V5 => "MQTT 5.0".to_string(),
+ };
+ MqttBrokerInfo {
+ broker_url: self.config.broker_url(),
+ client_id: self.config.client_id.clone(),
+ connected: self.connected.load(Ordering::Acquire),
+ protocol_version,
+ subscription_count: subscriptions.len(),
+ }
+ }
+
+ /// 订阅 topic
+ pub async fn subscribe(&self, topic: &str, qos: MqttQoS) -> Result<(), String> {
+ validate_topic_filter(topic)?;
+ if self
+ .desired_subscriptions
+ .read()
+ .await
+ .iter()
+ .any(|(current, current_qos)| current == topic && *current_qos == qos)
+ {
+ return Ok(());
+ }
+ if self.subscription_requests.lock().await.has_pending_topic(topic) {
+ return Err(format!("主题“{topic}”的订阅操作正在处理中,请稍后再试"));
+ }
+
+ let (completion, result) = oneshot::channel();
+ self.queue_subscribe_request(topic.to_string(), qos, true, Some(completion)).await?;
+ result.await.map_err(|_| "订阅确认通道已关闭,请检查 MQTT 连接状态".to_string())?
+ }
+
+ /// 取消订阅 topic
+ pub async fn unsubscribe(&self, topic: &str) -> Result<(), String> {
+ validate_topic_filter(topic)?;
+ let exists = self.desired_subscriptions.read().await.iter().any(|(current, _)| current == topic);
+ if !exists {
+ return Ok(());
+ }
+ if self.subscription_requests.lock().await.has_pending_topic(topic) {
+ return Err(format!("主题“{topic}”的订阅操作正在处理中,请稍后再试"));
+ }
+
+ let (completion, result) = oneshot::channel();
+ self.queue_unsubscribe_request(topic.to_string(), Some(completion)).await?;
+ result.await.map_err(|_| "取消订阅确认通道已关闭,请检查 MQTT 连接状态".to_string())?
+ }
+
+ /// 发布消息;QoS 0 等待写入网络,QoS 1/2 分别等待 PUBACK/PUBCOMP。
+ pub async fn publish(&self, request: &MqttPublishRequest) -> Result<(), String> {
+ validate_publish_topic(&request.topic)?;
+ let payload = if let Some(ref text) = request.payload_text {
+ text.as_bytes().to_vec()
+ } else {
+ use base64::Engine;
+ base64::engine::general_purpose::STANDARD
+ .decode(&request.payload_base64)
+ .map_err(|e| format!("Base64 解码失败: {e}"))?
+ };
+ let packet_size =
+ mqtt_publish_packet_size(&request.topic, payload.len(), request.qos, self.config.protocol_version);
+ if packet_size > self.config.max_packet_size_bytes {
+ return Err(format!(
+ "发布消息失败:报文大小约为 {packet_size} 字节,超过当前连接允许的 {} 字节,请调整最大报文大小或缩小消息负载",
+ self.config.max_packet_size_bytes
+ ));
+ }
+
+ let qos = mqtt_qos(request.qos);
+ let now_ms = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis() as u64;
+ let payload_base64 = if request.payload_text.is_some() {
+ use base64::Engine;
+ base64::engine::general_purpose::STANDARD.encode(&payload)
+ } else {
+ request.payload_base64.clone()
+ };
+ let sent_msg = MqttMessage {
+ topic: request.topic.clone(),
+ payload_base64,
+ payload_text: request.payload_text.clone(),
+ qos: qos_to_u8(qos),
+ retain: request.retain,
+ received_at_ms: now_ms,
+ direction: MqttMessageDirection::Sent,
+ };
+
+ let _send_guard = self.publish_send_lock.lock().await;
+ let (completion, result) = oneshot::channel();
+ let sequence = self.publish_requests.lock().await.queue(qos, sent_msg, completion);
+ if let Err(error) = self.backend.publish(&request.topic, qos, request.retain, payload).await {
+ self.publish_requests.lock().await.remove_queued(sequence);
+ return Err(error);
+ }
+ drop(_send_guard);
+ result.await.map_err(|_| "发布确认通道已关闭,请检查 MQTT 连接状态".to_string())?
+ }
+
+ /// 获取已订阅的 topic 列表
+ pub async fn list_topics(&self) -> Vec<(String, MqttQoS)> {
+ self.subscriptions.read().await.clone()
+ }
+
+ /// 获取消息缓冲区中的消息
+ pub async fn get_messages(&self, topic_filter: Option<&str>, limit: usize) -> Vec<MqttMessage> {
+ let buffer = self.message_buffer.read().await;
+ let filtered: Vec<MqttMessage> = buffer
+ .iter()
+ .filter(|msg| {
+ topic_filter
+ .map(|filter| msg.topic == filter || topic_matches_filter(&msg.topic, filter))
+ .unwrap_or(true)
+ })
+ .rev()
+ .take(limit)
+ .cloned()
+ .collect();
+ filtered
+ }
+
+ /// 清空消息缓冲区
+ pub async fn clear_messages(&self) {
+ self.message_buffer.write().await.clear();
+ self.seen_retained.write().await.clear();
+ }
+
+ /// 从 topic 列表构建 topic 树
+ pub async fn build_topic_tree(&self) -> MqttTopicNode {
+ let subscriptions = self.subscriptions.read().await;
+ let mut root = MqttTopicNode {
+ name: "root".to_string(),
+ full_path: String::new(),
+ children: Vec::new(),
+ message_count: None,
+ is_leaf: false,
+ };
+
+ for (topic, _) in subscriptions.iter() {
+ insert_topic_into_tree(&mut root, topic);
+ }
+ sort_topic_tree(&mut root);
+ root
+ }
+
+ /// 优雅关闭:发送 DISCONNECT,等待事件循环退出;超时后强制中止后台任务。
+ /// 调用此方法后不应再使用该客户端。
+ pub async fn disconnect(&self) {
+ log::info!("MQTT 客户端正在断开连接...");
+ self.connected.store(false, Ordering::Release);
+ self.subscriptions.write().await.clear();
+ self.granted_subscriptions.write().await.clear();
+ self.seen_retained.write().await.clear();
+ let pending = self.subscription_requests.lock().await.take_all();
+ for request in pending {
+ request.fail("MQTT 连接已关闭,操作未完成");
+ }
+ let mut pending_publishes = self.publish_requests.lock().await.take_all();
+ for publish in &mut pending_publishes {
+ publish.fail("MQTT 连接已关闭,发布操作未完成");
+ }
+
+ if let Err(err) = self.backend.disconnect().await {
+ log::warn!("MQTT DISCONNECT 请求发送失败: {err}");
+ }
+
+ let mut handle_lock = self.event_loop_handle.write().await;
+ if let Some(mut handle) = handle_lock.take() {
+ tokio::select! {
+ result = &mut handle => {
+ let _ = result;
+ }
+ _ = tokio::time::sleep(Duration::from_secs(5)) => {
+ log::warn!("MQTT 事件循环未在 5 秒内退出,正在强制中止");
+ self.shutdown_notify.notify_waiters();
+ handle.abort();
+ let _ = handle.await;
+ }
+ }
+ }
+ }
+}
+
+impl Drop for MqttClient {
+ fn drop(&mut self) {
+ self.shutdown_notify.notify_one();
+ }
+}
+
+impl MqttBackendClient {
+ async fn subscribe(&self, topic: &str, qos: QoSV4) -> Result<(), String> {
+ match self {
+ MqttBackendClient::V4(client) => {
+ client.subscribe(topic, qos).await.map_err(|e| format!("订阅 topic 失败: {e}"))
+ }
+ MqttBackendClient::V5(client) => {
+ client.subscribe(topic, qos_v4_to_v5(qos)).await.map_err(|e| format!("订阅 topic 失败: {e}"))
+ }
+ }
+ }
+
+ async fn unsubscribe(&self, topic: &str) -> Result<(), String> {
+ match self {
+ MqttBackendClient::V4(client) => {
+ client.unsubscribe(topic).await.map_err(|e| format!("取消订阅 topic 失败: {e}"))
+ }
+ MqttBackendClient::V5(client) => {
+ client.unsubscribe(topic).await.map_err(|e| format!("取消订阅 topic 失败: {e}"))
+ }
+ }
+ }
+
+ async fn publish(&self, topic: &str, qos: QoSV4, retain: bool, payload: Vec<u8>) -> Result<(), String> {
+ match self {
+ MqttBackendClient::V4(client) => {
+ client.publish(topic, qos, retain, payload).await.map_err(|e| format!("发布消息失败: {e}"))
+ }
+ MqttBackendClient::V5(client) => client
+ .publish(topic, qos_v4_to_v5(qos), retain, payload)
+ .await
+ .map_err(|e| format!("发布消息失败: {e}")),
+ }
+ }
+
+ async fn disconnect(&self) -> Result<(), String> {
+ match self {
+ MqttBackendClient::V4(client) => client.disconnect().await.map_err(|e| format!("断开 MQTT 连接失败: {e}")),
+ MqttBackendClient::V5(client) => client.disconnect().await.map_err(|e| format!("断开 MQTT 连接失败: {e}")),
+ }
+ }
+}
+
+fn build_connect_plan(config: &MqttConnectionConfig) -> MqttConnectPlan {
+ let backend = match config.protocol_version {
+ super::types::MqttProtocolVersion::V3 | super::types::MqttProtocolVersion::V4 => MqttBackendKind::V4,
+ super::types::MqttProtocolVersion::V5 => MqttBackendKind::V5,
+ };
+ let protocol = match config.protocol_version {
+ super::types::MqttProtocolVersion::V3 => Some(Protocol::V3),
+ super::types::MqttProtocolVersion::V4 => Some(Protocol::V4),
+ super::types::MqttProtocolVersion::V5 => None,
+ };
+ let tls_verification_mode = config.tls_verification_mode();
+ let transport = build_transport(config.transport, tls_verification_mode);
+
+ MqttConnectPlan { backend, broker_addr: config.broker_addr_for_transport(), transport, protocol }
+}
+
+fn build_transport(transport: MqttTransport, tls_verification_mode: Option<MqttTlsVerificationMode>) -> Transport {
+ match (transport, tls_verification_mode) {
+ (MqttTransport::Tcp, None) => Transport::Tcp,
+ (MqttTransport::Tcp, Some(MqttTlsVerificationMode::VerifyServerCert)) => Transport::tls_with_default_config(),
+ (MqttTransport::Tcp, Some(MqttTlsVerificationMode::SkipServerCertVerification)) => {
+ Transport::tls_with_config(insecure_tls_configuration())
+ }
+ (MqttTransport::WebSocket, None) => Transport::ws(),
+ (MqttTransport::WebSocket, Some(MqttTlsVerificationMode::VerifyServerCert)) => {
+ Transport::wss_with_default_config()
+ }
+ (MqttTransport::WebSocket, Some(MqttTlsVerificationMode::SkipServerCertVerification)) => {
+ Transport::wss_with_config(insecure_tls_configuration())
+ }
+ }
+}
+
+fn insecure_tls_configuration() -> TlsConfiguration {
+ let provider = Arc::new(rustls::crypto::ring::default_provider());
+ let config = rustls::ClientConfig::builder()
+ .dangerous()
+ .with_custom_certificate_verifier(Arc::new(NoCertificateVerification { provider }))
+ .with_no_client_auth();
+ TlsConfiguration::from(config)
+}
+
+#[derive(Debug)]
+struct NoCertificateVerification {
+ provider: Arc<CryptoProvider>,
+}
+
+impl ServerCertVerifier for NoCertificateVerification {
+ fn verify_server_cert(
+ &self,
+ _end_entity: &CertificateDer<'_>,
+ _intermediates: &[CertificateDer<'_>],
+ _server_name: &ServerName<'_>,
+ _ocsp_response: &[u8],
+ _now: UnixTime,
+ ) -> Result<ServerCertVerified, rustls::Error> {
+ Ok(ServerCertVerified::assertion())
+ }
+
+ fn verify_tls12_signature(
+ &self,
+ message: &[u8],
+ cert: &CertificateDer<'_>,
+ dss: &rustls::DigitallySignedStruct,
+ ) -> Result<HandshakeSignatureValid, rustls::Error> {
+ verify_tls12_signature(message, cert, dss, &self.provider.signature_verification_algorithms)
+ }
+
+ fn verify_tls13_signature(
+ &self,
+ message: &[u8],
+ cert: &CertificateDer<'_>,
+ dss: &rustls::DigitallySignedStruct,
+ ) -> Result<HandshakeSignatureValid, rustls::Error> {
+ verify_tls13_signature(message, cert, dss, &self.provider.signature_verification_algorithms)
+ }
+
+ fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
+ self.provider.signature_verification_algorithms.supported_schemes()
+ }
+}
+
+fn apply_credentials_v4(options: &mut MqttOptionsV4, auth: &MqttAuth) {
+ if let MqttAuth::Password { username, password } = auth {
+ options.set_credentials(username, password);
+ }
+}
+
+fn apply_credentials_v5(options: &mut MqttOptionsV5, auth: &MqttAuth) {
+ if let MqttAuth::Password { username, password } = auth {
+ options.set_credentials(username, password);
+ }
+}
+
+fn mqtt_qos_from_v4(qos: QoSV4) -> MqttQoS {
+ match qos {
+ QoSV4::AtMostOnce => MqttQoS::AtMostOnce,
+ QoSV4::AtLeastOnce => MqttQoS::AtLeastOnce,
+ QoSV4::ExactlyOnce => MqttQoS::ExactlyOnce,
+ }
+}
+
+fn mqtt_qos_from_v5(qos: QoSV5) -> MqttQoS {
+ match qos {
+ QoSV5::AtMostOnce => MqttQoS::AtMostOnce,
+ QoSV5::AtLeastOnce => MqttQoS::AtLeastOnce,
+ QoSV5::ExactlyOnce => MqttQoS::ExactlyOnce,
+ }
+}
+
+fn mqtt_publish_packet_size(
+ topic: &str,
+ payload_len: usize,
+ qos: MqttQoS,
+ protocol: super::types::MqttProtocolVersion,
+) -> usize {
+ let variable_header = 2 + topic.len() + usize::from(qos != MqttQoS::AtMostOnce) * 2;
+ let properties = usize::from(matches!(protocol, super::types::MqttProtocolVersion::V5));
+ let remaining_len = variable_header + properties + payload_len;
+ 1 + mqtt_remaining_length_bytes(remaining_len) + remaining_len
+}
+
+fn mqtt_remaining_length_bytes(mut value: usize) -> usize {
+ let mut bytes = 1;
+ while value >= 128 {
+ value /= 128;
+ bytes += 1;
+ }
+ bytes
+}
+
+fn mqtt_qos(qos: MqttQoS) -> QoSV4 {
+ match qos {
+ MqttQoS::AtMostOnce => QoSV4::AtMostOnce,
+ MqttQoS::AtLeastOnce => QoSV4::AtLeastOnce,
+ MqttQoS::ExactlyOnce => QoSV4::ExactlyOnce,
+ }
+}
+
+fn qos_to_u8(qos: QoSV4) -> u8 {
+ match qos {
+ QoSV4::AtMostOnce => 0,
+ QoSV4::AtLeastOnce => 1,
+ QoSV4::ExactlyOnce => 2,
+ }
+}
+
+fn qos_v5_to_u8(qos: QoSV5) -> u8 {
+ match qos {
+ QoSV5::AtMostOnce => 0,
+ QoSV5::AtLeastOnce => 1,
+ QoSV5::ExactlyOnce => 2,
+ }
+}
+
+fn qos_v4_to_v5(qos: QoSV4) -> QoSV5 {
+ match qos {
+ QoSV4::AtMostOnce => QoSV5::AtMostOnce,
+ QoSV4::AtLeastOnce => QoSV5::AtLeastOnce,
+ QoSV4::ExactlyOnce => QoSV5::ExactlyOnce,
+ }
+}
+
+fn decode_utf8_lossy(bytes: &[u8]) -> String {
+ String::from_utf8(bytes.to_vec()).unwrap_or_else(|_| String::from_utf8_lossy(bytes).into_owned())
+}
+
+async fn upsert_subscription(subscriptions: &RwLock<Vec<(String, MqttQoS)>>, topic: &str, qos: MqttQoS) {
+ let mut subscriptions = subscriptions.write().await;
+ if let Some((_, current_qos)) = subscriptions.iter_mut().find(|(current, _)| current == topic) {
+ *current_qos = qos;
+ } else {
+ subscriptions.push((topic.to_string(), qos));
+ }
+}
+
+async fn remove_subscription(subscriptions: &RwLock<Vec<(String, MqttQoS)>>, topic: &str) {
+ subscriptions.write().await.retain(|(current, _)| current != topic);
+}
+
+fn validate_topic_length_and_content(topic: &str, action: &str) -> Result<(), String> {
+ if topic.is_empty() {
+ return Err(format!("{action}主题不能为空,请输入有效的 MQTT 主题"));
+ }
+ if topic.contains('\0') {
+ return Err(format!("{action}主题不能包含空字符"));
+ }
+ if topic.len() > u16::MAX as usize {
+ return Err(format!("{action}主题过长,UTF-8 编码后不能超过 65535 字节"));
+ }
+ Ok(())
+}
+
+fn validate_topic_filter(topic: &str) -> Result<(), String> {
+ validate_topic_length_and_content(topic, "订阅")?;
+ if !valid_filter(topic) {
+ return Err("订阅主题格式无效:通配符 + 必须独占一个层级,通配符 # 必须独占最后一个层级".to_string());
+ }
+ Ok(())
+}
+
+fn validate_publish_topic(topic: &str) -> Result<(), String> {
+ validate_topic_length_and_content(topic, "发布")?;
+ if !valid_topic(topic) {
+ return Err("发布主题不能包含通配符 # 或 +,请填写一个具体主题".to_string());
+ }
+ Ok(())
+}
+
+/// 将 topic 路径插入到树结构中
+fn insert_topic_into_tree(root: &mut MqttTopicNode, topic: &str) {
+ let (path_prefix, visible_topic) = topic.strip_prefix('/').map_or(("", topic), |path| ("/", path));
+ let segments: Vec<&str> = visible_topic.split('/').collect();
+ let mut current = root;
+
+ for (i, segment) in segments.iter().enumerate() {
+ let full_path = format!("{path_prefix}{}", segments[..=i].join("/"));
+ let is_leaf = i == segments.len() - 1;
+
+ let child_idx = match current.children.iter().position(|child| child.name == *segment) {
+ Some(index) => index,
+ None => {
+ current.children.push(MqttTopicNode {
+ name: segment.to_string(),
+ full_path,
+ children: Vec::new(),
+ message_count: None,
+ is_leaf,
+ });
+ current.children.len() - 1
+ }
+ };
+ if is_leaf {
+ current.children[child_idx].is_leaf = true;
+ }
+ current = &mut current.children[child_idx];
+ }
+}
+
+fn sort_topic_tree(node: &mut MqttTopicNode) {
+ node.children.sort_by(|left, right| left.name.cmp(&right.name));
+ for child in &mut node.children {
+ sort_topic_tree(child);
+ }
+}
+
+/// 简单 topic 通配符匹配(支持 + 和 #)
+fn topic_matches_filter(topic: &str, filter: &str) -> bool {
+ let topic_segments: Vec<&str> = topic.split('/').collect();
+ let filter_segments: Vec<&str> = filter.split('/').collect();
+
+ let mut ti = 0;
+ let mut fi = 0;
+
+ while fi < filter_segments.len() {
+ match filter_segments[fi] {
+ "#" => return true,
+ "+" => {
+ if ti >= topic_segments.len() {
+ return false;
+ }
+ ti += 1;
+ fi += 1;
+ }
+ segment => {
+ if ti >= topic_segments.len() || topic_segments[ti] != segment {
+ return false;
+ }
+ ti += 1;
+ fi += 1;
+ }
+ }
+ }
+
+ ti == topic_segments.len()
+}
+
+#[cfg(test)]
+mod tests {
+ use std::time::Duration;
+
+ use super::*;
+
+ #[test]
+ fn topic_tree_from_subscriptions() {
+ let mut root = MqttTopicNode {
+ name: "root".to_string(),
+ full_path: String::new(),
+ children: Vec::new(),
+ message_count: None,
+ is_leaf: false,
+ };
+ insert_topic_into_tree(&mut root, "sensors/building1/temperature");
+ insert_topic_into_tree(&mut root, "sensors/building1/humidity");
+ insert_topic_into_tree(&mut root, "sensors/building2/temperature");
+
+ assert_eq!(root.children.len(), 1);
+ assert_eq!(root.children[0].children.len(), 2);
+ assert_eq!(root.children[0].children[0].children.len(), 2);
+ }
+
+ #[test]
+ fn topic_tree_hides_only_the_leading_separator() {
+ let mut root = MqttTopicNode {
+ name: "root".to_string(),
+ full_path: String::new(),
+ children: Vec::new(),
+ message_count: None,
+ is_leaf: false,
+ };
+ insert_topic_into_tree(&mut root, "/device/ctrl/1219/1219000213/#");
+
+ assert_eq!(root.children.len(), 1);
+ assert_eq!(root.children[0].name, "device");
+ assert_eq!(root.children[0].full_path, "/device");
+ assert_eq!(root.children[0].children[0].full_path, "/device/ctrl");
+ assert_eq!(
+ root.children[0].children[0].children[0].children[0].children[0].full_path,
+ "/device/ctrl/1219/1219000213/#"
+ );
+ }
+
+ #[test]
+ fn validates_publish_topics_and_subscription_filters() {
+ assert!(validate_publish_topic("/device/ctrl/1219000213/set").is_ok());
+ assert_eq!(
+ validate_publish_topic("/device/ctrl/1219000213/#").unwrap_err(),
+ "发布主题不能包含通配符 # 或 +,请填写一个具体主题"
+ );
+ assert!(validate_topic_filter("/device/ctrl/1219000213/#").is_ok());
+ assert!(validate_topic_filter("/device/+/status").is_ok());
+ assert!(validate_topic_filter("/device/a#").is_err());
+ assert!(validate_topic_filter("/device/#/status").is_err());
+ }
+
+ #[test]
+ fn topic_filter_wildcards() {
+ assert!(topic_matches_filter("sensors/b1/temp", "sensors/+/temp"));
+ assert!(topic_matches_filter("a/b/c/d", "a/#"));
+ assert!(!topic_matches_filter("sensors/b1/temp", "actuators/+/temp"));
+ assert!(topic_matches_filter("a/b", "a/b"));
+ }
+
+ #[test]
+ fn connect_plan_uses_ws_path_and_insecure_tls_transport() {
+ let config = MqttConnectionConfig {
+ host: "broker.example.com".to_string(),
+ port: 8084,
+ transport: MqttTransport::WebSocket,
+ tls: true,
+ tls_skip_verify: true,
+ ws_path: Some("/custom/mqtt".to_string()),
+ ..Default::default()
+ };
+
+ let plan = build_connect_plan(&config);
+ assert_eq!(plan.broker_addr, "wss://broker.example.com:8084/custom/mqtt");
+ assert_eq!(plan.backend, MqttBackendKind::V5);
+ match plan.transport {
+ Transport::Wss(TlsConfiguration::Rustls(_)) => {}
+ _ => panic!("tlsSkipVerify 应使用显式 Rustls 自定义校验器"),
+ }
+ }
+
+ #[tokio::test]
+ async fn connect_applies_selected_protocol_versions() {
+ assert_v4_connect_protocol(crate::mqtt::types::MqttProtocolVersion::V3, Protocol::V3).await;
+ assert_v4_connect_protocol(crate::mqtt::types::MqttProtocolVersion::V4, Protocol::V4).await;
+ assert_v5_connect_protocol().await;
+ }
+
+ #[tokio::test]
+ async fn service_test_connection_disconnects_each_session() {
+ let (port, broker) = spawn_v5_mock_broker(2).await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V5);
+
+ let first = crate::mqtt::service::test_connection(&config).await.unwrap();
+ assert_eq!(first.protocol_version, "MQTT 5.0");
+ let second = crate::mqtt::service::test_connection(&config).await.unwrap();
+ assert_eq!(second.protocol_version, "MQTT 5.0");
+
+ await_broker(broker).await;
+ }
+
+ #[tokio::test]
+ async fn failed_test_connection_closes_session_and_event_loop() {
+ let (port, broker) = spawn_stalled_v5_mock_broker().await;
+ let mut config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V5);
+ config.connect_timeout_secs = 1;
+
+ let error = crate::mqtt::service::test_connection(&config).await.unwrap_err();
+ assert!(error.contains("连接超时"), "unexpected error: {error}");
+
+ await_broker(broker).await;
+ }
+
+ #[tokio::test]
+ async fn dropping_client_closes_session_and_event_loop() {
+ let (port, broker) = spawn_v5_mock_broker_expecting_eof().await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V5);
+
+ let client = MqttClient::connect(config).await.unwrap();
+ drop(client);
+
+ await_broker(broker).await;
+ }
+
+ #[tokio::test]
+ async fn reconnect_updates_connection_state_and_restores_confirmed_subscriptions() {
+ let (port, disconnect_broker, restored, broker) = spawn_v4_reconnecting_broker().await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V4);
+ let client = MqttClient::connect(config).await.unwrap();
+
+ client.subscribe("device/reconnect/#", MqttQoS::AtMostOnce).await.unwrap();
+ assert!(client.broker_info().await.connected);
+ assert_eq!(client.list_topics().await.len(), 1);
+
+ disconnect_broker.send(()).unwrap();
+ wait_for_connection_state(&client, false).await;
+ assert!(client.list_topics().await.is_empty());
+
+ restored.await.unwrap();
+ wait_for_connection_state(&client, true).await;
+ wait_for_topic_count(&client, 1).await;
+
+ client.disconnect().await;
+ await_broker(broker).await;
+ }
+
+ #[tokio::test]
+ async fn subscription_state_changes_only_after_suback_and_unsuback() {
+ let (port, subscribe_seen, allow_suback, unsubscribe_seen, allow_unsuback, broker) =
+ spawn_v4_delayed_ack_broker().await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V4);
+ let client = MqttClient::connect(config).await.unwrap();
+
+ let subscribing_client = Arc::clone(&client);
+ let subscribe =
+ tokio::spawn(async move { subscribing_client.subscribe("device/ack/#", MqttQoS::AtLeastOnce).await });
+ subscribe_seen.await.unwrap();
+ assert!(client.list_topics().await.is_empty());
+ allow_suback.send(()).unwrap();
+ subscribe.await.unwrap().unwrap();
+ assert_eq!(client.list_topics().await, vec![("device/ack/#".to_string(), MqttQoS::AtLeastOnce)]);
+
+ let unsubscribing_client = Arc::clone(&client);
+ let unsubscribe = tokio::spawn(async move { unsubscribing_client.unsubscribe("device/ack/#").await });
+ unsubscribe_seen.await.unwrap();
+ assert_eq!(client.list_topics().await.len(), 1);
+ allow_unsuback.send(()).unwrap();
+ unsubscribe.await.unwrap().unwrap();
+ assert!(client.list_topics().await.is_empty());
+
+ client.disconnect().await;
+ await_broker(broker).await;
+ }
+
+ #[tokio::test]
+ async fn publish_larger_than_rumqttc_default_waits_until_written() {
+ let payload = "x".repeat(12 * 1024);
+ let (port, publish_seen, broker) = spawn_v4_publish_broker(payload.len(), false).await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V4);
+ let client = MqttClient::connect(config).await.unwrap();
+ let request = publish_request("device/large", &payload, MqttQoS::AtMostOnce);
+
+ client.publish(&request).await.unwrap();
+ publish_seen.await.unwrap();
+ assert_eq!(client.get_messages(None, 10).await.len(), 1);
+
+ client.disconnect().await;
+ await_broker(broker).await;
+ }
+
+ #[tokio::test]
+ async fn qos1_publish_completes_only_after_puback() {
+ let payload = "confirmed";
+ let (port, publish_seen, allow_puback, broker) = spawn_v4_delayed_puback_broker().await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V4);
+ let client = MqttClient::connect(config).await.unwrap();
+ let request = publish_request("device/confirmed", payload, MqttQoS::AtLeastOnce);
+
+ let publishing_client = Arc::clone(&client);
+ let publish = tokio::spawn(async move { publishing_client.publish(&request).await });
+ publish_seen.await.unwrap();
+ assert!(!publish.is_finished());
+ assert!(client.get_messages(None, 10).await.is_empty());
+ allow_puback.send(()).unwrap();
+ publish.await.unwrap().unwrap();
+ assert_eq!(client.get_messages(None, 10).await.len(), 1);
+
+ client.disconnect().await;
+ await_broker(broker).await;
+ }
+
+ #[tokio::test]
+ async fn broker_packet_limit_error_is_returned_and_not_recorded() {
+ let (port, broker) = spawn_v5_small_packet_limit_broker().await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V5);
+ let client = MqttClient::connect(config).await.unwrap();
+ let request = publish_request("device/too-large", &"x".repeat(128), MqttQoS::AtMostOnce);
+
+ let error = client.publish(&request).await.unwrap_err();
+ assert!(error.contains("maximum packet size"), "unexpected error: {error}");
+ assert!(client.get_messages(None, 10).await.is_empty());
+
+ drop(client);
+ await_broker(broker).await;
+ }
+ #[tokio::test]
+ async fn publish_connection_error_is_returned_and_not_recorded() {
+ let (port, broker) = spawn_v4_publish_disconnect_broker().await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V4);
+ let client = MqttClient::connect(config).await.unwrap();
+ let request = publish_request("device/failure", "not-confirmed", MqttQoS::AtLeastOnce);
+
+ let error = client.publish(&request).await.unwrap_err();
+ assert!(error.contains("发布消息未完成"), "unexpected error: {error}");
+ assert!(client.get_messages(None, 10).await.is_empty());
+
+ drop(client);
+ await_broker(broker).await;
+ }
+ #[tokio::test]
+ async fn suback_records_broker_granted_qos() {
+ let (port, broker) = spawn_v4_granted_subscription_broker(QoSV4::AtMostOnce).await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V4);
+ let client = MqttClient::connect(config).await.unwrap();
+
+ client.subscribe("device/downgraded/#", MqttQoS::ExactlyOnce).await.unwrap();
+ assert_eq!(client.list_topics().await, vec![("device/downgraded/#".to_string(), MqttQoS::AtMostOnce)]);
+
+ client.disconnect().await;
+ await_broker(broker).await;
+ }
+
+ #[tokio::test]
+ async fn retained_dedup_resets_for_resubscribe_and_clear() {
+ let (port, send_after_clear, broker) = spawn_v4_retained_lifecycle_broker().await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V4);
+ let client = MqttClient::connect(config).await.unwrap();
+
+ client.subscribe("retained/#", MqttQoS::AtMostOnce).await.unwrap();
+ wait_for_message_count(&client, 2).await;
+ let initial = client.get_messages(None, 10).await;
+ assert_eq!(initial.iter().filter(|message| message.payload_text.as_deref() == Some("v1")).count(), 1);
+ assert!(initial.iter().any(|message| message.payload_text.as_deref() == Some("v2")));
+
+ client.unsubscribe("retained/#").await.unwrap();
+ client.subscribe("retained/#", MqttQoS::AtMostOnce).await.unwrap();
+ wait_for_message_count(&client, 3).await;
+ assert_eq!(
+ client
+ .get_messages(None, 10)
+ .await
+ .iter()
+ .filter(|message| message.payload_text.as_deref() == Some("v1"))
+ .count(),
+ 2
+ );
+
+ client.clear_messages().await;
+ send_after_clear.send(()).unwrap();
+ wait_for_message_count(&client, 1).await;
+ assert_eq!(client.get_messages(None, 10).await[0].payload_text.as_deref(), Some("v1"));
+
+ client.disconnect().await;
+ await_broker(broker).await;
+ }
+
+ #[test]
+ fn reconnect_replayed_publish_cannot_complete_a_new_request() {
+ let mut tracker = PublishRequestTracker::default();
+ let old_message = test_message("old");
+ let (old_completion, mut old_result) = oneshot::channel();
+ tracker.queue(QoSV4::AtLeastOnce, old_message, old_completion);
+ tracker.mark_outgoing(7);
+
+ let mut failed = tracker.take_for_connection_loss();
+ failed[0].fail("连接中断");
+ assert_eq!(old_result.try_recv().unwrap(), Err("连接中断".to_string()));
+
+ let (new_completion, mut new_result) = oneshot::channel();
+ tracker.queue(QoSV4::AtLeastOnce, test_message("new"), new_completion);
+ assert!(tracker.mark_outgoing(7).is_none());
+ assert!(tracker.take_acknowledged(7).is_none());
+ assert!(matches!(new_result.try_recv(), Err(tokio::sync::oneshot::error::TryRecvError::Empty)));
+
+ tracker.mark_outgoing(8);
+ let pending = tracker.take_acknowledged(8).unwrap();
+ assert_eq!(pending.message.topic, "new");
+ }
+
+ fn test_message(topic: &str) -> MqttMessage {
+ MqttMessage {
+ topic: topic.to_string(),
+ payload_base64: String::new(),
+ payload_text: None,
+ qos: 1,
+ retain: false,
+ received_at_ms: 0,
+ direction: MqttMessageDirection::Sent,
+ }
+ }
+ #[test]
+ fn retained_dedup_is_bounded() {
+ let mut seen = RetainedDedup::new(2);
+ assert!(seen.insert("topic/1", b"payload"));
+ assert!(seen.insert("topic/2", b"payload"));
+ assert!(seen.insert("topic/3", b"payload"));
+ assert_eq!(seen.entries.len(), 2);
+ assert!(seen.insert("topic/1", b"payload"));
+ }
+ #[tokio::test]
+ async fn rejected_suback_does_not_record_subscription() {
+ let (port, broker) = spawn_v4_rejecting_subscription_broker().await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V4);
+ let client = MqttClient::connect(config).await.unwrap();
+
+ let error = client.subscribe("device/rejected/#", MqttQoS::AtMostOnce).await.unwrap_err();
+ assert!(error.contains("拒绝"), "unexpected error: {error}");
+ assert!(client.list_topics().await.is_empty());
+
+ client.disconnect().await;
+ await_broker(broker).await;
+ }
+
+ async fn assert_v4_connect_protocol(protocol_version: crate::mqtt::types::MqttProtocolVersion, expected: Protocol) {
+ let (port, broker) = spawn_v4_mock_broker(expected, 1).await;
+ let config = mqtt_config(port, protocol_version);
+ let client = MqttClient::connect(config).await.unwrap();
+ client.disconnect().await;
+ await_broker(broker).await;
+ }
+
+ async fn assert_v5_connect_protocol() {
+ let (port, broker) = spawn_v5_mock_broker(1).await;
+ let config = mqtt_config(port, crate::mqtt::types::MqttProtocolVersion::V5);
+ let client = MqttClient::connect(config).await.unwrap();
+ client.disconnect().await;
+ await_broker(broker).await;
+ }
+
+ fn mqtt_config(port: u16, protocol_version: crate::mqtt::types::MqttProtocolVersion) -> MqttConnectionConfig {
+ MqttConnectionConfig {
+ host: "127.0.0.1".to_string(),
+ port,
+ client_id: format!("dbx-test-{}", uuid::Uuid::new_v4()),
+ protocol_version,
+ connect_timeout_secs: 5,
+ ..Default::default()
+ }
+ }
+
+ async fn spawn_v4_mock_broker(
+ expected_protocol: Protocol,
+ expected_connections: usize,
+ ) -> (u16, tokio::task::JoinHandle<()>) {
+ use bytes::BytesMut;
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let handle = tokio::spawn(async move {
+ for _ in 0..expected_connections {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ let frame = read_mqtt_frame(&mut stream).await.unwrap();
+ let mut bytes = BytesMut::from(&frame[..]);
+ let packet = rumqttc::mqttbytes::v4::read(&mut bytes, 1024 * 1024).unwrap();
+ match packet {
+ rumqttc::mqttbytes::v4::Packet::Connect(connect) => assert_eq!(connect.protocol, expected_protocol),
+ other => panic!("unexpected packet: {other:?}"),
+ }
+
+ stream.write_all(&[0x20, 0x02, 0x00, 0x00]).await.unwrap();
+ let frame = read_mqtt_frame(&mut stream).await.unwrap();
+ let mut bytes = BytesMut::from(&frame[..]);
+ let packet = rumqttc::mqttbytes::v4::read(&mut bytes, 1024 * 1024).unwrap();
+ assert!(matches!(packet, rumqttc::mqttbytes::v4::Packet::Disconnect));
+ }
+ });
+ (port, handle)
+ }
+
+ async fn spawn_v4_reconnecting_broker(
+ ) -> (u16, oneshot::Sender<()>, oneshot::Receiver<()>, tokio::task::JoinHandle<()>) {
+ use bytes::BytesMut;
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let (disconnect_tx, disconnect_rx) = oneshot::channel();
+ let (restored_tx, restored_rx) = oneshot::channel();
+ let handle = tokio::spawn(async move {
+ let (mut first, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut first).await.unwrap();
+ first.write_all(&[0x20, 0x02, 0x00, 0x00]).await.unwrap();
+ acknowledge_v4_subscribe(&mut first, rumqttc::SubscribeReasonCode::Success(QoSV4::AtMostOnce)).await;
+ disconnect_rx.await.unwrap();
+ drop(first);
+
+ let (mut second, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut second).await.unwrap();
+ second.write_all(&[0x20, 0x02, 0x00, 0x00]).await.unwrap();
+ acknowledge_v4_subscribe(&mut second, rumqttc::SubscribeReasonCode::Success(QoSV4::AtMostOnce)).await;
+ restored_tx.send(()).unwrap();
+
+ let frame = read_mqtt_frame(&mut second).await.unwrap();
+ let mut bytes = BytesMut::from(&frame[..]);
+ let packet = rumqttc::mqttbytes::v4::read(&mut bytes, 1024 * 1024).unwrap();
+ assert!(matches!(packet, rumqttc::mqttbytes::v4::Packet::Disconnect));
+ });
+ (port, disconnect_tx, restored_rx, handle)
+ }
+
+ async fn spawn_v4_delayed_ack_broker() -> (
+ u16,
+ oneshot::Receiver<()>,
+ oneshot::Sender<()>,
+ oneshot::Receiver<()>,
+ oneshot::Sender<()>,
+ tokio::task::JoinHandle<()>,
+ ) {
+ use bytes::BytesMut;
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let (subscribe_seen_tx, subscribe_seen_rx) = oneshot::channel();
+ let (allow_suback_tx, allow_suback_rx) = oneshot::channel();
+ let (unsubscribe_seen_tx, unsubscribe_seen_rx) = oneshot::channel();
+ let (allow_unsuback_tx, allow_unsuback_rx) = oneshot::channel();
+ let handle = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut stream).await.unwrap();
+ stream.write_all(&[0x20, 0x02, 0x00, 0x00]).await.unwrap();
+
+ let subscribe_pkid = read_v4_subscribe_pkid(&mut stream).await;
+ subscribe_seen_tx.send(()).unwrap();
+ allow_suback_rx.await.unwrap();
+ write_v4_suback(&mut stream, subscribe_pkid, rumqttc::SubscribeReasonCode::Success(QoSV4::AtLeastOnce))
+ .await;
+
+ let frame = read_mqtt_frame(&mut stream).await.unwrap();
+ let mut bytes = BytesMut::from(&frame[..]);
+ let packet = rumqttc::mqttbytes::v4::read(&mut bytes, 1024 * 1024).unwrap();
+ let unsubscribe_pkid = match packet {
+ rumqttc::mqttbytes::v4::Packet::Unsubscribe(unsubscribe) => unsubscribe.pkid,
+ other => panic!("unexpected packet: {other:?}"),
+ };
+ unsubscribe_seen_tx.send(()).unwrap();
+ allow_unsuback_rx.await.unwrap();
+ let mut ack = BytesMut::new();
+ rumqttc::UnsubAck::new(unsubscribe_pkid).write(&mut ack).unwrap();
+ stream.write_all(&ack).await.unwrap();
+
+ let frame = read_mqtt_frame(&mut stream).await.unwrap();
+ assert_eq!(frame, vec![0xe0, 0x00]);
+ });
+ (port, subscribe_seen_rx, allow_suback_tx, unsubscribe_seen_rx, allow_unsuback_tx, handle)
+ }
+
+ fn publish_request(topic: &str, payload: &str, qos: MqttQoS) -> MqttPublishRequest {
+ MqttPublishRequest {
+ topic: topic.to_string(),
+ payload_base64: String::new(),
+ payload_text: Some(payload.to_string()),
+ qos,
+ retain: false,
+ }
+ }
+
+ async fn spawn_v4_publish_broker(
+ expected_payload_len: usize,
+ acknowledge: bool,
+ ) -> (u16, oneshot::Receiver<()>, tokio::task::JoinHandle<()>) {
+ use bytes::BytesMut;
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let (seen_tx, seen_rx) = oneshot::channel();
+ let handle = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut stream).await.unwrap();
+ stream.write_all(&[0x20, 0x02, 0x00, 0x00]).await.unwrap();
+ let publish = read_v4_publish(&mut stream).await;
+ assert_eq!(publish.payload.len(), expected_payload_len);
+ seen_tx.send(()).unwrap();
+ if acknowledge {
+ let mut ack = BytesMut::new();
+ rumqttc::PubAck::new(publish.pkid).write(&mut ack).unwrap();
+ stream.write_all(&ack).await.unwrap();
+ }
+ assert_eq!(read_mqtt_frame(&mut stream).await.unwrap(), vec![0xe0, 0x00]);
+ });
+ (port, seen_rx, handle)
+ }
+
+ async fn spawn_v4_delayed_puback_broker(
+ ) -> (u16, oneshot::Receiver<()>, oneshot::Sender<()>, tokio::task::JoinHandle<()>) {
+ use bytes::BytesMut;
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let (seen_tx, seen_rx) = oneshot::channel();
+ let (allow_tx, allow_rx) = oneshot::channel();
+ let handle = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut stream).await.unwrap();
+ stream.write_all(&[0x20, 0x02, 0x00, 0x00]).await.unwrap();
+ let publish = read_v4_publish(&mut stream).await;
+ assert_eq!(publish.qos, QoSV4::AtLeastOnce);
+ seen_tx.send(()).unwrap();
+ allow_rx.await.unwrap();
+ let mut ack = BytesMut::new();
+ rumqttc::PubAck::new(publish.pkid).write(&mut ack).unwrap();
+ stream.write_all(&ack).await.unwrap();
+ assert_eq!(read_mqtt_frame(&mut stream).await.unwrap(), vec![0xe0, 0x00]);
+ });
+ (port, seen_rx, allow_tx, handle)
+ }
+
+ async fn spawn_v5_small_packet_limit_broker() -> (u16, tokio::task::JoinHandle<()>) {
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let handle = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut stream).await.unwrap();
+ // CONNACK 的 Maximum Packet Size 属性将 broker 接收上限设为 32 字节。
+ stream.write_all(&[0x20, 0x08, 0x00, 0x00, 0x05, 0x27, 0x00, 0x00, 0x00, 0x20]).await.unwrap();
+ expect_stream_closed(&mut stream).await;
+ });
+ (port, handle)
+ }
+ async fn spawn_v4_publish_disconnect_broker() -> (u16, tokio::task::JoinHandle<()>) {
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let handle = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut stream).await.unwrap();
+ stream.write_all(&[0x20, 0x02, 0x00, 0x00]).await.unwrap();
+ let publish = read_v4_publish(&mut stream).await;
+ assert_eq!(publish.qos, QoSV4::AtLeastOnce);
+ });
+ (port, handle)
+ }
+ async fn spawn_v4_granted_subscription_broker(granted: QoSV4) -> (u16, tokio::task::JoinHandle<()>) {
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let handle = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut stream).await.unwrap();
+ stream.write_all(&[0x20, 0x02, 0x00, 0x00]).await.unwrap();
+ acknowledge_v4_subscribe(&mut stream, rumqttc::SubscribeReasonCode::Success(granted)).await;
+ assert_eq!(read_mqtt_frame(&mut stream).await.unwrap(), vec![0xe0, 0x00]);
+ });
+ (port, handle)
+ }
+
+ async fn spawn_v4_retained_lifecycle_broker() -> (u16, oneshot::Sender<()>, tokio::task::JoinHandle<()>) {
+ use bytes::BytesMut;
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let (after_clear_tx, after_clear_rx) = oneshot::channel();
+ let handle = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut stream).await.unwrap();
+ stream.write_all(&[0x20, 0x02, 0x00, 0x00]).await.unwrap();
+
+ acknowledge_v4_subscribe(&mut stream, rumqttc::SubscribeReasonCode::Success(QoSV4::AtMostOnce)).await;
+ write_v4_retained(&mut stream, "retained/value", b"v1").await;
+ write_v4_retained(&mut stream, "retained/value", b"v1").await;
+ write_v4_retained(&mut stream, "retained/value", b"v2").await;
+
+ let frame = read_mqtt_frame(&mut stream).await.unwrap();
+ let mut bytes = BytesMut::from(&frame[..]);
+ let packet = rumqttc::mqttbytes::v4::read(&mut bytes, 1024 * 1024).unwrap();
+ let unsubscribe_pkid = match packet {
+ rumqttc::mqttbytes::v4::Packet::Unsubscribe(unsubscribe) => unsubscribe.pkid,
+ other => panic!("unexpected packet: {other:?}"),
+ };
+ let mut ack = BytesMut::new();
+ rumqttc::UnsubAck::new(unsubscribe_pkid).write(&mut ack).unwrap();
+ stream.write_all(&ack).await.unwrap();
+
+ acknowledge_v4_subscribe(&mut stream, rumqttc::SubscribeReasonCode::Success(QoSV4::AtMostOnce)).await;
+ write_v4_retained(&mut stream, "retained/value", b"v1").await;
+ after_clear_rx.await.unwrap();
+ write_v4_retained(&mut stream, "retained/value", b"v1").await;
+ assert_eq!(read_mqtt_frame(&mut stream).await.unwrap(), vec![0xe0, 0x00]);
+ });
+ (port, after_clear_tx, handle)
+ }
+
+ async fn read_v4_publish(stream: &mut tokio::net::TcpStream) -> rumqttc::Publish {
+ use bytes::BytesMut;
+
+ let frame = read_mqtt_frame(stream).await.unwrap();
+ let mut bytes = BytesMut::from(&frame[..]);
+ match rumqttc::mqttbytes::v4::read(&mut bytes, 32 * 1024 * 1024).unwrap() {
+ rumqttc::mqttbytes::v4::Packet::Publish(publish) => publish,
+ other => panic!("unexpected packet: {other:?}"),
+ }
+ }
+
+ async fn write_v4_retained(stream: &mut tokio::net::TcpStream, topic: &str, payload: &[u8]) {
+ use bytes::BytesMut;
+ use tokio::io::AsyncWriteExt;
+
+ let mut publish = rumqttc::Publish::new(topic, QoSV4::AtMostOnce, payload);
+ publish.retain = true;
+ let mut bytes = BytesMut::new();
+ publish.write(&mut bytes).unwrap();
+ stream.write_all(&bytes).await.unwrap();
+ }
+ async fn spawn_v4_rejecting_subscription_broker() -> (u16, tokio::task::JoinHandle<()>) {
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let handle = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut stream).await.unwrap();
+ stream.write_all(&[0x20, 0x02, 0x00, 0x00]).await.unwrap();
+ acknowledge_v4_subscribe(&mut stream, rumqttc::SubscribeReasonCode::Failure).await;
+ let frame = read_mqtt_frame(&mut stream).await.unwrap();
+ assert_eq!(frame, vec![0xe0, 0x00]);
+ });
+ (port, handle)
+ }
+
+ async fn acknowledge_v4_subscribe(stream: &mut tokio::net::TcpStream, reason: rumqttc::SubscribeReasonCode) {
+ let pkid = read_v4_subscribe_pkid(stream).await;
+ write_v4_suback(stream, pkid, reason).await;
+ }
+
+ async fn read_v4_subscribe_pkid(stream: &mut tokio::net::TcpStream) -> u16 {
+ use bytes::BytesMut;
+
+ let frame = read_mqtt_frame(stream).await.unwrap();
+ let mut bytes = BytesMut::from(&frame[..]);
+ let packet = rumqttc::mqttbytes::v4::read(&mut bytes, 1024 * 1024).unwrap();
+ match packet {
+ rumqttc::mqttbytes::v4::Packet::Subscribe(subscribe) => subscribe.pkid,
+ other => panic!("unexpected packet: {other:?}"),
+ }
+ }
+
+ async fn write_v4_suback(stream: &mut tokio::net::TcpStream, pkid: u16, reason: rumqttc::SubscribeReasonCode) {
+ use bytes::BytesMut;
+ use tokio::io::AsyncWriteExt;
+
+ let mut ack = BytesMut::new();
+ rumqttc::SubAck::new(pkid, vec![reason]).write(&mut ack).unwrap();
+ stream.write_all(&ack).await.unwrap();
+ }
+
+ async fn wait_for_connection_state(client: &MqttClient, expected: bool) {
+ tokio::time::timeout(Duration::from_secs(4), async {
+ loop {
+ if client.broker_info().await.connected == expected {
+ break;
+ }
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .unwrap_or_else(|_| panic!("MQTT connected state did not become {expected}"));
+ }
+
+ async fn wait_for_message_count(client: &MqttClient, expected: usize) {
+ tokio::time::timeout(Duration::from_secs(4), async {
+ loop {
+ if client.get_messages(None, expected + 10).await.len() == expected {
+ break;
+ }
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .unwrap_or_else(|_| panic!("MQTT message count did not become {expected}"));
+ }
+ async fn wait_for_topic_count(client: &MqttClient, expected: usize) {
+ tokio::time::timeout(Duration::from_secs(4), async {
+ loop {
+ if client.list_topics().await.len() == expected {
+ break;
+ }
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .unwrap_or_else(|_| panic!("MQTT topic count did not become {expected}"));
+ }
+
+ async fn spawn_v5_mock_broker(expected_connections: usize) -> (u16, tokio::task::JoinHandle<()>) {
+ use bytes::BytesMut;
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let handle = tokio::spawn(async move {
+ for _ in 0..expected_connections {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ let frame = read_mqtt_frame(&mut stream).await.unwrap();
+ let mut bytes = BytesMut::from(&frame[..]);
+ let packet = rumqttc::v5::mqttbytes::v5::Packet::read(&mut bytes, Some(1024 * 1024)).unwrap();
+ match packet {
+ rumqttc::v5::mqttbytes::v5::Packet::Connect(connect, _, _) => {
+ assert!(connect.client_id.starts_with("dbx-test-"));
+ }
+ other => panic!("unexpected packet: {other:?}"),
+ }
+
+ let connack = rumqttc::v5::mqttbytes::v5::ConnAck {
+ session_present: false,
+ code: rumqttc::v5::mqttbytes::v5::ConnectReturnCode::Success,
+ properties: None,
+ };
+ let mut connack_bytes = BytesMut::new();
+ connack.write(&mut connack_bytes).unwrap();
+ stream.write_all(&connack_bytes).await.unwrap();
+
+ let frame = read_mqtt_frame(&mut stream).await.unwrap();
+ assert_eq!(frame, vec![0xe0, 0x00]);
+ }
+ });
+ (port, handle)
+ }
+
+ async fn spawn_stalled_v5_mock_broker() -> (u16, tokio::task::JoinHandle<()>) {
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let handle = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut stream).await.unwrap();
+ expect_stream_closed(&mut stream).await;
+ });
+ (port, handle)
+ }
+
+ async fn spawn_v5_mock_broker_expecting_eof() -> (u16, tokio::task::JoinHandle<()>) {
+ use bytes::BytesMut;
+ use tokio::io::AsyncWriteExt;
+ use tokio::net::TcpListener;
+
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let handle = tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ read_mqtt_frame(&mut stream).await.unwrap();
+
+ let connack = rumqttc::v5::mqttbytes::v5::ConnAck {
+ session_present: false,
+ code: rumqttc::v5::mqttbytes::v5::ConnectReturnCode::Success,
+ properties: None,
+ };
+ let mut connack_bytes = BytesMut::new();
+ connack.write(&mut connack_bytes).unwrap();
+ stream.write_all(&connack_bytes).await.unwrap();
+
+ expect_stream_closed(&mut stream).await;
+ });
+ (port, handle)
+ }
+
+ async fn expect_stream_closed(stream: &mut tokio::net::TcpStream) {
+ use tokio::io::AsyncReadExt;
+
+ let mut byte = [0u8; 1];
+ let bytes_read = tokio::time::timeout(Duration::from_secs(3), stream.read(&mut byte))
+ .await
+ .expect("MQTT session was not closed")
+ .expect("failed to read MQTT session state");
+ assert_eq!(bytes_read, 0, "unexpected MQTT data after client cleanup");
+ }
+
+ async fn read_mqtt_frame(stream: &mut tokio::net::TcpStream) -> std::io::Result<Vec<u8>> {
+ use tokio::io::AsyncReadExt;
+
+ let mut first = [0u8; 1];
+ stream.read_exact(&mut first).await?;
+ let mut len_bytes = Vec::new();
+ let mut remaining_len = 0usize;
+ let mut multiplier = 1usize;
+ loop {
+ let mut byte = [0u8; 1];
+ stream.read_exact(&mut byte).await?;
+ len_bytes.push(byte[0]);
+ remaining_len += ((byte[0] & 0x7f) as usize) * multiplier;
+ if (byte[0] & 0x80) == 0 {
+ break;
+ }
+ multiplier *= 128;
+ }
+
+ let mut rest = vec![0u8; remaining_len];
+ stream.read_exact(&mut rest).await?;
+ let mut frame = Vec::with_capacity(1 + len_bytes.len() + rest.len());
+ frame.push(first[0]);
+ frame.extend(len_bytes);
+ frame.extend(rest);
+ Ok(frame)
+ }
+
+ async fn await_broker(handle: tokio::task::JoinHandle<()>) {
+ tokio::time::timeout(Duration::from_secs(5), handle)
+ .await
+ .expect("mock broker timed out")
+ .expect("mock broker task failed");
+ }
+}
diff --git a/crates/dbx-core/src/mqtt/mod.rs b/crates/dbx-core/src/mqtt/mod.rs
new file mode 100644
index 0000000000..8ae7bb7047
--- /dev/null
+++ b/crates/dbx-core/src/mqtt/mod.rs
@@ -0,0 +1,22 @@
+//! MQTT 模块:MQTT broker 连接管理、消息订阅/发布。
+//!
+//! ## 架构
+//! ```text
+//! DBX 前端 (Vue)
+//! │ Tauri invoke
+//! ▼
+//! src-tauri/src/commands/mqtt_cmd.rs (Tauri command 入口)
+//! │
+//! ▼
+//! crates/dbx-core/src/mqtt/service.rs (共享核心逻辑)
+//! │
+//! ▼
+//! crates/dbx-core/src/mqtt/client.rs (rumqttc 客户端封装)
+//! │
+//! ▼
+//! MQTT Broker (EMQX / Mosquitto / HiveMQ / ...)
+//! ```
+
+pub mod client;
+pub mod service;
+pub mod types;
diff --git a/crates/dbx-core/src/mqtt/service.rs b/crates/dbx-core/src/mqtt/service.rs
new file mode 100644
index 0000000000..415bbc8bbe
--- /dev/null
+++ b/crates/dbx-core/src/mqtt/service.rs
@@ -0,0 +1,71 @@
+//! MQTT 服务层:供 Tauri command 和 Web 路由复用的核心函数。
+
+use std::sync::Arc;
+
+use super::client::MqttClient;
+use super::types::*;
+
+/// 获取或建立 MQTT 客户端连接。
+/// 将 MQTT 连接视为"获取或创建"的模式——同一 connection_id 共享同一客户端。
+pub async fn ensure_mqtt_client(
+ client: &Option<Arc<MqttClient>>,
+ config: &MqttConnectionConfig,
+) -> Result<Arc<MqttClient>, String> {
+ if let Some(ref client) = client {
+ return Ok(Arc::clone(client));
+ }
+ MqttClient::connect(config.clone()).await
+}
+
+/// 测试 MQTT 连接
+pub async fn test_connection(config: &MqttConnectionConfig) -> Result<MqttBrokerInfo, String> {
+ let client = MqttClient::connect(config.clone()).await?;
+ let info = client.broker_info().await;
+ client.disconnect().await;
+ Ok(info)
+}
+
+/// 获取 broker 信息
+pub async fn get_broker_info(client: &Arc<MqttClient>) -> Result<MqttBrokerInfo, String> {
+ Ok(client.broker_info().await)
+}
+
+/// 订阅 topic
+pub async fn subscribe(client: &Arc<MqttClient>, topic: &str, qos: MqttQoS) -> Result<(), String> {
+ client.subscribe(topic, qos).await
+}
+
+/// 取消订阅 topic
+pub async fn unsubscribe(client: &Arc<MqttClient>, topic: &str) -> Result<(), String> {
+ client.unsubscribe(topic).await
+}
+
+/// 发布消息
+pub async fn publish(client: &Arc<MqttClient>, request: &MqttPublishRequest) -> Result<(), String> {
+ client.publish(request).await
+}
+
+/// 获取已订阅的 topic 列表
+pub async fn list_topics(client: &Arc<MqttClient>) -> Result<Vec<(String, MqttQoS)>, String> {
+ Ok(client.list_topics().await)
+}
+
+/// 获取 topic 树
+pub async fn get_topic_tree(client: &Arc<MqttClient>) -> Result<MqttTopicNode, String> {
+ Ok(client.build_topic_tree().await)
+}
+
+/// 获取消息列表
+pub async fn get_messages(
+ client: &Arc<MqttClient>,
+ topic_filter: Option<&str>,
+ limit: usize,
+) -> Result<Vec<MqttMessage>, String> {
+ Ok(client.get_messages(topic_filter, limit).await)
+}
+
+/// 清空消息缓冲区
+pub async fn clear_messages(client: &Arc<MqttClient>) -> Result<(), String> {
+ client.clear_messages().await;
+ Ok(())
+}
diff --git a/crates/dbx-core/src/mqtt/types.rs b/crates/dbx-core/src/mqtt/types.rs
new file mode 100644
index 0000000000..cc4f690ad8
--- /dev/null
+++ b/crates/dbx-core/src/mqtt/types.rs
@@ -0,0 +1,363 @@
+//! MQTT broker 连接的类型定义。与前端 `apps/desktop/src/types/mqtt.ts` 保持一致。
+
+use serde::{Deserialize, Serialize};
+
+/// MQTT 协议版本
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[serde(rename_all = "lowercase")]
+pub enum MqttProtocolVersion {
+ V3,
+ V4,
+ #[default]
+ V5,
+}
+
+/// 传输层协议
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[serde(rename_all = "lowercase")]
+pub enum MqttTransport {
+ #[default]
+ Tcp,
+ #[serde(rename = "websocket")]
+ WebSocket,
+}
+
+/// 认证方式
+#[derive(Debug, Clone, Serialize, Deserialize, Default)]
+#[serde(rename_all = "camelCase", tag = "kind")]
+pub enum MqttAuth {
+ #[serde(rename = "none")]
+ #[default]
+ None,
+ #[serde(rename = "password")]
+ Password { username: String, password: String },
+ #[serde(rename = "certificate")]
+ Certificate { ca_cert_path: Option<String>, client_cert_path: Option<String>, client_key_path: Option<String> },
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub(crate) enum MqttTlsVerificationMode {
+ VerifyServerCert,
+ SkipServerCertVerification,
+}
+
+impl MqttAuth {
+ pub fn kind_str(&self) -> &'static str {
+ match self {
+ MqttAuth::None => "none",
+ MqttAuth::Password { .. } => "password",
+ MqttAuth::Certificate { .. } => "certificate",
+ }
+ }
+}
+
+/// MQTT 连接配置,存储在 `ConnectionConfig.external_config` 中。
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct MqttConnectionConfig {
+ /// Broker 地址(IP 或域名)
+ pub host: String,
+ /// Broker 端口(默认 1883,TLS 默认 8883)
+ pub port: u16,
+ /// MQTT 客户端标识符
+ pub client_id: String,
+ /// 协议版本,默认 V5
+ #[serde(default)]
+ pub protocol_version: MqttProtocolVersion,
+ /// 传输层,默认 TCP
+ #[serde(default)]
+ pub transport: MqttTransport,
+ /// 是否启用 TLS
+ #[serde(default)]
+ pub tls: bool,
+ /// 跳过 TLS 证书验证
+ #[serde(default)]
+ pub tls_skip_verify: bool,
+ /// 认证方式
+ #[serde(default)]
+ pub auth: MqttAuth,
+ /// Keep Alive 间隔(秒),默认 60
+ #[serde(default = "default_keep_alive")]
+ pub keep_alive_secs: u64,
+ /// 连接超时(秒),默认 30
+ #[serde(default = "default_connect_timeout")]
+ pub connect_timeout_secs: u64,
+ /// 单个 MQTT 报文的最大字节数,默认 16 MiB
+ #[serde(default = "default_max_packet_size")]
+ pub max_packet_size_bytes: usize,
+ /// WebSocket 路径(仅 WebSocket 传输时使用)
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub ws_path: Option<String>,
+}
+
+fn default_keep_alive() -> u64 {
+ 60
+}
+
+fn default_connect_timeout() -> u64 {
+ 30
+}
+
+fn default_max_packet_size() -> usize {
+ 16 * 1024 * 1024
+}
+
+impl Default for MqttConnectionConfig {
+ fn default() -> Self {
+ Self {
+ host: String::new(),
+ port: 1883,
+ client_id: format!("dbx-{}", uuid::Uuid::new_v4()),
+ protocol_version: MqttProtocolVersion::default(),
+ transport: MqttTransport::default(),
+ tls: false,
+ tls_skip_verify: false,
+ auth: MqttAuth::default(),
+ keep_alive_secs: default_keep_alive(),
+ connect_timeout_secs: default_connect_timeout(),
+ max_packet_size_bytes: default_max_packet_size(),
+ ws_path: None,
+ }
+ }
+}
+
+impl MqttConnectionConfig {
+ /// 从 `ConnectionConfig.external_config` 解析 MQTT 连接配置。
+ pub fn from_connection(cfg: &crate::models::connection::ConnectionConfig) -> Result<Self, String> {
+ let raw = cfg.external_config.as_ref().ok_or("MQTT 连接缺少 external_config 配置")?;
+ let parsed: MqttConnectionConfig =
+ serde_json::from_value(raw.clone()).map_err(|e| format!("MQTT 配置解析失败: {e}"))?;
+ if parsed.host.trim().is_empty() {
+ return Err("MQTT Broker 地址不能为空".to_string());
+ }
+ if parsed.client_id.trim().is_empty() {
+ return Err("MQTT Client ID 不能为空".to_string());
+ }
+ if !(1024..=268_435_455).contains(&parsed.max_packet_size_bytes) {
+ return Err("MQTT 最大报文大小必须在 1024 到 268435455 字节之间".to_string());
+ }
+ Ok(parsed)
+ }
+
+ /// 构建 MQTT broker URL
+ pub fn broker_url(&self) -> String {
+ let scheme = if self.tls { "mqtts" } else { "mqtt" };
+ match self.transport {
+ MqttTransport::Tcp => format!("{}://{}:{}", scheme, self.host, self.port),
+ MqttTransport::WebSocket => {
+ let ws_scheme = if self.tls { "wss" } else { "ws" };
+ let path = self.ws_path.as_deref().unwrap_or("/mqtt");
+ format!("{}://{}:{}{}", ws_scheme, self.host, self.port, path)
+ }
+ }
+ }
+
+ pub fn broker_addr_for_transport(&self) -> String {
+ match self.transport {
+ MqttTransport::Tcp => self.host.clone(),
+ MqttTransport::WebSocket => self.broker_url(),
+ }
+ }
+
+ pub(crate) fn tls_verification_mode(&self) -> Option<MqttTlsVerificationMode> {
+ if !self.tls {
+ None
+ } else if self.tls_skip_verify {
+ Some(MqttTlsVerificationMode::SkipServerCertVerification)
+ } else {
+ Some(MqttTlsVerificationMode::VerifyServerCert)
+ }
+ }
+}
+
+/// MQTT 消息服务质量
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[serde(rename_all = "lowercase")]
+pub enum MqttQoS {
+ #[default]
+ AtMostOnce,
+ AtLeastOnce,
+ ExactlyOnce,
+}
+
+impl MqttQoS {
+ pub fn as_u8(self) -> u8 {
+ match self {
+ MqttQoS::AtMostOnce => 0,
+ MqttQoS::AtLeastOnce => 1,
+ MqttQoS::ExactlyOnce => 2,
+ }
+ }
+
+ pub fn from_u8(value: u8) -> Self {
+ match value {
+ 0 => MqttQoS::AtMostOnce,
+ 1 => MqttQoS::AtLeastOnce,
+ 2 => MqttQoS::ExactlyOnce,
+ _ => MqttQoS::AtMostOnce,
+ }
+ }
+}
+
+/// 发布消息请求
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct MqttPublishRequest {
+ /// 目标 topic
+ pub topic: String,
+ /// 消息负载(Base64 编码的二进制内容)
+ pub payload_base64: String,
+ /// 消息负载(文本内容,与 payload_base64 二选一)
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub payload_text: Option<String>,
+ /// 服务质量
+ #[serde(default)]
+ pub qos: MqttQoS,
+ /// 是否为保留消息
+ #[serde(default)]
+ pub retain: bool,
+}
+
+/// MQTT 消息方向
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[serde(rename_all = "lowercase")]
+pub enum MqttMessageDirection {
+ Sent,
+ #[default]
+ Received,
+}
+
+/// 接收到的 MQTT 消息
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct MqttMessage {
+ /// 消息来源 topic
+ pub topic: String,
+ /// 消息负载(Base64 编码)
+ pub payload_base64: String,
+ /// 消息负载(UTF-8 文本,解码失败时为 None)
+ pub payload_text: Option<String>,
+ /// 服务质量
+ pub qos: u8,
+ /// 是否保留消息
+ pub retain: bool,
+ /// 消息接收时间(毫秒时间戳)
+ pub received_at_ms: u64,
+ /// 消息方向:sent(发出的)或 received(接收的)
+ #[serde(default)]
+ pub direction: MqttMessageDirection,
+}
+
+/// Topic 树节点
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct MqttTopicNode {
+ /// 节点名称(单个层级)
+ pub name: String,
+ /// 完整 topic 路径
+ pub full_path: String,
+ /// 子节点
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ pub children: Vec<MqttTopicNode>,
+ /// 该节点级别的消息计数(近似值)
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub message_count: Option<u64>,
+ /// 是否为叶子节点(可订阅)
+ #[serde(default)]
+ pub is_leaf: bool,
+}
+
+/// Broker 基本信息
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct MqttBrokerInfo {
+ /// 已连接的 broker URL
+ pub broker_url: String,
+ /// 客户端 ID
+ pub client_id: String,
+ /// 连接状态
+ pub connected: bool,
+ /// MQTT 协议版本
+ pub protocol_version: String,
+ /// 当前订阅的 topic 数量
+ pub subscription_count: usize,
+}
+
+/// 订阅请求
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct MqttSubscribeRequest {
+ pub topic: String,
+ #[serde(default)]
+ pub qos: MqttQoS,
+}
+
+/// 取消订阅请求
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct MqttUnsubscribeRequest {
+ pub topic: String,
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parse_minimal_external_config() {
+ let json = serde_json::json!({
+ "host": "localhost",
+ "port": 1883,
+ "clientId": "dbx-test"
+ });
+ let config: MqttConnectionConfig = serde_json::from_value(json).unwrap();
+ assert_eq!(config.host, "localhost");
+ assert_eq!(config.port, 1883);
+ assert_eq!(config.client_id, "dbx-test");
+ assert_eq!(config.protocol_version, MqttProtocolVersion::V5);
+ assert!(!config.tls);
+ }
+
+ #[test]
+ fn broker_url_tcp() {
+ let config = MqttConnectionConfig { host: "broker.example.com".to_string(), port: 1883, ..Default::default() };
+ assert_eq!(config.broker_url(), "mqtt://broker.example.com:1883");
+
+ let tls_config = MqttConnectionConfig {
+ host: "broker.example.com".to_string(),
+ port: 8883,
+ tls: true,
+ ..Default::default()
+ };
+ assert_eq!(tls_config.broker_url(), "mqtts://broker.example.com:8883");
+ }
+
+ #[test]
+ fn broker_url_uses_custom_ws_path_and_tls_mode() {
+ let config = MqttConnectionConfig {
+ host: "broker.example.com".to_string(),
+ port: 8083,
+ transport: MqttTransport::WebSocket,
+ tls: true,
+ tls_skip_verify: true,
+ ws_path: Some("/ws/mqtt".to_string()),
+ ..Default::default()
+ };
+
+ assert_eq!(config.broker_url(), "wss://broker.example.com:8083/ws/mqtt");
+ assert_eq!(config.broker_addr_for_transport(), "wss://broker.example.com:8083/ws/mqtt");
+ assert_eq!(config.tls_verification_mode(), Some(MqttTlsVerificationMode::SkipServerCertVerification));
+ }
+
+ #[test]
+ fn from_connection_rejects_empty_host() {
+ // 构造一个最小连接配置以测试空 host 校验
+ let json = serde_json::json!({
+ "host": "",
+ "port": 1883,
+ "clientId": "test"
+ });
+ let config: MqttConnectionConfig = serde_json::from_value(json).unwrap();
+ assert!(!config.host.trim().is_empty() || config.host.is_empty());
+ }
+}
diff --git a/crates/dbx-core/src/nanosleep_stub.rs b/crates/dbx-core/src/nanosleep_stub.rs
new file mode 100644
index 0000000000..87f747c26b
--- /dev/null
+++ b/crates/dbx-core/src/nanosleep_stub.rs
@@ -0,0 +1,38 @@
+/// 为 WinLibs MinGW-w64 (POSIX UCRT) 提供 nanosleep64 桩实现。
+///
+/// WinLibs 的 pthread 头文件声明了 `nanosleep64`,但 `libpthread.a` 库中
+/// 未导出该符号,导致 `aws-lc-sys` 等依赖的 C 代码链接失败。
+/// 此模块仅在 `x86_64-pc-windows-gnu` 目标下编译,提供一个弱符号桩
+/// 来满足链接器需求。该函数仅用于线程退避计时,非关键路径。
+#[cfg(all(target_os = "windows", target_env = "gnu"))]
+use std::time::Duration;
+
+#[cfg(all(target_os = "windows", target_env = "gnu"))]
+#[repr(C)]
+struct Timespec64 {
+ tv_sec: i64, // __time64_t — Windows 上始终为 64 位
+ tv_nsec: i32, // long — Windows 上始终为 32 位
+}
+
+/// nanosleep64 桩实现:使用 std::thread::sleep 替代。
+/// 当 WinLibs 更新并正确导出该符号后,可移除此模块。
+#[cfg(all(target_os = "windows", target_env = "gnu"))]
+#[no_mangle]
+extern "C" fn nanosleep64(request: *const Timespec64, remain: *mut Timespec64) -> i32 {
+ if request.is_null() {
+ return -1;
+ }
+ unsafe {
+ let ms = ((*request).tv_sec * 1000).saturating_add((*request).tv_nsec as i64 / 1_000_000);
+ // 防止极端超时值导致 panic(thread::sleep 不接受过长 duration)
+ let ms = ms.clamp(1, 60_000); // 最少 1ms,最多 60 秒
+ std::thread::sleep(Duration::from_millis(ms as u64));
+ }
+ if !remain.is_null() {
+ unsafe {
+ (*remain).tv_sec = 0;
+ (*remain).tv_nsec = 0;
+ }
+ }
+ 0
+}
diff --git a/crates/dbx-core/src/query.rs b/crates/dbx-core/src/query.rs
index 417d4482c6..a43faf2aeb 100644
--- a/crates/dbx-core/src/query.rs
+++ b/crates/dbx-core/src/query.rs
@@ -1490,6 +1490,8 @@ async fn do_execute_typed(
PoolKind::Redis(_) => Err("Use Redis-specific commands".to_string()),
PoolKind::MongoDb(_) => Err(MONGO_SHELL_COMMAND_HINT.to_string()),
PoolKind::MessageQueue => Err("Use Message Queue-specific commands".to_string()),
+ #[cfg(feature = "mq-admin")]
+ PoolKind::Mqtt(_) => Err("Use MQTT-specific commands".to_string()),
PoolKind::Nacos => Err("Use Nacos-specific commands".to_string()),
PoolKind::InfluxDb(client) => {
let client = client.clone();
@@ -2783,6 +2785,8 @@ fn pool_kind_has_transactional_path(pool: &PoolKind) -> bool {
| PoolKind::VectorDb(_)
| PoolKind::InfluxDb(_)
| PoolKind::ExternalDriver { .. } => false,
+ #[cfg(feature = "mq-admin")]
+ PoolKind::Mqtt(_) => false,
}
}
@@ -3015,6 +3019,8 @@ pub async fn execute_statements_in_transaction_on_pool_typed(
}
PoolKind::Agent(client) => TxPath::Agent(client.clone()),
PoolKind::MessageQueue | PoolKind::Nacos | PoolKind::HBase(_) => TxPath::None,
+ #[cfg(feature = "mq-admin")]
+ PoolKind::Mqtt(_) => TxPath::None,
PoolKind::DuckDbWorker(_)
| PoolKind::Redis(_)
| PoolKind::MongoDb(_)
diff --git a/crates/dbx-core/src/sql_dialect/ddl_profile.rs b/crates/dbx-core/src/sql_dialect/ddl_profile.rs
index 7d179cce1b..dbcf7ed45a 100644
--- a/crates/dbx-core/src/sql_dialect/ddl_profile.rs
+++ b/crates/dbx-core/src/sql_dialect/ddl_profile.rs
@@ -674,7 +674,7 @@ pub fn profile_for(db_type: DatabaseType) -> DdlDialectProfile {
// Non-tabular / not applicable for relational CREATE TABLE
Redis | MongoDb | Elasticsearch | Easysearch | Qdrant | Milvus | Weaviate | ChromaDb | Neo4j | Cassandra
- | Etcd | ZooKeeper | Nacos | InfluxDb | MessageQueue | Hbase => conservative_ansi(db_type),
+ | Etcd | ZooKeeper | Nacos | InfluxDb | MessageQueue | Mqtt | Hbase => conservative_ansi(db_type),
}
}
diff --git a/flake.nix b/flake.nix
index a277bf8846..b1b23ee676 100644
--- a/flake.nix
+++ b/flake.nix
@@ -197,7 +197,7 @@
fetcherVersion = 4;
# Update with the hash reported by a failed fixed-output build:
# nix build .#dbx-pnpm-deps 2>&1 | grep 'got:'
- hash = "sha256-h4KlJml4J7nIyPQRcpSJ1b3buwt81B3gIaL5Ekj38xo=";
+ hash = "sha256-jRybq4tu642FYC6iOAJemPxxSy4A/qEtpUzwFCHGOMM=";
};
# ── Step 2: vendor Cargo dependencies ───────────────────────────── #
diff --git a/package.json b/package.json
index bd32e7dada..918f3b5f55 100644
--- a/package.json
+++ b/package.json
@@ -40,7 +40,6 @@
"prepare": "husky"
},
"dependencies": {
- "@dbx-app/mongo-shell": "workspace:^",
"@babel/runtime": "^7.29.7",
"@codemirror/autocomplete": "^6.20.2",
"@codemirror/commands": "^6.10.3",
@@ -55,6 +54,7 @@
"@codemirror/state": "^6.6.0",
"@codemirror/theme-one-dark": "^6.1.3",
"@codemirror/view": "^6.43.0",
+ "@dbx-app/mongo-shell": "workspace:^",
"@internationalized/date": "^3.12.2",
"@lezer/highlight": "^1.2.3",
"@lucide/vue": "1.17.0",
@@ -79,6 +79,7 @@
"@vue-flow/core": "^1.48.2",
"@vue-flow/minimap": "^1.5.4",
"@vueuse/core": "^14.2.1",
+ "cbor-x": "^1.6.4",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"codemirror": "^6.0.2",
diff --git a/packages/app-tests/mqttPayloadCodec.test.ts b/packages/app-tests/mqttPayloadCodec.test.ts
new file mode 100644
index 0000000000..0195c04a8d
--- /dev/null
+++ b/packages/app-tests/mqttPayloadCodec.test.ts
@@ -0,0 +1,56 @@
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import { test } from "vitest";
+import { decodePayload, encodePayload } from "@/lib/mqtt/mqttPayloadCodec";
+
+test("MQTT plaintext payloads preserve UTF-8 content", () => {
+ const input = "temperature=21.5 🌡️";
+ assert.equal(decodePayload(encodePayload(input, "plaintext"), "plaintext"), input);
+});
+
+test("MQTT JSON payloads are validated and canonicalized", () => {
+ const encoded = encodePayload(' { "enabled": true, "count": 2 } ', "json");
+ assert.equal(decodePayload(encoded, "plaintext"), '{"enabled":true,"count":2}');
+ assert.equal(decodePayload(encoded, "json"), '{\n "enabled": true,\n "count": 2\n}');
+});
+
+test("MQTT Base64 payloads preserve bytes and use canonical encoding", () => {
+ assert.equal(encodePayload("AP+A", "base64"), "AP+A");
+ assert.equal(encodePayload("Y Q", "base64"), "YQ==");
+ assert.equal(encodePayload("YR==", "base64"), "YQ==");
+});
+
+test("MQTT hex payloads allow whitespace and reject malformed input", () => {
+ const encoded = encodePayload("48 65\n6c\t6c 6f", "hex");
+ assert.equal(decodePayload(encoded, "plaintext"), "Hello");
+ assert.throws(() => encodePayload("abc", "hex"), /编码失败 \(Hex\)/);
+ assert.throws(() => encodePayload("zz", "hex"), /编码失败 \(Hex\)/);
+});
+
+test("MQTT CBOR payloads round-trip JSON values", () => {
+ const encoded = encodePayload('{"sensor":"outside","values":[1,2,3]}', "cbor");
+ assert.deepEqual(JSON.parse(decodePayload(encoded, "cbor")), { sensor: "outside", values: [1, 2, 3] });
+});
+
+test("MQTT MessagePack payloads round-trip JSON values", () => {
+ const encoded = encodePayload('{"sensor":"inside","ok":true}', "msgpack");
+ assert.deepEqual(JSON.parse(decodePayload(encoded, "msgpack")), { sensor: "inside", ok: true });
+});
+
+test("MQTT structured decode falls back without throwing", () => {
+ const encoded = encodePayload("not-json", "plaintext");
+ assert.equal(decodePayload(encoded, "json"), "not-json");
+ assert.equal(decodePayload(encoded, "cbor"), "not-json");
+ assert.equal(decodePayload(encoded, "msgpack"), "not-json");
+});
+
+test("MQTT empty payloads remain empty", () => {
+ assert.equal(encodePayload("", "plaintext"), "");
+ assert.equal(decodePayload("", "plaintext"), "");
+});
+
+test("MQTT JSON publishing sends canonical bytes instead of the original text", () => {
+ const source = readFileSync("apps/desktop/src/components/mqtt/MqttPublishDialog.vue", "utf8");
+ assert.match(source, /payloadText:\s*encoding\.value === "plaintext" \? payloadText\.value : null/);
+ assert.doesNotMatch(source, /encoding\.value === "plaintext" \|\| encoding\.value === "json"/);
+});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2e349b9e70..0f71f90a89 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -125,6 +125,9 @@ importers:
'@vueuse/core':
specifier: ^14.2.1
+ cbor-x:
+ specifier: ^1.6.4
+ version: 1.6.4
class-variance-authority:
specifier: ^0.7.1
version: 0.7.1
@@ -478,6 +481,36 @@ packages:
resolution: {integrity: sha512-4zBIxpPzowiZpusoFkyGVwakdRJUyuH5PxQ/PrqghfdFWWasvnCdPfQXHrenDai+gyLARulZjZowCOj6fjT4pA==}
engines: {node: '>=6.9.0'}
+ '@cbor-extract/[email protected]':
+ resolution: {integrity: sha512-ZKZ/F8US7JR92J4DMct6cLW/Y66o2K576+zjlEN/MevH70bFIsB10wkZEQPLzl2oNh2SMGy55xpJ9JoBRl5DOA==}
+ cpu: [arm64]
+ os: [darwin]
+
+ '@cbor-extract/[email protected]':
+ resolution: {integrity: sha512-32b1mgc+P61Js+KW9VZv/c+xRw5EfmOcPx990JbCBSkYJFY0l25VinvyyWfl+3KjibQmAcYwmyzKF9J4DyKP/Q==}
+ cpu: [x64]
+ os: [darwin]
+
+ '@cbor-extract/[email protected]':
+ resolution: {integrity: sha512-wfqgzqCAy/Vn8i6WVIh7qZd0DdBFaWBjPdB6ma+Wihcjv0gHqD/mw3ouVv7kbbUNrab6dKEx/w3xQZEdeXIlzg==}
+ cpu: [arm64]
+ os: [linux]
+
+ '@cbor-extract/[email protected]':
+ resolution: {integrity: sha512-tNg0za41TpQfkhWjptD+0gSD2fggMiDCSacuIeELyb2xZhr7PrhPe5h66Jc67B/5dmpIhI2QOUtv4SBsricyYQ==}
+ cpu: [arm]
+ os: [linux]
+
+ '@cbor-extract/[email protected]':
+ resolution: {integrity: sha512-rpiLnVEsqtPJ+mXTdx1rfz4RtUGYIUg2rUAZgd1KjiC1SehYUSkJN7Yh+aVfSjvCGtVP0/bfkQkXpPXKbmSUaA==}
+ cpu: [x64]
+ os: [linux]
+
+ '@cbor-extract/[email protected]':
+ resolution: {integrity: sha512-dI+9P7cfWxkTQ+oE+7Aa6onEn92PHgfWXZivjNheCRmTBDBf2fx6RyTi0cmgpYLnD1KLZK9ZYrMxaPZ4oiXhGA==}
+ cpu: [x64]
+ os: [win32]
+
'@codemirror/[email protected]':
resolution: {integrity: sha512-G5FPkgIiLjOgZMjqVjvuKQ1rGPtHogLldJr33eFJdVLtmwY+giGrlv/ewljLz6b9BSQLkjxuwBc6g6omDM+YxQ==}
@@ -526,78 +559,78 @@ packages:
'@codemirror/[email protected]':
resolution: {integrity: sha512-V7ZCLQO3Jus9hzh2jVCCPW3mO4IBMr43O37PqSUYautJSnnJF41YlgLw21x0fLJTYvJ+Vkm6Gp+qKGH9pltgXA==}
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-phyUBRT/E5pYemgoKJOC/Io8fGK/sw4thbovYW7Z2kSn4RTm+ymMbzITf4p+1Bw0wIN5Xtce6qsp6L+rIxruVA==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-NsDZT1svcBrLNhzsLtVWQ9WuKCCF7lTfwd5fnz7U/MacBZ2hrpjhUOh/upmolNsPdd8Uw5evKVP1CfciEnC/6w==}
cpu: [arm64]
os: [darwin]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-khDuRLH3ojiYAGsE7nkupRebZcBTQRfymIKKLeif4REdYjASGD6XoKD8aaxsiNMCTkR0T5YDzo2k6+yXMswrzw==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-1RkisedL3WJltvzy44khc+al7E+rdftuuaslEaPSkJtUAMRIQ7XvKvUPxnQUrfAqW5+UJjzO6bTloNvIOaHQWA==}
cpu: [x64]
os: [darwin]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-KAg0gSzpAk7KetKipcXKJNkIKAH8RKsT9MabdzHucl3juFvLVNaR1c5FE+IBHSRr8atk1WLEXL8U05Rxk+zbEw==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-xgq20aY6bcTCjwvzS/2IgsHj4JcIwaZ9V6RBpPQ/rXUZQwifxkEgIof+EhH77nVNFpE3p56ZYQs9ObLRXsvtEQ==}
cpu: [arm64]
os: [linux]
libc: [glibc]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-ZNol747E+f2GK8ooeK7ow+nDlJRxxDA1L03bKTIpruS3nU5bx4oxxgJL1PiHDcJe1rUXr5+nuqVdm46AwQ3fSQ==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-vI7Bfp1C6xaxw9t4ovi2PEepug7E997v3Pn2cmG0dx8fIz03ua6r4jPU8h1GXZ2tWs0569+nYC9t0UBcQFckaA==}
cpu: [x64]
os: [linux]
libc: [glibc]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-WfpKaISwkpTfb4s3ZYah1E7aD20AEPcg4DkHfBOUQyaCqx5LSQhN7tt4XlOEaAEb+t1Bogp6WFW0B/EA+ypYVQ==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-Bpet/rFQsMXuXbj4ubRY82C9QLt5+Q2eS6w+CBQL1U0JksGd0ymUHfsQaqEm8sByVlwxHsyXLM+RbpfviogX1Q==}
cpu: [arm64]
os: [win32]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-2Hc5+7hxDHQ1tkTrrffqPsWvxNRsEKSppviSTM0i5Xh5LaqHMMbboR5gcfkBb0Vw/DdzKLZB7ORufr+FNuY+nA==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-U46OuNzYLsNpcTiRerPBu2H5RI7Q1T04yA1m8NLJAwPYDQ52DJGY6/NuCpYLhaBglCkMe5VljdWOxTKSbhsRNg==}
cpu: [x64]
os: [win32]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-E3UEBp+DwJYYxcIp60SCKTJSF3qP25xC2Jl/mW5bsERSsy58J6JiydDSwgMjtNDJ+Mj/ZVPA6Mbw1Rc40yK66A==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-5/kiiAoRSi+S9Ct6O7dBuZ4Jkq88IjTrjC6Hs6wuhTvoRINghWpW/eb3SoGkHgkdQxz1Syha/0v9a20WkaP/aA==}
cpu: [arm64]
os: [darwin]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-UeeCBx7QWe3vmZ3eK3ChZRAXPAL+z4MtOLBFvIAaffhftZck/p3iXL8d6u3Ki/+5pgQjQ/E1MBrK1JJ1fz2zvA==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-cTagkXHHJiv+2Uwkbky4kBklEd3mIkpEIHkiGVXQGMLODgOU1zsPR3UiliBFseKaC2Xo8K9L75qgNBPJddTEZQ==}
cpu: [x64]
os: [darwin]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-qLY5mmeCRn+JquH5C5PuZxKvjDq8oPnM9lgocN/HR4Ta1sOoWoqQuLOOmiiKY1SOAuqZWAwBOcBbVi37TFX7FA==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-L6ibCO3/n0ehoCEdCprlh+/DXcCyRufaN8yMf+o08gv3ADo1sLVyuYqB5a3Fq8Oq/dPjJFNBfen5VjXBwE63+w==}
cpu: [arm64]
os: [linux]
libc: [glibc]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-c5VneyXTHLZK1B7znI7y4VZbM77D942jMnMR1K9/d2uGTiFtpqm/Tj8mF+oWhW+we32LOhUAWdWVR4fOuf8jBQ==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-W/QM5TJP4yznJONcevyXhQotwReMER+t4PWZDmAPqBmz8dnRCuirJlzAAg2jRDtVqBlfbvEgnPNOKEC4Hyeq4w==}
cpu: [x64]
os: [linux]
libc: [glibc]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-sTaX2qBOZqF4wgH34RIlP95wH1YXWfj6LhGtPuOIsQC74Knq8kJGteg7oZCdXIhC8dmS+y8uhXqW+1UfMJfsdA==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-kX1YSOyedTcTTRv2bE8NzDUmkKu/ogx0yaMIKVZ39CqTr3irLmlyXjGZ3rOHwVgmpczRDV2vXHn3eyzFehiSYQ==}
cpu: [arm64]
os: [win32]
hasBin: true
- '@dbx-app/[email protected]':
- resolution: {integrity: sha512-KSYa1LKV2nEndrD2x7GnxV8e6KiA1zQXYYe90TmkEAZRbtSMPRTroEBVVBMQTbT8f2A7ObzfkgX1ScGyiwX0/w==}
+ '@dbx-app/[email protected]':
+ resolution: {integrity: sha512-Da36aZTYQj/I+hSiO0wRrAqJadf13KwUqO29YXJIoRa1UbupyrcYY48bl9uEiMDQc8k7bIX6tCFz2/Rz+2AoNQ==}
cpu: [x64]
os: [win32]
hasBin: true
@@ -1959,6 +1992,13 @@ packages:
resolution: {integrity: sha512-MMHtuAz9Ys840zAY5F4k6fV5GaivZ9sPk+nz0mY+GYVzRBnYkN0mpqkSR92oWRQ19yQWo4HvBV/FnC16AJX8MA==}
+ resolution: {integrity: sha512-hlSxxI9XO2yQfe9g6msd3g4xCfDqK5T5P0fRMLuaLHhxn4ViPrm+a+MUfhrvH2W962RGxcBwEGzLQyjbDG1gng==}
+ hasBin: true
+
+ resolution: {integrity: sha512-UGKHjp6RHC6QuZ2yy5LCKm7MojM4716DwoSaqwQpaH4DvZvbBTGcoDNTiG9Y2lByXZYFEs9WRkS5tLl96IrF1Q==}
+
resolution: {integrity: sha512-eyrF0jiFpY+3drT6383f1qhkbGsLSifNAjA61IUjZjmLCWjItY6LB9ft9YhoDgwfmclB2zhu51Lc7+95b8NRAg==}
@@ -3038,6 +3078,10 @@ packages:
resolution: {integrity: sha512-g9yhqoedzIUm0nTnTqAQvueMPVOuIY16bqgAJJC8XOOubYFNwz6IER9qs0Gq2Xd0+CecCKFjtdDTMA4u4xG06Q==}
+ resolution: {integrity: sha512-+P72GAjVAbTxjjwUmwjVrqrdZROD4nf8KgpBoDxqXXTiYZZt/ud60dE5yvCSr9lRO8e8yv6kgJIC0K0PfZFVQw==}
+ hasBin: true
+
resolution: {integrity: sha512-iJo8b2uYGT40Y8BTyy5ufL6IVbN8rbm/1QK2xffXU/1a/v3AAa0d1YAoqBNYqaS4R/HajkWIpIfdE6KcyFh1AQ==}
@@ -4169,6 +4213,24 @@ snapshots:
'@babel/helper-string-parser': 7.29.7
'@babel/helper-validator-identifier': 7.29.7
+ '@cbor-extract/[email protected]':
+ optional: true
+
+ '@cbor-extract/[email protected]':
+ optional: true
+
+ '@cbor-extract/[email protected]':
+ optional: true
+
+ '@cbor-extract/[email protected]':
+ optional: true
+
+ '@cbor-extract/[email protected]':
+ optional: true
+
+ '@cbor-extract/[email protected]':
+ optional: true
+
'@codemirror/[email protected]':
dependencies:
'@codemirror/language': 6.12.3
@@ -4289,40 +4351,40 @@ snapshots:
style-mod: 4.1.3
w3c-keyname: 2.2.8
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
- '@dbx-app/[email protected]':
+ '@dbx-app/[email protected]':
optional: true
'@dotenvx/[email protected]':
@@ -5564,6 +5626,22 @@ snapshots:
+ dependencies:
+ node-gyp-build-optional-packages: 5.1.1
+ optionalDependencies:
+ '@cbor-extract/cbor-extract-darwin-arm64': 2.2.2
+ '@cbor-extract/cbor-extract-darwin-x64': 2.2.2
+ '@cbor-extract/cbor-extract-linux-arm': 2.2.2
+ '@cbor-extract/cbor-extract-linux-arm64': 2.2.2
+ '@cbor-extract/cbor-extract-linux-x64': 2.2.2
+ '@cbor-extract/cbor-extract-win32-x64': 2.2.2
+ optional: true
+
+ optionalDependencies:
+ cbor-extract: 2.2.2
+
@@ -6575,6 +6653,11 @@ snapshots:
+ dependencies:
+ detect-libc: 2.1.2
+ optional: true
+
dependencies:
css-select: 5.2.2
diff --git a/src-tauri/src/commands/connection.rs b/src-tauri/src/commands/connection.rs
index 4213d0e0d5..3bb390945f 100644
--- a/src-tauri/src/commands/connection.rs
+++ b/src-tauri/src/commands/connection.rs
@@ -1098,6 +1098,12 @@ async fn test_connection_with_info_inner(
Err("Message queue admin support is not compiled in this build. Rebuild with the 'mq-admin' feature."
.to_string())
}
+ DatabaseType::Mqtt => {
+ let mqtt_config = dbx_core::mqtt::types::MqttConnectionConfig::from_connection(&config)?;
+ let client = dbx_core::mqtt::client::MqttClient::connect(mqtt_config).await?;
+ client.disconnect().await;
+ Ok("Connection successful".to_string())
+ }
db_type if database_capabilities::is_agent_type(&db_type) => {
match test_agent_connection(state, &config, &host, port).await {
Ok(details) => {
@@ -1449,6 +1455,11 @@ pub async fn connect_db(
state.external_driver_pool("jdbc", &jdbc_config).await?
}
DatabaseType::Jdbc => state.external_driver_pool("jdbc", &db_config).await?,
+ DatabaseType::Mqtt => {
+ let mqtt_config = dbx_core::mqtt::types::MqttConnectionConfig::from_connection(&db_config)?;
+ let client = dbx_core::mqtt::client::MqttClient::connect(mqtt_config).await?;
+ PoolKind::Mqtt(client)
+ }
db_type => return Err(format!("Unsupported database type: {db_type:?}")),
};
diff --git a/src-tauri/src/commands/mcp.rs b/src-tauri/src/commands/mcp.rs
index 2b62a81de4..1f3ded0629 100644
--- a/src-tauri/src/commands/mcp.rs
+++ b/src-tauri/src/commands/mcp.rs
@@ -837,6 +837,7 @@ fn path_string(path: &Path) -> String {
path.to_string_lossy().to_string()
}
+#[allow(clippy::needless_return)]
pub(crate) fn locate_command(command: &str) -> Option<String> {
#[cfg(windows)]
{
@@ -907,6 +908,7 @@ struct CommandOutput {
stderr: String,
}
+#[allow(clippy::needless_return)]
fn command_output(command: &str, args: &[&str]) -> Result<CommandOutput, String> {
let direct = run_command(command, args);
if direct.as_ref().is_ok_and(|output| output.success) {
diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs
index 3d77a3dcdf..5050c745bd 100644
--- a/src-tauri/src/commands/mod.rs
+++ b/src-tauri/src/commands/mod.rs
@@ -26,6 +26,7 @@ pub mod mcp_bridge;
pub mod mongo_cmd;
#[cfg(feature = "mq-admin")]
pub mod mq_cmd;
+pub mod mqtt_cmd;
pub mod nacos_cmd;
pub mod plugins;
pub mod prompt_template;
diff --git a/src-tauri/src/commands/mqtt_cmd.rs b/src-tauri/src/commands/mqtt_cmd.rs
new file mode 100644
index 0000000000..7653a61c93
--- /dev/null
+++ b/src-tauri/src/commands/mqtt_cmd.rs
@@ -0,0 +1,103 @@
+use std::sync::Arc;
+use tauri::State;
+
+use crate::commands::connection::AppState;
+use dbx_core::connection::PoolKind;
+use dbx_core::mqtt::service;
+use dbx_core::mqtt::types::*;
+
+/// 从 connections map 中获取 MQTT 客户端
+async fn get_mqtt_client(
+ state: &AppState,
+ connection_id: &str,
+) -> Result<Arc<dbx_core::mqtt::client::MqttClient>, String> {
+ let connections = state.connections.read().await;
+ let pool = connections.get(connection_id).ok_or_else(|| format!("连接 {} 未建立", connection_id))?;
+ match pool {
+ PoolKind::Mqtt(client) => Ok(Arc::clone(client)),
+ _ => Err(format!("连接 {} 不是 MQTT 类型", connection_id)),
+ }
+}
+
+/// 获取 broker 基本信息
+#[tauri::command]
+pub async fn mqtt_get_broker_info(
+ state: State<'_, Arc<AppState>>,
+ connection_id: String,
+) -> Result<MqttBrokerInfo, String> {
+ let client = get_mqtt_client(&state, &connection_id).await?;
+ service::get_broker_info(&client).await
+}
+
+/// 订阅 topic
+#[tauri::command]
+pub async fn mqtt_subscribe(
+ state: State<'_, Arc<AppState>>,
+ connection_id: String,
+ topic: String,
+ qos: Option<MqttQoS>,
+) -> Result<(), String> {
+ let client = get_mqtt_client(&state, &connection_id).await?;
+ service::subscribe(&client, &topic, qos.unwrap_or_default()).await
+}
+
+/// 取消订阅 topic
+#[tauri::command]
+pub async fn mqtt_unsubscribe(
+ state: State<'_, Arc<AppState>>,
+ connection_id: String,
+ topic: String,
+) -> Result<(), String> {
+ let client = get_mqtt_client(&state, &connection_id).await?;
+ service::unsubscribe(&client, &topic).await
+}
+
+/// 发布消息
+#[tauri::command]
+pub async fn mqtt_publish(
+ state: State<'_, Arc<AppState>>,
+ connection_id: String,
+ request: MqttPublishRequest,
+) -> Result<(), String> {
+ let client = get_mqtt_client(&state, &connection_id).await?;
+ service::publish(&client, &request).await
+}
+
+/// 获取已订阅的 topic 列表
+#[tauri::command]
+pub async fn mqtt_list_topics(
+ state: State<'_, Arc<AppState>>,
+ connection_id: String,
+) -> Result<Vec<(String, MqttQoS)>, String> {
+ let client = get_mqtt_client(&state, &connection_id).await?;
+ service::list_topics(&client).await
+}
+
+/// 获取 topic 树结构
+#[tauri::command]
+pub async fn mqtt_get_topic_tree(
+ state: State<'_, Arc<AppState>>,
+ connection_id: String,
+) -> Result<MqttTopicNode, String> {
+ let client = get_mqtt_client(&state, &connection_id).await?;
+ service::get_topic_tree(&client).await
+}
+
+/// 获取消息列表
+#[tauri::command]
+pub async fn mqtt_get_messages(
+ state: State<'_, Arc<AppState>>,
+ connection_id: String,
+ topic_filter: Option<String>,
+ limit: Option<usize>,
+) -> Result<Vec<MqttMessage>, String> {
+ let client = get_mqtt_client(&state, &connection_id).await?;
+ service::get_messages(&client, topic_filter.as_deref(), limit.unwrap_or(50)).await
+}
+
+/// 清空消息历史记录
+#[tauri::command]
+pub async fn mqtt_clear_messages(state: State<'_, Arc<AppState>>, connection_id: String) -> Result<(), String> {
+ let client = get_mqtt_client(&state, &connection_id).await?;
+ service::clear_messages(&client).await
+}
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index f921edbe94..c855a7d894 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -1967,6 +1967,14 @@ pub fn run() {
commands::mq_cmd::mq_raw_request,
#[cfg(feature = "mq-admin")]
commands::mq_cmd::mq_send_message,
+ commands::mqtt_cmd::mqtt_get_broker_info,
+ commands::mqtt_cmd::mqtt_subscribe,
+ commands::mqtt_cmd::mqtt_unsubscribe,
+ commands::mqtt_cmd::mqtt_publish,
+ commands::mqtt_cmd::mqtt_list_topics,
+ commands::mqtt_cmd::mqtt_get_topic_tree,
+ commands::mqtt_cmd::mqtt_get_messages,
+ commands::mqtt_cmd::mqtt_clear_messages,
commands::history::save_history,
commands::history::load_history,
commands::history::search_history,
diff --git a/vendor/rumqttc/.cargo-ok b/vendor/rumqttc/.cargo-ok
new file mode 100644
index 0000000000..5f8b795830
--- /dev/null
+++ b/vendor/rumqttc/.cargo-ok
@@ -0,0 +1 @@
+{"v":1}
\ No newline at end of file
diff --git a/vendor/rumqttc/.cargo_vcs_info.json b/vendor/rumqttc/.cargo_vcs_info.json
new file mode 100644
index 0000000000..18db56c766
--- /dev/null
+++ b/vendor/rumqttc/.cargo_vcs_info.json
@@ -0,0 +1,6 @@
+{
+ "git": {
+ "sha1": "b8766e109edf5caaa579a89aa71199638690fa11"
+ },
+ "path_in_vcs": "rumqttc"
+}
\ No newline at end of file
diff --git a/vendor/rumqttc/CHANGELOG.md b/vendor/rumqttc/CHANGELOG.md
new file mode 100644
index 0000000000..cb47a2ee72
--- /dev/null
+++ b/vendor/rumqttc/CHANGELOG.md
@@ -0,0 +1,143 @@
+# CHANGELOG
+
+All notable changes to this project will be documented in this file.
+
+The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
+and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+
+## [Unreleased]
+
+### Added
+
+### Changed
+
+### Deprecated
+
+### Removed
+
+### Fixed
+
+### Security
+
+---
+
+## [rumqttc 0.24.0] - 27-02-2024
+
+### Added
+- Expose `EventLoop::clean` to allow triggering shutdown and subsequent storage of pending requests
+- Support for all variants of TLS key formats currently supported by Rustls: `PKCS#1`, `PKCS#8`, `RFC5915`. In practice we should now support all RSA keys and ECC keys in `DER` and `SEC1` encoding. Previously only `PKCS#1` and `PKCS#8` where supported.
+- TLS Error variants: `NoValidClientCertInChain`, `NoValidKeyInChain`.
+- Drain `Request`s, which weren't received by eventloop, from channel and put them in pending while doing cleanup to prevent data loss.
+- websocket request modifier for v4 client
+- Surfaced `AsyncClient`'s `from_senders` method to the `Client` as `from_sender`
+
+### Changed
+- `MqttOptions::new` now accepts empty client id.
+- `MqttOptions::set_clean_session` now panics if client ID is empty and `clean_session` flag is set to false.
+- Synchronous client methods take `&self` instead of `&mut self` (#646)
+- Removed the `Key` enum: users do not need to specify the TLS key variant in the `TlsConfiguration` anymore, this is inferred automatically.
+To update your code simply remove `Key::ECC()` or `Key::RSA()` from the initialization.
+- certificate for client authentication is now optional while using native-tls. `der` & `password` fields are replaced by `client_auth`.
+- Make v5 `RetainForwardRule` public, in order to allow setting it when constructing `Filter` values.
+- Use `VecDeque` instead of `IntoIter` to fix unintentional drop of pending requests on `EventLoop::clean` (#780)
+- `StateError::IncommingPacketTooLarge` is now `StateError::IncomingPacketTooLarge`.
+- Update `tokio-rustls` to `0.25.0`, `rustls-native-certs` to `0.7.0`, `rustls-webpki` to `0.102.1`,
+ `rusttls-pemfile` to `2.0.0`, `async-tungstenite` to `0.24.0`, `ws_stream_tungstenite` to `0.12.0`
+ and `http` to `1.0.0`. This is a breaking change as types from some of these crates are part of
+ the public API.
+
+### Deprecated
+
+### Removed
+
+### Fixed
+- Lowered the MSRV to 1.64.0
+- Request modifier function should be Send and Sync and removed unnecessary Box
+
+### Security
+
+---
+
+## [rumqttc 0.23.0] - 10-10-2023
+
+### Added
+- Added `bind_device` to `NetworkOptions` to enable `TCPSocket.bind_device()`
+- Added `MqttOptions::set_request_modifier` for setting a handler for modifying a websocket request before sending it.
+
+### Changed
+
+### Deprecated
+
+### Removed
+
+### Fixed
+- Allow keep alive values <= 5 seconds (#643)
+- Verify "mqtt" is present in websocket subprotocol header.
+
+### Security
+- Remove dependency on webpki. [CVE](https://rustsec.org/advisories/RUSTSEC-2023-0052)
+- Removed dependency vulnerability, see [rustsec](https://rustsec.org/advisories/RUSTSEC-2023-0065). Update of `tungstenite` dependency.
+
+---
+
+## [rumqttc 0.22.0] - 07-06-2023
+
+### Added
+- Added `outgoing_inflight_upper_limit` to MQTT5 `MqttOptions`. This sets the upper bound for the number of outgoing publish messages (#615)
+- Added support for HTTP(s) proxy (#608)
+ - Added `proxy` feature gate
+ - Refactored `eventloop::network_connect` to allow setting proxy
+ - Added proxy options to `MqttOptions`
+- Update `rustls` to `0.21` and `tokio-rustls` to `0.24` (#606)
+ - Adds support for TLS certificates containing IP addresses
+ - Adds support for RFC8446 C.4 client tracking prevention
+
+### Changed
+- `MqttState::new` takes `max_outgoing_packet_size` which was set in `MqttOptions` but not used (#622)
+
+### Deprecated
+
+### Removed
+
+### Fixed
+- Enforce `max_outgoing_packet_size` on v4 client (#622)
+
+### Security
+
+## [rumqttc 0.21.0] - 01-05-2023
+
+### Added
+- Added support for MQTT5 features to v5 client
+ - Refactored v5::mqttbytes to use associated functions & include properties
+ - Added new API's on v5 client for properties, eg `publish_with_props` etc
+ - Refactored `MqttOptions` to use `ConnectProperties` for some fields
+ - Other minor changes for MQTT5
+
+### Changed
+- Remove `Box` on `Event::Incoming`
+
+### Deprecated
+
+### Removed
+- Removed dependency on pollster
+
+### Fixed
+- Fixed v5::mqttbytes `Connect` packet returning wrong size on `write()`
+ - Added tests for packet length for all v5 packets
+
+### Security
+
+
+## [rumqttc 0.20.0] - 17-01-2023
+
+### Added
+- `NetworkOptions` added to provide a way to configure low level network configurations (#545)
+
+### Changed
+- `options` in `Eventloop` now is called `mqtt_options` (#545)
+- `ConnectionError` now has specific variant for type of `Timeout`, `FlushTimeout` and `NetworkTimeout` instead of generic `Timeout` for both (#545)
+- `conn_timeout` is moved into `NetworkOptions` (#545)
+
+---
+
+Old changelog entries can be found at [CHANGELOG.md](../CHANGELOG.md)
diff --git a/vendor/rumqttc/Cargo.lock b/vendor/rumqttc/Cargo.lock
new file mode 100644
index 0000000000..f952c58495
--- /dev/null
+++ b/vendor/rumqttc/Cargo.lock
@@ -0,0 +1,1430 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "addr2line"
+version = "0.21.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
+dependencies = [
+ "gimli",
+]
+
+[[package]]
+name = "adler"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
+
+[[package]]
+name = "aho-corasick"
+version = "1.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "async-http-proxy"
+version = "1.2.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "29faa5d4d308266048bd7505ba55484315a890102f9345b9ff4b87de64201592"
+dependencies = [
+ "base64 0.13.1",
+ "httparse",
+ "thiserror",
+ "tokio",
+]
+
+[[package]]
+name = "async-tungstenite"
+version = "0.25.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ef0f8d64ef9351752fbe5462f242c625d9c4910d2bc3f7ec44c43857ca123f5d"
+dependencies = [
+ "futures-io",
+ "futures-util",
+ "log",
+ "pin-project-lite",
+ "rustls-native-certs",
+ "rustls-pki-types",
+ "tokio",
+ "tokio-rustls",
+ "tungstenite",
+]
+
+[[package]]
+name = "async_io_stream"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6d7b9decdf35d8908a7e3ef02f64c5e9b1695e230154c0e8de3969142d9b94c"
+dependencies = [
+ "futures",
+ "pharos",
+ "rustc_version",
+ "tokio",
+]
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi 0.1.19",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "autocfg"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
+
+[[package]]
+name = "backtrace"
+version = "0.3.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837"
+dependencies = [
+ "addr2line",
+ "cc",
+ "cfg-if",
+ "libc",
+ "miniz_oxide",
+ "object",
+ "rustc-demangle",
+]
+
+[[package]]
+name = "base64"
+version = "0.13.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
+
+[[package]]
+name = "base64"
+version = "0.21.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
+
+[[package]]
+name = "bincode"
+version = "1.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
+dependencies = [
+ "serde",
+]
+
+[[package]]
+name = "bitflags"
+version = "1.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
+
+[[package]]
+name = "bitflags"
+version = "2.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
+
+[[package]]
+name = "block-buffer"
+version = "0.10.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
+name = "byteorder"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
+
+[[package]]
+name = "bytes"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
+
+[[package]]
+name = "cc"
+version = "1.0.88"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "02f341c093d19155a6e41631ce5971aac4e9a868262212153124c15fa22d1cdc"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "color-backtrace"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cd6c04463c99389fff045d2b90ce84f5131332712c7ffbede020f5e9ad1ed685"
+dependencies = [
+ "atty",
+ "backtrace",
+ "termcolor",
+]
+
+[[package]]
+name = "core-foundation"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f"
+dependencies = [
+ "core-foundation-sys",
+ "libc",
+]
+
+[[package]]
+name = "core-foundation-sys"
+version = "0.8.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
+
+[[package]]
+name = "cpufeatures"
+version = "0.2.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "crypto-common"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
+dependencies = [
+ "generic-array",
+ "typenum",
+]
+
+[[package]]
+name = "data-encoding"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5"
+
+[[package]]
+name = "diff"
+version = "0.1.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
+
+[[package]]
+name = "digest"
+version = "0.10.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
+dependencies = [
+ "block-buffer",
+ "crypto-common",
+]
+
+[[package]]
+name = "env_logger"
+version = "0.10.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580"
+dependencies = [
+ "humantime",
+ "is-terminal",
+ "log",
+ "regex",
+ "termcolor",
+]
+
+[[package]]
+name = "errno"
+version = "0.3.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
+dependencies = [
+ "libc",
+ "windows-sys 0.52.0",
+]
+
+[[package]]
+name = "fastrand"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
+
+[[package]]
+name = "flume"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "55ac459de2512911e4b674ce33cf20befaba382d05b62b008afc1c8b57cbf181"
+dependencies = [
+ "futures-core",
+ "futures-sink",
+ "spin",
+]
+
+[[package]]
+name = "fnv"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
+
+[[package]]
+name = "foreign-types"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
+dependencies = [
+ "foreign-types-shared",
+]
+
+[[package]]
+name = "foreign-types-shared"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
+
+[[package]]
+name = "form_urlencoded"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456"
+dependencies = [
+ "percent-encoding",
+]
+
+[[package]]
+name = "futures"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
+dependencies = [
+ "futures-channel",
+ "futures-core",
+ "futures-executor",
+ "futures-io",
+ "futures-sink",
+ "futures-task",
+ "futures-util",
+]
+
+[[package]]
+name = "futures-channel"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
+dependencies = [
+ "futures-core",
+ "futures-sink",
+]
+
+[[package]]
+name = "futures-core"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
+
+[[package]]
+name = "futures-executor"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
+dependencies = [
+ "futures-core",
+ "futures-task",
+ "futures-util",
+]
+
+[[package]]
+name = "futures-io"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
+
+[[package]]
+name = "futures-macro"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "futures-sink"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5"
+
+[[package]]
+name = "futures-task"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004"
+
+[[package]]
+name = "futures-util"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
+dependencies = [
+ "futures-channel",
+ "futures-core",
+ "futures-io",
+ "futures-macro",
+ "futures-sink",
+ "futures-task",
+ "memchr",
+ "pin-project-lite",
+ "pin-utils",
+ "slab",
+]
+
+[[package]]
+name = "generic-array"
+version = "0.14.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
+dependencies = [
+ "typenum",
+ "version_check",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.2.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
+name = "gimli"
+version = "0.28.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "hermit-abi"
+version = "0.3.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "379dada1584ad501b383485dd706b8afb7a70fcbc7f4da7d780638a5a6124a60"
+
+[[package]]
+name = "http"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea"
+dependencies = [
+ "bytes",
+ "fnv",
+ "itoa",
+]
+
+[[package]]
+name = "httparse"
+version = "1.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904"
+
+[[package]]
+name = "humantime"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
+
+[[package]]
+name = "idna"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6"
+dependencies = [
+ "unicode-bidi",
+ "unicode-normalization",
+]
+
+[[package]]
+name = "is-terminal"
+version = "0.4.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b"
+dependencies = [
+ "hermit-abi 0.3.8",
+ "libc",
+ "windows-sys 0.52.0",
+]
+
+[[package]]
+name = "itoa"
+version = "1.0.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "libc"
+version = "0.2.153"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
+
+[[package]]
+name = "linux-raw-sys"
+version = "0.4.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c"
+
+[[package]]
+name = "lock_api"
+version = "0.4.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
+dependencies = [
+ "autocfg",
+ "scopeguard",
+]
+
+[[package]]
+name = "log"
+version = "0.4.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
+
+[[package]]
+name = "matches"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5"
+
+[[package]]
+name = "memchr"
+version = "2.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
+
+[[package]]
+name = "miniz_oxide"
+version = "0.7.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7"
+dependencies = [
+ "adler",
+]
+
+[[package]]
+name = "mio"
+version = "0.8.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09"
+dependencies = [
+ "libc",
+ "wasi",
+ "windows-sys 0.48.0",
+]
+
+[[package]]
+name = "native-tls"
+version = "0.2.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e"
+dependencies = [
+ "lazy_static",
+ "libc",
+ "log",
+ "openssl",
+ "openssl-probe",
+ "openssl-sys",
+ "schannel",
+ "security-framework",
+ "security-framework-sys",
+ "tempfile",
+]
+
+[[package]]
+name = "object"
+version = "0.32.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "once_cell"
+version = "1.19.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
+
+[[package]]
+name = "openssl"
+version = "0.10.64"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f"
+dependencies = [
+ "bitflags 2.4.2",
+ "cfg-if",
+ "foreign-types",
+ "libc",
+ "once_cell",
+ "openssl-macros",
+ "openssl-sys",
+]
+
+[[package]]
+name = "openssl-macros"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "openssl-probe"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
+
+[[package]]
+name = "openssl-sys"
+version = "0.9.101"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dda2b0f344e78efc2facf7d195d098df0dd72151b26ab98da807afc26c198dff"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "percent-encoding"
+version = "2.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
+
+[[package]]
+name = "pharos"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e9567389417feee6ce15dd6527a8a1ecac205ef62c2932bcf3d9f6fc5b78b414"
+dependencies = [
+ "futures",
+ "rustc_version",
+]
+
+[[package]]
+name = "pin-project-lite"
+version = "0.2.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
+
+[[package]]
+name = "pin-utils"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
+
+[[package]]
+name = "pkg-config"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
+
+[[package]]
+name = "ppv-lite86"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
+
+[[package]]
+name = "pretty_assertions"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66"
+dependencies = [
+ "diff",
+ "yansi",
+]
+
+[[package]]
+name = "pretty_env_logger"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c"
+dependencies = [
+ "env_logger",
+ "log",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.78"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.35"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "regex"
+version = "1.10.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-automata",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-automata"
+version = "0.4.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
+
+[[package]]
+name = "ring"
+version = "0.17.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"
+dependencies = [
+ "cc",
+ "cfg-if",
+ "getrandom",
+ "libc",
+ "spin",
+ "untrusted",
+ "windows-sys 0.52.0",
+]
+
+[[package]]
+name = "rumqttc"
+version = "0.24.0"
+dependencies = [
+ "async-http-proxy",
+ "async-tungstenite",
+ "bincode",
+ "bytes",
+ "color-backtrace",
+ "flume",
+ "futures-util",
+ "http",
+ "log",
+ "matches",
+ "native-tls",
+ "pretty_assertions",
+ "pretty_env_logger",
+ "rustls-native-certs",
+ "rustls-pemfile",
+ "rustls-webpki",
+ "serde",
+ "thiserror",
+ "tokio",
+ "tokio-native-tls",
+ "tokio-rustls",
+ "url",
+ "ws_stream_tungstenite",
+]
+
+[[package]]
+name = "rustc-demangle"
+version = "0.1.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
+
+[[package]]
+name = "rustc_version"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
+dependencies = [
+ "semver",
+]
+
+[[package]]
+name = "rustix"
+version = "0.38.31"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949"
+dependencies = [
+ "bitflags 2.4.2",
+ "errno",
+ "libc",
+ "linux-raw-sys",
+ "windows-sys 0.52.0",
+]
+
+[[package]]
+name = "rustls"
+version = "0.22.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e87c9956bd9807afa1f77e0f7594af32566e830e088a5576d27c5b6f30f49d41"
+dependencies = [
+ "log",
+ "ring",
+ "rustls-pki-types",
+ "rustls-webpki",
+ "subtle",
+ "zeroize",
+]
+
+[[package]]
+name = "rustls-native-certs"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f1fb85efa936c42c6d5fc28d2629bb51e4b2f4b8a5211e297d599cc5a093792"
+dependencies = [
+ "openssl-probe",
+ "rustls-pemfile",
+ "rustls-pki-types",
+ "schannel",
+ "security-framework",
+]
+
+[[package]]
+name = "rustls-pemfile"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3c333bb734fcdedcea57de1602543590f545f127dc8b533324318fd492c5c70b"
+dependencies = [
+ "base64 0.21.7",
+ "rustls-pki-types",
+]
+
+[[package]]
+name = "rustls-pki-types"
+version = "1.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5ede67b28608b4c60685c7d54122d4400d90f62b40caee7700e700380a390fa8"
+
+[[package]]
+name = "rustls-webpki"
+version = "0.102.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "faaa0a62740bedb9b2ef5afa303da42764c012f743917351dc9a237ea1663610"
+dependencies = [
+ "ring",
+ "rustls-pki-types",
+ "untrusted",
+]
+
+[[package]]
+name = "schannel"
+version = "0.1.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534"
+dependencies = [
+ "windows-sys 0.52.0",
+]
+
+[[package]]
+name = "scopeguard"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
+
+[[package]]
+name = "security-framework"
+version = "2.9.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de"
+dependencies = [
+ "bitflags 1.3.2",
+ "core-foundation",
+ "core-foundation-sys",
+ "libc",
+ "security-framework-sys",
+]
+
+[[package]]
+name = "security-framework-sys"
+version = "2.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a"
+dependencies = [
+ "core-foundation-sys",
+ "libc",
+]
+
+[[package]]
+name = "semver"
+version = "1.0.22"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca"
+
+[[package]]
+name = "serde"
+version = "1.0.197"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.197"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "sha1"
+version = "0.10.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
+dependencies = [
+ "cfg-if",
+ "cpufeatures",
+ "digest",
+]
+
+[[package]]
+name = "slab"
+version = "0.4.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "socket2"
+version = "0.5.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871"
+dependencies = [
+ "libc",
+ "windows-sys 0.52.0",
+]
+
+[[package]]
+name = "spin"
+version = "0.9.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
+dependencies = [
+ "lock_api",
+]
+
+[[package]]
+name = "subtle"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
+
+[[package]]
+name = "syn"
+version = "2.0.51"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ab617d94515e94ae53b8406c628598680aa0c9587474ecbe58188f7b345d66c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "tempfile"
+version = "3.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1"
+dependencies = [
+ "cfg-if",
+ "fastrand",
+ "rustix",
+ "windows-sys 0.52.0",
+]
+
+[[package]]
+name = "termcolor"
+version = "1.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
+dependencies = [
+ "winapi-util",
+]
+
+[[package]]
+name = "thiserror"
+version = "1.0.57"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b"
+dependencies = [
+ "thiserror-impl",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "1.0.57"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "tinyvec"
+version = "1.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
+dependencies = [
+ "tinyvec_macros",
+]
+
+[[package]]
+name = "tinyvec_macros"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
+
+[[package]]
+name = "tokio"
+version = "1.36.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931"
+dependencies = [
+ "backtrace",
+ "bytes",
+ "libc",
+ "mio",
+ "pin-project-lite",
+ "socket2",
+ "tokio-macros",
+ "windows-sys 0.48.0",
+]
+
+[[package]]
+name = "tokio-macros"
+version = "2.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "tokio-native-tls"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2"
+dependencies = [
+ "native-tls",
+ "tokio",
+]
+
+[[package]]
+name = "tokio-rustls"
+version = "0.25.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f"
+dependencies = [
+ "rustls",
+ "rustls-pki-types",
+ "tokio",
+]
+
+[[package]]
+name = "tracing"
+version = "0.1.40"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
+dependencies = [
+ "pin-project-lite",
+ "tracing-attributes",
+ "tracing-core",
+]
+
+[[package]]
+name = "tracing-attributes"
+version = "0.1.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "tracing-core"
+version = "0.1.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
+dependencies = [
+ "once_cell",
+]
+
+[[package]]
+name = "tungstenite"
+version = "0.21.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1"
+dependencies = [
+ "byteorder",
+ "bytes",
+ "data-encoding",
+ "http",
+ "httparse",
+ "log",
+ "rand",
+ "rustls",
+ "rustls-pki-types",
+ "sha1",
+ "thiserror",
+ "url",
+ "utf-8",
+]
+
+[[package]]
+name = "typenum"
+version = "1.17.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
+
+[[package]]
+name = "unicode-bidi"
+version = "0.3.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75"
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
+
+[[package]]
+name = "unicode-normalization"
+version = "0.1.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5"
+dependencies = [
+ "tinyvec",
+]
+
+[[package]]
+name = "untrusted"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
+
+[[package]]
+name = "url"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633"
+dependencies = [
+ "form_urlencoded",
+ "idna",
+ "percent-encoding",
+]
+
+[[package]]
+name = "utf-8"
+version = "0.7.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
+
+[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
+[[package]]
+name = "version_check"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
+
+[[package]]
+name = "wasi"
+version = "0.11.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-util"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+
+[[package]]
+name = "windows-sys"
+version = "0.48.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
+dependencies = [
+ "windows-targets 0.48.5",
+]
+
+[[package]]
+name = "windows-sys"
+version = "0.52.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
+dependencies = [
+ "windows-targets 0.52.3",
+]
+
+[[package]]
+name = "windows-targets"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
+dependencies = [
+ "windows_aarch64_gnullvm 0.48.5",
+ "windows_aarch64_msvc 0.48.5",
+ "windows_i686_gnu 0.48.5",
+ "windows_i686_msvc 0.48.5",
+ "windows_x86_64_gnu 0.48.5",
+ "windows_x86_64_gnullvm 0.48.5",
+ "windows_x86_64_msvc 0.48.5",
+]
+
+[[package]]
+name = "windows-targets"
+version = "0.52.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d380ba1dc7187569a8a9e91ed34b8ccfc33123bbacb8c0aed2d1ad7f3ef2dc5f"
+dependencies = [
+ "windows_aarch64_gnullvm 0.52.3",
+ "windows_aarch64_msvc 0.52.3",
+ "windows_i686_gnu 0.52.3",
+ "windows_i686_msvc 0.52.3",
+ "windows_x86_64_gnu 0.52.3",
+ "windows_x86_64_gnullvm 0.52.3",
+ "windows_x86_64_msvc 0.52.3",
+]
+
+[[package]]
+name = "windows_aarch64_gnullvm"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
+
+[[package]]
+name = "windows_aarch64_gnullvm"
+version = "0.52.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "68e5dcfb9413f53afd9c8f86e56a7b4d86d9a2fa26090ea2dc9e40fba56c6ec6"
+
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
+
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.52.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8dab469ebbc45798319e69eebf92308e541ce46760b49b18c6b3fe5e8965b30f"
+
+[[package]]
+name = "windows_i686_gnu"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
+
+[[package]]
+name = "windows_i686_gnu"
+version = "0.52.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2a4e9b6a7cac734a8b4138a4e1044eac3404d8326b6c0f939276560687a033fb"
+
+[[package]]
+name = "windows_i686_msvc"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
+
+[[package]]
+name = "windows_i686_msvc"
+version = "0.52.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "28b0ec9c422ca95ff34a78755cfa6ad4a51371da2a5ace67500cf7ca5f232c58"
+
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
+
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.52.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "704131571ba93e89d7cd43482277d6632589b18ecf4468f591fbae0a8b101614"
+
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
+
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.52.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "42079295511643151e98d61c38c0acc444e52dd42ab456f7ccfd5152e8ecf21c"
+
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
+
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.52.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0770833d60a970638e989b3fa9fd2bb1aaadcf88963d1659fd7d9990196ed2d6"
+
+[[package]]
+name = "ws_stream_tungstenite"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a198f414f083fb19fcc1bffcb0fa0cf46d33ccfa229adf248cac12c180e91609"
+dependencies = [
+ "async-tungstenite",
+ "async_io_stream",
+ "bitflags 2.4.2",
+ "futures-core",
+ "futures-io",
+ "futures-sink",
+ "futures-util",
+ "pharos",
+ "rustc_version",
+ "tokio",
+ "tracing",
+ "tungstenite",
+]
+
+[[package]]
+name = "yansi"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
+
+[[package]]
+name = "zeroize"
+version = "1.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d"
diff --git a/vendor/rumqttc/Cargo.toml b/vendor/rumqttc/Cargo.toml
new file mode 100644
index 0000000000..36d0117e1a
--- /dev/null
+++ b/vendor/rumqttc/Cargo.toml
@@ -0,0 +1,179 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies.
+#
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
+
+[package]
+edition = "2021"
+rust-version = "1.64.0"
+name = "rumqttc"
+version = "0.24.0"
+authors = ["tekjar <[email protected]>"]
+publish = true
+description = "An efficient and robust mqtt client for your connected devices"
+readme = "README.md"
+keywords = [
+ "mqtt",
+ "iot",
+ "coap",
+ "http",
+]
+categories = ["network-programming"]
+license = "Apache-2.0"
+repository = "https://github.com/bytebeamio/rumqtt"
+
+[package.metadata.docs.rs]
+all-features = true
+rustdoc-args = [
+ "--cfg",
+ "docsrs",
+]
+
+[[example]]
+name = "tls"
+path = "examples/tls.rs"
+required-features = ["use-rustls"]
+
+[[example]]
+name = "tls2"
+path = "examples/tls2.rs"
+required-features = ["use-rustls"]
+
+[[example]]
+name = "websocket"
+path = "examples/websocket.rs"
+required-features = ["websocket"]
+
+[[example]]
+name = "websocket_proxy"
+path = "examples/websocket_proxy.rs"
+required-features = [
+ "websocket",
+ "proxy",
+]
+
+[dependencies.async-http-proxy]
+version = "1.2.5"
+features = [
+ "runtime-tokio",
+ "basic-auth",
+]
+optional = true
+
+[dependencies.async-tungstenite]
+version = "0.25.0"
+features = ["tokio-rustls-native-certs"]
+optional = true
+default-features = false
+
+[dependencies.bytes]
+version = "1.5"
+
+[dependencies.flume]
+version = "0.11"
+features = ["async"]
+default-features = false
+
+[dependencies.futures-util]
+version = "0.3"
+features = ["std"]
+default_features = false
+
+[dependencies.http]
+version = "1.0.0"
+optional = true
+
+[dependencies.log]
+version = "0.4"
+
+[dependencies.native-tls]
+version = "0.2.11"
+optional = true
+
+[dependencies.rustls-native-certs]
+version = "0.7.0"
+optional = true
+
+[dependencies.rustls-pemfile]
+version = "2.1.0"
+optional = true
+
+[dependencies.rustls-webpki]
+version = "0.102.2"
+optional = true
+
+[dependencies.thiserror]
+version = "1"
+
+[dependencies.tokio]
+version = "1.36"
+features = [
+ "rt",
+ "macros",
+ "io-util",
+ "net",
+ "time",
+]
+
+[dependencies.tokio-native-tls]
+version = "0.3.1"
+optional = true
+
+[dependencies.tokio-rustls]
+version = "0.25.0"
+optional = true
+
+[dependencies.url]
+version = "2"
+optional = true
+default-features = false
+
+[dependencies.ws_stream_tungstenite]
+version = "0.13.0"
+features = ["tokio_io"]
+optional = true
+default-features = false
+
+[dev-dependencies.bincode]
+version = "1.3.3"
+
+[dev-dependencies.color-backtrace]
+version = "0.5"
+
+[dev-dependencies.matches]
+version = "0.1"
+
+[dev-dependencies.pretty_assertions]
+version = "1"
+
+[dev-dependencies.pretty_env_logger]
+version = "0.5"
+
+[dev-dependencies.serde]
+version = "1"
+features = ["derive"]
+
+[features]
+default = ["use-rustls"]
+proxy = ["dep:async-http-proxy"]
+use-native-tls = [
+ "dep:tokio-native-tls",
+ "dep:native-tls",
+]
+use-rustls = [
+ "dep:tokio-rustls",
+ "dep:rustls-webpki",
+ "dep:rustls-pemfile",
+ "dep:rustls-native-certs",
+]
+websocket = [
+ "dep:async-tungstenite",
+ "dep:ws_stream_tungstenite",
+ "dep:http",
+]
diff --git a/vendor/rumqttc/Cargo.toml.orig b/vendor/rumqttc/Cargo.toml.orig
new file mode 100644
index 0000000000..551dcab533
--- /dev/null
+++ b/vendor/rumqttc/Cargo.toml.orig
@@ -0,0 +1,77 @@
+[package]
+name = "rumqttc"
+version = "0.24.0"
+publish = true
+description = "An efficient and robust mqtt client for your connected devices"
+keywords = ["mqtt", "iot", "coap", "http"]
+categories = ["network-programming"]
+rust-version = "1.64.0"
+edition.workspace = true
+repository.workspace = true
+license.workspace = true
+authors.workspace = true
+
+[package.metadata.docs.rs]
+all-features = true
+rustdoc-args = ["--cfg", "docsrs"]
+
+[features]
+default = ["use-rustls"]
+use-rustls = ["dep:tokio-rustls", "dep:rustls-webpki", "dep:rustls-pemfile", "dep:rustls-native-certs"]
+use-native-tls = ["dep:tokio-native-tls", "dep:native-tls"]
+websocket = ["dep:async-tungstenite", "dep:ws_stream_tungstenite", "dep:http"]
+proxy = ["dep:async-http-proxy"]
+
+[dependencies]
+futures-util = { version = "0.3", default_features = false, features = ["std"] }
+tokio = { version = "1.36", features = ["rt", "macros", "io-util", "net", "time"] }
+bytes = "1.5"
+log = "0.4"
+flume = { version = "0.11", default-features = false, features = ["async"] }
+thiserror = "1"
+
+# Optional
+# rustls
+tokio-rustls = { version = "0.25.0", optional = true }
+rustls-webpki = { version = "0.102.2", optional = true }
+rustls-pemfile = { version = "2.1.0", optional = true }
+rustls-native-certs = { version = "0.7.0", optional = true }
+# websockets
+async-tungstenite = { version = "0.25.0", default-features = false, features = ["tokio-rustls-native-certs"], optional = true }
+ws_stream_tungstenite = { version= "0.13.0", default-features = false, features = ["tokio_io"], optional = true }
+http = { version = "1.0.0", optional = true }
+# native-tls
+tokio-native-tls = { version = "0.3.1", optional = true }
+native-tls = { version = "0.2.11", optional = true }
+# url
+url = { version = "2", default-features = false, optional = true }
+# proxy
+async-http-proxy = { version = "1.2.5", features = ["runtime-tokio", "basic-auth"], optional = true }
+
+[dev-dependencies]
+bincode = "1.3.3"
+color-backtrace = "0.5"
+matches = "0.1"
+pretty_assertions = "1"
+pretty_env_logger = "0.5"
+serde = { version = "1", features = ["derive"] }
+
+[[example]]
+name = "tls"
+path = "examples/tls.rs"
+required-features = ["use-rustls"]
+
+[[example]]
+name = "tls2"
+path = "examples/tls2.rs"
+required-features = ["use-rustls"]
+
+[[example]]
+name = "websocket"
+path = "examples/websocket.rs"
+required-features = ["websocket"]
+
+[[example]]
+name = "websocket_proxy"
+path = "examples/websocket_proxy.rs"
+required-features = ["websocket", "proxy"]
diff --git a/vendor/rumqttc/README.md b/vendor/rumqttc/README.md
new file mode 100644
index 0000000000..cde2740d65
--- /dev/null
+++ b/vendor/rumqttc/README.md
@@ -0,0 +1,86 @@
+# rumqttc
+
+[![crates.io page](https://img.shields.io/crates/v/rumqttc.svg)](https://crates.io/crates/rumqttc)
+[![docs.rs page](https://docs.rs/rumqttc/badge.svg)](https://docs.rs/rumqttc)
+
+A pure rust MQTT client which strives to be robust, efficient and easy to use. This library is backed by an async(using tokio) eventloop which enables users to send and receive MQTT messages in correspondence with a broker.
+
+## Examples
+
+A simple synchronous publish and subscribe
+----------------------------
+
+```rust
+use rumqttc::{MqttOptions, Client, QoS};
+use std::time::Duration;
+use std::thread;
+
+let mut mqttoptions = MqttOptions::new("rumqtt-sync", "test.mosquitto.org", 1883);
+mqttoptions.set_keep_alive(Duration::from_secs(5));
+
+let (mut client, mut connection) = Client::new(mqttoptions, 10);
+client.subscribe("hello/rumqtt", QoS::AtMostOnce).unwrap();
+thread::spawn(move || for i in 0..10 {
+ client.publish("hello/rumqtt", QoS::AtLeastOnce, false, vec![i; i as usize]).unwrap();
+ thread::sleep(Duration::from_millis(100));
+});
+
+// Iterate to poll the eventloop for connection progress
+for (i, notification) in connection.iter().enumerate() {
+ println!("Notification = {:?}", notification);
+}
+```
+
+A simple asynchronous publish and subscribe
+------------------------------
+
+```rust
+use rumqttc::{MqttOptions, AsyncClient, QoS};
+use tokio::{task, time};
+use std::time::Duration;
+use std::error::Error;
+
+let mut mqttoptions = MqttOptions::new("rumqtt-async", "test.mosquitto.org", 1883);
+mqttoptions.set_keep_alive(Duration::from_secs(5));
+
+let (mut client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
+client.subscribe("hello/rumqtt", QoS::AtMostOnce).await.unwrap();
+
+task::spawn(async move {
+ for i in 0..10 {
+ client.publish("hello/rumqtt", QoS::AtLeastOnce, false, vec![i; i as usize]).await.unwrap();
+ time::sleep(Duration::from_millis(100)).await;
+ }
+});
+
+while let Ok(notification) = eventloop.poll().await {
+ println!("Received = {:?}", notification);
+}
+```
+
+Quick overview of features
+- Eventloop orchestrates outgoing/incoming packets concurrently and handles the state
+- Pings the broker when necessary and detects client side half open connections as well
+- Throttling of outgoing packets (todo)
+- Queue size based flow control on outgoing packets
+- Automatic reconnections by just continuing the `eventloop.poll()/connection.iter()` loop
+- Natural backpressure to client APIs during bad network
+- Support for WebSockets
+- Secure transport using TLS
+
+In short, everything necessary to maintain a robust connection
+
+Since the eventloop is externally polled (with `iter()/poll()` in a loop)
+out side the library and `Eventloop` is accessible, users can
+- Distribute incoming messages based on topics
+- Stop it when required
+- Access internal state for use cases like graceful shutdown or to modify options before reconnection
+
+### Important notes
+
+- Looping on `connection.iter()`/`eventloop.poll()` is necessary to run the
+ event loop and make progress. It yields incoming and outgoing activity
+ notifications which allows customization as you see fit.
+
+- Blocking inside the `connection.iter()`/`eventloop.poll()` loop will block
+ connection progress.
diff --git a/vendor/rumqttc/certs/generate.sh b/vendor/rumqttc/certs/generate.sh
new file mode 100644
index 0000000000..8d0bf63f8f
--- /dev/null
+++ b/vendor/rumqttc/certs/generate.sh
@@ -0,0 +1,23 @@
+set -ex
+
+if [[ $# -ne 1 ]]; then
+ echo "Pass number of bikes"
+ echo "Usage: ./createvehicle 10"
+ exit
+fi
+
+for i in $(seq 1 $1); do
+ bike_id=bike-$i
+ wget https://pki.google.com/roots.pem
+
+ openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes -out rsa_cert.pem -subj "/CN=unused"
+ openssl rsa -in rsa_private.pem -outform DER -out rsa_private.der
+ # openssl x509 -in roots.pem -outform DER -out roots.der
+
+ gcloud iot devices delete -q $bike_id --region=asia-east1 --registry=iotcore || true
+ gcloud iot devices create $bike_id --region=asia-east1 --registry=iotcore --public-key path=rsa_cert.pem,type=rs256
+
+ mkdir -p $bike_id
+ mv roots.pem rsa_private.pem rsa_private.der $bike_id
+ rm -rf *.pem *.der
+done
diff --git a/vendor/rumqttc/design.md b/vendor/rumqttc/design.md
new file mode 100644
index 0000000000..737f2527a6
--- /dev/null
+++ b/vendor/rumqttc/design.md
@@ -0,0 +1,273 @@
+-[ ] Make core publish take qos with publish
+-[ ] Rust iterator like design
+-[ ] Should the APIs take files directly
+
+This doc is a journal. Make a blog out of this
+
+Primarily designed to efficiently perform streaming (unbounded) mqtt publishes and
+subscriptions in flaky networks.
+
+But the design choices to take a `Stream` for user requests (publishes, subscriptions etc) and the
+eventloop it self being a `Stream` yielding incoming packets to the user makes other use cases easy to implement.
+
+
+With out boring myself and you with only details, let's try to dive in
+with some real world use cases
+
+
+##### Robustness (reconnections and retransmissions)
+------
+
+
+
+##### Pausing the network activity to cooperate with other processes which needs bandwidth
+------
+
+
+
+##### Shutting down the eventloop by saving current state to the disk
+------
+
+
+
+##### Disk aware request stream
+------
+
+
+
+
+##### Bounded requests
+------
+
+
+
+
+
+
+* Automatic reconnections
+* No commands for misc operations like network pause/
+Connection and Automatic reconnections
+-------
+
+Open question: When should event loop start taking control of reconnections? After initial success or should
+we expose options similar to current implementation? what should be the default behavior
+
+
+```
+Reconnect::AfterFirstSuccess
+Reconnect::Always
+Reconnect::Never
+```
+
+Let's do this for starters
+
+```$xslt
+ // create an eventloop after the initial mqtt connection is successful
+ // user will decide if this should be retried or not
+ let eventloop = connect(mqttoptions) -> Result<EventLoop, Error>
+
+ // during intermittent reconnetions due to bad network, eventloop will
+ // behave as per configured reconnection options to the eventloop
+ let stream = eventloop.assemble(reconnection_options, inputs);
+```
+
+Possible reconnection options
+
+```$xslt
+Reconnect::Never
+Reconnect::Automatic
+```
+
+I feel this a good middle ground between manual user control and rumq being very opinionated about
+reconnection behaviour.
+
+But why not leave the intermittent reconnection behaviour as well to the user you ask?
+
+Because maintaini
+state is a much more complicated business than just retrying the connection. We don't want the user to think
+about mqttstate by default. If the user intends for a much more custom behaviour, he/she can use
+`Reconnect::Never` and pass the returned `MqttState` to the next connection.
+
+Takes any stream type as input
+-------
+
+Allows eventloop to be channel implementation agnostic. This opens up interesting usage patterns. Instead of providing
+methods to communicate with the eventloop over a channel, we can directly communicate with th eventloop over the specified stream. Users can customize the stream in any way that fits their usecase. For example we plugin streams which orchestrate data between
+disk and memory.
+
+This strategy prevents resource overhead in cases where user uses a different kind of channel than what rumqtt uses. E.g if
+rumqtt uses futures channnel and user wants to plugin data coming from different kind of stream, user has to make extra copies
+passing data fromn the channel of his choice to futures channel (requires user to run an extra thread which does the handover)
+
+```
+// thread 1
+user_channel_tx.send(data)
+
+// thread 2
+data = user_channel_rx.recv();
+rumqtt_channel_tx.send(data);
+
+// thread 3
+rumqtt_eventloop.start(rumqtt_channel_rx);
+```
+
+vs
+
+```
+// thread 1
+user_channel_tx.send(data)
+
+// thread 2
+rumqtt_eventloop.start(user_channel_rx_stream);
+```
+
+
+Don't spawn any thread from the library
+-------
+
+Provide all the eventloop handling necessary for a robust mqtt connection but don't spawn any inner threads. This choice is
+left to the user. Usecases might vary from running streams with fixed input to channels that keep producing data forever
+
+```
+let stream = vec![publish1, publish2, publish3];
+let eventloop = MqttEventLoop::new(options);
+eventloop.run(stream);
+```
+
+or
+
+```
+let (tx, rx) = channel::bound(10);
+
+thread::spawn(move || loop {
+ tx.send(publish);
+ sleep(1)
+});
+
+let eventloop = MqttEventLoop::new(options);
+eventloop.run(rx);
+```
+
+
+Support both synchronous and asynchronous use cases
+-------
+
+Leverage on the above pattern to support both synchronous and asynchronus publishes with timeouts
+
+```
+let publishes = [p1, p2, p3];
+eventloop.run_timeout(publishes, 10 * Duration::SECS);
+```
+
+Eventloop will wait for all the acks within timeout (where ever necessary) and exits
+
+
+Command channels to configure eventloop dynamically
+-------
+
+reconnections, disconnection, throttle speed, pause/resume (without disconnections)
+
+
+#### shutdown, disconnection, reconnection, pause/resume semantics
+
+there might be no need to implement separate shutdown to stop the eventloop and return existing state
+
+```
+let eventloop = MqttEventloop::new();
+
+thread::spawn(|| {
+ command.shutdown()
+})
+
+enum EventloopStatus {
+ Shutdown(MqttState)
+ // sends disconnect to the server and waits for server server disconnection
+ // should't process any new data from the user channel
+ Disconnect
+}
+
+eventloop.run() //return -> Result<MqttSt>
+```
+
+
+Keep additional functionality like gcloud jwt auth out of rumqtt
+-------
+
+Prevents (some) conflicts w.r.t different versions of ring. conflicts because of rustls are still possible but atleast
+prevents ones w.r.t jsonwebtoken.
+
+Keeps the codebase small which eases some maintainence burden
+
+Keep alive
+-------
+
+Keepalives can be a little tricky
+
+client should keep track of keepalive timeouts for 2 different reasons
+
+* when there is no incoming n/w activity
+
+to detect any halfopen connections to the broker, client should send a pingrequest packet
+and validate next pingreq with previous pingresp.
+if previous ack isn't received, client should consider this as a halfopen connection and
+disconnect. takes 2 keepalive times to detect halfopen connection and disconnect
+
+* to prevent broker from disconnecting the client due to no client activity
+
+broker should receive some packet activity from a client or else it'll assume the
+connection as halfopen and disconnect.
+for this reason, even though the client is receiving some incoming packets(qos0 publishes)
+client should timeout when there is no outgoing packet activity and send a ping request
+
+
+We would require timeouts on network incoming packets as well as network outgoing packets. concurrently
+
+so we need to create 2 streams with timeouts, one on n/w incoming packets and another on n/w outgoing packets and select
+
+```rust
+let incoming_stream = stream!(tcp.read_mqtt().timeout(NetworkTimeout))
+
+This captures incoming stream timeout
+
+Caputuring timeout on outgoing packects (replys due to incoming packets + user requests) is tricky because replys
+are a sideeffect of processing incoming packets which generate notifications for the user as well.
+To have a timeout on a combination of reply and requests, we need to filter out notifications (or put it into other stream)
+which beats our design of returning one eventloop stream to the user to handle full mqtt io
+
+
+let mqtt_stream = stream! {
+ loop {
+ select! {
+ (notification, reply) = incoming_stream.next().handle_incoming_packet(),
+ (request) = requests.next()
+ }
+ yield reply
+ }
+}
+```
+
+So there is no way to separate notifications from reply without creating a duplicate stream
+
+Option 2 is to timeout on user requests alone. This will lead to sending unnecessary pingreqests after keep alive time
+even when there is outgoing network activity due to network replys. But this will let us have one simple
+stream to poll. Returning 2 streams might not be intuitive to the users. main eventloop progress can stop when the user
+doensn't poll the notification stream (tx.send will block which eventually blocks all incoming packets). Having a second stream also results in more allocations in the hotpath (which might not be a big deal but not ideal)
+Option2 is also considerable less codebase and hence easy maintainence.
+
+Keep alive take 2
+
+Ok, creating independent timeout streams on network and request streams makes keep alive complicated. Both the streams mutating a common timeout is the solution
+
+* When there is an incoming packet which replys the network with some packet, reset the timer and don't generate `PingReq`
+* When there are no incoming and outgoing packets, timeout, hence generate `PingReq`. Reset the delay
+* In a keep alive window, when there is an incoming packet (which doesn't trigger a reply), mark it
+* When there is an outgoing request in this window with incoming already marked. Reset the timer.
+* If there is no outgoing request in this window, timeout, hence generate `PingReq`. Reset the delay and markers
+
+
+Timeout for packets which are not acked
+------------------
+
+Implement timeout for unacked packets and drop/republish them. Useful if the
+broker is buggy. But might not be too important as unacked messages are
+anyway retried during reconnection
diff --git a/vendor/rumqttc/examples/async_manual_acks.rs b/vendor/rumqttc/examples/async_manual_acks.rs
new file mode 100644
index 0000000000..e5360aa047
--- /dev/null
+++ b/vendor/rumqttc/examples/async_manual_acks.rs
@@ -0,0 +1,86 @@
+use tokio::{task, time};
+
+use rumqttc::{AsyncClient, Event, EventLoop, Incoming, MqttOptions, QoS};
+use std::error::Error;
+use std::time::Duration;
+
+fn create_conn() -> (AsyncClient, EventLoop) {
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
+ mqttoptions
+ .set_keep_alive(Duration::from_secs(5))
+ .set_manual_acks(true)
+ .set_clean_session(false);
+
+ AsyncClient::new(mqttoptions, 10)
+}
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error>> {
+ pretty_env_logger::init();
+
+ // create mqtt connection with clean_session = false and manual_acks = true
+ let (client, mut eventloop) = create_conn();
+
+ // subscribe example topic
+ client
+ .subscribe("hello/world", QoS::AtLeastOnce)
+ .await
+ .unwrap();
+
+ task::spawn(async move {
+ // send some messages to example topic and disconnect
+ requests(client.clone()).await;
+ client.disconnect().await.unwrap();
+ });
+
+ loop {
+ // get subscribed messages without acking
+ let event = eventloop.poll().await;
+ match &event {
+ Ok(notif) => {
+ println!("Event = {notif:?}");
+ }
+ Err(error) => {
+ println!("Error = {error:?}");
+ break;
+ }
+ }
+ }
+
+ // create new broker connection but do not start a clean session
+ let (client, mut eventloop) = create_conn();
+
+ loop {
+ // previously published messages should be republished after reconnection.
+ let event = eventloop.poll().await;
+ match &event {
+ Ok(notif) => {
+ println!("Event = {notif:?}");
+ }
+ Err(error) => {
+ println!("Error = {error:?}");
+ return Ok(());
+ }
+ }
+
+ if let Ok(Event::Incoming(Incoming::Publish(publish))) = event {
+ // this time we will ack incoming publishes.
+ // Its important not to block eventloop as this can cause deadlock.
+ let c = client.clone();
+ tokio::spawn(async move {
+ c.ack(&publish).await.unwrap();
+ });
+ }
+ }
+}
+
+async fn requests(client: AsyncClient) {
+ for i in 1..=10 {
+ client
+ .publish("hello/world", QoS::AtLeastOnce, false, vec![1; i])
+ .await
+ .unwrap();
+
+ time::sleep(Duration::from_secs(1)).await;
+ }
+}
diff --git a/vendor/rumqttc/examples/async_manual_acks_v5.rs b/vendor/rumqttc/examples/async_manual_acks_v5.rs
new file mode 100644
index 0000000000..bcf1bf3569
--- /dev/null
+++ b/vendor/rumqttc/examples/async_manual_acks_v5.rs
@@ -0,0 +1,85 @@
+use rumqttc::v5::mqttbytes::v5::Packet;
+use rumqttc::v5::mqttbytes::QoS;
+use tokio::{task, time};
+
+use rumqttc::v5::{AsyncClient, Event, EventLoop, MqttOptions};
+use std::error::Error;
+use std::time::Duration;
+
+fn create_conn() -> (AsyncClient, EventLoop) {
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1884);
+ mqttoptions
+ .set_keep_alive(Duration::from_secs(5))
+ .set_manual_acks(true)
+ .set_clean_start(false);
+
+ AsyncClient::new(mqttoptions, 10)
+}
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error>> {
+ // todo!("fix this example with new way of spawning clients")
+ pretty_env_logger::init();
+
+ // create mqtt connection with clean_session = false and manual_acks = true
+ let (client, mut eventloop) = create_conn();
+
+ // subscribe example topic
+ client
+ .subscribe("hello/world", QoS::AtLeastOnce)
+ .await
+ .unwrap();
+
+ task::spawn(async move {
+ // send some messages to example topic and disconnect
+ requests(&client).await;
+ client.disconnect().await.unwrap()
+ });
+
+ // get subscribed messages without acking
+ loop {
+ let event = eventloop.poll().await;
+ match &event {
+ Ok(v) => {
+ println!("Event = {v:?}");
+ }
+ Err(e) => {
+ println!("Error = {e:?}");
+ break;
+ }
+ }
+ }
+
+ // create new broker connection
+ let (client, mut eventloop) = create_conn();
+
+ while let Ok(event) = eventloop.poll().await {
+ println!("{event:?}");
+
+ if let Event::Incoming(packet) = event {
+ let publish = match packet {
+ Packet::Publish(publish) => publish,
+ _ => continue,
+ };
+ // this time we will ack incoming publishes.
+ // Its important not to block notifier as this can cause deadlock.
+ let c = client.clone();
+ tokio::spawn(async move {
+ c.ack(&publish).await.unwrap();
+ });
+ }
+ }
+
+ Ok(())
+}
+
+async fn requests(client: &AsyncClient) {
+ for i in 1..=10 {
+ client
+ .publish("hello/world", QoS::AtLeastOnce, false, vec![1; i])
+ .await
+ .unwrap();
+
+ time::sleep(Duration::from_secs(1)).await;
+ }
+}
diff --git a/vendor/rumqttc/examples/asyncpubsub.rs b/vendor/rumqttc/examples/asyncpubsub.rs
new file mode 100644
index 0000000000..d4d70d3ff6
--- /dev/null
+++ b/vendor/rumqttc/examples/asyncpubsub.rs
@@ -0,0 +1,51 @@
+use tokio::{task, time};
+
+use rumqttc::{AsyncClient, MqttOptions, QoS};
+use std::error::Error;
+use std::time::Duration;
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error>> {
+ pretty_env_logger::init();
+ // color_backtrace::install();
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
+ mqttoptions.set_keep_alive(Duration::from_secs(5));
+
+ let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
+ task::spawn(async move {
+ requests(client).await;
+ time::sleep(Duration::from_secs(3)).await;
+ });
+
+ loop {
+ let event = eventloop.poll().await;
+ match &event {
+ Ok(v) => {
+ println!("Event = {v:?}");
+ }
+ Err(e) => {
+ println!("Error = {e:?}");
+ return Ok(());
+ }
+ }
+ }
+}
+
+async fn requests(client: AsyncClient) {
+ client
+ .subscribe("hello/world", QoS::AtMostOnce)
+ .await
+ .unwrap();
+
+ for i in 1..=10 {
+ client
+ .publish("hello/world", QoS::ExactlyOnce, false, vec![1; i])
+ .await
+ .unwrap();
+
+ time::sleep(Duration::from_secs(1)).await;
+ }
+
+ time::sleep(Duration::from_secs(120)).await;
+}
diff --git a/vendor/rumqttc/examples/asyncpubsub_v5.rs b/vendor/rumqttc/examples/asyncpubsub_v5.rs
new file mode 100644
index 0000000000..fd53b53fd2
--- /dev/null
+++ b/vendor/rumqttc/examples/asyncpubsub_v5.rs
@@ -0,0 +1,52 @@
+use rumqttc::v5::mqttbytes::QoS;
+use tokio::{task, time};
+
+use rumqttc::v5::{AsyncClient, MqttOptions};
+use std::error::Error;
+use std::time::Duration;
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error>> {
+ pretty_env_logger::init();
+ // color_backtrace::install();
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1884);
+ mqttoptions.set_keep_alive(Duration::from_secs(5));
+
+ let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
+ task::spawn(async move {
+ requests(client).await;
+ time::sleep(Duration::from_secs(3)).await;
+ });
+
+ loop {
+ let event = eventloop.poll().await;
+ match &event {
+ Ok(v) => {
+ println!("Event = {v:?}");
+ }
+ Err(e) => {
+ println!("Error = {e:?}");
+ return Ok(());
+ }
+ }
+ }
+}
+
+async fn requests(client: AsyncClient) {
+ client
+ .subscribe("hello/world", QoS::AtMostOnce)
+ .await
+ .unwrap();
+
+ for i in 1..=10 {
+ client
+ .publish("hello/world", QoS::ExactlyOnce, false, vec![1; i])
+ .await
+ .unwrap();
+
+ time::sleep(Duration::from_secs(1)).await;
+ }
+
+ time::sleep(Duration::from_secs(120)).await;
+}
diff --git a/vendor/rumqttc/examples/serde.rs b/vendor/rumqttc/examples/serde.rs
new file mode 100644
index 0000000000..478a88d565
--- /dev/null
+++ b/vendor/rumqttc/examples/serde.rs
@@ -0,0 +1,62 @@
+use bincode::ErrorKind;
+use rumqttc::{Client, Event, Incoming, MqttOptions, QoS};
+use serde::{Deserialize, Serialize};
+use std::convert::TryFrom;
+use std::thread;
+use std::time::{Duration, SystemTime};
+
+#[derive(Serialize, Deserialize, Debug)]
+struct Message {
+ i: usize,
+ time: SystemTime,
+}
+
+impl From<&Message> for Vec<u8> {
+ fn from(value: &Message) -> Self {
+ bincode::serialize(value).unwrap()
+ }
+}
+
+impl From<Message> for Vec<u8> {
+ fn from(value: Message) -> Self {
+ bincode::serialize(&value).unwrap()
+ }
+}
+
+impl TryFrom<&[u8]> for Message {
+ type Error = Box<ErrorKind>;
+
+ fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
+ bincode::deserialize(value)
+ }
+}
+
+fn main() {
+ let mqqt_opts = MqttOptions::new("test-1", "localhost", 1883);
+
+ let (client, mut connection) = Client::new(mqqt_opts, 10);
+ client.subscribe("hello/rumqtt", QoS::AtMostOnce).unwrap();
+ thread::spawn(move || {
+ for i in 0..10 {
+ let message = Message {
+ i,
+ time: SystemTime::now(),
+ };
+
+ client
+ .publish("hello/rumqtt", QoS::AtLeastOnce, false, message)
+ .unwrap();
+ thread::sleep(Duration::from_millis(100));
+ }
+ });
+
+ // Iterate to poll the eventloop for connection progress
+ for notification in connection.iter().flatten() {
+ if let Event::Incoming(Incoming::Publish(packet)) = notification {
+ match Message::try_from(packet.payload.as_ref()) {
+ Ok(message) => println!("Payload = {message:?}"),
+ Err(error) => println!("Error = {error}"),
+ }
+ }
+ }
+}
diff --git a/vendor/rumqttc/examples/subscription_ids.rs b/vendor/rumqttc/examples/subscription_ids.rs
new file mode 100644
index 0000000000..b741bfaf71
--- /dev/null
+++ b/vendor/rumqttc/examples/subscription_ids.rs
@@ -0,0 +1,82 @@
+use rumqttc::v5::mqttbytes::v5::SubscribeProperties;
+use rumqttc::v5::mqttbytes::QoS;
+use tokio::{task, time};
+
+use rumqttc::v5::{AsyncClient, MqttOptions};
+use std::error::Error;
+use std::time::Duration;
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error>> {
+ pretty_env_logger::init();
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1884);
+ mqttoptions.set_keep_alive(Duration::from_secs(5));
+
+ let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
+ task::spawn(async move {
+ requests(client).await;
+ time::sleep(Duration::from_secs(3)).await;
+ });
+
+ while let Ok(event) = eventloop.poll().await {
+ println!("{:?}", event);
+ }
+
+ Ok(())
+}
+
+async fn requests(client: AsyncClient) {
+ let props = SubscribeProperties {
+ id: Some(1),
+ user_properties: vec![],
+ };
+
+ client
+ .subscribe_with_properties("hello/world", QoS::AtMostOnce, props)
+ .await
+ .unwrap();
+
+ let props = SubscribeProperties {
+ id: Some(2),
+ user_properties: vec![],
+ };
+
+ client
+ .subscribe_with_properties("hello/#", QoS::AtMostOnce, props)
+ .await
+ .unwrap();
+
+ time::sleep(Duration::from_millis(500)).await;
+ // we will receive two publishes
+ // one due to hello/world and other due to hello/#
+ // both will have respective subscription ids
+ client
+ .publish(
+ "hello/world",
+ QoS::AtMostOnce,
+ false,
+ "both having subscription IDs!",
+ )
+ .await
+ .unwrap();
+
+ time::sleep(Duration::from_millis(500)).await;
+ client.unsubscribe("hello/#").await.unwrap();
+ client.subscribe("hello/#", QoS::AtMostOnce).await.unwrap();
+ time::sleep(Duration::from_millis(500)).await;
+
+ // we will receive two publishes
+ // but only one will have subscription ID
+ // cuz we unsubscribed to hello/# and then
+ // subscribed without properties!
+ client
+ .publish(
+ "hello/world",
+ QoS::AtMostOnce,
+ false,
+ "Only one with subscription ID!",
+ )
+ .await
+ .unwrap();
+}
diff --git a/vendor/rumqttc/examples/syncpubsub.rs b/vendor/rumqttc/examples/syncpubsub.rs
new file mode 100644
index 0000000000..5c42ad43c4
--- /dev/null
+++ b/vendor/rumqttc/examples/syncpubsub.rs
@@ -0,0 +1,44 @@
+use rumqttc::{Client, LastWill, MqttOptions, QoS};
+use std::thread;
+use std::time::Duration;
+
+fn main() {
+ pretty_env_logger::init();
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
+ let will = LastWill::new("hello/world", "good bye", QoS::AtMostOnce, false);
+ mqttoptions
+ .set_keep_alive(Duration::from_secs(5))
+ .set_last_will(will);
+
+ let (client, mut connection) = Client::new(mqttoptions, 10);
+ thread::spawn(move || publish(client));
+
+ for (i, notification) in connection.iter().enumerate() {
+ match notification {
+ Ok(notif) => {
+ println!("{i}. Notification = {notif:?}");
+ }
+ Err(error) => {
+ println!("{i}. Notification = {error:?}");
+ return;
+ }
+ }
+ }
+
+ println!("Done with the stream!!");
+}
+
+fn publish(client: Client) {
+ thread::sleep(Duration::from_secs(1));
+ client.subscribe("hello/+/world", QoS::AtMostOnce).unwrap();
+ for i in 0..10_usize {
+ let payload = vec![1; i];
+ let topic = format!("hello/{i}/world");
+ let qos = QoS::AtLeastOnce;
+
+ client.publish(topic, qos, true, payload).unwrap();
+ }
+
+ thread::sleep(Duration::from_secs(1));
+}
diff --git a/vendor/rumqttc/examples/syncpubsub_v5.rs b/vendor/rumqttc/examples/syncpubsub_v5.rs
new file mode 100644
index 0000000000..cc8632d42d
--- /dev/null
+++ b/vendor/rumqttc/examples/syncpubsub_v5.rs
@@ -0,0 +1,45 @@
+use rumqttc::v5::mqttbytes::{v5::LastWill, QoS};
+use rumqttc::v5::{Client, ConnectionError, MqttOptions};
+use std::thread;
+use std::time::Duration;
+
+fn main() {
+ pretty_env_logger::init();
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1884);
+ let will = LastWill::new("hello/world", "good bye", QoS::AtMostOnce, false, None);
+ mqttoptions
+ .set_keep_alive(Duration::from_secs(5))
+ .set_last_will(will);
+
+ let (client, mut connection) = Client::new(mqttoptions, 10);
+ thread::spawn(move || publish(client));
+
+ for (i, notification) in connection.iter().enumerate() {
+ match notification {
+ Err(ConnectionError::Io(error))
+ if error.kind() == std::io::ErrorKind::ConnectionRefused =>
+ {
+ println!("Failed to connect to the server. Make sure correct client is configured properly!\nError: {error:?}");
+ return;
+ }
+ _ => {}
+ }
+ println!("{i}. Notification = {notification:?}");
+ }
+
+ println!("Done with the stream!!");
+}
+
+fn publish(client: Client) {
+ client.subscribe("hello/+/world", QoS::AtMostOnce).unwrap();
+ for i in 0..10_usize {
+ let payload = vec![1; i];
+ let topic = format!("hello/{i}/world");
+ let qos = QoS::AtLeastOnce;
+
+ let _ = client.publish(topic, qos, true, payload);
+ }
+
+ thread::sleep(Duration::from_secs(1));
+}
diff --git a/vendor/rumqttc/examples/syncrecv.rs b/vendor/rumqttc/examples/syncrecv.rs
new file mode 100644
index 0000000000..f0da81775e
--- /dev/null
+++ b/vendor/rumqttc/examples/syncrecv.rs
@@ -0,0 +1,41 @@
+use rumqttc::{Client, LastWill, MqttOptions, QoS};
+use std::thread;
+use std::time::Duration;
+
+fn main() {
+ pretty_env_logger::init();
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
+ let will = LastWill::new("hello/world", "good bye", QoS::AtMostOnce, false);
+ mqttoptions
+ .set_keep_alive(Duration::from_secs(5))
+ .set_last_will(will);
+
+ let (client, mut connection) = Client::new(mqttoptions, 10);
+ thread::spawn(move || publish(client));
+
+ if let Ok(notification) = connection.recv() {
+ println!("Notification = {notification:?}");
+ }
+
+ if let Ok(notification) = connection.try_recv() {
+ println!("Notification = {notification:?}");
+ }
+
+ if let Ok(notification) = connection.recv_timeout(Duration::from_secs(10)) {
+ println!("Notification = {notification:?}");
+ }
+}
+
+fn publish(client: Client) {
+ client.subscribe("hello/+/world", QoS::AtMostOnce).unwrap();
+ for i in 0..3 {
+ let payload = vec![1; i];
+ let topic = format!("hello/{i}/world");
+ let qos = QoS::AtLeastOnce;
+
+ client.publish(topic, qos, true, payload).unwrap();
+ }
+
+ thread::sleep(Duration::from_secs(1));
+}
diff --git a/vendor/rumqttc/examples/syncrecv_v5.rs b/vendor/rumqttc/examples/syncrecv_v5.rs
new file mode 100644
index 0000000000..7de064a573
--- /dev/null
+++ b/vendor/rumqttc/examples/syncrecv_v5.rs
@@ -0,0 +1,42 @@
+use rumqttc::v5::mqttbytes::{v5::LastWill, QoS};
+use rumqttc::v5::{Client, MqttOptions};
+use std::thread;
+use std::time::Duration;
+
+fn main() {
+ pretty_env_logger::init();
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1884);
+ let will = LastWill::new("hello/world", "good bye", QoS::AtMostOnce, false, None);
+ mqttoptions
+ .set_keep_alive(Duration::from_secs(5))
+ .set_last_will(will);
+
+ let (client, mut connection) = Client::new(mqttoptions, 10);
+ thread::spawn(move || publish(client));
+
+ if let Ok(notification) = connection.recv() {
+ println!("Notification = {notification:?}");
+ }
+
+ if let Ok(notification) = connection.try_recv() {
+ println!("Notification = {notification:?}");
+ }
+
+ if let Ok(notification) = connection.recv_timeout(Duration::from_secs(10)) {
+ println!("Notification = {notification:?}");
+ }
+}
+
+fn publish(client: Client) {
+ client.subscribe("hello/+/world", QoS::AtMostOnce).unwrap();
+ for i in 0..3 {
+ let payload = vec![1; i];
+ let topic = format!("hello/{i}/world");
+ let qos = QoS::AtLeastOnce;
+
+ client.publish(topic, qos, true, payload).unwrap();
+ }
+
+ thread::sleep(Duration::from_secs(1));
+}
diff --git a/vendor/rumqttc/examples/tls.rs b/vendor/rumqttc/examples/tls.rs
new file mode 100644
index 0000000000..dc08636f1e
--- /dev/null
+++ b/vendor/rumqttc/examples/tls.rs
@@ -0,0 +1,45 @@
+//! Example of how to configure rumqttd to connect to a server using TLS and authentication.
+use std::error::Error;
+
+use rumqttc::{AsyncClient, Event, Incoming, MqttOptions, Transport};
+use tokio_rustls::rustls::ClientConfig;
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error>> {
+ pretty_env_logger::init();
+ color_backtrace::install();
+
+ let mut mqttoptions = MqttOptions::new("test-1", "mqtt.example.server", 8883);
+ mqttoptions.set_keep_alive(std::time::Duration::from_secs(5));
+ mqttoptions.set_credentials("username", "password");
+
+ // Use rustls-native-certs to load root certificates from the operating system.
+ let mut root_cert_store = tokio_rustls::rustls::RootCertStore::empty();
+ root_cert_store.add_parsable_certificates(
+ rustls_native_certs::load_native_certs().expect("could not load platform certs"),
+ );
+
+ let client_config = ClientConfig::builder()
+ .with_root_certificates(root_cert_store)
+ .with_no_client_auth();
+
+ mqttoptions.set_transport(Transport::tls_with_config(client_config.into()));
+
+ let (_client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
+
+ loop {
+ match eventloop.poll().await {
+ Ok(Event::Incoming(Incoming::Publish(p))) => {
+ println!("Topic: {}, Payload: {:?}", p.topic, p.payload);
+ }
+ Ok(Event::Incoming(i)) => {
+ println!("Incoming = {i:?}");
+ }
+ Ok(Event::Outgoing(o)) => println!("Outgoing = {o:?}"),
+ Err(e) => {
+ println!("Error = {e:?}");
+ return Ok(());
+ }
+ }
+ }
+}
diff --git a/vendor/rumqttc/examples/tls2.rs b/vendor/rumqttc/examples/tls2.rs
new file mode 100644
index 0000000000..c088adb097
--- /dev/null
+++ b/vendor/rumqttc/examples/tls2.rs
@@ -0,0 +1,45 @@
+//! Example of how to configure rumqttd to connect to a server using TLS and authentication.
+use std::error::Error;
+
+use rumqttc::{AsyncClient, MqttOptions, TlsConfiguration, Transport};
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error>> {
+ pretty_env_logger::init();
+ color_backtrace::install();
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 8883);
+ mqttoptions.set_keep_alive(std::time::Duration::from_secs(5));
+
+ // Dummies to prevent compilation error in CI
+ let ca = vec![1, 2, 3];
+ let client_cert = vec![1, 2, 3];
+ let client_key = vec![1, 2, 3];
+ // let ca = include_bytes!("/home/tekjar/tlsfiles/ca.cert.pem");
+ // let client_cert = include_bytes!("/home/tekjar/tlsfiles/device-1.cert.pem");
+ // let client_key = include_bytes!("/home/tekjar/tlsfiles/device-1.key.pem");
+
+ let transport = Transport::Tls(TlsConfiguration::Simple {
+ ca,
+ alpn: None,
+ client_auth: Some((client_cert, client_key)),
+ });
+
+ mqttoptions.set_transport(transport);
+
+ let (_client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
+
+ loop {
+ match eventloop.poll().await {
+ Ok(v) => {
+ println!("Event = {v:?}");
+ }
+ Err(e) => {
+ println!("Error = {e:?}");
+ break;
+ }
+ }
+ }
+
+ Ok(())
+}
diff --git a/vendor/rumqttc/examples/topic_alias.rs b/vendor/rumqttc/examples/topic_alias.rs
new file mode 100644
index 0000000000..394ad3cd53
--- /dev/null
+++ b/vendor/rumqttc/examples/topic_alias.rs
@@ -0,0 +1,88 @@
+use rumqttc::v5::mqttbytes::{v5::PublishProperties, QoS};
+use tokio::{task, time};
+
+use rumqttc::v5::{AsyncClient, MqttOptions};
+use std::error::Error;
+use std::time::Duration;
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error>> {
+ pretty_env_logger::init();
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1884);
+ mqttoptions.set_keep_alive(Duration::from_secs(5));
+ mqttoptions.set_topic_alias_max(10.into());
+
+ let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
+ task::spawn(async move {
+ requests(client).await;
+ time::sleep(Duration::from_secs(3)).await;
+ });
+
+ while let Ok(event) = eventloop.poll().await {
+ println!("{:?}", event);
+ }
+
+ Ok(())
+}
+
+async fn requests(client: AsyncClient) {
+ client
+ .subscribe("hello/world", QoS::AtMostOnce)
+ .await
+ .unwrap();
+
+ client
+ .subscribe("bye/world", QoS::AtMostOnce)
+ .await
+ .unwrap();
+
+ let props = PublishProperties {
+ topic_alias: Some(28),
+ ..Default::default()
+ };
+
+ client
+ .publish_with_properties(
+ "hello/world",
+ QoS::AtMostOnce,
+ false,
+ vec![3; 3],
+ props.clone(),
+ )
+ .await
+ .unwrap();
+
+ let mut other_props = props.clone();
+ other_props.topic_alias = Some(51);
+
+ client
+ .publish_with_properties(
+ "bye/world",
+ QoS::AtMostOnce,
+ false,
+ vec![3; 3],
+ other_props.clone(),
+ )
+ .await
+ .unwrap();
+
+ for i in 1..=10 {
+ // alternately choose the properties
+ let properties = if i % 2 == 0 {
+ other_props.clone()
+ } else {
+ props.clone()
+ };
+
+ // no need to specify topic as we are using topic alias
+ client
+ .publish_with_properties("", QoS::AtMostOnce, false, vec![1; i], properties)
+ .await
+ .unwrap();
+
+ time::sleep(Duration::from_secs(1)).await;
+ }
+
+ time::sleep(Duration::from_secs(120)).await;
+}
diff --git a/vendor/rumqttc/examples/websocket.rs b/vendor/rumqttc/examples/websocket.rs
new file mode 100644
index 0000000000..8aceb5bfd0
--- /dev/null
+++ b/vendor/rumqttc/examples/websocket.rs
@@ -0,0 +1,54 @@
+use rumqttc::{AsyncClient, MqttOptions, QoS, Transport};
+use std::{error::Error, time::Duration};
+use tokio::{task, time};
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error>> {
+ pretty_env_logger::init();
+
+ // port parameter is ignored when scheme is websocket
+ let mut mqttoptions = MqttOptions::new(
+ "clientId-aSziq39Bp3",
+ "ws://broker.mqttdashboard.com:8000/mqtt",
+ 8000,
+ );
+ mqttoptions.set_transport(Transport::Ws);
+ mqttoptions.set_keep_alive(Duration::from_secs(60));
+
+ let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
+ task::spawn(async move {
+ requests(client).await;
+ time::sleep(Duration::from_secs(3)).await;
+ });
+
+ loop {
+ let event = eventloop.poll().await;
+ match event {
+ Ok(notif) => {
+ println!("Event = {notif:?}");
+ }
+ Err(err) => {
+ println!("Error = {err:?}");
+ return Ok(());
+ }
+ }
+ }
+}
+
+async fn requests(client: AsyncClient) {
+ client
+ .subscribe("hello/world", QoS::AtMostOnce)
+ .await
+ .unwrap();
+
+ for i in 1..=10 {
+ client
+ .publish("hello/world", QoS::ExactlyOnce, false, vec![1; i])
+ .await
+ .unwrap();
+
+ time::sleep(Duration::from_secs(1)).await;
+ }
+
+ time::sleep(Duration::from_secs(120)).await;
+}
diff --git a/vendor/rumqttc/examples/websocket_proxy.rs b/vendor/rumqttc/examples/websocket_proxy.rs
new file mode 100644
index 0000000000..42c12353e4
--- /dev/null
+++ b/vendor/rumqttc/examples/websocket_proxy.rs
@@ -0,0 +1,63 @@
+use rumqttc::{AsyncClient, Proxy, ProxyAuth, ProxyType, QoS, Transport};
+use std::{error::Error, time::Duration};
+use tokio::{task, time};
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error>> {
+ use rumqttc::MqttOptions;
+
+ pretty_env_logger::init();
+
+ // port parameter is ignored when scheme is websocket
+ let mut mqttoptions = MqttOptions::new(
+ "clientId-aSziq39Bp3",
+ "ws://broker.mqttdashboard.com:8000/mqtt",
+ 8000,
+ );
+ mqttoptions.set_transport(Transport::Ws);
+ mqttoptions.set_keep_alive(Duration::from_secs(60));
+ // Presumes that there is a proxy server already set up listening on 127.0.0.1:8100
+ mqttoptions.set_proxy(Proxy {
+ ty: ProxyType::Http,
+ auth: ProxyAuth::None,
+ addr: "127.0.0.1".into(),
+ port: 8100,
+ });
+
+ let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
+ task::spawn(async move {
+ requests(client).await;
+ time::sleep(Duration::from_secs(3)).await;
+ });
+
+ loop {
+ let event = eventloop.poll().await;
+ match event {
+ Ok(notif) => {
+ println!("Event = {notif:?}");
+ }
+ Err(err) => {
+ println!("Error = {err:?}");
+ return Ok(());
+ }
+ }
+ }
+}
+
+async fn requests(client: AsyncClient) {
+ client
+ .subscribe("hello/world", QoS::AtMostOnce)
+ .await
+ .unwrap();
+
+ for i in 1..=10 {
+ client
+ .publish("hello/world", QoS::ExactlyOnce, false, vec![1; i])
+ .await
+ .unwrap();
+
+ time::sleep(Duration::from_secs(1)).await;
+ }
+
+ time::sleep(Duration::from_secs(120)).await;
+}
diff --git a/vendor/rumqttc/src/client.rs b/vendor/rumqttc/src/client.rs
new file mode 100644
index 0000000000..863b73191e
--- /dev/null
+++ b/vendor/rumqttc/src/client.rs
@@ -0,0 +1,523 @@
+//! This module offers a high level synchronous and asynchronous abstraction to
+//! async eventloop.
+use std::time::Duration;
+
+use crate::mqttbytes::{v4::*, QoS};
+use crate::{valid_topic, ConnectionError, Event, EventLoop, MqttOptions, Request};
+
+use bytes::Bytes;
+use flume::{SendError, Sender, TrySendError};
+use futures_util::FutureExt;
+use tokio::runtime::{self, Runtime};
+use tokio::time::timeout;
+
+/// Client Error
+#[derive(Debug, thiserror::Error)]
+pub enum ClientError {
+ #[error("Failed to send mqtt requests to eventloop")]
+ Request(Request),
+ #[error("Failed to send mqtt requests to eventloop")]
+ TryRequest(Request),
+}
+
+impl From<SendError<Request>> for ClientError {
+ fn from(e: SendError<Request>) -> Self {
+ Self::Request(e.into_inner())
+ }
+}
+
+impl From<TrySendError<Request>> for ClientError {
+ fn from(e: TrySendError<Request>) -> Self {
+ Self::TryRequest(e.into_inner())
+ }
+}
+
+/// An asynchronous client, communicates with MQTT `EventLoop`.
+///
+/// This is cloneable and can be used to asynchronously [`publish`](`AsyncClient::publish`),
+/// [`subscribe`](`AsyncClient::subscribe`) through the `EventLoop`, which is to be polled parallelly.
+///
+/// **NOTE**: The `EventLoop` must be regularly polled in order to send, receive and process packets
+/// from the broker, i.e. move ahead.
+#[derive(Clone, Debug)]
+pub struct AsyncClient {
+ request_tx: Sender<Request>,
+}
+
+impl AsyncClient {
+ /// Create a new `AsyncClient`.
+ ///
+ /// `cap` specifies the capacity of the bounded async channel.
+ pub fn new(options: MqttOptions, cap: usize) -> (AsyncClient, EventLoop) {
+ let eventloop = EventLoop::new(options, cap);
+ let request_tx = eventloop.requests_tx.clone();
+
+ let client = AsyncClient { request_tx };
+
+ (client, eventloop)
+ }
+
+ /// Create a new `AsyncClient` from a channel `Sender`.
+ ///
+ /// This is mostly useful for creating a test instance where you can
+ /// listen on the corresponding receiver.
+ pub fn from_senders(request_tx: Sender<Request>) -> AsyncClient {
+ AsyncClient { request_tx }
+ }
+
+ /// Sends a MQTT Publish to the `EventLoop`.
+ pub async fn publish<S, V>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: V,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ V: Into<Vec<u8>>,
+ {
+ let topic = topic.into();
+ let mut publish = Publish::new(&topic, qos, payload);
+ publish.retain = retain;
+ let publish = Request::Publish(publish);
+ if !valid_topic(&topic) {
+ return Err(ClientError::Request(publish));
+ }
+ self.request_tx.send_async(publish).await?;
+ Ok(())
+ }
+
+ /// Attempts to send a MQTT Publish to the `EventLoop`.
+ pub fn try_publish<S, V>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: V,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ V: Into<Vec<u8>>,
+ {
+ let topic = topic.into();
+ let mut publish = Publish::new(&topic, qos, payload);
+ publish.retain = retain;
+ let publish = Request::Publish(publish);
+ if !valid_topic(&topic) {
+ return Err(ClientError::TryRequest(publish));
+ }
+ self.request_tx.try_send(publish)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT PubAck to the `EventLoop`. Only needed in if `manual_acks` flag is set.
+ pub async fn ack(&self, publish: &Publish) -> Result<(), ClientError> {
+ let ack = get_ack_req(publish);
+
+ if let Some(ack) = ack {
+ self.request_tx.send_async(ack).await?;
+ }
+ Ok(())
+ }
+
+ /// Attempts to send a MQTT PubAck to the `EventLoop`. Only needed in if `manual_acks` flag is set.
+ pub fn try_ack(&self, publish: &Publish) -> Result<(), ClientError> {
+ let ack = get_ack_req(publish);
+ if let Some(ack) = ack {
+ self.request_tx.try_send(ack)?;
+ }
+ Ok(())
+ }
+
+ /// Sends a MQTT Publish to the `EventLoop`
+ pub async fn publish_bytes<S>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: Bytes,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ {
+ let mut publish = Publish::from_bytes(topic, qos, payload);
+ publish.retain = retain;
+ let publish = Request::Publish(publish);
+ self.request_tx.send_async(publish).await?;
+ Ok(())
+ }
+
+ /// Sends a MQTT Subscribe to the `EventLoop`
+ pub async fn subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<(), ClientError> {
+ let subscribe = Subscribe::new(topic.into(), qos);
+ let request = Request::Subscribe(subscribe);
+ self.request_tx.send_async(request).await?;
+ Ok(())
+ }
+
+ /// Attempts to send a MQTT Subscribe to the `EventLoop`
+ pub fn try_subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<(), ClientError> {
+ let subscribe = Subscribe::new(topic.into(), qos);
+ let request = Request::Subscribe(subscribe);
+ self.request_tx.try_send(request)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT Subscribe for multiple topics to the `EventLoop`
+ pub async fn subscribe_many<T>(&self, topics: T) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = SubscribeFilter>,
+ {
+ let subscribe = Subscribe::new_many(topics);
+ let request = Request::Subscribe(subscribe);
+ self.request_tx.send_async(request).await?;
+ Ok(())
+ }
+
+ /// Attempts to send a MQTT Subscribe for multiple topics to the `EventLoop`
+ pub fn try_subscribe_many<T>(&self, topics: T) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = SubscribeFilter>,
+ {
+ let subscribe = Subscribe::new_many(topics);
+ let request = Request::Subscribe(subscribe);
+ self.request_tx.try_send(request)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT Unsubscribe to the `EventLoop`
+ pub async fn unsubscribe<S: Into<String>>(&self, topic: S) -> Result<(), ClientError> {
+ let unsubscribe = Unsubscribe::new(topic.into());
+ let request = Request::Unsubscribe(unsubscribe);
+ self.request_tx.send_async(request).await?;
+ Ok(())
+ }
+
+ /// Attempts to send a MQTT Unsubscribe to the `EventLoop`
+ pub fn try_unsubscribe<S: Into<String>>(&self, topic: S) -> Result<(), ClientError> {
+ let unsubscribe = Unsubscribe::new(topic.into());
+ let request = Request::Unsubscribe(unsubscribe);
+ self.request_tx.try_send(request)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT disconnect to the `EventLoop`
+ pub async fn disconnect(&self) -> Result<(), ClientError> {
+ let request = Request::Disconnect(Disconnect);
+ self.request_tx.send_async(request).await?;
+ Ok(())
+ }
+
+ /// Attempts to send a MQTT disconnect to the `EventLoop`
+ pub fn try_disconnect(&self) -> Result<(), ClientError> {
+ let request = Request::Disconnect(Disconnect);
+ self.request_tx.try_send(request)?;
+ Ok(())
+ }
+}
+
+fn get_ack_req(publish: &Publish) -> Option<Request> {
+ let ack = match publish.qos {
+ QoS::AtMostOnce => return None,
+ QoS::AtLeastOnce => Request::PubAck(PubAck::new(publish.pkid)),
+ QoS::ExactlyOnce => Request::PubRec(PubRec::new(publish.pkid)),
+ };
+ Some(ack)
+}
+
+/// A synchronous client, communicates with MQTT `EventLoop`.
+///
+/// This is cloneable and can be used to synchronously [`publish`](`AsyncClient::publish`),
+/// [`subscribe`](`AsyncClient::subscribe`) through the `EventLoop`/`Connection`, which is to be polled in parallel
+/// by iterating over the object returned by [`Connection.iter()`](Connection::iter) in a separate thread.
+///
+/// **NOTE**: The `EventLoop`/`Connection` must be regularly polled(`.next()` in case of `Connection`) in order
+/// to send, receive and process packets from the broker, i.e. move ahead.
+///
+/// An asynchronous channel handle can also be extracted if necessary.
+#[derive(Clone)]
+pub struct Client {
+ client: AsyncClient,
+}
+
+impl Client {
+ /// Create a new `Client`
+ ///
+ /// `cap` specifies the capacity of the bounded async channel.
+ pub fn new(options: MqttOptions, cap: usize) -> (Client, Connection) {
+ let (client, eventloop) = AsyncClient::new(options, cap);
+ let client = Client { client };
+ let runtime = runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .unwrap();
+
+ let connection = Connection::new(eventloop, runtime);
+ (client, connection)
+ }
+
+ /// Create a new `Client` from a channel `Sender`.
+ ///
+ /// This is mostly useful for creating a test instance where you can
+ /// listen on the corresponding receiver.
+ pub fn from_sender(request_tx: Sender<Request>) -> Client {
+ Client {
+ client: AsyncClient::from_senders(request_tx),
+ }
+ }
+
+ /// Sends a MQTT Publish to the `EventLoop`
+ pub fn publish<S, V>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: V,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ V: Into<Vec<u8>>,
+ {
+ let topic = topic.into();
+ let mut publish = Publish::new(&topic, qos, payload);
+ publish.retain = retain;
+ let publish = Request::Publish(publish);
+ if !valid_topic(&topic) {
+ return Err(ClientError::Request(publish));
+ }
+ self.client.request_tx.send(publish)?;
+ Ok(())
+ }
+
+ pub fn try_publish<S, V>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: V,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ V: Into<Vec<u8>>,
+ {
+ self.client.try_publish(topic, qos, retain, payload)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT PubAck to the `EventLoop`. Only needed in if `manual_acks` flag is set.
+ pub fn ack(&self, publish: &Publish) -> Result<(), ClientError> {
+ let ack = get_ack_req(publish);
+
+ if let Some(ack) = ack {
+ self.client.request_tx.send(ack)?;
+ }
+ Ok(())
+ }
+
+ /// Sends a MQTT PubAck to the `EventLoop`. Only needed in if `manual_acks` flag is set.
+ pub fn try_ack(&self, publish: &Publish) -> Result<(), ClientError> {
+ self.client.try_ack(publish)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT Subscribe to the `EventLoop`
+ pub fn subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<(), ClientError> {
+ let subscribe = Subscribe::new(topic.into(), qos);
+ let request = Request::Subscribe(subscribe);
+ self.client.request_tx.send(request)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT Subscribe to the `EventLoop`
+ pub fn try_subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<(), ClientError> {
+ self.client.try_subscribe(topic, qos)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT Subscribe for multiple topics to the `EventLoop`
+ pub fn subscribe_many<T>(&self, topics: T) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = SubscribeFilter>,
+ {
+ let subscribe = Subscribe::new_many(topics);
+ let request = Request::Subscribe(subscribe);
+ self.client.request_tx.send(request)?;
+ Ok(())
+ }
+
+ pub fn try_subscribe_many<T>(&self, topics: T) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = SubscribeFilter>,
+ {
+ self.client.try_subscribe_many(topics)
+ }
+
+ /// Sends a MQTT Unsubscribe to the `EventLoop`
+ pub fn unsubscribe<S: Into<String>>(&self, topic: S) -> Result<(), ClientError> {
+ let unsubscribe = Unsubscribe::new(topic.into());
+ let request = Request::Unsubscribe(unsubscribe);
+ self.client.request_tx.send(request)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT Unsubscribe to the `EventLoop`
+ pub fn try_unsubscribe<S: Into<String>>(&self, topic: S) -> Result<(), ClientError> {
+ self.client.try_unsubscribe(topic)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT disconnect to the `EventLoop`
+ pub fn disconnect(&self) -> Result<(), ClientError> {
+ let request = Request::Disconnect(Disconnect);
+ self.client.request_tx.send(request)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT disconnect to the `EventLoop`
+ pub fn try_disconnect(&self) -> Result<(), ClientError> {
+ self.client.try_disconnect()?;
+ Ok(())
+ }
+}
+
+/// Error type returned by [`Connection::recv`]
+#[derive(Debug, Eq, PartialEq)]
+pub struct RecvError;
+
+/// Error type returned by [`Connection::try_recv`]
+#[derive(Debug, Eq, PartialEq)]
+pub enum TryRecvError {
+ /// User has closed requests channel
+ Disconnected,
+ /// Did not resolve
+ Empty,
+}
+
+/// Error type returned by [`Connection::recv_timeout`]
+#[derive(Debug, Eq, PartialEq)]
+pub enum RecvTimeoutError {
+ /// User has closed requests channel
+ Disconnected,
+ /// Recv request timedout
+ Timeout,
+}
+
+/// MQTT connection. Maintains all the necessary state
+pub struct Connection {
+ pub eventloop: EventLoop,
+ runtime: Runtime,
+}
+impl Connection {
+ fn new(eventloop: EventLoop, runtime: Runtime) -> Connection {
+ Connection { eventloop, runtime }
+ }
+
+ /// Returns an iterator over this connection. Iterating over this is all that's
+ /// necessary to make connection progress and maintain a robust connection.
+ /// Just continuing to loop will reconnect
+ /// **NOTE** Don't block this while iterating
+ // ideally this should be named iter_mut because it requires a mutable reference
+ // Also we can implement IntoIter for this to make it easy to iterate over it
+ #[must_use = "Connection should be iterated over a loop to make progress"]
+ pub fn iter(&mut self) -> Iter<'_> {
+ Iter { connection: self }
+ }
+
+ /// Attempt to fetch an incoming [`Event`] on the [`EvenLoop`], returning an error
+ /// if all clients/users have closed requests channel.
+ ///
+ /// [`EvenLoop`]: super::EventLoop
+ pub fn recv(&mut self) -> Result<Result<Event, ConnectionError>, RecvError> {
+ let f = self.eventloop.poll();
+ let event = self.runtime.block_on(f);
+
+ resolve_event(event).ok_or(RecvError)
+ }
+
+ /// Attempt to fetch an incoming [`Event`] on the [`EvenLoop`], returning an error
+ /// if none immediately present or all clients/users have closed requests channel.
+ ///
+ /// [`EvenLoop`]: super::EventLoop
+ pub fn try_recv(&mut self) -> Result<Result<Event, ConnectionError>, TryRecvError> {
+ let f = self.eventloop.poll();
+ // Enters the runtime context so we can poll the future, as required by `now_or_never()`.
+ // ref: https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html#method.enter
+ let _guard = self.runtime.enter();
+ let event = f.now_or_never().ok_or(TryRecvError::Empty)?;
+
+ resolve_event(event).ok_or(TryRecvError::Disconnected)
+ }
+
+ /// Attempt to fetch an incoming [`Event`] on the [`EvenLoop`], returning an error
+ /// if all clients/users have closed requests channel or the timeout has expired.
+ ///
+ /// [`EvenLoop`]: super::EventLoop
+ pub fn recv_timeout(
+ &mut self,
+ duration: Duration,
+ ) -> Result<Result<Event, ConnectionError>, RecvTimeoutError> {
+ let f = self.eventloop.poll();
+ let event = self
+ .runtime
+ .block_on(async { timeout(duration, f).await })
+ .map_err(|_| RecvTimeoutError::Timeout)?;
+
+ resolve_event(event).ok_or(RecvTimeoutError::Disconnected)
+ }
+}
+
+fn resolve_event(event: Result<Event, ConnectionError>) -> Option<Result<Event, ConnectionError>> {
+ match event {
+ Ok(v) => Some(Ok(v)),
+ // closing of request channel should stop the iterator
+ Err(ConnectionError::RequestsDone) => {
+ trace!("Done with requests");
+ None
+ }
+ Err(e) => Some(Err(e)),
+ }
+}
+
+/// Iterator which polls the `EventLoop` for connection progress
+pub struct Iter<'a> {
+ connection: &'a mut Connection,
+}
+
+impl Iterator for Iter<'_> {
+ type Item = Result<Event, ConnectionError>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.connection.recv().ok()
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn calling_iter_twice_on_connection_shouldnt_panic() {
+ use std::time::Duration;
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
+ let will = LastWill::new("hello/world", "good bye", QoS::AtMostOnce, false);
+ mqttoptions
+ .set_keep_alive(Duration::from_secs(5))
+ .set_last_will(will);
+
+ let (_, mut connection) = Client::new(mqttoptions, 10);
+ let _ = connection.iter();
+ let _ = connection.iter();
+ }
+
+ #[test]
+ fn should_be_able_to_build_test_client_from_channel() {
+ let (tx, rx) = flume::bounded(1);
+ let client = Client::from_sender(tx);
+ client
+ .publish("hello/world", QoS::ExactlyOnce, false, "good bye")
+ .expect("Should be able to publish");
+ let _ = rx.try_recv().expect("Should have message");
+ }
+}
diff --git a/vendor/rumqttc/src/eventloop.rs b/vendor/rumqttc/src/eventloop.rs
new file mode 100644
index 0000000000..1dc3f6219b
--- /dev/null
+++ b/vendor/rumqttc/src/eventloop.rs
@@ -0,0 +1,476 @@
+use crate::{framed::Network, Transport};
+use crate::{Incoming, MqttState, NetworkOptions, Packet, Request, StateError};
+use crate::{MqttOptions, Outgoing};
+
+use crate::framed::N;
+use crate::mqttbytes::v4::*;
+use flume::{bounded, Receiver, Sender};
+use tokio::net::{lookup_host, TcpSocket, TcpStream};
+use tokio::select;
+use tokio::time::{self, Instant, Sleep};
+
+use std::collections::VecDeque;
+use std::io;
+use std::net::SocketAddr;
+use std::pin::Pin;
+use std::time::Duration;
+
+#[cfg(unix)]
+use {std::path::Path, tokio::net::UnixStream};
+
+#[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+use crate::tls;
+
+#[cfg(feature = "websocket")]
+use {
+ crate::websockets::{split_url, validate_response_headers, UrlError},
+ async_tungstenite::tungstenite::client::IntoClientRequest,
+ ws_stream_tungstenite::WsStream,
+};
+
+#[cfg(feature = "proxy")]
+use crate::proxy::ProxyError;
+
+/// Critical errors during eventloop polling
+#[derive(Debug, thiserror::Error)]
+pub enum ConnectionError {
+ #[error("Mqtt state: {0}")]
+ MqttState(#[from] StateError),
+ #[error("Network timeout")]
+ NetworkTimeout,
+ #[error("Flush timeout")]
+ FlushTimeout,
+ #[cfg(feature = "websocket")]
+ #[error("Websocket: {0}")]
+ Websocket(#[from] async_tungstenite::tungstenite::error::Error),
+ #[cfg(feature = "websocket")]
+ #[error("Websocket Connect: {0}")]
+ WsConnect(#[from] http::Error),
+ #[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+ #[error("TLS: {0}")]
+ Tls(#[from] tls::Error),
+ #[error("I/O: {0}")]
+ Io(#[from] io::Error),
+ #[error("Connection refused, return code: `{0:?}`")]
+ ConnectionRefused(ConnectReturnCode),
+ #[error("Expected ConnAck packet, received: {0:?}")]
+ NotConnAck(Packet),
+ #[error("Requests done")]
+ RequestsDone,
+ #[cfg(feature = "websocket")]
+ #[error("Invalid Url: {0}")]
+ InvalidUrl(#[from] UrlError),
+ #[cfg(feature = "proxy")]
+ #[error("Proxy Connect: {0}")]
+ Proxy(#[from] ProxyError),
+ #[cfg(feature = "websocket")]
+ #[error("Websocket response validation error: ")]
+ ResponseValidation(#[from] crate::websockets::ValidationError),
+}
+
+/// Eventloop with all the state of a connection
+pub struct EventLoop {
+ /// Options of the current mqtt connection
+ pub mqtt_options: MqttOptions,
+ /// Current state of the connection
+ pub state: MqttState,
+ /// Request stream
+ requests_rx: Receiver<Request>,
+ /// Requests handle to send requests
+ pub(crate) requests_tx: Sender<Request>,
+ /// Pending packets from last session
+ pub pending: VecDeque<Request>,
+ /// Network connection to the broker
+ network: Option<Network>,
+ /// Keep alive time
+ keepalive_timeout: Option<Pin<Box<Sleep>>>,
+ pub network_options: NetworkOptions,
+}
+
+/// Events which can be yielded by the event loop
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum Event {
+ Incoming(Incoming),
+ Outgoing(Outgoing),
+}
+
+impl EventLoop {
+ /// New MQTT `EventLoop`
+ ///
+ /// When connection encounters critical errors (like auth failure), user has a choice to
+ /// access and update `options`, `state` and `requests`.
+ pub fn new(mqtt_options: MqttOptions, cap: usize) -> EventLoop {
+ let (requests_tx, requests_rx) = bounded(cap);
+ let pending = VecDeque::new();
+ let max_inflight = mqtt_options.inflight;
+ let manual_acks = mqtt_options.manual_acks;
+ let max_outgoing_packet_size = mqtt_options.max_outgoing_packet_size;
+
+ EventLoop {
+ mqtt_options,
+ state: MqttState::new(max_inflight, manual_acks, max_outgoing_packet_size),
+ requests_tx,
+ requests_rx,
+ pending,
+ network: None,
+ keepalive_timeout: None,
+ network_options: NetworkOptions::new(),
+ }
+ }
+
+ /// Last session might contain packets which aren't acked. MQTT says these packets should be
+ /// republished in the next session. Move pending messages from state to eventloop, drops the
+ /// underlying network connection and clears the keepalive timeout if any.
+ ///
+ /// > NOTE: Use only when EventLoop is blocked on network and unable to immediately handle disconnect.
+ /// > Also, while this helps prevent data loss, the pending list length should be managed properly.
+ /// > For this reason we recommend setting [`AsycClient`](crate::AsyncClient)'s channel capacity to `0`.
+ pub fn clean(&mut self) {
+ self.network = None;
+ self.keepalive_timeout = None;
+ self.pending.extend(self.state.clean());
+
+ // drain requests from channel which weren't yet received
+ let requests_in_channel = self.requests_rx.drain();
+ self.pending.extend(requests_in_channel);
+ }
+
+ /// Yields Next notification or outgoing request and periodically pings
+ /// the broker. Continuing to poll will reconnect to the broker if there is
+ /// a disconnection.
+ /// **NOTE** Don't block this while iterating
+ pub async fn poll(&mut self) -> Result<Event, ConnectionError> {
+ if self.network.is_none() {
+ let (network, connack) = match time::timeout(
+ Duration::from_secs(self.network_options.connection_timeout()),
+ connect(&self.mqtt_options, self.network_options.clone()),
+ )
+ .await
+ {
+ Ok(inner) => inner?,
+ Err(_) => return Err(ConnectionError::NetworkTimeout),
+ };
+ self.network = Some(network);
+
+ if self.keepalive_timeout.is_none() && !self.mqtt_options.keep_alive.is_zero() {
+ self.keepalive_timeout = Some(Box::pin(time::sleep(self.mqtt_options.keep_alive)));
+ }
+
+ return Ok(Event::Incoming(connack));
+ }
+
+ match self.select().await {
+ Ok(v) => Ok(v),
+ Err(e) => {
+ self.clean();
+ Err(e)
+ }
+ }
+ }
+
+ /// Select on network and requests and generate keepalive pings when necessary
+ async fn select(&mut self) -> Result<Event, ConnectionError> {
+ let network = self.network.as_mut().unwrap();
+ // let await_acks = self.state.await_acks;
+ let inflight_full = self.state.inflight >= self.mqtt_options.inflight;
+ let collision = self.state.collision.is_some();
+ let network_timeout = Duration::from_secs(self.network_options.connection_timeout());
+
+ // Read buffered events from previous polls before calling a new poll
+ if let Some(event) = self.state.events.pop_front() {
+ return Ok(event);
+ }
+
+ let mut no_sleep = Box::pin(time::sleep(Duration::ZERO));
+ // this loop is necessary since self.incoming.pop_front() might return None. In that case,
+ // instead of returning a None event, we try again.
+ select! {
+ // Pull a bunch of packets from network, reply in bunch and yield the first item
+ o = network.readb(&mut self.state) => {
+ o?;
+ // flush all the acks and return first incoming packet
+ match time::timeout(network_timeout, network.flush(&mut self.state.write)).await {
+ Ok(inner) => inner?,
+ Err(_)=> return Err(ConnectionError::FlushTimeout),
+ };
+ Ok(self.state.events.pop_front().unwrap())
+ },
+ // Handles pending and new requests.
+ // If available, prioritises pending requests from previous session.
+ // Else, pulls next request from user requests channel.
+ // If conditions in the below branch are for flow control.
+ // The branch is disabled if there's no pending messages and new user requests
+ // cannot be serviced due flow control.
+ // We read next user user request only when inflight messages are < configured inflight
+ // and there are no collisions while handling previous outgoing requests.
+ //
+ // Flow control is based on ack count. If inflight packet count in the buffer is
+ // less than max_inflight setting, next outgoing request will progress. For this
+ // to work correctly, broker should ack in sequence (a lot of brokers won't)
+ //
+ // E.g If max inflight = 5, user requests will be blocked when inflight queue
+ // looks like this -> [1, 2, 3, 4, 5].
+ // If broker acking 2 instead of 1 -> [1, x, 3, 4, 5].
+ // This pulls next user request. But because max packet id = max_inflight, next
+ // user request's packet id will roll to 1. This replaces existing packet id 1.
+ // Resulting in a collision
+ //
+ // Eventloop can stop receiving outgoing user requests when previous outgoing
+ // request collided. I.e collision state. Collision state will be cleared only
+ // when correct ack is received
+ // Full inflight queue will look like -> [1a, 2, 3, 4, 5].
+ // If 3 is acked instead of 1 first -> [1a, 2, x, 4, 5].
+ // After collision with pkid 1 -> [1b ,2, x, 4, 5].
+ // 1a is saved to state and event loop is set to collision mode stopping new
+ // outgoing requests (along with 1b).
+ o = Self::next_request(
+ &mut self.pending,
+ &self.requests_rx,
+ self.mqtt_options.pending_throttle
+ ), if !self.pending.is_empty() || (!inflight_full && !collision) => match o {
+ Ok(request) => {
+ self.state.handle_outgoing_packet(request)?;
+ match time::timeout(network_timeout, network.flush(&mut self.state.write)).await {
+ Ok(inner) => inner?,
+ Err(_)=> return Err(ConnectionError::FlushTimeout),
+ };
+ Ok(self.state.events.pop_front().unwrap())
+ }
+ Err(_) => Err(ConnectionError::RequestsDone),
+ },
+ // We generate pings irrespective of network activity. This keeps the ping logic
+ // simple. We can change this behavior in future if necessary (to prevent extra pings)
+ _ = self.keepalive_timeout.as_mut().unwrap_or(&mut no_sleep),
+ if self.keepalive_timeout.is_some() && !self.mqtt_options.keep_alive.is_zero() => {
+ let timeout = self.keepalive_timeout.as_mut().unwrap();
+ timeout.as_mut().reset(Instant::now() + self.mqtt_options.keep_alive);
+
+ self.state.handle_outgoing_packet(Request::PingReq(PingReq))?;
+ match time::timeout(network_timeout, network.flush(&mut self.state.write)).await {
+ Ok(inner) => inner?,
+ Err(_)=> return Err(ConnectionError::FlushTimeout),
+ };
+ Ok(self.state.events.pop_front().unwrap())
+ }
+ }
+ }
+
+ pub fn network_options(&self) -> NetworkOptions {
+ self.network_options.clone()
+ }
+
+ pub fn set_network_options(&mut self, network_options: NetworkOptions) -> &mut Self {
+ self.network_options = network_options;
+ self
+ }
+
+ async fn next_request(
+ pending: &mut VecDeque<Request>,
+ rx: &Receiver<Request>,
+ pending_throttle: Duration,
+ ) -> Result<Request, ConnectionError> {
+ if !pending.is_empty() {
+ time::sleep(pending_throttle).await;
+ // We must call .pop_front() AFTER sleep() otherwise we would have
+ // advanced the iterator but the future might be canceled before return
+ Ok(pending.pop_front().unwrap())
+ } else {
+ match rx.recv_async().await {
+ Ok(r) => Ok(r),
+ Err(_) => Err(ConnectionError::RequestsDone),
+ }
+ }
+ }
+}
+
+/// This stream internally processes requests from the request stream provided to the eventloop
+/// while also consuming byte stream from the network and yielding mqtt packets as the output of
+/// the stream.
+/// This function (for convenience) includes internal delays for users to perform internal sleeps
+/// between re-connections so that cancel semantics can be used during this sleep
+async fn connect(
+ mqtt_options: &MqttOptions,
+ network_options: NetworkOptions,
+) -> Result<(Network, Incoming), ConnectionError> {
+ // connect to the broker
+ let mut network = network_connect(mqtt_options, network_options).await?;
+
+ // make MQTT connection request (which internally awaits for ack)
+ let packet = mqtt_connect(mqtt_options, &mut network).await?;
+
+ Ok((network, packet))
+}
+
+pub(crate) async fn socket_connect(
+ host: String,
+ network_options: NetworkOptions,
+) -> io::Result<TcpStream> {
+ let addrs = lookup_host(host).await?;
+ let mut last_err = None;
+
+ for addr in addrs {
+ let socket = match addr {
+ SocketAddr::V4(_) => TcpSocket::new_v4()?,
+ SocketAddr::V6(_) => TcpSocket::new_v6()?,
+ };
+
+ if let Some(send_buff_size) = network_options.tcp_send_buffer_size {
+ socket.set_send_buffer_size(send_buff_size).unwrap();
+ }
+ if let Some(recv_buffer_size) = network_options.tcp_recv_buffer_size {
+ socket.set_recv_buffer_size(recv_buffer_size).unwrap();
+ }
+
+ #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
+ {
+ if let Some(bind_device) = &network_options.bind_device {
+ // call the bind_device function only if the bind_device network option is defined
+ // If binding device is None or an empty string it removes the binding,
+ // which is causing PermissionDenied errors in AWS environment (lambda function).
+ socket.bind_device(Some(bind_device.as_bytes()))?;
+ }
+ }
+
+ match socket.connect(addr).await {
+ Ok(s) => return Ok(s),
+ Err(e) => {
+ last_err = Some(e);
+ }
+ };
+ }
+
+ Err(last_err.unwrap_or_else(|| {
+ io::Error::new(
+ io::ErrorKind::InvalidInput,
+ "could not resolve to any address",
+ )
+ }))
+}
+
+async fn network_connect(
+ options: &MqttOptions,
+ network_options: NetworkOptions,
+) -> Result<Network, ConnectionError> {
+ // Process Unix files early, as proxy is not supported for them.
+ #[cfg(unix)]
+ if matches!(options.transport(), Transport::Unix) {
+ let file = options.broker_addr.as_str();
+ let socket = UnixStream::connect(Path::new(file)).await?;
+ let network = Network::new(socket, options.max_incoming_packet_size);
+ return Ok(network);
+ }
+
+ // For websockets domain and port are taken directly from `broker_addr` (which is a url).
+ let (domain, port) = match options.transport() {
+ #[cfg(feature = "websocket")]
+ Transport::Ws => split_url(&options.broker_addr)?,
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ Transport::Wss(_) => split_url(&options.broker_addr)?,
+ _ => options.broker_address(),
+ };
+
+ let tcp_stream: Box<dyn N> = {
+ #[cfg(feature = "proxy")]
+ match options.proxy() {
+ Some(proxy) => proxy.connect(&domain, port, network_options).await?,
+ None => {
+ let addr = format!("{domain}:{port}");
+ let tcp = socket_connect(addr, network_options).await?;
+ Box::new(tcp)
+ }
+ }
+ #[cfg(not(feature = "proxy"))]
+ {
+ let addr = format!("{domain}:{port}");
+ let tcp = socket_connect(addr, network_options).await?;
+ Box::new(tcp)
+ }
+ };
+
+ let network = match options.transport() {
+ Transport::Tcp => Network::new(tcp_stream, options.max_incoming_packet_size),
+ #[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+ Transport::Tls(tls_config) => {
+ let socket =
+ tls::tls_connect(&options.broker_addr, options.port, &tls_config, tcp_stream)
+ .await?;
+ Network::new(socket, options.max_incoming_packet_size)
+ }
+ #[cfg(unix)]
+ Transport::Unix => unreachable!(),
+ #[cfg(feature = "websocket")]
+ Transport::Ws => {
+ let mut request = options.broker_addr.as_str().into_client_request()?;
+ request
+ .headers_mut()
+ .insert("Sec-WebSocket-Protocol", "mqtt".parse().unwrap());
+
+ if let Some(request_modifier) = options.request_modifier() {
+ request = request_modifier(request).await;
+ }
+
+ let (socket, response) =
+ async_tungstenite::tokio::client_async(request, tcp_stream).await?;
+ validate_response_headers(response)?;
+
+ Network::new(WsStream::new(socket), options.max_incoming_packet_size)
+ }
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ Transport::Wss(tls_config) => {
+ let mut request = options.broker_addr.as_str().into_client_request()?;
+ request
+ .headers_mut()
+ .insert("Sec-WebSocket-Protocol", "mqtt".parse().unwrap());
+
+ if let Some(request_modifier) = options.request_modifier() {
+ request = request_modifier(request).await;
+ }
+
+ let connector = tls::rustls_connector(&tls_config).await?;
+
+ let (socket, response) = async_tungstenite::tokio::client_async_tls_with_connector(
+ request,
+ tcp_stream,
+ Some(connector),
+ )
+ .await?;
+ validate_response_headers(response)?;
+
+ Network::new(WsStream::new(socket), options.max_incoming_packet_size)
+ }
+ };
+
+ Ok(network)
+}
+
+async fn mqtt_connect(
+ options: &MqttOptions,
+ network: &mut Network,
+) -> Result<Incoming, ConnectionError> {
+ let keep_alive = options.keep_alive().as_secs() as u16;
+ let clean_session = options.clean_session();
+ let last_will = options.last_will();
+
+ let mut connect = Connect::new(options.client_id());
+ connect.protocol = options.protocol();
+ connect.keep_alive = keep_alive;
+ connect.clean_session = clean_session;
+ connect.last_will = last_will;
+
+ if let Some((username, password)) = options.credentials() {
+ let login = Login::new(username, password);
+ connect.login = Some(login);
+ }
+
+ // send mqtt connect packet
+ network.connect(connect).await?;
+
+ // validate connack
+ match network.read().await? {
+ Incoming::ConnAck(connack) if connack.code == ConnectReturnCode::Success => {
+ Ok(Packet::ConnAck(connack))
+ }
+ Incoming::ConnAck(connack) => Err(ConnectionError::ConnectionRefused(connack.code)),
+ packet => Err(ConnectionError::NotConnAck(packet)),
+ }
+}
diff --git a/vendor/rumqttc/src/framed.rs b/vendor/rumqttc/src/framed.rs
new file mode 100644
index 0000000000..b0a536e78d
--- /dev/null
+++ b/vendor/rumqttc/src/framed.rs
@@ -0,0 +1,121 @@
+use bytes::BytesMut;
+use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
+
+use crate::mqttbytes::{self, v4::*};
+use crate::{Incoming, MqttState, StateError};
+use std::io;
+
+/// Network transforms packets <-> frames efficiently. It takes
+/// advantage of pre-allocation, buffering and vectorization when
+/// appropriate to achieve performance
+pub struct Network {
+ /// Socket for IO
+ socket: Box<dyn N>,
+ /// Buffered reads
+ read: BytesMut,
+ /// Maximum packet size
+ max_incoming_size: usize,
+ /// Maximum readv count
+ max_readb_count: usize,
+}
+
+impl Network {
+ pub fn new(socket: impl N + 'static, max_incoming_size: usize) -> Network {
+ let socket = Box::new(socket) as Box<dyn N>;
+ Network {
+ socket,
+ read: BytesMut::with_capacity(10 * 1024),
+ max_incoming_size,
+ max_readb_count: 10,
+ }
+ }
+
+ /// Reads more than 'required' bytes to frame a packet into self.read buffer
+ async fn read_bytes(&mut self, required: usize) -> io::Result<usize> {
+ let mut total_read = 0;
+ loop {
+ let read = self.socket.read_buf(&mut self.read).await?;
+ if 0 == read {
+ return if self.read.is_empty() {
+ Err(io::Error::new(
+ io::ErrorKind::ConnectionAborted,
+ "connection closed by peer",
+ ))
+ } else {
+ Err(io::Error::new(
+ io::ErrorKind::ConnectionReset,
+ "connection reset by peer",
+ ))
+ };
+ }
+
+ total_read += read;
+ if total_read >= required {
+ return Ok(total_read);
+ }
+ }
+ }
+
+ pub async fn read(&mut self) -> io::Result<Incoming> {
+ loop {
+ let required = match read(&mut self.read, self.max_incoming_size) {
+ Ok(packet) => return Ok(packet),
+ Err(mqttbytes::Error::InsufficientBytes(required)) => required,
+ Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e.to_string())),
+ };
+
+ // read more packets until a frame can be created. This function
+ // blocks until a frame can be created. Use this in a select! branch
+ self.read_bytes(required).await?;
+ }
+ }
+
+ /// Read packets in bulk. This allow replies to be in bulk. This method is used
+ /// after the connection is established to read a bunch of incoming packets
+ pub async fn readb(&mut self, state: &mut MqttState) -> Result<(), StateError> {
+ let mut count = 0;
+ loop {
+ match read(&mut self.read, self.max_incoming_size) {
+ Ok(packet) => {
+ state.handle_incoming_packet(packet)?;
+
+ count += 1;
+ if count >= self.max_readb_count {
+ return Ok(());
+ }
+ }
+ // If some packets are already framed, return those
+ Err(mqttbytes::Error::InsufficientBytes(_)) if count > 0 => return Ok(()),
+ // Wait for more bytes until a frame can be created
+ Err(mqttbytes::Error::InsufficientBytes(required)) => {
+ self.read_bytes(required).await?;
+ }
+ Err(e) => return Err(StateError::Deserialization(e)),
+ };
+ }
+ }
+
+ pub async fn connect(&mut self, connect: Connect) -> io::Result<usize> {
+ let mut write = BytesMut::new();
+ let len = match connect.write(&mut write) {
+ Ok(size) => size,
+ Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e.to_string())),
+ };
+
+ self.socket.write_all(&write[..]).await?;
+ Ok(len)
+ }
+
+ pub async fn flush(&mut self, write: &mut BytesMut) -> io::Result<()> {
+ if write.is_empty() {
+ return Ok(());
+ }
+
+ self.socket.write_all(&write[..]).await?;
+ write.clear();
+ Ok(())
+ }
+}
+
+pub trait N: AsyncRead + AsyncWrite + Send + Unpin {}
+impl<T> N for T where T: AsyncRead + AsyncWrite + Send + Unpin {}
diff --git a/vendor/rumqttc/src/lib.rs b/vendor/rumqttc/src/lib.rs
new file mode 100644
index 0000000000..8c37ea5c6d
--- /dev/null
+++ b/vendor/rumqttc/src/lib.rs
@@ -0,0 +1,1029 @@
+//! A pure rust MQTT client which strives to be robust, efficient and easy to use.
+//! This library is backed by an async (tokio) eventloop which handles all the
+//! robustness and and efficiency parts of MQTT but naturally fits into both sync
+//! and async worlds as we'll see
+//!
+//! Let's jump into examples right away
+//!
+//! A simple synchronous publish and subscribe
+//! ----------------------------
+//!
+//! ```no_run
+//! use rumqttc::{MqttOptions, Client, QoS};
+//! use std::time::Duration;
+//! use std::thread;
+//!
+//! let mut mqttoptions = MqttOptions::new("rumqtt-sync", "test.mosquitto.org", 1883);
+//! mqttoptions.set_keep_alive(Duration::from_secs(5));
+//!
+//! let (mut client, mut connection) = Client::new(mqttoptions, 10);
+//! client.subscribe("hello/rumqtt", QoS::AtMostOnce).unwrap();
+//! thread::spawn(move || for i in 0..10 {
+//! client.publish("hello/rumqtt", QoS::AtLeastOnce, false, vec![i; i as usize]).unwrap();
+//! thread::sleep(Duration::from_millis(100));
+//! });
+//!
+//! // Iterate to poll the eventloop for connection progress
+//! for (i, notification) in connection.iter().enumerate() {
+//! println!("Notification = {:?}", notification);
+//! }
+//! ```
+//!
+//! A simple asynchronous publish and subscribe
+//! ------------------------------
+//!
+//! ```no_run
+//! use rumqttc::{MqttOptions, AsyncClient, QoS};
+//! use tokio::{task, time};
+//! use std::time::Duration;
+//! use std::error::Error;
+//!
+//! # #[tokio::main(flavor = "current_thread")]
+//! # async fn main() {
+//! let mut mqttoptions = MqttOptions::new("rumqtt-async", "test.mosquitto.org", 1883);
+//! mqttoptions.set_keep_alive(Duration::from_secs(5));
+//!
+//! let (mut client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
+//! client.subscribe("hello/rumqtt", QoS::AtMostOnce).await.unwrap();
+//!
+//! task::spawn(async move {
+//! for i in 0..10 {
+//! client.publish("hello/rumqtt", QoS::AtLeastOnce, false, vec![i; i as usize]).await.unwrap();
+//! time::sleep(Duration::from_millis(100)).await;
+//! }
+//! });
+//!
+//! loop {
+//! let notification = eventloop.poll().await.unwrap();
+//! println!("Received = {:?}", notification);
+//! }
+//! # }
+//! ```
+//!
+//! Quick overview of features
+//! - Eventloop orchestrates outgoing/incoming packets concurrently and handles the state
+//! - Pings the broker when necessary and detects client side half open connections as well
+//! - Throttling of outgoing packets (todo)
+//! - Queue size based flow control on outgoing packets
+//! - Automatic reconnections by just continuing the `eventloop.poll()`/`connection.iter()` loop
+//! - Natural backpressure to client APIs during bad network
+//!
+//! In short, everything necessary to maintain a robust connection
+//!
+//! Since the eventloop is externally polled (with `iter()/poll()` in a loop)
+//! out side the library and `Eventloop` is accessible, users can
+//! - Distribute incoming messages based on topics
+//! - Stop it when required
+//! - Access internal state for use cases like graceful shutdown or to modify options before reconnection
+//!
+//! ## Important notes
+//!
+//! - Looping on `connection.iter()`/`eventloop.poll()` is necessary to run the
+//! event loop and make progress. It yields incoming and outgoing activity
+//! notifications which allows customization as you see fit.
+//!
+//! - Blocking inside the `connection.iter()`/`eventloop.poll()` loop will block
+//! connection progress.
+//!
+//! ## FAQ
+//! **Connecting to a broker using raw ip doesn't work**
+//!
+//! You cannot create a TLS connection to a bare IP address with a self-signed
+//! certificate. This is a [limitation of rustls](https://github.com/ctz/rustls/issues/184).
+//! One workaround, which only works under *nix/BSD-like systems, is to add an
+//! entry to wherever your DNS resolver looks (e.g. `/etc/hosts`) for the bare IP
+//! address and use that name in your code.
+#![cfg_attr(docsrs, feature(doc_cfg))]
+
+#[macro_use]
+extern crate log;
+
+use std::fmt::{self, Debug, Formatter};
+
+#[cfg(any(feature = "use-rustls", feature = "websocket"))]
+use std::sync::Arc;
+
+use std::time::Duration;
+
+mod client;
+mod eventloop;
+mod framed;
+pub mod mqttbytes;
+mod state;
+pub mod v5;
+
+#[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+mod tls;
+
+#[cfg(feature = "websocket")]
+mod websockets;
+
+#[cfg(feature = "websocket")]
+use std::{
+ future::{Future, IntoFuture},
+ pin::Pin,
+};
+
+#[cfg(feature = "websocket")]
+type RequestModifierFn = Arc<
+ dyn Fn(http::Request<()>) -> Pin<Box<dyn Future<Output = http::Request<()>> + Send>>
+ + Send
+ + Sync,
+>;
+
+#[cfg(feature = "proxy")]
+mod proxy;
+
+pub use client::{
+ AsyncClient, Client, ClientError, Connection, Iter, RecvError, RecvTimeoutError, TryRecvError,
+};
+pub use eventloop::{ConnectionError, Event, EventLoop};
+pub use mqttbytes::v4::*;
+pub use mqttbytes::*;
+#[cfg(feature = "use-rustls")]
+use rustls_native_certs::load_native_certs;
+pub use state::{MqttState, StateError};
+#[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+pub use tls::Error as TlsError;
+#[cfg(feature = "use-rustls")]
+pub use tokio_rustls;
+#[cfg(feature = "use-rustls")]
+use tokio_rustls::rustls::{ClientConfig, RootCertStore};
+
+#[cfg(feature = "proxy")]
+pub use proxy::{Proxy, ProxyAuth, ProxyType};
+
+pub type Incoming = Packet;
+
+/// Current outgoing activity on the eventloop
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum Outgoing {
+ /// Publish packet with packet identifier. 0 implies QoS 0
+ Publish(u16),
+ /// Subscribe packet with packet identifier
+ Subscribe(u16),
+ /// Unsubscribe packet with packet identifier
+ Unsubscribe(u16),
+ /// PubAck packet
+ PubAck(u16),
+ /// PubRec packet
+ PubRec(u16),
+ /// PubRel packet
+ PubRel(u16),
+ /// PubComp packet
+ PubComp(u16),
+ /// Ping request packet
+ PingReq,
+ /// Ping response packet
+ PingResp,
+ /// Disconnect packet
+ Disconnect,
+ /// Await for an ack for more outgoing progress
+ AwaitAck(u16),
+}
+
+/// Requests by the client to mqtt event loop. Request are
+/// handled one by one.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub enum Request {
+ Publish(Publish),
+ PubAck(PubAck),
+ PubRec(PubRec),
+ PubComp(PubComp),
+ PubRel(PubRel),
+ PingReq(PingReq),
+ PingResp(PingResp),
+ Subscribe(Subscribe),
+ SubAck(SubAck),
+ Unsubscribe(Unsubscribe),
+ UnsubAck(UnsubAck),
+ Disconnect(Disconnect),
+}
+
+impl Request {
+ fn size(&self) -> usize {
+ match &self {
+ Request::Publish(publish) => publish.size(),
+ Request::PubAck(puback) => puback.size(),
+ Request::PubRec(pubrec) => pubrec.size(),
+ Request::PubComp(pubcomp) => pubcomp.size(),
+ Request::PubRel(pubrel) => pubrel.size(),
+ Request::PingReq(pingreq) => pingreq.size(),
+ Request::PingResp(pingresp) => pingresp.size(),
+ Request::Subscribe(subscribe) => subscribe.size(),
+ Request::SubAck(suback) => suback.size(),
+ Request::Unsubscribe(unsubscribe) => unsubscribe.size(),
+ Request::UnsubAck(unsuback) => unsuback.size(),
+ Request::Disconnect(disconn) => disconn.size(),
+ }
+ }
+}
+
+impl From<Publish> for Request {
+ fn from(publish: Publish) -> Request {
+ Request::Publish(publish)
+ }
+}
+
+impl From<Subscribe> for Request {
+ fn from(subscribe: Subscribe) -> Request {
+ Request::Subscribe(subscribe)
+ }
+}
+
+impl From<Unsubscribe> for Request {
+ fn from(unsubscribe: Unsubscribe) -> Request {
+ Request::Unsubscribe(unsubscribe)
+ }
+}
+
+/// Transport methods. Defaults to TCP.
+#[derive(Clone)]
+pub enum Transport {
+ Tcp,
+ #[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+ Tls(TlsConfiguration),
+ #[cfg(unix)]
+ Unix,
+ #[cfg(feature = "websocket")]
+ #[cfg_attr(docsrs, doc(cfg(feature = "websocket")))]
+ Ws,
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ #[cfg_attr(docsrs, doc(cfg(all(feature = "use-rustls", feature = "websocket"))))]
+ Wss(TlsConfiguration),
+}
+
+impl Default for Transport {
+ fn default() -> Self {
+ Self::tcp()
+ }
+}
+
+impl Transport {
+ /// Use regular tcp as transport (default)
+ pub fn tcp() -> Self {
+ Self::Tcp
+ }
+
+ #[cfg(feature = "use-rustls")]
+ pub fn tls_with_default_config() -> Self {
+ Self::tls_with_config(Default::default())
+ }
+
+ /// Use secure tcp with tls as transport
+ #[cfg(feature = "use-rustls")]
+ pub fn tls(
+ ca: Vec<u8>,
+ client_auth: Option<(Vec<u8>, Vec<u8>)>,
+ alpn: Option<Vec<Vec<u8>>>,
+ ) -> Self {
+ let config = TlsConfiguration::Simple {
+ ca,
+ alpn,
+ client_auth,
+ };
+
+ Self::tls_with_config(config)
+ }
+
+ #[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+ pub fn tls_with_config(tls_config: TlsConfiguration) -> Self {
+ Self::Tls(tls_config)
+ }
+
+ #[cfg(unix)]
+ pub fn unix() -> Self {
+ Self::Unix
+ }
+
+ /// Use websockets as transport
+ #[cfg(feature = "websocket")]
+ #[cfg_attr(docsrs, doc(cfg(feature = "websocket")))]
+ pub fn ws() -> Self {
+ Self::Ws
+ }
+
+ /// Use secure websockets with tls as transport
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ #[cfg_attr(docsrs, doc(cfg(all(feature = "use-rustls", feature = "websocket"))))]
+ pub fn wss(
+ ca: Vec<u8>,
+ client_auth: Option<(Vec<u8>, Vec<u8>)>,
+ alpn: Option<Vec<Vec<u8>>>,
+ ) -> Self {
+ let config = TlsConfiguration::Simple {
+ ca,
+ client_auth,
+ alpn,
+ };
+
+ Self::wss_with_config(config)
+ }
+
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ #[cfg_attr(docsrs, doc(cfg(all(feature = "use-rustls", feature = "websocket"))))]
+ pub fn wss_with_config(tls_config: TlsConfiguration) -> Self {
+ Self::Wss(tls_config)
+ }
+
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ #[cfg_attr(docsrs, doc(cfg(all(feature = "use-rustls", feature = "websocket"))))]
+ pub fn wss_with_default_config() -> Self {
+ Self::Wss(Default::default())
+ }
+}
+
+/// TLS configuration method
+#[derive(Clone, Debug)]
+#[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+pub enum TlsConfiguration {
+ #[cfg(feature = "use-rustls")]
+ Simple {
+ /// connection method
+ ca: Vec<u8>,
+ /// alpn settings
+ alpn: Option<Vec<Vec<u8>>>,
+ /// tls client_authentication
+ client_auth: Option<(Vec<u8>, Vec<u8>)>,
+ },
+ #[cfg(feature = "use-native-tls")]
+ SimpleNative {
+ /// ca certificate
+ ca: Vec<u8>,
+ /// pkcs12 binary der and
+ /// password for use with der
+ client_auth: Option<(Vec<u8>, String)>,
+ },
+ #[cfg(feature = "use-rustls")]
+ /// Injected rustls ClientConfig for TLS, to allow more customisation.
+ Rustls(Arc<ClientConfig>),
+ #[cfg(feature = "use-native-tls")]
+ Native,
+}
+
+#[cfg(feature = "use-rustls")]
+impl Default for TlsConfiguration {
+ fn default() -> Self {
+ let mut root_cert_store = RootCertStore::empty();
+ for cert in load_native_certs().expect("could not load platform certs") {
+ root_cert_store.add(cert).unwrap();
+ }
+ let tls_config = ClientConfig::builder()
+ .with_root_certificates(root_cert_store)
+ .with_no_client_auth();
+
+ Self::Rustls(Arc::new(tls_config))
+ }
+}
+
+#[cfg(feature = "use-rustls")]
+impl From<ClientConfig> for TlsConfiguration {
+ fn from(config: ClientConfig) -> Self {
+ TlsConfiguration::Rustls(Arc::new(config))
+ }
+}
+
+/// Provides a way to configure low level network connection configurations
+#[derive(Clone, Default)]
+pub struct NetworkOptions {
+ tcp_send_buffer_size: Option<u32>,
+ tcp_recv_buffer_size: Option<u32>,
+ conn_timeout: u64,
+ #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
+ bind_device: Option<String>,
+}
+
+impl NetworkOptions {
+ pub fn new() -> Self {
+ NetworkOptions {
+ tcp_send_buffer_size: None,
+ tcp_recv_buffer_size: None,
+ conn_timeout: 5,
+ #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
+ bind_device: None,
+ }
+ }
+
+ pub fn set_tcp_send_buffer_size(&mut self, size: u32) {
+ self.tcp_send_buffer_size = Some(size);
+ }
+
+ pub fn set_tcp_recv_buffer_size(&mut self, size: u32) {
+ self.tcp_recv_buffer_size = Some(size);
+ }
+
+ /// set connection timeout in secs
+ pub fn set_connection_timeout(&mut self, timeout: u64) -> &mut Self {
+ self.conn_timeout = timeout;
+ self
+ }
+
+ /// get timeout in secs
+ pub fn connection_timeout(&self) -> u64 {
+ self.conn_timeout
+ }
+
+ /// bind connection to a specific network device by name
+ #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
+ #[cfg_attr(
+ docsrs,
+ doc(cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux")))
+ )]
+ pub fn set_bind_device(&mut self, bind_device: &str) -> &mut Self {
+ self.bind_device = Some(bind_device.to_string());
+ self
+ }
+}
+
+// TODO: Should all the options be exposed as public? Drawback
+// would be loosing the ability to panic when the user options
+// are wrong (e.g empty client id) or aggressive (keep alive time)
+/// Options to configure the behaviour of MQTT connection
+#[derive(Clone)]
+pub struct MqttOptions {
+ /// broker address that you want to connect to
+ broker_addr: String,
+ /// broker port
+ port: u16,
+ // What transport protocol to use
+ transport: Transport,
+ /// MQTT protocol version
+ protocol: Protocol,
+ /// keep alive time to send pingreq to broker when the connection is idle
+ keep_alive: Duration,
+ /// clean (or) persistent session
+ clean_session: bool,
+ /// client identifier
+ client_id: String,
+ /// username and password
+ credentials: Option<(String, String)>,
+ /// maximum incoming packet size (verifies remaining length of the packet)
+ max_incoming_packet_size: usize,
+ /// Maximum outgoing packet size (only verifies publish payload size)
+ max_outgoing_packet_size: usize,
+ /// request (publish, subscribe) channel capacity
+ request_channel_capacity: usize,
+ /// Max internal request batching
+ max_request_batch: usize,
+ /// Minimum delay time between consecutive outgoing packets
+ /// while retransmitting pending packets
+ pending_throttle: Duration,
+ /// maximum number of outgoing inflight messages
+ inflight: u16,
+ /// Last will that will be issued on unexpected disconnect
+ last_will: Option<LastWill>,
+ /// If set to `true` MQTT acknowledgements are not sent automatically.
+ /// Every incoming publish packet must be manually acknowledged with `client.ack(...)` method.
+ manual_acks: bool,
+ #[cfg(feature = "proxy")]
+ /// Proxy configuration.
+ proxy: Option<Proxy>,
+ #[cfg(feature = "websocket")]
+ request_modifier: Option<RequestModifierFn>,
+}
+
+impl MqttOptions {
+ /// Create an [`MqttOptions`] object that contains default values for all settings other than
+ /// - id: A string to identify the device connecting to a broker
+ /// - host: The broker's domain name or IP address
+ /// - port: The port number on which broker must be listening for incoming connections
+ ///
+ /// ```
+ /// # use rumqttc::MqttOptions;
+ /// let options = MqttOptions::new("123", "localhost", 1883);
+ /// ```
+ pub fn new<S: Into<String>, T: Into<String>>(id: S, host: T, port: u16) -> MqttOptions {
+ MqttOptions {
+ broker_addr: host.into(),
+ port,
+ transport: Transport::tcp(),
+ protocol: Protocol::V4,
+ keep_alive: Duration::from_secs(60),
+ clean_session: true,
+ client_id: id.into(),
+ credentials: None,
+ max_incoming_packet_size: 10 * 1024,
+ max_outgoing_packet_size: 10 * 1024,
+ request_channel_capacity: 10,
+ max_request_batch: 0,
+ pending_throttle: Duration::from_micros(0),
+ inflight: 100,
+ last_will: None,
+ manual_acks: false,
+ #[cfg(feature = "proxy")]
+ proxy: None,
+ #[cfg(feature = "websocket")]
+ request_modifier: None,
+ }
+ }
+
+ #[cfg(feature = "url")]
+ /// Creates an [`MqttOptions`] object by parsing provided string with the [url] crate's
+ /// [`Url::parse(url)`](url::Url::parse) method and is only enabled when run using the "url" feature.
+ ///
+ /// ```
+ /// # use rumqttc::MqttOptions;
+ /// let options = MqttOptions::parse_url("mqtt://example.com:1883?client_id=123").unwrap();
+ /// ```
+ ///
+ /// **NOTE:** A url must be prefixed with one of either `tcp://`, `mqtt://`, `ssl://`,`mqtts://`,
+ /// `ws://` or `wss://` to denote the protocol for establishing a connection with the broker.
+ ///
+ /// **NOTE:** Encrypted connections(i.e. `mqtts://`, `ssl://`, `wss://`) by default use the
+ /// system's root certificates. To configure with custom certificates, one may use the
+ /// [`set_transport`](MqttOptions::set_transport) method.
+ ///
+ /// ```ignore
+ /// # use rumqttc::{MqttOptions, Transport};
+ /// # use tokio_rustls::rustls::ClientConfig;
+ /// # let root_cert_store = rustls::RootCertStore::empty();
+ /// # let client_config = ClientConfig::builder()
+ /// # .with_root_certificates(root_cert_store)
+ /// # .with_no_client_auth();
+ /// let mut options = MqttOptions::parse_url("mqtts://example.com?client_id=123").unwrap();
+ /// options.set_transport(Transport::tls_with_config(client_config.into()));
+ /// ```
+ pub fn parse_url<S: Into<String>>(url: S) -> Result<MqttOptions, OptionError> {
+ let url = url::Url::parse(&url.into())?;
+ let options = MqttOptions::try_from(url)?;
+
+ Ok(options)
+ }
+
+ /// Broker address
+ pub fn broker_address(&self) -> (String, u16) {
+ (self.broker_addr.clone(), self.port)
+ }
+
+ pub fn set_last_will(&mut self, will: LastWill) -> &mut Self {
+ self.last_will = Some(will);
+ self
+ }
+
+ pub fn last_will(&self) -> Option<LastWill> {
+ self.last_will.clone()
+ }
+
+ pub fn set_transport(&mut self, transport: Transport) -> &mut Self {
+ self.transport = transport;
+ self
+ }
+
+ pub fn transport(&self) -> Transport {
+ self.transport.clone()
+ }
+
+ pub fn set_protocol(&mut self, protocol: Protocol) -> &mut Self {
+ self.protocol = protocol;
+ self
+ }
+
+ pub fn protocol(&self) -> Protocol {
+ self.protocol
+ }
+
+ /// Set number of seconds after which client should ping the broker
+ /// if there is no other data exchange
+ pub fn set_keep_alive(&mut self, duration: Duration) -> &mut Self {
+ assert!(
+ duration.is_zero() || duration >= Duration::from_secs(1),
+ "Keep alives should be specified in seconds. Durations less than \
+ a second are not allowed, except for Duration::ZERO."
+ );
+
+ self.keep_alive = duration;
+ self
+ }
+
+ /// Keep alive time
+ pub fn keep_alive(&self) -> Duration {
+ self.keep_alive
+ }
+
+ /// Client identifier
+ pub fn client_id(&self) -> String {
+ self.client_id.clone()
+ }
+
+ /// Set packet size limit for outgoing an incoming packets
+ pub fn set_max_packet_size(&mut self, incoming: usize, outgoing: usize) -> &mut Self {
+ self.max_incoming_packet_size = incoming;
+ self.max_outgoing_packet_size = outgoing;
+ self
+ }
+
+ /// Maximum packet size
+ pub fn max_packet_size(&self) -> usize {
+ self.max_incoming_packet_size
+ }
+
+ /// `clean_session = true` removes all the state from queues & instructs the broker
+ /// to clean all the client state when client disconnects.
+ ///
+ /// When set `false`, broker will hold the client state and performs pending
+ /// operations on the client when reconnection with same `client_id`
+ /// happens. Local queue state is also held to retransmit packets after reconnection.
+ ///
+ /// # Panic
+ ///
+ /// Panics if `clean_session` is false when `client_id` is empty.
+ ///
+ /// ```should_panic
+ /// # use rumqttc::MqttOptions;
+ /// let mut options = MqttOptions::new("", "localhost", 1883);
+ /// options.set_clean_session(false);
+ /// ```
+ pub fn set_clean_session(&mut self, clean_session: bool) -> &mut Self {
+ assert!(
+ !self.client_id.is_empty() || clean_session,
+ "Cannot unset clean session when client id is empty"
+ );
+ self.clean_session = clean_session;
+ self
+ }
+
+ /// Clean session
+ pub fn clean_session(&self) -> bool {
+ self.clean_session
+ }
+
+ /// Username and password
+ pub fn set_credentials<U: Into<String>, P: Into<String>>(
+ &mut self,
+ username: U,
+ password: P,
+ ) -> &mut Self {
+ self.credentials = Some((username.into(), password.into()));
+ self
+ }
+
+ /// Security options
+ pub fn credentials(&self) -> Option<(String, String)> {
+ self.credentials.clone()
+ }
+
+ /// Set request channel capacity
+ pub fn set_request_channel_capacity(&mut self, capacity: usize) -> &mut Self {
+ self.request_channel_capacity = capacity;
+ self
+ }
+
+ /// Request channel capacity
+ pub fn request_channel_capacity(&self) -> usize {
+ self.request_channel_capacity
+ }
+
+ /// Enables throttling and sets outoing message rate to the specified 'rate'
+ pub fn set_pending_throttle(&mut self, duration: Duration) -> &mut Self {
+ self.pending_throttle = duration;
+ self
+ }
+
+ /// Outgoing message rate
+ pub fn pending_throttle(&self) -> Duration {
+ self.pending_throttle
+ }
+
+ /// Set number of concurrent in flight messages
+ pub fn set_inflight(&mut self, inflight: u16) -> &mut Self {
+ assert!(inflight != 0, "zero in flight is not allowed");
+
+ self.inflight = inflight;
+ self
+ }
+
+ /// Number of concurrent in flight messages
+ pub fn inflight(&self) -> u16 {
+ self.inflight
+ }
+
+ /// set manual acknowledgements
+ pub fn set_manual_acks(&mut self, manual_acks: bool) -> &mut Self {
+ self.manual_acks = manual_acks;
+ self
+ }
+
+ /// get manual acknowledgements
+ pub fn manual_acks(&self) -> bool {
+ self.manual_acks
+ }
+
+ #[cfg(feature = "proxy")]
+ pub fn set_proxy(&mut self, proxy: Proxy) -> &mut Self {
+ self.proxy = Some(proxy);
+ self
+ }
+
+ #[cfg(feature = "proxy")]
+ pub fn proxy(&self) -> Option<Proxy> {
+ self.proxy.clone()
+ }
+
+ #[cfg(feature = "websocket")]
+ pub fn set_request_modifier<F, O>(&mut self, request_modifier: F) -> &mut Self
+ where
+ F: Fn(http::Request<()>) -> O + Send + Sync + 'static,
+ O: IntoFuture<Output = http::Request<()>> + 'static,
+ O::IntoFuture: Send,
+ {
+ self.request_modifier = Some(Arc::new(move |request| {
+ let request_modifier = request_modifier(request).into_future();
+ Box::pin(request_modifier)
+ }));
+
+ self
+ }
+
+ #[cfg(feature = "websocket")]
+ pub fn request_modifier(&self) -> Option<RequestModifierFn> {
+ self.request_modifier.clone()
+ }
+}
+
+#[cfg(feature = "url")]
+#[derive(Debug, PartialEq, Eq, thiserror::Error)]
+pub enum OptionError {
+ #[error("Unsupported URL scheme.")]
+ Scheme,
+
+ #[error("Missing client ID.")]
+ ClientId,
+
+ #[error("Invalid keep-alive value.")]
+ KeepAlive,
+
+ #[error("Invalid clean-session value.")]
+ CleanSession,
+
+ #[error("Invalid max-incoming-packet-size value.")]
+ MaxIncomingPacketSize,
+
+ #[error("Invalid max-outgoing-packet-size value.")]
+ MaxOutgoingPacketSize,
+
+ #[error("Invalid request-channel-capacity value.")]
+ RequestChannelCapacity,
+
+ #[error("Invalid max-request-batch value.")]
+ MaxRequestBatch,
+
+ #[error("Invalid pending-throttle value.")]
+ PendingThrottle,
+
+ #[error("Invalid inflight value.")]
+ Inflight,
+
+ #[error("Unknown option: {0}")]
+ Unknown(String),
+
+ #[error("Couldn't parse option from url: {0}")]
+ Parse(#[from] url::ParseError),
+}
+
+#[cfg(feature = "url")]
+impl std::convert::TryFrom<url::Url> for MqttOptions {
+ type Error = OptionError;
+
+ fn try_from(url: url::Url) -> Result<Self, Self::Error> {
+ use std::collections::HashMap;
+
+ let host = url.host_str().unwrap_or_default().to_owned();
+
+ let (transport, default_port) = match url.scheme() {
+ // Encrypted connections are supported, but require explicit TLS configuration. We fall
+ // back to the unencrypted transport layer, so that `set_transport` can be used to
+ // configure the encrypted transport layer with the provided TLS configuration.
+ #[cfg(feature = "use-rustls")]
+ "mqtts" | "ssl" => (Transport::tls_with_default_config(), 8883),
+ "mqtt" | "tcp" => (Transport::Tcp, 1883),
+ #[cfg(feature = "websocket")]
+ "ws" => (Transport::Ws, 8000),
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ "wss" => (Transport::wss_with_default_config(), 8000),
+ _ => return Err(OptionError::Scheme),
+ };
+
+ let port = url.port().unwrap_or(default_port);
+
+ let mut queries = url.query_pairs().collect::<HashMap<_, _>>();
+
+ let id = queries
+ .remove("client_id")
+ .ok_or(OptionError::ClientId)?
+ .into_owned();
+
+ let mut options = MqttOptions::new(id, host, port);
+ options.set_transport(transport);
+
+ if let Some(keep_alive) = queries
+ .remove("keep_alive_secs")
+ .map(|v| v.parse::<u64>().map_err(|_| OptionError::KeepAlive))
+ .transpose()?
+ {
+ options.set_keep_alive(Duration::from_secs(keep_alive));
+ }
+
+ if let Some(clean_session) = queries
+ .remove("clean_session")
+ .map(|v| v.parse::<bool>().map_err(|_| OptionError::CleanSession))
+ .transpose()?
+ {
+ options.set_clean_session(clean_session);
+ }
+
+ if let Some((username, password)) = {
+ match url.username() {
+ "" => None,
+ username => Some((
+ username.to_owned(),
+ url.password().unwrap_or_default().to_owned(),
+ )),
+ }
+ } {
+ options.set_credentials(username, password);
+ }
+
+ if let (Some(incoming), Some(outgoing)) = (
+ queries
+ .remove("max_incoming_packet_size_bytes")
+ .map(|v| {
+ v.parse::<usize>()
+ .map_err(|_| OptionError::MaxIncomingPacketSize)
+ })
+ .transpose()?,
+ queries
+ .remove("max_outgoing_packet_size_bytes")
+ .map(|v| {
+ v.parse::<usize>()
+ .map_err(|_| OptionError::MaxOutgoingPacketSize)
+ })
+ .transpose()?,
+ ) {
+ options.set_max_packet_size(incoming, outgoing);
+ }
+
+ if let Some(request_channel_capacity) = queries
+ .remove("request_channel_capacity_num")
+ .map(|v| {
+ v.parse::<usize>()
+ .map_err(|_| OptionError::RequestChannelCapacity)
+ })
+ .transpose()?
+ {
+ options.request_channel_capacity = request_channel_capacity;
+ }
+
+ if let Some(max_request_batch) = queries
+ .remove("max_request_batch_num")
+ .map(|v| v.parse::<usize>().map_err(|_| OptionError::MaxRequestBatch))
+ .transpose()?
+ {
+ options.max_request_batch = max_request_batch;
+ }
+
+ if let Some(pending_throttle) = queries
+ .remove("pending_throttle_usecs")
+ .map(|v| v.parse::<u64>().map_err(|_| OptionError::PendingThrottle))
+ .transpose()?
+ {
+ options.set_pending_throttle(Duration::from_micros(pending_throttle));
+ }
+
+ if let Some(inflight) = queries
+ .remove("inflight_num")
+ .map(|v| v.parse::<u16>().map_err(|_| OptionError::Inflight))
+ .transpose()?
+ {
+ options.set_inflight(inflight);
+ }
+
+ if let Some((opt, _)) = queries.into_iter().next() {
+ return Err(OptionError::Unknown(opt.into_owned()));
+ }
+
+ Ok(options)
+ }
+}
+
+// Implement Debug manually because ClientConfig doesn't implement it, so derive(Debug) doesn't
+// work.
+impl Debug for MqttOptions {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ f.debug_struct("MqttOptions")
+ .field("broker_addr", &self.broker_addr)
+ .field("port", &self.port)
+ .field("protocol", &self.protocol)
+ .field("keep_alive", &self.keep_alive)
+ .field("clean_session", &self.clean_session)
+ .field("client_id", &self.client_id)
+ .field("credentials", &self.credentials)
+ .field("max_packet_size", &self.max_incoming_packet_size)
+ .field("request_channel_capacity", &self.request_channel_capacity)
+ .field("max_request_batch", &self.max_request_batch)
+ .field("pending_throttle", &self.pending_throttle)
+ .field("inflight", &self.inflight)
+ .field("last_will", &self.last_will)
+ .field("manual_acks", &self.manual_acks)
+ .finish()
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ fn no_scheme() {
+ let mut mqttoptions = MqttOptions::new("client_a", "a3f8czas.iot.eu-west-1.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=MyCreds%2F20201001%2Feu-west-1%2Fiotdevicegateway%2Faws4_request&X-Amz-Date=20201001T130812Z&X-Amz-Expires=7200&X-Amz-Signature=9ae09b49896f44270f2707551581953e6cac71a4ccf34c7c3415555be751b2d1&X-Amz-SignedHeaders=host", 443);
+
+ mqttoptions.set_transport(crate::Transport::wss(Vec::from("Test CA"), None, None));
+
+ if let crate::Transport::Wss(TlsConfiguration::Simple {
+ ca,
+ client_auth,
+ alpn,
+ }) = mqttoptions.transport
+ {
+ assert_eq!(ca, Vec::from("Test CA"));
+ assert_eq!(client_auth, None);
+ assert_eq!(alpn, None);
+ } else {
+ panic!("Unexpected transport!");
+ }
+
+ assert_eq!(mqttoptions.broker_addr, "a3f8czas.iot.eu-west-1.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=MyCreds%2F20201001%2Feu-west-1%2Fiotdevicegateway%2Faws4_request&X-Amz-Date=20201001T130812Z&X-Amz-Expires=7200&X-Amz-Signature=9ae09b49896f44270f2707551581953e6cac71a4ccf34c7c3415555be751b2d1&X-Amz-SignedHeaders=host");
+ }
+
+ #[test]
+ #[cfg(feature = "url")]
+ fn from_url() {
+ fn opt(s: &str) -> Result<MqttOptions, OptionError> {
+ MqttOptions::parse_url(s)
+ }
+ fn ok(s: &str) -> MqttOptions {
+ opt(s).expect("valid options")
+ }
+ fn err(s: &str) -> OptionError {
+ opt(s).expect_err("invalid options")
+ }
+
+ let v = ok("mqtt://host:42?client_id=foo");
+ assert_eq!(v.broker_address(), ("host".to_owned(), 42));
+ assert_eq!(v.client_id(), "foo".to_owned());
+
+ let v = ok("mqtt://host:42?client_id=foo&keep_alive_secs=5");
+ assert_eq!(v.keep_alive, Duration::from_secs(5));
+
+ assert_eq!(err("mqtt://host:42"), OptionError::ClientId);
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&foo=bar"),
+ OptionError::Unknown("foo".to_owned())
+ );
+ assert_eq!(err("mqt://host:42?client_id=foo"), OptionError::Scheme);
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&keep_alive_secs=foo"),
+ OptionError::KeepAlive
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&clean_session=foo"),
+ OptionError::CleanSession
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&max_incoming_packet_size_bytes=foo"),
+ OptionError::MaxIncomingPacketSize
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&max_outgoing_packet_size_bytes=foo"),
+ OptionError::MaxOutgoingPacketSize
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&request_channel_capacity_num=foo"),
+ OptionError::RequestChannelCapacity
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&max_request_batch_num=foo"),
+ OptionError::MaxRequestBatch
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&pending_throttle_usecs=foo"),
+ OptionError::PendingThrottle
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&inflight_num=foo"),
+ OptionError::Inflight
+ );
+ }
+
+ #[test]
+ fn accept_empty_client_id() {
+ let _mqtt_opts = MqttOptions::new("", "127.0.0.1", 1883).set_clean_session(true);
+ }
+
+ #[test]
+ fn set_clean_session_when_client_id_present() {
+ let mut options = MqttOptions::new("client_id", "127.0.0.1", 1883);
+ options.set_clean_session(false);
+ options.set_clean_session(true);
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/mod.rs b/vendor/rumqttc/src/mqttbytes/mod.rs
new file mode 100644
index 0000000000..57ddea2e91
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/mod.rs
@@ -0,0 +1,337 @@
+//! # mqttbytes
+//!
+//! This module contains the low level struct definitions required to assemble and disassemble MQTT 3.1.1 packets in rumqttc.
+//! The [`bytes`](https://docs.rs/bytes) crate is used internally.
+
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+use core::fmt;
+use std::slice::Iter;
+
+mod topic;
+pub mod v4;
+
+pub use topic::*;
+
+/// Error during serialization and deserialization
+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
+pub enum Error {
+ #[error("Expected Connect, received: {0:?}")]
+ NotConnect(PacketType),
+ #[error("Unexpected Connect")]
+ UnexpectedConnect,
+ #[error("Invalid Connect return code: {0}")]
+ InvalidConnectReturnCode(u8),
+ #[error("Invalid protocol")]
+ InvalidProtocol,
+ #[error("Invalid protocol level: {0}")]
+ InvalidProtocolLevel(u8),
+ #[error("Incorrect packet format")]
+ IncorrectPacketFormat,
+ #[error("Invalid packet type: {0}")]
+ InvalidPacketType(u8),
+ #[error("Invalid property type: {0}")]
+ InvalidPropertyType(u8),
+ #[error("Invalid QoS level: {0}")]
+ InvalidQoS(u8),
+ #[error("Invalid subscribe reason code: {0}")]
+ InvalidSubscribeReasonCode(u8),
+ #[error("Packet id Zero")]
+ PacketIdZero,
+ #[error("Payload size is incorrect")]
+ PayloadSizeIncorrect,
+ #[error("payload is too long")]
+ PayloadTooLong,
+ #[error("payload size limit exceeded: {0}")]
+ PayloadSizeLimitExceeded(usize),
+ #[error("Payload required")]
+ PayloadRequired,
+ #[error("Topic is not UTF-8")]
+ TopicNotUtf8,
+ #[error("Promised boundary crossed: {0}")]
+ BoundaryCrossed(usize),
+ #[error("Malformed packet")]
+ MalformedPacket,
+ #[error("Malformed remaining length")]
+ MalformedRemainingLength,
+ #[error("A Subscribe packet must contain atleast one filter")]
+ EmptySubscription,
+ /// More bytes required to frame packet. Argument
+ /// implies minimum additional bytes required to
+ /// proceed further
+ #[error("At least {0} more bytes required to frame packet")]
+ InsufficientBytes(usize),
+}
+
+/// MQTT packet type
+#[repr(u8)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum PacketType {
+ Connect = 1,
+ ConnAck,
+ Publish,
+ PubAck,
+ PubRec,
+ PubRel,
+ PubComp,
+ Subscribe,
+ SubAck,
+ Unsubscribe,
+ UnsubAck,
+ PingReq,
+ PingResp,
+ Disconnect,
+}
+
+/// Protocol type
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum Protocol {
+ V3,
+ V4,
+ V5,
+}
+
+/// Quality of service
+#[repr(u8)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
+pub enum QoS {
+ AtMostOnce = 0,
+ AtLeastOnce = 1,
+ ExactlyOnce = 2,
+}
+
+/// Packet type from a byte
+///
+/// ```text
+/// 7 3 0
+/// +--------------------------+--------------------------+
+/// byte 1 | MQTT Control Packet Type | Flags for each type |
+/// +--------------------------+--------------------------+
+/// | Remaining Bytes Len (1/2/3/4 bytes) |
+/// +-----------------------------------------------------+
+///
+/// <https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349207>
+/// ```
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
+pub struct FixedHeader {
+ /// First byte of the stream. Used to identify packet types and
+ /// several flags
+ byte1: u8,
+ /// Length of fixed header. Byte 1 + (1..4) bytes. So fixed header
+ /// len can vary from 2 bytes to 5 bytes
+ /// 1..4 bytes are variable length encoded to represent remaining length
+ fixed_header_len: usize,
+ /// Remaining length of the packet. Doesn't include fixed header bytes
+ /// Represents variable header + payload size
+ remaining_len: usize,
+}
+
+impl FixedHeader {
+ pub fn new(byte1: u8, remaining_len_len: usize, remaining_len: usize) -> FixedHeader {
+ FixedHeader {
+ byte1,
+ fixed_header_len: remaining_len_len + 1,
+ remaining_len,
+ }
+ }
+
+ pub fn packet_type(&self) -> Result<PacketType, Error> {
+ let num = self.byte1 >> 4;
+ match num {
+ 1 => Ok(PacketType::Connect),
+ 2 => Ok(PacketType::ConnAck),
+ 3 => Ok(PacketType::Publish),
+ 4 => Ok(PacketType::PubAck),
+ 5 => Ok(PacketType::PubRec),
+ 6 => Ok(PacketType::PubRel),
+ 7 => Ok(PacketType::PubComp),
+ 8 => Ok(PacketType::Subscribe),
+ 9 => Ok(PacketType::SubAck),
+ 10 => Ok(PacketType::Unsubscribe),
+ 11 => Ok(PacketType::UnsubAck),
+ 12 => Ok(PacketType::PingReq),
+ 13 => Ok(PacketType::PingResp),
+ 14 => Ok(PacketType::Disconnect),
+ _ => Err(Error::InvalidPacketType(num)),
+ }
+ }
+
+ /// Returns the size of full packet (fixed header + variable header + payload)
+ /// Fixed header is enough to get the size of a frame in the stream
+ pub fn frame_length(&self) -> usize {
+ self.fixed_header_len + self.remaining_len
+ }
+}
+
+/// Checks if the stream has enough bytes to frame a packet and returns fixed header
+/// only if a packet can be framed with existing bytes in the `stream`.
+/// The passed stream doesn't modify parent stream's cursor. If this function
+/// returned an error, next `check` on the same parent stream is forced start
+/// with cursor at 0 again (Iter is owned. Only Iter's cursor is changed internally)
+pub fn check(stream: Iter<u8>, max_packet_size: usize) -> Result<FixedHeader, Error> {
+ // Create fixed header if there are enough bytes in the stream
+ // to frame full packet
+ let stream_len = stream.len();
+ let fixed_header = parse_fixed_header(stream)?;
+
+ // Don't let rogue connections attack with huge payloads.
+ // Disconnect them before reading all that data
+ if fixed_header.remaining_len > max_packet_size {
+ return Err(Error::PayloadSizeLimitExceeded(fixed_header.remaining_len));
+ }
+
+ // If the current call fails due to insufficient bytes in the stream,
+ // after calculating remaining length, we extend the stream
+ let frame_length = fixed_header.frame_length();
+ if stream_len < frame_length {
+ return Err(Error::InsufficientBytes(frame_length - stream_len));
+ }
+
+ Ok(fixed_header)
+}
+
+/// Parses fixed header
+fn parse_fixed_header(mut stream: Iter<u8>) -> Result<FixedHeader, Error> {
+ // At least 2 bytes are necessary to frame a packet
+ let stream_len = stream.len();
+ if stream_len < 2 {
+ return Err(Error::InsufficientBytes(2 - stream_len));
+ }
+
+ let byte1 = stream.next().unwrap();
+ let (len_len, len) = length(stream)?;
+
+ Ok(FixedHeader::new(*byte1, len_len, len))
+}
+
+/// Parses variable byte integer in the stream and returns the length
+/// and number of bytes that make it. Used for remaining length calculation
+/// as well as for calculating property lengths
+fn length(stream: Iter<u8>) -> Result<(usize, usize), Error> {
+ let mut len: usize = 0;
+ let mut len_len = 0;
+ let mut done = false;
+ let mut shift = 0;
+
+ // Use continuation bit at position 7 to continue reading next
+ // byte to frame 'length'.
+ // Stream 0b1xxx_xxxx 0b1yyy_yyyy 0b1zzz_zzzz 0b0www_wwww will
+ // be framed as number 0bwww_wwww_zzz_zzzz_yyy_yyyy_xxx_xxxx
+ for byte in stream {
+ len_len += 1;
+ let byte = *byte as usize;
+ len += (byte & 0x7F) << shift;
+
+ // stop when continue bit is 0
+ done = (byte & 0x80) == 0;
+ if done {
+ break;
+ }
+
+ shift += 7;
+
+ // Only a max of 4 bytes allowed for remaining length
+ // more than 4 shifts (0, 7, 14, 21) implies bad length
+ if shift > 21 {
+ return Err(Error::MalformedRemainingLength);
+ }
+ }
+
+ // Not enough bytes to frame remaining length. wait for
+ // one more byte
+ if !done {
+ return Err(Error::InsufficientBytes(1));
+ }
+
+ Ok((len_len, len))
+}
+
+/// Reads a series of bytes with a length from a byte stream
+fn read_mqtt_bytes(stream: &mut Bytes) -> Result<Bytes, Error> {
+ let len = read_u16(stream)? as usize;
+
+ // Prevent attacks with wrong remaining length. This method is used in
+ // `packet.assembly()` with (enough) bytes to frame packet. Ensures that
+ // reading variable len string or bytes doesn't cross promised boundary
+ // with `read_fixed_header()`
+ if len > stream.len() {
+ return Err(Error::BoundaryCrossed(len));
+ }
+
+ Ok(stream.split_to(len))
+}
+
+/// Reads a string from bytes stream
+fn read_mqtt_string(stream: &mut Bytes) -> Result<String, Error> {
+ let s = read_mqtt_bytes(stream)?;
+ match String::from_utf8(s.to_vec()) {
+ Ok(v) => Ok(v),
+ Err(_e) => Err(Error::TopicNotUtf8),
+ }
+}
+
+/// Serializes bytes to stream (including length)
+fn write_mqtt_bytes(stream: &mut BytesMut, bytes: &[u8]) {
+ stream.put_u16(bytes.len() as u16);
+ stream.extend_from_slice(bytes);
+}
+
+/// Serializes a string to stream
+fn write_mqtt_string(stream: &mut BytesMut, string: &str) {
+ write_mqtt_bytes(stream, string.as_bytes());
+}
+
+/// Writes remaining length to stream and returns number of bytes for remaining length
+fn write_remaining_length(stream: &mut BytesMut, len: usize) -> Result<usize, Error> {
+ if len > 268_435_455 {
+ return Err(Error::PayloadTooLong);
+ }
+
+ let mut done = false;
+ let mut x = len;
+ let mut count = 0;
+
+ while !done {
+ let mut byte = (x % 128) as u8;
+ x /= 128;
+ if x > 0 {
+ byte |= 128;
+ }
+
+ stream.put_u8(byte);
+ count += 1;
+ done = x == 0;
+ }
+
+ Ok(count)
+}
+
+/// Maps a number to QoS
+pub fn qos(num: u8) -> Result<QoS, Error> {
+ match num {
+ 0 => Ok(QoS::AtMostOnce),
+ 1 => Ok(QoS::AtLeastOnce),
+ 2 => Ok(QoS::ExactlyOnce),
+ qos => Err(Error::InvalidQoS(qos)),
+ }
+}
+
+/// After collecting enough bytes to frame a packet (packet's frame())
+/// , It's possible that content itself in the stream is wrong. Like expected
+/// packet id or qos not being present. In cases where `read_mqtt_string` or
+/// `read_mqtt_bytes` exhausted remaining length but packet framing expects to
+/// parse qos next, these pre checks will prevent `bytes` crashes
+fn read_u16(stream: &mut Bytes) -> Result<u16, Error> {
+ if stream.len() < 2 {
+ return Err(Error::MalformedPacket);
+ }
+
+ Ok(stream.get_u16())
+}
+
+fn read_u8(stream: &mut Bytes) -> Result<u8, Error> {
+ if stream.is_empty() {
+ return Err(Error::MalformedPacket);
+ }
+
+ Ok(stream.get_u8())
+}
diff --git a/vendor/rumqttc/src/mqttbytes/topic.rs b/vendor/rumqttc/src/mqttbytes/topic.rs
new file mode 100644
index 0000000000..a66c35f85e
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/topic.rs
@@ -0,0 +1,214 @@
+/// Checks if a topic or topic filter has wildcards
+pub fn has_wildcards(s: &str) -> bool {
+ s.contains('+') || s.contains('#')
+}
+
+/// Checks if a topic is valid
+pub fn valid_topic(topic: &str) -> bool {
+ // topic can't contain wildcards
+ if topic.contains('+') || topic.contains('#') {
+ return false;
+ }
+
+ true
+}
+
+/// Checks if the filter is valid
+///
+/// <https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718106>
+pub fn valid_filter(filter: &str) -> bool {
+ if filter.is_empty() {
+ return false;
+ }
+
+ // rev() is used so we can easily get the last entry
+ let mut hirerarchy = filter.split('/').rev();
+
+ // split will never return an empty iterator
+ // even if the pattern isn't matched, the original string will be there
+ // so it is safe to just unwrap here!
+ let last = hirerarchy.next().unwrap();
+
+ // only single '#" or '+' is allowed in last entry
+ // invalid: sport/tennis#
+ // invalid: sport/++
+ if last.len() != 1 && (last.contains('#') || last.contains('+')) {
+ return false;
+ }
+
+ // remaining entries
+ for entry in hirerarchy {
+ // # is not allowed in filter except as a last entry
+ // invalid: sport/tennis#/player
+ // invalid: sport/tennis/#/ranking
+ if entry.contains('#') {
+ return false;
+ }
+
+ // + must occupy an entire level of the filter
+ // invalid: sport+
+ if entry.len() > 1 && entry.contains('+') {
+ return false;
+ }
+ }
+
+ true
+}
+
+/// Checks if topic matches a filter. topic and filter validation isn't done here.
+///
+/// **NOTE**: 'topic' is a misnomer in the arg. this can also be used to match 2 wild subscriptions
+/// **NOTE**: make sure a topic is validated during a publish and filter is validated
+/// during a subscribe
+pub fn matches(topic: &str, filter: &str) -> bool {
+ if !topic.is_empty() && topic[..1].contains('$') {
+ return false;
+ }
+
+ let mut topics = topic.split('/');
+ let mut filters = filter.split('/');
+
+ for f in filters.by_ref() {
+ // "#" being the last element is validated by the broker with 'valid_filter'
+ if f == "#" {
+ return true;
+ }
+
+ // filter still has remaining elements
+ // filter = a/b/c/# should match topci = a/b/c
+ // filter = a/b/c/d should not match topic = a/b/c
+ let top = topics.next();
+ match top {
+ Some("#") => return false,
+ Some(_) if f == "+" => continue,
+ Some(t) if f != t => return false,
+ Some(_) => continue,
+ None => return false,
+ }
+ }
+
+ // topic has remaining elements and filter's last element isn't "#"
+ if topics.next().is_some() {
+ return false;
+ }
+
+ true
+}
+
+#[cfg(test)]
+mod test {
+ #[test]
+ fn wildcards_are_detected_correctly() {
+ assert!(!super::has_wildcards("a/b/c"));
+ assert!(super::has_wildcards("a/+/c"));
+ assert!(super::has_wildcards("a/b/#"));
+ }
+
+ #[test]
+ fn topics_are_validated_correctly() {
+ assert!(!super::valid_topic("+wrong"));
+ assert!(!super::valid_topic("wro#ng"));
+ assert!(!super::valid_topic("w/r/o/n/g+"));
+ assert!(!super::valid_topic("wrong/#/path"));
+ }
+
+ #[test]
+ fn filters_are_validated_correctly() {
+ assert!(!super::valid_filter("wrong/#/filter"));
+ assert!(!super::valid_filter("wrong/wr#ng/filter"));
+ assert!(!super::valid_filter("wrong/filter#"));
+ assert!(super::valid_filter("correct/filter/#"));
+ assert!(!super::valid_filter("wr/o+/ng"));
+ assert!(!super::valid_filter("wr/+o+/ng"));
+ assert!(!super::valid_filter("wron/+g"));
+ assert!(super::valid_filter("cor/+/rect/+"));
+ }
+
+ #[test]
+ fn zero_len_subscriptions_are_not_allowed() {
+ assert!(!super::valid_filter(""));
+ }
+
+ #[test]
+ fn dollar_subscriptions_doesnt_match_dollar_topic() {
+ assert!(super::matches("sy$tem/metrics", "sy$tem/+"));
+ assert!(!super::matches("$system/metrics", "$system/+"));
+ assert!(!super::matches("$system/metrics", "+/+"));
+ }
+
+ #[test]
+ fn topics_match_with_filters_as_expected() {
+ let topic = "a/b/c";
+ let filter = "a/b/c";
+ assert!(super::matches(topic, filter));
+
+ let topic = "a/b/c";
+ let filter = "d/b/c";
+ assert!(!super::matches(topic, filter));
+
+ let topic = "a/b/c";
+ let filter = "a/b/e";
+ assert!(!super::matches(topic, filter));
+
+ let topic = "a/b/c";
+ let filter = "a/b/c/d";
+ assert!(!super::matches(topic, filter));
+
+ let topic = "a/b/c";
+ let filter = "#";
+ assert!(super::matches(topic, filter));
+
+ let topic = "a/b/c";
+ let filter = "a/b/c/#";
+ assert!(super::matches(topic, filter));
+
+ let topic = "a/b/c/d";
+ let filter = "a/b/c";
+ assert!(!super::matches(topic, filter));
+
+ let topic = "a/b/c/d";
+ let filter = "a/b/c/#";
+ assert!(super::matches(topic, filter));
+
+ let topic = "a/b/c/d/e/f";
+ let filter = "a/b/c/#";
+ assert!(super::matches(topic, filter));
+
+ let topic = "a/b/c";
+ let filter = "a/+/c";
+ assert!(super::matches(topic, filter));
+ let topic = "a/b/c/d/e";
+ let filter = "a/+/c/+/e";
+ assert!(super::matches(topic, filter));
+
+ let topic = "a/b";
+ let filter = "a/b/+";
+ assert!(!super::matches(topic, filter));
+
+ let filter1 = "a/b/+";
+ let filter2 = "a/b/#";
+ assert!(super::matches(filter1, filter2));
+ assert!(!super::matches(filter2, filter1));
+
+ let filter1 = "a/b/+";
+ let filter2 = "#";
+ assert!(super::matches(filter1, filter2));
+
+ let filter1 = "a/+/c/d";
+ let filter2 = "a/+/+/d";
+ assert!(super::matches(filter1, filter2));
+ assert!(!super::matches(filter2, filter1));
+
+ let filter1 = "a/b/c/d/e";
+ let filter2 = "a/+/+/+/e";
+ assert!(super::matches(filter1, filter2));
+
+ let filter1 = "a/+/c/+/e";
+ let filter2 = "a/+/+/+/e";
+ assert!(super::matches(filter1, filter2));
+
+ let filter1 = "a/+/+/+/e";
+ let filter2 = "a/+/+/+/e";
+ assert!(super::matches(filter1, filter2));
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/connack.rs b/vendor/rumqttc/src/mqttbytes/v4/connack.rs
new file mode 100644
index 0000000000..4539195150
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/connack.rs
@@ -0,0 +1,124 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// Return code in connack
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[repr(u8)]
+pub enum ConnectReturnCode {
+ Success = 0,
+ RefusedProtocolVersion,
+ BadClientId,
+ ServiceUnavailable,
+ BadUserNamePassword,
+ NotAuthorized,
+}
+
+/// Acknowledgement to connect packet
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ConnAck {
+ pub session_present: bool,
+ pub code: ConnectReturnCode,
+}
+
+impl ConnAck {
+ pub fn new(code: ConnectReturnCode, session_present: bool) -> ConnAck {
+ ConnAck {
+ session_present,
+ code,
+ }
+ }
+
+ fn len(&self) -> usize {
+ // sesssion present + code
+
+ 1 + 1
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+
+ let flags = read_u8(&mut bytes)?;
+ let return_code = read_u8(&mut bytes)?;
+
+ let session_present = (flags & 0x01) == 1;
+ let code = connect_return(return_code)?;
+ let connack = ConnAck {
+ session_present,
+ code,
+ };
+
+ Ok(connack)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+ buffer.put_u8(0x20);
+
+ let count = write_remaining_length(buffer, len)?;
+ buffer.put_u8(self.session_present as u8);
+ buffer.put_u8(self.code as u8);
+
+ Ok(1 + count + len)
+ }
+}
+
+/// Connection return code type
+fn connect_return(num: u8) -> Result<ConnectReturnCode, Error> {
+ match num {
+ 0 => Ok(ConnectReturnCode::Success),
+ 1 => Ok(ConnectReturnCode::RefusedProtocolVersion),
+ 2 => Ok(ConnectReturnCode::BadClientId),
+ 3 => Ok(ConnectReturnCode::ServiceUnavailable),
+ 4 => Ok(ConnectReturnCode::BadUserNamePassword),
+ 5 => Ok(ConnectReturnCode::NotAuthorized),
+ num => Err(Error::InvalidConnectReturnCode(num)),
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn connack_parsing_works() {
+ let mut stream = bytes::BytesMut::new();
+ let packetstream = &[
+ 0b0010_0000,
+ 0x02, // packet type, flags and remaining len
+ 0x01,
+ 0x00, // variable header. connack flags, connect return code
+ 0xDE,
+ 0xAD,
+ 0xBE,
+ 0xEF, // extra packets in the stream
+ ];
+
+ stream.extend_from_slice(&packetstream[..]);
+ let fixed_header = parse_fixed_header(stream.iter()).unwrap();
+ let connack_bytes = stream.split_to(fixed_header.frame_length()).freeze();
+ let connack = ConnAck::read(fixed_header, connack_bytes).unwrap();
+
+ assert_eq!(
+ connack,
+ ConnAck {
+ session_present: true,
+ code: ConnectReturnCode::Success,
+ }
+ );
+ }
+
+ #[test]
+ fn connack_encoding_works() {
+ let connack = ConnAck {
+ session_present: true,
+ code: ConnectReturnCode::Success,
+ };
+
+ let mut buf = BytesMut::new();
+ connack.write(&mut buf).unwrap();
+ assert_eq!(buf, vec![0b0010_0000, 0x02, 0x01, 0x00]);
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/connect.rs b/vendor/rumqttc/src/mqttbytes/v4/connect.rs
new file mode 100644
index 0000000000..68b046d090
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/connect.rs
@@ -0,0 +1,444 @@
+use super::*;
+use bytes::{Buf, Bytes};
+
+/// Connection packet initiated by the client
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Connect {
+ /// Mqtt protocol version
+ pub protocol: Protocol,
+ /// Mqtt keep alive time
+ pub keep_alive: u16,
+ /// Client Id
+ pub client_id: String,
+ /// Clean session. Asks the broker to clear previous state
+ pub clean_session: bool,
+ /// Will that broker needs to publish when the client disconnects
+ pub last_will: Option<LastWill>,
+ /// Login credentials
+ pub login: Option<Login>,
+}
+
+impl Connect {
+ pub fn new<S: Into<String>>(id: S) -> Connect {
+ Connect {
+ protocol: Protocol::V4,
+ keep_alive: 10,
+ client_id: id.into(),
+ clean_session: true,
+ last_will: None,
+ login: None,
+ }
+ }
+
+ pub fn set_login<U: Into<String>, P: Into<String>>(&mut self, u: U, p: P) -> &mut Connect {
+ let login = Login {
+ username: u.into(),
+ password: p.into(),
+ };
+
+ self.login = Some(login);
+ self
+ }
+
+ fn len(&self) -> usize {
+ let protocol_name = match self.protocol {
+ Protocol::V3 => "MQIsdp",
+ Protocol::V4 | Protocol::V5 => "MQTT",
+ };
+
+ let mut len = 2 + protocol_name.len() // protocol name
+ + 1 // protocol version
+ + 1 // connect flags
+ + 2; // keep alive
+
+ len += 2 + self.client_id.len();
+
+ // last will len
+ if let Some(last_will) = &self.last_will {
+ len += last_will.len();
+ }
+
+ // username and password len
+ if let Some(login) = &self.login {
+ len += login.len();
+ }
+
+ len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Connect, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+
+ // Variable header
+ let protocol_name = read_mqtt_string(&mut bytes)?;
+ let protocol_level = read_u8(&mut bytes)?;
+ if protocol_name != "MQTT" && protocol_name != "MQIsdp" {
+ return Err(Error::InvalidProtocol);
+ }
+
+ let protocol = match protocol_level {
+ 3 => Protocol::V3,
+ 4 => Protocol::V4,
+ 5 => Protocol::V5,
+ num => return Err(Error::InvalidProtocolLevel(num)),
+ };
+
+ let connect_flags = read_u8(&mut bytes)?;
+ let clean_session = (connect_flags & 0b10) != 0;
+ let keep_alive = read_u16(&mut bytes)?;
+
+ let client_id = read_mqtt_string(&mut bytes)?;
+ let last_will = LastWill::read(connect_flags, &mut bytes)?;
+ let login = Login::read(connect_flags, &mut bytes)?;
+
+ let connect = Connect {
+ protocol,
+ keep_alive,
+ client_id,
+ clean_session,
+ last_will,
+ login,
+ };
+
+ Ok(connect)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+ buffer.put_u8(0b0001_0000);
+ let count = write_remaining_length(buffer, len)?;
+ let protocol_name = match self.protocol {
+ Protocol::V3 => "MQIsdp",
+ Protocol::V4 | Protocol::V5 => "MQTT",
+ };
+ write_mqtt_string(buffer, protocol_name);
+
+ match self.protocol {
+ Protocol::V3 => buffer.put_u8(0x03),
+ Protocol::V4 => buffer.put_u8(0x04),
+ Protocol::V5 => buffer.put_u8(0x05),
+ }
+
+ let flags_index = 1 + count + 2 + protocol_name.len() + 1;
+
+ let mut connect_flags = 0;
+ if self.clean_session {
+ connect_flags |= 0x02;
+ }
+
+ buffer.put_u8(connect_flags);
+ buffer.put_u16(self.keep_alive);
+ write_mqtt_string(buffer, &self.client_id);
+
+ if let Some(last_will) = &self.last_will {
+ connect_flags |= last_will.write(buffer)?;
+ }
+
+ if let Some(login) = &self.login {
+ connect_flags |= login.write(buffer);
+ }
+
+ // update connect flags
+ buffer[flags_index] = connect_flags;
+ Ok(len)
+ }
+}
+
+/// LastWill that broker forwards on behalf of the client
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct LastWill {
+ pub topic: String,
+ pub message: Bytes,
+ pub qos: QoS,
+ pub retain: bool,
+}
+
+impl LastWill {
+ pub fn new(
+ topic: impl Into<String>,
+ payload: impl Into<Vec<u8>>,
+ qos: QoS,
+ retain: bool,
+ ) -> LastWill {
+ LastWill {
+ topic: topic.into(),
+ message: Bytes::from(payload.into()),
+ qos,
+ retain,
+ }
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 0;
+ len += 2 + self.topic.len() + 2 + self.message.len();
+ len
+ }
+
+ fn read(connect_flags: u8, bytes: &mut Bytes) -> Result<Option<LastWill>, Error> {
+ let last_will = match connect_flags & 0b100 {
+ 0 if (connect_flags & 0b0011_1000) != 0 => {
+ return Err(Error::IncorrectPacketFormat);
+ }
+ 0 => None,
+ _ => {
+ let will_topic = read_mqtt_string(bytes)?;
+ let will_message = read_mqtt_bytes(bytes)?;
+ let will_qos = qos((connect_flags & 0b11000) >> 3)?;
+ Some(LastWill {
+ topic: will_topic,
+ message: will_message,
+ qos: will_qos,
+ retain: (connect_flags & 0b0010_0000) != 0,
+ })
+ }
+ };
+
+ Ok(last_will)
+ }
+
+ fn write(&self, buffer: &mut BytesMut) -> Result<u8, Error> {
+ let mut connect_flags = 0;
+
+ connect_flags |= 0x04 | (self.qos as u8) << 3;
+ if self.retain {
+ connect_flags |= 0x20;
+ }
+
+ write_mqtt_string(buffer, &self.topic);
+ write_mqtt_bytes(buffer, &self.message);
+ Ok(connect_flags)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Login {
+ pub username: String,
+ pub password: String,
+}
+
+impl Login {
+ pub fn new<U: Into<String>, P: Into<String>>(u: U, p: P) -> Login {
+ Login {
+ username: u.into(),
+ password: p.into(),
+ }
+ }
+
+ fn read(connect_flags: u8, bytes: &mut Bytes) -> Result<Option<Login>, Error> {
+ let username = match connect_flags & 0b1000_0000 {
+ 0 => String::new(),
+ _ => read_mqtt_string(bytes)?,
+ };
+
+ let password = match connect_flags & 0b0100_0000 {
+ 0 => String::new(),
+ _ => read_mqtt_string(bytes)?,
+ };
+
+ if username.is_empty() && password.is_empty() {
+ Ok(None)
+ } else {
+ Ok(Some(Login { username, password }))
+ }
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if !self.username.is_empty() {
+ len += 2 + self.username.len();
+ }
+
+ if !self.password.is_empty() {
+ len += 2 + self.password.len();
+ }
+
+ len
+ }
+
+ fn write(&self, buffer: &mut BytesMut) -> u8 {
+ let mut connect_flags = 0;
+ if !self.username.is_empty() {
+ connect_flags |= 0x80;
+ write_mqtt_string(buffer, &self.username);
+ }
+
+ if !self.password.is_empty() {
+ connect_flags |= 0x40;
+ write_mqtt_string(buffer, &self.password);
+ }
+
+ connect_flags
+ }
+
+ pub fn validate(&self, username: &str, password: &str) -> bool {
+ (self.username == *username) && (self.password == *password)
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn connect_parsing_works() {
+ let mut stream = bytes::BytesMut::new();
+ let packetstream = &[
+ 0x10,
+ 39, // packet type, flags and remaining len
+ 0x00,
+ 0x04,
+ b'M',
+ b'Q',
+ b'T',
+ b'T',
+ 0x04, // variable header
+ 0b1100_1110, // variable header. +username, +password, -will retain, will qos=1, +last_will, +clean_session
+ 0x00,
+ 0x0a, // variable header. keep alive = 10 sec
+ 0x00,
+ 0x04,
+ b't',
+ b'e',
+ b's',
+ b't', // payload. client_id
+ 0x00,
+ 0x02,
+ b'/',
+ b'a', // payload. will topic = '/a'
+ 0x00,
+ 0x07,
+ b'o',
+ b'f',
+ b'f',
+ b'l',
+ b'i',
+ b'n',
+ b'e', // payload. variable header. will msg = 'offline'
+ 0x00,
+ 0x04,
+ b'r',
+ b'u',
+ b'm',
+ b'q', // payload. username = 'rumq'
+ 0x00,
+ 0x02,
+ b'm',
+ b'q', // payload. password = 'mq'
+ 0xDE,
+ 0xAD,
+ 0xBE,
+ 0xEF, // extra packets in the stream
+ ];
+
+ stream.extend_from_slice(&packetstream[..]);
+ let fixed_header = parse_fixed_header(stream.iter()).unwrap();
+ let connect_bytes = stream.split_to(fixed_header.frame_length()).freeze();
+ let packet = Connect::read(fixed_header, connect_bytes).unwrap();
+
+ assert_eq!(
+ packet,
+ Connect {
+ protocol: Protocol::V4,
+ keep_alive: 10,
+ client_id: "test".to_owned(),
+ clean_session: true,
+ last_will: Some(LastWill::new("/a", "offline", QoS::AtLeastOnce, false)),
+ login: Some(Login::new("rumq", "mq")),
+ }
+ );
+ }
+
+ fn sample_bytes() -> Vec<u8> {
+ vec![
+ 0x10,
+ 39,
+ 0x00,
+ 0x04,
+ b'M',
+ b'Q',
+ b'T',
+ b'T',
+ 0x04,
+ 0b1100_1110, // +username, +password, -will retain, will qos=1, +last_will, +clean_session
+ 0x00,
+ 0x0a, // 10 sec
+ 0x00,
+ 0x04,
+ b't',
+ b'e',
+ b's',
+ b't', // client_id
+ 0x00,
+ 0x02,
+ b'/',
+ b'a', // will topic = '/a'
+ 0x00,
+ 0x07,
+ b'o',
+ b'f',
+ b'f',
+ b'l',
+ b'i',
+ b'n',
+ b'e', // will msg = 'offline'
+ 0x00,
+ 0x04,
+ b'r',
+ b'u',
+ b's',
+ b't', // username = 'rust'
+ 0x00,
+ 0x02,
+ b'm',
+ b'q', // password = 'mq'
+ ]
+ }
+
+ #[test]
+ fn connect_encoding_works() {
+ let connect = Connect {
+ protocol: Protocol::V4,
+ keep_alive: 10,
+ client_id: "test".to_owned(),
+ clean_session: true,
+ last_will: Some(LastWill::new("/a", "offline", QoS::AtLeastOnce, false)),
+ login: Some(Login::new("rust", "mq")),
+ };
+
+ let mut buf = BytesMut::new();
+ connect.write(&mut buf).unwrap();
+
+ // println!("{:?}", &buf[..]);
+ // println!("{:?}", sample_bytes());
+
+ assert_eq!(buf, sample_bytes());
+ }
+
+ #[test]
+ fn connect_v3_encoding_works() {
+ let connect = Connect {
+ protocol: Protocol::V3,
+ keep_alive: 10,
+ client_id: "test".to_owned(),
+ clean_session: true,
+ last_will: Some(LastWill::new("/a", "offline", QoS::AtLeastOnce, false)),
+ login: Some(Login::new("rust", "mq")),
+ };
+
+ let mut buf = BytesMut::new();
+ connect.write(&mut buf).unwrap();
+
+ assert_eq!(
+ buf,
+ vec![
+ 0x10, 41, 0x00, 0x06, b'M', b'Q', b'I', b's', b'd', b'p', 0x03, 0b1100_1110, 0x00, 0x0a, 0x00, 0x04,
+ b't', b'e', b's', b't', 0x00, 0x02, b'/', b'a', 0x00, 0x07, b'o', b'f', b'f', b'l', b'i', b'n', b'e',
+ 0x00, 0x04, b'r', b'u', b's', b't', 0x00, 0x02, b'm', b'q'
+ ]
+ );
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/disconnect.rs b/vendor/rumqttc/src/mqttbytes/v4/disconnect.rs
new file mode 100644
index 0000000000..63ac16dab8
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/disconnect.rs
@@ -0,0 +1,16 @@
+use super::*;
+use bytes::{BufMut, BytesMut};
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Disconnect;
+
+impl Disconnect {
+ pub fn size(&self) -> usize {
+ 2
+ }
+
+ pub fn write(&self, payload: &mut BytesMut) -> Result<usize, Error> {
+ payload.put_slice(&[0xE0, 0x00]);
+ Ok(2)
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/mod.rs b/vendor/rumqttc/src/mqttbytes/v4/mod.rs
new file mode 100644
index 0000000000..abe4561279
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/mod.rs
@@ -0,0 +1,100 @@
+use super::*;
+
+mod connack;
+mod connect;
+mod disconnect;
+mod ping;
+mod puback;
+mod pubcomp;
+mod publish;
+mod pubrec;
+mod pubrel;
+mod suback;
+mod subscribe;
+mod unsuback;
+mod unsubscribe;
+
+pub use connack::*;
+pub use connect::*;
+pub use disconnect::*;
+pub use ping::*;
+pub use puback::*;
+pub use pubcomp::*;
+pub use publish::*;
+pub use pubrec::*;
+pub use pubrel::*;
+pub use suback::*;
+pub use subscribe::*;
+pub use unsuback::*;
+pub use unsubscribe::*;
+
+/// Encapsulates all MQTT packet types
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum Packet {
+ Connect(Connect),
+ ConnAck(ConnAck),
+ Publish(Publish),
+ PubAck(PubAck),
+ PubRec(PubRec),
+ PubRel(PubRel),
+ PubComp(PubComp),
+ Subscribe(Subscribe),
+ SubAck(SubAck),
+ Unsubscribe(Unsubscribe),
+ UnsubAck(UnsubAck),
+ PingReq,
+ PingResp,
+ Disconnect,
+}
+
+/// Reads a stream of bytes and extracts next MQTT packet out of it
+pub fn read(stream: &mut BytesMut, max_size: usize) -> Result<Packet, Error> {
+ let fixed_header = check(stream.iter(), max_size)?;
+
+ // Test with a stream with exactly the size to check border panics
+ let packet = stream.split_to(fixed_header.frame_length());
+ let packet_type = fixed_header.packet_type()?;
+
+ if fixed_header.remaining_len == 0 {
+ // no payload packets
+ return match packet_type {
+ PacketType::PingReq => Ok(Packet::PingReq),
+ PacketType::PingResp => Ok(Packet::PingResp),
+ PacketType::Disconnect => Ok(Packet::Disconnect),
+ _ => Err(Error::PayloadRequired),
+ };
+ }
+
+ let packet = packet.freeze();
+ let packet = match packet_type {
+ PacketType::Connect => Packet::Connect(Connect::read(fixed_header, packet)?),
+ PacketType::ConnAck => Packet::ConnAck(ConnAck::read(fixed_header, packet)?),
+ PacketType::Publish => Packet::Publish(Publish::read(fixed_header, packet)?),
+ PacketType::PubAck => Packet::PubAck(PubAck::read(fixed_header, packet)?),
+ PacketType::PubRec => Packet::PubRec(PubRec::read(fixed_header, packet)?),
+ PacketType::PubRel => Packet::PubRel(PubRel::read(fixed_header, packet)?),
+ PacketType::PubComp => Packet::PubComp(PubComp::read(fixed_header, packet)?),
+ PacketType::Subscribe => Packet::Subscribe(Subscribe::read(fixed_header, packet)?),
+ PacketType::SubAck => Packet::SubAck(SubAck::read(fixed_header, packet)?),
+ PacketType::Unsubscribe => Packet::Unsubscribe(Unsubscribe::read(fixed_header, packet)?),
+ PacketType::UnsubAck => Packet::UnsubAck(UnsubAck::read(fixed_header, packet)?),
+ PacketType::PingReq => Packet::PingReq,
+ PacketType::PingResp => Packet::PingResp,
+ PacketType::Disconnect => Packet::Disconnect,
+ };
+
+ Ok(packet)
+}
+
+/// Return number of remaining length bytes required for encoding length
+fn len_len(len: usize) -> usize {
+ if len >= 2_097_152 {
+ 4
+ } else if len >= 16_384 {
+ 3
+ } else if len >= 128 {
+ 2
+ } else {
+ 1
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/ping.rs b/vendor/rumqttc/src/mqttbytes/v4/ping.rs
new file mode 100644
index 0000000000..91c53be4ac
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/ping.rs
@@ -0,0 +1,30 @@
+use super::*;
+use bytes::{BufMut, BytesMut};
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PingReq;
+
+impl PingReq {
+ pub fn size(&self) -> usize {
+ 2
+ }
+
+ pub fn write(&self, payload: &mut BytesMut) -> Result<usize, Error> {
+ payload.put_slice(&[0xC0, 0x00]);
+ Ok(2)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PingResp;
+
+impl PingResp {
+ pub fn size(&self) -> usize {
+ 2
+ }
+
+ pub fn write(&self, payload: &mut BytesMut) -> Result<usize, Error> {
+ payload.put_slice(&[0xD0, 0x00]);
+ Ok(2)
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/puback.rs b/vendor/rumqttc/src/mqttbytes/v4/puback.rs
new file mode 100644
index 0000000000..2a9ccae209
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/puback.rs
@@ -0,0 +1,81 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// Acknowledgement to QoS1 publish
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubAck {
+ pub pkid: u16,
+}
+
+impl PubAck {
+ pub fn new(pkid: u16) -> PubAck {
+ PubAck { pkid }
+ }
+
+ fn len(&self) -> usize {
+ // pkid
+ 2
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let pkid = read_u16(&mut bytes)?;
+
+ // No reason code or properties if remaining length == 2
+ if fixed_header.remaining_len == 2 {
+ return Ok(PubAck { pkid });
+ }
+
+ // No properties len or properties if remaining len > 2 but < 4
+ if fixed_header.remaining_len < 4 {
+ return Ok(PubAck { pkid });
+ }
+
+ let puback = PubAck { pkid };
+
+ Ok(puback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+ buffer.put_u8(0x40);
+ let count = write_remaining_length(buffer, len)?;
+ buffer.put_u16(self.pkid);
+ Ok(1 + count + len)
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn puback_encoding_works() {
+ let stream = &[
+ 0b0100_0000,
+ 0x02, // packet type, flags and remaining len
+ 0x00,
+ 0x0A, // fixed header. packet identifier = 10
+ 0xDE,
+ 0xAD,
+ 0xBE,
+ 0xEF, // extra packets in the stream
+ ];
+ let mut stream = BytesMut::from(&stream[..]);
+ let fixed_header = parse_fixed_header(stream.iter()).unwrap();
+ let ack_bytes = stream.split_to(fixed_header.frame_length()).freeze();
+ let packet = PubAck::read(fixed_header, ack_bytes).unwrap();
+
+ assert_eq!(packet, PubAck { pkid: 10 });
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/pubcomp.rs b/vendor/rumqttc/src/mqttbytes/v4/pubcomp.rs
new file mode 100644
index 0000000000..0ad13b630b
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/pubcomp.rs
@@ -0,0 +1,51 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// QoS2 Assured publish complete, in response to PUBREL packet
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubComp {
+ pub pkid: u16,
+}
+
+impl PubComp {
+ pub fn new(pkid: u16) -> PubComp {
+ PubComp { pkid }
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+ 1 + remaining_len_size + len
+ }
+
+ fn len(&self) -> usize {
+ // pkid
+ 2
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let pkid = read_u16(&mut bytes)?;
+
+ if fixed_header.remaining_len == 2 {
+ return Ok(PubComp { pkid });
+ }
+
+ if fixed_header.remaining_len < 4 {
+ return Ok(PubComp { pkid });
+ }
+
+ let puback = PubComp { pkid };
+
+ Ok(puback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+ buffer.put_u8(0x70);
+ let count = write_remaining_length(buffer, len)?;
+ buffer.put_u16(self.pkid);
+ Ok(1 + count + len)
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/publish.rs b/vendor/rumqttc/src/mqttbytes/v4/publish.rs
new file mode 100644
index 0000000000..5d8978c46d
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/publish.rs
@@ -0,0 +1,274 @@
+use super::*;
+use bytes::{Buf, Bytes};
+
+/// Publish packet
+#[derive(Clone, PartialEq, Eq)]
+pub struct Publish {
+ pub dup: bool,
+ pub qos: QoS,
+ pub retain: bool,
+ pub topic: String,
+ pub pkid: u16,
+ pub payload: Bytes,
+}
+
+impl Publish {
+ pub fn new<S: Into<String>, P: Into<Vec<u8>>>(topic: S, qos: QoS, payload: P) -> Publish {
+ Publish {
+ dup: false,
+ qos,
+ retain: false,
+ pkid: 0,
+ topic: topic.into(),
+ payload: Bytes::from(payload.into()),
+ }
+ }
+
+ pub fn from_bytes<S: Into<String>>(topic: S, qos: QoS, payload: Bytes) -> Publish {
+ Publish {
+ dup: false,
+ qos,
+ retain: false,
+ pkid: 0,
+ topic: topic.into(),
+ payload,
+ }
+ }
+
+ fn len(&self) -> usize {
+ let len = 2 + self.topic.len() + self.payload.len();
+ if self.qos != QoS::AtMostOnce && self.pkid != 0 {
+ len + 2
+ } else {
+ len
+ }
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ let qos = qos((fixed_header.byte1 & 0b0110) >> 1)?;
+ let dup = (fixed_header.byte1 & 0b1000) != 0;
+ let retain = (fixed_header.byte1 & 0b0001) != 0;
+
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let topic = read_mqtt_string(&mut bytes)?;
+
+ // Packet identifier exists where QoS > 0
+ let pkid = match qos {
+ QoS::AtMostOnce => 0,
+ QoS::AtLeastOnce | QoS::ExactlyOnce => read_u16(&mut bytes)?,
+ };
+
+ if qos != QoS::AtMostOnce && pkid == 0 {
+ return Err(Error::PacketIdZero);
+ }
+
+ let publish = Publish {
+ dup,
+ retain,
+ qos,
+ pkid,
+ topic,
+ payload: bytes,
+ };
+
+ Ok(publish)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+
+ let dup = self.dup as u8;
+ let qos = self.qos as u8;
+ let retain = self.retain as u8;
+ buffer.put_u8(0b0011_0000 | retain | qos << 1 | dup << 3);
+
+ let count = write_remaining_length(buffer, len)?;
+ write_mqtt_string(buffer, self.topic.as_str());
+
+ if self.qos != QoS::AtMostOnce {
+ let pkid = self.pkid;
+ if pkid == 0 {
+ return Err(Error::PacketIdZero);
+ }
+
+ buffer.put_u16(pkid);
+ }
+
+ buffer.extend_from_slice(&self.payload);
+
+ // TODO: Returned length is wrong in other packets. Fix it
+ Ok(1 + count + len)
+ }
+}
+
+impl fmt::Debug for Publish {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "Topic = {}, Qos = {:?}, Retain = {}, Pkid = {:?}, Payload Size = {}",
+ self.topic,
+ self.qos,
+ self.retain,
+ self.pkid,
+ self.payload.len()
+ )
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use bytes::{Bytes, BytesMut};
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn qos1_publish_parsing_works() {
+ let stream = &[
+ 0b0011_0010,
+ 11, // packet type, flags and remaining len
+ 0x00,
+ 0x03,
+ b'a',
+ b'/',
+ b'b', // variable header. topic name = 'a/b'
+ 0x00,
+ 0x0a, // variable header. pkid = 10
+ 0xF1,
+ 0xF2,
+ 0xF3,
+ 0xF4, // publish payload
+ 0xDE,
+ 0xAD,
+ 0xBE,
+ 0xEF, // extra packets in the stream
+ ];
+
+ let mut stream = BytesMut::from(&stream[..]);
+ let fixed_header = parse_fixed_header(stream.iter()).unwrap();
+ let publish_bytes = stream.split_to(fixed_header.frame_length()).freeze();
+ let packet = Publish::read(fixed_header, publish_bytes).unwrap();
+
+ let payload = &[0xF1, 0xF2, 0xF3, 0xF4];
+ assert_eq!(
+ packet,
+ Publish {
+ dup: false,
+ qos: QoS::AtLeastOnce,
+ retain: false,
+ topic: "a/b".to_owned(),
+ pkid: 10,
+ payload: Bytes::from(&payload[..]),
+ }
+ );
+ }
+
+ #[test]
+ fn qos0_publish_parsing_works() {
+ let stream = &[
+ 0b0011_0000,
+ 7, // packet type, flags and remaining len
+ 0x00,
+ 0x03,
+ b'a',
+ b'/',
+ b'b', // variable header. topic name = 'a/b'
+ 0x01,
+ 0x02, // payload
+ 0xDE,
+ 0xAD,
+ 0xBE,
+ 0xEF, // extra packets in the stream
+ ];
+
+ let mut stream = BytesMut::from(&stream[..]);
+ let fixed_header = parse_fixed_header(stream.iter()).unwrap();
+ let publish_bytes = stream.split_to(fixed_header.frame_length()).freeze();
+ let packet = Publish::read(fixed_header, publish_bytes).unwrap();
+
+ assert_eq!(
+ packet,
+ Publish {
+ dup: false,
+ qos: QoS::AtMostOnce,
+ retain: false,
+ topic: "a/b".to_owned(),
+ pkid: 0,
+ payload: Bytes::from(&[0x01, 0x02][..]),
+ }
+ );
+ }
+
+ #[test]
+ fn qos1_publish_encoding_works() {
+ let publish = Publish {
+ dup: false,
+ qos: QoS::AtLeastOnce,
+ retain: false,
+ topic: "a/b".to_owned(),
+ pkid: 10,
+ payload: Bytes::from(vec![0xF1, 0xF2, 0xF3, 0xF4]),
+ };
+
+ let mut buf = BytesMut::new();
+ publish.write(&mut buf).unwrap();
+
+ assert_eq!(
+ buf,
+ vec![
+ 0b0011_0010,
+ 11,
+ 0x00,
+ 0x03,
+ b'a',
+ b'/',
+ b'b',
+ 0x00,
+ 0x0a,
+ 0xF1,
+ 0xF2,
+ 0xF3,
+ 0xF4
+ ]
+ );
+ }
+
+ #[test]
+ fn qos0_publish_encoding_works() {
+ let publish = Publish {
+ dup: false,
+ qos: QoS::AtMostOnce,
+ retain: false,
+ topic: "a/b".to_owned(),
+ pkid: 0,
+ payload: Bytes::from(vec![0xE1, 0xE2, 0xE3, 0xE4]),
+ };
+
+ let mut buf = BytesMut::new();
+ publish.write(&mut buf).unwrap();
+
+ assert_eq!(
+ buf,
+ vec![
+ 0b0011_0000,
+ 9,
+ 0x00,
+ 0x03,
+ b'a',
+ b'/',
+ b'b',
+ 0xE1,
+ 0xE2,
+ 0xE3,
+ 0xE4
+ ]
+ );
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/pubrec.rs b/vendor/rumqttc/src/mqttbytes/v4/pubrec.rs
new file mode 100644
index 0000000000..80132b9298
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/pubrec.rs
@@ -0,0 +1,51 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// Acknowledgement to QoS2 publish
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubRec {
+ pub pkid: u16,
+}
+
+impl PubRec {
+ pub fn new(pkid: u16) -> PubRec {
+ PubRec { pkid }
+ }
+
+ fn len(&self) -> usize {
+ // pkid
+ 2
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let pkid = read_u16(&mut bytes)?;
+ if fixed_header.remaining_len == 2 {
+ return Ok(PubRec { pkid });
+ }
+
+ if fixed_header.remaining_len < 4 {
+ return Ok(PubRec { pkid });
+ }
+
+ let puback = PubRec { pkid };
+
+ Ok(puback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+ buffer.put_u8(0x50);
+ let count = write_remaining_length(buffer, len)?;
+ buffer.put_u16(self.pkid);
+ Ok(1 + count + len)
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/pubrel.rs b/vendor/rumqttc/src/mqttbytes/v4/pubrel.rs
new file mode 100644
index 0000000000..f59909cbbc
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/pubrel.rs
@@ -0,0 +1,51 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// QoS2 Publish release, in response to PUBREC packet
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubRel {
+ pub pkid: u16,
+}
+
+impl PubRel {
+ pub fn new(pkid: u16) -> PubRel {
+ PubRel { pkid }
+ }
+
+ fn len(&self) -> usize {
+ // pkid
+ 2
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let pkid = read_u16(&mut bytes)?;
+ if fixed_header.remaining_len == 2 {
+ return Ok(PubRel { pkid });
+ }
+
+ if fixed_header.remaining_len < 4 {
+ return Ok(PubRel { pkid });
+ }
+
+ let puback = PubRel { pkid };
+
+ Ok(puback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+ buffer.put_u8(0x62);
+ let count = write_remaining_length(buffer, len)?;
+ buffer.put_u16(self.pkid);
+ Ok(1 + count + len)
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/suback.rs b/vendor/rumqttc/src/mqttbytes/v4/suback.rs
new file mode 100644
index 0000000000..2b443592ad
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/suback.rs
@@ -0,0 +1,118 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+use std::convert::{TryFrom, TryInto};
+
+/// Acknowledgement to subscribe
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SubAck {
+ pub pkid: u16,
+ pub return_codes: Vec<SubscribeReasonCode>,
+}
+
+impl SubAck {
+ pub fn new(pkid: u16, return_codes: Vec<SubscribeReasonCode>) -> SubAck {
+ SubAck { pkid, return_codes }
+ }
+
+ fn len(&self) -> usize {
+ 2 + self.return_codes.len()
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let pkid = read_u16(&mut bytes)?;
+
+ if !bytes.has_remaining() {
+ return Err(Error::MalformedPacket);
+ }
+
+ let mut return_codes = Vec::new();
+ while bytes.has_remaining() {
+ let return_code = read_u8(&mut bytes)?;
+ return_codes.push(return_code.try_into()?);
+ }
+
+ let suback = SubAck { pkid, return_codes };
+ Ok(suback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ buffer.put_u8(0x90);
+ let remaining_len = self.len();
+ let remaining_len_bytes = write_remaining_length(buffer, remaining_len)?;
+
+ buffer.put_u16(self.pkid);
+ let p: Vec<u8> = self
+ .return_codes
+ .iter()
+ .map(|&code| match code {
+ SubscribeReasonCode::Success(qos) => qos as u8,
+ SubscribeReasonCode::Failure => 0x80,
+ })
+ .collect();
+ buffer.extend_from_slice(&p);
+ Ok(1 + remaining_len_bytes + remaining_len)
+ }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum SubscribeReasonCode {
+ Success(QoS),
+ Failure,
+}
+
+impl TryFrom<u8> for SubscribeReasonCode {
+ type Error = super::Error;
+
+ fn try_from(value: u8) -> Result<Self, Self::Error> {
+ let v = match value {
+ 0 => SubscribeReasonCode::Success(QoS::AtMostOnce),
+ 1 => SubscribeReasonCode::Success(QoS::AtLeastOnce),
+ 2 => SubscribeReasonCode::Success(QoS::ExactlyOnce),
+ 128 => SubscribeReasonCode::Failure,
+ v => return Err(super::Error::InvalidSubscribeReasonCode(v)),
+ };
+
+ Ok(v)
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn suback_parsing_works() {
+ let stream = vec![
+ 0x90, 4, // packet type, flags and remaining len
+ 0x00, 0x0F, // variable header. pkid = 15
+ 0x01, 0x80, // payload. return codes [success qos1, failure]
+ 0xDE, 0xAD, 0xBE, 0xEF, // extra packets in the stream
+ ];
+
+ let mut stream = BytesMut::from(&stream[..]);
+ let fixed_header = parse_fixed_header(stream.iter()).unwrap();
+ let ack_bytes = stream.split_to(fixed_header.frame_length()).freeze();
+ let packet = SubAck::read(fixed_header, ack_bytes).unwrap();
+
+ assert_eq!(
+ packet,
+ SubAck {
+ pkid: 15,
+ return_codes: vec![
+ SubscribeReasonCode::Success(QoS::AtLeastOnce),
+ SubscribeReasonCode::Failure,
+ ],
+ }
+ );
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/subscribe.rs b/vendor/rumqttc/src/mqttbytes/v4/subscribe.rs
new file mode 100644
index 0000000000..42ddb57b10
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/subscribe.rs
@@ -0,0 +1,242 @@
+use super::*;
+use bytes::{Buf, Bytes};
+
+/// Subscription packet
+#[derive(Clone, PartialEq, Eq)]
+pub struct Subscribe {
+ pub pkid: u16,
+ pub filters: Vec<SubscribeFilter>,
+}
+
+impl Subscribe {
+ pub fn new<S: Into<String>>(path: S, qos: QoS) -> Subscribe {
+ let filter = SubscribeFilter {
+ path: path.into(),
+ qos,
+ };
+
+ Subscribe {
+ pkid: 0,
+ filters: vec![filter],
+ }
+ }
+
+ pub fn new_many<T>(topics: T) -> Subscribe
+ where
+ T: IntoIterator<Item = SubscribeFilter>,
+ {
+ let filters: Vec<SubscribeFilter> = topics.into_iter().collect();
+
+ Subscribe { pkid: 0, filters }
+ }
+
+ pub fn add(&mut self, path: String, qos: QoS) -> &mut Self {
+ let filter = SubscribeFilter { path, qos };
+
+ self.filters.push(filter);
+ self
+ }
+
+ fn len(&self) -> usize {
+ // len of pkid + vec![subscribe filter len]
+ 2 + self.filters.iter().fold(0, |s, t| s + t.len())
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+
+ let pkid = read_u16(&mut bytes)?;
+
+ // variable header size = 2 (packet identifier)
+ let mut filters = Vec::new();
+
+ while bytes.has_remaining() {
+ let path = read_mqtt_string(&mut bytes)?;
+ let options = read_u8(&mut bytes)?;
+ let requested_qos = options & 0b0000_0011;
+
+ filters.push(SubscribeFilter {
+ path,
+ qos: qos(requested_qos)?,
+ });
+ }
+
+ match filters.len() {
+ 0 => Err(Error::EmptySubscription),
+ _ => Ok(Subscribe { pkid, filters }),
+ }
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ // write packet type
+ buffer.put_u8(0x82);
+
+ // write remaining length
+ let remaining_len = self.len();
+ let remaining_len_bytes = write_remaining_length(buffer, remaining_len)?;
+
+ // write packet id
+ buffer.put_u16(self.pkid);
+
+ // write filters
+ for filter in self.filters.iter() {
+ filter.write(buffer);
+ }
+
+ Ok(1 + remaining_len_bytes + remaining_len)
+ }
+}
+
+/// Subscription filter
+#[derive(Clone, PartialEq, Eq)]
+pub struct SubscribeFilter {
+ pub path: String,
+ pub qos: QoS,
+}
+
+impl SubscribeFilter {
+ pub fn new(path: String, qos: QoS) -> SubscribeFilter {
+ SubscribeFilter { path, qos }
+ }
+
+ fn len(&self) -> usize {
+ // filter len + filter + options
+ 2 + self.path.len() + 1
+ }
+
+ fn write(&self, buffer: &mut BytesMut) {
+ let mut options = 0;
+ options |= self.qos as u8;
+
+ write_mqtt_string(buffer, self.path.as_str());
+ buffer.put_u8(options);
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum RetainForwardRule {
+ OnEverySubscribe,
+ OnNewSubscribe,
+ Never,
+}
+
+impl fmt::Debug for Subscribe {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "Filters = {:?}, Packet id = {:?}",
+ self.filters, self.pkid
+ )
+ }
+}
+
+impl fmt::Debug for SubscribeFilter {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "Filter = {}, Qos = {:?}", self.path, self.qos)
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn subscribe_parsing_works() {
+ let stream = &[
+ 0b1000_0010,
+ 20, // packet type, flags and remaining len
+ 0x01,
+ 0x04, // variable header. pkid = 260
+ 0x00,
+ 0x03,
+ b'a',
+ b'/',
+ b'+', // payload. topic filter = 'a/+'
+ 0x00, // payload. qos = 0
+ 0x00,
+ 0x01,
+ b'#', // payload. topic filter = '#'
+ 0x01, // payload. qos = 1
+ 0x00,
+ 0x05,
+ b'a',
+ b'/',
+ b'b',
+ b'/',
+ b'c', // payload. topic filter = 'a/b/c'
+ 0x02, // payload. qos = 2
+ 0xDE,
+ 0xAD,
+ 0xBE,
+ 0xEF, // extra packets in the stream
+ ];
+ let mut stream = BytesMut::from(&stream[..]);
+ let fixed_header = parse_fixed_header(stream.iter()).unwrap();
+ let subscribe_bytes = stream.split_to(fixed_header.frame_length()).freeze();
+ let packet = Subscribe::read(fixed_header, subscribe_bytes).unwrap();
+
+ assert_eq!(
+ packet,
+ Subscribe {
+ pkid: 260,
+ filters: vec![
+ SubscribeFilter::new("a/+".to_owned(), QoS::AtMostOnce),
+ SubscribeFilter::new("#".to_owned(), QoS::AtLeastOnce),
+ SubscribeFilter::new("a/b/c".to_owned(), QoS::ExactlyOnce)
+ ],
+ }
+ );
+ }
+
+ #[test]
+ fn subscribe_encoding_works() {
+ let subscribe = Subscribe {
+ pkid: 260,
+ filters: vec![
+ SubscribeFilter::new("a/+".to_owned(), QoS::AtMostOnce),
+ SubscribeFilter::new("#".to_owned(), QoS::AtLeastOnce),
+ SubscribeFilter::new("a/b/c".to_owned(), QoS::ExactlyOnce),
+ ],
+ };
+
+ let mut buf = BytesMut::new();
+ subscribe.write(&mut buf).unwrap();
+ assert_eq!(
+ buf,
+ vec![
+ 0b1000_0010,
+ 20,
+ 0x01,
+ 0x04, // pkid = 260
+ 0x00,
+ 0x03,
+ b'a',
+ b'/',
+ b'+', // topic filter = 'a/+'
+ 0x00, // qos = 0
+ 0x00,
+ 0x01,
+ b'#', // topic filter = '#'
+ 0x01, // qos = 1
+ 0x00,
+ 0x05,
+ b'a',
+ b'/',
+ b'b',
+ b'/',
+ b'c', // topic filter = 'a/b/c'
+ 0x02 // qos = 2
+ ]
+ );
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/unsuback.rs b/vendor/rumqttc/src/mqttbytes/v4/unsuback.rs
new file mode 100644
index 0000000000..ad534b3550
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/unsuback.rs
@@ -0,0 +1,38 @@
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+use crate::mqttbytes::{read_u16, Error, FixedHeader};
+
+/// Acknowledgement to unsubscribe
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct UnsubAck {
+ pub pkid: u16,
+}
+
+impl UnsubAck {
+ pub fn new(pkid: u16) -> UnsubAck {
+ UnsubAck { pkid }
+ }
+
+ pub fn size(&self) -> usize {
+ 4
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ if fixed_header.remaining_len != 2 {
+ return Err(Error::PayloadSizeIncorrect);
+ }
+
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let pkid = read_u16(&mut bytes)?;
+ let unsuback = UnsubAck { pkid };
+
+ Ok(unsuback)
+ }
+
+ pub fn write(&self, payload: &mut BytesMut) -> Result<usize, Error> {
+ payload.put_slice(&[0xB0, 0x02]);
+ payload.put_u16(self.pkid);
+ Ok(4)
+ }
+}
diff --git a/vendor/rumqttc/src/mqttbytes/v4/unsubscribe.rs b/vendor/rumqttc/src/mqttbytes/v4/unsubscribe.rs
new file mode 100644
index 0000000000..da34fbcc6d
--- /dev/null
+++ b/vendor/rumqttc/src/mqttbytes/v4/unsubscribe.rs
@@ -0,0 +1,61 @@
+use super::*;
+use bytes::{Buf, Bytes};
+
+/// Unsubscribe packet
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Unsubscribe {
+ pub pkid: u16,
+ pub topics: Vec<String>,
+}
+
+impl Unsubscribe {
+ pub fn new<S: Into<String>>(topic: S) -> Unsubscribe {
+ Unsubscribe {
+ pkid: 0,
+ topics: vec![topic.into()],
+ }
+ }
+
+ fn len(&self) -> usize {
+ // len of pkid + vec![subscribe topics len]
+ 2 + self.topics.iter().fold(0, |s, t| s + t.len() + 2)
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+
+ let pkid = read_u16(&mut bytes)?;
+ let mut payload_bytes = fixed_header.remaining_len - 2;
+ let mut topics = Vec::with_capacity(1);
+
+ while payload_bytes > 0 {
+ let topic_filter = read_mqtt_string(&mut bytes)?;
+ payload_bytes -= topic_filter.len() + 2;
+ topics.push(topic_filter);
+ }
+
+ let unsubscribe = Unsubscribe { pkid, topics };
+ Ok(unsubscribe)
+ }
+
+ pub fn write(&self, payload: &mut BytesMut) -> Result<usize, Error> {
+ let remaining_len = self.len();
+
+ payload.put_u8(0xA2);
+ let remaining_len_bytes = write_remaining_length(payload, remaining_len)?;
+ payload.put_u16(self.pkid);
+
+ for topic in self.topics.iter() {
+ write_mqtt_string(payload, topic.as_str());
+ }
+ Ok(1 + remaining_len_bytes + remaining_len)
+ }
+}
diff --git a/vendor/rumqttc/src/proxy.rs b/vendor/rumqttc/src/proxy.rs
new file mode 100644
index 0000000000..3dbe741cfb
--- /dev/null
+++ b/vendor/rumqttc/src/proxy.rs
@@ -0,0 +1,83 @@
+use crate::eventloop::socket_connect;
+use crate::framed::N;
+use crate::NetworkOptions;
+
+use std::io;
+
+#[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+use crate::{tls, TlsConfiguration};
+
+#[derive(Clone, Debug)]
+pub struct Proxy {
+ pub ty: ProxyType,
+ pub auth: ProxyAuth,
+ pub addr: String,
+ pub port: u16,
+}
+
+#[derive(Clone, Debug)]
+pub enum ProxyType {
+ Http,
+ #[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+ Https(TlsConfiguration),
+}
+
+#[derive(Clone, Debug)]
+pub enum ProxyAuth {
+ None,
+ Basic { username: String, password: String },
+}
+
+#[derive(Debug, thiserror::Error)]
+pub enum ProxyError {
+ #[error("Socket connect: {0}.")]
+ Io(#[from] io::Error),
+ #[error("Proxy connect: {0}.")]
+ Proxy(#[from] async_http_proxy::HttpError),
+
+ #[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+ #[error("Tls connect: {0}.")]
+ Tls(#[from] tls::Error),
+}
+
+impl Proxy {
+ pub(crate) async fn connect(
+ self,
+ broker_addr: &str,
+ broker_port: u16,
+ network_options: NetworkOptions,
+ ) -> Result<Box<dyn N>, ProxyError> {
+ let proxy_addr = format!("{}:{}", self.addr, self.port);
+
+ let tcp: Box<dyn N> = Box::new(socket_connect(proxy_addr, network_options).await?);
+ let mut tcp = match self.ty {
+ ProxyType::Http => tcp,
+ #[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+ ProxyType::Https(tls_config) => {
+ tls::tls_connect(&self.addr, self.port, &tls_config, tcp).await?
+ }
+ };
+ self.auth.auth(broker_addr, broker_port, &mut tcp).await?;
+ Ok(tcp)
+ }
+}
+
+impl ProxyAuth {
+ async fn auth(
+ self,
+ host: &str,
+ port: u16,
+ tcp_stream: &mut Box<dyn N>,
+ ) -> Result<(), ProxyError> {
+ match self {
+ Self::None => async_http_proxy::http_connect_tokio(tcp_stream, host, port).await?,
+ Self::Basic { username, password } => {
+ async_http_proxy::http_connect_tokio_with_basic_auth(
+ tcp_stream, host, port, &username, &password,
+ )
+ .await?
+ }
+ }
+ Ok(())
+ }
+}
diff --git a/vendor/rumqttc/src/state.rs b/vendor/rumqttc/src/state.rs
new file mode 100644
index 0000000000..da33bd2f2a
--- /dev/null
+++ b/vendor/rumqttc/src/state.rs
@@ -0,0 +1,940 @@
+use crate::{Event, Incoming, Outgoing, Request};
+
+use crate::mqttbytes::v4::*;
+use crate::mqttbytes::{self, *};
+use bytes::BytesMut;
+use std::collections::VecDeque;
+use std::{io, time::Instant};
+
+/// Errors during state handling
+#[derive(Debug, thiserror::Error)]
+pub enum StateError {
+ /// Io Error while state is passed to network
+ #[error("Io error: {0:?}")]
+ Io(#[from] io::Error),
+ /// Invalid state for a given operation
+ #[error("Invalid state for a given operation")]
+ InvalidState,
+ /// Received a packet (ack) which isn't asked for
+ #[error("Received unsolicited ack pkid: {0}")]
+ Unsolicited(u16),
+ /// Last pingreq isn't acked
+ #[error("Last pingreq isn't acked")]
+ AwaitPingResp,
+ /// Received a wrong packet while waiting for another packet
+ #[error("Received a wrong packet while waiting for another packet")]
+ WrongPacket,
+ #[error("Timeout while waiting to resolve collision")]
+ CollisionTimeout,
+ #[error("A Subscribe packet must contain atleast one filter")]
+ EmptySubscription,
+ #[error("Mqtt serialization/deserialization error: {0}")]
+ Deserialization(#[from] mqttbytes::Error),
+ #[error("Cannot send packet of size '{pkt_size:?}'. It's greater than the broker's maximum packet size of: '{max:?}'")]
+ OutgoingPacketTooLarge { pkt_size: usize, max: usize },
+}
+
+/// State of the mqtt connection.
+// Design: Methods will just modify the state of the object without doing any network operations
+// Design: All inflight queues are maintained in a pre initialized vec with index as packet id.
+// This is done for 2 reasons
+// Bad acks or out of order acks aren't O(n) causing cpu spikes
+// Any missing acks from the broker are detected during the next recycled use of packet ids
+#[derive(Debug, Clone)]
+pub struct MqttState {
+ /// Status of last ping
+ pub await_pingresp: bool,
+ /// Collision ping count. Collisions stop user requests
+ /// which inturn trigger pings. Multiple pings without
+ /// resolving collisions will result in error
+ pub collision_ping_count: usize,
+ /// Last incoming packet time
+ last_incoming: Instant,
+ /// Last outgoing packet time
+ last_outgoing: Instant,
+ /// Packet id of the last outgoing packet
+ pub(crate) last_pkid: u16,
+ /// Packet id of the last acked publish
+ pub(crate) last_puback: u16,
+ /// Number of outgoing inflight publishes
+ pub(crate) inflight: u16,
+ /// Maximum number of allowed inflight
+ pub(crate) max_inflight: u16,
+ /// Outgoing QoS 1, 2 publishes which aren't acked yet
+ pub(crate) outgoing_pub: Vec<Option<Publish>>,
+ /// Packet ids of released QoS 2 publishes
+ pub(crate) outgoing_rel: Vec<Option<u16>>,
+ /// Packet ids on incoming QoS 2 publishes
+ pub(crate) incoming_pub: Vec<Option<u16>>,
+ /// Last collision due to broker not acking in order
+ pub collision: Option<Publish>,
+ /// Buffered incoming packets
+ pub events: VecDeque<Event>,
+ /// Write buffer
+ pub write: BytesMut,
+ /// Indicates if acknowledgements should be send immediately
+ pub manual_acks: bool,
+ /// Maximum outgoing packet size, set via MqttOptions
+ pub max_outgoing_packet_size: usize,
+}
+
+impl MqttState {
+ /// Creates new mqtt state. Same state should be used during a
+ /// connection for persistent sessions while new state should
+ /// instantiated for clean sessions
+ pub fn new(max_inflight: u16, manual_acks: bool, max_outgoing_packet_size: usize) -> Self {
+ MqttState {
+ await_pingresp: false,
+ collision_ping_count: 0,
+ last_incoming: Instant::now(),
+ last_outgoing: Instant::now(),
+ last_pkid: 0,
+ last_puback: 0,
+ inflight: 0,
+ max_inflight,
+ // index 0 is wasted as 0 is not a valid packet id
+ outgoing_pub: vec![None; max_inflight as usize + 1],
+ outgoing_rel: vec![None; max_inflight as usize + 1],
+ incoming_pub: vec![None; std::u16::MAX as usize + 1],
+ collision: None,
+ // TODO: Optimize these sizes later
+ events: VecDeque::with_capacity(100),
+ write: BytesMut::with_capacity(10 * 1024),
+ manual_acks,
+ max_outgoing_packet_size,
+ }
+ }
+
+ /// Returns inflight outgoing packets and clears internal queues
+ pub fn clean(&mut self) -> Vec<Request> {
+ let mut pending = Vec::with_capacity(100);
+ let (first_half, second_half) = self
+ .outgoing_pub
+ .split_at_mut(self.last_puback as usize + 1);
+
+ for publish in second_half.iter_mut().chain(first_half) {
+ if let Some(publish) = publish.take() {
+ let request = Request::Publish(publish);
+ pending.push(request);
+ }
+ }
+
+ // remove and collect pending releases
+ for rel in self.outgoing_rel.iter_mut() {
+ if let Some(pkid) = rel.take() {
+ let request = Request::PubRel(PubRel::new(pkid));
+ pending.push(request);
+ }
+ }
+
+ // remove packed ids of incoming qos2 publishes
+ for id in self.incoming_pub.iter_mut() {
+ id.take();
+ }
+
+ self.await_pingresp = false;
+ self.collision_ping_count = 0;
+ self.inflight = 0;
+ self.write.clear();
+ pending
+ }
+
+ pub fn inflight(&self) -> u16 {
+ self.inflight
+ }
+
+ /// Consolidates handling of all outgoing mqtt packet logic. Returns a packet which should
+ /// be put on to the network by the eventloop
+ pub fn handle_outgoing_packet(&mut self, request: Request) -> Result<(), StateError> {
+ // Enforce max outgoing packet size
+ self.check_size(request.size())?;
+ match request {
+ Request::Publish(publish) => self.outgoing_publish(publish)?,
+ Request::PubRel(pubrel) => self.outgoing_pubrel(pubrel)?,
+ Request::Subscribe(subscribe) => self.outgoing_subscribe(subscribe)?,
+ Request::Unsubscribe(unsubscribe) => self.outgoing_unsubscribe(unsubscribe)?,
+ Request::PingReq(_) => self.outgoing_ping()?,
+ Request::Disconnect(_) => self.outgoing_disconnect()?,
+ Request::PubAck(puback) => self.outgoing_puback(puback)?,
+ Request::PubRec(pubrec) => self.outgoing_pubrec(pubrec)?,
+ _ => unimplemented!(),
+ };
+
+ self.last_outgoing = Instant::now();
+ Ok(())
+ }
+
+ /// Consolidates handling of all incoming mqtt packets. Returns a `Notification` which for the
+ /// user to consume and `Packet` which for the eventloop to put on the network
+ /// E.g For incoming QoS1 publish packet, this method returns (Publish, Puback). Publish packet will
+ /// be forwarded to user and Pubck packet will be written to network
+ pub fn handle_incoming_packet(&mut self, packet: Incoming) -> Result<(), StateError> {
+ let out = match &packet {
+ Incoming::PingResp => self.handle_incoming_pingresp(),
+ Incoming::Publish(publish) => self.handle_incoming_publish(publish),
+ Incoming::SubAck(_suback) => self.handle_incoming_suback(),
+ Incoming::UnsubAck(_unsuback) => self.handle_incoming_unsuback(),
+ Incoming::PubAck(puback) => self.handle_incoming_puback(puback),
+ Incoming::PubRec(pubrec) => self.handle_incoming_pubrec(pubrec),
+ Incoming::PubRel(pubrel) => self.handle_incoming_pubrel(pubrel),
+ Incoming::PubComp(pubcomp) => self.handle_incoming_pubcomp(pubcomp),
+ _ => {
+ error!("Invalid incoming packet = {:?}", packet);
+ return Err(StateError::WrongPacket);
+ }
+ };
+
+ out?;
+ self.events.push_back(Event::Incoming(packet));
+ self.last_incoming = Instant::now();
+ Ok(())
+ }
+
+ fn handle_incoming_suback(&mut self) -> Result<(), StateError> {
+ Ok(())
+ }
+
+ fn handle_incoming_unsuback(&mut self) -> Result<(), StateError> {
+ Ok(())
+ }
+
+ /// Results in a publish notification in all the QoS cases. Replys with an ack
+ /// in case of QoS1 and Replys rec in case of QoS while also storing the message
+ fn handle_incoming_publish(&mut self, publish: &Publish) -> Result<(), StateError> {
+ let qos = publish.qos;
+
+ match qos {
+ QoS::AtMostOnce => Ok(()),
+ QoS::AtLeastOnce => {
+ if !self.manual_acks {
+ let puback = PubAck::new(publish.pkid);
+ self.outgoing_puback(puback)?;
+ }
+ Ok(())
+ }
+ QoS::ExactlyOnce => {
+ let pkid = publish.pkid;
+ self.incoming_pub[pkid as usize] = Some(pkid);
+
+ if !self.manual_acks {
+ let pubrec = PubRec::new(pkid);
+ self.outgoing_pubrec(pubrec)?;
+ }
+ Ok(())
+ }
+ }
+ }
+
+ fn handle_incoming_puback(&mut self, puback: &PubAck) -> Result<(), StateError> {
+ let publish = self
+ .outgoing_pub
+ .get_mut(puback.pkid as usize)
+ .ok_or(StateError::Unsolicited(puback.pkid))?;
+
+ self.last_puback = puback.pkid;
+ let v = match publish.take() {
+ Some(_) => {
+ self.inflight -= 1;
+ Ok(())
+ }
+ None => {
+ error!("Unsolicited puback packet: {:?}", puback.pkid);
+ Err(StateError::Unsolicited(puback.pkid))
+ }
+ };
+
+ if let Some(publish) = self.check_collision(puback.pkid) {
+ self.outgoing_pub[publish.pkid as usize] = Some(publish.clone());
+ self.inflight += 1;
+
+ publish.write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Publish(publish.pkid));
+ self.events.push_back(event);
+ self.collision_ping_count = 0;
+ }
+
+ v
+ }
+
+ fn handle_incoming_pubrec(&mut self, pubrec: &PubRec) -> Result<(), StateError> {
+ let publish = self
+ .outgoing_pub
+ .get_mut(pubrec.pkid as usize)
+ .ok_or(StateError::Unsolicited(pubrec.pkid))?;
+ match publish.take() {
+ Some(_) => {
+ // NOTE: Inflight - 1 for qos2 in comp
+ self.outgoing_rel[pubrec.pkid as usize] = Some(pubrec.pkid);
+ PubRel::new(pubrec.pkid).write(&mut self.write)?;
+
+ let event = Event::Outgoing(Outgoing::PubRel(pubrec.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+ None => {
+ error!("Unsolicited pubrec packet: {:?}", pubrec.pkid);
+ Err(StateError::Unsolicited(pubrec.pkid))
+ }
+ }
+ }
+
+ fn handle_incoming_pubrel(&mut self, pubrel: &PubRel) -> Result<(), StateError> {
+ let publish = self
+ .incoming_pub
+ .get_mut(pubrel.pkid as usize)
+ .ok_or(StateError::Unsolicited(pubrel.pkid))?;
+ match publish.take() {
+ Some(_) => {
+ PubComp::new(pubrel.pkid).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::PubComp(pubrel.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+ None => {
+ error!("Unsolicited pubrel packet: {:?}", pubrel.pkid);
+ Err(StateError::Unsolicited(pubrel.pkid))
+ }
+ }
+ }
+
+ fn handle_incoming_pubcomp(&mut self, pubcomp: &PubComp) -> Result<(), StateError> {
+ if let Some(publish) = self.check_collision(pubcomp.pkid) {
+ publish.write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Publish(publish.pkid));
+ self.events.push_back(event);
+ self.collision_ping_count = 0;
+ }
+
+ let pubrel = self
+ .outgoing_rel
+ .get_mut(pubcomp.pkid as usize)
+ .ok_or(StateError::Unsolicited(pubcomp.pkid))?;
+ match pubrel.take() {
+ Some(_) => {
+ self.inflight -= 1;
+ Ok(())
+ }
+ None => {
+ error!("Unsolicited pubcomp packet: {:?}", pubcomp.pkid);
+ Err(StateError::Unsolicited(pubcomp.pkid))
+ }
+ }
+ }
+
+ fn handle_incoming_pingresp(&mut self) -> Result<(), StateError> {
+ self.await_pingresp = false;
+ Ok(())
+ }
+
+ /// Adds next packet identifier to QoS 1 and 2 publish packets and returns
+ /// it buy wrapping publish in packet
+ fn outgoing_publish(&mut self, mut publish: Publish) -> Result<(), StateError> {
+ if publish.qos != QoS::AtMostOnce {
+ if publish.pkid == 0 {
+ publish.pkid = self.next_pkid();
+ }
+
+ let pkid = publish.pkid;
+ if self
+ .outgoing_pub
+ .get(publish.pkid as usize)
+ .ok_or(StateError::Unsolicited(publish.pkid))?
+ .is_some()
+ {
+ info!("Collision on packet id = {:?}", publish.pkid);
+ self.collision = Some(publish);
+ let event = Event::Outgoing(Outgoing::AwaitAck(pkid));
+ self.events.push_back(event);
+ return Ok(());
+ }
+
+ // if there is an existing publish at this pkid, this implies that broker hasn't acked this
+ // packet yet. This error is possible only when broker isn't acking sequentially
+ self.outgoing_pub[pkid as usize] = Some(publish.clone());
+ self.inflight += 1;
+ };
+
+ debug!(
+ "Publish. Topic = {}, Pkid = {:?}, Payload Size = {:?}",
+ publish.topic,
+ publish.pkid,
+ publish.payload.len()
+ );
+
+ publish.write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Publish(publish.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_pubrel(&mut self, pubrel: PubRel) -> Result<(), StateError> {
+ let pubrel = self.save_pubrel(pubrel)?;
+
+ debug!("Pubrel. Pkid = {}", pubrel.pkid);
+ PubRel::new(pubrel.pkid).write(&mut self.write)?;
+
+ let event = Event::Outgoing(Outgoing::PubRel(pubrel.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_puback(&mut self, puback: PubAck) -> Result<(), StateError> {
+ puback.write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::PubAck(puback.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_pubrec(&mut self, pubrec: PubRec) -> Result<(), StateError> {
+ pubrec.write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::PubRec(pubrec.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ /// check when the last control packet/pingreq packet is received and return
+ /// the status which tells if keep alive time has exceeded
+ /// NOTE: status will be checked for zero keepalive times also
+ fn outgoing_ping(&mut self) -> Result<(), StateError> {
+ let elapsed_in = self.last_incoming.elapsed();
+ let elapsed_out = self.last_outgoing.elapsed();
+
+ if self.collision.is_some() {
+ self.collision_ping_count += 1;
+ if self.collision_ping_count >= 2 {
+ return Err(StateError::CollisionTimeout);
+ }
+ }
+
+ // raise error if last ping didn't receive ack
+ if self.await_pingresp {
+ return Err(StateError::AwaitPingResp);
+ }
+
+ self.await_pingresp = true;
+
+ debug!(
+ "Pingreq,
+ last incoming packet before {} millisecs,
+ last outgoing request before {} millisecs",
+ elapsed_in.as_millis(),
+ elapsed_out.as_millis()
+ );
+
+ PingReq.write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::PingReq);
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_subscribe(&mut self, mut subscription: Subscribe) -> Result<(), StateError> {
+ if subscription.filters.is_empty() {
+ return Err(StateError::EmptySubscription);
+ }
+
+ let pkid = self.next_pkid();
+ subscription.pkid = pkid;
+
+ debug!(
+ "Subscribe. Topics = {:?}, Pkid = {:?}",
+ subscription.filters, subscription.pkid
+ );
+
+ subscription.write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Subscribe(subscription.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_unsubscribe(&mut self, mut unsub: Unsubscribe) -> Result<(), StateError> {
+ let pkid = self.next_pkid();
+ unsub.pkid = pkid;
+
+ debug!(
+ "Unsubscribe. Topics = {:?}, Pkid = {:?}",
+ unsub.topics, unsub.pkid
+ );
+
+ unsub.write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Unsubscribe(unsub.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_disconnect(&mut self) -> Result<(), StateError> {
+ debug!("Disconnect");
+
+ Disconnect.write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Disconnect);
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn check_collision(&mut self, pkid: u16) -> Option<Publish> {
+ if let Some(publish) = &self.collision {
+ if publish.pkid == pkid {
+ return self.collision.take();
+ }
+ }
+
+ None
+ }
+
+ fn check_size(&self, pkt_size: usize) -> Result<(), StateError> {
+ if pkt_size > self.max_outgoing_packet_size {
+ Err(StateError::OutgoingPacketTooLarge {
+ pkt_size,
+ max: self.max_outgoing_packet_size,
+ })
+ } else {
+ Ok(())
+ }
+ }
+
+ fn save_pubrel(&mut self, mut pubrel: PubRel) -> Result<PubRel, StateError> {
+ let pubrel = match pubrel.pkid {
+ // consider PacketIdentifier(0) as uninitialized packets
+ 0 => {
+ pubrel.pkid = self.next_pkid();
+ pubrel
+ }
+ _ => pubrel,
+ };
+
+ self.outgoing_rel[pubrel.pkid as usize] = Some(pubrel.pkid);
+ self.inflight += 1;
+ Ok(pubrel)
+ }
+
+ /// http://stackoverflow.com/questions/11115364/mqtt-messageid-practical-implementation
+ /// Packet ids are incremented till maximum set inflight messages and reset to 1 after that.
+ ///
+ fn next_pkid(&mut self) -> u16 {
+ let next_pkid = self.last_pkid + 1;
+
+ // When next packet id is at the edge of inflight queue,
+ // set await flag. This instructs eventloop to stop
+ // processing requests until all the inflight publishes
+ // are acked
+ if next_pkid == self.max_inflight {
+ self.last_pkid = 0;
+ return next_pkid;
+ }
+
+ self.last_pkid = next_pkid;
+ next_pkid
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::{MqttState, StateError};
+ use crate::mqttbytes::v4::*;
+ use crate::mqttbytes::*;
+ use crate::{Event, Incoming, Outgoing, Request};
+ use bytes::BufMut;
+
+ fn build_outgoing_publish(qos: QoS) -> Publish {
+ let topic = "hello/world".to_owned();
+ let payload = vec![1, 2, 3];
+
+ let mut publish = Publish::new(topic, QoS::AtLeastOnce, payload);
+ publish.qos = qos;
+ publish
+ }
+
+ fn build_incoming_publish(qos: QoS, pkid: u16) -> Publish {
+ let topic = "hello/world".to_owned();
+ let payload = vec![1, 2, 3];
+
+ let mut publish = Publish::new(topic, QoS::AtLeastOnce, payload);
+ publish.pkid = pkid;
+ publish.qos = qos;
+ publish
+ }
+
+ fn build_mqttstate() -> MqttState {
+ MqttState::new(100, false, usize::MAX)
+ }
+
+ #[test]
+ fn next_pkid_increments_as_expected() {
+ let mut mqtt = build_mqttstate();
+
+ for i in 1..=100 {
+ let pkid = mqtt.next_pkid();
+
+ // loops between 0-99. % 100 == 0 implies border
+ let expected = i % 100;
+ if expected == 0 {
+ break;
+ }
+
+ assert_eq!(expected, pkid);
+ }
+ }
+
+ #[test]
+ fn outgoing_max_packet_size_check() {
+ let mut mqtt = MqttState::new(100, false, 200);
+
+ let small_publish = Publish::new("hello/world", QoS::AtLeastOnce, vec![1; 100]);
+ assert_eq!(
+ mqtt.handle_outgoing_packet(Request::Publish(small_publish))
+ .is_ok(),
+ true
+ );
+
+ let large_publish = Publish::new("hello/world", QoS::AtLeastOnce, vec![1; 265]);
+ assert_eq!(
+ mqtt.handle_outgoing_packet(Request::Publish(large_publish))
+ .is_ok(),
+ false
+ );
+ }
+
+ #[test]
+ fn outgoing_publish_should_set_pkid_and_add_publish_to_queue() {
+ let mut mqtt = build_mqttstate();
+
+ // QoS0 Publish
+ let publish = build_outgoing_publish(QoS::AtMostOnce);
+
+ // QoS 0 publish shouldn't be saved in queue
+ mqtt.outgoing_publish(publish).unwrap();
+ assert_eq!(mqtt.last_pkid, 0);
+ assert_eq!(mqtt.inflight, 0);
+
+ // QoS1 Publish
+ let publish = build_outgoing_publish(QoS::AtLeastOnce);
+
+ // Packet id should be set and publish should be saved in queue
+ mqtt.outgoing_publish(publish.clone()).unwrap();
+ assert_eq!(mqtt.last_pkid, 1);
+ assert_eq!(mqtt.inflight, 1);
+
+ // Packet id should be incremented and publish should be saved in queue
+ mqtt.outgoing_publish(publish).unwrap();
+ assert_eq!(mqtt.last_pkid, 2);
+ assert_eq!(mqtt.inflight, 2);
+
+ // QoS1 Publish
+ let publish = build_outgoing_publish(QoS::ExactlyOnce);
+
+ // Packet id should be set and publish should be saved in queue
+ mqtt.outgoing_publish(publish.clone()).unwrap();
+ assert_eq!(mqtt.last_pkid, 3);
+ assert_eq!(mqtt.inflight, 3);
+
+ // Packet id should be incremented and publish should be saved in queue
+ mqtt.outgoing_publish(publish).unwrap();
+ assert_eq!(mqtt.last_pkid, 4);
+ assert_eq!(mqtt.inflight, 4);
+ }
+
+ #[test]
+ fn incoming_publish_should_be_added_to_queue_correctly() {
+ let mut mqtt = build_mqttstate();
+
+ // QoS0, 1, 2 Publishes
+ let publish1 = build_incoming_publish(QoS::AtMostOnce, 1);
+ let publish2 = build_incoming_publish(QoS::AtLeastOnce, 2);
+ let publish3 = build_incoming_publish(QoS::ExactlyOnce, 3);
+
+ mqtt.handle_incoming_publish(&publish1).unwrap();
+ mqtt.handle_incoming_publish(&publish2).unwrap();
+ mqtt.handle_incoming_publish(&publish3).unwrap();
+
+ let pkid = mqtt.incoming_pub[3].unwrap();
+
+ // only qos2 publish should be add to queue
+ assert_eq!(pkid, 3);
+ }
+
+ #[test]
+ fn incoming_publish_should_be_acked() {
+ let mut mqtt = build_mqttstate();
+
+ // QoS0, 1, 2 Publishes
+ let publish1 = build_incoming_publish(QoS::AtMostOnce, 1);
+ let publish2 = build_incoming_publish(QoS::AtLeastOnce, 2);
+ let publish3 = build_incoming_publish(QoS::ExactlyOnce, 3);
+
+ mqtt.handle_incoming_publish(&publish1).unwrap();
+ mqtt.handle_incoming_publish(&publish2).unwrap();
+ mqtt.handle_incoming_publish(&publish3).unwrap();
+
+ if let Event::Outgoing(Outgoing::PubAck(pkid)) = mqtt.events[0] {
+ assert_eq!(pkid, 2);
+ } else {
+ panic!("missing puback");
+ }
+
+ if let Event::Outgoing(Outgoing::PubRec(pkid)) = mqtt.events[1] {
+ assert_eq!(pkid, 3);
+ } else {
+ panic!("missing PubRec");
+ }
+ }
+
+ #[test]
+ fn incoming_publish_should_not_be_acked_with_manual_acks() {
+ let mut mqtt = build_mqttstate();
+ mqtt.manual_acks = true;
+
+ // QoS0, 1, 2 Publishes
+ let publish1 = build_incoming_publish(QoS::AtMostOnce, 1);
+ let publish2 = build_incoming_publish(QoS::AtLeastOnce, 2);
+ let publish3 = build_incoming_publish(QoS::ExactlyOnce, 3);
+
+ mqtt.handle_incoming_publish(&publish1).unwrap();
+ mqtt.handle_incoming_publish(&publish2).unwrap();
+ mqtt.handle_incoming_publish(&publish3).unwrap();
+
+ let pkid = mqtt.incoming_pub[3].unwrap();
+ assert_eq!(pkid, 3);
+
+ assert!(mqtt.events.is_empty());
+ }
+
+ #[test]
+ fn incoming_qos2_publish_should_send_rec_to_network_and_publish_to_user() {
+ let mut mqtt = build_mqttstate();
+ let publish = build_incoming_publish(QoS::ExactlyOnce, 1);
+
+ mqtt.handle_incoming_publish(&publish).unwrap();
+ let packet = read(&mut mqtt.write, 10 * 1024).unwrap();
+ match packet {
+ Packet::PubRec(pubrec) => assert_eq!(pubrec.pkid, 1),
+ _ => panic!("Invalid network request: {:?}", packet),
+ }
+ }
+
+ #[test]
+ fn incoming_puback_should_remove_correct_publish_from_queue() {
+ let mut mqtt = build_mqttstate();
+
+ let publish1 = build_outgoing_publish(QoS::AtLeastOnce);
+ let publish2 = build_outgoing_publish(QoS::ExactlyOnce);
+
+ mqtt.outgoing_publish(publish1).unwrap();
+ mqtt.outgoing_publish(publish2).unwrap();
+ assert_eq!(mqtt.inflight, 2);
+
+ mqtt.handle_incoming_puback(&PubAck::new(1)).unwrap();
+ assert_eq!(mqtt.inflight, 1);
+
+ mqtt.handle_incoming_puback(&PubAck::new(2)).unwrap();
+ assert_eq!(mqtt.inflight, 0);
+
+ assert!(mqtt.outgoing_pub[1].is_none());
+ assert!(mqtt.outgoing_pub[2].is_none());
+ }
+
+ #[test]
+ fn incoming_puback_with_pkid_greater_than_max_inflight_should_be_handled_gracefully() {
+ let mut mqtt = build_mqttstate();
+
+ let got = mqtt.handle_incoming_puback(&PubAck::new(101)).unwrap_err();
+
+ match got {
+ StateError::Unsolicited(pkid) => assert_eq!(pkid, 101),
+ e => panic!("Unexpected error: {}", e),
+ }
+ }
+
+ #[test]
+ fn incoming_pubrec_should_release_publish_from_queue_and_add_relid_to_rel_queue() {
+ let mut mqtt = build_mqttstate();
+
+ let publish1 = build_outgoing_publish(QoS::AtLeastOnce);
+ let publish2 = build_outgoing_publish(QoS::ExactlyOnce);
+
+ let _publish_out = mqtt.outgoing_publish(publish1);
+ let _publish_out = mqtt.outgoing_publish(publish2);
+
+ mqtt.handle_incoming_pubrec(&PubRec::new(2)).unwrap();
+ assert_eq!(mqtt.inflight, 2);
+
+ // check if the remaining element's pkid is 1
+ let backup = mqtt.outgoing_pub[1].clone();
+ assert_eq!(backup.unwrap().pkid, 1);
+
+ // check if the qos2 element's release pkid is 2
+ assert_eq!(mqtt.outgoing_rel[2].unwrap(), 2);
+ }
+
+ #[test]
+ fn incoming_pubrec_should_send_release_to_network_and_nothing_to_user() {
+ let mut mqtt = build_mqttstate();
+
+ let publish = build_outgoing_publish(QoS::ExactlyOnce);
+ mqtt.outgoing_publish(publish).unwrap();
+ let packet = read(&mut mqtt.write, 10 * 1024).unwrap();
+ match packet {
+ Packet::Publish(publish) => assert_eq!(publish.pkid, 1),
+ packet => panic!("Invalid network request: {:?}", packet),
+ }
+
+ mqtt.handle_incoming_pubrec(&PubRec::new(1)).unwrap();
+ let packet = read(&mut mqtt.write, 10 * 1024).unwrap();
+ match packet {
+ Packet::PubRel(pubrel) => assert_eq!(pubrel.pkid, 1),
+ packet => panic!("Invalid network request: {:?}", packet),
+ }
+ }
+
+ #[test]
+ fn incoming_pubrel_should_send_comp_to_network_and_nothing_to_user() {
+ let mut mqtt = build_mqttstate();
+ let publish = build_incoming_publish(QoS::ExactlyOnce, 1);
+
+ mqtt.handle_incoming_publish(&publish).unwrap();
+ let packet = read(&mut mqtt.write, 10 * 1024).unwrap();
+ match packet {
+ Packet::PubRec(pubrec) => assert_eq!(pubrec.pkid, 1),
+ packet => panic!("Invalid network request: {:?}", packet),
+ }
+
+ mqtt.handle_incoming_pubrel(&PubRel::new(1)).unwrap();
+ let packet = read(&mut mqtt.write, 10 * 1024).unwrap();
+ match packet {
+ Packet::PubComp(pubcomp) => assert_eq!(pubcomp.pkid, 1),
+ packet => panic!("Invalid network request: {:?}", packet),
+ }
+ }
+
+ #[test]
+ fn incoming_pubcomp_should_release_correct_pkid_from_release_queue() {
+ let mut mqtt = build_mqttstate();
+ let publish = build_outgoing_publish(QoS::ExactlyOnce);
+
+ mqtt.outgoing_publish(publish).unwrap();
+ mqtt.handle_incoming_pubrec(&PubRec::new(1)).unwrap();
+
+ mqtt.handle_incoming_pubcomp(&PubComp::new(1)).unwrap();
+ assert_eq!(mqtt.inflight, 0);
+ }
+
+ #[test]
+ fn outgoing_ping_handle_should_throw_errors_for_no_pingresp() {
+ let mut mqtt = build_mqttstate();
+ mqtt.outgoing_ping().unwrap();
+
+ // network activity other than pingresp
+ let publish = build_outgoing_publish(QoS::AtLeastOnce);
+ mqtt.handle_outgoing_packet(Request::Publish(publish))
+ .unwrap();
+ mqtt.handle_incoming_packet(Incoming::PubAck(PubAck::new(1)))
+ .unwrap();
+
+ // should throw error because we didn't get pingresp for previous ping
+ match mqtt.outgoing_ping() {
+ Ok(_) => panic!("Should throw pingresp await error"),
+ Err(StateError::AwaitPingResp) => (),
+ Err(e) => panic!("Should throw pingresp await error. Error = {:?}", e),
+ }
+ }
+
+ #[test]
+ fn outgoing_ping_handle_should_succeed_if_pingresp_is_received() {
+ let mut mqtt = build_mqttstate();
+
+ // should ping
+ mqtt.outgoing_ping().unwrap();
+ mqtt.handle_incoming_packet(Incoming::PingResp).unwrap();
+
+ // should ping
+ mqtt.outgoing_ping().unwrap();
+ }
+
+ #[test]
+ fn state_should_be_clean_properly() {
+ let mut mqtt = build_mqttstate();
+ mqtt.write.put(&b"test"[..]);
+ // After this clean state.write should be empty
+ mqtt.clean();
+ assert!(mqtt.write.is_empty());
+ }
+
+ #[test]
+ fn clean_is_calculating_pending_correctly() {
+ let mut mqtt = build_mqttstate();
+
+ fn build_outgoing_pub() -> Vec<Option<Publish>> {
+ vec![
+ None,
+ Some(Publish {
+ dup: false,
+ qos: QoS::AtMostOnce,
+ retain: false,
+ topic: "test".to_string(),
+ pkid: 1,
+ payload: "".into(),
+ }),
+ Some(Publish {
+ dup: false,
+ qos: QoS::AtMostOnce,
+ retain: false,
+ topic: "test".to_string(),
+ pkid: 2,
+ payload: "".into(),
+ }),
+ Some(Publish {
+ dup: false,
+ qos: QoS::AtMostOnce,
+ retain: false,
+ topic: "test".to_string(),
+ pkid: 3,
+ payload: "".into(),
+ }),
+ None,
+ None,
+ Some(Publish {
+ dup: false,
+ qos: QoS::AtMostOnce,
+ retain: false,
+ topic: "test".to_string(),
+ pkid: 6,
+ payload: "".into(),
+ }),
+ ]
+ }
+
+ mqtt.outgoing_pub = build_outgoing_pub();
+ mqtt.last_puback = 3;
+ let requests = mqtt.clean();
+ let res = vec![6, 1, 2, 3];
+ for (req, idx) in requests.iter().zip(res) {
+ if let Request::Publish(publish) = req {
+ assert_eq!(publish.pkid, idx);
+ } else {
+ unreachable!()
+ }
+ }
+
+ mqtt.outgoing_pub = build_outgoing_pub();
+ mqtt.last_puback = 0;
+ let requests = mqtt.clean();
+ let res = vec![1, 2, 3, 6];
+ for (req, idx) in requests.iter().zip(res) {
+ if let Request::Publish(publish) = req {
+ assert_eq!(publish.pkid, idx);
+ } else {
+ unreachable!()
+ }
+ }
+
+ mqtt.outgoing_pub = build_outgoing_pub();
+ mqtt.last_puback = 6;
+ let requests = mqtt.clean();
+ let res = vec![1, 2, 3, 6];
+ for (req, idx) in requests.iter().zip(res) {
+ if let Request::Publish(publish) = req {
+ assert_eq!(publish.pkid, idx);
+ } else {
+ unreachable!()
+ }
+ }
+ }
+}
diff --git a/vendor/rumqttc/src/tls.rs b/vendor/rumqttc/src/tls.rs
new file mode 100644
index 0000000000..c8e775712e
--- /dev/null
+++ b/vendor/rumqttc/src/tls.rs
@@ -0,0 +1,187 @@
+#[cfg(feature = "use-rustls")]
+use rustls_pemfile::Item;
+#[cfg(feature = "use-rustls")]
+use tokio_rustls::rustls::{
+ self,
+ pki_types::{InvalidDnsNameError, ServerName},
+ ClientConfig, RootCertStore,
+};
+#[cfg(feature = "use-rustls")]
+use tokio_rustls::TlsConnector as RustlsConnector;
+
+#[cfg(feature = "use-rustls")]
+use std::convert::TryFrom;
+#[cfg(feature = "use-rustls")]
+use std::io::{BufReader, Cursor};
+#[cfg(feature = "use-rustls")]
+use std::sync::Arc;
+
+use crate::framed::N;
+use crate::TlsConfiguration;
+
+#[cfg(feature = "use-native-tls")]
+use tokio_native_tls::TlsConnector as NativeTlsConnector;
+
+#[cfg(feature = "use-native-tls")]
+use tokio_native_tls::native_tls::{Error as NativeTlsError, Identity};
+
+use std::io;
+use std::net::AddrParseError;
+
+#[derive(Debug, thiserror::Error)]
+pub enum Error {
+ /// Error parsing IP address
+ #[error("Addr")]
+ Addr(#[from] AddrParseError),
+ /// I/O related error
+ #[error("I/O: {0}")]
+ Io(#[from] io::Error),
+ #[cfg(feature = "use-rustls")]
+ /// Certificate/Name validation error
+ #[error("Web Pki: {0}")]
+ WebPki(#[from] webpki::Error),
+ /// Invalid DNS name
+ #[cfg(feature = "use-rustls")]
+ #[error("DNS name")]
+ DNSName(#[from] InvalidDnsNameError),
+ #[cfg(feature = "use-rustls")]
+ /// Error from rustls module
+ #[error("TLS error: {0}")]
+ TLS(#[from] rustls::Error),
+ #[cfg(feature = "use-rustls")]
+ /// No valid CA cert found
+ #[error("No valid CA certificate provided")]
+ NoValidCertInChain,
+ #[cfg(feature = "use-rustls")]
+ /// No valid client cert found
+ #[error("No valid certificate for client authentication in chain")]
+ NoValidClientCertInChain,
+ #[cfg(feature = "use-rustls")]
+ /// No valid key found
+ #[error("No valid key in chain")]
+ NoValidKeyInChain,
+ #[cfg(feature = "use-native-tls")]
+ #[error("Native TLS error {0}")]
+ NativeTls(#[from] NativeTlsError),
+}
+
+#[cfg(feature = "use-rustls")]
+pub async fn rustls_connector(tls_config: &TlsConfiguration) -> Result<RustlsConnector, Error> {
+ let config = match tls_config {
+ TlsConfiguration::Simple {
+ ca,
+ alpn,
+ client_auth,
+ } => {
+ // Add ca to root store if the connection is TLS
+ let mut root_cert_store = RootCertStore::empty();
+ let certs = rustls_pemfile::certs(&mut BufReader::new(Cursor::new(ca)))
+ .collect::<Result<Vec<_>, _>>()?;
+
+ root_cert_store.add_parsable_certificates(certs);
+
+ if root_cert_store.is_empty() {
+ return Err(Error::NoValidCertInChain);
+ }
+
+ let config = ClientConfig::builder().with_root_certificates(root_cert_store);
+
+ // Add der encoded client cert and key
+ let mut config = if let Some(client) = client_auth.as_ref() {
+ let certs =
+ rustls_pemfile::certs(&mut BufReader::new(Cursor::new(client.0.clone())))
+ .collect::<Result<Vec<_>, _>>()?;
+ if certs.is_empty() {
+ return Err(Error::NoValidClientCertInChain);
+ }
+
+ // Create buffer for key file
+ let mut key_buffer = BufReader::new(Cursor::new(client.1.clone()));
+
+ // Read PEM items until we find a valid key.
+ let key = loop {
+ let item = rustls_pemfile::read_one(&mut key_buffer)?;
+ match item {
+ Some(Item::Sec1Key(key)) => {
+ break key.into();
+ }
+ Some(Item::Pkcs1Key(key)) => {
+ break key.into();
+ }
+ Some(Item::Pkcs8Key(key)) => {
+ break key.into();
+ }
+ None => return Err(Error::NoValidKeyInChain),
+ _ => {}
+ }
+ };
+
+ config.with_client_auth_cert(certs, key)?
+ } else {
+ config.with_no_client_auth()
+ };
+
+ // Set ALPN
+ if let Some(alpn) = alpn.as_ref() {
+ config.alpn_protocols.extend_from_slice(alpn);
+ }
+
+ Arc::new(config)
+ }
+ TlsConfiguration::Rustls(tls_client_config) => tls_client_config.clone(),
+ #[allow(unreachable_patterns)]
+ _ => unreachable!("This cannot be called for other TLS backends than Rustls"),
+ };
+
+ Ok(RustlsConnector::from(config))
+}
+
+#[cfg(feature = "use-native-tls")]
+pub async fn native_tls_connector(
+ tls_config: &TlsConfiguration,
+) -> Result<NativeTlsConnector, Error> {
+ let connector = match tls_config {
+ TlsConfiguration::SimpleNative { ca, client_auth } => {
+ let cert = native_tls::Certificate::from_pem(ca)?;
+
+ let mut connector_builder = native_tls::TlsConnector::builder();
+ connector_builder.add_root_certificate(cert);
+
+ if let Some((der, password)) = client_auth {
+ let identity = Identity::from_pkcs12(der, password)?;
+ connector_builder.identity(identity);
+ }
+
+ connector_builder.build()?
+ }
+ TlsConfiguration::Native => native_tls::TlsConnector::new()?,
+ #[allow(unreachable_patterns)]
+ _ => unreachable!("This cannot be called for other TLS backends than Native TLS"),
+ };
+
+ Ok(connector.into())
+}
+
+pub async fn tls_connect(
+ addr: &str,
+ _port: u16,
+ tls_config: &TlsConfiguration,
+ tcp: Box<dyn N>,
+) -> Result<Box<dyn N>, Error> {
+ let tls: Box<dyn N> = match tls_config {
+ #[cfg(feature = "use-rustls")]
+ TlsConfiguration::Simple { .. } | TlsConfiguration::Rustls(_) => {
+ let connector = rustls_connector(tls_config).await?;
+ let domain = ServerName::try_from(addr)?.to_owned();
+ Box::new(connector.connect(domain, tcp).await?)
+ }
+ #[cfg(feature = "use-native-tls")]
+ TlsConfiguration::Native | TlsConfiguration::SimpleNative { .. } => {
+ let connector = native_tls_connector(tls_config).await?;
+ Box::new(connector.connect(addr, tcp).await?)
+ }
+ #[allow(unreachable_patterns)]
+ _ => panic!("Unknown or not enabled TLS backend configuration"),
+ };
+ Ok(tls)
+}
diff --git a/vendor/rumqttc/src/v5/client.rs b/vendor/rumqttc/src/v5/client.rs
new file mode 100644
index 0000000000..910da504f6
--- /dev/null
+++ b/vendor/rumqttc/src/v5/client.rs
@@ -0,0 +1,871 @@
+//! This module offers a high level synchronous and asynchronous abstraction to
+//! async eventloop.
+use std::time::Duration;
+
+use super::mqttbytes::v5::{
+ Filter, PubAck, PubRec, Publish, PublishProperties, Subscribe, SubscribeProperties,
+ Unsubscribe, UnsubscribeProperties,
+};
+use super::mqttbytes::QoS;
+use super::{ConnectionError, Event, EventLoop, MqttOptions, Request};
+use crate::valid_topic;
+
+use bytes::Bytes;
+use flume::{SendError, Sender, TrySendError};
+use futures_util::FutureExt;
+use tokio::runtime::{self, Runtime};
+use tokio::time::timeout;
+
+/// Client Error
+#[derive(Debug, thiserror::Error)]
+pub enum ClientError {
+ #[error("Failed to send mqtt requests to eventloop")]
+ Request(Request),
+ #[error("Failed to send mqtt requests to eventloop")]
+ TryRequest(Request),
+}
+
+impl From<SendError<Request>> for ClientError {
+ fn from(e: SendError<Request>) -> Self {
+ Self::Request(e.into_inner())
+ }
+}
+
+impl From<TrySendError<Request>> for ClientError {
+ fn from(e: TrySendError<Request>) -> Self {
+ Self::TryRequest(e.into_inner())
+ }
+}
+
+/// An asynchronous client, communicates with MQTT `EventLoop`.
+///
+/// This is cloneable and can be used to asynchronously [`publish`](`AsyncClient::publish`),
+/// [`subscribe`](`AsyncClient::subscribe`) through the `EventLoop`, which is to be polled parallelly.
+///
+/// **NOTE**: The `EventLoop` must be regularly polled in order to send, receive and process packets
+/// from the broker, i.e. move ahead.
+#[derive(Clone, Debug)]
+pub struct AsyncClient {
+ request_tx: Sender<Request>,
+}
+
+impl AsyncClient {
+ /// Create a new `AsyncClient`.
+ ///
+ /// `cap` specifies the capacity of the bounded async channel.
+ pub fn new(options: MqttOptions, cap: usize) -> (AsyncClient, EventLoop) {
+ let eventloop = EventLoop::new(options, cap);
+ let request_tx = eventloop.requests_tx.clone();
+
+ let client = AsyncClient { request_tx };
+
+ (client, eventloop)
+ }
+
+ /// Create a new `AsyncClient` from a channel `Sender`.
+ ///
+ /// This is mostly useful for creating a test instance where you can
+ /// listen on the corresponding receiver.
+ pub fn from_senders(request_tx: Sender<Request>) -> AsyncClient {
+ AsyncClient { request_tx }
+ }
+
+ /// Sends a MQTT Publish to the `EventLoop`.
+ async fn handle_publish<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ properties: Option<PublishProperties>,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ let topic = topic.into();
+ let mut publish = Publish::new(&topic, qos, payload, properties);
+ publish.retain = retain;
+ let publish = Request::Publish(publish);
+ if !valid_topic(&topic) {
+ return Err(ClientError::Request(publish));
+ }
+ self.request_tx.send_async(publish).await?;
+ Ok(())
+ }
+
+ pub async fn publish_with_properties<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ properties: PublishProperties,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ self.handle_publish(topic, qos, retain, payload, Some(properties))
+ .await
+ }
+
+ pub async fn publish<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ self.handle_publish(topic, qos, retain, payload, None).await
+ }
+
+ /// Attempts to send a MQTT Publish to the `EventLoop`.
+ fn handle_try_publish<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ properties: Option<PublishProperties>,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ let topic = topic.into();
+ let mut publish = Publish::new(&topic, qos, payload, properties);
+ publish.retain = retain;
+ let publish = Request::Publish(publish);
+ if !valid_topic(&topic) {
+ return Err(ClientError::TryRequest(publish));
+ }
+ self.request_tx.try_send(publish)?;
+ Ok(())
+ }
+
+ pub fn try_publish_with_properties<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ properties: PublishProperties,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ self.handle_try_publish(topic, qos, retain, payload, Some(properties))
+ }
+
+ pub fn try_publish<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ self.handle_try_publish(topic, qos, retain, payload, None)
+ }
+
+ /// Sends a MQTT PubAck to the `EventLoop`. Only needed in if `manual_acks` flag is set.
+ pub async fn ack(&self, publish: &Publish) -> Result<(), ClientError> {
+ let ack = get_ack_req(publish);
+
+ if let Some(ack) = ack {
+ self.request_tx.send_async(ack).await?;
+ }
+ Ok(())
+ }
+
+ /// Attempts to send a MQTT PubAck to the `EventLoop`. Only needed in if `manual_acks` flag is set.
+ pub fn try_ack(&self, publish: &Publish) -> Result<(), ClientError> {
+ let ack = get_ack_req(publish);
+ if let Some(ack) = ack {
+ self.request_tx.try_send(ack)?;
+ }
+ Ok(())
+ }
+
+ /// Sends a MQTT Publish to the `EventLoop`
+ async fn handle_publish_bytes<S>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: Bytes,
+ properties: Option<PublishProperties>,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ {
+ let topic = topic.into();
+ let mut publish = Publish::new(&topic, qos, payload, properties);
+ publish.retain = retain;
+ let publish = Request::Publish(publish);
+ if !valid_topic(&topic) {
+ return Err(ClientError::TryRequest(publish));
+ }
+ self.request_tx.send_async(publish).await?;
+ Ok(())
+ }
+
+ pub async fn publish_bytes_with_properties<S>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: Bytes,
+ properties: PublishProperties,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ {
+ self.handle_publish_bytes(topic, qos, retain, payload, Some(properties))
+ .await
+ }
+
+ pub async fn publish_bytes<S>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: Bytes,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ {
+ self.handle_publish_bytes(topic, qos, retain, payload, None)
+ .await
+ }
+
+ /// Sends a MQTT Subscribe to the `EventLoop`
+ async fn handle_subscribe<S: Into<String>>(
+ &self,
+ topic: S,
+ qos: QoS,
+ properties: Option<SubscribeProperties>,
+ ) -> Result<(), ClientError> {
+ let filter = Filter::new(topic, qos);
+ let subscribe = Subscribe::new(filter, properties);
+ let request: Request = Request::Subscribe(subscribe);
+ self.request_tx.send_async(request).await?;
+ Ok(())
+ }
+
+ pub async fn subscribe_with_properties<S: Into<String>>(
+ &self,
+ topic: S,
+ qos: QoS,
+ properties: SubscribeProperties,
+ ) -> Result<(), ClientError> {
+ self.handle_subscribe(topic, qos, Some(properties)).await
+ }
+
+ pub async fn subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<(), ClientError> {
+ self.handle_subscribe(topic, qos, None).await
+ }
+
+ /// Attempts to send a MQTT Subscribe to the `EventLoop`
+ fn handle_try_subscribe<S: Into<String>>(
+ &self,
+ topic: S,
+ qos: QoS,
+ properties: Option<SubscribeProperties>,
+ ) -> Result<(), ClientError> {
+ let filter = Filter::new(topic, qos);
+ let subscribe = Subscribe::new(filter, properties);
+ let request = Request::Subscribe(subscribe);
+ self.request_tx.try_send(request)?;
+ Ok(())
+ }
+
+ pub fn try_subscribe_with_properties<S: Into<String>>(
+ &self,
+ topic: S,
+ qos: QoS,
+ properties: SubscribeProperties,
+ ) -> Result<(), ClientError> {
+ self.handle_try_subscribe(topic, qos, Some(properties))
+ }
+
+ pub fn try_subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<(), ClientError> {
+ self.handle_try_subscribe(topic, qos, None)
+ }
+
+ /// Sends a MQTT Subscribe for multiple topics to the `EventLoop`
+ async fn handle_subscribe_many<T>(
+ &self,
+ topics: T,
+ properties: Option<SubscribeProperties>,
+ ) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ let subscribe = Subscribe::new_many(topics, properties);
+ let request = Request::Subscribe(subscribe);
+ self.request_tx.send_async(request).await?;
+ Ok(())
+ }
+
+ pub async fn subscribe_many_with_properties<T>(
+ &self,
+ topics: T,
+ properties: SubscribeProperties,
+ ) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ self.handle_subscribe_many(topics, Some(properties)).await
+ }
+
+ pub async fn subscribe_many<T>(&self, topics: T) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ self.handle_subscribe_many(topics, None).await
+ }
+
+ /// Attempts to send a MQTT Subscribe for multiple topics to the `EventLoop`
+ fn handle_try_subscribe_many<T>(
+ &self,
+ topics: T,
+ properties: Option<SubscribeProperties>,
+ ) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ let subscribe = Subscribe::new_many(topics, properties);
+ let request = Request::Subscribe(subscribe);
+ self.request_tx.try_send(request)?;
+ Ok(())
+ }
+
+ pub fn try_subscribe_many_with_properties<T>(
+ &self,
+ topics: T,
+ properties: SubscribeProperties,
+ ) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ self.handle_try_subscribe_many(topics, Some(properties))
+ }
+
+ pub fn try_subscribe_many<T>(&self, topics: T) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ self.handle_try_subscribe_many(topics, None)
+ }
+
+ /// Sends a MQTT Unsubscribe to the `EventLoop`
+ async fn handle_unsubscribe<S: Into<String>>(
+ &self,
+ topic: S,
+ properties: Option<UnsubscribeProperties>,
+ ) -> Result<(), ClientError> {
+ let unsubscribe = Unsubscribe::new(topic, properties);
+ let request = Request::Unsubscribe(unsubscribe);
+ self.request_tx.send_async(request).await?;
+ Ok(())
+ }
+
+ pub async fn unsubscribe_with_properties<S: Into<String>>(
+ &self,
+ topic: S,
+ properties: UnsubscribeProperties,
+ ) -> Result<(), ClientError> {
+ self.handle_unsubscribe(topic, Some(properties)).await
+ }
+
+ pub async fn unsubscribe<S: Into<String>>(&self, topic: S) -> Result<(), ClientError> {
+ self.handle_unsubscribe(topic, None).await
+ }
+
+ /// Attempts to send a MQTT Unsubscribe to the `EventLoop`
+ fn handle_try_unsubscribe<S: Into<String>>(
+ &self,
+ topic: S,
+ properties: Option<UnsubscribeProperties>,
+ ) -> Result<(), ClientError> {
+ let unsubscribe = Unsubscribe::new(topic, properties);
+ let request = Request::Unsubscribe(unsubscribe);
+ self.request_tx.try_send(request)?;
+ Ok(())
+ }
+
+ pub fn try_unsubscribe_with_properties<S: Into<String>>(
+ &self,
+ topic: S,
+ properties: UnsubscribeProperties,
+ ) -> Result<(), ClientError> {
+ self.handle_try_unsubscribe(topic, Some(properties))
+ }
+
+ pub fn try_unsubscribe<S: Into<String>>(&self, topic: S) -> Result<(), ClientError> {
+ self.handle_try_unsubscribe(topic, None)
+ }
+
+ /// Sends a MQTT disconnect to the `EventLoop`
+ pub async fn disconnect(&self) -> Result<(), ClientError> {
+ let request = Request::Disconnect;
+ self.request_tx.send_async(request).await?;
+ Ok(())
+ }
+
+ /// Attempts to send a MQTT disconnect to the `EventLoop`
+ pub fn try_disconnect(&self) -> Result<(), ClientError> {
+ let request = Request::Disconnect;
+ self.request_tx.try_send(request)?;
+ Ok(())
+ }
+}
+
+fn get_ack_req(publish: &Publish) -> Option<Request> {
+ let ack = match publish.qos {
+ QoS::AtMostOnce => return None,
+ QoS::AtLeastOnce => Request::PubAck(PubAck::new(publish.pkid, None)),
+ QoS::ExactlyOnce => Request::PubRec(PubRec::new(publish.pkid, None)),
+ };
+ Some(ack)
+}
+
+/// A synchronous client, communicates with MQTT `EventLoop`.
+///
+/// This is cloneable and can be used to synchronously [`publish`](`AsyncClient::publish`),
+/// [`subscribe`](`AsyncClient::subscribe`) through the `EventLoop`/`Connection`, which is to be polled in parallel
+/// by iterating over the object returned by [`Connection.iter()`](Connection::iter) in a separate thread.
+///
+/// **NOTE**: The `EventLoop`/`Connection` must be regularly polled(`.next()` in case of `Connection`) in order
+/// to send, receive and process packets from the broker, i.e. move ahead.
+///
+/// An asynchronous channel handle can also be extracted if necessary.
+#[derive(Clone)]
+pub struct Client {
+ client: AsyncClient,
+}
+
+impl Client {
+ /// Create a new `Client`
+ ///
+ /// `cap` specifies the capacity of the bounded async channel.
+ pub fn new(options: MqttOptions, cap: usize) -> (Client, Connection) {
+ let (client, eventloop) = AsyncClient::new(options, cap);
+ let client = Client { client };
+
+ let runtime = runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .unwrap();
+
+ let connection = Connection::new(eventloop, runtime);
+ (client, connection)
+ }
+
+ /// Create a new `Client` from a channel `Sender`.
+ ///
+ /// This is mostly useful for creating a test instance where you can
+ /// listen on the corresponding receiver.
+ pub fn from_sender(request_tx: Sender<Request>) -> Client {
+ Client {
+ client: AsyncClient::from_senders(request_tx),
+ }
+ }
+
+ /// Sends a MQTT Publish to the `EventLoop`
+ fn handle_publish<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ properties: Option<PublishProperties>,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ let topic = topic.into();
+ let mut publish = Publish::new(&topic, qos, payload, properties);
+ publish.retain = retain;
+ let publish = Request::Publish(publish);
+ if !valid_topic(&topic) {
+ return Err(ClientError::Request(publish));
+ }
+ self.client.request_tx.send(publish)?;
+ Ok(())
+ }
+
+ pub fn publish_with_properties<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ properties: PublishProperties,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ self.handle_publish(topic, qos, retain, payload, Some(properties))
+ }
+
+ pub fn publish<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ self.handle_publish(topic, qos, retain, payload, None)
+ }
+
+ pub fn try_publish_with_properties<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ properties: PublishProperties,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ self.client
+ .try_publish_with_properties(topic, qos, retain, payload, properties)
+ }
+
+ pub fn try_publish<S, P>(
+ &self,
+ topic: S,
+ qos: QoS,
+ retain: bool,
+ payload: P,
+ ) -> Result<(), ClientError>
+ where
+ S: Into<String>,
+ P: Into<Bytes>,
+ {
+ self.client.try_publish(topic, qos, retain, payload)
+ }
+
+ /// Sends a MQTT PubAck to the `EventLoop`. Only needed in if `manual_acks` flag is set.
+ pub fn ack(&self, publish: &Publish) -> Result<(), ClientError> {
+ let ack = get_ack_req(publish);
+
+ if let Some(ack) = ack {
+ self.client.request_tx.send(ack)?;
+ }
+ Ok(())
+ }
+
+ /// Sends a MQTT PubAck to the `EventLoop`. Only needed in if `manual_acks` flag is set.
+ pub fn try_ack(&self, publish: &Publish) -> Result<(), ClientError> {
+ self.client.try_ack(publish)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT Subscribe to the `EventLoop`
+ fn handle_subscribe<S: Into<String>>(
+ &self,
+ topic: S,
+ qos: QoS,
+ properties: Option<SubscribeProperties>,
+ ) -> Result<(), ClientError> {
+ let filter = Filter::new(topic, qos);
+ let subscribe = Subscribe::new(filter, properties);
+ let request = Request::Subscribe(subscribe);
+ self.client.request_tx.send(request)?;
+ Ok(())
+ }
+
+ pub fn subscribe_with_properties<S: Into<String>>(
+ &self,
+ topic: S,
+ qos: QoS,
+ properties: SubscribeProperties,
+ ) -> Result<(), ClientError> {
+ self.handle_subscribe(topic, qos, Some(properties))
+ }
+
+ pub fn subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<(), ClientError> {
+ self.handle_subscribe(topic, qos, None)
+ }
+
+ /// Sends a MQTT Subscribe to the `EventLoop`
+ pub fn try_subscribe_with_properties<S: Into<String>>(
+ &self,
+ topic: S,
+ qos: QoS,
+ properties: SubscribeProperties,
+ ) -> Result<(), ClientError> {
+ self.client
+ .try_subscribe_with_properties(topic, qos, properties)
+ }
+
+ pub fn try_subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<(), ClientError> {
+ self.client.try_subscribe(topic, qos)
+ }
+
+ /// Sends a MQTT Subscribe for multiple topics to the `EventLoop`
+ fn handle_subscribe_many<T>(
+ &self,
+ topics: T,
+ properties: Option<SubscribeProperties>,
+ ) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ let subscribe = Subscribe::new_many(topics, properties);
+ let request = Request::Subscribe(subscribe);
+ self.client.request_tx.send(request)?;
+ Ok(())
+ }
+
+ pub fn subscribe_many_with_properties<T>(
+ &self,
+ topics: T,
+ properties: SubscribeProperties,
+ ) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ self.handle_subscribe_many(topics, Some(properties))
+ }
+
+ pub fn subscribe_many<T>(&self, topics: T) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ self.handle_subscribe_many(topics, None)
+ }
+
+ pub fn try_subscribe_many_with_properties<T>(
+ &self,
+ topics: T,
+ properties: SubscribeProperties,
+ ) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ self.client
+ .try_subscribe_many_with_properties(topics, properties)
+ }
+
+ pub fn try_subscribe_many<T>(&self, topics: T) -> Result<(), ClientError>
+ where
+ T: IntoIterator<Item = Filter>,
+ {
+ self.client.try_subscribe_many(topics)
+ }
+
+ /// Sends a MQTT Unsubscribe to the `EventLoop`
+ fn handle_unsubscribe<S: Into<String>>(
+ &self,
+ topic: S,
+ properties: Option<UnsubscribeProperties>,
+ ) -> Result<(), ClientError> {
+ let unsubscribe = Unsubscribe::new(topic, properties);
+ let request = Request::Unsubscribe(unsubscribe);
+ self.client.request_tx.send(request)?;
+ Ok(())
+ }
+
+ pub fn unsubscribe_with_properties<S: Into<String>>(
+ &self,
+ topic: S,
+ properties: UnsubscribeProperties,
+ ) -> Result<(), ClientError> {
+ self.handle_unsubscribe(topic, Some(properties))
+ }
+
+ pub fn unsubscribe<S: Into<String>>(&self, topic: S) -> Result<(), ClientError> {
+ self.handle_unsubscribe(topic, None)
+ }
+
+ /// Sends a MQTT Unsubscribe to the `EventLoop`
+ pub fn try_unsubscribe_with_properties<S: Into<String>>(
+ &self,
+ topic: S,
+ properties: UnsubscribeProperties,
+ ) -> Result<(), ClientError> {
+ self.client
+ .try_unsubscribe_with_properties(topic, properties)
+ }
+
+ pub fn try_unsubscribe<S: Into<String>>(&self, topic: S) -> Result<(), ClientError> {
+ self.client.try_unsubscribe(topic)
+ }
+
+ /// Sends a MQTT disconnect to the `EventLoop`
+ pub fn disconnect(&self) -> Result<(), ClientError> {
+ let request = Request::Disconnect;
+ self.client.request_tx.send(request)?;
+ Ok(())
+ }
+
+ /// Sends a MQTT disconnect to the `EventLoop`
+ pub fn try_disconnect(&self) -> Result<(), ClientError> {
+ self.client.try_disconnect()?;
+ Ok(())
+ }
+}
+
+/// Error type returned by [`Connection::recv`]
+#[derive(Debug, Eq, PartialEq)]
+pub struct RecvError;
+
+/// Error type returned by [`Connection::try_recv`]
+#[derive(Debug, Eq, PartialEq)]
+pub enum TryRecvError {
+ /// User has closed requests channel
+ Disconnected,
+ /// Did not resolve
+ Empty,
+}
+
+/// Error type returned by [`Connection::recv_timeout`]
+#[derive(Debug, Eq, PartialEq)]
+pub enum RecvTimeoutError {
+ /// User has closed requests channel
+ Disconnected,
+ /// Recv request timedout
+ Timeout,
+}
+
+/// MQTT connection. Maintains all the necessary state
+pub struct Connection {
+ pub eventloop: EventLoop,
+ runtime: Runtime,
+}
+impl Connection {
+ fn new(eventloop: EventLoop, runtime: Runtime) -> Connection {
+ Connection { eventloop, runtime }
+ }
+
+ /// Returns an iterator over this connection. Iterating over this is all that's
+ /// necessary to make connection progress and maintain a robust connection.
+ /// Just continuing to loop will reconnect
+ /// **NOTE** Don't block this while iterating
+ // ideally this should be named iter_mut because it requires a mutable reference
+ // Also we can implement IntoIter for this to make it easy to iterate over it
+ #[must_use = "Connection should be iterated over a loop to make progress"]
+ pub fn iter(&mut self) -> Iter<'_> {
+ Iter { connection: self }
+ }
+
+ /// Attempt to fetch an incoming [`Event`] on the [`EvenLoop`], returning an error
+ /// if all clients/users have closed requests channel.
+ ///
+ /// [`EvenLoop`]: super::EventLoop
+ pub fn recv(&mut self) -> Result<Result<Event, ConnectionError>, RecvError> {
+ let f = self.eventloop.poll();
+ let event = self.runtime.block_on(f);
+
+ resolve_event(event).ok_or(RecvError)
+ }
+
+ /// Attempt to fetch an incoming [`Event`] on the [`EvenLoop`], returning an error
+ /// if none immediately present or all clients/users have closed requests channel.
+ ///
+ /// [`EvenLoop`]: super::EventLoop
+ pub fn try_recv(&mut self) -> Result<Result<Event, ConnectionError>, TryRecvError> {
+ let f = self.eventloop.poll();
+ // Enters the runtime context so we can poll the future, as required by `now_or_never()`.
+ // ref: https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html#method.enter
+ let _guard = self.runtime.enter();
+ let event = f.now_or_never().ok_or(TryRecvError::Empty)?;
+
+ resolve_event(event).ok_or(TryRecvError::Disconnected)
+ }
+
+ /// Attempt to fetch an incoming [`Event`] on the [`EvenLoop`], returning an error
+ /// if all clients/users have closed requests channel or the timeout has expired.
+ ///
+ /// [`EvenLoop`]: super::EventLoop
+ pub fn recv_timeout(
+ &mut self,
+ duration: Duration,
+ ) -> Result<Result<Event, ConnectionError>, RecvTimeoutError> {
+ let f = self.eventloop.poll();
+ let event = self
+ .runtime
+ .block_on(async { timeout(duration, f).await })
+ .map_err(|_| RecvTimeoutError::Timeout)?;
+
+ resolve_event(event).ok_or(RecvTimeoutError::Disconnected)
+ }
+}
+
+fn resolve_event(event: Result<Event, ConnectionError>) -> Option<Result<Event, ConnectionError>> {
+ match event {
+ Ok(v) => Some(Ok(v)),
+ // closing of request channel should stop the iterator
+ Err(ConnectionError::RequestsDone) => {
+ trace!("Done with requests");
+ None
+ }
+ Err(e) => Some(Err(e)),
+ }
+}
+
+/// Iterator which polls the `EventLoop` for connection progress
+pub struct Iter<'a> {
+ connection: &'a mut Connection,
+}
+
+impl Iterator for Iter<'_> {
+ type Item = Result<Event, ConnectionError>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.connection.recv().ok()
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use crate::v5::mqttbytes::v5::LastWill;
+
+ use super::*;
+
+ #[test]
+ fn calling_iter_twice_on_connection_shouldnt_panic() {
+ use std::time::Duration;
+
+ let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
+ let will = LastWill::new("hello/world", "good bye", QoS::AtMostOnce, false, None);
+ mqttoptions
+ .set_keep_alive(Duration::from_secs(5))
+ .set_last_will(will);
+
+ let (_, mut connection) = Client::new(mqttoptions, 10);
+ let _ = connection.iter();
+ let _ = connection.iter();
+ }
+
+ #[test]
+ fn should_be_able_to_build_test_client_from_channel() {
+ let (tx, rx) = flume::bounded(1);
+ let client = Client::from_sender(tx);
+ client
+ .publish("hello/world", QoS::ExactlyOnce, false, "good bye")
+ .expect("Should be able to publish");
+ let _ = rx.try_recv().expect("Should have message");
+ }
+}
diff --git a/vendor/rumqttc/src/v5/eventloop.rs b/vendor/rumqttc/src/v5/eventloop.rs
new file mode 100644
index 0000000000..36c10971df
--- /dev/null
+++ b/vendor/rumqttc/src/v5/eventloop.rs
@@ -0,0 +1,418 @@
+use super::framed::Network;
+use super::mqttbytes::v5::*;
+use super::{Incoming, MqttOptions, MqttState, Outgoing, Request, StateError, Transport};
+use crate::eventloop::socket_connect;
+use crate::framed::N;
+
+use flume::{bounded, Receiver, Sender};
+use tokio::select;
+use tokio::time::{self, error::Elapsed, Instant, Sleep};
+
+use std::collections::VecDeque;
+use std::convert::TryInto;
+use std::io;
+use std::pin::Pin;
+use std::time::Duration;
+
+use super::mqttbytes::v5::ConnectReturnCode;
+
+#[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+use crate::tls;
+
+#[cfg(unix)]
+use {std::path::Path, tokio::net::UnixStream};
+
+#[cfg(feature = "websocket")]
+use {
+ crate::websockets::{split_url, validate_response_headers, UrlError},
+ async_tungstenite::tungstenite::client::IntoClientRequest,
+ ws_stream_tungstenite::WsStream,
+};
+
+#[cfg(feature = "proxy")]
+use crate::proxy::ProxyError;
+
+/// Critical errors during eventloop polling
+#[derive(Debug, thiserror::Error)]
+pub enum ConnectionError {
+ #[error("Mqtt state: {0}")]
+ MqttState(#[from] StateError),
+ #[error("Timeout")]
+ Timeout(#[from] Elapsed),
+ #[cfg(feature = "websocket")]
+ #[error("Websocket: {0}")]
+ Websocket(#[from] async_tungstenite::tungstenite::error::Error),
+ #[cfg(feature = "websocket")]
+ #[error("Websocket Connect: {0}")]
+ WsConnect(#[from] http::Error),
+ #[cfg(any(feature = "use-rustls", feature = "use-native-tls"))]
+ #[error("TLS: {0}")]
+ Tls(#[from] tls::Error),
+ #[error("I/O: {0}")]
+ Io(#[from] io::Error),
+ #[error("Connection refused, return code: `{0:?}`")]
+ ConnectionRefused(ConnectReturnCode),
+ #[error("Expected ConnAck packet, received: {0:?}")]
+ NotConnAck(Box<Packet>),
+ #[error("Requests done")]
+ RequestsDone,
+ #[cfg(feature = "websocket")]
+ #[error("Invalid Url: {0}")]
+ InvalidUrl(#[from] UrlError),
+ #[cfg(feature = "proxy")]
+ #[error("Proxy Connect: {0}")]
+ Proxy(#[from] ProxyError),
+ #[cfg(feature = "websocket")]
+ #[error("Websocket response validation error: ")]
+ ResponseValidation(#[from] crate::websockets::ValidationError),
+}
+
+/// Eventloop with all the state of a connection
+pub struct EventLoop {
+ /// Options of the current mqtt connection
+ pub options: MqttOptions,
+ /// Current state of the connection
+ pub state: MqttState,
+ /// Request stream
+ requests_rx: Receiver<Request>,
+ /// Requests handle to send requests
+ pub(crate) requests_tx: Sender<Request>,
+ /// Pending packets from last session
+ pub pending: VecDeque<Request>,
+ /// Network connection to the broker
+ network: Option<Network>,
+ /// Keep alive time
+ keepalive_timeout: Option<Pin<Box<Sleep>>>,
+}
+
+/// Events which can be yielded by the event loop
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum Event {
+ Incoming(Incoming),
+ Outgoing(Outgoing),
+}
+
+impl EventLoop {
+ /// New MQTT `EventLoop`
+ ///
+ /// When connection encounters critical errors (like auth failure), user has a choice to
+ /// access and update `options`, `state` and `requests`.
+ pub fn new(options: MqttOptions, cap: usize) -> EventLoop {
+ let (requests_tx, requests_rx) = bounded(cap);
+ let pending = VecDeque::new();
+ let inflight_limit = options.outgoing_inflight_upper_limit.unwrap_or(u16::MAX);
+ let manual_acks = options.manual_acks;
+
+ EventLoop {
+ options,
+ state: MqttState::new(inflight_limit, manual_acks),
+ requests_tx,
+ requests_rx,
+ pending,
+ network: None,
+ keepalive_timeout: None,
+ }
+ }
+
+ /// Last session might contain packets which aren't acked. MQTT says these packets should be
+ /// republished in the next session. Move pending messages from state to eventloop, drops the
+ /// underlying network connection and clears the keepalive timeout if any.
+ ///
+ /// > NOTE: Use only when EventLoop is blocked on network and unable to immediately handle disconnect.
+ /// > Also, while this helps prevent data loss, the pending list length should be managed properly.
+ /// > For this reason we recommend setting [`AsycClient`](super::AsyncClient)'s channel capacity to `0`.
+ pub fn clean(&mut self) {
+ self.network = None;
+ self.keepalive_timeout = None;
+ self.pending.extend(self.state.clean());
+
+ // drain requests from channel which weren't yet received
+ let requests_in_channel = self.requests_rx.drain();
+ self.pending.extend(requests_in_channel);
+ }
+
+ /// Yields Next notification or outgoing request and periodically pings
+ /// the broker. Continuing to poll will reconnect to the broker if there is
+ /// a disconnection.
+ /// **NOTE** Don't block this while iterating
+ pub async fn poll(&mut self) -> Result<Event, ConnectionError> {
+ if self.network.is_none() {
+ let (network, connack) = time::timeout(
+ Duration::from_secs(self.options.connection_timeout()),
+ connect(&mut self.options),
+ )
+ .await??;
+ self.network = Some(network);
+
+ if self.keepalive_timeout.is_none() {
+ self.keepalive_timeout = Some(Box::pin(time::sleep(self.options.keep_alive)));
+ }
+
+ self.state.handle_incoming_packet(connack)?;
+ }
+
+ match self.select().await {
+ Ok(v) => Ok(v),
+ Err(e) => {
+ self.clean();
+ Err(e)
+ }
+ }
+ }
+
+ /// Select on network and requests and generate keepalive pings when necessary
+ async fn select(&mut self) -> Result<Event, ConnectionError> {
+ let network = self.network.as_mut().unwrap();
+ // let await_acks = self.state.await_acks;
+
+ let inflight_full = self.state.inflight >= self.state.max_outgoing_inflight;
+ let collision = self.state.collision.is_some();
+
+ // Read buffered events from previous polls before calling a new poll
+ if let Some(event) = self.state.events.pop_front() {
+ return Ok(event);
+ }
+
+ // this loop is necessary since self.incoming.pop_front() might return None. In that case,
+ // instead of returning a None event, we try again.
+ select! {
+ // Handles pending and new requests.
+ // If available, prioritises pending requests from previous session.
+ // Else, pulls next request from user requests channel.
+ // If conditions in the below branch are for flow control.
+ // The branch is disabled if there's no pending messages and new user requests
+ // cannot be serviced due flow control.
+ // We read next user user request only when inflight messages are < configured inflight
+ // and there are no collisions while handling previous outgoing requests.
+ //
+ // Flow control is based on ack count. If inflight packet count in the buffer is
+ // less than max_inflight setting, next outgoing request will progress. For this
+ // to work correctly, broker should ack in sequence (a lot of brokers won't)
+ //
+ // E.g If max inflight = 5, user requests will be blocked when inflight queue
+ // looks like this -> [1, 2, 3, 4, 5].
+ // If broker acking 2 instead of 1 -> [1, x, 3, 4, 5].
+ // This pulls next user request. But because max packet id = max_inflight, next
+ // user request's packet id will roll to 1. This replaces existing packet id 1.
+ // Resulting in a collision
+ //
+ // Eventloop can stop receiving outgoing user requests when previous outgoing
+ // request collided. I.e collision state. Collision state will be cleared only
+ // when correct ack is received
+ // Full inflight queue will look like -> [1a, 2, 3, 4, 5].
+ // If 3 is acked instead of 1 first -> [1a, 2, x, 4, 5].
+ // After collision with pkid 1 -> [1b ,2, x, 4, 5].
+ // 1a is saved to state and event loop is set to collision mode stopping new
+ // outgoing requests (along with 1b).
+ o = Self::next_request(
+ &mut self.pending,
+ &self.requests_rx,
+ self.options.pending_throttle
+ ), if !self.pending.is_empty() || (!inflight_full && !collision) => match o {
+ Ok(request) => {
+ self.state.handle_outgoing_packet(request)?;
+ network.flush(&mut self.state.write).await?;
+ Ok(self.state.events.pop_front().unwrap())
+ }
+ Err(_) => Err(ConnectionError::RequestsDone),
+ },
+ // Pull a bunch of packets from network, reply in bunch and yield the first item
+ o = network.readb(&mut self.state) => {
+ o?;
+ // flush all the acks and return first incoming packet
+ network.flush(&mut self.state.write).await?;
+ Ok(self.state.events.pop_front().unwrap())
+ },
+ // We generate pings irrespective of network activity. This keeps the ping logic
+ // simple. We can change this behavior in future if necessary (to prevent extra pings)
+ _ = self.keepalive_timeout.as_mut().unwrap() => {
+ let timeout = self.keepalive_timeout.as_mut().unwrap();
+ timeout.as_mut().reset(Instant::now() + self.options.keep_alive);
+
+ self.state.handle_outgoing_packet(Request::PingReq)?;
+ network.flush(&mut self.state.write).await?;
+ Ok(self.state.events.pop_front().unwrap())
+ }
+ }
+ }
+
+ async fn next_request(
+ pending: &mut VecDeque<Request>,
+ rx: &Receiver<Request>,
+ pending_throttle: Duration,
+ ) -> Result<Request, ConnectionError> {
+ if !pending.is_empty() {
+ time::sleep(pending_throttle).await;
+ // We must call .next() AFTER sleep() otherwise .next() would
+ // advance the iterator but the future might be canceled before return
+ Ok(pending.pop_front().unwrap())
+ } else {
+ match rx.recv_async().await {
+ Ok(r) => Ok(r),
+ Err(_) => Err(ConnectionError::RequestsDone),
+ }
+ }
+ }
+}
+
+/// This stream internally processes requests from the request stream provided to the eventloop
+/// while also consuming byte stream from the network and yielding mqtt packets as the output of
+/// the stream.
+/// This function (for convenience) includes internal delays for users to perform internal sleeps
+/// between re-connections so that cancel semantics can be used during this sleep
+async fn connect(options: &mut MqttOptions) -> Result<(Network, Incoming), ConnectionError> {
+ // connect to the broker
+ let mut network = network_connect(options).await?;
+
+ // make MQTT connection request (which internally awaits for ack)
+ let packet = mqtt_connect(options, &mut network).await?;
+
+ // Last session might contain packets which aren't acked. MQTT says these packets should be
+ // republished in the next session
+ // move pending messages from state to eventloop
+ // let pending = self.state.clean();
+ // self.pending = pending.into_iter();
+ Ok((network, packet))
+}
+
+async fn network_connect(options: &MqttOptions) -> Result<Network, ConnectionError> {
+ let mut max_incoming_pkt_size = Some(options.default_max_incoming_size);
+
+ // Override default value if max_packet_size is set on `connect_properties`
+ if let Some(connect_props) = &options.connect_properties {
+ if let Some(max_size) = connect_props.max_packet_size {
+ let max_size = max_size.try_into().map_err(StateError::Coversion)?;
+ max_incoming_pkt_size = Some(max_size);
+ }
+ }
+
+ // Process Unix files early, as proxy is not supported for them.
+ #[cfg(unix)]
+ if matches!(options.transport(), Transport::Unix) {
+ let file = options.broker_addr.as_str();
+ let socket = UnixStream::connect(Path::new(file)).await?;
+ let network = Network::new(socket, max_incoming_pkt_size);
+ return Ok(network);
+ }
+
+ // For websockets domain and port are taken directly from `broker_addr` (which is a url).
+ let (domain, port) = match options.transport() {
+ #[cfg(feature = "websocket")]
+ Transport::Ws => split_url(&options.broker_addr)?,
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ Transport::Wss(_) => split_url(&options.broker_addr)?,
+ _ => options.broker_address(),
+ };
+
+ let tcp_stream: Box<dyn N> = {
+ #[cfg(feature = "proxy")]
+ match options.proxy() {
+ Some(proxy) => {
+ proxy
+ .connect(&domain, port, options.network_options())
+ .await?
+ }
+ None => {
+ let addr = format!("{domain}:{port}");
+ let tcp = socket_connect(addr, options.network_options()).await?;
+ Box::new(tcp)
+ }
+ }
+ #[cfg(not(feature = "proxy"))]
+ {
+ let addr = format!("{domain}:{port}");
+ let tcp = socket_connect(addr, options.network_options()).await?;
+ Box::new(tcp)
+ }
+ };
+
+ let network = match options.transport() {
+ Transport::Tcp => Network::new(tcp_stream, max_incoming_pkt_size),
+ #[cfg(any(feature = "use-native-tls", feature = "use-rustls"))]
+ Transport::Tls(tls_config) => {
+ let socket =
+ tls::tls_connect(&options.broker_addr, options.port, &tls_config, tcp_stream)
+ .await?;
+ Network::new(socket, max_incoming_pkt_size)
+ }
+ #[cfg(unix)]
+ Transport::Unix => unreachable!(),
+ #[cfg(feature = "websocket")]
+ Transport::Ws => {
+ let mut request = options.broker_addr.as_str().into_client_request()?;
+ request
+ .headers_mut()
+ .insert("Sec-WebSocket-Protocol", "mqtt".parse().unwrap());
+
+ if let Some(request_modifier) = options.request_modifier() {
+ request = request_modifier(request).await;
+ }
+
+ let (socket, response) =
+ async_tungstenite::tokio::client_async(request, tcp_stream).await?;
+ validate_response_headers(response)?;
+
+ Network::new(WsStream::new(socket), max_incoming_pkt_size)
+ }
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ Transport::Wss(tls_config) => {
+ let mut request = options.broker_addr.as_str().into_client_request()?;
+ request
+ .headers_mut()
+ .insert("Sec-WebSocket-Protocol", "mqtt".parse().unwrap());
+
+ if let Some(request_modifier) = options.request_modifier() {
+ request = request_modifier(request).await;
+ }
+
+ let connector = tls::rustls_connector(&tls_config).await?;
+
+ let (socket, response) = async_tungstenite::tokio::client_async_tls_with_connector(
+ request,
+ tcp_stream,
+ Some(connector),
+ )
+ .await?;
+ validate_response_headers(response)?;
+
+ Network::new(WsStream::new(socket), max_incoming_pkt_size)
+ }
+ };
+
+ Ok(network)
+}
+
+async fn mqtt_connect(
+ options: &mut MqttOptions,
+ network: &mut Network,
+) -> Result<Incoming, ConnectionError> {
+ let keep_alive = options.keep_alive().as_secs() as u16;
+ let clean_start = options.clean_start();
+ let client_id = options.client_id();
+ let properties = options.connect_properties();
+
+ let connect = Connect {
+ keep_alive,
+ client_id,
+ clean_start,
+ properties,
+ };
+
+ // send mqtt connect packet
+ network.connect(connect, options).await?;
+
+ // validate connack
+ match network.read().await? {
+ Incoming::ConnAck(connack) if connack.code == ConnectReturnCode::Success => {
+ // Override local keep_alive value if set by server.
+ if let Some(props) = &connack.properties {
+ if let Some(keep_alive) = props.server_keep_alive {
+ options.keep_alive = Duration::from_secs(keep_alive as u64);
+ }
+ }
+ Ok(Packet::ConnAck(connack))
+ }
+ Incoming::ConnAck(connack) => Err(ConnectionError::ConnectionRefused(connack.code)),
+ packet => Err(ConnectionError::NotConnAck(Box::new(packet))),
+ }
+}
diff --git a/vendor/rumqttc/src/v5/framed.rs b/vendor/rumqttc/src/v5/framed.rs
new file mode 100644
index 0000000000..a4f6b525b3
--- /dev/null
+++ b/vendor/rumqttc/src/v5/framed.rs
@@ -0,0 +1,132 @@
+use bytes::BytesMut;
+use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
+
+use super::mqttbytes;
+use super::mqttbytes::v5::{Connect, Login, Packet};
+use super::{Incoming, MqttOptions, MqttState, StateError};
+use std::io;
+
+/// Network transforms packets <-> frames efficiently. It takes
+/// advantage of pre-allocation, buffering and vectorization when
+/// appropriate to achieve performance
+pub struct Network {
+ /// Socket for IO
+ socket: Box<dyn N>,
+ /// Buffered reads
+ read: BytesMut,
+ /// Maximum packet size
+ max_incoming_size: Option<usize>,
+ /// Maximum readv count
+ max_readb_count: usize,
+}
+
+impl Network {
+ pub fn new(socket: impl N + 'static, max_incoming_size: Option<usize>) -> Network {
+ let socket = Box::new(socket) as Box<dyn N>;
+ Network {
+ socket,
+ read: BytesMut::with_capacity(10 * 1024),
+ max_incoming_size,
+ max_readb_count: 10,
+ }
+ }
+
+ /// Reads more than 'required' bytes to frame a packet into self.read buffer
+ async fn read_bytes(&mut self, required: usize) -> io::Result<usize> {
+ let mut total_read = 0;
+ loop {
+ let read = self.socket.read_buf(&mut self.read).await?;
+ if 0 == read {
+ return if self.read.is_empty() {
+ Err(io::Error::new(
+ io::ErrorKind::ConnectionAborted,
+ "connection closed by peer",
+ ))
+ } else {
+ Err(io::Error::new(
+ io::ErrorKind::ConnectionReset,
+ "connection reset by peer",
+ ))
+ };
+ }
+
+ total_read += read;
+ if total_read >= required {
+ return Ok(total_read);
+ }
+ }
+ }
+
+ pub async fn read(&mut self) -> io::Result<Incoming> {
+ loop {
+ let required = match Packet::read(&mut self.read, self.max_incoming_size) {
+ Ok(packet) => return Ok(packet),
+ Err(mqttbytes::Error::InsufficientBytes(required)) => required,
+ Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e.to_string())),
+ };
+
+ // read more packets until a frame can be created. This function
+ // blocks until a frame can be created. Use this in a select! branch
+ self.read_bytes(required).await?;
+ }
+ }
+
+ /// Read packets in bulk. This allow replies to be in bulk. This method is used
+ /// after the connection is established to read a bunch of incoming packets
+ pub async fn readb(&mut self, state: &mut MqttState) -> Result<(), StateError> {
+ let mut count = 0;
+ loop {
+ match Packet::read(&mut self.read, self.max_incoming_size) {
+ Ok(packet) => {
+ state.handle_incoming_packet(packet)?;
+
+ count += 1;
+ if count >= self.max_readb_count {
+ return Ok(());
+ }
+ }
+ // If some packets are already framed, return those
+ Err(mqttbytes::Error::InsufficientBytes(_)) if count > 0 => return Ok(()),
+ // Wait for more bytes until a frame can be created
+ Err(mqttbytes::Error::InsufficientBytes(required)) => {
+ self.read_bytes(required).await?;
+ }
+ Err(mqttbytes::Error::PayloadSizeLimitExceeded { pkt_size, max }) => {
+ state.handle_protocol_error()?;
+ return Err(StateError::IncomingPacketTooLarge { pkt_size, max });
+ }
+ Err(e) => return Err(StateError::Deserialization(e)),
+ };
+ }
+ }
+
+ pub async fn connect(&mut self, connect: Connect, options: &MqttOptions) -> io::Result<usize> {
+ let mut write = BytesMut::new();
+ let last_will = options.last_will();
+ let login = options.credentials().map(|l| Login {
+ username: l.0,
+ password: l.1,
+ });
+
+ let len = match Packet::Connect(connect, last_will, login).write(&mut write) {
+ Ok(size) => size,
+ Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e.to_string())),
+ };
+
+ self.socket.write_all(&write[..]).await?;
+ Ok(len)
+ }
+
+ pub async fn flush(&mut self, write: &mut BytesMut) -> io::Result<()> {
+ if write.is_empty() {
+ return Ok(());
+ }
+
+ self.socket.write_all(&write[..]).await?;
+ write.clear();
+ Ok(())
+ }
+}
+
+pub trait N: AsyncRead + AsyncWrite + Send + Unpin {}
+impl<T> N for T where T: AsyncRead + AsyncWrite + Send + Unpin {}
diff --git a/vendor/rumqttc/src/v5/mod.rs b/vendor/rumqttc/src/v5/mod.rs
new file mode 100644
index 0000000000..663cfd2785
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mod.rs
@@ -0,0 +1,809 @@
+use bytes::Bytes;
+use std::fmt::{self, Debug, Formatter};
+use std::time::Duration;
+#[cfg(feature = "websocket")]
+use std::{
+ future::{Future, IntoFuture},
+ pin::Pin,
+ sync::Arc,
+};
+
+mod client;
+mod eventloop;
+mod framed;
+pub mod mqttbytes;
+mod state;
+
+use crate::Outgoing;
+use crate::{NetworkOptions, Transport};
+
+use mqttbytes::v5::*;
+
+pub use client::{AsyncClient, Client, ClientError, Connection, Iter};
+pub use eventloop::{ConnectionError, Event, EventLoop};
+pub use state::{MqttState, StateError};
+
+#[cfg(feature = "use-rustls")]
+pub use crate::tls::Error as TlsError;
+
+#[cfg(feature = "proxy")]
+pub use crate::proxy::{Proxy, ProxyAuth, ProxyType};
+
+pub type Incoming = Packet;
+
+/// Requests by the client to mqtt event loop. Request are
+/// handled one by one.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub enum Request {
+ Publish(Publish),
+ PubAck(PubAck),
+ PubRec(PubRec),
+ PubComp(PubComp),
+ PubRel(PubRel),
+ PingReq,
+ PingResp,
+ Subscribe(Subscribe),
+ SubAck(SubAck),
+ Unsubscribe(Unsubscribe),
+ UnsubAck(UnsubAck),
+ Disconnect,
+}
+
+#[cfg(feature = "websocket")]
+type RequestModifierFn = Arc<
+ dyn Fn(http::Request<()>) -> Pin<Box<dyn Future<Output = http::Request<()>> + Send>>
+ + Send
+ + Sync,
+>;
+
+// TODO: Should all the options be exposed as public? Drawback
+// would be loosing the ability to panic when the user options
+// are wrong (e.g empty client id) or aggressive (keep alive time)
+/// Options to configure the behaviour of MQTT connection
+#[derive(Clone)]
+pub struct MqttOptions {
+ /// broker address that you want to connect to
+ broker_addr: String,
+ /// broker port
+ port: u16,
+ // What transport protocol to use
+ transport: Transport,
+ /// keep alive time to send pingreq to broker when the connection is idle
+ keep_alive: Duration,
+ /// clean (or) persistent session
+ clean_start: bool,
+ /// client identifier
+ client_id: String,
+ /// username and password
+ credentials: Option<(String, String)>,
+ /// request (publish, subscribe) channel capacity
+ request_channel_capacity: usize,
+ /// Max internal request batching
+ max_request_batch: usize,
+ /// Minimum delay time between consecutive outgoing packets
+ /// while retransmitting pending packets
+ pending_throttle: Duration,
+ /// Last will that will be issued on unexpected disconnect
+ last_will: Option<LastWill>,
+ /// Connection timeout
+ conn_timeout: u64,
+ /// Default value of for maximum incoming packet size.
+ /// Used when `max_incomming_size` in `connect_properties` is NOT available.
+ default_max_incoming_size: usize,
+ /// Connect Properties
+ connect_properties: Option<ConnectProperties>,
+ /// If set to `true` MQTT acknowledgements are not sent automatically.
+ /// Every incoming publish packet must be manually acknowledged with `client.ack(...)` method.
+ manual_acks: bool,
+ network_options: NetworkOptions,
+ #[cfg(feature = "proxy")]
+ /// Proxy configuration.
+ proxy: Option<Proxy>,
+ /// Upper limit on maximum number of inflight requests.
+ /// The server may set its own maximum inflight limit, the smaller of the two will be used.
+ outgoing_inflight_upper_limit: Option<u16>,
+ #[cfg(feature = "websocket")]
+ request_modifier: Option<RequestModifierFn>,
+}
+
+impl MqttOptions {
+ /// Create an [`MqttOptions`] object that contains default values for all settings other than
+ /// - id: A string to identify the device connecting to a broker
+ /// - host: The broker's domain name or IP address
+ /// - port: The port number on which broker must be listening for incoming connections
+ ///
+ /// ```
+ /// # use rumqttc::v5::MqttOptions;
+ /// let options = MqttOptions::new("123", "localhost", 1883);
+ /// ```
+ pub fn new<S: Into<String>, T: Into<String>>(id: S, host: T, port: u16) -> MqttOptions {
+ MqttOptions {
+ broker_addr: host.into(),
+ port,
+ transport: Transport::tcp(),
+ keep_alive: Duration::from_secs(60),
+ clean_start: true,
+ client_id: id.into(),
+ credentials: None,
+ request_channel_capacity: 10,
+ max_request_batch: 0,
+ pending_throttle: Duration::from_micros(0),
+ last_will: None,
+ conn_timeout: 5,
+ default_max_incoming_size: 10 * 1024,
+ connect_properties: None,
+ manual_acks: false,
+ network_options: NetworkOptions::new(),
+ #[cfg(feature = "proxy")]
+ proxy: None,
+ outgoing_inflight_upper_limit: None,
+ #[cfg(feature = "websocket")]
+ request_modifier: None,
+ }
+ }
+
+ #[cfg(feature = "url")]
+ /// Creates an [`MqttOptions`] object by parsing provided string with the [url] crate's
+ /// [`Url::parse(url)`](url::Url::parse) method and is only enabled when run using the "url" feature.
+ ///
+ /// ```
+ /// # use rumqttc::MqttOptions;
+ /// let options = MqttOptions::parse_url("mqtt://example.com:1883?client_id=123").unwrap();
+ /// ```
+ ///
+ /// **NOTE:** A url must be prefixed with one of either `tcp://`, `mqtt://`, `ssl://`,`mqtts://`,
+ /// `ws://` or `wss://` to denote the protocol for establishing a connection with the broker.
+ ///
+ /// **NOTE:** Encrypted connections(i.e. `mqtts://`, `ssl://`, `wss://`) by default use the
+ /// system's root certificates. To configure with custom certificates, one may use the
+ /// [`set_transport`](MqttOptions::set_transport) method.
+ ///
+ /// ```ignore
+ /// # use rumqttc::{MqttOptions, Transport};
+ /// # use tokio_rustls::rustls::ClientConfig;
+ /// # let root_cert_store = rustls::RootCertStore::empty();
+ /// # let client_config = ClientConfig::builder()
+ /// # .with_root_certificates(root_cert_store)
+ /// # .with_no_client_auth();
+ /// let mut options = MqttOptions::parse_url("mqtts://example.com?client_id=123").unwrap();
+ /// options.set_transport(Transport::tls_with_config(client_config.into()));
+ /// ```
+ pub fn parse_url<S: Into<String>>(url: S) -> Result<MqttOptions, OptionError> {
+ let url = url::Url::parse(&url.into())?;
+ let options = MqttOptions::try_from(url)?;
+
+ Ok(options)
+ }
+
+ /// Broker address
+ pub fn broker_address(&self) -> (String, u16) {
+ (self.broker_addr.clone(), self.port)
+ }
+
+ pub fn set_last_will(&mut self, will: LastWill) -> &mut Self {
+ self.last_will = Some(will);
+ self
+ }
+
+ pub fn last_will(&self) -> Option<LastWill> {
+ self.last_will.clone()
+ }
+
+ #[cfg(feature = "websocket")]
+ pub fn set_request_modifier<F, O>(&mut self, request_modifier: F) -> &mut Self
+ where
+ F: Fn(http::Request<()>) -> O + Send + Sync + 'static,
+ O: IntoFuture<Output = http::Request<()>> + 'static,
+ O::IntoFuture: Send,
+ {
+ self.request_modifier = Some(Arc::new(move |request| {
+ let request_modifier = request_modifier(request).into_future();
+ Box::pin(request_modifier)
+ }));
+
+ self
+ }
+
+ #[cfg(feature = "websocket")]
+ pub fn request_modifier(&self) -> Option<RequestModifierFn> {
+ self.request_modifier.clone()
+ }
+
+ pub fn set_transport(&mut self, transport: Transport) -> &mut Self {
+ self.transport = transport;
+ self
+ }
+
+ pub fn transport(&self) -> Transport {
+ self.transport.clone()
+ }
+
+ /// Set number of seconds after which client should ping the broker
+ /// if there is no other data exchange
+ pub fn set_keep_alive(&mut self, duration: Duration) -> &mut Self {
+ assert!(duration.as_secs() >= 5, "Keep alives should be >= 5 secs");
+
+ self.keep_alive = duration;
+ self
+ }
+
+ /// Keep alive time
+ pub fn keep_alive(&self) -> Duration {
+ self.keep_alive
+ }
+
+ /// Client identifier
+ pub fn client_id(&self) -> String {
+ self.client_id.clone()
+ }
+
+ /// `clean_start = true` removes all the state from queues & instructs the broker
+ /// to clean all the client state when client disconnects.
+ ///
+ /// When set `false`, broker will hold the client state and performs pending
+ /// operations on the client when reconnection with same `client_id`
+ /// happens. Local queue state is also held to retransmit packets after reconnection.
+ pub fn set_clean_start(&mut self, clean_start: bool) -> &mut Self {
+ self.clean_start = clean_start;
+ self
+ }
+
+ /// Clean session
+ pub fn clean_start(&self) -> bool {
+ self.clean_start
+ }
+
+ /// Username and password
+ pub fn set_credentials<U: Into<String>, P: Into<String>>(
+ &mut self,
+ username: U,
+ password: P,
+ ) -> &mut Self {
+ self.credentials = Some((username.into(), password.into()));
+ self
+ }
+
+ /// Security options
+ pub fn credentials(&self) -> Option<(String, String)> {
+ self.credentials.clone()
+ }
+
+ /// Set request channel capacity
+ pub fn set_request_channel_capacity(&mut self, capacity: usize) -> &mut Self {
+ self.request_channel_capacity = capacity;
+ self
+ }
+
+ /// Request channel capacity
+ pub fn request_channel_capacity(&self) -> usize {
+ self.request_channel_capacity
+ }
+
+ /// Enables throttling and sets outoing message rate to the specified 'rate'
+ pub fn set_pending_throttle(&mut self, duration: Duration) -> &mut Self {
+ self.pending_throttle = duration;
+ self
+ }
+
+ /// Outgoing message rate
+ pub fn pending_throttle(&self) -> Duration {
+ self.pending_throttle
+ }
+
+ /// set connection timeout in secs
+ pub fn set_connection_timeout(&mut self, timeout: u64) -> &mut Self {
+ self.conn_timeout = timeout;
+ self
+ }
+
+ /// get timeout in secs
+ pub fn connection_timeout(&self) -> u64 {
+ self.conn_timeout
+ }
+
+ /// set connection properties
+ pub fn set_connect_properties(&mut self, properties: ConnectProperties) -> &mut Self {
+ self.connect_properties = Some(properties);
+ self
+ }
+
+ /// get connection properties
+ pub fn connect_properties(&self) -> Option<ConnectProperties> {
+ self.connect_properties.clone()
+ }
+
+ /// set receive maximum on connection properties
+ pub fn set_receive_maximum(&mut self, recv_max: Option<u16>) -> &mut Self {
+ if let Some(conn_props) = &mut self.connect_properties {
+ conn_props.receive_maximum = recv_max;
+ self
+ } else {
+ let mut conn_props = ConnectProperties::new();
+ conn_props.receive_maximum = recv_max;
+ self.set_connect_properties(conn_props)
+ }
+ }
+
+ /// get receive maximum from connection properties
+ pub fn receive_maximum(&self) -> Option<u16> {
+ if let Some(conn_props) = &self.connect_properties {
+ conn_props.receive_maximum
+ } else {
+ None
+ }
+ }
+
+ /// set max packet size on connection properties
+ pub fn set_max_packet_size(&mut self, max_size: Option<u32>) -> &mut Self {
+ if let Some(conn_props) = &mut self.connect_properties {
+ conn_props.max_packet_size = max_size;
+ self
+ } else {
+ let mut conn_props = ConnectProperties::new();
+ conn_props.max_packet_size = max_size;
+ self.set_connect_properties(conn_props)
+ }
+ }
+
+ /// get max packet size from connection properties
+ pub fn max_packet_size(&self) -> Option<u32> {
+ if let Some(conn_props) = &self.connect_properties {
+ conn_props.max_packet_size
+ } else {
+ None
+ }
+ }
+
+ /// set max topic alias on connection properties
+ pub fn set_topic_alias_max(&mut self, topic_alias_max: Option<u16>) -> &mut Self {
+ if let Some(conn_props) = &mut self.connect_properties {
+ conn_props.topic_alias_max = topic_alias_max;
+ self
+ } else {
+ let mut conn_props = ConnectProperties::new();
+ conn_props.topic_alias_max = topic_alias_max;
+ self.set_connect_properties(conn_props)
+ }
+ }
+
+ /// get max topic alias from connection properties
+ pub fn topic_alias_max(&self) -> Option<u16> {
+ if let Some(conn_props) = &self.connect_properties {
+ conn_props.topic_alias_max
+ } else {
+ None
+ }
+ }
+
+ /// set request response info on connection properties
+ pub fn set_request_response_info(&mut self, request_response_info: Option<u8>) -> &mut Self {
+ if let Some(conn_props) = &mut self.connect_properties {
+ conn_props.request_response_info = request_response_info;
+ self
+ } else {
+ let mut conn_props = ConnectProperties::new();
+ conn_props.request_response_info = request_response_info;
+ self.set_connect_properties(conn_props)
+ }
+ }
+
+ /// get request response info from connection properties
+ pub fn request_response_info(&self) -> Option<u8> {
+ if let Some(conn_props) = &self.connect_properties {
+ conn_props.request_response_info
+ } else {
+ None
+ }
+ }
+
+ /// set request problem info on connection properties
+ pub fn set_request_problem_info(&mut self, request_problem_info: Option<u8>) -> &mut Self {
+ if let Some(conn_props) = &mut self.connect_properties {
+ conn_props.request_problem_info = request_problem_info;
+ self
+ } else {
+ let mut conn_props = ConnectProperties::new();
+ conn_props.request_problem_info = request_problem_info;
+ self.set_connect_properties(conn_props)
+ }
+ }
+
+ /// get request problem info from connection properties
+ pub fn request_problem_info(&self) -> Option<u8> {
+ if let Some(conn_props) = &self.connect_properties {
+ conn_props.request_problem_info
+ } else {
+ None
+ }
+ }
+
+ /// set user properties on connection properties
+ pub fn set_user_properties(&mut self, user_properties: Vec<(String, String)>) -> &mut Self {
+ if let Some(conn_props) = &mut self.connect_properties {
+ conn_props.user_properties = user_properties;
+ self
+ } else {
+ let mut conn_props = ConnectProperties::new();
+ conn_props.user_properties = user_properties;
+ self.set_connect_properties(conn_props)
+ }
+ }
+
+ /// get user properties from connection properties
+ pub fn user_properties(&self) -> Vec<(String, String)> {
+ if let Some(conn_props) = &self.connect_properties {
+ conn_props.user_properties.clone()
+ } else {
+ Vec::new()
+ }
+ }
+
+ /// set authentication method on connection properties
+ pub fn set_authentication_method(
+ &mut self,
+ authentication_method: Option<String>,
+ ) -> &mut Self {
+ if let Some(conn_props) = &mut self.connect_properties {
+ conn_props.authentication_method = authentication_method;
+ self
+ } else {
+ let mut conn_props = ConnectProperties::new();
+ conn_props.authentication_method = authentication_method;
+ self.set_connect_properties(conn_props)
+ }
+ }
+
+ /// get authentication method from connection properties
+ pub fn authentication_method(&self) -> Option<String> {
+ if let Some(conn_props) = &self.connect_properties {
+ conn_props.authentication_method.clone()
+ } else {
+ None
+ }
+ }
+
+ /// set authentication data on connection properties
+ pub fn set_authentication_data(&mut self, authentication_data: Option<Bytes>) -> &mut Self {
+ if let Some(conn_props) = &mut self.connect_properties {
+ conn_props.authentication_data = authentication_data;
+ self
+ } else {
+ let mut conn_props = ConnectProperties::new();
+ conn_props.authentication_data = authentication_data;
+ self.set_connect_properties(conn_props)
+ }
+ }
+
+ /// get authentication data from connection properties
+ pub fn authentication_data(&self) -> Option<Bytes> {
+ if let Some(conn_props) = &self.connect_properties {
+ conn_props.authentication_data.clone()
+ } else {
+ None
+ }
+ }
+
+ /// set manual acknowledgements
+ pub fn set_manual_acks(&mut self, manual_acks: bool) -> &mut Self {
+ self.manual_acks = manual_acks;
+ self
+ }
+
+ /// get manual acknowledgements
+ pub fn manual_acks(&self) -> bool {
+ self.manual_acks
+ }
+
+ pub fn network_options(&self) -> NetworkOptions {
+ self.network_options.clone()
+ }
+
+ pub fn set_network_options(&mut self, network_options: NetworkOptions) -> &mut Self {
+ self.network_options = network_options;
+ self
+ }
+
+ #[cfg(feature = "proxy")]
+ pub fn set_proxy(&mut self, proxy: Proxy) -> &mut Self {
+ self.proxy = Some(proxy);
+ self
+ }
+
+ #[cfg(feature = "proxy")]
+ pub fn proxy(&self) -> Option<Proxy> {
+ self.proxy.clone()
+ }
+
+ /// Get the upper limit on maximum number of inflight outgoing publishes.
+ /// The server may set its own maximum inflight limit, the smaller of the two will be used.
+ pub fn set_outgoing_inflight_upper_limit(&mut self, limit: u16) -> &mut Self {
+ self.outgoing_inflight_upper_limit = Some(limit);
+ self
+ }
+
+ /// Set the upper limit on maximum number of inflight outgoing publishes.
+ /// The server may set its own maximum inflight limit, the smaller of the two will be used.
+ pub fn get_outgoing_inflight_upper_limit(&self) -> Option<u16> {
+ self.outgoing_inflight_upper_limit
+ }
+}
+
+#[cfg(feature = "url")]
+#[derive(Debug, PartialEq, Eq, thiserror::Error)]
+pub enum OptionError {
+ #[error("Unsupported URL scheme.")]
+ Scheme,
+
+ #[error("Missing client ID.")]
+ ClientId,
+
+ #[error("Invalid keep-alive value.")]
+ KeepAlive,
+
+ #[error("Invalid clean-start value.")]
+ CleanStart,
+
+ #[error("Invalid max-incoming-packet-size value.")]
+ MaxIncomingPacketSize,
+
+ #[error("Invalid max-outgoing-packet-size value.")]
+ MaxOutgoingPacketSize,
+
+ #[error("Invalid request-channel-capacity value.")]
+ RequestChannelCapacity,
+
+ #[error("Invalid max-request-batch value.")]
+ MaxRequestBatch,
+
+ #[error("Invalid pending-throttle value.")]
+ PendingThrottle,
+
+ #[error("Invalid inflight value.")]
+ Inflight,
+
+ #[error("Invalid conn-timeout value.")]
+ ConnTimeout,
+
+ #[error("Unknown option: {0}")]
+ Unknown(String),
+
+ #[error("Couldn't parse option from url: {0}")]
+ Parse(#[from] url::ParseError),
+}
+
+#[cfg(feature = "url")]
+impl std::convert::TryFrom<url::Url> for MqttOptions {
+ type Error = OptionError;
+
+ fn try_from(url: url::Url) -> Result<Self, Self::Error> {
+ use std::collections::HashMap;
+
+ let host = url.host_str().unwrap_or_default().to_owned();
+
+ let (transport, default_port) = match url.scheme() {
+ // Encrypted connections are supported, but require explicit TLS configuration. We fall
+ // back to the unencrypted transport layer, so that `set_transport` can be used to
+ // configure the encrypted transport layer with the provided TLS configuration.
+ #[cfg(feature = "use-rustls")]
+ "mqtts" | "ssl" => (Transport::tls_with_default_config(), 8883),
+ "mqtt" | "tcp" => (Transport::Tcp, 1883),
+ #[cfg(feature = "websocket")]
+ "ws" => (Transport::Ws, 8000),
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ "wss" => (Transport::wss_with_default_config(), 8000),
+ _ => return Err(OptionError::Scheme),
+ };
+
+ let port = url.port().unwrap_or(default_port);
+
+ let mut queries = url.query_pairs().collect::<HashMap<_, _>>();
+
+ let id = queries
+ .remove("client_id")
+ .ok_or(OptionError::ClientId)?
+ .into_owned();
+
+ let mut options = MqttOptions::new(id, host, port);
+ let mut connect_props = ConnectProperties::new();
+ options.set_transport(transport);
+
+ if let Some(keep_alive) = queries
+ .remove("keep_alive_secs")
+ .map(|v| v.parse::<u64>().map_err(|_| OptionError::KeepAlive))
+ .transpose()?
+ {
+ options.set_keep_alive(Duration::from_secs(keep_alive));
+ }
+
+ if let Some(clean_start) = queries
+ .remove("clean_start")
+ .map(|v| v.parse::<bool>().map_err(|_| OptionError::CleanStart))
+ .transpose()?
+ {
+ options.set_clean_start(clean_start);
+ }
+
+ if let Some((username, password)) = {
+ match url.username() {
+ "" => None,
+ username => Some((
+ username.to_owned(),
+ url.password().unwrap_or_default().to_owned(),
+ )),
+ }
+ } {
+ options.set_credentials(username, password);
+ }
+
+ connect_props.max_packet_size = queries
+ .remove("max_incoming_packet_size_bytes")
+ .map(|v| {
+ v.parse::<u32>()
+ .map_err(|_| OptionError::MaxIncomingPacketSize)
+ })
+ .transpose()?;
+
+ if let Some(request_channel_capacity) = queries
+ .remove("request_channel_capacity_num")
+ .map(|v| {
+ v.parse::<usize>()
+ .map_err(|_| OptionError::RequestChannelCapacity)
+ })
+ .transpose()?
+ {
+ options.request_channel_capacity = request_channel_capacity;
+ }
+
+ if let Some(max_request_batch) = queries
+ .remove("max_request_batch_num")
+ .map(|v| v.parse::<usize>().map_err(|_| OptionError::MaxRequestBatch))
+ .transpose()?
+ {
+ options.max_request_batch = max_request_batch;
+ }
+
+ if let Some(pending_throttle) = queries
+ .remove("pending_throttle_usecs")
+ .map(|v| v.parse::<u64>().map_err(|_| OptionError::PendingThrottle))
+ .transpose()?
+ {
+ options.set_pending_throttle(Duration::from_micros(pending_throttle));
+ }
+
+ connect_props.receive_maximum = queries
+ .remove("inflight_num")
+ .map(|v| v.parse::<u16>().map_err(|_| OptionError::Inflight))
+ .transpose()?;
+
+ if let Some(conn_timeout) = queries
+ .remove("conn_timeout_secs")
+ .map(|v| v.parse::<u64>().map_err(|_| OptionError::ConnTimeout))
+ .transpose()?
+ {
+ options.set_connection_timeout(conn_timeout);
+ }
+
+ if let Some((opt, _)) = queries.into_iter().next() {
+ return Err(OptionError::Unknown(opt.into_owned()));
+ }
+
+ options.connect_properties = Some(connect_props);
+ Ok(options)
+ }
+}
+
+// Implement Debug manually because ClientConfig doesn't implement it, so derive(Debug) doesn't
+// work.
+impl Debug for MqttOptions {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ f.debug_struct("MqttOptions")
+ .field("broker_addr", &self.broker_addr)
+ .field("port", &self.port)
+ .field("keep_alive", &self.keep_alive)
+ .field("clean_start", &self.clean_start)
+ .field("client_id", &self.client_id)
+ .field("credentials", &self.credentials)
+ .field("request_channel_capacity", &self.request_channel_capacity)
+ .field("max_request_batch", &self.max_request_batch)
+ .field("pending_throttle", &self.pending_throttle)
+ .field("last_will", &self.last_will)
+ .field("conn_timeout", &self.conn_timeout)
+ .field("manual_acks", &self.manual_acks)
+ .field("connect properties", &self.connect_properties)
+ .finish()
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ #[cfg(all(feature = "use-rustls", feature = "websocket"))]
+ fn no_scheme() {
+ use crate::{TlsConfiguration, Transport};
+ let mut mqttoptions = MqttOptions::new("client_a", "a3f8czas.iot.eu-west-1.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=MyCreds%2F20201001%2Feu-west-1%2Fiotdevicegateway%2Faws4_request&X-Amz-Date=20201001T130812Z&X-Amz-Expires=7200&X-Amz-Signature=9ae09b49896f44270f2707551581953e6cac71a4ccf34c7c3415555be751b2d1&X-Amz-SignedHeaders=host", 443);
+
+ mqttoptions.set_transport(Transport::wss(Vec::from("Test CA"), None, None));
+
+ if let Transport::Wss(TlsConfiguration::Simple {
+ ca,
+ client_auth,
+ alpn,
+ }) = mqttoptions.transport
+ {
+ assert_eq!(ca, Vec::from("Test CA"));
+ assert_eq!(client_auth, None);
+ assert_eq!(alpn, None);
+ } else {
+ panic!("Unexpected transport!");
+ }
+
+ assert_eq!(mqttoptions.broker_addr, "a3f8czas.iot.eu-west-1.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=MyCreds%2F20201001%2Feu-west-1%2Fiotdevicegateway%2Faws4_request&X-Amz-Date=20201001T130812Z&X-Amz-Expires=7200&X-Amz-Signature=9ae09b49896f44270f2707551581953e6cac71a4ccf34c7c3415555be751b2d1&X-Amz-SignedHeaders=host");
+ }
+
+ #[test]
+ #[cfg(feature = "url")]
+ fn from_url() {
+ fn opt(s: &str) -> Result<MqttOptions, OptionError> {
+ MqttOptions::parse_url(s)
+ }
+ fn ok(s: &str) -> MqttOptions {
+ opt(s).expect("valid options")
+ }
+ fn err(s: &str) -> OptionError {
+ opt(s).expect_err("invalid options")
+ }
+
+ let v = ok("mqtt://host:42?client_id=foo");
+ assert_eq!(v.broker_address(), ("host".to_owned(), 42));
+ assert_eq!(v.client_id(), "foo".to_owned());
+
+ let v = ok("mqtt://host:42?client_id=foo&keep_alive_secs=5");
+ assert_eq!(v.keep_alive, Duration::from_secs(5));
+
+ assert_eq!(err("mqtt://host:42"), OptionError::ClientId);
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&foo=bar"),
+ OptionError::Unknown("foo".to_owned())
+ );
+ assert_eq!(err("mqt://host:42?client_id=foo"), OptionError::Scheme);
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&keep_alive_secs=foo"),
+ OptionError::KeepAlive
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&clean_start=foo"),
+ OptionError::CleanStart
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&max_incoming_packet_size_bytes=foo"),
+ OptionError::MaxIncomingPacketSize
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&request_channel_capacity_num=foo"),
+ OptionError::RequestChannelCapacity
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&max_request_batch_num=foo"),
+ OptionError::MaxRequestBatch
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&pending_throttle_usecs=foo"),
+ OptionError::PendingThrottle
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&inflight_num=foo"),
+ OptionError::Inflight
+ );
+ assert_eq!(
+ err("mqtt://host:42?client_id=foo&conn_timeout_secs=foo"),
+ OptionError::ConnTimeout
+ );
+ }
+
+ #[test]
+ fn allow_empty_client_id() {
+ let _mqtt_opts = MqttOptions::new("", "127.0.0.1", 1883).set_clean_start(true);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/mod.rs b/vendor/rumqttc/src/v5/mqttbytes/mod.rs
new file mode 100644
index 0000000000..231c680676
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/mod.rs
@@ -0,0 +1,186 @@
+use std::{str::Utf8Error, vec};
+
+/// This module is the place where all the protocol specifics gets abstracted
+/// out and creates a structures which are common across protocols. Since,
+/// MQTT is the core protocol that this broker supports, a lot of structs closely
+/// map to what MQTT specifies in its protocol
+pub mod v5;
+
+/// Quality of service
+#[repr(u8)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
+#[allow(clippy::enum_variant_names)]
+pub enum QoS {
+ AtMostOnce = 0,
+ AtLeastOnce = 1,
+ ExactlyOnce = 2,
+}
+
+impl Default for QoS {
+ fn default() -> Self {
+ Self::AtMostOnce
+ }
+}
+
+/// Maps a number to QoS
+pub fn qos(num: u8) -> Option<QoS> {
+ match num {
+ 0 => Some(QoS::AtMostOnce),
+ 1 => Some(QoS::AtLeastOnce),
+ 2 => Some(QoS::ExactlyOnce),
+ _ => None,
+ }
+}
+
+/// Checks if a topic or topic filter has wildcards
+pub fn has_wildcards(s: &str) -> bool {
+ s.contains('+') || s.contains('#')
+}
+
+/// Checks if a topic is valid
+pub fn valid_topic(topic: &str) -> bool {
+ // topic can't contain wildcards
+ if topic.contains('+') || topic.contains('#') {
+ return false;
+ }
+
+ true
+}
+
+/// Checks if the filter is valid
+///
+/// <https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718106>
+pub fn valid_filter(filter: &str) -> bool {
+ if filter.is_empty() {
+ return false;
+ }
+
+ // rev() is used so we can easily get the last entry
+ let mut hirerarchy = filter.split('/').rev();
+
+ // split will never return an empty iterator
+ // even if the pattern isn't matched, the original string will be there
+ // so it is safe to just unwrap here!
+ let last = hirerarchy.next().unwrap();
+
+ // only single '#" or '+' is allowed in last entry
+ // invalid: sport/tennis#
+ // invalid: sport/++
+ if last.len() != 1 && (last.contains('#') || last.contains('+')) {
+ return false;
+ }
+
+ // remaining entries
+ for entry in hirerarchy {
+ // # is not allowed in filter except as a last entry
+ // invalid: sport/tennis#/player
+ // invalid: sport/tennis/#/ranking
+ if entry.contains('#') {
+ return false;
+ }
+
+ // + must occupy an entire level of the filter
+ // invalid: sport+
+ if entry.len() > 1 && entry.contains('+') {
+ return false;
+ }
+ }
+
+ true
+}
+
+/// Checks if topic matches a filter. topic and filter validation isn't done here.
+///
+/// **NOTE**: 'topic' is a misnomer in the arg. this can also be used to match 2 wild subscriptions
+/// **NOTE**: make sure a topic is validated during a publish and filter is validated
+/// during a subscribe
+pub fn matches(topic: &str, filter: &str) -> bool {
+ if !topic.is_empty() && topic[..1].contains('$') {
+ return false;
+ }
+
+ let mut topics = topic.split('/');
+ let mut filters = filter.split('/');
+
+ for f in filters.by_ref() {
+ // "#" being the last element is validated by the broker with 'valid_filter'
+ if f == "#" {
+ return true;
+ }
+
+ // filter still has remaining elements
+ // filter = a/b/c/# should match topci = a/b/c
+ // filter = a/b/c/d should not match topic = a/b/c
+ let top = topics.next();
+ match top {
+ Some("#") => return false,
+ Some(_) if f == "+" => continue,
+ Some(t) if f != t => return false,
+ Some(_) => continue,
+ None => return false,
+ }
+ }
+
+ // topic has remaining elements and filter's last element isn't "#"
+ if topics.next().is_some() {
+ return false;
+ }
+
+ true
+}
+
+/// Error during serialization and deserialization
+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
+pub enum Error {
+ #[error("Invalid return code received as response for connect = {0}")]
+ InvalidConnectReturnCode(u8),
+ #[error("Invalid reason = {0}")]
+ InvalidReason(u8),
+ #[error("Invalid remaining length = {0}")]
+ InvalidRemainingLength(usize),
+ #[error("Invalid protocol used")]
+ InvalidProtocol,
+ #[error("Invalid protocol level")]
+ InvalidProtocolLevel(u8),
+ #[error("Invalid packet format")]
+ IncorrectPacketFormat,
+ #[error("Invalid packet type = {0}")]
+ InvalidPacketType(u8),
+ #[error("Invalid retain forward rule = {0}")]
+ InvalidRetainForwardRule(u8),
+ #[error("Invalid QoS level = {0}")]
+ InvalidQoS(u8),
+ #[error("Invalid subscribe reason code = {0}")]
+ InvalidSubscribeReasonCode(u8),
+ #[error("Packet received has id Zero")]
+ PacketIdZero,
+ #[error("Empty Subscription")]
+ EmptySubscription,
+ #[error("Subscription had id Zero")]
+ SubscriptionIdZero,
+ #[error("Payload size is incorrect")]
+ PayloadSizeIncorrect,
+ #[error("Payload is too long")]
+ PayloadTooLong,
+ #[error("Max Payload size of {max:?} has been exceeded by packet of {pkt_size:?} bytes")]
+ PayloadSizeLimitExceeded { pkt_size: usize, max: usize },
+ #[error("Payload is required")]
+ PayloadRequired,
+ #[error("Payload is required = {0}")]
+ PayloadNotUtf8(#[from] Utf8Error),
+ #[error("Topic not utf-8")]
+ TopicNotUtf8,
+ #[error("Promised boundary crossed, contains {0} bytes")]
+ BoundaryCrossed(usize),
+ #[error("Packet is malformed")]
+ MalformedPacket,
+ #[error("Remaining length is malformed")]
+ MalformedRemainingLength,
+ #[error("Invalid property type = {0}")]
+ InvalidPropertyType(u8),
+ /// More bytes required to frame packet. Argument
+ /// implies minimum additional bytes required to
+ /// proceed further
+ #[error("Insufficient number of bytes to frame packet, {0} more bytes required")]
+ InsufficientBytes(usize),
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/connack.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/connack.rs
new file mode 100644
index 0000000000..cd1bba502f
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/connack.rs
@@ -0,0 +1,530 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// Return code in connack
+// This contains return codes for both MQTT v311 and v5
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum ConnectReturnCode {
+ Success,
+ RefusedProtocolVersion,
+ BadClientId,
+ ServiceUnavailable,
+ UnspecifiedError,
+ MalformedPacket,
+ ProtocolError,
+ ImplementationSpecificError,
+ UnsupportedProtocolVersion,
+ ClientIdentifierNotValid,
+ BadUserNamePassword,
+ NotAuthorized,
+ ServerUnavailable,
+ ServerBusy,
+ Banned,
+ BadAuthenticationMethod,
+ TopicNameInvalid,
+ PacketTooLarge,
+ QuotaExceeded,
+ PayloadFormatInvalid,
+ RetainNotSupported,
+ QoSNotSupported,
+ UseAnotherServer,
+ ServerMoved,
+ ConnectionRateExceeded,
+}
+
+/// Acknowledgement to connect packet
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ConnAck {
+ pub session_present: bool,
+ pub code: ConnectReturnCode,
+ pub properties: Option<ConnAckProperties>,
+}
+
+impl ConnAck {
+ fn len(&self) -> usize {
+ let mut len = 1 // session present
+ + 1; // code
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ len += 1;
+ }
+
+ len
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<ConnAck, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+
+ let flags = read_u8(&mut bytes)?;
+ let return_code = read_u8(&mut bytes)?;
+ let properties = ConnAckProperties::read(&mut bytes)?;
+
+ let session_present = (flags & 0x01) == 1;
+ let code = connect_return(return_code)?;
+ let connack = ConnAck {
+ session_present,
+ code,
+ properties,
+ };
+
+ Ok(connack)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = Self::len(self);
+ buffer.put_u8(0x20);
+
+ let count = write_remaining_length(buffer, len)?;
+ buffer.put_u8(self.session_present as u8);
+ buffer.put_u8(connect_code(self.code));
+
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ Ok(1 + count + len)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ConnAckProperties {
+ pub session_expiry_interval: Option<u32>,
+ pub receive_max: Option<u16>,
+ pub max_qos: Option<u8>,
+ pub retain_available: Option<u8>,
+ pub max_packet_size: Option<u32>,
+ pub assigned_client_identifier: Option<String>,
+ pub topic_alias_max: Option<u16>,
+ pub reason_string: Option<String>,
+ pub user_properties: Vec<(String, String)>,
+ pub wildcard_subscription_available: Option<u8>,
+ pub subscription_identifiers_available: Option<u8>,
+ pub shared_subscription_available: Option<u8>,
+ pub server_keep_alive: Option<u16>,
+ pub response_information: Option<String>,
+ pub server_reference: Option<String>,
+ pub authentication_method: Option<String>,
+ pub authentication_data: Option<Bytes>,
+}
+
+impl ConnAckProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if self.session_expiry_interval.is_some() {
+ len += 1 + 4;
+ }
+
+ if self.receive_max.is_some() {
+ len += 1 + 2;
+ }
+
+ if self.max_qos.is_some() {
+ len += 1 + 1;
+ }
+
+ if self.retain_available.is_some() {
+ len += 1 + 1;
+ }
+
+ if self.max_packet_size.is_some() {
+ len += 1 + 4;
+ }
+
+ if let Some(id) = &self.assigned_client_identifier {
+ len += 1 + 2 + id.len();
+ }
+
+ if self.topic_alias_max.is_some() {
+ len += 1 + 2;
+ }
+
+ if let Some(reason) = &self.reason_string {
+ len += 1 + 2 + reason.len();
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ if self.wildcard_subscription_available.is_some() {
+ len += 1 + 1;
+ }
+
+ if self.subscription_identifiers_available.is_some() {
+ len += 1 + 1;
+ }
+
+ if self.shared_subscription_available.is_some() {
+ len += 1 + 1;
+ }
+
+ if self.server_keep_alive.is_some() {
+ len += 1 + 2;
+ }
+
+ if let Some(info) = &self.response_information {
+ len += 1 + 2 + info.len();
+ }
+
+ if let Some(reference) = &self.server_reference {
+ len += 1 + 2 + reference.len();
+ }
+
+ if let Some(authentication_method) = &self.authentication_method {
+ len += 1 + 2 + authentication_method.len();
+ }
+
+ if let Some(authentication_data) = &self.authentication_data {
+ len += 1 + 2 + authentication_data.len();
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<ConnAckProperties>, Error> {
+ let mut session_expiry_interval = None;
+ let mut receive_max = None;
+ let mut max_qos = None;
+ let mut retain_available = None;
+ let mut max_packet_size = None;
+ let mut assigned_client_identifier = None;
+ let mut topic_alias_max = None;
+ let mut reason_string = None;
+ let mut user_properties = Vec::new();
+ let mut wildcard_subscription_available = None;
+ let mut subscription_identifiers_available = None;
+ let mut shared_subscription_available = None;
+ let mut server_keep_alive = None;
+ let mut response_information = None;
+ let mut server_reference = None;
+ let mut authentication_method = None;
+ let mut authentication_data = None;
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::SessionExpiryInterval => {
+ session_expiry_interval = Some(read_u32(bytes)?);
+ cursor += 4;
+ }
+ PropertyType::ReceiveMaximum => {
+ receive_max = Some(read_u16(bytes)?);
+ cursor += 2;
+ }
+ PropertyType::MaximumQos => {
+ max_qos = Some(read_u8(bytes)?);
+ cursor += 1;
+ }
+ PropertyType::RetainAvailable => {
+ retain_available = Some(read_u8(bytes)?);
+ cursor += 1;
+ }
+ PropertyType::AssignedClientIdentifier => {
+ let id = read_mqtt_string(bytes)?;
+ cursor += 2 + id.len();
+ assigned_client_identifier = Some(id);
+ }
+ PropertyType::MaximumPacketSize => {
+ max_packet_size = Some(read_u32(bytes)?);
+ cursor += 4;
+ }
+ PropertyType::TopicAliasMaximum => {
+ topic_alias_max = Some(read_u16(bytes)?);
+ cursor += 2;
+ }
+ PropertyType::ReasonString => {
+ let reason = read_mqtt_string(bytes)?;
+ cursor += 2 + reason.len();
+ reason_string = Some(reason);
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ PropertyType::WildcardSubscriptionAvailable => {
+ wildcard_subscription_available = Some(read_u8(bytes)?);
+ cursor += 1;
+ }
+ PropertyType::SubscriptionIdentifierAvailable => {
+ subscription_identifiers_available = Some(read_u8(bytes)?);
+ cursor += 1;
+ }
+ PropertyType::SharedSubscriptionAvailable => {
+ shared_subscription_available = Some(read_u8(bytes)?);
+ cursor += 1;
+ }
+ PropertyType::ServerKeepAlive => {
+ server_keep_alive = Some(read_u16(bytes)?);
+ cursor += 2;
+ }
+ PropertyType::ResponseInformation => {
+ let info = read_mqtt_string(bytes)?;
+ cursor += 2 + info.len();
+ response_information = Some(info);
+ }
+ PropertyType::ServerReference => {
+ let reference = read_mqtt_string(bytes)?;
+ cursor += 2 + reference.len();
+ server_reference = Some(reference);
+ }
+ PropertyType::AuthenticationMethod => {
+ let method = read_mqtt_string(bytes)?;
+ cursor += 2 + method.len();
+ authentication_method = Some(method);
+ }
+ PropertyType::AuthenticationData => {
+ let data = read_mqtt_bytes(bytes)?;
+ cursor += 2 + data.len();
+ authentication_data = Some(data);
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(ConnAckProperties {
+ session_expiry_interval,
+ receive_max,
+ max_qos,
+ retain_available,
+ max_packet_size,
+ assigned_client_identifier,
+ topic_alias_max,
+ reason_string,
+ user_properties,
+ wildcard_subscription_available,
+ subscription_identifiers_available,
+ shared_subscription_available,
+ server_keep_alive,
+ response_information,
+ server_reference,
+ authentication_method,
+ authentication_data,
+ }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(session_expiry_interval) = self.session_expiry_interval {
+ buffer.put_u8(PropertyType::SessionExpiryInterval as u8);
+ buffer.put_u32(session_expiry_interval);
+ }
+
+ if let Some(receive_maximum) = self.receive_max {
+ buffer.put_u8(PropertyType::ReceiveMaximum as u8);
+ buffer.put_u16(receive_maximum);
+ }
+
+ if let Some(qos) = self.max_qos {
+ buffer.put_u8(PropertyType::MaximumQos as u8);
+ buffer.put_u8(qos);
+ }
+
+ if let Some(retain_available) = self.retain_available {
+ buffer.put_u8(PropertyType::RetainAvailable as u8);
+ buffer.put_u8(retain_available);
+ }
+
+ if let Some(max_packet_size) = self.max_packet_size {
+ buffer.put_u8(PropertyType::MaximumPacketSize as u8);
+ buffer.put_u32(max_packet_size);
+ }
+
+ if let Some(id) = &self.assigned_client_identifier {
+ buffer.put_u8(PropertyType::AssignedClientIdentifier as u8);
+ write_mqtt_string(buffer, id);
+ }
+
+ if let Some(topic_alias_max) = self.topic_alias_max {
+ buffer.put_u8(PropertyType::TopicAliasMaximum as u8);
+ buffer.put_u16(topic_alias_max);
+ }
+
+ if let Some(reason) = &self.reason_string {
+ buffer.put_u8(PropertyType::ReasonString as u8);
+ write_mqtt_string(buffer, reason);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ if let Some(w) = self.wildcard_subscription_available {
+ buffer.put_u8(PropertyType::WildcardSubscriptionAvailable as u8);
+ buffer.put_u8(w);
+ }
+
+ if let Some(s) = self.subscription_identifiers_available {
+ buffer.put_u8(PropertyType::SubscriptionIdentifierAvailable as u8);
+ buffer.put_u8(s);
+ }
+
+ if let Some(s) = self.shared_subscription_available {
+ buffer.put_u8(PropertyType::SharedSubscriptionAvailable as u8);
+ buffer.put_u8(s);
+ }
+
+ if let Some(keep_alive) = self.server_keep_alive {
+ buffer.put_u8(PropertyType::ServerKeepAlive as u8);
+ buffer.put_u16(keep_alive);
+ }
+
+ if let Some(info) = &self.response_information {
+ buffer.put_u8(PropertyType::ResponseInformation as u8);
+ write_mqtt_string(buffer, info);
+ }
+
+ if let Some(reference) = &self.server_reference {
+ buffer.put_u8(PropertyType::ServerReference as u8);
+ write_mqtt_string(buffer, reference);
+ }
+
+ if let Some(authentication_method) = &self.authentication_method {
+ buffer.put_u8(PropertyType::AuthenticationMethod as u8);
+ write_mqtt_string(buffer, authentication_method);
+ }
+
+ if let Some(authentication_data) = &self.authentication_data {
+ buffer.put_u8(PropertyType::AuthenticationData as u8);
+ write_mqtt_bytes(buffer, authentication_data);
+ }
+
+ Ok(())
+ }
+}
+
+/// Connection return code type
+fn connect_return(num: u8) -> Result<ConnectReturnCode, Error> {
+ let code = match num {
+ 0 => ConnectReturnCode::Success,
+ 128 => ConnectReturnCode::UnspecifiedError,
+ 129 => ConnectReturnCode::MalformedPacket,
+ 130 => ConnectReturnCode::ProtocolError,
+ 131 => ConnectReturnCode::ImplementationSpecificError,
+ 132 => ConnectReturnCode::UnsupportedProtocolVersion,
+ 133 => ConnectReturnCode::ClientIdentifierNotValid,
+ 134 => ConnectReturnCode::BadUserNamePassword,
+ 135 => ConnectReturnCode::NotAuthorized,
+ 136 => ConnectReturnCode::ServerUnavailable,
+ 137 => ConnectReturnCode::ServerBusy,
+ 138 => ConnectReturnCode::Banned,
+ 140 => ConnectReturnCode::BadAuthenticationMethod,
+ 144 => ConnectReturnCode::TopicNameInvalid,
+ 149 => ConnectReturnCode::PacketTooLarge,
+ 151 => ConnectReturnCode::QuotaExceeded,
+ 153 => ConnectReturnCode::PayloadFormatInvalid,
+ 154 => ConnectReturnCode::RetainNotSupported,
+ 155 => ConnectReturnCode::QoSNotSupported,
+ 156 => ConnectReturnCode::UseAnotherServer,
+ 157 => ConnectReturnCode::ServerMoved,
+ 159 => ConnectReturnCode::ConnectionRateExceeded,
+ num => return Err(Error::InvalidConnectReturnCode(num)),
+ };
+
+ Ok(code)
+}
+
+fn connect_code(return_code: ConnectReturnCode) -> u8 {
+ match return_code {
+ ConnectReturnCode::Success => 0,
+ ConnectReturnCode::UnspecifiedError => 128,
+ ConnectReturnCode::MalformedPacket => 129,
+ ConnectReturnCode::ProtocolError => 130,
+ ConnectReturnCode::ImplementationSpecificError => 131,
+ ConnectReturnCode::UnsupportedProtocolVersion => 132,
+ ConnectReturnCode::ClientIdentifierNotValid => 133,
+ ConnectReturnCode::BadUserNamePassword => 134,
+ ConnectReturnCode::NotAuthorized => 135,
+ ConnectReturnCode::ServerUnavailable => 136,
+ ConnectReturnCode::ServerBusy => 137,
+ ConnectReturnCode::Banned => 138,
+ ConnectReturnCode::BadAuthenticationMethod => 140,
+ ConnectReturnCode::TopicNameInvalid => 144,
+ ConnectReturnCode::PacketTooLarge => 149,
+ ConnectReturnCode::QuotaExceeded => 151,
+ ConnectReturnCode::PayloadFormatInvalid => 153,
+ ConnectReturnCode::RetainNotSupported => 154,
+ ConnectReturnCode::QoSNotSupported => 155,
+ ConnectReturnCode::UseAnotherServer => 156,
+ ConnectReturnCode::ServerMoved => 157,
+ ConnectReturnCode::ConnectionRateExceeded => 159,
+ _ => unreachable!(),
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let connack_props = ConnAckProperties {
+ session_expiry_interval: None,
+ receive_max: None,
+ max_qos: None,
+ retain_available: None,
+ max_packet_size: None,
+ assigned_client_identifier: None,
+ topic_alias_max: None,
+ reason_string: None,
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ wildcard_subscription_available: None,
+ subscription_identifiers_available: None,
+ shared_subscription_available: None,
+ server_keep_alive: None,
+ response_information: None,
+ server_reference: None,
+ authentication_method: None,
+ authentication_data: None,
+ };
+
+ let connack_pkt = ConnAck {
+ session_present: false,
+ code: ConnectReturnCode::Success,
+ properties: Some(connack_props),
+ };
+
+ let size_from_size = connack_pkt.size();
+ let size_from_write = connack_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/connect.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/connect.rs
new file mode 100644
index 0000000000..83918b8717
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/connect.rs
@@ -0,0 +1,687 @@
+use super::*;
+use bytes::{Buf, Bytes};
+
+/// Connection packet initiated by the client
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Connect {
+ /// Mqtt keep alive time
+ pub keep_alive: u16,
+ /// Client Id
+ pub client_id: String,
+ /// Clean session. Asks the broker to clear previous state
+ pub clean_start: bool,
+ pub properties: Option<ConnectProperties>,
+}
+
+impl Connect {
+ #[allow(clippy::type_complexity)]
+ pub fn read(
+ fixed_header: FixedHeader,
+ mut bytes: Bytes,
+ ) -> Result<(Connect, Option<LastWill>, Option<Login>), Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+
+ // Variable header
+ let protocol_name = read_mqtt_string(&mut bytes)?;
+ let protocol_level = read_u8(&mut bytes)?;
+ if protocol_name != "MQTT" {
+ return Err(Error::InvalidProtocol);
+ }
+
+ if protocol_level != 5 {
+ return Err(Error::InvalidProtocolLevel(protocol_level));
+ }
+
+ let connect_flags = read_u8(&mut bytes)?;
+ let clean_start = (connect_flags & 0b10) != 0;
+ let keep_alive = read_u16(&mut bytes)?;
+
+ let properties = ConnectProperties::read(&mut bytes)?;
+
+ let client_id = read_mqtt_string(&mut bytes)?;
+ let will = LastWill::read(connect_flags, &mut bytes)?;
+ let login = Login::read(connect_flags, &mut bytes)?;
+
+ let connect = Connect {
+ keep_alive,
+ client_id,
+ clean_start,
+ properties,
+ };
+
+ Ok((connect, will, login))
+ }
+
+ fn len(&self, will: &Option<LastWill>, l: &Option<Login>) -> usize {
+ let mut len = 2 + "MQTT".len() // protocol name
+ + 1 // protocol version
+ + 1 // connect flags
+ + 2; // keep alive
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ // just 1 byte representing 0 len
+ len += 1;
+ }
+
+ len += 2 + self.client_id.len();
+
+ // last will len
+ if let Some(w) = will {
+ len += w.len();
+ }
+
+ // username and password len
+ if let Some(l) = l {
+ len += l.len();
+ }
+
+ len
+ }
+
+ pub fn write(
+ &self,
+ will: &Option<LastWill>,
+ l: &Option<Login>,
+ buffer: &mut BytesMut,
+ ) -> Result<usize, Error> {
+ let len = self.len(will, l);
+
+ buffer.put_u8(0b0001_0000);
+ let count = write_remaining_length(buffer, len)?;
+ write_mqtt_string(buffer, "MQTT");
+
+ buffer.put_u8(0x05);
+ let flags_index = 1 + count + 2 + 4 + 1;
+
+ let mut connect_flags = 0;
+ if self.clean_start {
+ connect_flags |= 0x02;
+ }
+
+ buffer.put_u8(connect_flags);
+ buffer.put_u16(self.keep_alive);
+
+ match &self.properties {
+ Some(p) => p.write(buffer)?,
+ None => {
+ write_remaining_length(buffer, 0)?;
+ }
+ };
+
+ write_mqtt_string(buffer, &self.client_id);
+
+ if let Some(w) = will {
+ connect_flags |= w.write(buffer)?;
+ }
+
+ if let Some(l) = l {
+ connect_flags |= l.write(buffer);
+ }
+
+ // update connect flags
+ buffer[flags_index] = connect_flags;
+ Ok(1 + count + len)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ConnectProperties {
+ /// Expiry interval property after loosing connection
+ pub session_expiry_interval: Option<u32>,
+ /// Maximum simultaneous packets
+ pub receive_maximum: Option<u16>,
+ /// Maximum packet size
+ pub max_packet_size: Option<u32>,
+ /// Maximum mapping integer for a topic
+ pub topic_alias_max: Option<u16>,
+ pub request_response_info: Option<u8>,
+ pub request_problem_info: Option<u8>,
+ /// List of user properties
+ pub user_properties: Vec<(String, String)>,
+ /// Method of authentication
+ pub authentication_method: Option<String>,
+ /// Authentication data
+ pub authentication_data: Option<Bytes>,
+}
+
+impl ConnectProperties {
+ pub fn new() -> ConnectProperties {
+ ConnectProperties {
+ session_expiry_interval: None,
+ receive_maximum: None,
+ max_packet_size: None,
+ topic_alias_max: None,
+ request_response_info: None,
+ request_problem_info: None,
+ user_properties: Vec::new(),
+ authentication_method: None,
+ authentication_data: None,
+ }
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<ConnectProperties>, Error> {
+ let mut session_expiry_interval = None;
+ let mut receive_maximum = None;
+ let mut max_packet_size = None;
+ let mut topic_alias_max = None;
+ let mut request_response_info = None;
+ let mut request_problem_info = None;
+ let mut user_properties = Vec::new();
+ let mut authentication_method = None;
+ let mut authentication_data = None;
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+ match property(prop)? {
+ PropertyType::SessionExpiryInterval => {
+ session_expiry_interval = Some(read_u32(bytes)?);
+ cursor += 4;
+ }
+ PropertyType::ReceiveMaximum => {
+ receive_maximum = Some(read_u16(bytes)?);
+ cursor += 2;
+ }
+ PropertyType::MaximumPacketSize => {
+ max_packet_size = Some(read_u32(bytes)?);
+ cursor += 4;
+ }
+ PropertyType::TopicAliasMaximum => {
+ topic_alias_max = Some(read_u16(bytes)?);
+ cursor += 2;
+ }
+ PropertyType::RequestResponseInformation => {
+ request_response_info = Some(read_u8(bytes)?);
+ cursor += 1;
+ }
+ PropertyType::RequestProblemInformation => {
+ request_problem_info = Some(read_u8(bytes)?);
+ cursor += 1;
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ PropertyType::AuthenticationMethod => {
+ let method = read_mqtt_string(bytes)?;
+ cursor += 2 + method.len();
+ authentication_method = Some(method);
+ }
+ PropertyType::AuthenticationData => {
+ let data = read_mqtt_bytes(bytes)?;
+ cursor += 2 + data.len();
+ authentication_data = Some(data);
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(ConnectProperties {
+ session_expiry_interval,
+ receive_maximum,
+ max_packet_size,
+ topic_alias_max,
+ request_response_info,
+ request_problem_info,
+ user_properties,
+ authentication_method,
+ authentication_data,
+ }))
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if self.session_expiry_interval.is_some() {
+ len += 1 + 4;
+ }
+
+ if self.receive_maximum.is_some() {
+ len += 1 + 2;
+ }
+
+ if self.max_packet_size.is_some() {
+ len += 1 + 4;
+ }
+
+ if self.topic_alias_max.is_some() {
+ len += 1 + 2;
+ }
+
+ if self.request_response_info.is_some() {
+ len += 1 + 1;
+ }
+
+ if self.request_problem_info.is_some() {
+ len += 1 + 1;
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ if let Some(authentication_method) = &self.authentication_method {
+ len += 1 + 2 + authentication_method.len();
+ }
+
+ if let Some(authentication_data) = &self.authentication_data {
+ len += 1 + 2 + authentication_data.len();
+ }
+
+ len
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(session_expiry_interval) = self.session_expiry_interval {
+ buffer.put_u8(PropertyType::SessionExpiryInterval as u8);
+ buffer.put_u32(session_expiry_interval);
+ }
+
+ if let Some(receive_maximum) = self.receive_maximum {
+ buffer.put_u8(PropertyType::ReceiveMaximum as u8);
+ buffer.put_u16(receive_maximum);
+ }
+
+ if let Some(max_packet_size) = self.max_packet_size {
+ buffer.put_u8(PropertyType::MaximumPacketSize as u8);
+ buffer.put_u32(max_packet_size);
+ }
+
+ if let Some(topic_alias_max) = self.topic_alias_max {
+ buffer.put_u8(PropertyType::TopicAliasMaximum as u8);
+ buffer.put_u16(topic_alias_max);
+ }
+
+ if let Some(request_response_info) = self.request_response_info {
+ buffer.put_u8(PropertyType::RequestResponseInformation as u8);
+ buffer.put_u8(request_response_info);
+ }
+
+ if let Some(request_problem_info) = self.request_problem_info {
+ buffer.put_u8(PropertyType::RequestProblemInformation as u8);
+ buffer.put_u8(request_problem_info);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ if let Some(authentication_method) = &self.authentication_method {
+ buffer.put_u8(PropertyType::AuthenticationMethod as u8);
+ write_mqtt_string(buffer, authentication_method);
+ }
+
+ if let Some(authentication_data) = &self.authentication_data {
+ buffer.put_u8(PropertyType::AuthenticationData as u8);
+ write_mqtt_bytes(buffer, authentication_data);
+ }
+
+ Ok(())
+ }
+}
+
+impl Default for ConnectProperties {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+/// LastWill that broker forwards on behalf of the client
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct LastWill {
+ pub topic: Bytes,
+ pub message: Bytes,
+ pub qos: QoS,
+ pub retain: bool,
+ pub properties: Option<LastWillProperties>,
+}
+
+impl LastWill {
+ pub fn new(
+ topic: impl Into<String>,
+ payload: impl Into<Vec<u8>>,
+ qos: QoS,
+ retain: bool,
+ properties: Option<LastWillProperties>,
+ ) -> LastWill {
+ let topic = Bytes::copy_from_slice(topic.into().as_bytes());
+ LastWill {
+ topic,
+ message: Bytes::from(payload.into()),
+ qos,
+ retain,
+ properties,
+ }
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ // just 1 byte representing 0 len
+ len += 1;
+ }
+
+ len += 2 + self.topic.len() + 2 + self.message.len();
+ len
+ }
+
+ pub fn read(connect_flags: u8, bytes: &mut Bytes) -> Result<Option<LastWill>, Error> {
+ let o = match connect_flags & 0b100 {
+ 0 if (connect_flags & 0b0011_1000) != 0 => {
+ return Err(Error::IncorrectPacketFormat);
+ }
+ 0 => None,
+ _ => {
+ // Properties in variable header
+ let properties = LastWillProperties::read(bytes)?;
+
+ let will_topic = read_mqtt_bytes(bytes)?;
+ let will_message = read_mqtt_bytes(bytes)?;
+ let qos_num = (connect_flags & 0b11000) >> 3;
+ let will_qos = qos(qos_num).ok_or(Error::InvalidQoS(qos_num))?;
+ Some(LastWill {
+ topic: will_topic,
+ message: will_message,
+ qos: will_qos,
+ retain: (connect_flags & 0b0010_0000) != 0,
+ properties,
+ })
+ }
+ };
+
+ Ok(o)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<u8, Error> {
+ let mut connect_flags = 0;
+
+ connect_flags |= 0x04 | (self.qos as u8) << 3;
+ if self.retain {
+ connect_flags |= 0x20;
+ }
+
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ write_mqtt_bytes(buffer, &self.topic);
+ write_mqtt_bytes(buffer, &self.message);
+ Ok(connect_flags)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct LastWillProperties {
+ pub delay_interval: Option<u32>,
+ pub payload_format_indicator: Option<u8>,
+ pub message_expiry_interval: Option<u32>,
+ pub content_type: Option<String>,
+ pub response_topic: Option<String>,
+ pub correlation_data: Option<Bytes>,
+ pub user_properties: Vec<(String, String)>,
+}
+
+impl LastWillProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if self.delay_interval.is_some() {
+ len += 1 + 4;
+ }
+
+ if self.payload_format_indicator.is_some() {
+ len += 1 + 1;
+ }
+
+ if self.message_expiry_interval.is_some() {
+ len += 1 + 4;
+ }
+
+ if let Some(typ) = &self.content_type {
+ len += 1 + 2 + typ.len()
+ }
+
+ if let Some(topic) = &self.response_topic {
+ len += 1 + 2 + topic.len()
+ }
+
+ if let Some(data) = &self.correlation_data {
+ len += 1 + 2 + data.len()
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<LastWillProperties>, Error> {
+ let mut delay_interval = None;
+ let mut payload_format_indicator = None;
+ let mut message_expiry_interval = None;
+ let mut content_type = None;
+ let mut response_topic = None;
+ let mut correlation_data = None;
+ let mut user_properties = Vec::new();
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::WillDelayInterval => {
+ delay_interval = Some(read_u32(bytes)?);
+ cursor += 4;
+ }
+ PropertyType::PayloadFormatIndicator => {
+ payload_format_indicator = Some(read_u8(bytes)?);
+ cursor += 1;
+ }
+ PropertyType::MessageExpiryInterval => {
+ message_expiry_interval = Some(read_u32(bytes)?);
+ cursor += 4;
+ }
+ PropertyType::ContentType => {
+ let typ = read_mqtt_string(bytes)?;
+ cursor += 2 + typ.len();
+ content_type = Some(typ);
+ }
+ PropertyType::ResponseTopic => {
+ let topic = read_mqtt_string(bytes)?;
+ cursor += 2 + topic.len();
+ response_topic = Some(topic);
+ }
+ PropertyType::CorrelationData => {
+ let data = read_mqtt_bytes(bytes)?;
+ cursor += 2 + data.len();
+ correlation_data = Some(data);
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(LastWillProperties {
+ delay_interval,
+ payload_format_indicator,
+ message_expiry_interval,
+ content_type,
+ response_topic,
+ correlation_data,
+ user_properties,
+ }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(delay_interval) = self.delay_interval {
+ buffer.put_u8(PropertyType::WillDelayInterval as u8);
+ buffer.put_u32(delay_interval);
+ }
+
+ if let Some(payload_format_indicator) = self.payload_format_indicator {
+ buffer.put_u8(PropertyType::PayloadFormatIndicator as u8);
+ buffer.put_u8(payload_format_indicator);
+ }
+
+ if let Some(message_expiry_interval) = self.message_expiry_interval {
+ buffer.put_u8(PropertyType::MessageExpiryInterval as u8);
+ buffer.put_u32(message_expiry_interval);
+ }
+
+ if let Some(typ) = &self.content_type {
+ buffer.put_u8(PropertyType::ContentType as u8);
+ write_mqtt_string(buffer, typ);
+ }
+
+ if let Some(topic) = &self.response_topic {
+ buffer.put_u8(PropertyType::ResponseTopic as u8);
+ write_mqtt_string(buffer, topic);
+ }
+
+ if let Some(data) = &self.correlation_data {
+ buffer.put_u8(PropertyType::CorrelationData as u8);
+ write_mqtt_bytes(buffer, data);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ Ok(())
+ }
+}
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Login {
+ pub username: String,
+ pub password: String,
+}
+
+impl Login {
+ pub fn new<U: Into<String>, P: Into<String>>(u: U, p: P) -> Login {
+ Login {
+ username: u.into(),
+ password: p.into(),
+ }
+ }
+
+ pub fn read(connect_flags: u8, bytes: &mut Bytes) -> Result<Option<Login>, Error> {
+ let username = match connect_flags & 0b1000_0000 {
+ 0 => String::new(),
+ _ => read_mqtt_string(bytes)?,
+ };
+
+ let password = match connect_flags & 0b0100_0000 {
+ 0 => String::new(),
+ _ => read_mqtt_string(bytes)?,
+ };
+
+ if username.is_empty() && password.is_empty() {
+ Ok(None)
+ } else {
+ Ok(Some(Login { username, password }))
+ }
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if !self.username.is_empty() {
+ len += 2 + self.username.len();
+ }
+
+ if !self.password.is_empty() {
+ len += 2 + self.password.len();
+ }
+
+ len
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> u8 {
+ let mut connect_flags = 0;
+ if !self.username.is_empty() {
+ connect_flags |= 0x80;
+ write_mqtt_string(buffer, &self.username);
+ }
+
+ if !self.password.is_empty() {
+ connect_flags |= 0x40;
+ write_mqtt_string(buffer, &self.password);
+ }
+
+ connect_flags
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ let mut connect_props = ConnectProperties::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ connect_props.user_properties = vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())];
+ let connect_pkt = Connect {
+ keep_alive: 5,
+ client_id: "client".into(),
+ clean_start: true,
+ properties: Some(connect_props),
+ };
+
+ let reported_size = connect_pkt.write(&None, &None, &mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(reported_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/disconnect.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/disconnect.rs
new file mode 100644
index 0000000000..098260c2a6
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/disconnect.rs
@@ -0,0 +1,470 @@
+use std::convert::{TryFrom, TryInto};
+
+use bytes::{BufMut, Bytes, BytesMut};
+
+use super::*;
+
+use super::{property, PropertyType};
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[repr(u8)]
+pub enum DisconnectReasonCode {
+ /// Close the connection normally. Do not send the Will Message.
+ NormalDisconnection = 0x00,
+ /// The Client wishes to disconnect but requires that the Server also publishes its Will Message.
+ DisconnectWithWillMessage = 0x04,
+ /// The Connection is closed but the sender either does not wish to reveal the reason, or none of the other Reason Codes apply.
+ UnspecifiedError = 0x80,
+ /// The received packet does not conform to this specification.
+ MalformedPacket = 0x81,
+ /// An unexpected or out of order packet was received.
+ ProtocolError = 0x82,
+ /// The packet received is valid but cannot be processed by this implementation.
+ ImplementationSpecificError = 0x83,
+ /// The request is not authorized.
+ NotAuthorized = 0x87,
+ /// The Server is busy and cannot continue processing requests from this Client.
+ ServerBusy = 0x89,
+ /// The Server is shutting down.
+ ServerShuttingDown = 0x8B,
+ /// The Connection is closed because no packet has been received for 1.5 times the Keepalive time.
+ KeepAliveTimeout = 0x8D,
+ /// Another Connection using the same ClientID has connected causing this Connection to be closed.
+ SessionTakenOver = 0x8E,
+ /// The Topic Filter is correctly formed, but is not accepted by this Sever.
+ TopicFilterInvalid = 0x8F,
+ /// The Topic Name is correctly formed, but is not accepted by this Client or Server.
+ TopicNameInvalid = 0x90,
+ /// The Client or Server has received more than Receive Maximum publication for which it has not sent PUBACK or PUBCOMP.
+ ReceiveMaximumExceeded = 0x93,
+ /// The Client or Server has received a PUBLISH packet containing a Topic Alias which is greater than the Maximum Topic Alias it sent in the CONNECT or CONNACK packet.
+ TopicAliasInvalid = 0x94,
+ /// The packet size is greater than Maximum Packet Size for this Client or Server.
+ PacketTooLarge = 0x95,
+ /// The received data rate is too high.
+ MessageRateTooHigh = 0x96,
+ /// An implementation or administrative imposed limit has been exceeded.
+ QuotaExceeded = 0x97,
+ /// The Connection is closed due to an administrative action.
+ AdministrativeAction = 0x98,
+ /// The payload format does not match the one specified by the Payload Format Indicator.
+ PayloadFormatInvalid = 0x99,
+ /// The Server has does not support retained messages.
+ RetainNotSupported = 0x9A,
+ /// The Client specified a QoS greater than the QoS specified in a Maximum QoS in the CONNACK.
+ QoSNotSupported = 0x9B,
+ /// The Client should temporarily change its Server.
+ UseAnotherServer = 0x9C,
+ /// The Server is moved and the Client should permanently change its server location.
+ ServerMoved = 0x9D,
+ /// The Server does not support Shared Subscriptions.
+ SharedSubscriptionNotSupported = 0x9E,
+ /// This connection is closed because the connection rate is too high.
+ ConnectionRateExceeded = 0x9F,
+ /// The maximum connection time authorized for this connection has been exceeded.
+ MaximumConnectTime = 0xA0,
+ /// The Server does not support Subscription Identifiers; the subscription is not accepted.
+ SubscriptionIdentifiersNotSupported = 0xA1,
+ /// The Server does not support Wildcard subscription; the subscription is not accepted.
+ WildcardSubscriptionsNotSupported = 0xA2,
+}
+
+impl TryFrom<u8> for DisconnectReasonCode {
+ type Error = Error;
+
+ fn try_from(value: u8) -> Result<Self, Self::Error> {
+ let rc = match value {
+ 0x00 => Self::NormalDisconnection,
+ 0x04 => Self::DisconnectWithWillMessage,
+ 0x80 => Self::UnspecifiedError,
+ 0x81 => Self::MalformedPacket,
+ 0x82 => Self::ProtocolError,
+ 0x83 => Self::ImplementationSpecificError,
+ 0x87 => Self::NotAuthorized,
+ 0x89 => Self::ServerBusy,
+ 0x8B => Self::ServerShuttingDown,
+ 0x8D => Self::KeepAliveTimeout,
+ 0x8E => Self::SessionTakenOver,
+ 0x8F => Self::TopicFilterInvalid,
+ 0x90 => Self::TopicNameInvalid,
+ 0x93 => Self::ReceiveMaximumExceeded,
+ 0x94 => Self::TopicAliasInvalid,
+ 0x95 => Self::PacketTooLarge,
+ 0x96 => Self::MessageRateTooHigh,
+ 0x97 => Self::QuotaExceeded,
+ 0x98 => Self::AdministrativeAction,
+ 0x99 => Self::PayloadFormatInvalid,
+ 0x9A => Self::RetainNotSupported,
+ 0x9B => Self::QoSNotSupported,
+ 0x9C => Self::UseAnotherServer,
+ 0x9D => Self::ServerMoved,
+ 0x9E => Self::SharedSubscriptionNotSupported,
+ 0x9F => Self::ConnectionRateExceeded,
+ 0xA0 => Self::MaximumConnectTime,
+ 0xA1 => Self::SubscriptionIdentifiersNotSupported,
+ 0xA2 => Self::WildcardSubscriptionsNotSupported,
+ other => return Err(Error::InvalidConnectReturnCode(other)),
+ };
+
+ Ok(rc)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct DisconnectProperties {
+ /// Session Expiry Interval in seconds
+ pub session_expiry_interval: Option<u32>,
+
+ /// Human readable reason for the disconnect
+ pub reason_string: Option<String>,
+
+ /// List of user properties
+ pub user_properties: Vec<(String, String)>,
+
+ /// String which can be used by the Client to identify another Server to use.
+ pub server_reference: Option<String>,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Disconnect {
+ /// Disconnect Reason Code
+ pub reason_code: DisconnectReasonCode,
+
+ /// Disconnect Properties
+ pub properties: Option<DisconnectProperties>,
+}
+
+impl DisconnectProperties {
+ // pub fn new() -> Self {
+ // Self {
+ // session_expiry_interval: None,
+ // reason_string: None,
+ // user_properties: Vec::new(),
+ // server_reference: None,
+ // }
+ // }
+
+ fn len(&self) -> usize {
+ let mut length = 0;
+
+ if self.session_expiry_interval.is_some() {
+ length += 1 + 4;
+ }
+
+ if let Some(reason) = &self.reason_string {
+ length += 1 + 2 + reason.len();
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ length += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ if let Some(server_reference) = &self.server_reference {
+ length += 1 + 2 + server_reference.len();
+ }
+
+ length
+ }
+
+ pub fn extract(bytes: &mut Bytes) -> Result<Option<Self>, Error> {
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+
+ bytes.advance(properties_len_len);
+
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut session_expiry_interval = None;
+ let mut reason_string = None;
+ let mut user_properties = Vec::new();
+ let mut server_reference = None;
+
+ let mut cursor = 0;
+
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::SessionExpiryInterval => {
+ session_expiry_interval = Some(read_u32(bytes)?);
+ cursor += 4;
+ }
+ PropertyType::ReasonString => {
+ let reason = read_mqtt_string(bytes)?;
+ cursor += 2 + reason.len();
+ reason_string = Some(reason);
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ PropertyType::ServerReference => {
+ let reference = read_mqtt_string(bytes)?;
+ cursor += 2 + reference.len();
+ server_reference = Some(reference);
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ let properties = Self {
+ session_expiry_interval,
+ reason_string,
+ user_properties,
+ server_reference,
+ };
+
+ Ok(Some(properties))
+ }
+
+ fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let length = self.len();
+ write_remaining_length(buffer, length)?;
+
+ if let Some(session_expiry_interval) = self.session_expiry_interval {
+ buffer.put_u8(PropertyType::SessionExpiryInterval as u8);
+ buffer.put_u32(session_expiry_interval);
+ }
+
+ if let Some(reason) = &self.reason_string {
+ buffer.put_u8(PropertyType::ReasonString as u8);
+ write_mqtt_string(buffer, reason);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ if let Some(reference) = &self.server_reference {
+ buffer.put_u8(PropertyType::ServerReference as u8);
+ write_mqtt_string(buffer, reference);
+ }
+
+ Ok(())
+ }
+}
+
+impl Disconnect {
+ pub fn new(reason: DisconnectReasonCode) -> Self {
+ Self {
+ reason_code: reason,
+ properties: None,
+ }
+ }
+
+ fn len(&self) -> usize {
+ if self.reason_code == DisconnectReasonCode::NormalDisconnection
+ && self.properties.is_none()
+ {
+ return 2; // Packet type + 0x00
+ }
+
+ let mut length = 0;
+
+ if let Some(properties) = &self.properties {
+ length += 1; // Disconnect Reason Code
+
+ let properties_len = properties.len();
+ let properties_len_len = len_len(properties_len);
+ length += properties_len_len + properties_len;
+ } else {
+ length += 1;
+ }
+
+ length
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ if len == 2 {
+ return len;
+ }
+
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
+ let packet_type = fixed_header.byte1 >> 4;
+ let flags = fixed_header.byte1 & 0b0000_1111;
+
+ bytes.advance(fixed_header.fixed_header_len);
+
+ if packet_type != PacketType::Disconnect as u8 {
+ return Err(Error::InvalidPacketType(packet_type));
+ };
+
+ if flags != 0x00 {
+ return Err(Error::MalformedPacket);
+ };
+
+ if fixed_header.remaining_len == 0 {
+ return Ok(Self::new(DisconnectReasonCode::NormalDisconnection));
+ }
+
+ let reason_code = read_u8(&mut bytes)?;
+
+ let disconnect = Self {
+ reason_code: reason_code.try_into()?,
+ properties: DisconnectProperties::extract(&mut bytes)?,
+ };
+
+ Ok(disconnect)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ buffer.put_u8(0xE0);
+
+ let length = self.len();
+
+ if length == 2 {
+ buffer.put_u8(0x00);
+ return Ok(length);
+ }
+
+ let len_len = write_remaining_length(buffer, length)?;
+
+ buffer.put_u8(self.reason_code as u8);
+
+ if let Some(properties) = &self.properties {
+ properties.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ Ok(1 + len_len + length)
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use bytes::BytesMut;
+
+ use super::parse_fixed_header;
+
+ use super::{Disconnect, DisconnectProperties, DisconnectReasonCode};
+
+ #[test]
+ fn disconnect1_parsing_works() {
+ let mut buffer = bytes::BytesMut::new();
+ let packet_bytes = [
+ 0xE0, // Packet type
+ 0x00, // Remaining length
+ ];
+ let expected = Disconnect::new(DisconnectReasonCode::NormalDisconnection);
+
+ buffer.extend_from_slice(&packet_bytes[..]);
+
+ let fixed_header = parse_fixed_header(buffer.iter()).unwrap();
+ let disconnect_bytes = buffer.split_to(fixed_header.frame_length()).freeze();
+ let disconnect = Disconnect::read(fixed_header, disconnect_bytes).unwrap();
+
+ assert_eq!(disconnect, expected);
+ }
+
+ #[test]
+ fn disconnect1_encoding_works() {
+ let mut buffer = BytesMut::new();
+ let disconnect = Disconnect::new(DisconnectReasonCode::NormalDisconnection);
+ let expected = [
+ 0xE0, // Packet type
+ 0x00, // Remaining length
+ ];
+
+ disconnect.write(&mut buffer).unwrap();
+
+ assert_eq!(&buffer[..], &expected);
+ }
+
+ fn sample2() -> Disconnect {
+ let properties = DisconnectProperties {
+ // TODO: change to 2137 xD
+ session_expiry_interval: Some(1234),
+ reason_string: Some("test".to_owned()),
+ user_properties: vec![("test".to_owned(), "test".to_owned())],
+ server_reference: Some("test".to_owned()),
+ };
+
+ Disconnect {
+ reason_code: DisconnectReasonCode::UnspecifiedError,
+ properties: Some(properties),
+ }
+ }
+
+ fn sample_bytes2() -> Vec<u8> {
+ vec![
+ 0xE0, // Packet type
+ 0x22, // Remaining length
+ 0x80, // Disconnect Reason Code
+ 0x20, // Properties length
+ 0x11, 0x00, 0x00, 0x04, 0xd2, // Session expiry interval
+ 0x1F, 0x00, 0x04, 0x74, 0x65, 0x73, 0x74, // Reason string
+ 0x26, 0x00, 0x04, 0x74, 0x65, 0x73, 0x74, 0x00, 0x04, 0x74, 0x65, 0x73,
+ 0x74, // User properties
+ 0x1C, 0x00, 0x04, 0x74, 0x65, 0x73, 0x74, // server reference
+ ]
+ }
+
+ #[test]
+ fn disconnect2_parsing_works() {
+ let mut buffer = bytes::BytesMut::new();
+ let packet_bytes = sample_bytes2();
+ let expected = sample2();
+
+ buffer.extend_from_slice(&packet_bytes[..]);
+
+ let fixed_header = parse_fixed_header(buffer.iter()).unwrap();
+ let disconnect_bytes = buffer.split_to(fixed_header.frame_length()).freeze();
+ let disconnect = Disconnect::read(fixed_header, disconnect_bytes).unwrap();
+
+ assert_eq!(disconnect, expected);
+ }
+
+ #[test]
+ fn disconnect2_encoding_works() {
+ let mut buffer = BytesMut::new();
+
+ let disconnect = sample2();
+ let expected = sample_bytes2();
+
+ disconnect.write(&mut buffer).unwrap();
+
+ assert_eq!(&buffer[..], &expected);
+ }
+
+ // use super::*;
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ // use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let disconn_props = DisconnectProperties {
+ session_expiry_interval: None,
+ reason_string: None,
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ server_reference: None,
+ };
+
+ let mut disconn_pkt = Disconnect::new(DisconnectReasonCode::NormalDisconnection);
+ disconn_pkt.properties = Some(disconn_props);
+
+ let size_from_size = disconn_pkt.size();
+ let size_from_write = disconn_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/mod.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/mod.rs
new file mode 100644
index 0000000000..bf4dcb425a
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/mod.rs
@@ -0,0 +1,495 @@
+use std::slice::Iter;
+
+pub use self::{
+ connack::{ConnAck, ConnAckProperties, ConnectReturnCode},
+ connect::{Connect, ConnectProperties, LastWill, LastWillProperties, Login},
+ disconnect::{Disconnect, DisconnectReasonCode},
+ ping::{PingReq, PingResp},
+ puback::{PubAck, PubAckProperties, PubAckReason},
+ pubcomp::{PubComp, PubCompProperties, PubCompReason},
+ publish::{Publish, PublishProperties},
+ pubrec::{PubRec, PubRecProperties, PubRecReason},
+ pubrel::{PubRel, PubRelProperties, PubRelReason},
+ suback::{SubAck, SubAckProperties, SubscribeReasonCode},
+ subscribe::{Filter, RetainForwardRule, Subscribe, SubscribeProperties},
+ unsuback::{UnsubAck, UnsubAckProperties, UnsubAckReason},
+ unsubscribe::{Unsubscribe, UnsubscribeProperties},
+};
+
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+mod connack;
+mod connect;
+mod disconnect;
+mod ping;
+mod puback;
+mod pubcomp;
+mod publish;
+mod pubrec;
+mod pubrel;
+mod suback;
+mod subscribe;
+mod unsuback;
+mod unsubscribe;
+
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub enum Packet {
+ Connect(Connect, Option<LastWill>, Option<Login>),
+ ConnAck(ConnAck),
+ Publish(Publish),
+ PubAck(PubAck),
+ PingReq(PingReq),
+ PingResp(PingResp),
+ Subscribe(Subscribe),
+ SubAck(SubAck),
+ PubRec(PubRec),
+ PubRel(PubRel),
+ PubComp(PubComp),
+ Unsubscribe(Unsubscribe),
+ UnsubAck(UnsubAck),
+ Disconnect(Disconnect),
+}
+
+impl Packet {
+ /// Reads a stream of bytes and extracts next MQTT packet out of it
+ pub fn read(stream: &mut BytesMut, max_size: Option<usize>) -> Result<Packet, Error> {
+ let fixed_header = check(stream.iter(), max_size)?;
+
+ // Test with a stream with exactly the size to check border panics
+ let packet = stream.split_to(fixed_header.frame_length());
+ let packet_type = fixed_header.packet_type()?;
+
+ if fixed_header.remaining_len == 0 {
+ // no payload packets, Disconnect still has a bit more info
+ return match packet_type {
+ PacketType::PingReq => Ok(Packet::PingReq(PingReq)),
+ PacketType::PingResp => Ok(Packet::PingResp(PingResp)),
+ _ => Err(Error::PayloadRequired),
+ };
+ }
+
+ let packet = packet.freeze();
+ let packet = match packet_type {
+ PacketType::Connect => {
+ let (connect, will, login) = Connect::read(fixed_header, packet)?;
+ Packet::Connect(connect, will, login)
+ }
+ PacketType::Publish => {
+ let publish = Publish::read(fixed_header, packet)?;
+ Packet::Publish(publish)
+ }
+ PacketType::Subscribe => {
+ let subscribe = Subscribe::read(fixed_header, packet)?;
+ Packet::Subscribe(subscribe)
+ }
+ PacketType::Unsubscribe => {
+ let unsubscribe = Unsubscribe::read(fixed_header, packet)?;
+ Packet::Unsubscribe(unsubscribe)
+ }
+ PacketType::ConnAck => {
+ let connack = ConnAck::read(fixed_header, packet)?;
+ Packet::ConnAck(connack)
+ }
+ PacketType::PubAck => {
+ let puback = PubAck::read(fixed_header, packet)?;
+ Packet::PubAck(puback)
+ }
+ PacketType::PubRec => {
+ let pubrec = PubRec::read(fixed_header, packet)?;
+ Packet::PubRec(pubrec)
+ }
+ PacketType::PubRel => {
+ let pubrel = PubRel::read(fixed_header, packet)?;
+ Packet::PubRel(pubrel)
+ }
+ PacketType::PubComp => {
+ let pubcomp = PubComp::read(fixed_header, packet)?;
+ Packet::PubComp(pubcomp)
+ }
+ PacketType::SubAck => {
+ let suback = SubAck::read(fixed_header, packet)?;
+ Packet::SubAck(suback)
+ }
+ PacketType::UnsubAck => {
+ let unsuback = UnsubAck::read(fixed_header, packet)?;
+ Packet::UnsubAck(unsuback)
+ }
+ PacketType::PingReq => Packet::PingReq(PingReq),
+ PacketType::PingResp => Packet::PingResp(PingResp),
+ PacketType::Disconnect => {
+ let disconnect = Disconnect::read(fixed_header, packet)?;
+ Packet::Disconnect(disconnect)
+ }
+ };
+
+ Ok(packet)
+ }
+
+ pub fn write(&self, write: &mut BytesMut) -> Result<usize, Error> {
+ match self {
+ Self::Publish(publish) => publish.write(write),
+ Self::Subscribe(subscription) => subscription.write(write),
+ Self::Unsubscribe(unsubscribe) => unsubscribe.write(write),
+ Self::ConnAck(ack) => ack.write(write),
+ Self::PubAck(ack) => ack.write(write),
+ Self::SubAck(ack) => ack.write(write),
+ Self::UnsubAck(unsuback) => unsuback.write(write),
+ Self::PubRec(pubrec) => pubrec.write(write),
+ Self::PubRel(pubrel) => pubrel.write(write),
+ Self::PubComp(pubcomp) => pubcomp.write(write),
+ Self::Connect(connect, will, login) => connect.write(will, login, write),
+ Self::PingReq(_) => PingReq::write(write),
+ Self::PingResp(_) => PingResp::write(write),
+ Self::Disconnect(disconnect) => disconnect.write(write),
+ }
+ }
+}
+
+/// MQTT packet type
+#[repr(u8)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum PacketType {
+ Connect = 1,
+ ConnAck,
+ Publish,
+ PubAck,
+ PubRec,
+ PubRel,
+ PubComp,
+ Subscribe,
+ SubAck,
+ Unsubscribe,
+ UnsubAck,
+ PingReq,
+ PingResp,
+ Disconnect,
+}
+
+#[repr(u8)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum PropertyType {
+ PayloadFormatIndicator = 1,
+ MessageExpiryInterval = 2,
+ ContentType = 3,
+ ResponseTopic = 8,
+ CorrelationData = 9,
+ SubscriptionIdentifier = 11,
+ SessionExpiryInterval = 17,
+ AssignedClientIdentifier = 18,
+ ServerKeepAlive = 19,
+ AuthenticationMethod = 21,
+ AuthenticationData = 22,
+ RequestProblemInformation = 23,
+ WillDelayInterval = 24,
+ RequestResponseInformation = 25,
+ ResponseInformation = 26,
+ ServerReference = 28,
+ ReasonString = 31,
+ ReceiveMaximum = 33,
+ TopicAliasMaximum = 34,
+ TopicAlias = 35,
+ MaximumQos = 36,
+ RetainAvailable = 37,
+ UserProperty = 38,
+ MaximumPacketSize = 39,
+ WildcardSubscriptionAvailable = 40,
+ SubscriptionIdentifierAvailable = 41,
+ SharedSubscriptionAvailable = 42,
+}
+
+/// Packet type from a byte
+///
+/// ```ignore
+/// 7 3 0
+/// +--------------------------+--------------------------+
+/// byte 1 | MQTT Control Packet Type | Flags for each type |
+/// +--------------------------+--------------------------+
+/// | Remaining Bytes Len (1/2/3/4 bytes) |
+/// +-----------------------------------------------------+
+///
+/// <https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349207>
+/// ```
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
+pub struct FixedHeader {
+ /// First byte of the stream. Used to identify packet types and
+ /// several flags
+ byte1: u8,
+ /// Length of fixed header. Byte 1 + (1..4) bytes. So fixed header
+ /// len can vary from 2 bytes to 5 bytes
+ /// 1..4 bytes are variable length encoded to represent remaining length
+ fixed_header_len: usize,
+ /// Remaining length of the packet. Doesn't include fixed header bytes
+ /// Represents variable header + payload size
+ remaining_len: usize,
+}
+
+impl FixedHeader {
+ pub fn new(byte1: u8, remaining_len_len: usize, remaining_len: usize) -> FixedHeader {
+ FixedHeader {
+ byte1,
+ fixed_header_len: remaining_len_len + 1,
+ remaining_len,
+ }
+ }
+
+ pub fn packet_type(&self) -> Result<PacketType, Error> {
+ let num = self.byte1 >> 4;
+ match num {
+ 1 => Ok(PacketType::Connect),
+ 2 => Ok(PacketType::ConnAck),
+ 3 => Ok(PacketType::Publish),
+ 4 => Ok(PacketType::PubAck),
+ 5 => Ok(PacketType::PubRec),
+ 6 => Ok(PacketType::PubRel),
+ 7 => Ok(PacketType::PubComp),
+ 8 => Ok(PacketType::Subscribe),
+ 9 => Ok(PacketType::SubAck),
+ 10 => Ok(PacketType::Unsubscribe),
+ 11 => Ok(PacketType::UnsubAck),
+ 12 => Ok(PacketType::PingReq),
+ 13 => Ok(PacketType::PingResp),
+ 14 => Ok(PacketType::Disconnect),
+ _ => Err(Error::InvalidPacketType(num)),
+ }
+ }
+
+ /// Returns the size of full packet (fixed header + variable header + payload)
+ /// Fixed header is enough to get the size of a frame in the stream
+ pub fn frame_length(&self) -> usize {
+ self.fixed_header_len + self.remaining_len
+ }
+}
+
+fn property(num: u8) -> Result<PropertyType, Error> {
+ let property = match num {
+ 1 => PropertyType::PayloadFormatIndicator,
+ 2 => PropertyType::MessageExpiryInterval,
+ 3 => PropertyType::ContentType,
+ 8 => PropertyType::ResponseTopic,
+ 9 => PropertyType::CorrelationData,
+ 11 => PropertyType::SubscriptionIdentifier,
+ 17 => PropertyType::SessionExpiryInterval,
+ 18 => PropertyType::AssignedClientIdentifier,
+ 19 => PropertyType::ServerKeepAlive,
+ 21 => PropertyType::AuthenticationMethod,
+ 22 => PropertyType::AuthenticationData,
+ 23 => PropertyType::RequestProblemInformation,
+ 24 => PropertyType::WillDelayInterval,
+ 25 => PropertyType::RequestResponseInformation,
+ 26 => PropertyType::ResponseInformation,
+ 28 => PropertyType::ServerReference,
+ 31 => PropertyType::ReasonString,
+ 33 => PropertyType::ReceiveMaximum,
+ 34 => PropertyType::TopicAliasMaximum,
+ 35 => PropertyType::TopicAlias,
+ 36 => PropertyType::MaximumQos,
+ 37 => PropertyType::RetainAvailable,
+ 38 => PropertyType::UserProperty,
+ 39 => PropertyType::MaximumPacketSize,
+ 40 => PropertyType::WildcardSubscriptionAvailable,
+ 41 => PropertyType::SubscriptionIdentifierAvailable,
+ 42 => PropertyType::SharedSubscriptionAvailable,
+ num => return Err(Error::InvalidPropertyType(num)),
+ };
+
+ Ok(property)
+}
+
+/// Checks if the stream has enough bytes to frame a packet and returns fixed header
+/// only if a packet can be framed with existing bytes in the `stream`.
+/// The passed stream doesn't modify parent stream's cursor. If this function
+/// returned an error, next `check` on the same parent stream is forced start
+/// with cursor at 0 again (Iter is owned. Only Iter's cursor is changed internally)
+pub fn check(stream: Iter<u8>, max_packet_size: Option<usize>) -> Result<FixedHeader, Error> {
+ // Create fixed header if there are enough bytes in the stream
+ // to frame full packet
+ let stream_len = stream.len();
+ let fixed_header = parse_fixed_header(stream)?;
+
+ // Don't let rogue connections attack with huge payloads.
+ // Disconnect them before reading all that data
+ if let Some(max_size) = max_packet_size {
+ if fixed_header.remaining_len > max_size {
+ return Err(Error::PayloadSizeLimitExceeded {
+ pkt_size: fixed_header.remaining_len,
+ max: max_size,
+ });
+ }
+ }
+
+ // If the current call fails due to insufficient bytes in the stream,
+ // after calculating remaining length, we extend the stream
+ let frame_length = fixed_header.frame_length();
+ if stream_len < frame_length {
+ return Err(Error::InsufficientBytes(frame_length - stream_len));
+ }
+
+ Ok(fixed_header)
+}
+
+/// Parses fixed header
+fn parse_fixed_header(mut stream: Iter<u8>) -> Result<FixedHeader, Error> {
+ // At least 2 bytes are necessary to frame a packet
+ let stream_len = stream.len();
+ if stream_len < 2 {
+ return Err(Error::InsufficientBytes(2 - stream_len));
+ }
+
+ let byte1 = stream.next().unwrap();
+ let (len_len, len) = length(stream)?;
+
+ Ok(FixedHeader::new(*byte1, len_len, len))
+}
+
+/// Parses variable byte integer in the stream and returns the length
+/// and number of bytes that make it. Used for remaining length calculation
+/// as well as for calculating property lengths
+fn length(stream: Iter<u8>) -> Result<(usize, usize), Error> {
+ let mut len: usize = 0;
+ let mut len_len = 0;
+ let mut done = false;
+ let mut shift = 0;
+
+ // Use continuation bit at position 7 to continue reading next
+ // byte to frame 'length'.
+ // Stream 0b1xxx_xxxx 0b1yyy_yyyy 0b1zzz_zzzz 0b0www_wwww will
+ // be framed as number 0bwww_wwww_zzz_zzzz_yyy_yyyy_xxx_xxxx
+ for byte in stream {
+ len_len += 1;
+ let byte = *byte as usize;
+ len += (byte & 0x7F) << shift;
+
+ // stop when continue bit is 0
+ done = (byte & 0x80) == 0;
+ if done {
+ break;
+ }
+
+ shift += 7;
+
+ // Only a max of 4 bytes allowed for remaining length
+ // more than 4 shifts (0, 7, 14, 21) implies bad length
+ if shift > 21 {
+ return Err(Error::MalformedRemainingLength);
+ }
+ }
+
+ // Not enough bytes to frame remaining length. wait for
+ // one more byte
+ if !done {
+ return Err(Error::InsufficientBytes(1));
+ }
+
+ Ok((len_len, len))
+}
+
+/// Reads a series of bytes with a length from a byte stream
+fn read_mqtt_bytes(stream: &mut Bytes) -> Result<Bytes, Error> {
+ let len = read_u16(stream)? as usize;
+
+ // Prevent attacks with wrong remaining length. This method is used in
+ // `packet.assembly()` with (enough) bytes to frame packet. Ensures that
+ // reading variable len string or bytes doesn't cross promised boundary
+ // with `read_fixed_header()`
+ if len > stream.len() {
+ return Err(Error::BoundaryCrossed(len));
+ }
+
+ Ok(stream.split_to(len))
+}
+
+/// Reads a string from bytes stream
+fn read_mqtt_string(stream: &mut Bytes) -> Result<String, Error> {
+ let s = read_mqtt_bytes(stream)?;
+ match String::from_utf8(s.to_vec()) {
+ Ok(v) => Ok(v),
+ Err(_e) => Err(Error::TopicNotUtf8),
+ }
+}
+
+/// Serializes bytes to stream (including length)
+fn write_mqtt_bytes(stream: &mut BytesMut, bytes: &[u8]) {
+ stream.put_u16(bytes.len() as u16);
+ stream.extend_from_slice(bytes);
+}
+
+/// Serializes a string to stream
+fn write_mqtt_string(stream: &mut BytesMut, string: &str) {
+ write_mqtt_bytes(stream, string.as_bytes());
+}
+
+/// Writes remaining length to stream and returns number of bytes for remaining length
+fn write_remaining_length(stream: &mut BytesMut, len: usize) -> Result<usize, Error> {
+ if len > 268_435_455 {
+ return Err(Error::PayloadTooLong);
+ }
+
+ let mut done = false;
+ let mut x = len;
+ let mut count = 0;
+
+ while !done {
+ let mut byte = (x % 128) as u8;
+ x /= 128;
+ if x > 0 {
+ byte |= 128;
+ }
+
+ stream.put_u8(byte);
+ count += 1;
+ done = x == 0;
+ }
+
+ Ok(count)
+}
+
+/// Return number of remaining length bytes required for encoding length
+fn len_len(len: usize) -> usize {
+ if len >= 2_097_152 {
+ 4
+ } else if len >= 16_384 {
+ 3
+ } else if len >= 128 {
+ 2
+ } else {
+ 1
+ }
+}
+
+/// After collecting enough bytes to frame a packet (packet's frame())
+/// , It's possible that content itself in the stream is wrong. Like expected
+/// packet id or qos not being present. In cases where `read_mqtt_string` or
+/// `read_mqtt_bytes` exhausted remaining length but packet framing expects to
+/// parse qos next, these pre checks will prevent `bytes` crashes
+fn read_u16(stream: &mut Bytes) -> Result<u16, Error> {
+ if stream.len() < 2 {
+ return Err(Error::MalformedPacket);
+ }
+
+ Ok(stream.get_u16())
+}
+
+fn read_u8(stream: &mut Bytes) -> Result<u8, Error> {
+ if stream.is_empty() {
+ return Err(Error::MalformedPacket);
+ }
+
+ Ok(stream.get_u8())
+}
+
+fn read_u32(stream: &mut Bytes) -> Result<u32, Error> {
+ if stream.len() < 4 {
+ return Err(Error::MalformedPacket);
+ }
+
+ Ok(stream.get_u32())
+}
+
+mod test {
+ // These are used in tests by packets
+ #[allow(dead_code)]
+ pub const USER_PROP_KEY: &str = "property";
+ #[allow(dead_code)]
+ pub const USER_PROP_VAL: &str = "a value thats really long............................................................................................................";
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/ping.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/ping.rs
new file mode 100644
index 0000000000..086311ed9a
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/ping.rs
@@ -0,0 +1,22 @@
+use super::*;
+use bytes::{BufMut, BytesMut};
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PingReq;
+
+impl PingReq {
+ pub fn write(payload: &mut BytesMut) -> Result<usize, Error> {
+ payload.put_slice(&[0xC0, 0x00]);
+ Ok(2)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PingResp;
+
+impl PingResp {
+ pub fn write(payload: &mut BytesMut) -> Result<usize, Error> {
+ payload.put_slice(&[0xD0, 0x00]);
+ Ok(2)
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/puback.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/puback.rs
new file mode 100644
index 0000000000..9905a4500e
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/puback.rs
@@ -0,0 +1,259 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// Return code in puback
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum PubAckReason {
+ Success,
+ NoMatchingSubscribers,
+ UnspecifiedError,
+ ImplementationSpecificError,
+ NotAuthorized,
+ TopicNameInvalid,
+ PacketIdentifierInUse,
+ QuotaExceeded,
+ PayloadFormatInvalid,
+}
+
+/// Acknowledgement to QoS1 publish
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubAck {
+ pub pkid: u16,
+ pub reason: PubAckReason,
+ pub properties: Option<PubAckProperties>,
+}
+
+impl PubAck {
+ pub fn new(pkid: u16, properties: Option<PubAckProperties>) -> Self {
+ Self {
+ pkid,
+ reason: PubAckReason::Success,
+ properties,
+ }
+ }
+
+ pub fn size(&self) -> usize {
+ if self.reason == PubAckReason::Success && self.properties.is_none() {
+ return 4;
+ }
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 2 + 1; // pkid + reason
+
+ // If there are no properties, sending reason code is optional
+ if self.reason == PubAckReason::Success && self.properties.is_none() {
+ return 2;
+ }
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ // just 1 byte representing 0 len properties
+ len += 1;
+ }
+
+ len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<PubAck, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let pkid = read_u16(&mut bytes)?;
+
+ // No reason code or properties if remaining length == 2
+ if fixed_header.remaining_len == 2 {
+ return Ok(PubAck {
+ pkid,
+ reason: PubAckReason::Success,
+ properties: None,
+ });
+ }
+
+ // No properties len or properties if remaining len > 2 but < 4
+ let ack_reason = read_u8(&mut bytes)?;
+ if fixed_header.remaining_len < 4 {
+ return Ok(PubAck {
+ pkid,
+ reason: reason(ack_reason)?,
+ properties: None,
+ });
+ }
+
+ let properties = PubAckProperties::read(&mut bytes)?;
+ let puback = PubAck {
+ pkid,
+ reason: reason(ack_reason)?,
+ properties,
+ };
+
+ Ok(puback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+ buffer.put_u8(0x40);
+
+ let count = write_remaining_length(buffer, len)?;
+ buffer.put_u16(self.pkid);
+
+ // Reason code is optional with success if there are no properties
+ if self.reason == PubAckReason::Success && self.properties.is_none() {
+ return Ok(4);
+ }
+
+ buffer.put_u8(code(self.reason));
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ Ok(1 + count + len)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubAckProperties {
+ pub reason_string: Option<String>,
+ pub user_properties: Vec<(String, String)>,
+}
+
+impl PubAckProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if let Some(reason) = &self.reason_string {
+ len += 1 + 2 + reason.len();
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<PubAckProperties>, Error> {
+ let mut reason_string = None;
+ let mut user_properties = Vec::new();
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::ReasonString => {
+ let reason = read_mqtt_string(bytes)?;
+ cursor += 2 + reason.len();
+ reason_string = Some(reason);
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(PubAckProperties {
+ reason_string,
+ user_properties,
+ }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(reason) = &self.reason_string {
+ buffer.put_u8(PropertyType::ReasonString as u8);
+ write_mqtt_string(buffer, reason);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ Ok(())
+ }
+}
+
+/// Connection return code type
+fn reason(num: u8) -> Result<PubAckReason, Error> {
+ let code = match num {
+ 0 => PubAckReason::Success,
+ 16 => PubAckReason::NoMatchingSubscribers,
+ 128 => PubAckReason::UnspecifiedError,
+ 131 => PubAckReason::ImplementationSpecificError,
+ 135 => PubAckReason::NotAuthorized,
+ 144 => PubAckReason::TopicNameInvalid,
+ 145 => PubAckReason::PacketIdentifierInUse,
+ 151 => PubAckReason::QuotaExceeded,
+ 153 => PubAckReason::PayloadFormatInvalid,
+ num => return Err(Error::InvalidConnectReturnCode(num)),
+ };
+
+ Ok(code)
+}
+
+// TODO: Is typecasting significantly faster than functions?
+fn code(reason: PubAckReason) -> u8 {
+ match reason {
+ PubAckReason::Success => 0,
+ PubAckReason::NoMatchingSubscribers => 16,
+ PubAckReason::UnspecifiedError => 128,
+ PubAckReason::ImplementationSpecificError => 131,
+ PubAckReason::NotAuthorized => 135,
+ PubAckReason::TopicNameInvalid => 144,
+ PubAckReason::PacketIdentifierInUse => 145,
+ PubAckReason::QuotaExceeded => 151,
+ PubAckReason::PayloadFormatInvalid => 153,
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let puback_props = PubAckProperties {
+ reason_string: None,
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ };
+
+ let puback_pkt = PubAck::new(1, Some(puback_props));
+
+ let size_from_size = puback_pkt.size();
+ let size_from_write = puback_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/pubcomp.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/pubcomp.rs
new file mode 100644
index 0000000000..ca1877808e
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/pubcomp.rs
@@ -0,0 +1,237 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// Return code in PubComp
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[repr(u8)]
+pub enum PubCompReason {
+ Success,
+ PacketIdentifierNotFound,
+}
+
+/// QoS2 Assured publish complete, in response to PUBREL packet
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubComp {
+ pub pkid: u16,
+ pub reason: PubCompReason,
+ pub properties: Option<PubCompProperties>,
+}
+
+impl PubComp {
+ pub fn new(pkid: u16, properties: Option<PubCompProperties>) -> Self {
+ Self {
+ pkid,
+ reason: PubCompReason::Success,
+ properties,
+ }
+ }
+
+ pub fn size(&self) -> usize {
+ if self.reason == PubCompReason::Success && self.properties.is_none() {
+ return 4;
+ }
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 2 + 1; // pkid + reason
+
+ // The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success)
+ // and there are no Properties. In this case the PUBCOMP has a Remaining Length of 2.
+ // <https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901154>
+ if self.reason == PubCompReason::Success && self.properties.is_none() {
+ return 2;
+ }
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ len += 1;
+ }
+
+ len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<PubComp, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let pkid = read_u16(&mut bytes)?;
+
+ if fixed_header.remaining_len == 2 {
+ return Ok(PubComp {
+ pkid,
+ reason: PubCompReason::Success,
+ properties: None,
+ });
+ }
+
+ let ack_reason = read_u8(&mut bytes)?;
+ if fixed_header.remaining_len < 4 {
+ return Ok(PubComp {
+ pkid,
+ reason: reason(ack_reason)?,
+ properties: None,
+ });
+ }
+
+ let properties = PubCompProperties::read(&mut bytes)?;
+ let puback = PubComp {
+ pkid,
+ reason: reason(ack_reason)?,
+ properties,
+ };
+
+ Ok(puback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+ buffer.put_u8(0x70);
+ let count = write_remaining_length(buffer, len)?;
+ buffer.put_u16(self.pkid);
+
+ // If there are no properties during success, sending reason code is optional
+ if self.reason == PubCompReason::Success && self.properties.is_none() {
+ return Ok(4);
+ }
+
+ buffer.put_u8(code(self.reason));
+
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ Ok(1 + count + len)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubCompProperties {
+ pub reason_string: Option<String>,
+ pub user_properties: Vec<(String, String)>,
+}
+
+impl PubCompProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if let Some(reason) = &self.reason_string {
+ len += 1 + 2 + reason.len();
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<PubCompProperties>, Error> {
+ let mut reason_string = None;
+ let mut user_properties = Vec::new();
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::ReasonString => {
+ let reason = read_mqtt_string(bytes)?;
+ cursor += 2 + reason.len();
+ reason_string = Some(reason);
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(PubCompProperties {
+ reason_string,
+ user_properties,
+ }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(reason) = &self.reason_string {
+ buffer.put_u8(PropertyType::ReasonString as u8);
+ write_mqtt_string(buffer, reason);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ Ok(())
+ }
+}
+
+/// Connection return code type
+fn reason(num: u8) -> Result<PubCompReason, Error> {
+ let code = match num {
+ 0 => PubCompReason::Success,
+ 146 => PubCompReason::PacketIdentifierNotFound,
+ num => return Err(Error::InvalidConnectReturnCode(num)),
+ };
+
+ Ok(code)
+}
+
+fn code(reason: PubCompReason) -> u8 {
+ match reason {
+ PubCompReason::Success => 0,
+ PubCompReason::PacketIdentifierNotFound => 146,
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let pubcomp_props = PubCompProperties {
+ reason_string: None,
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ };
+
+ let pubcomp_pkt = PubComp::new(1, Some(pubcomp_props));
+
+ let size_from_size = pubcomp_pkt.size();
+ let size_from_write = pubcomp_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/publish.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/publish.rs
new file mode 100644
index 0000000000..74fbee2255
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/publish.rs
@@ -0,0 +1,340 @@
+use super::*;
+use bytes::{Buf, Bytes};
+
+/// Publish packet
+#[derive(Clone, Debug, PartialEq, Eq, Default)]
+pub struct Publish {
+ pub dup: bool,
+ pub qos: QoS,
+ pub retain: bool,
+ pub topic: Bytes,
+ pub pkid: u16,
+ pub payload: Bytes,
+ pub properties: Option<PublishProperties>,
+}
+
+impl Publish {
+ pub fn new<T: Into<String>, P: Into<Bytes>>(
+ topic: T,
+ qos: QoS,
+ payload: P,
+ properties: Option<PublishProperties>,
+ ) -> Self {
+ let topic = Bytes::copy_from_slice(topic.into().as_bytes());
+ Self {
+ qos,
+ topic,
+ payload: payload.into(),
+ properties,
+ ..Default::default()
+ }
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 2 + self.topic.len();
+ if self.qos != QoS::AtMostOnce && self.pkid != 0 {
+ len += 2;
+ }
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ // just 1 byte representing 0 len
+ len += 1;
+ }
+
+ len += self.payload.len();
+ len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Publish, Error> {
+ let qos_num = (fixed_header.byte1 & 0b0110) >> 1;
+ let qos = qos(qos_num).ok_or(Error::InvalidQoS(qos_num))?;
+ let dup = (fixed_header.byte1 & 0b1000) != 0;
+ let retain = (fixed_header.byte1 & 0b0001) != 0;
+
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let topic = read_mqtt_bytes(&mut bytes)?;
+
+ // Packet identifier exists where QoS > 0
+ let pkid = match qos {
+ QoS::AtMostOnce => 0,
+ QoS::AtLeastOnce | QoS::ExactlyOnce => read_u16(&mut bytes)?,
+ };
+
+ if qos != QoS::AtMostOnce && pkid == 0 {
+ return Err(Error::PacketIdZero);
+ }
+
+ let properties = PublishProperties::read(&mut bytes)?;
+ let publish = Publish {
+ dup,
+ retain,
+ qos,
+ pkid,
+ topic,
+ payload: bytes,
+ properties,
+ };
+
+ Ok(publish)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+
+ let dup = self.dup as u8;
+ let qos = self.qos as u8;
+ let retain = self.retain as u8;
+ buffer.put_u8(0b0011_0000 | retain | qos << 1 | dup << 3);
+
+ let count = write_remaining_length(buffer, len)?;
+ write_mqtt_bytes(buffer, &self.topic);
+
+ if self.qos != QoS::AtMostOnce {
+ let pkid = self.pkid;
+ if pkid == 0 {
+ return Err(Error::PacketIdZero);
+ }
+
+ buffer.put_u16(pkid);
+ }
+
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ buffer.extend_from_slice(&self.payload);
+
+ Ok(1 + count + len)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Default)]
+pub struct PublishProperties {
+ pub payload_format_indicator: Option<u8>,
+ pub message_expiry_interval: Option<u32>,
+ pub topic_alias: Option<u16>,
+ pub response_topic: Option<String>,
+ pub correlation_data: Option<Bytes>,
+ pub user_properties: Vec<(String, String)>,
+ pub subscription_identifiers: Vec<usize>,
+ pub content_type: Option<String>,
+}
+
+impl PublishProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if self.payload_format_indicator.is_some() {
+ len += 1 + 1;
+ }
+
+ if self.message_expiry_interval.is_some() {
+ len += 1 + 4;
+ }
+
+ if self.topic_alias.is_some() {
+ len += 1 + 2;
+ }
+
+ if let Some(topic) = &self.response_topic {
+ len += 1 + 2 + topic.len()
+ }
+
+ if let Some(data) = &self.correlation_data {
+ len += 1 + 2 + data.len()
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ for id in self.subscription_identifiers.iter() {
+ len += 1 + len_len(*id);
+ }
+
+ if let Some(typ) = &self.content_type {
+ len += 1 + 2 + typ.len()
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<PublishProperties>, Error> {
+ let mut payload_format_indicator = None;
+ let mut message_expiry_interval = None;
+ let mut topic_alias = None;
+ let mut response_topic = None;
+ let mut correlation_data = None;
+ let mut user_properties = Vec::new();
+ let mut subscription_identifiers = Vec::new();
+ let mut content_type = None;
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::PayloadFormatIndicator => {
+ payload_format_indicator = Some(read_u8(bytes)?);
+ cursor += 1;
+ }
+ PropertyType::MessageExpiryInterval => {
+ message_expiry_interval = Some(read_u32(bytes)?);
+ cursor += 4;
+ }
+ PropertyType::TopicAlias => {
+ topic_alias = Some(read_u16(bytes)?);
+ cursor += 2;
+ }
+ PropertyType::ResponseTopic => {
+ let topic = read_mqtt_string(bytes)?;
+ cursor += 2 + topic.len();
+ response_topic = Some(topic);
+ }
+ PropertyType::CorrelationData => {
+ let data = read_mqtt_bytes(bytes)?;
+ cursor += 2 + data.len();
+ correlation_data = Some(data);
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ PropertyType::SubscriptionIdentifier => {
+ let (id_len, id) = length(bytes.iter())?;
+ cursor += 1 + id_len;
+ bytes.advance(id_len);
+ subscription_identifiers.push(id);
+ }
+ PropertyType::ContentType => {
+ let typ = read_mqtt_string(bytes)?;
+ cursor += 2 + typ.len();
+ content_type = Some(typ);
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(PublishProperties {
+ payload_format_indicator,
+ message_expiry_interval,
+ topic_alias,
+ response_topic,
+ correlation_data,
+ user_properties,
+ subscription_identifiers,
+ content_type,
+ }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(payload_format_indicator) = self.payload_format_indicator {
+ buffer.put_u8(PropertyType::PayloadFormatIndicator as u8);
+ buffer.put_u8(payload_format_indicator);
+ }
+
+ if let Some(message_expiry_interval) = self.message_expiry_interval {
+ buffer.put_u8(PropertyType::MessageExpiryInterval as u8);
+ buffer.put_u32(message_expiry_interval);
+ }
+
+ if let Some(topic_alias) = self.topic_alias {
+ buffer.put_u8(PropertyType::TopicAlias as u8);
+ buffer.put_u16(topic_alias);
+ }
+
+ if let Some(topic) = &self.response_topic {
+ buffer.put_u8(PropertyType::ResponseTopic as u8);
+ write_mqtt_string(buffer, topic);
+ }
+
+ if let Some(data) = &self.correlation_data {
+ buffer.put_u8(PropertyType::CorrelationData as u8);
+ write_mqtt_bytes(buffer, data);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ for id in self.subscription_identifiers.iter() {
+ buffer.put_u8(PropertyType::SubscriptionIdentifier as u8);
+ write_remaining_length(buffer, *id)?;
+ }
+
+ if let Some(typ) = &self.content_type {
+ buffer.put_u8(PropertyType::ContentType as u8);
+ write_mqtt_string(buffer, typ);
+ }
+
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let publish_props = PublishProperties {
+ payload_format_indicator: None,
+ message_expiry_interval: None,
+ topic_alias: None,
+ response_topic: None,
+ correlation_data: None,
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ subscription_identifiers: vec![1],
+ content_type: None,
+ };
+
+ let publish_pkt = Publish::new(
+ "hello/world",
+ QoS::AtMostOnce,
+ vec![1; 10],
+ Some(publish_props),
+ );
+
+ let size_from_size = publish_pkt.size();
+ let size_from_write = publish_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/pubrec.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/pubrec.rs
new file mode 100644
index 0000000000..6b3aad36dd
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/pubrec.rs
@@ -0,0 +1,253 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// Return code in PubRec
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[repr(u8)]
+pub enum PubRecReason {
+ Success,
+ NoMatchingSubscribers,
+ UnspecifiedError,
+ ImplementationSpecificError,
+ NotAuthorized,
+ TopicNameInvalid,
+ PacketIdentifierInUse,
+ QuotaExceeded,
+ PayloadFormatInvalid,
+}
+
+/// Acknowledgement to QoS1 publish
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubRec {
+ pub pkid: u16,
+ pub reason: PubRecReason,
+ pub properties: Option<PubRecProperties>,
+}
+
+impl PubRec {
+ pub fn new(pkid: u16, properties: Option<PubRecProperties>) -> Self {
+ Self {
+ pkid,
+ reason: PubRecReason::Success,
+ properties,
+ }
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 2 + 1; // pkid + reason
+
+ // The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success)
+ // and there are no Properties. In this case the PUBREC has a Remaining Length of 2.
+ // <https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901134>
+ if self.reason == PubRecReason::Success && self.properties.is_none() {
+ return 2;
+ }
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ len += 1
+ }
+
+ len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<PubRec, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let pkid = read_u16(&mut bytes)?;
+ if fixed_header.remaining_len == 2 {
+ return Ok(PubRec {
+ pkid,
+ reason: PubRecReason::Success,
+ properties: None,
+ });
+ }
+
+ let ack_reason = read_u8(&mut bytes)?;
+ if fixed_header.remaining_len < 4 {
+ return Ok(PubRec {
+ pkid,
+ reason: reason(ack_reason)?,
+ properties: None,
+ });
+ }
+
+ let properties = PubRecProperties::read(&mut bytes)?;
+ let puback = PubRec {
+ pkid,
+ reason: reason(ack_reason)?,
+ properties,
+ };
+ Ok(puback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+ buffer.put_u8(0x50);
+ let count = write_remaining_length(buffer, len)?;
+ buffer.put_u16(self.pkid);
+
+ // If there are no properties during success, sending reason code is optional
+ if self.reason == PubRecReason::Success && self.properties.is_none() {
+ return Ok(4);
+ }
+
+ buffer.put_u8(code(self.reason));
+
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ Ok(1 + count + len)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubRecProperties {
+ pub reason_string: Option<String>,
+ pub user_properties: Vec<(String, String)>,
+}
+
+impl PubRecProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if let Some(reason) = &self.reason_string {
+ len += 1 + 2 + reason.len();
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<PubRecProperties>, Error> {
+ let mut reason_string = None;
+ let mut user_properties = Vec::new();
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::ReasonString => {
+ let reason = read_mqtt_string(bytes)?;
+ cursor += 2 + reason.len();
+ reason_string = Some(reason);
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(PubRecProperties {
+ reason_string,
+ user_properties,
+ }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(reason) = &self.reason_string {
+ buffer.put_u8(PropertyType::ReasonString as u8);
+ write_mqtt_string(buffer, reason);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ Ok(())
+ }
+}
+
+/// Connection return code type
+fn reason(num: u8) -> Result<PubRecReason, Error> {
+ let code = match num {
+ 0 => PubRecReason::Success,
+ 16 => PubRecReason::NoMatchingSubscribers,
+ 128 => PubRecReason::UnspecifiedError,
+ 131 => PubRecReason::ImplementationSpecificError,
+ 135 => PubRecReason::NotAuthorized,
+ 144 => PubRecReason::TopicNameInvalid,
+ 145 => PubRecReason::PacketIdentifierInUse,
+ 151 => PubRecReason::QuotaExceeded,
+ 153 => PubRecReason::PayloadFormatInvalid,
+ num => return Err(Error::InvalidConnectReturnCode(num)),
+ };
+
+ Ok(code)
+}
+
+fn code(reason: PubRecReason) -> u8 {
+ match reason {
+ PubRecReason::Success => 0,
+ PubRecReason::NoMatchingSubscribers => 16,
+ PubRecReason::UnspecifiedError => 128,
+ PubRecReason::ImplementationSpecificError => 131,
+ PubRecReason::NotAuthorized => 135,
+ PubRecReason::TopicNameInvalid => 144,
+ PubRecReason::PacketIdentifierInUse => 145,
+ PubRecReason::QuotaExceeded => 151,
+ PubRecReason::PayloadFormatInvalid => 153,
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let pubrec_props = PubRecProperties {
+ reason_string: None,
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ };
+
+ let pubrec_pkt = PubRec::new(1, Some(pubrec_props));
+
+ let size_from_size = pubrec_pkt.size();
+ let size_from_write = pubrec_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/pubrel.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/pubrel.rs
new file mode 100644
index 0000000000..262b52203c
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/pubrel.rs
@@ -0,0 +1,238 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// Return code in PubRel
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[repr(u8)]
+pub enum PubRelReason {
+ Success,
+ PacketIdentifierNotFound,
+}
+
+/// QoS2 Publish release, in response to PUBREC packet
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubRel {
+ pub pkid: u16,
+ pub reason: PubRelReason,
+ pub properties: Option<PubRelProperties>,
+}
+
+impl PubRel {
+ pub fn new(pkid: u16, properties: Option<PubRelProperties>) -> Self {
+ Self {
+ pkid,
+ reason: PubRelReason::Success,
+ properties,
+ }
+ }
+
+ pub fn size(&self) -> usize {
+ // If there are no properties during success, sending reason code is optional
+ if self.reason == PubRelReason::Success && self.properties.is_none() {
+ return 4;
+ }
+
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 2 + 1; // pkid + reason
+
+ // The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success)
+ // and there are no Properties. In this case the PUBREL has a Remaining Length of 2.
+ // <https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144>
+ if self.reason == PubRelReason::Success && self.properties.is_none() {
+ return 2;
+ }
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ len += 1;
+ }
+
+ len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<PubRel, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+ let pkid = read_u16(&mut bytes)?;
+ if fixed_header.remaining_len == 2 {
+ return Ok(PubRel {
+ pkid,
+ reason: PubRelReason::Success,
+ properties: None,
+ });
+ }
+
+ let ack_reason = read_u8(&mut bytes)?;
+ if fixed_header.remaining_len < 4 {
+ return Ok(PubRel {
+ pkid,
+ reason: reason(ack_reason)?,
+ properties: None,
+ });
+ }
+
+ let properties = PubRelProperties::read(&mut bytes)?;
+ let puback = PubRel {
+ pkid,
+ reason: reason(ack_reason)?,
+ properties,
+ };
+
+ Ok(puback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ let len = self.len();
+ buffer.put_u8(0x62);
+ let count = write_remaining_length(buffer, len)?;
+ buffer.put_u16(self.pkid);
+
+ // If there are no properties during success, sending reason code is optional
+ if self.reason == PubRelReason::Success && self.properties.is_none() {
+ return Ok(4);
+ }
+
+ buffer.put_u8(code(self.reason));
+
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ Ok(1 + count + len)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PubRelProperties {
+ pub reason_string: Option<String>,
+ pub user_properties: Vec<(String, String)>,
+}
+
+impl PubRelProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if let Some(reason) = &self.reason_string {
+ len += 1 + 2 + reason.len();
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<PubRelProperties>, Error> {
+ let mut reason_string = None;
+ let mut user_properties = Vec::new();
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::ReasonString => {
+ let reason = read_mqtt_string(bytes)?;
+ cursor += 2 + reason.len();
+ reason_string = Some(reason);
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(PubRelProperties {
+ reason_string,
+ user_properties,
+ }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(reason) = &self.reason_string {
+ buffer.put_u8(PropertyType::ReasonString as u8);
+ write_mqtt_string(buffer, reason);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ Ok(())
+ }
+}
+
+/// Connection return code type
+fn reason(num: u8) -> Result<PubRelReason, Error> {
+ let code = match num {
+ 0 => PubRelReason::Success,
+ 146 => PubRelReason::PacketIdentifierNotFound,
+ num => return Err(Error::InvalidConnectReturnCode(num)),
+ };
+
+ Ok(code)
+}
+
+fn code(reason: PubRelReason) -> u8 {
+ match reason {
+ PubRelReason::Success => 0,
+ PubRelReason::PacketIdentifierNotFound => 146,
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let pubrel_props = PubRelProperties {
+ reason_string: None,
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ };
+
+ let pubrel_pkt = PubRel::new(1, Some(pubrel_props));
+
+ let size_from_size = pubrel_pkt.size();
+ let size_from_write = pubrel_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/suback.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/suback.rs
new file mode 100644
index 0000000000..bd41d91454
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/suback.rs
@@ -0,0 +1,240 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// Acknowledgement to subscribe
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SubAck {
+ pub pkid: u16,
+ pub return_codes: Vec<SubscribeReasonCode>,
+ pub properties: Option<SubAckProperties>,
+}
+
+impl SubAck {
+ fn len(&self) -> usize {
+ let mut len = 2 + self.return_codes.len();
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ // just 1 byte representing 0 len
+ len += 1;
+ }
+
+ len
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<SubAck, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+
+ let pkid = read_u16(&mut bytes)?;
+ let properties = SubAckProperties::read(&mut bytes)?;
+
+ if !bytes.has_remaining() {
+ return Err(Error::MalformedPacket);
+ }
+
+ let mut return_codes = Vec::new();
+ while bytes.has_remaining() {
+ let return_code = read_u8(&mut bytes)?;
+ return_codes.push(reason(return_code)?);
+ }
+
+ let suback = SubAck {
+ pkid,
+ return_codes,
+ properties,
+ };
+
+ Ok(suback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ buffer.put_u8(0x90);
+ let remaining_len = self.len();
+ let remaining_len_bytes = write_remaining_length(buffer, remaining_len)?;
+
+ buffer.put_u16(self.pkid);
+
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ let p: Vec<u8> = self.return_codes.iter().map(|&c| code(c)).collect();
+
+ buffer.extend_from_slice(&p);
+ Ok(1 + remaining_len_bytes + remaining_len)
+ }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum SubscribeReasonCode {
+ Success(QoS),
+ Failure,
+ Unspecified,
+ ImplementationSpecific,
+ NotAuthorized,
+ TopicFilterInvalid,
+ PkidInUse,
+ QuotaExceeded,
+ SharedSubscriptionsNotSupported,
+ SubscriptionIdNotSupported,
+ WildcardSubscriptionsNotSupported,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SubAckProperties {
+ pub reason_string: Option<String>,
+ pub user_properties: Vec<(String, String)>,
+}
+
+impl SubAckProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if let Some(reason) = &self.reason_string {
+ len += 1 + 2 + reason.len();
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<SubAckProperties>, Error> {
+ let mut reason_string = None;
+ let mut user_properties = Vec::new();
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::ReasonString => {
+ let reason = read_mqtt_string(bytes)?;
+ cursor += 2 + reason.len();
+ reason_string = Some(reason);
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(SubAckProperties {
+ reason_string,
+ user_properties,
+ }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(reason) = &self.reason_string {
+ buffer.put_u8(PropertyType::ReasonString as u8);
+ write_mqtt_string(buffer, reason);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ Ok(())
+ }
+}
+
+fn reason(code: u8) -> Result<SubscribeReasonCode, Error> {
+ let v = match code {
+ 0 => SubscribeReasonCode::Success(QoS::AtMostOnce),
+ 1 => SubscribeReasonCode::Success(QoS::AtLeastOnce),
+ 2 => SubscribeReasonCode::Success(QoS::ExactlyOnce),
+ 128 => SubscribeReasonCode::Unspecified,
+ 131 => SubscribeReasonCode::ImplementationSpecific,
+ 135 => SubscribeReasonCode::NotAuthorized,
+ 143 => SubscribeReasonCode::TopicFilterInvalid,
+ 145 => SubscribeReasonCode::PkidInUse,
+ 151 => SubscribeReasonCode::QuotaExceeded,
+ 158 => SubscribeReasonCode::SharedSubscriptionsNotSupported,
+ 161 => SubscribeReasonCode::SubscriptionIdNotSupported,
+ 162 => SubscribeReasonCode::WildcardSubscriptionsNotSupported,
+ v => return Err(Error::InvalidSubscribeReasonCode(v)),
+ };
+
+ Ok(v)
+}
+
+fn code(value: SubscribeReasonCode) -> u8 {
+ match value {
+ SubscribeReasonCode::Success(qos) => qos as u8,
+ SubscribeReasonCode::Failure => 0x80,
+ SubscribeReasonCode::Unspecified => 128,
+ SubscribeReasonCode::ImplementationSpecific => 131,
+ SubscribeReasonCode::NotAuthorized => 135,
+ SubscribeReasonCode::TopicFilterInvalid => 143,
+ SubscribeReasonCode::PkidInUse => 145,
+ SubscribeReasonCode::QuotaExceeded => 151,
+ SubscribeReasonCode::SharedSubscriptionsNotSupported => 158,
+ SubscribeReasonCode::SubscriptionIdNotSupported => 161,
+ SubscribeReasonCode::WildcardSubscriptionsNotSupported => 162,
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let suback_props = SubAckProperties {
+ reason_string: None,
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ };
+
+ let suback_pkt = SubAck {
+ pkid: 1,
+ return_codes: vec![SubscribeReasonCode::Success(QoS::ExactlyOnce)],
+ properties: Some(suback_props),
+ };
+
+ let size_from_size = suback_pkt.size();
+ let size_from_write = suback_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/subscribe.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/subscribe.rs
new file mode 100644
index 0000000000..4167cd6718
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/subscribe.rs
@@ -0,0 +1,305 @@
+use super::*;
+use bytes::{Buf, Bytes};
+
+/// Subscription packet
+#[derive(Clone, Debug, PartialEq, Eq, Default)]
+pub struct Subscribe {
+ pub pkid: u16,
+ pub filters: Vec<Filter>,
+ pub properties: Option<SubscribeProperties>,
+}
+
+impl Subscribe {
+ pub fn new(filter: Filter, properties: Option<SubscribeProperties>) -> Self {
+ Self {
+ filters: vec![filter],
+ properties,
+ ..Default::default()
+ }
+ }
+
+ pub fn new_many<F>(filters: F, properties: Option<SubscribeProperties>) -> Self
+ where
+ F: IntoIterator<Item = Filter>,
+ {
+ Self {
+ filters: filters.into_iter().collect(),
+ properties,
+ ..Default::default()
+ }
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ fn len(&self) -> usize {
+ let mut len = 2 + self.filters.iter().fold(0, |s, t| s + t.len());
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ // just 1 byte representing 0 len
+ len += 1;
+ }
+
+ len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Subscribe, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+
+ let pkid = read_u16(&mut bytes)?;
+ let properties = SubscribeProperties::read(&mut bytes)?;
+
+ // variable header size = 2 (packet identifier)
+ let filters = Filter::read(&mut bytes)?;
+
+ match filters.len() {
+ 0 => Err(Error::EmptySubscription),
+ _ => Ok(Subscribe {
+ pkid,
+ filters,
+ properties,
+ }),
+ }
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ // write packet type
+ buffer.put_u8(0x82);
+
+ // write remaining length
+ let remaining_len = self.len();
+ let remaining_len_bytes = write_remaining_length(buffer, remaining_len)?;
+
+ // write packet id
+ buffer.put_u16(self.pkid);
+
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ // write filters
+ for f in self.filters.iter() {
+ f.write(buffer);
+ }
+
+ Ok(1 + remaining_len_bytes + remaining_len)
+ }
+}
+
+/// Subscription filter
+#[derive(Clone, Debug, PartialEq, Eq, Default)]
+pub struct Filter {
+ pub path: String,
+ pub qos: QoS,
+ pub nolocal: bool,
+ pub preserve_retain: bool,
+ pub retain_forward_rule: RetainForwardRule,
+}
+
+impl Filter {
+ pub fn new<T: Into<String>>(topic: T, qos: QoS) -> Self {
+ Self {
+ path: topic.into(),
+ qos,
+ ..Default::default()
+ }
+ }
+
+ fn len(&self) -> usize {
+ // filter len + filter + options
+ 2 + self.path.len() + 1
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Vec<Filter>, Error> {
+ // variable header size = 2 (packet identifier)
+ let mut filters = Vec::new();
+
+ while bytes.has_remaining() {
+ let path = read_mqtt_string(bytes)?;
+ let options = read_u8(bytes)?;
+ let requested_qos = options & 0b0000_0011;
+
+ let nolocal = options >> 2 & 0b0000_0001;
+ let nolocal = nolocal != 0;
+
+ let preserve_retain = options >> 3 & 0b0000_0001;
+ let preserve_retain = preserve_retain != 0;
+
+ let retain_forward_rule = (options >> 4) & 0b0000_0011;
+ let retain_forward_rule = match retain_forward_rule {
+ 0 => RetainForwardRule::OnEverySubscribe,
+ 1 => RetainForwardRule::OnNewSubscribe,
+ 2 => RetainForwardRule::Never,
+ r => return Err(Error::InvalidRetainForwardRule(r)),
+ };
+
+ filters.push(Filter {
+ path,
+ qos: qos(requested_qos).ok_or(Error::InvalidQoS(requested_qos))?,
+ nolocal,
+ preserve_retain,
+ retain_forward_rule,
+ });
+ }
+
+ Ok(filters)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) {
+ let mut options = 0;
+ options |= self.qos as u8;
+
+ if self.nolocal {
+ options |= 0b0000_0100;
+ }
+
+ if self.preserve_retain {
+ options |= 0b0000_1000;
+ }
+
+ options |= match self.retain_forward_rule {
+ RetainForwardRule::OnEverySubscribe => 0b0000_0000,
+ RetainForwardRule::OnNewSubscribe => 0b0001_0000,
+ RetainForwardRule::Never => 0b0010_0000,
+ };
+
+ write_mqtt_string(buffer, self.path.as_str());
+ buffer.put_u8(options);
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum RetainForwardRule {
+ OnEverySubscribe,
+ OnNewSubscribe,
+ Never,
+}
+
+impl Default for RetainForwardRule {
+ fn default() -> Self {
+ Self::OnEverySubscribe
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SubscribeProperties {
+ pub id: Option<usize>,
+ pub user_properties: Vec<(String, String)>,
+}
+
+impl SubscribeProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if let Some(id) = &self.id {
+ len += 1 + len_len(*id);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<SubscribeProperties>, Error> {
+ let mut id = None;
+ let mut user_properties = Vec::new();
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::SubscriptionIdentifier => {
+ let (id_len, sub_id) = length(bytes.iter())?;
+ // TODO: Validate 1 +. Tests are working either way
+ cursor += 1 + id_len;
+ bytes.advance(id_len);
+ id = Some(sub_id)
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(SubscribeProperties {
+ id,
+ user_properties,
+ }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(id) = &self.id {
+ buffer.put_u8(PropertyType::SubscriptionIdentifier as u8);
+ write_remaining_length(buffer, *id)?;
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let subscribe_props = SubscribeProperties {
+ id: None,
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ };
+
+ let subscribe_pkt = Subscribe::new(
+ Filter::new("hello/world", QoS::AtMostOnce),
+ Some(subscribe_props),
+ );
+
+ let size_from_size = subscribe_pkt.size();
+ let size_from_write = subscribe_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/unsuback.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/unsuback.rs
new file mode 100644
index 0000000000..d21dc5ef7f
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/unsuback.rs
@@ -0,0 +1,228 @@
+use super::*;
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+/// Acknowledgement to unsubscribe
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct UnsubAck {
+ pub pkid: u16,
+ pub reasons: Vec<UnsubAckReason>,
+ pub properties: Option<UnsubAckProperties>,
+}
+
+impl UnsubAck {
+ fn len(&self) -> usize {
+ let mut len = 2 + self.reasons.len();
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ // just 1 byte representing 0 len
+ len += 1;
+ }
+
+ len
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<UnsubAck, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+
+ let pkid = read_u16(&mut bytes)?;
+ let properties = UnsubAckProperties::read(&mut bytes)?;
+
+ if !bytes.has_remaining() {
+ return Err(Error::MalformedPacket);
+ }
+
+ let mut reasons = Vec::new();
+ while bytes.has_remaining() {
+ let r = read_u8(&mut bytes)?;
+ reasons.push(reason(r)?);
+ }
+
+ let unsuback = UnsubAck {
+ pkid,
+ reasons,
+ properties,
+ };
+
+ Ok(unsuback)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ buffer.put_u8(0xB0);
+ let remaining_len = self.len();
+ let remaining_len_bytes = write_remaining_length(buffer, remaining_len)?;
+
+ buffer.put_u16(self.pkid);
+
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ let p: Vec<u8> = self.reasons.iter().map(|&c| code(c)).collect();
+ buffer.extend_from_slice(&p);
+ Ok(1 + remaining_len_bytes + remaining_len)
+ }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[repr(u8)]
+pub enum UnsubAckReason {
+ Success,
+ NoSubscriptionExisted,
+ UnspecifiedError,
+ ImplementationSpecificError,
+ NotAuthorized,
+ TopicFilterInvalid,
+ PacketIdentifierInUse,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct UnsubAckProperties {
+ pub reason_string: Option<String>,
+ pub user_properties: Vec<(String, String)>,
+}
+
+impl UnsubAckProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ if let Some(reason) = &self.reason_string {
+ len += 1 + 2 + reason.len();
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<UnsubAckProperties>, Error> {
+ let mut reason_string = None;
+ let mut user_properties = Vec::new();
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::ReasonString => {
+ let reason = read_mqtt_string(bytes)?;
+ cursor += 2 + reason.len();
+ reason_string = Some(reason);
+ }
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(UnsubAckProperties {
+ reason_string,
+ user_properties,
+ }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ if let Some(reason) = &self.reason_string {
+ buffer.put_u8(PropertyType::ReasonString as u8);
+ write_mqtt_string(buffer, reason);
+ }
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ Ok(())
+ }
+}
+
+/// Connection return code type
+fn reason(num: u8) -> Result<UnsubAckReason, Error> {
+ let code = match num {
+ 0x00 => UnsubAckReason::Success,
+ 0x11 => UnsubAckReason::NoSubscriptionExisted,
+ 0x80 => UnsubAckReason::UnspecifiedError,
+ 0x83 => UnsubAckReason::ImplementationSpecificError,
+ 0x87 => UnsubAckReason::NotAuthorized,
+ 0x8F => UnsubAckReason::TopicFilterInvalid,
+ 0x91 => UnsubAckReason::PacketIdentifierInUse,
+ num => return Err(Error::InvalidSubscribeReasonCode(num)),
+ };
+
+ Ok(code)
+}
+
+fn code(reason: UnsubAckReason) -> u8 {
+ match reason {
+ UnsubAckReason::Success => 0x00,
+ UnsubAckReason::NoSubscriptionExisted => 0x11,
+ UnsubAckReason::UnspecifiedError => 0x80,
+ UnsubAckReason::ImplementationSpecificError => 0x83,
+ UnsubAckReason::NotAuthorized => 0x87,
+ UnsubAckReason::TopicFilterInvalid => 0x8F,
+ UnsubAckReason::PacketIdentifierInUse => 0x91,
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let unsuback_props = UnsubAckProperties {
+ reason_string: None,
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ };
+
+ let unsuback_pkt = UnsubAck {
+ pkid: 1,
+ reasons: vec![UnsubAckReason::Success],
+ properties: Some(unsuback_props),
+ };
+
+ let size_from_size = unsuback_pkt.size();
+ let size_from_write = unsuback_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/mqttbytes/v5/unsubscribe.rs b/vendor/rumqttc/src/v5/mqttbytes/v5/unsubscribe.rs
new file mode 100644
index 0000000000..2b671ce397
--- /dev/null
+++ b/vendor/rumqttc/src/v5/mqttbytes/v5/unsubscribe.rs
@@ -0,0 +1,176 @@
+use super::*;
+use bytes::{Buf, Bytes};
+
+/// Unsubscribe packet
+#[derive(Debug, Clone, PartialEq, Eq, Default)]
+pub struct Unsubscribe {
+ pub pkid: u16,
+ pub filters: Vec<String>,
+ pub properties: Option<UnsubscribeProperties>,
+}
+
+impl Unsubscribe {
+ pub fn new<S: Into<String>>(filter: S, properties: Option<UnsubscribeProperties>) -> Self {
+ Self {
+ filters: vec![filter.into()],
+ properties,
+ ..Default::default()
+ }
+ }
+
+ pub fn size(&self) -> usize {
+ let len = self.len();
+ let remaining_len_size = len_len(len);
+
+ 1 + remaining_len_size + len
+ }
+
+ fn len(&self) -> usize {
+ // Packet id + length of filters (unlike subscribe, this just a string.
+ // Hence 2 is prefixed for len per filter)
+ let mut len = 2 + self.filters.iter().fold(0, |s, t| 2 + s + t.len());
+
+ if let Some(p) = &self.properties {
+ let properties_len = p.len();
+ let properties_len_len = len_len(properties_len);
+ len += properties_len_len + properties_len;
+ } else {
+ // just 1 byte representing 0 len
+ len += 1;
+ }
+
+ len
+ }
+
+ pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Unsubscribe, Error> {
+ let variable_header_index = fixed_header.fixed_header_len;
+ bytes.advance(variable_header_index);
+
+ let pkid = read_u16(&mut bytes)?;
+ let properties = UnsubscribeProperties::read(&mut bytes)?;
+
+ let mut filters = Vec::with_capacity(1);
+ while bytes.has_remaining() {
+ let filter = read_mqtt_string(&mut bytes)?;
+ filters.push(filter);
+ }
+
+ let unsubscribe = Unsubscribe {
+ pkid,
+ filters,
+ properties,
+ };
+ Ok(unsubscribe)
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<usize, Error> {
+ buffer.put_u8(0xA2);
+
+ // write remaining length
+ let remaining_len = self.len();
+ let remaining_len_bytes = write_remaining_length(buffer, remaining_len)?;
+
+ // write packet id
+ buffer.put_u16(self.pkid);
+
+ if let Some(p) = &self.properties {
+ p.write(buffer)?;
+ } else {
+ write_remaining_length(buffer, 0)?;
+ }
+
+ // write filters
+ for filter in self.filters.iter() {
+ write_mqtt_string(buffer, filter);
+ }
+
+ Ok(1 + remaining_len_bytes + remaining_len)
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct UnsubscribeProperties {
+ pub user_properties: Vec<(String, String)>,
+}
+
+impl UnsubscribeProperties {
+ fn len(&self) -> usize {
+ let mut len = 0;
+
+ for (key, value) in self.user_properties.iter() {
+ len += 1 + 2 + key.len() + 2 + value.len();
+ }
+
+ len
+ }
+
+ pub fn read(bytes: &mut Bytes) -> Result<Option<UnsubscribeProperties>, Error> {
+ let mut user_properties = Vec::new();
+
+ let (properties_len_len, properties_len) = length(bytes.iter())?;
+ bytes.advance(properties_len_len);
+
+ if properties_len == 0 {
+ return Ok(None);
+ }
+
+ let mut cursor = 0;
+ // read until cursor reaches property length. properties_len = 0 will skip this loop
+ while cursor < properties_len {
+ let prop = read_u8(bytes)?;
+ cursor += 1;
+
+ match property(prop)? {
+ PropertyType::UserProperty => {
+ let key = read_mqtt_string(bytes)?;
+ let value = read_mqtt_string(bytes)?;
+ cursor += 2 + key.len() + 2 + value.len();
+ user_properties.push((key, value));
+ }
+ _ => return Err(Error::InvalidPropertyType(prop)),
+ }
+ }
+
+ Ok(Some(UnsubscribeProperties { user_properties }))
+ }
+
+ pub fn write(&self, buffer: &mut BytesMut) -> Result<(), Error> {
+ let len = self.len();
+ write_remaining_length(buffer, len)?;
+
+ for (key, value) in self.user_properties.iter() {
+ buffer.put_u8(PropertyType::UserProperty as u8);
+ write_mqtt_string(buffer, key);
+ write_mqtt_string(buffer, value);
+ }
+
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::super::test::{USER_PROP_KEY, USER_PROP_VAL};
+ use super::*;
+ use bytes::BytesMut;
+ use pretty_assertions::assert_eq;
+
+ #[test]
+ fn length_calculation() {
+ let mut dummy_bytes = BytesMut::new();
+ // Use user_properties to pad the size to exceed ~128 bytes to make the
+ // remaining_length field in the packet be 2 bytes long.
+ let unsubscribe_props = UnsubscribeProperties {
+ user_properties: vec![(USER_PROP_KEY.into(), USER_PROP_VAL.into())],
+ };
+
+ let unsubscribe_pkt = Unsubscribe::new("hello/world", Some(unsubscribe_props));
+
+ let size_from_size = unsubscribe_pkt.size();
+ let size_from_write = unsubscribe_pkt.write(&mut dummy_bytes).unwrap();
+ let size_from_bytes = dummy_bytes.len();
+
+ assert_eq!(size_from_write, size_from_bytes);
+ assert_eq!(size_from_size, size_from_bytes);
+ }
+}
diff --git a/vendor/rumqttc/src/v5/state.rs b/vendor/rumqttc/src/v5/state.rs
new file mode 100644
index 0000000000..8473f1f4cf
--- /dev/null
+++ b/vendor/rumqttc/src/v5/state.rs
@@ -0,0 +1,1039 @@
+use super::mqttbytes::v5::{
+ ConnAck, ConnectReturnCode, Disconnect, DisconnectReasonCode, Packet, PingReq, PubAck,
+ PubAckReason, PubComp, PubCompReason, PubRec, PubRecReason, PubRel, PubRelReason, Publish,
+ SubAck, Subscribe, SubscribeReasonCode, UnsubAck, UnsubAckReason, Unsubscribe,
+};
+use super::mqttbytes::{self, QoS};
+
+use super::{Event, Incoming, Outgoing, Request};
+
+use bytes::{Bytes, BytesMut};
+use std::collections::{HashMap, VecDeque};
+use std::convert::TryInto;
+use std::{io, time::Instant};
+
+/// Errors during state handling
+#[derive(Debug, thiserror::Error)]
+pub enum StateError {
+ /// Io Error while state is passed to network
+ #[error("Io error: {0:?}")]
+ Io(#[from] io::Error),
+ #[error("Conversion error {0:?}")]
+ Coversion(#[from] core::num::TryFromIntError),
+ /// Invalid state for a given operation
+ #[error("Invalid state for a given operation")]
+ InvalidState,
+ /// Received a packet (ack) which isn't asked for
+ #[error("Received unsolicited ack pkid: {0}")]
+ Unsolicited(u16),
+ /// Last pingreq isn't acked
+ #[error("Last pingreq isn't acked")]
+ AwaitPingResp,
+ /// Received a wrong packet while waiting for another packet
+ #[error("Received a wrong packet while waiting for another packet")]
+ WrongPacket,
+ #[error("Timeout while waiting to resolve collision")]
+ CollisionTimeout,
+ #[error("A Subscribe packet must contain atleast one filter")]
+ EmptySubscription,
+ #[error("Mqtt serialization/deserialization error: {0}")]
+ Deserialization(#[from] mqttbytes::Error),
+ #[error(
+ "Cannot use topic alias '{alias:?}'. It's greater than the broker's maximum of '{max:?}'."
+ )]
+ InvalidAlias { alias: u16, max: u16 },
+ #[error("Cannot send packet of size '{pkt_size:?}'. It's greater than the broker's maximum packet size of: '{max:?}'")]
+ OutgoingPacketTooLarge { pkt_size: u32, max: u32 },
+ #[error("Cannot receive packet of size '{pkt_size:?}'. It's greater than the client's maximum packet size of: '{max:?}'")]
+ IncomingPacketTooLarge { pkt_size: usize, max: usize },
+ #[error("Server sent disconnect with reason `{reason_string:?}` and code '{reason_code:?}' ")]
+ ServerDisconnect {
+ reason_code: DisconnectReasonCode,
+ reason_string: Option<String>,
+ },
+ #[error("Unsubscribe failed with reason '{reason:?}' ")]
+ UnsubFail { reason: UnsubAckReason },
+ #[error("Subscribe failed with reason '{reason:?}' ")]
+ SubFail { reason: SubscribeReasonCode },
+ #[error("Publish acknowledgement failed with reason '{reason:?}' ")]
+ PubAckFail { reason: PubAckReason },
+ #[error("Publish receive failed with reason '{reason:?}' ")]
+ PubRecFail { reason: PubRecReason },
+ #[error("Publish release failed with reason '{reason:?}' ")]
+ PubRelFail { reason: PubRelReason },
+ #[error("Publish completion failed with reason '{reason:?}' ")]
+ PubCompFail { reason: PubCompReason },
+ #[error("Connection failed with reason '{reason:?}' ")]
+ ConnFail { reason: ConnectReturnCode },
+}
+
+/// State of the mqtt connection.
+// Design: Methods will just modify the state of the object without doing any network operations
+// Design: All inflight queues are maintained in a pre initialized vec with index as packet id.
+// This is done for 2 reasons
+// Bad acks or out of order acks aren't O(n) causing cpu spikes
+// Any missing acks from the broker are detected during the next recycled use of packet ids
+#[derive(Debug, Clone)]
+pub struct MqttState {
+ /// Status of last ping
+ pub await_pingresp: bool,
+ /// Collision ping count. Collisions stop user requests
+ /// which inturn trigger pings. Multiple pings without
+ /// resolving collisions will result in error
+ pub collision_ping_count: usize,
+ /// Last incoming packet time
+ last_incoming: Instant,
+ /// Last outgoing packet time
+ last_outgoing: Instant,
+ /// Packet id of the last outgoing packet
+ pub(crate) last_pkid: u16,
+ /// Number of outgoing inflight publishes
+ pub(crate) inflight: u16,
+ /// Outgoing QoS 1, 2 publishes which aren't acked yet
+ pub(crate) outgoing_pub: Vec<Option<Publish>>,
+ /// Packet ids of released QoS 2 publishes
+ pub(crate) outgoing_rel: Vec<Option<u16>>,
+ /// Packet ids on incoming QoS 2 publishes
+ pub(crate) incoming_pub: Vec<Option<u16>>,
+ /// Last collision due to broker not acking in order
+ pub collision: Option<Publish>,
+ /// Buffered incoming packets
+ pub events: VecDeque<Event>,
+ /// Write buffer
+ pub write: BytesMut,
+ /// Indicates if acknowledgements should be send immediately
+ pub manual_acks: bool,
+ /// Map of alias_id->topic
+ topic_alises: HashMap<u16, Bytes>,
+ /// `topic_alias_maximum` RECEIVED via connack packet
+ pub broker_topic_alias_max: u16,
+ /// The broker's `max_packet_size` received via connack
+ pub max_outgoing_packet_size: Option<u32>,
+ /// Maximum number of allowed inflight QoS1 & QoS2 requests
+ pub(crate) max_outgoing_inflight: u16,
+ /// Upper limit on the maximum number of allowed inflight QoS1 & QoS2 requests
+ max_outgoing_inflight_upper_limit: u16,
+}
+
+impl MqttState {
+ /// Creates new mqtt state. Same state should be used during a
+ /// connection for persistent sessions while new state should
+ /// instantiated for clean sessions
+ pub fn new(max_inflight: u16, manual_acks: bool) -> Self {
+ MqttState {
+ await_pingresp: false,
+ collision_ping_count: 0,
+ last_incoming: Instant::now(),
+ last_outgoing: Instant::now(),
+ last_pkid: 0,
+ inflight: 0,
+ // index 0 is wasted as 0 is not a valid packet id
+ outgoing_pub: vec![None; max_inflight as usize + 1],
+ outgoing_rel: vec![None; max_inflight as usize + 1],
+ incoming_pub: vec![None; std::u16::MAX as usize + 1],
+ collision: None,
+ // TODO: Optimize these sizes later
+ events: VecDeque::with_capacity(100),
+ write: BytesMut::with_capacity(10 * 1024),
+ manual_acks,
+ topic_alises: HashMap::new(),
+ // Set via CONNACK
+ broker_topic_alias_max: 0,
+ max_outgoing_packet_size: None,
+ max_outgoing_inflight: max_inflight,
+ max_outgoing_inflight_upper_limit: max_inflight,
+ }
+ }
+
+ /// Returns inflight outgoing packets and clears internal queues
+ pub fn clean(&mut self) -> Vec<Request> {
+ let mut pending = Vec::with_capacity(100);
+ // remove and collect pending publishes
+ for publish in self.outgoing_pub.iter_mut() {
+ if let Some(publish) = publish.take() {
+ let request = Request::Publish(publish);
+ pending.push(request);
+ }
+ }
+
+ // remove and collect pending releases
+ for rel in self.outgoing_rel.iter_mut() {
+ if let Some(pkid) = rel.take() {
+ let request = Request::PubRel(PubRel::new(pkid, None));
+ pending.push(request);
+ }
+ }
+
+ // remove packed ids of incoming qos2 publishes
+ for id in self.incoming_pub.iter_mut() {
+ id.take();
+ }
+
+ self.await_pingresp = false;
+ self.collision_ping_count = 0;
+ self.inflight = 0;
+ pending
+ }
+
+ pub fn inflight(&self) -> u16 {
+ self.inflight
+ }
+
+ /// Consolidates handling of all outgoing mqtt packet logic. Returns a packet which should
+ /// be put on to the network by the eventloop
+ pub fn handle_outgoing_packet(&mut self, request: Request) -> Result<(), StateError> {
+ match request {
+ Request::Publish(publish) => {
+ self.check_size(publish.size())?;
+ self.outgoing_publish(publish)?
+ }
+ Request::PubRel(pubrel) => {
+ self.check_size(pubrel.size())?;
+ self.outgoing_pubrel(pubrel)?
+ }
+ Request::Subscribe(subscribe) => {
+ self.check_size(subscribe.size())?;
+ self.outgoing_subscribe(subscribe)?
+ }
+ Request::Unsubscribe(unsubscribe) => {
+ self.check_size(unsubscribe.size())?;
+ self.outgoing_unsubscribe(unsubscribe)?
+ }
+ Request::PingReq => self.outgoing_ping()?,
+ Request::Disconnect => {
+ self.outgoing_disconnect(DisconnectReasonCode::NormalDisconnection)?
+ }
+ Request::PubAck(puback) => {
+ self.check_size(puback.size())?;
+ self.outgoing_puback(puback)?
+ }
+ Request::PubRec(pubrec) => {
+ self.check_size(pubrec.size())?;
+ self.outgoing_pubrec(pubrec)?
+ }
+ _ => unimplemented!(),
+ };
+
+ self.last_outgoing = Instant::now();
+ Ok(())
+ }
+
+ /// Consolidates handling of all incoming mqtt packets. Returns a `Notification` which for the
+ /// user to consume and `Packet` which for the eventloop to put on the network
+ /// E.g For incoming QoS1 publish packet, this method returns (Publish, Puback). Publish packet will
+ /// be forwarded to user and Pubck packet will be written to network
+ pub fn handle_incoming_packet(&mut self, mut packet: Incoming) -> Result<(), StateError> {
+ let out = match &mut packet {
+ Incoming::PingResp(_) => self.handle_incoming_pingresp(),
+ Incoming::Publish(publish) => self.handle_incoming_publish(publish),
+ Incoming::SubAck(suback) => self.handle_incoming_suback(suback),
+ Incoming::UnsubAck(unsuback) => self.handle_incoming_unsuback(unsuback),
+ Incoming::PubAck(puback) => self.handle_incoming_puback(puback),
+ Incoming::PubRec(pubrec) => self.handle_incoming_pubrec(pubrec),
+ Incoming::PubRel(pubrel) => self.handle_incoming_pubrel(pubrel),
+ Incoming::PubComp(pubcomp) => self.handle_incoming_pubcomp(pubcomp),
+ Incoming::ConnAck(connack) => self.handle_incoming_connack(connack),
+ Incoming::Disconnect(disconn) => self.handle_incoming_disconn(disconn),
+ _ => {
+ error!("Invalid incoming packet = {:?}", packet);
+ return Err(StateError::WrongPacket);
+ }
+ };
+
+ out?;
+ self.events.push_back(Event::Incoming(packet));
+ self.last_incoming = Instant::now();
+ Ok(())
+ }
+
+ pub fn handle_protocol_error(&mut self) -> Result<(), StateError> {
+ // send DISCONNECT packet with REASON_CODE 0x82
+ self.outgoing_disconnect(DisconnectReasonCode::ProtocolError)
+ }
+
+ fn handle_incoming_suback(&mut self, suback: &mut SubAck) -> Result<(), StateError> {
+ for reason in suback.return_codes.iter() {
+ match reason {
+ SubscribeReasonCode::Success(qos) => {
+ debug!("SubAck Pkid = {:?}, QoS = {:?}", suback.pkid, qos);
+ }
+ _ => return Err(StateError::SubFail { reason: *reason }),
+ }
+ }
+ Ok(())
+ }
+
+ fn handle_incoming_unsuback(&mut self, unsuback: &mut UnsubAck) -> Result<(), StateError> {
+ for reason in unsuback.reasons.iter() {
+ if reason != &UnsubAckReason::Success {
+ return Err(StateError::UnsubFail { reason: *reason });
+ }
+ }
+ Ok(())
+ }
+
+ fn handle_incoming_connack(&mut self, connack: &mut ConnAck) -> Result<(), StateError> {
+ if connack.code != ConnectReturnCode::Success {
+ return Err(StateError::ConnFail {
+ reason: connack.code,
+ });
+ }
+
+ if let Some(props) = &connack.properties {
+ if let Some(topic_alias_max) = props.topic_alias_max {
+ self.broker_topic_alias_max = topic_alias_max
+ }
+
+ if let Some(max_inflight) = props.receive_max {
+ self.max_outgoing_inflight =
+ max_inflight.min(self.max_outgoing_inflight_upper_limit);
+ // FIXME: Maybe resize the pubrec and pubrel queues here
+ // to save some space.
+ }
+
+ self.max_outgoing_packet_size = props.max_packet_size;
+ }
+ Ok(())
+ }
+
+ fn handle_incoming_disconn(&mut self, disconn: &mut Disconnect) -> Result<(), StateError> {
+ let reason_code = disconn.reason_code;
+ let reason_string = if let Some(props) = &disconn.properties {
+ props.reason_string.clone()
+ } else {
+ None
+ };
+ Err(StateError::ServerDisconnect {
+ reason_code,
+ reason_string,
+ })
+ }
+
+ /// Results in a publish notification in all the QoS cases. Replys with an ack
+ /// in case of QoS1 and Replys rec in case of QoS while also storing the message
+ fn handle_incoming_publish(&mut self, publish: &mut Publish) -> Result<(), StateError> {
+ let qos = publish.qos;
+
+ let topic_alias = match &publish.properties {
+ Some(props) => props.topic_alias,
+ None => None,
+ };
+
+ if !publish.topic.is_empty() {
+ if let Some(alias) = topic_alias {
+ self.topic_alises.insert(alias, publish.topic.clone());
+ }
+ } else if let Some(alias) = topic_alias {
+ if let Some(topic) = self.topic_alises.get(&alias) {
+ publish.topic = topic.to_owned();
+ } else {
+ self.handle_protocol_error()?;
+ };
+ }
+
+ match qos {
+ QoS::AtMostOnce => Ok(()),
+ QoS::AtLeastOnce => {
+ if !self.manual_acks {
+ let puback = PubAck::new(publish.pkid, None);
+ self.outgoing_puback(puback)?;
+ }
+ Ok(())
+ }
+ QoS::ExactlyOnce => {
+ let pkid = publish.pkid;
+ self.incoming_pub[pkid as usize] = Some(pkid);
+
+ if !self.manual_acks {
+ let pubrec = PubRec::new(pkid, None);
+ self.outgoing_pubrec(pubrec)?;
+ }
+ Ok(())
+ }
+ }
+ }
+
+ fn handle_incoming_puback(&mut self, puback: &PubAck) -> Result<(), StateError> {
+ let publish = self
+ .outgoing_pub
+ .get_mut(puback.pkid as usize)
+ .ok_or(StateError::Unsolicited(puback.pkid))?;
+ let v = match publish.take() {
+ Some(_) => {
+ self.inflight -= 1;
+ Ok(())
+ }
+ None => {
+ error!("Unsolicited puback packet: {:?}", puback.pkid);
+ Err(StateError::Unsolicited(puback.pkid))
+ }
+ };
+
+ if puback.reason != PubAckReason::Success
+ && puback.reason != PubAckReason::NoMatchingSubscribers
+ {
+ return Err(StateError::PubAckFail {
+ reason: puback.reason,
+ });
+ }
+
+ if let Some(publish) = self.check_collision(puback.pkid) {
+ self.outgoing_pub[publish.pkid as usize] = Some(publish.clone());
+ self.inflight += 1;
+
+ let pkid = publish.pkid;
+ Packet::Publish(publish).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Publish(pkid));
+ self.events.push_back(event);
+ self.collision_ping_count = 0;
+ }
+
+ v
+ }
+
+ fn handle_incoming_pubrec(&mut self, pubrec: &PubRec) -> Result<(), StateError> {
+ let publish = self
+ .outgoing_pub
+ .get_mut(pubrec.pkid as usize)
+ .ok_or(StateError::Unsolicited(pubrec.pkid))?;
+ match publish.take() {
+ Some(_) => {
+ if pubrec.reason != PubRecReason::Success
+ && pubrec.reason != PubRecReason::NoMatchingSubscribers
+ {
+ return Err(StateError::PubRecFail {
+ reason: pubrec.reason,
+ });
+ }
+
+ // NOTE: Inflight - 1 for qos2 in comp
+ self.outgoing_rel[pubrec.pkid as usize] = Some(pubrec.pkid);
+ Packet::PubRel(PubRel::new(pubrec.pkid, None)).write(&mut self.write)?;
+
+ let event = Event::Outgoing(Outgoing::PubRel(pubrec.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+ None => {
+ error!("Unsolicited pubrec packet: {:?}", pubrec.pkid);
+ Err(StateError::Unsolicited(pubrec.pkid))
+ }
+ }
+ }
+
+ fn handle_incoming_pubrel(&mut self, pubrel: &PubRel) -> Result<(), StateError> {
+ let publish = self
+ .incoming_pub
+ .get_mut(pubrel.pkid as usize)
+ .ok_or(StateError::Unsolicited(pubrel.pkid))?;
+ match publish.take() {
+ Some(_) => {
+ if pubrel.reason != PubRelReason::Success {
+ return Err(StateError::PubRelFail {
+ reason: pubrel.reason,
+ });
+ }
+
+ Packet::PubComp(PubComp::new(pubrel.pkid, None)).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::PubComp(pubrel.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+ None => {
+ error!("Unsolicited pubrel packet: {:?}", pubrel.pkid);
+ Err(StateError::Unsolicited(pubrel.pkid))
+ }
+ }
+ }
+
+ fn handle_incoming_pubcomp(&mut self, pubcomp: &PubComp) -> Result<(), StateError> {
+ if let Some(publish) = self.check_collision(pubcomp.pkid) {
+ let pkid = publish.pkid;
+ Packet::Publish(publish).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Publish(pkid));
+ self.events.push_back(event);
+ self.collision_ping_count = 0;
+ }
+
+ let pubrel = self
+ .outgoing_rel
+ .get_mut(pubcomp.pkid as usize)
+ .ok_or(StateError::Unsolicited(pubcomp.pkid))?;
+ match pubrel.take() {
+ Some(_) => {
+ if pubcomp.reason != PubCompReason::Success {
+ return Err(StateError::PubCompFail {
+ reason: pubcomp.reason,
+ });
+ }
+
+ self.inflight -= 1;
+ Ok(())
+ }
+ None => {
+ error!("Unsolicited pubcomp packet: {:?}", pubcomp.pkid);
+ Err(StateError::Unsolicited(pubcomp.pkid))
+ }
+ }
+ }
+
+ fn handle_incoming_pingresp(&mut self) -> Result<(), StateError> {
+ self.await_pingresp = false;
+ Ok(())
+ }
+
+ /// Adds next packet identifier to QoS 1 and 2 publish packets and returns
+ /// it buy wrapping publish in packet
+ fn outgoing_publish(&mut self, mut publish: Publish) -> Result<(), StateError> {
+ if publish.qos != QoS::AtMostOnce {
+ if publish.pkid == 0 {
+ publish.pkid = self.next_pkid();
+ }
+
+ let pkid = publish.pkid;
+ if self
+ .outgoing_pub
+ .get(publish.pkid as usize)
+ .ok_or(StateError::Unsolicited(publish.pkid))?
+ .is_some()
+ {
+ info!("Collision on packet id = {:?}", publish.pkid);
+ self.collision = Some(publish);
+ let event = Event::Outgoing(Outgoing::AwaitAck(pkid));
+ self.events.push_back(event);
+ return Ok(());
+ }
+
+ // if there is an existing publish at this pkid, this implies that broker hasn't acked this
+ // packet yet. This error is possible only when broker isn't acking sequentially
+ self.outgoing_pub[pkid as usize] = Some(publish.clone());
+ self.inflight += 1;
+ };
+
+ debug!(
+ "Publish. Topic = {}, Pkid = {:?}, Payload Size = {:?}",
+ String::from_utf8(publish.topic.to_vec()).unwrap(),
+ publish.pkid,
+ publish.payload.len()
+ );
+
+ let pkid = publish.pkid;
+
+ if let Some(props) = &publish.properties {
+ if let Some(alias) = props.topic_alias {
+ if alias > self.broker_topic_alias_max {
+ // We MUST NOT send a Topic Alias that is greater than the
+ // broker's Topic Alias Maximum.
+ return Err(StateError::InvalidAlias {
+ alias,
+ max: self.broker_topic_alias_max,
+ });
+ }
+ }
+ };
+
+ Packet::Publish(publish).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Publish(pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_pubrel(&mut self, pubrel: PubRel) -> Result<(), StateError> {
+ let pubrel = self.save_pubrel(pubrel)?;
+
+ debug!("Pubrel. Pkid = {}", pubrel.pkid);
+ Packet::PubRel(PubRel::new(pubrel.pkid, None)).write(&mut self.write)?;
+
+ let event = Event::Outgoing(Outgoing::PubRel(pubrel.pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_puback(&mut self, puback: PubAck) -> Result<(), StateError> {
+ let pkid = puback.pkid;
+ Packet::PubAck(puback).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::PubAck(pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_pubrec(&mut self, pubrec: PubRec) -> Result<(), StateError> {
+ let pkid = pubrec.pkid;
+ Packet::PubRec(pubrec).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::PubRec(pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ /// check when the last control packet/pingreq packet is received and return
+ /// the status which tells if keep alive time has exceeded
+ /// NOTE: status will be checked for zero keepalive times also
+ fn outgoing_ping(&mut self) -> Result<(), StateError> {
+ let elapsed_in = self.last_incoming.elapsed();
+ let elapsed_out = self.last_outgoing.elapsed();
+
+ if self.collision.is_some() {
+ self.collision_ping_count += 1;
+ if self.collision_ping_count >= 2 {
+ return Err(StateError::CollisionTimeout);
+ }
+ }
+
+ // raise error if last ping didn't receive ack
+ if self.await_pingresp {
+ return Err(StateError::AwaitPingResp);
+ }
+
+ self.await_pingresp = true;
+
+ debug!(
+ "Pingreq, last incoming packet before {:?}, last outgoing request before {:?}",
+ elapsed_in, elapsed_out,
+ );
+
+ Packet::PingReq(PingReq).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::PingReq);
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_subscribe(&mut self, mut subscription: Subscribe) -> Result<(), StateError> {
+ if subscription.filters.is_empty() {
+ return Err(StateError::EmptySubscription);
+ }
+
+ let pkid = self.next_pkid();
+ subscription.pkid = pkid;
+
+ debug!(
+ "Subscribe. Topics = {:?}, Pkid = {:?}",
+ subscription.filters, subscription.pkid
+ );
+
+ let pkid = subscription.pkid;
+ Packet::Subscribe(subscription).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Subscribe(pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_unsubscribe(&mut self, mut unsub: Unsubscribe) -> Result<(), StateError> {
+ let pkid = self.next_pkid();
+ unsub.pkid = pkid;
+
+ debug!(
+ "Unsubscribe. Topics = {:?}, Pkid = {:?}",
+ unsub.filters, unsub.pkid
+ );
+
+ let pkid = unsub.pkid;
+ Packet::Unsubscribe(unsub).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Unsubscribe(pkid));
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn outgoing_disconnect(&mut self, reason: DisconnectReasonCode) -> Result<(), StateError> {
+ debug!("Disconnect with {:?}", reason);
+
+ Packet::Disconnect(Disconnect::new(reason)).write(&mut self.write)?;
+ let event = Event::Outgoing(Outgoing::Disconnect);
+ self.events.push_back(event);
+ Ok(())
+ }
+
+ fn check_collision(&mut self, pkid: u16) -> Option<Publish> {
+ if let Some(publish) = &self.collision {
+ if publish.pkid == pkid {
+ return self.collision.take();
+ }
+ }
+
+ None
+ }
+
+ fn check_size(&self, pkt_size: usize) -> Result<(), StateError> {
+ let pkt_size = pkt_size.try_into()?;
+
+ match self.max_outgoing_packet_size {
+ Some(max_size) if pkt_size > max_size => Err(StateError::OutgoingPacketTooLarge {
+ pkt_size,
+ max: max_size,
+ }),
+ _ => Ok(()),
+ }
+ }
+
+ fn save_pubrel(&mut self, mut pubrel: PubRel) -> Result<PubRel, StateError> {
+ let pubrel = match pubrel.pkid {
+ // consider PacketIdentifier(0) as uninitialized packets
+ 0 => {
+ pubrel.pkid = self.next_pkid();
+ pubrel
+ }
+ _ => pubrel,
+ };
+
+ self.outgoing_rel[pubrel.pkid as usize] = Some(pubrel.pkid);
+ self.inflight += 1;
+ Ok(pubrel)
+ }
+
+ /// http://stackoverflow.com/questions/11115364/mqtt-messageid-practical-implementation
+ /// Packet ids are incremented till maximum set inflight messages and reset to 1 after that.
+ ///
+ fn next_pkid(&mut self) -> u16 {
+ let next_pkid = self.last_pkid + 1;
+
+ // When next packet id is at the edge of inflight queue,
+ // set await flag. This instructs eventloop to stop
+ // processing requests until all the inflight publishes
+ // are acked
+ if next_pkid == self.max_outgoing_inflight {
+ self.last_pkid = 0;
+ return next_pkid;
+ }
+
+ self.last_pkid = next_pkid;
+ next_pkid
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::mqttbytes::v5::*;
+ use super::mqttbytes::*;
+ use super::{Event, Incoming, Outgoing, Request};
+ use super::{MqttState, StateError};
+
+ fn build_outgoing_publish(qos: QoS) -> Publish {
+ let topic = "hello/world".to_owned();
+ let payload = vec![1, 2, 3];
+
+ let mut publish = Publish::new(topic, QoS::AtLeastOnce, payload, None);
+ publish.qos = qos;
+ publish
+ }
+
+ fn build_incoming_publish(qos: QoS, pkid: u16) -> Publish {
+ let topic = "hello/world".to_owned();
+ let payload = vec![1, 2, 3];
+
+ let mut publish = Publish::new(topic, QoS::AtLeastOnce, payload, None);
+ publish.pkid = pkid;
+ publish.qos = qos;
+ publish
+ }
+
+ fn build_mqttstate() -> MqttState {
+ MqttState::new(u16::MAX, false)
+ }
+
+ #[test]
+ fn next_pkid_increments_as_expected() {
+ let mut mqtt = build_mqttstate();
+
+ for i in 1..=100 {
+ let pkid = mqtt.next_pkid();
+
+ // loops between 0-99. % 100 == 0 implies border
+ let expected = i % 100;
+ if expected == 0 {
+ break;
+ }
+
+ assert_eq!(expected, pkid);
+ }
+ }
+
+ #[test]
+ fn outgoing_publish_should_set_pkid_and_add_publish_to_queue() {
+ let mut mqtt = build_mqttstate();
+
+ // QoS0 Publish
+ let publish = build_outgoing_publish(QoS::AtMostOnce);
+
+ // QoS 0 publish shouldn't be saved in queue
+ mqtt.outgoing_publish(publish).unwrap();
+ assert_eq!(mqtt.last_pkid, 0);
+ assert_eq!(mqtt.inflight, 0);
+
+ // QoS1 Publish
+ let publish = build_outgoing_publish(QoS::AtLeastOnce);
+
+ // Packet id should be set and publish should be saved in queue
+ mqtt.outgoing_publish(publish.clone()).unwrap();
+ assert_eq!(mqtt.last_pkid, 1);
+ assert_eq!(mqtt.inflight, 1);
+
+ // Packet id should be incremented and publish should be saved in queue
+ mqtt.outgoing_publish(publish).unwrap();
+ assert_eq!(mqtt.last_pkid, 2);
+ assert_eq!(mqtt.inflight, 2);
+
+ // QoS1 Publish
+ let publish = build_outgoing_publish(QoS::ExactlyOnce);
+
+ // Packet id should be set and publish should be saved in queue
+ mqtt.outgoing_publish(publish.clone()).unwrap();
+ assert_eq!(mqtt.last_pkid, 3);
+ assert_eq!(mqtt.inflight, 3);
+
+ // Packet id should be incremented and publish should be saved in queue
+ mqtt.outgoing_publish(publish).unwrap();
+ assert_eq!(mqtt.last_pkid, 4);
+ assert_eq!(mqtt.inflight, 4);
+ }
+
+ #[test]
+ fn outgoing_publish_with_max_inflight_is_ok() {
+ let mut mqtt = MqttState::new(2, false);
+
+ // QoS2 publish
+ let publish = build_outgoing_publish(QoS::ExactlyOnce);
+
+ mqtt.outgoing_publish(publish.clone()).unwrap();
+ assert_eq!(mqtt.last_pkid, 1);
+ assert_eq!(mqtt.inflight, 1);
+
+ // Packet id should be set back down to 0, since we hit the limit
+ mqtt.outgoing_publish(publish.clone()).unwrap();
+ assert_eq!(mqtt.last_pkid, 0);
+ assert_eq!(mqtt.inflight, 2);
+
+ // This should cause a collition
+ mqtt.outgoing_publish(publish.clone()).unwrap();
+ assert_eq!(mqtt.last_pkid, 1);
+ assert_eq!(mqtt.inflight, 2);
+ assert!(mqtt.collision.is_some());
+
+ mqtt.handle_incoming_puback(&PubAck::new(1, None)).unwrap();
+ mqtt.handle_incoming_puback(&PubAck::new(2, None)).unwrap();
+ assert_eq!(mqtt.inflight, 1);
+
+ // Now there should be space in the outgoing queue
+ mqtt.outgoing_publish(publish.clone()).unwrap();
+ assert_eq!(mqtt.last_pkid, 0);
+ assert_eq!(mqtt.inflight, 2);
+ }
+
+ #[test]
+ fn incoming_publish_should_be_added_to_queue_correctly() {
+ let mut mqtt = build_mqttstate();
+
+ // QoS0, 1, 2 Publishes
+ let mut publish1 = build_incoming_publish(QoS::AtMostOnce, 1);
+ let mut publish2 = build_incoming_publish(QoS::AtLeastOnce, 2);
+ let mut publish3 = build_incoming_publish(QoS::ExactlyOnce, 3);
+
+ mqtt.handle_incoming_publish(&mut publish1).unwrap();
+ mqtt.handle_incoming_publish(&mut publish2).unwrap();
+ mqtt.handle_incoming_publish(&mut publish3).unwrap();
+
+ let pkid = mqtt.incoming_pub[3].unwrap();
+
+ // only qos2 publish should be add to queue
+ assert_eq!(pkid, 3);
+ }
+
+ #[test]
+ fn incoming_publish_should_be_acked() {
+ let mut mqtt = build_mqttstate();
+
+ // QoS0, 1, 2 Publishes
+ let mut publish1 = build_incoming_publish(QoS::AtMostOnce, 1);
+ let mut publish2 = build_incoming_publish(QoS::AtLeastOnce, 2);
+ let mut publish3 = build_incoming_publish(QoS::ExactlyOnce, 3);
+
+ mqtt.handle_incoming_publish(&mut publish1).unwrap();
+ mqtt.handle_incoming_publish(&mut publish2).unwrap();
+ mqtt.handle_incoming_publish(&mut publish3).unwrap();
+
+ if let Event::Outgoing(Outgoing::PubAck(pkid)) = mqtt.events[0] {
+ assert_eq!(pkid, 2);
+ } else {
+ panic!("missing puback");
+ }
+
+ if let Event::Outgoing(Outgoing::PubRec(pkid)) = mqtt.events[1] {
+ assert_eq!(pkid, 3);
+ } else {
+ panic!("missing PubRec");
+ }
+ }
+
+ #[test]
+ fn incoming_publish_should_not_be_acked_with_manual_acks() {
+ let mut mqtt = build_mqttstate();
+ mqtt.manual_acks = true;
+
+ // QoS0, 1, 2 Publishes
+ let mut publish1 = build_incoming_publish(QoS::AtMostOnce, 1);
+ let mut publish2 = build_incoming_publish(QoS::AtLeastOnce, 2);
+ let mut publish3 = build_incoming_publish(QoS::ExactlyOnce, 3);
+
+ mqtt.handle_incoming_publish(&mut publish1).unwrap();
+ mqtt.handle_incoming_publish(&mut publish2).unwrap();
+ mqtt.handle_incoming_publish(&mut publish3).unwrap();
+
+ let pkid = mqtt.incoming_pub[3].unwrap();
+ assert_eq!(pkid, 3);
+
+ assert!(mqtt.events.is_empty());
+ }
+
+ #[test]
+ fn incoming_qos2_publish_should_send_rec_to_network_and_publish_to_user() {
+ let mut mqtt = build_mqttstate();
+ let mut publish = build_incoming_publish(QoS::ExactlyOnce, 1);
+
+ mqtt.handle_incoming_publish(&mut publish).unwrap();
+ let packet = Packet::read(&mut mqtt.write, Some(10 * 1024)).unwrap();
+ match packet {
+ Packet::PubRec(pubrec) => assert_eq!(pubrec.pkid, 1),
+ _ => panic!("Invalid network request: {:?}", packet),
+ }
+ }
+
+ #[test]
+ fn incoming_puback_should_remove_correct_publish_from_queue() {
+ let mut mqtt = build_mqttstate();
+
+ let publish1 = build_outgoing_publish(QoS::AtLeastOnce);
+ let publish2 = build_outgoing_publish(QoS::ExactlyOnce);
+
+ mqtt.outgoing_publish(publish1).unwrap();
+ mqtt.outgoing_publish(publish2).unwrap();
+ assert_eq!(mqtt.inflight, 2);
+
+ mqtt.handle_incoming_puback(&PubAck::new(1, None)).unwrap();
+ assert_eq!(mqtt.inflight, 1);
+
+ mqtt.handle_incoming_puback(&PubAck::new(2, None)).unwrap();
+ assert_eq!(mqtt.inflight, 0);
+
+ assert!(mqtt.outgoing_pub[1].is_none());
+ assert!(mqtt.outgoing_pub[2].is_none());
+ }
+
+ #[test]
+ fn incoming_puback_with_pkid_greater_than_max_inflight_should_be_handled_gracefully() {
+ let mut mqtt = build_mqttstate();
+
+ let got = mqtt
+ .handle_incoming_puback(&PubAck::new(101, None))
+ .unwrap_err();
+
+ match got {
+ StateError::Unsolicited(pkid) => assert_eq!(pkid, 101),
+ e => panic!("Unexpected error: {}", e),
+ }
+ }
+
+ #[test]
+ fn incoming_pubrec_should_release_publish_from_queue_and_add_relid_to_rel_queue() {
+ let mut mqtt = build_mqttstate();
+
+ let publish1 = build_outgoing_publish(QoS::AtLeastOnce);
+ let publish2 = build_outgoing_publish(QoS::ExactlyOnce);
+
+ let _publish_out = mqtt.outgoing_publish(publish1);
+ let _publish_out = mqtt.outgoing_publish(publish2);
+
+ mqtt.handle_incoming_pubrec(&PubRec::new(2, None)).unwrap();
+ assert_eq!(mqtt.inflight, 2);
+
+ // check if the remaining element's pkid is 1
+ let backup = mqtt.outgoing_pub[1].clone();
+ assert_eq!(backup.unwrap().pkid, 1);
+
+ // check if the qos2 element's release pkid is 2
+ assert_eq!(mqtt.outgoing_rel[2].unwrap(), 2);
+ }
+
+ #[test]
+ fn incoming_pubrec_should_send_release_to_network_and_nothing_to_user() {
+ let mut mqtt = build_mqttstate();
+
+ let publish = build_outgoing_publish(QoS::ExactlyOnce);
+ mqtt.outgoing_publish(publish).unwrap();
+ let packet = Packet::read(&mut mqtt.write, Some(10 * 1024)).unwrap();
+ match packet {
+ Packet::Publish(publish) => assert_eq!(publish.pkid, 1),
+ packet => panic!("Invalid network request: {:?}", packet),
+ }
+
+ mqtt.handle_incoming_pubrec(&PubRec::new(1, None)).unwrap();
+ let packet = Packet::read(&mut mqtt.write, Some(10 * 1024)).unwrap();
+ match packet {
+ Packet::PubRel(pubrel) => assert_eq!(pubrel.pkid, 1),
+ packet => panic!("Invalid network request: {:?}", packet),
+ }
+ }
+
+ #[test]
+ fn incoming_pubrel_should_send_comp_to_network_and_nothing_to_user() {
+ let mut mqtt = build_mqttstate();
+ let mut publish = build_incoming_publish(QoS::ExactlyOnce, 1);
+
+ mqtt.handle_incoming_publish(&mut publish).unwrap();
+ let packet = Packet::read(&mut mqtt.write, Some(10 * 1024)).unwrap();
+ match packet {
+ Packet::PubRec(pubrec) => assert_eq!(pubrec.pkid, 1),
+ packet => panic!("Invalid network request: {:?}", packet),
+ }
+
+ mqtt.handle_incoming_pubrel(&PubRel::new(1, None)).unwrap();
+ let packet = Packet::read(&mut mqtt.write, Some(10 * 1024)).unwrap();
+ match packet {
+ Packet::PubComp(pubcomp) => assert_eq!(pubcomp.pkid, 1),
+ packet => panic!("Invalid network request: {:?}", packet),
+ }
+ }
+
+ #[test]
+ fn incoming_pubcomp_should_release_correct_pkid_from_release_queue() {
+ let mut mqtt = build_mqttstate();
+ let publish = build_outgoing_publish(QoS::ExactlyOnce);
+
+ mqtt.outgoing_publish(publish).unwrap();
+ mqtt.handle_incoming_pubrec(&PubRec::new(1, None)).unwrap();
+
+ mqtt.handle_incoming_pubcomp(&PubComp::new(1, None))
+ .unwrap();
+ assert_eq!(mqtt.inflight, 0);
+ }
+
+ #[test]
+ fn outgoing_ping_handle_should_throw_errors_for_no_pingresp() {
+ let mut mqtt = build_mqttstate();
+ mqtt.outgoing_ping().unwrap();
+
+ // network activity other than pingresp
+ let publish = build_outgoing_publish(QoS::AtLeastOnce);
+ mqtt.handle_outgoing_packet(Request::Publish(publish))
+ .unwrap();
+ mqtt.handle_incoming_packet(Incoming::PubAck(PubAck::new(1, None)))
+ .unwrap();
+
+ // should throw error because we didn't get pingresp for previous ping
+ match mqtt.outgoing_ping() {
+ Ok(_) => panic!("Should throw pingresp await error"),
+ Err(StateError::AwaitPingResp) => (),
+ Err(e) => panic!("Should throw pingresp await error. Error = {:?}", e),
+ }
+ }
+
+ #[test]
+ fn outgoing_ping_handle_should_succeed_if_pingresp_is_received() {
+ let mut mqtt = build_mqttstate();
+
+ // should ping
+ mqtt.outgoing_ping().unwrap();
+ mqtt.handle_incoming_packet(Incoming::PingResp(PingResp))
+ .unwrap();
+
+ // should ping
+ mqtt.outgoing_ping().unwrap();
+ }
+}
diff --git a/vendor/rumqttc/src/websockets.rs b/vendor/rumqttc/src/websockets.rs
new file mode 100644
index 0000000000..a4dafd51dc
--- /dev/null
+++ b/vendor/rumqttc/src/websockets.rs
@@ -0,0 +1,73 @@
+use http::{header::ToStrError, Response};
+
+#[derive(Debug, thiserror::Error)]
+pub enum UrlError {
+ #[error("Invalid protocol specified inside url.")]
+ Protocol,
+ #[error("Couldn't parse host from url.")]
+ Host,
+ #[error("Couldn't parse host url.")]
+ Parse(#[from] http::uri::InvalidUri),
+}
+
+#[derive(Debug, thiserror::Error)]
+pub enum ValidationError {
+ #[error("Websocket response does not contain subprotocol header")]
+ SubprotocolHeaderMissing,
+ #[error("MQTT not in subprotocol header: {0}")]
+ SubprotocolMqttMissing(String),
+ #[error("Subprotocol header couldn't be converted into string representation")]
+ HeaderToStr(#[from] ToStrError),
+}
+
+pub(crate) fn validate_response_headers(
+ response: Response<Option<Vec<u8>>>,
+) -> Result<(), ValidationError> {
+ let subprotocol = response
+ .headers()
+ .get("Sec-WebSocket-Protocol")
+ .ok_or(ValidationError::SubprotocolHeaderMissing)?
+ .to_str()?;
+
+ // Server must respond with Sec-WebSocket-Protocol header value of "mqtt"
+ // https://http.dev/ws#sec-websocket-protocol
+ if subprotocol.trim() != "mqtt" {
+ return Err(ValidationError::SubprotocolMqttMissing(
+ subprotocol.to_owned(),
+ ));
+ }
+
+ Ok(())
+}
+
+pub(crate) fn split_url(url: &str) -> Result<(String, u16), UrlError> {
+ let uri = url.parse::<http::Uri>()?;
+ let domain = domain(&uri).ok_or(UrlError::Protocol)?;
+ let port = port(&uri).ok_or(UrlError::Host)?;
+ Ok((domain, port))
+}
+
+fn domain(uri: &http::Uri) -> Option<String> {
+ uri.host().map(|host| {
+ // If host is an IPv6 address, it might be surrounded by brackets. These brackets are
+ // *not* part of a valid IP, so they must be stripped out.
+ //
+ // The URI from the request is guaranteed to be valid, so we don't need a separate
+ // check for the closing bracket.
+ let host = if host.starts_with('[') {
+ &host[1..host.len() - 1]
+ } else {
+ host
+ };
+
+ host.to_owned()
+ })
+}
+
+fn port(uri: &http::Uri) -> Option<u16> {
+ uri.port_u16().or_else(|| match uri.scheme_str() {
+ Some("wss") => Some(443),
+ Some("ws") => Some(80),
+ _ => None,
+ })
+}
diff --git a/vendor/rumqttc/tests/broker.rs b/vendor/rumqttc/tests/broker.rs
new file mode 100644
index 0000000000..a6ebacc82d
--- /dev/null
+++ b/vendor/rumqttc/tests/broker.rs
@@ -0,0 +1,306 @@
+use rumqttc::mqttbytes::v4::*;
+use rumqttc::mqttbytes::*;
+use std::collections::VecDeque;
+use std::io;
+use std::time::Duration;
+use tokio::net::TcpListener;
+use tokio::select;
+use tokio::{task, time};
+
+use bytes::BytesMut;
+use flume::{bounded, Receiver, Sender};
+use rumqttc::{Event, Incoming, Outgoing, Packet};
+use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
+
+pub struct Broker {
+ pub(crate) framed: Network,
+ pub(crate) incoming: VecDeque<Packet>,
+ outgoing_tx: Sender<Packet>,
+ outgoing_rx: Receiver<Packet>,
+}
+
+impl Broker {
+ /// Create a new broker which accepts 1 mqtt connection
+ pub async fn new(port: u16, connack: u8) -> Broker {
+ let addr = format!("127.0.0.1:{port}");
+ let listener = TcpListener::bind(&addr).await.unwrap();
+
+ let (stream, _) = listener.accept().await.unwrap();
+ let mut framed = Network::new(stream, 10 * 1024);
+ let mut incoming = VecDeque::new();
+ let (outgoing_tx, outgoing_rx) = bounded(10);
+ framed.readb(&mut incoming).await.unwrap();
+
+ match incoming.pop_front().unwrap() {
+ Packet::Connect(_) => {
+ let connack = match connack {
+ 0 => ConnAck::new(ConnectReturnCode::Success, false),
+ 1 => ConnAck::new(ConnectReturnCode::BadUserNamePassword, false),
+ _ => {
+ return Broker {
+ framed,
+ incoming,
+ outgoing_tx,
+ outgoing_rx,
+ }
+ }
+ };
+
+ framed.connack(connack).await.unwrap();
+ }
+ _ => {
+ panic!("Expecting connect packet");
+ }
+ }
+
+ Broker {
+ framed,
+ incoming: VecDeque::new(),
+ outgoing_tx,
+ outgoing_rx,
+ }
+ }
+
+ // Reads a publish packet from the stream with 2 second timeout
+ pub async fn read_publish(&mut self) -> Option<Publish> {
+ loop {
+ let packet = if !self.incoming.is_empty() {
+ self.incoming.pop_front().unwrap()
+ } else {
+ let packet = time::timeout(Duration::from_secs(2), async {
+ self.framed.readb(&mut self.incoming).await.unwrap();
+ self.incoming.pop_front().unwrap()
+ })
+ .await;
+
+ match packet {
+ Ok(packet) => packet,
+ Err(_e) => return None,
+ }
+ };
+
+ match packet {
+ Packet::Publish(publish) => return Some(publish),
+ Packet::PingReq => {
+ self.framed.write(Packet::PingResp).await.unwrap();
+ continue;
+ }
+ packet => panic!("Expecting a publish. Received = {:?}", packet),
+ }
+ }
+ }
+
+ /// Reads next packet from the stream
+ pub async fn read_packet(&mut self) -> Option<Packet> {
+ let _ = time::timeout(Duration::from_secs(30), async {
+ let p = self.framed.readb(&mut self.incoming).await;
+ if p.is_err() {
+ println!("Broker read = {p:?}");
+ }
+ // Sometimes there can be a timeout on the connection so instead of unwrapping return
+ // an Option<Packet>
+ // p.unwrap()
+ })
+ .await;
+
+ self.incoming.pop_front()
+ }
+
+ /// Reads next packet from the stream
+ pub async fn blackhole(&mut self) -> Packet {
+ loop {
+ self.framed.readb(&mut self.incoming).await.unwrap();
+ }
+ }
+
+ /// Sends an acknowledgement
+ pub async fn ack(&mut self, pkid: u16) {
+ let packet = Packet::PubAck(PubAck::new(pkid));
+ self.framed.write(packet).await.unwrap();
+ }
+
+ /// Sends an acknowledgement
+ pub async fn pingresp(&mut self) {
+ let packet = Packet::PingResp;
+ self.framed.write(packet).await.unwrap();
+ }
+
+ pub async fn spawn_publishes(&mut self, count: u8, qos: QoS, delay: u64) {
+ let tx = self.outgoing_tx.clone();
+
+ task::spawn(async move {
+ for i in 1..=count {
+ let topic = "hello/world".to_owned();
+ let payload = vec![1, 2, 3, i];
+ let mut publish = Publish::new(topic, qos, payload);
+
+ if qos as u8 > 0 {
+ publish.pkid = i as u16;
+ }
+
+ let packet = Packet::Publish(publish);
+ tx.send_async(packet).await.unwrap();
+ time::sleep(Duration::from_secs(delay)).await;
+ }
+ });
+ }
+
+ /// Selects between outgoing and incoming packets
+ pub async fn tick(&mut self) -> Event {
+ select! {
+ request = self.outgoing_rx.recv_async() => {
+ let request = request.unwrap();
+ let outgoing = self.framed.write(request).await.unwrap();
+ Event::Outgoing(outgoing)
+ }
+ packet = self.framed.readb(&mut self.incoming) => {
+ packet.unwrap();
+ let incoming = self.incoming.pop_front().unwrap();
+ Event::Incoming(incoming)
+ }
+ }
+ }
+}
+
+/// Network transforms packets <-> frames efficiently. It takes
+/// advantage of pre-allocation, buffering and vectorization when
+/// appropriate to achieve performance
+pub struct Network {
+ /// Socket for IO
+ socket: Box<dyn N>,
+ /// Buffered reads
+ read: BytesMut,
+ /// Buffered writes
+ write: BytesMut,
+ /// Maximum packet size
+ max_incoming_size: usize,
+ /// Maximum readv count
+ max_readb_count: usize,
+}
+
+impl Network {
+ pub fn new(socket: impl N + 'static, max_incoming_size: usize) -> Network {
+ let socket = Box::new(socket) as Box<dyn N>;
+ Network {
+ socket,
+ read: BytesMut::with_capacity(10 * 1024),
+ write: BytesMut::with_capacity(10 * 1024),
+ max_incoming_size,
+ max_readb_count: 10,
+ }
+ }
+
+ /// Reads more than 'required' bytes to frame a packet into self.read buffer
+ async fn read_bytes(&mut self, required: usize) -> io::Result<usize> {
+ let mut total_read = 0;
+ loop {
+ let read = self.socket.read_buf(&mut self.read).await?;
+ if 0 == read {
+ return if self.read.is_empty() {
+ Err(io::Error::new(
+ io::ErrorKind::ConnectionAborted,
+ "connection closed by peer",
+ ))
+ } else {
+ Err(io::Error::new(
+ io::ErrorKind::ConnectionReset,
+ "connection reset by peer",
+ ))
+ };
+ }
+
+ total_read += read;
+ if total_read >= required {
+ return Ok(total_read);
+ }
+ }
+ }
+
+ pub async fn connack(&mut self, connack: ConnAck) -> io::Result<usize> {
+ let mut write = BytesMut::new();
+ let len = match connack.write(&mut write) {
+ Ok(size) => size,
+ Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e.to_string())),
+ };
+
+ self.socket.write_all(&write[..]).await?;
+ Ok(len)
+ }
+
+ /// Read packets in bulk. This allow replies to be in bulk. This method is used
+ /// after the connection is established to read a bunch of incoming packets
+ pub async fn readb(&mut self, incoming: &mut VecDeque<Incoming>) -> io::Result<()> {
+ let mut count = 0;
+ loop {
+ match read(&mut self.read, self.max_incoming_size) {
+ Ok(packet) => {
+ incoming.push_back(packet);
+ count += 1;
+ if count >= self.max_readb_count {
+ return Ok(());
+ }
+ }
+ // If some packets are already framed, return those
+ Err(Error::InsufficientBytes(_)) if count > 0 => return Ok(()),
+ // Wait for more bytes until a frame can be created
+ Err(Error::InsufficientBytes(required)) => {
+ self.read_bytes(required).await?;
+ }
+ Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e.to_string())),
+ };
+ }
+ }
+
+ #[inline]
+ async fn write(&mut self, packet: Packet) -> Result<Outgoing, Error> {
+ let outgoing = outgoing(&packet);
+ match packet {
+ Packet::Publish(packet) => packet.write(&mut self.write)?,
+ Packet::PubRel(packet) => packet.write(&mut self.write)?,
+ Packet::PingReq => {
+ let packet = PingReq;
+ packet.write(&mut self.write)?
+ }
+ Packet::PingResp => {
+ let packet = PingResp;
+ packet.write(&mut self.write)?
+ }
+ Packet::Subscribe(packet) => packet.write(&mut self.write)?,
+ Packet::SubAck(packet) => packet.write(&mut self.write)?,
+ Packet::Unsubscribe(packet) => packet.write(&mut self.write)?,
+ Packet::UnsubAck(packet) => packet.write(&mut self.write)?,
+ Packet::Disconnect => {
+ let packet = Disconnect;
+ packet.write(&mut self.write)?
+ }
+ Packet::PubAck(packet) => packet.write(&mut self.write)?,
+ Packet::PubRec(packet) => packet.write(&mut self.write)?,
+ Packet::PubComp(packet) => packet.write(&mut self.write)?,
+ _ => unimplemented!(),
+ };
+
+ self.socket.write_all(&self.write[..]).await.unwrap();
+ self.write.clear();
+
+ Ok(outgoing)
+ }
+}
+
+fn outgoing(packet: &Packet) -> Outgoing {
+ match packet {
+ Packet::Publish(publish) => Outgoing::Publish(publish.pkid),
+ Packet::PubAck(puback) => Outgoing::PubAck(puback.pkid),
+ Packet::PubRec(pubrec) => Outgoing::PubRec(pubrec.pkid),
+ Packet::PubRel(pubrel) => Outgoing::PubRel(pubrel.pkid),
+ Packet::PubComp(pubcomp) => Outgoing::PubComp(pubcomp.pkid),
+ Packet::Subscribe(subscribe) => Outgoing::Subscribe(subscribe.pkid),
+ Packet::Unsubscribe(unsubscribe) => Outgoing::Unsubscribe(unsubscribe.pkid),
+ Packet::PingReq => Outgoing::PingReq,
+ Packet::PingResp => Outgoing::PingResp,
+ Packet::Disconnect => Outgoing::Disconnect,
+ packet => panic!("Invalid outgoing packet = {:?}", packet),
+ }
+}
+
+pub trait N: AsyncRead + AsyncWrite + Send + Sync + Unpin {}
+impl<T> N for T where T: AsyncRead + AsyncWrite + Unpin + Send + Sync {}
diff --git a/vendor/rumqttc/tests/reliability.rs b/vendor/rumqttc/tests/reliability.rs
new file mode 100644
index 0000000000..3e7acd1e35
--- /dev/null
+++ b/vendor/rumqttc/tests/reliability.rs
@@ -0,0 +1,583 @@
+use matches::assert_matches;
+use std::time::{Duration, Instant};
+use tokio::{task, time};
+
+mod broker;
+
+use broker::*;
+use rumqttc::*;
+
+async fn start_requests(count: u8, qos: QoS, delay: u64, client: AsyncClient) {
+ for i in 1..=count {
+ let topic = "hello/world".to_owned();
+ let payload = vec![i, 1, 2, 3];
+
+ let _ = client.publish(topic, qos, false, payload).await;
+ time::sleep(Duration::from_secs(delay)).await;
+ }
+}
+
+async fn start_requests_with_payload(
+ count: u8,
+ qos: QoS,
+ delay: u64,
+ client: AsyncClient,
+ payload: usize,
+) {
+ for i in 1..=count {
+ let topic = "hello/world".to_owned();
+ let payload = vec![i; payload];
+
+ let _ = client.publish(topic, qos, false, payload).await;
+ time::sleep(Duration::from_secs(delay)).await;
+ }
+}
+
+async fn run(eventloop: &mut EventLoop, reconnect: bool) -> Result<(), ConnectionError> {
+ 'reconnect: loop {
+ loop {
+ let o = eventloop.poll().await;
+ println!("Polled = {o:?}");
+ match o {
+ Ok(_) => continue,
+ Err(_) if reconnect => continue 'reconnect,
+ Err(e) => return Err(e),
+ }
+ }
+ }
+}
+
+async fn _tick(
+ eventloop: &mut EventLoop,
+ reconnect: bool,
+ count: usize,
+) -> Result<(), ConnectionError> {
+ 'reconnect: loop {
+ for i in 0..count {
+ let o = eventloop.poll().await;
+ println!("{i}. Polled = {o:?}");
+ match o {
+ Ok(_) => continue,
+ Err(_) if reconnect => continue 'reconnect,
+ Err(e) => return Err(e),
+ }
+ }
+
+ break;
+ }
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn connection_should_timeout_on_time() {
+ task::spawn(async move {
+ let _broker = Broker::new(1880, 3).await;
+ time::sleep(Duration::from_secs(10)).await;
+ });
+
+ time::sleep(Duration::from_secs(1)).await;
+ let options = MqttOptions::new("dummy", "127.0.0.1", 1880);
+ let mut eventloop = EventLoop::new(options, 5);
+
+ let start = Instant::now();
+ let o = eventloop.poll().await;
+ let elapsed = start.elapsed();
+
+ dbg!(&o);
+ assert_matches!(o, Err(ConnectionError::NetworkTimeout));
+ assert_eq!(elapsed.as_secs(), 5);
+}
+
+//
+// All keep alive tests here
+//
+
+#[test]
+#[should_panic]
+fn test_invalid_keep_alive_value() {
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 1885);
+ options.set_keep_alive(Duration::from_millis(10));
+}
+
+#[test]
+fn test_zero_keep_alive_values() {
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 1885);
+ options.set_keep_alive(Duration::ZERO);
+}
+
+#[test]
+fn test_valid_keep_alive_values() {
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 1885);
+ options.set_keep_alive(Duration::from_secs(1));
+}
+
+#[tokio::test]
+async fn idle_connection_triggers_pings_on_time() {
+ let keep_alive = 1;
+
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 1885);
+ options.set_keep_alive(Duration::from_secs(keep_alive));
+
+ // Create client eventloop and poll
+ task::spawn(async move {
+ let mut eventloop = EventLoop::new(options, 5);
+ run(&mut eventloop, false).await.unwrap();
+ });
+
+ let mut broker = Broker::new(1885, 0).await;
+ let mut count = 0;
+ let mut start = Instant::now();
+
+ for _ in 0..3 {
+ let packet = broker.read_packet().await.unwrap();
+ match packet {
+ Packet::PingReq => {
+ count += 1;
+ let elapsed = start.elapsed();
+ assert_eq!(elapsed.as_secs(), { keep_alive });
+ broker.pingresp().await;
+ start = Instant::now();
+ }
+ _ => {
+ panic!("Expecting ping, Received: {:?}", packet);
+ }
+ }
+ }
+
+ assert_eq!(count, 3);
+}
+
+#[tokio::test]
+async fn some_outgoing_and_no_incoming_should_trigger_pings_on_time() {
+ let keep_alive = 5;
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 1886);
+
+ options.set_keep_alive(Duration::from_secs(keep_alive));
+
+ // start sending qos0 publishes. this makes sure that there is
+ // outgoing activity but no incoming activity
+ let (client, mut eventloop) = AsyncClient::new(options, 5);
+
+ // Start sending publishes
+ task::spawn(async move {
+ start_requests(10, QoS::AtMostOnce, 1, client).await;
+ });
+
+ // start the eventloop
+ task::spawn(async move {
+ run(&mut eventloop, false).await.unwrap();
+ });
+
+ let mut broker = Broker::new(1886, 0).await;
+ let mut count = 0;
+ let mut start = Instant::now();
+
+ loop {
+ let event = broker.tick().await;
+
+ if event == Event::Incoming(Incoming::PingReq) {
+ // wait for 3 pings
+ count += 1;
+ if count == 3 {
+ break;
+ }
+
+ assert_eq!(start.elapsed().as_secs(), { keep_alive });
+ broker.pingresp().await;
+ start = Instant::now();
+ }
+ }
+
+ assert_eq!(count, 3);
+}
+
+#[tokio::test]
+async fn some_incoming_and_no_outgoing_should_trigger_pings_on_time() {
+ let keep_alive = 5;
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 2000);
+
+ options.set_keep_alive(Duration::from_secs(keep_alive));
+
+ task::spawn(async move {
+ let mut eventloop = EventLoop::new(options, 5);
+ run(&mut eventloop, false).await.unwrap();
+ });
+
+ let mut broker = Broker::new(2000, 0).await;
+ let mut count = 0;
+
+ // Start sending qos 0 publishes to the client. This triggers
+ // some incoming and no outgoing packets in the client
+ broker.spawn_publishes(10, QoS::AtMostOnce, 1).await;
+
+ let mut start = Instant::now();
+ loop {
+ let event = broker.tick().await;
+
+ if event == Event::Incoming(Incoming::PingReq) {
+ // wait for 3 pings
+ count += 1;
+ if count == 3 {
+ break;
+ }
+
+ assert_eq!(start.elapsed().as_secs(), { keep_alive });
+ broker.pingresp().await;
+ start = Instant::now();
+ }
+ }
+
+ assert_eq!(count, 3);
+}
+
+#[tokio::test]
+async fn detects_halfopen_connections_in_the_second_ping_request() {
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 2001);
+ options.set_keep_alive(Duration::from_secs(5));
+
+ // A broker which consumes packets but doesn't reply
+ task::spawn(async move {
+ let mut broker = Broker::new(2001, 0).await;
+ broker.blackhole().await;
+ });
+
+ time::sleep(Duration::from_secs(1)).await;
+ let start = Instant::now();
+ let mut eventloop = EventLoop::new(options, 5);
+ loop {
+ if let Err(e) = eventloop.poll().await {
+ match e {
+ ConnectionError::MqttState(StateError::AwaitPingResp) => break,
+ v => panic!("Expecting pingresp error. Found = {:?}", v),
+ }
+ }
+ }
+
+ assert_eq!(start.elapsed().as_secs(), 10);
+}
+
+//
+// All flow control tests here
+//
+
+#[tokio::test]
+async fn requests_are_blocked_after_max_inflight_queue_size() {
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 1887);
+ options.set_inflight(5);
+ let inflight = options.inflight();
+
+ // start sending qos0 publishes. this makes sure that there is
+ // outgoing activity but no incoming activity
+ let (client, mut eventloop) = AsyncClient::new(options, 5);
+ task::spawn(async move {
+ start_requests(10, QoS::AtLeastOnce, 1, client).await;
+ });
+
+ // start the eventloop
+ task::spawn(async move {
+ run(&mut eventloop, false).await.unwrap();
+ });
+
+ let mut broker = Broker::new(1887, 0).await;
+ for i in 1..=10 {
+ let packet = broker.read_publish().await;
+
+ if i > inflight {
+ assert!(packet.is_none());
+ }
+ }
+}
+
+#[tokio::test]
+async fn requests_are_recovered_after_inflight_queue_size_falls_below_max() {
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 1888);
+ options.set_inflight(3);
+
+ let (client, mut eventloop) = AsyncClient::new(options, 5);
+
+ task::spawn(async move {
+ start_requests(5, QoS::AtLeastOnce, 1, client).await;
+ time::sleep(Duration::from_secs(60)).await;
+ });
+
+ // start the eventloop
+ task::spawn(async move {
+ run(&mut eventloop, true).await.unwrap();
+ });
+
+ let mut broker = Broker::new(1888, 0).await;
+
+ // packet 1, 2, and 3
+ assert!(broker.read_publish().await.is_some());
+ assert!(broker.read_publish().await.is_some());
+ assert!(broker.read_publish().await.is_some());
+
+ // no packet 4. client inflight full as there aren't acks yet
+ assert!(broker.read_publish().await.is_none());
+
+ // ack packet 1 and client would produce packet 4
+ broker.ack(1).await;
+ assert!(broker.read_publish().await.is_some());
+ assert!(broker.read_publish().await.is_none());
+
+ // ack packet 2 and client would produce packet 5
+ broker.ack(2).await;
+ assert!(broker.read_publish().await.is_some());
+ assert!(broker.read_publish().await.is_none());
+}
+
+#[ignore]
+#[tokio::test]
+async fn packet_id_collisions_are_detected_and_flow_control_is_applied() {
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 1891);
+ options.set_inflight(10);
+
+ let (client, mut eventloop) = AsyncClient::new(options, 5);
+
+ task::spawn(async move {
+ start_requests(15, QoS::AtLeastOnce, 0, client).await;
+ time::sleep(Duration::from_secs(60)).await;
+ });
+
+ task::spawn(async move {
+ let mut broker = Broker::new(1891, 0).await;
+
+ // read all incoming packets first
+ for i in 1..=4 {
+ let packet = broker.read_publish().await;
+ assert_eq!(packet.unwrap().payload[0], i);
+ }
+
+ // out of order ack
+ broker.ack(3).await;
+ broker.ack(4).await;
+ time::sleep(Duration::from_secs(5)).await;
+ broker.ack(1).await;
+ broker.ack(2).await;
+
+ // read and ack remaining packets in order
+ for i in 5..=15 {
+ let packet = broker.read_publish().await;
+ let packet = packet.unwrap();
+ assert_eq!(packet.payload[0], i);
+ broker.ack(packet.pkid).await;
+ }
+
+ time::sleep(Duration::from_secs(10)).await;
+ });
+
+ time::sleep(Duration::from_secs(1)).await;
+
+ // sends 4 requests. 5th request will trigger collision
+ // Poll until there is collision.
+ loop {
+ match eventloop.poll().await.unwrap() {
+ Event::Outgoing(Outgoing::AwaitAck(1)) => break,
+ v => {
+ println!("Poll = {v:?}");
+ continue;
+ }
+ }
+ }
+
+ loop {
+ let start = Instant::now();
+ let event = eventloop.poll().await.unwrap();
+ println!("Poll = {event:?}");
+
+ match event {
+ Event::Outgoing(Outgoing::Publish(ack)) => {
+ if ack == 1 {
+ let elapsed = start.elapsed().as_millis() as i64;
+ let deviation_millis: i64 = (5000 - elapsed).abs();
+ assert!(deviation_millis < 100);
+ break;
+ }
+ }
+ _ => continue,
+ }
+ }
+}
+
+// #[tokio::test]
+// async fn packet_id_collisions_are_timedout_on_second_ping() {
+// let mut options = MqttOptions::new("dummy", "127.0.0.1", 1892);
+// options.set_inflight(4).set_keep_alive(5);
+//
+// let mut eventloop = EventLoop::new(options, 5);
+// let requests_tx = eventloop.handle();
+//
+// task::spawn(async move {
+// start_requests(10, QoS::AtLeastOnce, 0, requests_tx).await;
+// time::sleep(Duration::from_secs(60)).await;
+// });
+//
+// task::spawn(async move {
+// let mut broker = Broker::new(1892, 0).await;
+// // read all incoming packets first
+// for i in 1..=4 {
+// let packet = broker.read_publish().await;
+// assert_eq!(packet.unwrap().payload[0], i);
+// }
+//
+// // out of order ack
+// broker.ack(3).await;
+// broker.ack(4).await;
+// time::sleep(Duration::from_secs(15)).await;
+// });
+//
+// time::sleep(Duration::from_secs(1)).await;
+//
+// // Collision error but no network disconneciton
+// match run(&mut eventloop, false).await.unwrap() {
+// Event::Outgoing(Outgoing::AwaitAck(1)) => (),
+// o => panic!("Expecting collision error. Found = {:?}", o),
+// }
+//
+// match run(&mut eventloop, false).await {
+// Err(ConnectionError::MqttState(StateError::CollisionTimeout)) => (),
+// o => panic!("Expecting collision error. Found = {:?}", o),
+// }
+// }
+
+//
+// All reconnection tests here
+//
+#[tokio::test]
+async fn next_poll_after_connect_failure_reconnects() {
+ let options = MqttOptions::new("dummy", "127.0.0.1", 3000);
+
+ task::spawn(async move {
+ let _broker = Broker::new(3000, 1).await;
+ let _broker = Broker::new(3000, 0).await;
+ time::sleep(Duration::from_secs(15)).await;
+ });
+
+ time::sleep(Duration::from_secs(1)).await;
+ let mut eventloop = EventLoop::new(options, 5);
+
+ match eventloop.poll().await {
+ Err(ConnectionError::ConnectionRefused(ConnectReturnCode::BadUserNamePassword)) => (),
+ v => panic!("Expected bad username password error. Found = {:?}", v),
+ }
+
+ match eventloop.poll().await {
+ Ok(Event::Incoming(Packet::ConnAck(ConnAck {
+ code: ConnectReturnCode::Success,
+ session_present: false,
+ }))) => (),
+ v => panic!("Expected ConnAck Success. Found = {:?}", v),
+ }
+}
+
+#[tokio::test]
+async fn reconnection_resumes_from_the_previous_state() {
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 3001);
+ options.set_keep_alive(Duration::from_secs(5));
+
+ // start sending qos0 publishes. Makes sure that there is out activity but no in activity
+ let (client, mut eventloop) = AsyncClient::new(options, 5);
+ task::spawn(async move {
+ start_requests(10, QoS::AtLeastOnce, 1, client).await;
+ time::sleep(Duration::from_secs(10)).await;
+ });
+
+ // start the eventloop
+ task::spawn(async move {
+ run(&mut eventloop, true).await.unwrap();
+ });
+
+ // broker connection 1
+ let mut broker = Broker::new(3001, 0).await;
+ for i in 1..=2 {
+ let packet = broker.read_publish().await.unwrap();
+ assert_eq!(i, packet.payload[0]);
+ broker.ack(packet.pkid).await;
+ }
+
+ // NOTE: An interesting thing to notice here is that reassigning a new broker
+ // is behaving like a half-open connection instead of cleanly closing the socket
+ // and returning error immediately
+ // Manually dropping (`drop(broker.framed)`) the connection or adding
+ // a block around broker with {} is closing the connection as expected
+
+ // broker connection 2
+ let mut broker = Broker::new(3001, 0).await;
+ for i in 3..=4 {
+ let packet = broker.read_publish().await.unwrap();
+ assert_eq!(i, packet.payload[0]);
+ broker.ack(packet.pkid).await;
+ }
+}
+
+#[tokio::test]
+async fn reconnection_resends_unacked_packets_from_the_previous_connection_first() {
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 3002);
+ options.set_keep_alive(Duration::from_secs(5));
+
+ // start sending qos0 publishes. this makes sure that there is
+ // outgoing activity but no incoming activity
+ let (client, mut eventloop) = AsyncClient::new(options, 5);
+ task::spawn(async move {
+ start_requests(10, QoS::AtLeastOnce, 1, client).await;
+ time::sleep(Duration::from_secs(10)).await;
+ });
+
+ // start the client eventloop
+ task::spawn(async move {
+ run(&mut eventloop, true).await.unwrap();
+ });
+
+ // broker connection 1. receive but don't ack
+ let mut broker = Broker::new(3002, 0).await;
+ for i in 1..=2 {
+ let packet = broker.read_publish().await.unwrap();
+ assert_eq!(i, packet.payload[0]);
+ }
+
+ // broker connection 2 receives from scratch
+ let mut broker = Broker::new(3002, 0).await;
+ for i in 1..=6 {
+ let packet = broker.read_publish().await.unwrap();
+ assert_eq!(i, packet.payload[0]);
+ }
+}
+
+#[tokio::test]
+async fn state_is_being_cleaned_properly_and_pending_request_calculated_properly() {
+ let mut options = MqttOptions::new("dummy", "127.0.0.1", 3004);
+ options.set_keep_alive(Duration::from_secs(5));
+ let mut network_options = NetworkOptions::new();
+ network_options.set_tcp_send_buffer_size(1024);
+
+ let (client, mut eventloop) = AsyncClient::new(options, 5);
+ eventloop.set_network_options(network_options);
+ task::spawn(async move {
+ start_requests_with_payload(100, QoS::AtLeastOnce, 0, client, 5000).await;
+ time::sleep(Duration::from_secs(10)).await;
+ });
+
+ task::spawn(async move {
+ let mut broker = Broker::new(3004, 0).await;
+ while (broker.read_packet().await).is_some() {
+ time::sleep(Duration::from_secs_f64(0.5)).await;
+ }
+ });
+
+ let handle = task::spawn(async move {
+ let res = run(&mut eventloop, false).await;
+ if let Err(e) = res {
+ match e {
+ ConnectionError::FlushTimeout => {
+ assert!(eventloop.state.write.is_empty());
+ println!("State is being clean properly");
+ }
+ _ => {
+ println!("Couldn't fill the TCP send buffer to run this test properly. Try reducing the size of buffer.");
+ }
+ }
+ }
+ });
+ handle.await.unwrap();
+}