@@ -24,9 +24,8 @@ For a sync approach the stream has to implement the [`std::io::Read`] and [`std:
24
24
- Keep alive depends on actual communication
25
25
26
26
### To do
27
- - no_std (Requires a lot of work to use no heap allocations and depend on stack)
28
27
- Even More testing
29
- - More documentation
28
+ - Add TLS examples to repository
30
29
31
30
## MSRV
32
31
From 0.3 the tokio and smol variants will require MSRV: 1.75 due to async fn in trait feature.
@@ -38,159 +37,121 @@ From 0.3 the tokio and smol variants will require MSRV: 1.75 due to async fn in
38
37
- Create a new connection when an error or disconnect is encountered
39
38
- Handlers only get incoming packets
40
39
41
- ### TLS:
42
- TLS examples are too larger for a README. [ TLS examples] ( https://github.com/GunnarMorrigan/mqrstt/tree/main/examples ) .
43
40
44
41
### Smol example:
45
42
``` rust
46
43
use mqrstt :: {
47
- MqttClient ,
48
- ConnectOptions ,
49
- new_smol,
50
44
packets :: {self , Packet },
51
- AsyncEventHandler ,
52
- smol :: NetworkStatus ,
45
+ AsyncEventHandler , MqttClient , NetworkBuilder , NetworkStatus ,
53
46
};
54
- use bytes :: Bytes ;
55
47
pub struct PingPong {
56
48
pub client : MqttClient ,
57
49
}
58
50
impl AsyncEventHandler for PingPong {
59
51
// Handlers only get INCOMING packets. This can change later.
60
- async fn handle (& mut self , event : packets :: Packet {
52
+ async fn handle (& mut self , event : packets :: Packet ) {
61
53
match event {
62
54
Packet :: Publish (p ) => {
63
55
if let Ok (payload ) = String :: from_utf8 (p . payload. to_vec ()) {
64
56
if payload . to_lowercase (). contains (" ping" ) {
65
- self . client
66
- . publish (
67
- p . topic. clone (),
68
- p . qos,
69
- p . retain,
70
- Bytes :: from_static (b " pong" ),
71
- )
72
- . await
73
- . unwrap ();
57
+ self . client. publish (p . topic. clone (), p . qos, p . retain, b " pong" ). await . unwrap ();
74
58
println! (" Received Ping, Send pong!" );
75
59
}
76
60
}
77
- },
78
- Packet :: ConnAck (_ ) => { println! (" Connected!" ) },
61
+ }
62
+ Packet :: ConnAck (_ ) => {
63
+ println! (" Connected!" )
64
+ }
79
65
_ => (),
80
66
}
81
67
}
82
68
}
83
- smol :: block_on (async {
84
- let options = ConnectOptions :: new (" mqrsttSmolExample" );
85
- let (mut network , client ) = new_smol (options );
86
- let stream = smol :: net :: TcpStream :: connect ((" broker.emqx.io" , 1883 ))
87
- . await
88
- . unwrap ();
89
-
90
- let mut pingpong = PingPong {
91
- client : client . clone (),
92
- };
69
+ fn main () {
70
+ smol :: block_on (async {
71
+ let (mut network , client ) = NetworkBuilder :: new_from_client_id (" mqrsttSmolExample" ). smol_network ();
72
+ let stream = smol :: net :: TcpStream :: connect ((" broker.emqx.io" , 1883 )). await . unwrap ();
93
73
94
- network . connect ( stream , & mut pingpong ) . await . unwrap () ;
74
+ let mut pingpong = PingPong { client : client . clone () } ;
95
75
96
- // This subscribe is only processed when we run the network
97
- client . subscribe (" mqrstt" ). await . unwrap ();
76
+ network . connect (stream , & mut pingpong ). await . unwrap ();
77
+
78
+ // This subscribe is only processed when we run the network
79
+ client . subscribe (" mqrstt" ). await . unwrap ();
80
+
81
+ let task_handle = smol :: spawn (async move {
82
+ let result = network . run (& mut pingpong ). await ;
83
+ (result , pingpong )
84
+ });
85
+
86
+ smol :: Timer :: after (std :: time :: Duration :: from_secs (30 )). await ;
87
+ client . disconnect (). await . unwrap ();
88
+
89
+ let (result , _pingpong ) = task_handle . await ;
90
+
91
+ assert! (result . is_ok ());
92
+ assert_eq! (result . unwrap (), NetworkStatus :: OutgoingDisconnect );
93
+ });
94
+ }
98
95
99
- let (n , t ) = futures :: join! (
100
- async {
101
- loop {
102
- return match network . poll (& mut pingpong ). await {
103
- Ok (NetworkStatus :: Active ) => continue ,
104
- otherwise => otherwise ,
105
- };
106
- }
107
- },
108
- async {
109
- smol :: Timer :: after (std :: time :: Duration :: from_secs (30 )). await ;
110
- client . disconnect (). await . unwrap ();
111
- }
112
- );
113
- assert! (n . is_ok ());
114
- });
115
96
```
116
97
117
98
### Tokio example:
118
99
``` rust
119
100
use mqrstt :: {
120
- MqttClient ,
121
- ConnectOptions ,
122
- new_tokio,
123
101
packets :: {self , Packet },
124
- AsyncEventHandler ,
125
- tokio :: NetworkStatus ,
102
+ AsyncEventHandler , MqttClient , NetworkBuilder , NetworkStatus ,
126
103
};
127
104
use tokio :: time :: Duration ;
128
- use bytes :: Bytes ;
129
105
130
106
pub struct PingPong {
131
107
pub client : MqttClient ,
132
108
}
133
109
impl AsyncEventHandler for PingPong {
134
- // Handlers only get INCOMING packets. This can change later.
110
+ // Handlers only get INCOMING packets.
135
111
async fn handle (& mut self , event : packets :: Packet ) {
136
112
match event {
137
113
Packet :: Publish (p ) => {
138
114
if let Ok (payload ) = String :: from_utf8 (p . payload. to_vec ()) {
139
115
if payload . to_lowercase (). contains (" ping" ) {
140
- self . client
141
- . publish (
142
- p . topic. clone (),
143
- p . qos,
144
- p . retain,
145
- Bytes :: from_static (b " pong" ),
146
- )
147
- . await
148
- . unwrap ();
116
+ self . client. publish (p . topic. clone (), p . qos, p . retain, b " pong" ). await . unwrap ();
149
117
println! (" Received Ping, Send pong!" );
150
118
}
151
119
}
152
- },
153
- Packet :: ConnAck (_ ) => { println! (" Connected!" ) },
120
+ }
121
+ Packet :: ConnAck (_ ) => {
122
+ println! (" Connected!" )
123
+ }
154
124
_ => (),
155
125
}
156
126
}
157
127
}
158
128
159
129
#[tokio:: main]
160
130
async fn main () {
161
- let options = ConnectOptions :: new (" TokioTcpPingPongExample" );
162
-
163
- let (mut network , client ) = new_tokio (options );
164
-
165
- let stream = tokio :: net :: TcpStream :: connect ((" broker.emqx.io" , 1883 ))
166
- . await
167
- . unwrap ();
168
-
169
- let mut pingpong = PingPong {
170
- client : client . clone (),
171
- };
172
-
131
+ let (mut network , client ) = NetworkBuilder :: new_from_client_id (" TokioTcpPingPongExample" ). tokio_network ();
132
+
133
+ let stream = tokio :: net :: TcpStream :: connect ((" broker.emqx.io" , 1883 )). await . unwrap ();
134
+ let stream = tokio :: io :: BufStream :: new (stream );
135
+
136
+ let mut pingpong = PingPong { client : client . clone () };
137
+
173
138
network . connect (stream , & mut pingpong ). await . unwrap ();
174
-
139
+
175
140
client . subscribe (" mqrstt" ). await . unwrap ();
176
-
177
-
178
- let (n , _ ) = tokio :: join! (
179
- async {
180
- loop {
181
- return match network . poll (& mut pingpong ). await {
182
- Ok (NetworkStatus :: Active ) => continue ,
183
- otherwise => otherwise ,
184
- };
185
- }
186
- },
187
- async {
188
- tokio :: time :: sleep (Duration :: from_secs (30 )). await ;
189
- client . disconnect (). await . unwrap ();
190
- }
191
- );
192
- assert! (n . is_ok ());
141
+
142
+ let network_handle = tokio :: spawn (async move {
143
+ let result = network . run (& mut pingpong ). await ;
144
+ (result , pingpong )
145
+ });
146
+
147
+ tokio :: time :: sleep (Duration :: from_secs (30 )). await ;
148
+ client . disconnect (). await . unwrap ();
149
+
150
+ let (result , _pingpong ) = network_handle . await . unwrap ();
151
+ assert! (result . is_ok ());
152
+ assert_eq! (result . unwrap (), NetworkStatus :: OutgoingDisconnect );
193
153
}
154
+
194
155
```
195
156
196
157
### Sync example:
@@ -284,7 +245,6 @@ Licensed under
284
245
* Mozilla Public License, Version 2.0, [ (MPL-2.0)] ( https://choosealicense.com/licenses/mpl-2.0/ )
285
246
286
247
## Contribution
287
-
288
248
Unless you explicitly state otherwise, any contribution intentionally
289
249
submitted for inclusion in the work by you, shall be licensed under MPL-2.0, without any additional terms or
290
250
conditions.
0 commit comments