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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//! A non blocking tcp stream wrapper.
//!
//! This module is useful when wanting to use the non blocking feature of a
//! socket, but without having to depend on async.
//!
//! The [`NonBlockStream`] can be obtained from a [`TcpStream`] just with an
//! into() call.
//!
//! Reads [`NonBlockStream::read`] and writes [`NonBlockStream::write`] are
//! called whenever the user has time to check for messages or needs to write,
//! and regardless of the nature of the caller, the IO operations will happen
//! in the background in a separate thread maintained by the [`NonBlockStream`]
//! struct.
pub use Packs;
use ;
/// This is the configuration that is used to determine the versioning of the
/// messages.
///
/// When this is the default, messages emitted will have version 1 and only
/// accept version 1.
///
/// If only one version is converted from from u16 then that is the only
/// version emitted and supported.
///
/// If more options are given to [Versions::new] then the first will be
/// the version being emitted, the other two the minimum and maximum version
/// that will be accepted on read (inclusive).
///
/// ```
/// use pipenet::Versions;
///
/// let v: Versions = Versions::default();
/// let v: Versions = 1.into();
/// let v: Versions = Versions::new(3, 1, 3);
/// ```
/// A non blocking wrapper for a [`TcpStream`].
///
/// Supports [`From<TcpStream>`] so it is built throgh [`Into::into`]. The
/// original stream will be consumed by this process as this instance will now
/// own the stream.
///
/// [`NonBlockStream`] maintains its own IO thread in the background which will
/// be terminated once this instance gets dropped, or if the underlying socket
/// gets closed by returning the original [`std::io::Error`].
///
/// Upon any error returned to the caller of [`NonBlockStream::read`] or
/// [`NonBlockStream::write`], the caller will have to consider the stream to
/// be broken and it is required to drop this instance: the background thread
/// will have been terminated at that point and this [`NonBlockStream`] is now
/// unusable and no other calls to read or write should be made.
///
/// Since it is based on [`TcpStream`], it is sequential and can handle only a
/// single stream. The [`NonBlockStream`] is in a way dual channel, but through
/// means of interleaving read/write buffering. The buffer is changing and it's
/// always the size of the next message being written/read.
///
/// The IO thread will keep processing the stream in the background, but it
/// will also sleep (using [`mio::Poll`]) and wake up when either read or write
/// operations are possible again. Whether that will happen depends on the size
/// of the internal buffers of the [`TcpStream`] being passed from creation.
///
/// The [`TcpStream`] is kept as it is when received in its configuration, with
/// one exception of making it non blocking. During initialization, a call to
/// [`TcpStream::set_nonblocking`] is made and if not successful, it will
/// panic. Make sure to pass in a [`TcpStream`] that is either capable of being
/// set to non blocking, or better yet, set it before converting it onto a
/// [`NonBlockStream`].
///
/// It is expected that the [`TcpStream`] being passsed on creation is already
/// in the connected state.
///
/// The header is 10 bytes and is sent per every message.
/// Take that into consideration for how big the message type should be and if
/// it is advantaging to use this method for transmission.
///
/// Reads and writes will ingest or return boxed instances of the message.
///
/// In order to write to the stream use the [`NonBlockStream::write`]. This
/// will add the message to an internal channel (mpsc).
/// *The call to write does not block.*
///
/// To check if there is a message available call [`NonBlockStream::read`].
/// This will check another internal channel if some message is ready. If none
/// is, then the call to read will return [`None`].
/// *The call to read does not block.*
///
/// The [`NonBlockStream`] can be cloned and is [`Send`] and [`Sync`] so it can
/// be used across frameworks that require it.
///
/// ```no_run
/// use std::net::{TcpStream, SocketAddr};
/// use pipenet::NonBlockStream;
///
/// let stream = TcpStream::connect(SocketAddr::from(([127, 0, 0, 1], 9999))).unwrap();
/// let mut nbstream: NonBlockStream = stream.into();
///
/// // A simple, one time, echo example
/// if let Some(msg) = nbstream.read().unwrap() {
/// nbstream.write(vec![1,2,3]).unwrap();
/// }
/// ```
///
/// Versioning is supported. Marks the current version, and discards versions
/// that are outside the min/max version range.
///
/// To add more wrappers on the message, such as encryption or compresssion,
/// use the constructor with the encapsulations.
///
/// To use those methods the features "compression" and/or "encryption" will be
/// required.
///
/// ```ignore
/// use std::net::{TcpStream, SocketAddr};
/// use pipenet::NonBlockStream;
/// use pipenet::Versions;
/// use pipenet::Packs;
///
/// let msg = Msg { data:vec![] };
/// let key = &[0u8; 32];
/// let stream = TcpStream::connect(SocketAddr::from(([127, 0, 0, 1], 9999))).unwrap();
/// let mut nbstream: NonBlockStream<Msg> = NonBlockStream::from_version_encapsulations(
/// Versions::new(2, 1, 3), // Current version 2, supports from 1 to 3
/// Packs::default()
/// .compress()
/// .encrypt(key),
/// stream);
///
/// // A simple, one time, echo example
/// if let Some(msg) = nbstream.read().unwrap() {
/// nbstream.write(vec![1,2,3]).unwrap();
/// }
/// ```