This patch does a bunch of refactoring, mostly around error types, but it also
paves the way to allow `Codec` to be used standalone.
* `Codec` (and `FramedRead` / `FramedWrite`) is broken out into a codec module.
* An h2-codec crate is created that re-exports the frame and codec modules.
* New error types are introduced in the internals:
  * `RecvError` represents errors caused by trying to receive a frame.
  * `SendError` represents errors caused by trying to send a frame.
  * `UserError` is an enum of potential errors caused by invalid usage
    by the user of the lib.
  * `ProtoError` is either a `Reason` or an `io::Error`. However it doesn't
    specify connection or stream level.
  * `h2::Error` is an opaque error type and is the only error type exposed
    by the public API (used to be `ConnectionError`).
There are misc code changes to enable this as well. The biggest is a new "sink"
API for `Codec`. It provides buffer which queues up a frame followed by flush
which writes everything that is queued. This departs from the `Sink` trait in
order to provide more accurate error values. For example, buffer can never fail
(but it will panic if `poll_ready` is not called first).
		
	
		
			
				
	
	
		
			63 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use frame::Ping;
 | |
| use proto::*;
 | |
| 
 | |
| use std::io;
 | |
| 
 | |
| /// Acknowledges ping requests from the remote.
 | |
| #[derive(Debug)]
 | |
| pub struct PingPong<B> {
 | |
|     // TODO: this doesn't need to save the entire frame
 | |
|     sending_pong: Option<Frame<B>>,
 | |
|     received_pong: Option<PingPayload>,
 | |
|     // TODO: factor this out
 | |
|     blocked_ping: Option<task::Task>,
 | |
| }
 | |
| 
 | |
| impl<B> PingPong<B>
 | |
|     where B: Buf,
 | |
| {
 | |
|     pub fn new() -> Self {
 | |
|         PingPong {
 | |
|             sending_pong: None,
 | |
|             received_pong: None,
 | |
|             blocked_ping: None,
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /// Process a ping
 | |
|     pub fn recv_ping(&mut self, ping: Ping) {
 | |
|         // The caller should always check that `send_pongs` returns ready before
 | |
|         // calling `recv_ping`.
 | |
|         assert!(self.sending_pong.is_none());
 | |
| 
 | |
|         if ping.is_ack() {
 | |
|             // Save acknowledgements to be returned from take_pong().
 | |
|             self.received_pong = Some(ping.into_payload());
 | |
| 
 | |
|             if let Some(task) = self.blocked_ping.take() {
 | |
|                 task.notify();
 | |
|             }
 | |
|         } else {
 | |
|             // Save the ping's payload to be sent as an acknowledgement.
 | |
|             let pong = Ping::pong(ping.into_payload());
 | |
|             self.sending_pong = Some(pong.into());
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /// Send any pending pongs.
 | |
|     pub fn send_pending_pong<T>(&mut self, dst: &mut Codec<T, B>) -> Poll<(), io::Error>
 | |
|         where T: AsyncWrite,
 | |
|     {
 | |
|         if let Some(pong) = self.sending_pong.take() {
 | |
|             if !dst.poll_ready()?.is_ready() {
 | |
|                 self.sending_pong = Some(pong);
 | |
|                 return Ok(Async::NotReady);
 | |
|             }
 | |
| 
 | |
|             dst.buffer(pong).ok().expect("invalid pong frame");
 | |
|         }
 | |
| 
 | |
|         Ok(Async::Ready(()))
 | |
|     }
 | |
| }
 |