forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request torvalds#752 from wedsonaf/echo-server
samples/rust: add echo server sample
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
//! Rust echo server sample. | ||
use kernel::{ | ||
kasync::executor::{workqueue::Executor as WqExecutor, AutoStopHandle, Executor}, | ||
kasync::net::{TcpListener, TcpStream}, | ||
net::{self, Ipv4Addr, SocketAddr, SocketAddrV4}, | ||
prelude::*, | ||
spawn_task, | ||
sync::{Ref, RefBorrow}, | ||
}; | ||
|
||
async fn echo_server(stream: TcpStream) -> Result { | ||
let mut buf = [0u8; 1024]; | ||
loop { | ||
let n = stream.read(&mut buf).await?; | ||
if n == 0 { | ||
return Ok(()); | ||
} | ||
stream.write_all(&buf[..n]).await?; | ||
} | ||
} | ||
|
||
async fn accept_loop(listener: TcpListener, executor: Ref<impl Executor>) { | ||
loop { | ||
if let Ok(stream) = listener.accept().await { | ||
let _ = spawn_task!(executor.as_ref_borrow(), echo_server(stream)); | ||
} | ||
} | ||
} | ||
|
||
fn start_listener(ex: RefBorrow<'_, impl Executor + Send + Sync + 'static>) -> Result { | ||
let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::ANY, 8080)); | ||
let listener = TcpListener::try_new(net::init_ns(), &addr)?; | ||
spawn_task!(ex, accept_loop(listener, ex.into()))?; | ||
Ok(()) | ||
} | ||
|
||
struct RustEchoServer { | ||
_handle: AutoStopHandle<dyn Executor>, | ||
} | ||
|
||
impl kernel::Module for RustEchoServer { | ||
fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> { | ||
let handle = WqExecutor::try_new(kernel::workqueue::system())?; | ||
start_listener(handle.executor())?; | ||
Ok(Self { | ||
_handle: handle.into(), | ||
}) | ||
} | ||
} | ||
|
||
module! { | ||
type: RustEchoServer, | ||
name: b"rust_echo_server", | ||
author: b"Rust for Linux Contributors", | ||
description: b"Rust tcp echo sample", | ||
license: b"GPL v2", | ||
} |