-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix the alloc error and add unix socket
- Loading branch information
Showing
18 changed files
with
163 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[profile.release] | ||
#strip = true | ||
#incremental = true | ||
#debug = true | ||
opt-level = 3 | ||
lto = true |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,30 +1,74 @@ | ||
//! 有关 Unix 协议族下的套接字结构。(目前有关的功能有待支持) | ||
use alloc::{string::String, sync::Arc}; | ||
use alloc::{ | ||
string::String, | ||
sync::{Arc, Weak}, | ||
vec::Vec, | ||
}; | ||
|
||
use constants::LinuxErrno; | ||
use constants::{AlienResult, LinuxErrno}; | ||
use ksync::Mutex; | ||
use vfs::kfile::File; | ||
|
||
use crate::socket::{Socket, SocketFile, SocketFileExt}; | ||
|
||
/// Unix 协议族下的套接字结构 | ||
#[allow(unused)] | ||
pub struct UnixSocket { | ||
/// 文件路径,即套接字地址 | ||
file_path: Mutex<Option<String>>, | ||
/// 套接字数据 | ||
file: Mutex<Option<Arc<dyn File>>>, | ||
inner: Mutex<UnixSocketInner>, | ||
} | ||
|
||
struct UnixSocketInner { | ||
remote: Weak<SocketFile>, | ||
buf: Vec<u8>, | ||
} | ||
|
||
impl UnixSocket { | ||
/// 创建一个新的 Unix 协议族下的套接字结构 | ||
pub fn new() -> Self { | ||
Self { | ||
file_path: Mutex::new(None), | ||
file: Mutex::new(None), | ||
inner: Mutex::new(UnixSocketInner { | ||
remote: Weak::<SocketFile>::new(), | ||
buf: Vec::new(), | ||
}), | ||
} | ||
} | ||
|
||
/// UnixSocket 的 connect 操作 | ||
pub fn connect(&self, _file_path: String) -> Result<(), LinuxErrno> { | ||
pub fn connect(&self, _file_path: String) -> AlienResult<()> { | ||
Err(LinuxErrno::ENOENT) | ||
} | ||
|
||
pub fn set_remote(&self, remote: &Arc<SocketFile>) { | ||
self.inner.lock().remote = Arc::downgrade(remote); | ||
} | ||
|
||
pub fn send_to(&self, buf: &[u8]) -> AlienResult<usize> { | ||
if let Some(remote) = self.inner.lock().remote.upgrade() { | ||
let socket_guard = remote.get_socketdata()?; | ||
match socket_guard.socket { | ||
Socket::Unix(ref unix_socket) => { | ||
unix_socket.inner.lock().buf.extend_from_slice(buf); | ||
Ok(buf.len()) | ||
} | ||
_ => Err(LinuxErrno::EINVAL), | ||
} | ||
} else { | ||
Err(LinuxErrno::ENOTCONN) | ||
} | ||
} | ||
|
||
pub fn ready_read(&self) -> bool { | ||
self.inner.lock().buf.len() > 0 | ||
} | ||
|
||
pub fn recvfrom(&self, buf: &mut [u8]) -> AlienResult<usize> { | ||
let inner_buf = &mut self.inner.lock().buf; | ||
if inner_buf.len() > 0 { | ||
let len = inner_buf.len().min(buf.len()); | ||
buf[..len].copy_from_slice(&inner_buf[..len]); | ||
inner_buf.drain(..len); | ||
Ok(len) | ||
} else { | ||
Err(LinuxErrno::EAGAIN) | ||
} | ||
} | ||
} |
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
Binary file not shown.
Submodule qemu-loongarch-runenv
deleted from
bb489f
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 |
---|---|---|
|
@@ -26,7 +26,6 @@ BUILD_CRATES := \ | |
sleep \ | ||
socket_test \ | ||
final_test \ | ||
ftest \ | ||
# shell \ | ||
print \ | ||
|
||
|
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -18,4 +18,5 @@ build: | |
|
||
BUILD_CRATES := \ | ||
hello \ | ||
async_test | ||
async_test \ | ||
ftest |
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 |
---|---|---|
|
@@ -3,5 +3,4 @@ name = "ftest" | |
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
Mstd = {path = "../../userlib" } | ||
[dependencies] |
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,36 @@ | ||
use std::{fs::File, io::Read, time::Instant}; | ||
|
||
fn main() { | ||
read_bash_test(); | ||
// in cache | ||
read_bash_test(); | ||
// read_once_test(); | ||
} | ||
fn read_bash_test() { | ||
let mut file = File::open("/tests/bash2").unwrap(); | ||
let now = Instant::now(); | ||
let mut buf = [0u8; 4096]; | ||
let mut bytes = 0; | ||
loop { | ||
let res = file.read(&mut buf).unwrap(); | ||
if res == 0 { | ||
break; | ||
} | ||
bytes += res; | ||
} | ||
let ms = now.elapsed().as_millis(); | ||
let speed = bytes as f64 * 1000.0 / ms as f64 / 1024.0; | ||
println!( | ||
"Read {} bytes in {}ms, speed: {} KB/s", | ||
bytes, ms, speed as isize | ||
); | ||
} | ||
|
||
fn read_once_test() { | ||
let mut file = File::open("/tests/bash2").unwrap(); | ||
let now = Instant::now(); | ||
let mut buf = [0u8; 4096]; | ||
let res = file.read(&mut buf).unwrap(); | ||
let ms = now.elapsed().as_micros(); | ||
println!("Read {} bytes in {}us", res, ms); | ||
} |