GnoCiYeH d623e90231
socket统一改用GlobalSocketHandle,并且修复fcntl SETFD的错误 (#730)
* socket统一改用`GlobalSocketHandle`,并且修复fcntl SETFD的错误

---------

Co-authored-by: longjin <longjin@DragonOS.org>
2024-04-15 22:01:32 +08:00

40 lines
1.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use ida::IdAllocator;
use smoltcp::iface::SocketHandle;
int_like!(KernelHandle, usize);
/// # socket的句柄管理组件
/// 它在smoltcp的SocketHandle上封装了一层增加更多的功能。
/// 比如在socket被关闭时自动释放socket的资源通知系统的其他组件。
#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
pub enum GlobalSocketHandle {
Smoltcp(SocketHandle),
Kernel(KernelHandle),
}
static KERNEL_HANDLE_IDA: IdAllocator = IdAllocator::new(0, usize::MAX);
impl GlobalSocketHandle {
pub fn new_smoltcp_handle(handle: SocketHandle) -> Self {
return Self::Smoltcp(handle);
}
pub fn new_kernel_handle() -> Self {
return Self::Kernel(KernelHandle::new(KERNEL_HANDLE_IDA.alloc().unwrap()));
}
pub fn smoltcp_handle(&self) -> Option<SocketHandle> {
if let Self::Smoltcp(sh) = *self {
return Some(sh);
}
None
}
pub fn kernel_handle(&self) -> Option<KernelHandle> {
if let Self::Kernel(kh) = *self {
return Some(kh);
}
None
}
}