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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
#![deny(missing_docs, rust_2018_idioms, unused, unused_crate_dependencies, unused_import_braces, unused_lifetimes, unused_qualifications, warnings)]
#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
//! This is `async-proto`, a library crate facilitating simple binary network protocols with `async` support.
//!
//! The main feature is the [`Protocol`] trait, which allows reading a value of an implementing type from an async or sync stream, as well as writing one to an async or sync sink.
//!
//! [`Protocol`] can be derived for `enum`s and `struct`s if all fields implement [`Protocol`].
//!
//! # Features
//!
//! This crate offers optional dependencies on the following crates to enable [`Protocol`] implementations for some of their types:
//!
//! * [`bytes`](https://docs.rs/bytes): [`Bytes`](https://docs.rs/bytes/latest/bytes/struct.Bytes.html)
//! * [`chrono`](https://docs.rs/chrono): [`NaiveDate`](https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html), [`DateTime`](https://docs.rs/chrono/latest/chrono/struct.DateTime.html), [`Utc`](https://docs.rs/chrono/latest/chrono/offset/struct.Utc.html), and [`FixedOffset`](https://docs.rs/chrono/latest/chrono/offset/struct.FixedOffset.html)
//! * [`chrono-tz`](https://docs.rs/chrono-tz): [`Tz`](https://docs.rs/chrono-tz/latest/chrono_tz/enum.Tz.html)
//! * [`either`](https://docs.rs/either): [`Either`](https://docs.rs/either/latest/either/enum.Either.html)
//! * [`enumset`](https://docs.rs/enumset): [`EnumSet`](https://docs.rs/enumset/latest/enumset/struct.EnumSet.html)
//! * [`git2`](https://docs.rs/git2): [`Oid`](https://docs.rs/git2/latest/git2/struct.Oid.html)
//! * [`gix-hash`](https://docs.rs/gix-hash): [`ObjectId`](https://docs.rs/gix-hash/latest/gix_hash/enum.ObjectId.html)
//! * [`noisy_float`](https://docs.rs/noisy_float): [`NoisyFloat`](https://docs.rs/noisy_float/latest/noisy_float/struct.NoisyFloat.html)
//! * [`semver`](https://docs.rs/semver): [`Version`](https://docs.rs/semver/latest/semver/struct.Version.html), [`Prerelease`](https://docs.rs/semver/latest/semver/struct.Prerelease.html), and [`BuildMetadata`](https://docs.rs/semver/latest/semver/struct.BuildMetadata.html)
//! * [`serde_json`](https://docs.rs/serde_json): [`Value`](https://docs.rs/serde_json/latest/serde_json/enum.Value.html), [`Map`](https://docs.rs/serde_json/latest/serde_json/struct.Map.html), and [`Number`](https://docs.rs/serde_json/latest/serde_json/struct.Number.html)
//! * [`uuid`](https://docs.rs/uuid): [`Uuid`](https://docs.rs/uuid/latest/uuid/struct.Uuid.html)
//!
//! Additionally, the following features can be enabled via Cargo:
//!
//! * `tokio-tungstenite`: Adds a dependency on the [`tokio-tungstenite`](https://docs.rs/tokio-tungstenite) crate and convenience methods for reading/writing [`Protocol`] types from/to its websockets.
//! * `tungstenite`: Adds a dependency on the [`tungstenite`](https://docs.rs/tungstenite) crate and convenience methods for synchronously reading/writing [`Protocol`] types from/to its websockets.
use {
std::{
future::Future,
io::{
self,
prelude::*,
},
pin::Pin,
},
tokio::io::{
AsyncRead,
AsyncWrite,
},
};
#[cfg(feature = "tokio-tungstenite")] use {
std::{
iter,
mem,
},
futures::{
Sink,
SinkExt as _,
future::{
self,
Either,
},
stream::{
self,
Stream,
StreamExt as _,
TryStreamExt as _,
},
},
};
#[cfg(any(feature = "tokio-tungstenite", feature = "tungstenite"))] use fallible_collections::FallibleVec as _;
pub use {
async_proto_derive::{
Protocol,
bitflags,
},
crate::error::*,
};
#[doc(hidden)] pub use tokio; // used in proc macro
mod error;
mod impls;
/// The maximum message size that can be sent and received by tokio-tungstenite without errors on the default configuration.
#[cfg(any(feature = "tokio-tungstenite", feature = "tungstenite"))] const WS_MAX_MESSAGE_SIZE: usize = 16777216;
/// This trait allows reading a value of an implementing type from an async or sync stream, as well as writing one to an async or sync sink.
pub trait Protocol: Sized {
/// Reads a value of this type from an async stream.
///
/// # Cancellation safety
///
/// Implementations of this method are generally not cancellation safe.
fn read<'a, R: AsyncRead + Unpin + Send + 'a>(stream: &'a mut R) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>;
/// Writes a value of this type to an async sink.
///
/// # Cancellation safety
///
/// Implementations of this method are generally not cancellation safe.
fn write<'a, W: AsyncWrite + Unpin + Send + 'a>(&'a self, sink: &'a mut W) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>;
/// Reads a value of this type from a sync stream.
fn read_sync(stream: &mut impl Read) -> Result<Self, ReadError>;
/// Writes a value of this type to a sync sink.
fn write_sync(&self, sink: &mut impl Write) -> Result<(), WriteError>;
/// Takes ownership of an async stream, reads a value of this type from it, then returns it along with the stream.
///
/// This can be used to get around drop glue issues that might arise with `read`.
fn read_owned<R: AsyncRead + Unpin + Send + 'static>(mut stream: R) -> Pin<Box<dyn Future<Output = Result<(R, Self), ReadError>> + Send>> {
Box::pin(async move {
let value = Self::read(&mut stream).await?;
Ok((stream, value))
})
}
/// Attempts to read a value of this type from a prefix in a buffer and a suffix in a sync stream.
///
/// If [`io::ErrorKind::WouldBlock`] is encountered, `Ok(None)` is returned and the portion read successfully is appended to `buf`. Otherwise, the prefix representing the returned value is removed from `buf`.
///
/// Callers, not implementations, should ensure that `stream` is non-blocking if desired.
///
/// # Example
///
/// ```
/// use {
/// std::{
/// io,
/// net::TcpStream,
/// },
/// async_proto::Protocol,
/// };
///
/// struct Client {
/// tcp_stream: TcpStream,
/// buf: Vec<u8>,
/// }
///
/// impl Client {
/// fn new(tcp_stream: TcpStream) -> Self {
/// Self {
/// tcp_stream,
/// buf: Vec::default(),
/// }
/// }
///
/// fn try_read<T: Protocol>(&mut self) -> io::Result<Option<T>> {
/// self.tcp_stream.set_nonblocking(true)?;
/// Ok(T::try_read(&mut self.tcp_stream, &mut self.buf)?)
/// }
///
/// fn write<T: Protocol>(&mut self, msg: &T) -> io::Result<()> {
/// self.tcp_stream.set_nonblocking(false)?;
/// msg.write_sync(&mut self.tcp_stream)?;
/// Ok(())
/// }
/// }
/// ```
fn try_read(stream: &mut impl Read, buf: &mut Vec<u8>) -> Result<Option<Self>, ReadError> {
let mut temp_buf = vec![0; 8];
loop {
let mut slice = &mut &**buf;
match Self::read_sync(&mut slice) {
Ok(value) => {
let value_len = slice.len();
buf.drain(..buf.len() - value_len);
return Ok(Some(value))
}
Err(ReadError { kind: ReadErrorKind::Io(e), .. }) if e.kind() == io::ErrorKind::UnexpectedEof => {}
Err(e) => return Err(e),
}
match stream.read(&mut temp_buf) {
Ok(0) => return Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::EndOfStream,
}),
Ok(n) => buf.extend_from_slice(&temp_buf[..n]),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => return Ok(None),
Err(e) => return Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
}),
}
}
}
#[cfg(feature = "tokio-tungstenite")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-tungstenite")))]
/// Reads a value of this type from a `tokio-tungstenite` websocket.
///
/// # Cancellation safety
///
/// The default implementation of this method is cancellation safe.
fn read_ws<'a, R: Stream<Item = Result<tungstenite::Message, tungstenite::Error>> + Unpin + Send + 'a>(stream: &'a mut R) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>> {
Box::pin(async move {
let packet = stream.try_next().await.map_err(|e| ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?.ok_or_else(|| ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::EndOfStream,
})?;
match packet {
tungstenite::Message::Text(data) => match data.chars().next() {
Some('m') => {
let len = data[1..].parse::<usize>().map_err(|e| ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
let mut buf = Vec::try_with_capacity(len).map_err(|e| ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
while buf.len() < len {
let packet = stream.try_next().await.map_err(|e| ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?.ok_or_else(|| ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::EndOfStream,
})?;
if let tungstenite::Message::Binary(data) = packet {
buf.extend_from_slice(&data);
} else {
return Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::MessageKind(packet),
})
}
}
Self::read_sync(&mut &*buf).map_err(|ReadError { context, kind }| ReadError {
context: ErrorContext::WebSocket {
source: Box::new(context),
},
kind,
})
}
c => Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::WebSocketTextMessage(c),
}),
},
tungstenite::Message::Binary(data) => Self::read_sync(&mut &*data).map_err(|ReadError { context, kind }| ReadError {
context: ErrorContext::WebSocket {
source: Box::new(context),
},
kind,
}),
_ => Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::MessageKind(packet),
}),
}
})
}
#[cfg(feature = "tokio-tungstenite")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-tungstenite")))]
/// Writes a value of this type to a `tokio-tungstenite` websocket.
///
/// # Cancellation safety
///
/// The default implementation of this method is not cancellation safe.
fn write_ws<'a, W: Sink<tungstenite::Message, Error = tungstenite::Error> + Unpin + Send + 'a>(&'a self, sink: &'a mut W) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
where Self: Sync {
Box::pin(async move {
let mut buf = Vec::default();
self.write_sync(&mut buf).map_err(|WriteError { context, kind }| WriteError {
context: ErrorContext::WebSocket {
source: Box::new(context),
},
kind,
})?;
if buf.len() <= WS_MAX_MESSAGE_SIZE {
sink.send(tungstenite::Message::binary(buf)).await.map_err(|e| WriteError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
} else {
sink.send(tungstenite::Message::text(format!("m{}", buf.len()))).await.map_err(|e| WriteError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
for chunk in buf.chunks(WS_MAX_MESSAGE_SIZE) {
sink.send(tungstenite::Message::binary(chunk)).await.map_err(|e| WriteError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
}
}
Ok(())
})
}
#[cfg(feature = "tungstenite")]
#[cfg_attr(docsrs, doc(cfg(feature = "tungstenite")))]
/// Reads a value of this type from a [`tungstenite`] websocket.
fn read_ws_sync(websocket: &mut tungstenite::WebSocket<impl Read + Write>) -> Result<Self, ReadError> {
let packet = websocket.read().map_err(|e| ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
match packet {
tungstenite::Message::Text(data) => match data.chars().next() {
Some('m') => {
let len = data[1..].parse::<usize>().map_err(|e| ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
let mut buf = Vec::try_with_capacity(len).map_err(|e| ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
while buf.len() < len {
let packet = websocket.read().map_err(|e| ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
if let tungstenite::Message::Binary(data) = packet {
buf.extend_from_slice(&data);
} else {
return Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::MessageKind(packet),
})
}
}
Self::read_sync(&mut &*buf).map_err(|ReadError { context, kind }| ReadError {
context: ErrorContext::WebSocket {
source: Box::new(context),
},
kind,
})
}
c => return Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::WebSocketTextMessage(c),
}),
},
tungstenite::Message::Binary(data) => Self::read_sync(&mut &*data).map_err(|ReadError { context, kind }| ReadError {
context: ErrorContext::WebSocket {
source: Box::new(context),
},
kind,
}),
_ => Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::MessageKind(packet),
}),
}
}
#[cfg(feature = "tungstenite")]
#[cfg_attr(docsrs, doc(cfg(feature = "tungstenite")))]
/// Writes a value of this type to a [`tungstenite`] websocket.
fn write_ws_sync(&self, websocket: &mut tungstenite::WebSocket<impl Read + Write>) -> Result<(), WriteError> {
let mut buf = Vec::default();
self.write_sync(&mut buf).map_err(|WriteError { context, kind }| WriteError {
context: ErrorContext::WebSocket {
source: Box::new(context),
},
kind,
})?;
if buf.len() <= WS_MAX_MESSAGE_SIZE {
websocket.send(tungstenite::Message::binary(buf)).map_err(|e| WriteError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
} else {
websocket.send(tungstenite::Message::text(format!("m{}", buf.len()))).map_err(|e| WriteError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
for chunk in buf.chunks(WS_MAX_MESSAGE_SIZE) {
websocket.send(tungstenite::Message::binary(chunk)).map_err(|e| WriteError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
}
}
websocket.flush().map_err(|e| WriteError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
Ok(())
}
#[cfg(feature = "tokio-tungstenite")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-tungstenite")))]
/// Takes ownership of an async websocket stream, reads a value of this type from it, then returns it along with the stream.
///
/// This can be used to get around drop glue issues that might arise with `read_ws`.
fn read_ws_owned<R: Stream<Item = Result<tungstenite::Message, tungstenite::Error>> + Unpin + Send + 'static>(mut stream: R) -> Pin<Box<dyn Future<Output = Result<(R, Self), ReadError>> + Send>> {
Box::pin(async move {
let value = Self::read_ws(&mut stream).await?;
Ok((stream, value))
})
}
}
/// Establishes a WebSocket connection to the given URL and returns a typed sink/stream pair.
///
/// Useful for WebSocket connections where the message type per direction is always the same.
#[cfg(feature = "tokio-tungstenite")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-tungstenite")))]
pub async fn websocket<R: Protocol, W: Protocol>(request: impl tungstenite::client::IntoClientRequest + Unpin) -> tungstenite::Result<(impl Sink<W, Error = WriteError>, impl Stream<Item = Result<R, ReadError>>)> {
let (sock, _) = tokio_tungstenite::connect_async(request).await?;
let (sink, stream) = sock.split();
Ok((
sink.sink_map_err(|e| WriteError {
context: ErrorContext::WebSocketSink,
kind: e.into(),
}).with_flat_map::<W, _, _>(|msg| {
let mut buf = Vec::default();
match msg.write_sync(&mut buf) {
Ok(()) => Either::Left(if buf.len() <= WS_MAX_MESSAGE_SIZE {
Either::Left(stream::once(future::ready(tungstenite::Message::binary(buf))))
} else {
Either::Right(stream::iter(
iter::once(tungstenite::Message::text(format!("m{}", buf.len())))
.chain(buf.chunks(WS_MAX_MESSAGE_SIZE).map(tungstenite::Message::binary))
.collect::<Vec<_>>()
))
}.map(Ok)),
Err(WriteError { context, kind }) => Either::Right(stream::once(future::err(WriteError {
context: ErrorContext::WebSocket {
source: Box::new(context),
},
kind,
}))),
}
}),
/*
stream.map_err(|e| ReadError {
context: ErrorContext::WebSocketStream,
kind: e.into(),
}).and_then(|packet| async move {
if !packet.is_binary() {
return Err(ReadError {
context: ErrorContext::WebSocketStream,
kind: ReadErrorKind::MessageKind(packet),
})
}
R::read_sync(&mut &*packet.into_data()).map_err(|ReadError { context, kind }| ReadError {
context: ErrorContext::WebSocket {
source: Box::new(context),
},
kind,
})
}),
*/
stream.scan(None, |state, res| {
fn scanner<R: Protocol>(state: &mut Option<(usize, Vec<u8>)>, res: tungstenite::Result<tungstenite::Message>) -> Result<impl Stream<Item = Result<R, ReadError>>, ReadError> {
let packet = res.map_err(|e| ReadError {
context: ErrorContext::WebSocketStream,
kind: e.into(),
})?;
Ok(if let Some((len, buf)) = state {
if let tungstenite::Message::Binary(data) = packet {
buf.extend_from_slice(&data);
} else {
return Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::MessageKind(packet),
})
}
if buf.len() >= *len {
let buf = mem::take(buf);
*state = None;
Either::Right(stream::once(future::ok(R::read_sync(&mut &*buf).map_err(|ReadError { context, kind }| ReadError {
context: ErrorContext::WebSocket {
source: Box::new(context),
},
kind,
})?)))
} else {
Either::Left(stream::empty())
}
} else {
match packet {
tungstenite::Message::Text(data) => match data.chars().next() {
Some('m') => {
let len = data[1..].parse::<usize>().map_err(|e| ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
let buf = Vec::try_with_capacity(len).map_err(|e| ReadError {
context: ErrorContext::DefaultImpl,
kind: e.into(),
})?;
*state = Some((len, buf));
Either::Left(stream::empty())
}
c => return Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::WebSocketTextMessage(c),
}),
},
tungstenite::Message::Binary(data) => Either::Right(stream::once(future::ok(R::read_sync(&mut &*data).map_err(|ReadError { context, kind }| ReadError {
context: ErrorContext::WebSocket {
source: Box::new(context),
},
kind,
})?))),
_ => return Err(ReadError {
context: ErrorContext::DefaultImpl,
kind: ReadErrorKind::MessageKind(packet),
}),
}
})
}
future::ready(Some(scanner(state, res)))
}).try_flatten(),
))
}