16046f775S裕依 use alloc::{boxed::Box, sync::Arc, vec::Vec}; 26046f775S裕依 use system_error::SystemError; 36046f775S裕依 46046f775S裕依 use crate::{libs::spinlock::SpinLock, net::Endpoint}; 56046f775S裕依 6*d623e902SGnoCiYeH use super::{ 7*d623e902SGnoCiYeH handle::GlobalSocketHandle, Socket, SocketInode, SocketMetadata, SocketOptions, SocketType, 8*d623e902SGnoCiYeH }; 96046f775S裕依 106046f775S裕依 #[derive(Debug, Clone)] 116046f775S裕依 pub struct StreamSocket { 126046f775S裕依 metadata: SocketMetadata, 136046f775S裕依 buffer: Arc<SpinLock<Vec<u8>>>, 146046f775S裕依 peer_inode: Option<Arc<SocketInode>>, 15*d623e902SGnoCiYeH handle: GlobalSocketHandle, 166046f775S裕依 } 176046f775S裕依 186046f775S裕依 impl StreamSocket { 196046f775S裕依 /// 默认的元数据缓冲区大小 206046f775S裕依 pub const DEFAULT_METADATA_BUF_SIZE: usize = 1024; 216046f775S裕依 /// 默认的缓冲区大小 226046f775S裕依 pub const DEFAULT_BUF_SIZE: usize = 64 * 1024; 236046f775S裕依 246046f775S裕依 /// # 创建一个 Stream Socket 256046f775S裕依 /// 266046f775S裕依 /// ## 参数 276046f775S裕依 /// - `options`: socket选项 286046f775S裕依 pub fn new(options: SocketOptions) -> Self { 296046f775S裕依 let buffer = Arc::new(SpinLock::new(Vec::with_capacity(Self::DEFAULT_BUF_SIZE))); 306046f775S裕依 316046f775S裕依 let metadata = SocketMetadata::new( 326046f775S裕依 SocketType::Unix, 336046f775S裕依 Self::DEFAULT_BUF_SIZE, 346046f775S裕依 Self::DEFAULT_BUF_SIZE, 356046f775S裕依 Self::DEFAULT_METADATA_BUF_SIZE, 366046f775S裕依 options, 376046f775S裕依 ); 386046f775S裕依 396046f775S裕依 Self { 406046f775S裕依 metadata, 416046f775S裕依 buffer, 426046f775S裕依 peer_inode: None, 43*d623e902SGnoCiYeH handle: GlobalSocketHandle::new_kernel_handle(), 446046f775S裕依 } 456046f775S裕依 } 466046f775S裕依 } 476046f775S裕依 486046f775S裕依 impl Socket for StreamSocket { 49*d623e902SGnoCiYeH fn socket_handle(&self) -> GlobalSocketHandle { 50*d623e902SGnoCiYeH self.handle 51*d623e902SGnoCiYeH } 52*d623e902SGnoCiYeH 53*d623e902SGnoCiYeH fn close(&mut self) {} 54*d623e902SGnoCiYeH 556046f775S裕依 fn read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint) { 566046f775S裕依 let mut buffer = self.buffer.lock_irqsave(); 576046f775S裕依 586046f775S裕依 let len = core::cmp::min(buf.len(), buffer.len()); 596046f775S裕依 buf[..len].copy_from_slice(&buffer[..len]); 606046f775S裕依 616046f775S裕依 let _ = buffer.split_off(len); 626046f775S裕依 636046f775S裕依 (Ok(len), Endpoint::Inode(self.peer_inode.clone())) 646046f775S裕依 } 656046f775S裕依 666046f775S裕依 fn write(&self, buf: &[u8], _to: Option<Endpoint>) -> Result<usize, SystemError> { 676046f775S裕依 if self.peer_inode.is_none() { 686046f775S裕依 return Err(SystemError::ENOTCONN); 696046f775S裕依 } 706046f775S裕依 716046f775S裕依 let peer_inode = self.peer_inode.clone().unwrap(); 726046f775S裕依 let len = peer_inode.inner().write_buffer(buf)?; 736046f775S裕依 Ok(len) 746046f775S裕依 } 756046f775S裕依 766046f775S裕依 fn connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError> { 776046f775S裕依 if self.peer_inode.is_some() { 786046f775S裕依 return Err(SystemError::EISCONN); 796046f775S裕依 } 806046f775S裕依 816046f775S裕依 if let Endpoint::Inode(inode) = endpoint { 826046f775S裕依 self.peer_inode = inode; 836046f775S裕依 Ok(()) 846046f775S裕依 } else { 856046f775S裕依 Err(SystemError::EINVAL) 866046f775S裕依 } 876046f775S裕依 } 886046f775S裕依 896046f775S裕依 fn write_buffer(&self, buf: &[u8]) -> Result<usize, SystemError> { 906046f775S裕依 let mut buffer = self.buffer.lock_irqsave(); 916046f775S裕依 926046f775S裕依 let len = buf.len(); 936046f775S裕依 if buffer.capacity() - buffer.len() < len { 946046f775S裕依 return Err(SystemError::ENOBUFS); 956046f775S裕依 } 966046f775S裕依 buffer.extend_from_slice(buf); 976046f775S裕依 986046f775S裕依 Ok(len) 996046f775S裕依 } 1006046f775S裕依 1016046f775S裕依 fn metadata(&self) -> SocketMetadata { 1026046f775S裕依 self.metadata.clone() 1036046f775S裕依 } 1046046f775S裕依 1056046f775S裕依 fn box_clone(&self) -> Box<dyn Socket> { 1066046f775S裕依 Box::new(self.clone()) 1076046f775S裕依 } 1086046f775S裕依 1096046f775S裕依 fn as_any_ref(&self) -> &dyn core::any::Any { 1106046f775S裕依 self 1116046f775S裕依 } 1126046f775S裕依 1136046f775S裕依 fn as_any_mut(&mut self) -> &mut dyn core::any::Any { 1146046f775S裕依 self 1156046f775S裕依 } 1166046f775S裕依 } 1176046f775S裕依 1186046f775S裕依 #[derive(Debug, Clone)] 1196046f775S裕依 pub struct SeqpacketSocket { 1206046f775S裕依 metadata: SocketMetadata, 1216046f775S裕依 buffer: Arc<SpinLock<Vec<u8>>>, 1226046f775S裕依 peer_inode: Option<Arc<SocketInode>>, 123*d623e902SGnoCiYeH handle: GlobalSocketHandle, 1246046f775S裕依 } 1256046f775S裕依 1266046f775S裕依 impl SeqpacketSocket { 1276046f775S裕依 /// 默认的元数据缓冲区大小 1286046f775S裕依 pub const DEFAULT_METADATA_BUF_SIZE: usize = 1024; 1296046f775S裕依 /// 默认的缓冲区大小 1306046f775S裕依 pub const DEFAULT_BUF_SIZE: usize = 64 * 1024; 1316046f775S裕依 1326046f775S裕依 /// # 创建一个 Seqpacket Socket 1336046f775S裕依 /// 1346046f775S裕依 /// ## 参数 1356046f775S裕依 /// - `options`: socket选项 1366046f775S裕依 pub fn new(options: SocketOptions) -> Self { 1376046f775S裕依 let buffer = Arc::new(SpinLock::new(Vec::with_capacity(Self::DEFAULT_BUF_SIZE))); 1386046f775S裕依 1396046f775S裕依 let metadata = SocketMetadata::new( 1406046f775S裕依 SocketType::Unix, 1416046f775S裕依 Self::DEFAULT_BUF_SIZE, 1426046f775S裕依 Self::DEFAULT_BUF_SIZE, 1436046f775S裕依 Self::DEFAULT_METADATA_BUF_SIZE, 1446046f775S裕依 options, 1456046f775S裕依 ); 1466046f775S裕依 1476046f775S裕依 Self { 1486046f775S裕依 metadata, 1496046f775S裕依 buffer, 1506046f775S裕依 peer_inode: None, 151*d623e902SGnoCiYeH handle: GlobalSocketHandle::new_kernel_handle(), 1526046f775S裕依 } 1536046f775S裕依 } 1546046f775S裕依 } 1556046f775S裕依 1566046f775S裕依 impl Socket for SeqpacketSocket { 157*d623e902SGnoCiYeH fn close(&mut self) {} 158*d623e902SGnoCiYeH 1596046f775S裕依 fn read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint) { 1606046f775S裕依 let mut buffer = self.buffer.lock_irqsave(); 1616046f775S裕依 1626046f775S裕依 let len = core::cmp::min(buf.len(), buffer.len()); 1636046f775S裕依 buf[..len].copy_from_slice(&buffer[..len]); 1646046f775S裕依 1656046f775S裕依 let _ = buffer.split_off(len); 1666046f775S裕依 1676046f775S裕依 (Ok(len), Endpoint::Inode(self.peer_inode.clone())) 1686046f775S裕依 } 1696046f775S裕依 1706046f775S裕依 fn write(&self, buf: &[u8], _to: Option<Endpoint>) -> Result<usize, SystemError> { 1716046f775S裕依 if self.peer_inode.is_none() { 1726046f775S裕依 return Err(SystemError::ENOTCONN); 1736046f775S裕依 } 1746046f775S裕依 1756046f775S裕依 let peer_inode = self.peer_inode.clone().unwrap(); 1766046f775S裕依 let len = peer_inode.inner().write_buffer(buf)?; 1776046f775S裕依 Ok(len) 1786046f775S裕依 } 1796046f775S裕依 1806046f775S裕依 fn connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError> { 1816046f775S裕依 if self.peer_inode.is_some() { 1826046f775S裕依 return Err(SystemError::EISCONN); 1836046f775S裕依 } 1846046f775S裕依 1856046f775S裕依 if let Endpoint::Inode(inode) = endpoint { 1866046f775S裕依 self.peer_inode = inode; 1876046f775S裕依 Ok(()) 1886046f775S裕依 } else { 1896046f775S裕依 Err(SystemError::EINVAL) 1906046f775S裕依 } 1916046f775S裕依 } 1926046f775S裕依 1936046f775S裕依 fn write_buffer(&self, buf: &[u8]) -> Result<usize, SystemError> { 1946046f775S裕依 let mut buffer = self.buffer.lock_irqsave(); 1956046f775S裕依 1966046f775S裕依 let len = buf.len(); 1976046f775S裕依 if buffer.capacity() - buffer.len() < len { 1986046f775S裕依 return Err(SystemError::ENOBUFS); 1996046f775S裕依 } 2006046f775S裕依 buffer.extend_from_slice(buf); 2016046f775S裕依 2026046f775S裕依 Ok(len) 2036046f775S裕依 } 2046046f775S裕依 205*d623e902SGnoCiYeH fn socket_handle(&self) -> GlobalSocketHandle { 206*d623e902SGnoCiYeH self.handle 207*d623e902SGnoCiYeH } 208*d623e902SGnoCiYeH 2096046f775S裕依 fn metadata(&self) -> SocketMetadata { 2106046f775S裕依 self.metadata.clone() 2116046f775S裕依 } 2126046f775S裕依 2136046f775S裕依 fn box_clone(&self) -> Box<dyn Socket> { 2146046f775S裕依 Box::new(self.clone()) 2156046f775S裕依 } 2166046f775S裕依 2176046f775S裕依 fn as_any_ref(&self) -> &dyn core::any::Any { 2186046f775S裕依 self 2196046f775S裕依 } 2206046f775S裕依 2216046f775S裕依 fn as_any_mut(&mut self) -> &mut dyn core::any::Any { 2226046f775S裕依 self 2236046f775S裕依 } 2246046f775S裕依 } 225