-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
146 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::io::{self, Read}; | ||
|
||
use crate::pool::Connection; | ||
use crate::time::Instant; | ||
use crate::transport::Buffers; | ||
use crate::unit::{Event, Input, Unit}; | ||
use crate::Error; | ||
|
||
pub struct RecvBody { | ||
unit: Unit<'static>, | ||
connection: Connection, | ||
current_time: Box<dyn Fn() -> Instant>, | ||
} | ||
impl RecvBody { | ||
pub(crate) fn new( | ||
unit: Unit<'static>, | ||
connection: Connection, | ||
current_time: impl Fn() -> Instant + 'static, | ||
) -> Self { | ||
RecvBody { | ||
unit, | ||
connection, | ||
current_time: Box::new(current_time), | ||
} | ||
} | ||
|
||
fn do_read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | ||
let buffers = self.connection.borrow_buffers(); | ||
let event = self.unit.poll_event((self.current_time)(), buffers)?; | ||
|
||
let timeout = match event { | ||
Event::AwaitInput { timeout, is_body } => { | ||
assert!(is_body); | ||
timeout | ||
} | ||
_ => unreachable!("expected event AwaitInput"), | ||
}; | ||
|
||
let Buffers { input, .. } = self.connection.await_input(timeout, true)?; | ||
let input_used = | ||
self.unit | ||
.handle_input((self.current_time)(), Input::Input { input }, buf)?; | ||
self.connection.consume_input(input_used); | ||
|
||
let buffers = self.connection.borrow_buffers(); | ||
let event = self.unit.poll_event((self.current_time)(), buffers)?; | ||
|
||
let output_used = match event { | ||
Event::ResponseBody { amount } => amount, | ||
_ => unreachable!("expected event ResponseBody"), | ||
}; | ||
|
||
Ok(output_used) | ||
} | ||
} | ||
|
||
impl Read for RecvBody { | ||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
self.do_read(buf).map_err(|e| e.into_io())?; | ||
|
||
Ok(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.