Skip to main content

deboa_compio/client/http/
http2.rs

1use crate::client::http::conn::{BaseHttpConnection, Http2Connection};
2use compio::net::TcpStream;
3use cyper_core::{CompioExecutor, HyperStream};
4use deboa::{
5    conn::{HttpConnection, ProtoConnection},
6    errors::{ConnectionError, DeboaError},
7    request::Http2Request,
8    Result,
9};
10use http::version::Version;
11use hyper::client::conn::http2::handshake;
12
13impl HttpConnection for Http2Connection {
14    type Sender = Http2Request;
15    fn sender(&mut self) -> &mut Self::Sender {
16        &mut self.sender
17    }
18}
19
20impl ProtoConnection for Http2Connection {
21    type Connection = Http2Connection;
22    type RuntimeStream = HyperStream<TcpStream>;
23
24    #[inline]
25    fn protocol_version(&self) -> Version {
26        Version::HTTP_2
27    }
28
29    async fn connect(stream: HyperStream<TcpStream>) -> Result<Self::Connection> {
30        let (sender, conn) = handshake(CompioExecutor, stream)
31            .await
32            .map_err(|e| {
33                DeboaError::Connection(ConnectionError::Handshake { message: e.to_string() })
34            })?;
35
36        compio::runtime::spawn(async move {
37            match conn.await {
38                Ok(_) => (),
39                Err(err) => {
40                    log::error!("Error: {:#}", err)
41                }
42            };
43        })
44        .detach();
45
46        Ok(BaseHttpConnection::new(sender))
47    }
48}