Skip to content

Commit

Permalink
Refactor out tcp_echo_task
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalium committed Aug 28, 2024
1 parent 7ada2d7 commit f0c189d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 48 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,6 @@ generated/noVNC-% :

list_structs:
git grep -o 'struct [A-Z][A-Za-z0-9]* ' | sed -e 's#/src/#::#' -e 's#main\.rs:##' -e 's#lib\.rs:##' -e 's#struct ##' -e 's/\.rs:/::/' -e 's#/#::#' | sort -u

list_e2etests:
cargo test -p e2etest -- --list
19 changes: 12 additions & 7 deletions e2etest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,26 @@ mod test {
qemu.kill().await?;
Ok(())
}
fn assert_tcp_echo_client_is_working(test_string: &str) -> Result<()> {
eprintln!("sending {test_string}...");
let cmd = format!("sleep 1 ; echo {test_string} | nc -w 3 localhost 18080");
let (stdout, _) = run_shell_cmd(&cmd)?;
eprintln!("got {stdout}");
assert!(stdout.contains(test_string));
Ok(())
}
#[tokio::test]
async fn tcp_echo_server_is_working() -> Result<()> {
// cargo test -p e2etest -- tcp
const TEST_STRING: &str = "hello_from_tcp";
// cargo test -p e2etest -- --nocapture tcp
let dev_env = DevEnv::new()?;
let mut qemu = Qemu::new(dev_env.ovmf_path())?;
let _rootfs = qemu.launch_with_wasabi_os(dev_env.wasabi_efi_path())?;
qemu.wait_until_serial_output_contains(
"net: rx: DHCP: SERVER -> CLIENT yiaddr = 10.0.2.15 chaddr = 52:54:00:12:34:56",
)?;
let cmd = format!("echo {TEST_STRING} | nc -w 1 localhost 18080");
let (stdout, _) = run_shell_cmd(&cmd)?;
eprintln!("{stdout}");
println!("{stdout}");
assert!(stdout.contains(TEST_STRING));

assert_tcp_echo_client_is_working("hello_from_tcp1")?;
assert_tcp_echo_client_is_working("hello_from_tcp2")?;

qemu.kill().await?;
Ok(())
Expand Down
51 changes: 13 additions & 38 deletions os/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

extern crate alloc;

use alloc::rc::Rc;
use alloc::string::String;
use core::pin::Pin;
use core::str::FromStr;
Expand All @@ -29,6 +30,8 @@ use os::executor::TimeoutFuture;
use os::info;
use os::init;
use os::input::InputManager;
use os::net::manager::Network;
use os::net::tcp::TcpSocket;
use os::print;
use os::serial::SerialPort;
use os::x86_64;
Expand Down Expand Up @@ -175,50 +178,22 @@ fn run_tasks() -> Result<()> {
yield_execution().await;
}
};
/*
let mouse_cursor_task = async {
const CURSOR_SIZE: i64 = 16;
let mut cursor_bitmap = BitmapBuffer::new(CURSOR_SIZE, CURSOR_SIZE, CURSOR_SIZE);
for y in 0..CURSOR_SIZE {
for x in 0..(CURSOR_SIZE - y) {
if x <= y {
bitmap_draw_point(&mut cursor_bitmap, 0x00ff00, x, y)
.expect("Failed to paint cursor");
}
}
}
let mut vram = BootInfo::take().vram();
noli::bitmap::draw_bmp_clipped(&mut vram, &cursor_bitmap, 100, 100)
.ok_or(Error::Failed("Failed to draw mouse cursor"))?;
loop {
if let Some(MouseEvent {
position: p,
button: b,
}) = InputManager::take().pop_cursor_input_absolute()
{
let color = (b.l() as u32) * 0xff0000;
let color = !color;
bitmap_draw_rect(&mut vram, color, p.x, p.y, 1, 1)?;
/*
crate::graphics::draw_bmp_clipped(&mut vram, &cursor_bitmap, p.x, p.y)
.ok_or(Error::Failed("Failed to draw mouse cursor"))?;
*/
}
TimeoutFuture::new_ms(15).await;
yield_execution().await;
}
};
spawn_global(mouse_cursor_task);
*/
let tcp_echo_task = async {
let network = Network::take();
let sock = Rc::new(TcpSocket::new_server(18080));
network.register_tcp_socket(sock)?;
info!("tcp_echo_task has started");
loop {
yield_execution().await;
}
};
// Enqueue tasks
spawn_global(task0);
spawn_global(task1);
spawn_global(serial_task);
spawn_global(console_task);
spawn_global(init_task);
spawn_global(tcp_echo_task);
init::init_pci();
// Start executing tasks
run_global_poll_loop();
Expand Down
3 changes: 0 additions & 3 deletions os/src/net/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ impl Network {
let mut network = NETWORK.lock();
let network = network.get_or_insert_with(|| {
let network = Self::new();
network
.register_tcp_socket(Rc::new(TcpSocket::new_server(18080)))
.expect("builtin tcp socket registration should succeed");
let dns_client = Rc::new(UdpSocket::default());
network.register_udp_socket(PORT_DNS_SERVER, dns_client.clone());
spawn_global(async { network_manager_thread().await });
Expand Down

0 comments on commit f0c189d

Please sign in to comment.