Skip to content

Commit

Permalink
refactor: use poll-io for h2 example (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah authored Feb 1, 2024
1 parent aec19a0 commit 4f2e9b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
13 changes: 7 additions & 6 deletions examples/h2_client.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
//! Example for using h2 directly.
//! Note: This is only meant for compatible usage.
//! Example code is modified from https://github.com/hyperium/h2/blob/master/examples/client.rs.
use monoio::net::TcpStream;
use monoio_compat::StreamWrapper;
use monoio::{io::IntoPollIo, net::TcpStream};

#[monoio::main]
async fn main() {
let tcp = TcpStream::connect("127.0.0.1:5928").await.unwrap();
let tcp_wrapper = StreamWrapper::new(tcp);
let (mut client, h2) = h2::client::handshake(tcp_wrapper).await.unwrap();
let io = TcpStream::connect("127.0.0.1:5928")
.await
.unwrap()
.into_poll_io()
.unwrap();
let (mut client, h2) = h2::client::handshake(io).await.unwrap();

println!("sending request");

Expand Down
13 changes: 7 additions & 6 deletions examples/h2_server.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Example for using h2 directly.
//! Note: This is only meant for compatible usage.
//! Example code is modified from https://github.com/hyperium/h2/blob/master/examples/server.rs.
use monoio::net::{TcpListener, TcpStream};
use monoio_compat::StreamWrapper;
use monoio::{
io::IntoPollIo,
net::{TcpListener, TcpStream},
};

async fn serve(socket: TcpStream) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let socket_wrapper = StreamWrapper::new(socket);
let mut connection = h2::server::handshake(socket_wrapper).await?;
async fn serve(io: TcpStream) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let io = io.into_poll_io()?;
let mut connection = h2::server::handshake(io).await?;
println!("H2 connection bound");

while let Some(result) = connection.accept().await {
Expand Down

0 comments on commit 4f2e9b4

Please sign in to comment.