feat(socket): 添加shutdown方法并实现ShutdownTemp的TryFrom转换

This commit is contained in:
xiaolin2004 2024-11-28 19:20:01 +08:00
parent 57e0b2e96a
commit bbea79ec19
3 changed files with 41 additions and 4 deletions

View File

@ -1,4 +1,6 @@
use core::sync::atomic::AtomicU8;
use core::{default, sync::atomic::AtomicU8};
use system_error::SystemError;
bitflags! {
/// @brief 用于指定socket的关闭类型
@ -101,8 +103,8 @@ impl ShutdownTemp {
self.bit == 0
}
pub fn from_how(how: usize) -> Self {
Self { bit: how as u8 + 1 }
pub fn bits(&self) -> ShutdownBit {
ShutdownBit { bits: self.bit }
}
}
@ -116,3 +118,16 @@ impl From<ShutdownBit> for ShutdownTemp {
}
}
}
impl TryFrom<usize> for ShutdownTemp {
type Error = SystemError;
fn try_from(value: usize) -> Result<Self, Self::Error> {
match value {
0 | 1 | 2 => Ok(ShutdownTemp {
bit: value as u8 + 1,
}),
_ => Err(SystemError::EINVAL),
}
}
}

View File

@ -338,6 +338,28 @@ impl Socket for TcpSocket {
.recv_buffer_size()
}
fn shutdown(&self, how: ShutdownTemp) -> Result<(), SystemError> {
let self_shutdown = self.shutdown.get().bits();
let diff = how.bits().difference(self_shutdown);
match diff.is_empty(){
true => {
return Ok(())
},
false => {
if diff.contains(ShutdownBit::SHUT_RD){
self.shutdown.recv_shutdown();
// TODO 协议栈处理
}
if diff.contains(ShutdownBit::SHUT_WR){
self.shutdown.send_shutdown();
// TODO 协议栈处理
}
},
}
Ok(())
}
fn close(&self) -> Result<(), SystemError> {
let inner = self.inner
.write()

View File

@ -367,7 +367,7 @@ impl Syscall {
let socket: Arc<socket::Inode> = ProcessManager::current_pcb()
.get_socket(fd as i32)
.ok_or(SystemError::EBADF)?;
socket.shutdown(socket::ShutdownTemp::from_how(how))?;
socket.shutdown(how.try_into()?)?;
return Ok(0);
}