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 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
use std::time::Duration;
use std::{fmt, io};
use async_trait::async_trait;
mod framed;
pub use framed::*;
mod inmemory;
pub use inmemory::*;
mod tcp;
pub use tcp::*;
#[cfg(test)]
mod test;
#[cfg(test)]
pub use test::*;
#[cfg(unix)]
mod unix;
#[cfg(unix)]
pub use unix::*;
#[cfg(windows)]
mod windows;
pub use tokio::io::{Interest, Ready};
#[cfg(windows)]
pub use windows::*;
/// Duration to wait after WouldBlock received during looping operations like `read_exact`.
const SLEEP_DURATION: Duration = Duration::from_millis(1);
/// Interface representing a connection that is reconnectable.
#[async_trait]
pub trait Reconnectable {
/// Attempts to reconnect an already-established connection.
async fn reconnect(&mut self) -> io::Result<()>;
}
/// Interface representing a transport of raw bytes into and out of the system.
#[async_trait]
pub trait Transport: Reconnectable + fmt::Debug + Send + Sync {
/// Tries to read data from the transport into the provided buffer, returning how many bytes
/// were read.
///
/// This call may return an error with [`ErrorKind::WouldBlock`] in the case that the transport
/// is not ready to read data.
///
/// [`ErrorKind::WouldBlock`]: io::ErrorKind::WouldBlock
fn try_read(&self, buf: &mut [u8]) -> io::Result<usize>;
/// Try to write a buffer to the transport, returning how many bytes were written.
///
/// This call may return an error with [`ErrorKind::WouldBlock`] in the case that the transport
/// is not ready to write data.
///
/// [`ErrorKind::WouldBlock`]: io::ErrorKind::WouldBlock
fn try_write(&self, buf: &[u8]) -> io::Result<usize>;
/// Waits for the transport to be ready based on the given interest, returning the ready
/// status.
async fn ready(&self, interest: Interest) -> io::Result<Ready>;
}
#[async_trait]
impl Transport for Box<dyn Transport> {
fn try_read(&self, buf: &mut [u8]) -> io::Result<usize> {
Transport::try_read(AsRef::as_ref(self), buf)
}
fn try_write(&self, buf: &[u8]) -> io::Result<usize> {
Transport::try_write(AsRef::as_ref(self), buf)
}
async fn ready(&self, interest: Interest) -> io::Result<Ready> {
Transport::ready(AsRef::as_ref(self), interest).await
}
}
#[async_trait]
impl Reconnectable for Box<dyn Transport> {
async fn reconnect(&mut self) -> io::Result<()> {
Reconnectable::reconnect(AsMut::as_mut(self)).await
}
}
#[async_trait]
pub trait TransportExt {
/// Waits for the transport to be readable to follow up with `try_read`.
async fn readable(&self) -> io::Result<()>;
/// Waits for the transport to be writeable to follow up with `try_write`.
async fn writeable(&self) -> io::Result<()>;
/// Waits for the transport to be either readable or writeable.
async fn readable_or_writeable(&self) -> io::Result<()>;
/// Reads exactly `n` bytes where `n` is the length of `buf` by continuing to call [`try_read`]
/// until completed. Calls to [`readable`] are made to ensure the transport is ready. Returns
/// the total bytes read.
///
/// [`try_read`]: Transport::try_read
/// [`readable`]: Transport::readable
async fn read_exact(&self, buf: &mut [u8]) -> io::Result<usize>;
/// Reads all bytes until EOF in this source, placing them into `buf`.
///
/// All bytes read from this source will be appended to the specified buffer `buf`. This
/// function will continuously call [`try_read`] to append more data to `buf` until
/// [`try_read`] returns either [`Ok(0)`] or an error that is neither [`Interrupted`] or
/// [`WouldBlock`].
///
/// If successful, this function will return the total number of bytes read.
///
/// ### Errors
///
/// If this function encounters an error of the kind [`Interrupted`] or [`WouldBlock`], then
/// the error is ignored and the operation will continue.
///
/// If any other read error is encountered then this function immediately returns. Any bytes
/// which have already been read will be appended to `buf`.
///
/// [`Ok(0)`]: Ok
/// [`try_read`]: Transport::try_read
/// [`readable`]: Transport::readable
async fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize>;
/// Reads all bytes until EOF in this source, placing them into `buf`.
///
/// If successful, this function will return the total number of bytes read.
///
/// ### Errors
///
/// If the data in this stream is *not* valid UTF-8 then an error is returned and `buf` is
/// unchanged.
///
/// See [`read_to_end`] for other error semantics.
///
/// [`Ok(0)`]: Ok
/// [`try_read`]: Transport::try_read
/// [`readable`]: Transport::readable
/// [`read_to_end`]: TransportExt::read_to_end
async fn read_to_string(&self, buf: &mut String) -> io::Result<usize>;
/// Writes all of `buf` by continuing to call [`try_write`] until completed. Calls to
/// [`writeable`] are made to ensure the transport is ready.
///
/// [`try_write`]: Transport::try_write
/// [`writable`]: Transport::writable
async fn write_all(&self, buf: &[u8]) -> io::Result<()>;
}
#[async_trait]
impl<T: Transport> TransportExt for T {
async fn readable(&self) -> io::Result<()> {
self.ready(Interest::READABLE).await?;
Ok(())
}
async fn writeable(&self) -> io::Result<()> {
self.ready(Interest::WRITABLE).await?;
Ok(())
}
async fn readable_or_writeable(&self) -> io::Result<()> {
self.ready(Interest::READABLE | Interest::WRITABLE).await?;
Ok(())
}
async fn read_exact(&self, buf: &mut [u8]) -> io::Result<usize> {
let mut i = 0;
while i < buf.len() {
self.readable().await?;
match self.try_read(&mut buf[i..]) {
// If we get 0 bytes read, this usually means that the underlying reader
// has closed, so we will return an EOF error to reflect that
//
// NOTE: `try_read` can also return 0 if the buf len is zero, but because we check
// that our index is < len, the situation where we call try_read with a buf
// of len 0 will never happen
Ok(0) => return Err(io::Error::from(io::ErrorKind::UnexpectedEof)),
Ok(n) => i += n,
// Because we are using `try_read`, it can be possible for it to return
// WouldBlock; so, if we encounter that then we just wait for next readable
Err(x) if x.kind() == io::ErrorKind::WouldBlock => {
// NOTE: We sleep for a little bit before trying again to avoid pegging CPU
tokio::time::sleep(SLEEP_DURATION).await
}
Err(x) => return Err(x),
}
}
Ok(i)
}
async fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
let mut i = 0;
let mut tmp = [0u8; 1024];
loop {
self.readable().await?;
match self.try_read(&mut tmp) {
Ok(0) => return Ok(i),
Ok(n) => {
buf.extend_from_slice(&tmp[..n]);
i += n;
}
Err(x)
if x.kind() == io::ErrorKind::WouldBlock
|| x.kind() == io::ErrorKind::Interrupted =>
{
// NOTE: We sleep for a little bit before trying again to avoid pegging CPU
tokio::time::sleep(SLEEP_DURATION).await
}
Err(x) => return Err(x),
}
}
}
async fn read_to_string(&self, buf: &mut String) -> io::Result<usize> {
let mut tmp = Vec::new();
let n = self.read_to_end(&mut tmp).await?;
buf.push_str(
&String::from_utf8(tmp).map_err(|x| io::Error::new(io::ErrorKind::InvalidData, x))?,
);
Ok(n)
}
async fn write_all(&self, buf: &[u8]) -> io::Result<()> {
let mut i = 0;
while i < buf.len() {
self.writeable().await?;
match self.try_write(&buf[i..]) {
// If we get 0 bytes written, this usually means that the underlying writer
// has closed, so we will return a write zero error to reflect that
//
// NOTE: `try_write` can also return 0 if the buf len is zero, but because we check
// that our index is < len, the situation where we call try_write with a buf
// of len 0 will never happen
Ok(0) => return Err(io::Error::from(io::ErrorKind::WriteZero)),
Ok(n) => i += n,
// Because we are using `try_write`, it can be possible for it to return
// WouldBlock; so, if we encounter that then we just wait for next writeable
Err(x) if x.kind() == io::ErrorKind::WouldBlock => {
// NOTE: We sleep for a little bit before trying again to avoid pegging CPU
tokio::time::sleep(SLEEP_DURATION).await
}
Err(x) => return Err(x),
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use test_log::test;
use super::*;
#[test(tokio::test)]
async fn read_exact_should_fail_if_try_read_encounters_error_other_than_would_block() {
let transport = TestTransport {
f_try_read: Box::new(|_| Err(io::Error::from(io::ErrorKind::NotConnected))),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
let mut buf = [0; 1];
assert_eq!(
transport.read_exact(&mut buf).await.unwrap_err().kind(),
io::ErrorKind::NotConnected
);
}
#[test(tokio::test)]
async fn read_exact_should_fail_if_try_read_returns_0_before_necessary_bytes_read() {
let transport = TestTransport {
f_try_read: Box::new(|_| Ok(0)),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
let mut buf = [0; 1];
assert_eq!(
transport.read_exact(&mut buf).await.unwrap_err().kind(),
io::ErrorKind::UnexpectedEof
);
}
#[test(tokio::test)]
async fn read_exact_should_continue_to_call_try_read_until_buffer_is_filled() {
let transport = TestTransport {
f_try_read: Box::new(|buf| {
static mut CNT: u8 = 0;
unsafe {
buf[0] = b'a' + CNT;
CNT += 1;
}
Ok(1)
}),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
let mut buf = [0; 3];
assert_eq!(transport.read_exact(&mut buf).await.unwrap(), 3);
assert_eq!(&buf, b"abc");
}
#[test(tokio::test)]
async fn read_exact_should_continue_to_call_try_read_while_it_returns_would_block() {
// Configure `try_read` to alternate between reading a byte and WouldBlock
let transport = TestTransport {
f_try_read: Box::new(|buf| {
static mut CNT: u8 = 0;
unsafe {
buf[0] = b'a' + CNT;
CNT += 1;
if CNT % 2 == 1 {
Ok(1)
} else {
Err(io::Error::from(io::ErrorKind::WouldBlock))
}
}
}),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
let mut buf = [0; 3];
assert_eq!(transport.read_exact(&mut buf).await.unwrap(), 3);
assert_eq!(&buf, b"ace");
}
#[test(tokio::test)]
async fn read_exact_should_return_0_if_given_a_buffer_of_0_len() {
let transport = TestTransport {
f_try_read: Box::new(|_| Err(io::Error::from(io::ErrorKind::NotConnected))),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
let mut buf = [0; 0];
assert_eq!(transport.read_exact(&mut buf).await.unwrap(), 0);
}
#[test(tokio::test)]
async fn read_to_end_should_fail_if_try_read_encounters_error_other_than_would_block_and_interrupt(
) {
let transport = TestTransport {
f_try_read: Box::new(|_| Err(io::Error::from(io::ErrorKind::NotConnected))),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
assert_eq!(
transport
.read_to_end(&mut Vec::new())
.await
.unwrap_err()
.kind(),
io::ErrorKind::NotConnected
);
}
#[test(tokio::test)]
async fn read_to_end_should_read_until_0_bytes_returned_from_try_read() {
let transport = TestTransport {
f_try_read: Box::new(|buf| {
static mut CNT: u8 = 0;
unsafe {
if CNT == 0 {
buf[..5].copy_from_slice(b"hello");
CNT += 1;
Ok(5)
} else {
Ok(0)
}
}
}),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
let mut buf = Vec::new();
assert_eq!(transport.read_to_end(&mut buf).await.unwrap(), 5);
assert_eq!(buf, b"hello");
}
#[test(tokio::test)]
async fn read_to_end_should_continue_reading_when_interrupt_or_would_block_encountered() {
let transport = TestTransport {
f_try_read: Box::new(|buf| {
static mut CNT: u8 = 0;
unsafe {
CNT += 1;
if CNT == 1 {
buf[..6].copy_from_slice(b"hello ");
Ok(6)
} else if CNT == 2 {
Err(io::Error::from(io::ErrorKind::WouldBlock))
} else if CNT == 3 {
buf[..5].copy_from_slice(b"world");
Ok(5)
} else if CNT == 4 {
Err(io::Error::from(io::ErrorKind::Interrupted))
} else if CNT == 5 {
buf[..6].copy_from_slice(b", test");
Ok(6)
} else {
Ok(0)
}
}
}),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
let mut buf = Vec::new();
assert_eq!(transport.read_to_end(&mut buf).await.unwrap(), 17);
assert_eq!(buf, b"hello world, test");
}
#[test(tokio::test)]
async fn read_to_string_should_fail_if_try_read_encounters_error_other_than_would_block_and_interrupt(
) {
let transport = TestTransport {
f_try_read: Box::new(|_| Err(io::Error::from(io::ErrorKind::NotConnected))),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
assert_eq!(
transport
.read_to_string(&mut String::new())
.await
.unwrap_err()
.kind(),
io::ErrorKind::NotConnected
);
}
#[test(tokio::test)]
async fn read_to_string_should_fail_if_non_utf8_characters_read() {
let transport = TestTransport {
f_try_read: Box::new(|buf| {
static mut CNT: u8 = 0;
unsafe {
if CNT == 0 {
buf[0] = 0;
buf[1] = 159;
buf[2] = 146;
buf[3] = 150;
CNT += 1;
Ok(4)
} else {
Ok(0)
}
}
}),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
let mut buf = String::new();
assert_eq!(
transport.read_to_string(&mut buf).await.unwrap_err().kind(),
io::ErrorKind::InvalidData
);
}
#[test(tokio::test)]
async fn read_to_string_should_read_until_0_bytes_returned_from_try_read() {
let transport = TestTransport {
f_try_read: Box::new(|buf| {
static mut CNT: u8 = 0;
unsafe {
if CNT == 0 {
buf[..5].copy_from_slice(b"hello");
CNT += 1;
Ok(5)
} else {
Ok(0)
}
}
}),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
let mut buf = String::new();
assert_eq!(transport.read_to_string(&mut buf).await.unwrap(), 5);
assert_eq!(buf, "hello");
}
#[test(tokio::test)]
async fn read_to_string_should_continue_reading_when_interrupt_or_would_block_encountered() {
let transport = TestTransport {
f_try_read: Box::new(|buf| {
static mut CNT: u8 = 0;
unsafe {
CNT += 1;
if CNT == 1 {
buf[..6].copy_from_slice(b"hello ");
Ok(6)
} else if CNT == 2 {
Err(io::Error::from(io::ErrorKind::WouldBlock))
} else if CNT == 3 {
buf[..5].copy_from_slice(b"world");
Ok(5)
} else if CNT == 4 {
Err(io::Error::from(io::ErrorKind::Interrupted))
} else if CNT == 5 {
buf[..6].copy_from_slice(b", test");
Ok(6)
} else {
Ok(0)
}
}
}),
f_ready: Box::new(|_| Ok(Ready::READABLE)),
..Default::default()
};
let mut buf = String::new();
assert_eq!(transport.read_to_string(&mut buf).await.unwrap(), 17);
assert_eq!(buf, "hello world, test");
}
#[test(tokio::test)]
async fn write_all_should_fail_if_try_write_encounters_error_other_than_would_block() {
let transport = TestTransport {
f_try_write: Box::new(|_| Err(io::Error::from(io::ErrorKind::NotConnected))),
f_ready: Box::new(|_| Ok(Ready::WRITABLE)),
..Default::default()
};
assert_eq!(
transport.write_all(b"abc").await.unwrap_err().kind(),
io::ErrorKind::NotConnected
);
}
#[test(tokio::test)]
async fn write_all_should_fail_if_try_write_returns_0_before_all_bytes_written() {
let transport = TestTransport {
f_try_write: Box::new(|_| Ok(0)),
f_ready: Box::new(|_| Ok(Ready::WRITABLE)),
..Default::default()
};
assert_eq!(
transport.write_all(b"abc").await.unwrap_err().kind(),
io::ErrorKind::WriteZero
);
}
#[test(tokio::test)]
async fn write_all_should_continue_to_call_try_write_until_all_bytes_written() {
// Configure `try_write` to alternate between writing a byte and WouldBlock
let transport = TestTransport {
f_try_write: Box::new(|buf| {
static mut CNT: u8 = 0;
unsafe {
assert_eq!(buf[0], b'a' + CNT);
CNT += 1;
Ok(1)
}
}),
f_ready: Box::new(|_| Ok(Ready::WRITABLE)),
..Default::default()
};
transport.write_all(b"abc").await.unwrap();
}
#[test(tokio::test)]
async fn write_all_should_continue_to_call_try_write_while_it_returns_would_block() {
// Configure `try_write` to alternate between writing a byte and WouldBlock
let transport = TestTransport {
f_try_write: Box::new(|buf| {
static mut CNT: u8 = 0;
unsafe {
if CNT % 2 == 0 {
assert_eq!(buf[0], b'a' + CNT);
CNT += 1;
Ok(1)
} else {
CNT += 1;
Err(io::Error::from(io::ErrorKind::WouldBlock))
}
}
}),
f_ready: Box::new(|_| Ok(Ready::WRITABLE)),
..Default::default()
};
transport.write_all(b"ace").await.unwrap();
}
#[test(tokio::test)]
async fn write_all_should_return_immediately_if_given_buffer_of_0_len() {
let transport = TestTransport {
f_try_write: Box::new(|_| Err(io::Error::from(io::ErrorKind::NotConnected))),
f_ready: Box::new(|_| Ok(Ready::WRITABLE)),
..Default::default()
};
// No error takes place as we never call try_write
let buf = [0; 0];
transport.write_all(&buf).await.unwrap();
}
}