Sending request and receiving data with mio

Hi all,
I want to implement simple client with mio. According to the documents I should create a Poll and register the events. My question is after connecting to the server how should I got user data and send request to the server. All examples that I saw using simple static one shot request, but I want to get data from user interactively and send request to the server. Here is the pseudo code. Should I register a shared file descriptor in the Poll?

loop {
  get_user_data();
  prepare_request();
  send_data();
  read_response();
}

I just found the mio::channel. Is it a good practice to share a channel between thread that accept user data and mio event loop?

// accept user data thread
loop {
  get_user_data(); 
  prepare_request();
  write_data_to_shared_channel();
}

// mio event loop
loop {
  read_data_from_shared_channel();
  send_data();
}

Why do you want to use mio directly? It's usually not recommend since it's the hard way. The mio provides multiplexed IO and that's all. You need to build an state machine to handle IO event, which makes it really hard to write linear-looking code.

Is there some reason to block you to use either synchronous IO or async runtime like tokio which allows you to write asynchronous but linear-looking code?

I want to use the quiche library, and I should provide I/O and event loop according to their documentations.

The application is responsible for providing I/O (e.g. sockets handling) as well as an event loop with support for timers.

TBH, using quiche directly is not an easy task for beginners unless you have prior experience using multiplexed IO like epoll. It just work on neither synchronous nor asynchronous way. If I have to choose one, I'll use the async way as the quiche is not thread safe but you can run multiple tasks within a single thread. But to use async you may need to learn it which takes its own time.

However, if you want to use quiche just because to make HTTP3 request, consider send HTTP2 request instead. Things got a lot easier with this route - use reqwest::blocking in a loop.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.