Skip to content

Commit

Permalink
Merge pull request #1 from arceos-os/std_support_socket2
Browse files Browse the repository at this point in the history
add support for socket2
  • Loading branch information
equation314 authored Jul 14, 2023
2 parents b06ee44 + 938cf93 commit 17a0e1e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
19 changes: 18 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ members = [
"apps/std/mio-httpserver",
"apps/std/mio-tcpserver",
"apps/std/tokio-echoserver",
"apps/std/socket2-echoserver",
]

[profile.release]
Expand Down
13 changes: 13 additions & 0 deletions apps/std/socket2-echoserver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "socket2-echoserver"
version = "0.1.0"
edition = "2021"
authors = ["scpointer <[email protected]>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
env_logger = { version = "0.9.3", default-features = false }
socket2 = { git = "https://github.com/arceos-os/socket2.git", branch = "arceos", feature = "all" }

# axstd = { path = "../../../ulib/axstd", features = ["net"], optional = true }
67 changes: 67 additions & 0 deletions apps/std/socket2-echoserver/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//! Configuration needed to run this test (in the root Makefile):
//! A=apps/std/socket2-echoserver STD=y NET=y
use socket2::{Domain, Socket, Type};
use std::io::Write;
use std::mem::MaybeUninit;
use std::net::SocketAddr;
use std::str::from_utf8;

const DATA: &[u8] = b"hello world";
const BUF_SIZE: usize = 4096;
const ADDR: &str = "0.0.0.0:5555";

fn main() {
env_logger::init();
test()
}

fn test() {
let mut recv_buffer = [MaybeUninit::<u8>::new(0); BUF_SIZE];
let mut send_buffer = [0u8; BUF_SIZE];

let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
let addr: SocketAddr = ADDR.parse::<SocketAddr>().unwrap();
println!("addr {:#?}", addr);
#[cfg(not(target_os = "arceos"))]
let addr: socket2::SockAddr = addr.into();
socket.bind(&addr).unwrap();
socket.listen(128).unwrap();

println!("---------- socket2 echoserver ----------");
println!("type `nc {}` at another terminal:", ADDR);

loop {
let (mut connection, sockaddr) = loop {
if let Ok(result) = socket.accept() {
break result;
} else {
println!("user got a Err from accept, try again");
}
};
#[cfg(target_os = "arceos")]
println!("Accepted connection from: {}", sockaddr);
#[cfg(not(target_os = "arceos"))]
println!(
"Accepted connection from: {}",
sockaddr.as_socket().unwrap()
);
connection.write_all(DATA).unwrap();

loop {
let n = connection.recv(&mut recv_buffer).unwrap();
if n == 0 {
break;
}
for i in 0..n {
send_buffer[i] = unsafe { recv_buffer[i].assume_init() };
}
let received_data = &send_buffer[..n];
if let Ok(str_buf) = from_utf8(received_data) {
println!("Received data({}B): {}", n, str_buf.trim_end());
connection.write_all(received_data).unwrap();
} else {
println!("Received (none UTF-8) data: {:?}", received_data);
}
}
}
}
2 changes: 1 addition & 1 deletion scripts/make/cargo.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ define cargo_rustc
endef

define cargo_clippy
$(call run_cmd,cargo clippy,--all-features --workspace --exclude axlog --exclude mio-* --exclude tokio-* $(1) $(verbose))
$(call run_cmd,cargo clippy,--all-features --workspace --exclude axlog --exclude mio-* --exclude tokio-* --exclude socket2-* $(1) $(verbose))
$(call run_cmd,cargo clippy,-p axlog -p percpu -p percpu_macros $(1) $(verbose))
endef

Expand Down

0 comments on commit 17a0e1e

Please sign in to comment.