1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! Helpers for categorizing [`OvpnMessage`]s into responses and notifications.
//!
//! The raw codec yields [`OvpnMessage`] variants, and callers typically
//! need to branch on "is this a response to a command I sent?" vs. "is
//! this an asynchronous notification?". This module provides
//! [`ManagementEvent`] (the two-variant enum) and [`ClassifyExt`] (an
//! extension trait that adds [`.classify()`](ClassifyExt::classify) to
//! any stream of codec results).
//!
//! # Notification interleaving
//!
//! [`ManagementEvent::Notification`] can appear **between** sending a
//! command and receiving its [`ManagementEvent::Response`]. Consumers
//! should always handle both variants in their stream loop — do not
//! assume the next item after sending a command will be its response.
//!
//! # Example
//!
//! ```no_run
//! use tokio::net::TcpStream;
//! use tokio_util::codec::Framed;
//! use futures::{SinkExt, StreamExt};
//! use openvpn_mgmt_codec::{OvpnCodec, OvpnCommand, StatusFormat};
//! use openvpn_mgmt_codec::stream::{ManagementEvent, ClassifyExt};
//!
//! # async fn example() -> anyhow::Result<()> {
//! let stream = TcpStream::connect("127.0.0.1:7505").await?;
//! let framed = Framed::new(stream, OvpnCodec::new());
//! let (mut sink, raw_stream) = framed.split();
//!
//! let mut mgmt = raw_stream.classify();
//!
//! sink.send(OvpnCommand::Status(StatusFormat::V3)).await?;
//!
//! while let Some(event) = mgmt.next().await {
//! match event? {
//! ManagementEvent::Notification(notification) => {
//! println!("async notification: {notification:?}");
//! }
//! ManagementEvent::Response(msg) => {
//! println!("command response: {msg:?}");
//! }
//! }
//! }
//! # Ok(())
//! # }
//! ```
use io;
use Pin;
use ;
use Stream;
use pin_project;
use crate;
/// A management-interface event, categorized as either a command response
/// or an asynchronous notification.
pin_project!
/// Extension trait that adds [`.classify()`](ClassifyExt::classify) to
/// any stream of `Result<OvpnMessage, io::Error>`.
///
/// # Example
///
/// ```no_run
/// use anyhow::Context;
/// # async fn example() -> anyhow::Result<()> {
/// use tokio::net::TcpStream;
/// use tokio_util::codec::Framed;
/// use futures::{SinkExt, StreamExt};
/// use openvpn_mgmt_codec::{OvpnCodec, OvpnCommand};
///
/// let stream = TcpStream::connect("127.0.0.1:7505").await?;
/// let mut framed = Framed::new(stream, OvpnCodec::new());
///
/// // Send a command and read the response with a timeout.
/// framed.send(OvpnCommand::Pid).await?;
/// let response = tokio::time::timeout(
/// std::time::Duration::from_secs(5),
/// framed.next(),
/// ).await
/// .context("stream ended")?;
///
/// println!("got: {response:?}");
/// # Ok(())
/// # }
/// ```
///
/// # Reconnection with backoff
///
/// ```no_run
/// # async fn example() -> anyhow::Result<()> {
/// use tokio::net::TcpStream;
/// use tokio_util::codec::Framed;
/// use futures::StreamExt;
/// use openvpn_mgmt_codec::{OvpnCodec, OvpnMessage};
///
/// let mut backoff = std::time::Duration::from_secs(1);
/// loop {
/// match TcpStream::connect("127.0.0.1:7505").await {
/// Ok(stream) => {
/// backoff = std::time::Duration::from_secs(1); // reset
/// let mut framed = Framed::new(stream, OvpnCodec::new());
/// while let Some(msg) = framed.next().await {
/// match msg {
/// Ok(msg) => println!("{msg:?}"),
/// Err(error) => { eprintln!("decode error: {error}"); break; }
/// }
/// }
/// eprintln!("connection closed, reconnecting...");
/// }
/// Err(error) => {
/// eprintln!("connect failed: {error}, retrying in {backoff:?}");
/// }
/// }
/// tokio::time::sleep(backoff).await;
/// backoff = (backoff * 2).min(std::time::Duration::from_secs(30));
/// }
/// # }
/// ```
///
/// # Detecting connection loss via `>FATAL:`
///
/// ```no_run
/// # async fn example() -> anyhow::Result<()> {
/// use tokio::net::TcpStream;
/// use tokio_util::codec::Framed;
/// use futures::StreamExt;
/// use openvpn_mgmt_codec::{OvpnCodec, OvpnMessage, Notification};
///
/// let stream = TcpStream::connect("127.0.0.1:7505").await?;
/// let mut framed = Framed::new(stream, OvpnCodec::new());
///
/// while let Some(msg) = framed.next().await {
/// match msg? {
/// OvpnMessage::Notification(Notification::Fatal { message }) => {
/// eprintln!("OpenVPN fatal: {message}");
/// // Trigger graceful shutdown / reconnection.
/// break;
/// }
/// other => println!("{other:?}"),
/// }
/// }
/// // Stream ended — either FATAL or the daemon closed the connection.
/// // In both cases, you should reconnect (see reconnection example above).
/// # Ok(())
/// # }
/// ```