feat: v0.1.0 init commit
This commit is contained in:
39
src/server.rs
Normal file
39
src/server.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use std::fs;
|
||||
|
||||
use rust_util::XResult;
|
||||
use s2n_quic::Server;
|
||||
|
||||
use crate::config::ListenConfig;
|
||||
|
||||
async fn run(listen_config: &ListenConfig) -> XResult<()> {
|
||||
let cert_pem = opt_result!(fs::read_to_string(&listen_config.cert_pem_file),
|
||||
"Read cert pem file: {}, failed: {}", &listen_config.key_pem_file);
|
||||
let key_pem = opt_result!(fs::read_to_string(&listen_config.key_pem_file),
|
||||
"Read key pem file :{}, failed: {}", &listen_config.key_pem_file);
|
||||
|
||||
let mut server = Server::builder()
|
||||
.with_tls((cert_pem.as_str(), key_pem.as_str()))?
|
||||
.with_io(listen_config.listen.as_str())?
|
||||
.start()?;
|
||||
while let Some(mut connection) = server.accept().await {
|
||||
// spawn a new task for the connection
|
||||
tokio::spawn(async move {
|
||||
information!("Connection accepted from {:?}", connection.remote_addr());
|
||||
while let Ok(Some(mut stream)) = connection.accept_bidirectional_stream().await {
|
||||
// spawn a new task for the stream
|
||||
tokio::spawn(async move {
|
||||
information!("Stream opened from {:?}", stream.connection().remote_addr());
|
||||
// echo any data back to the stream
|
||||
// while let Ok(Some(data)) = stream.receive().await {
|
||||
// stream.send(data).await.expect("stream should be open");
|
||||
// }
|
||||
// println!("Stream closed from {:?}", stream.connection().remote_addr());
|
||||
// TODO ...
|
||||
});
|
||||
}
|
||||
println!("Connection closed from {:?}", connection.remote_addr());
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user