|
1 |
| -use core::any::Any; |
2 |
| - |
3 |
| -use alloc::{boxed::Box, sync::Arc, vec::Vec}; |
4 |
| -use system_error::SystemError; |
5 |
| - |
6 |
| -use crate::{libs::spinlock::SpinLock, net::Endpoint}; |
7 |
| - |
8 |
| -use super::{ |
9 |
| - handle::GlobalSocketHandle, PosixSocketHandleItem, Socket, inode::Inode, SocketMetadata, |
10 |
| - Options, InetSocketType, |
11 |
| -}; |
12 |
| - |
13 |
| -#[derive(Debug, Clone)] |
14 |
| -pub struct StreamSocket { |
15 |
| - metadata: SocketMetadata, |
16 |
| - buffer: Arc<SpinLock<Vec<u8>>>, |
17 |
| - peer_inode: Option<Arc<Inode>>, |
18 |
| - handle: GlobalSocketHandle, |
19 |
| - posix_item: Arc<PosixSocketHandleItem>, |
20 |
| -} |
21 |
| - |
22 |
| -impl StreamSocket { |
23 |
| - /// 默认的元数据缓冲区大小 |
24 |
| - pub const DEFAULT_METADATA_BUF_SIZE: usize = 1024; |
25 |
| - /// 默认的缓冲区大小 |
26 |
| - pub const DEFAULT_BUF_SIZE: usize = 64 * 1024; |
27 |
| - |
28 |
| - /// # 创建一个 Stream Socket |
29 |
| - /// |
30 |
| - /// ## 参数 |
31 |
| - /// - `options`: socket选项 |
32 |
| - pub fn new(options: Options) -> Self { |
33 |
| - let buffer = Arc::new(SpinLock::new(Vec::with_capacity(Self::DEFAULT_BUF_SIZE))); |
34 |
| - |
35 |
| - let metadata = SocketMetadata::new( |
36 |
| - InetSocketType::Unix, |
37 |
| - Self::DEFAULT_BUF_SIZE, |
38 |
| - Self::DEFAULT_BUF_SIZE, |
39 |
| - Self::DEFAULT_METADATA_BUF_SIZE, |
40 |
| - options, |
41 |
| - ); |
42 |
| - |
43 |
| - let posix_item = Arc::new(PosixSocketHandleItem::new(None)); |
44 |
| - |
45 |
| - Self { |
46 |
| - metadata, |
47 |
| - buffer, |
48 |
| - peer_inode: None, |
49 |
| - handle: GlobalSocketHandle::new_kernel_handle(), |
50 |
| - posix_item, |
51 |
| - } |
52 |
| - } |
53 |
| -} |
54 |
| - |
55 |
| -impl Socket for StreamSocket { |
56 |
| - fn socket_handle(&self) -> GlobalSocketHandle { |
57 |
| - self.handle |
58 |
| - } |
59 |
| - |
60 |
| - fn close(&mut self) {} |
61 |
| - |
62 |
| - fn read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint) { |
63 |
| - let mut buffer = self.buffer.lock_irqsave(); |
64 |
| - |
65 |
| - let len = core::cmp::min(buf.len(), buffer.len()); |
66 |
| - buf[..len].copy_from_slice(&buffer[..len]); |
67 |
| - |
68 |
| - let _ = buffer.split_off(len); |
69 |
| - |
70 |
| - (Ok(len), Endpoint::Inode(self.peer_inode.clone())) |
71 |
| - } |
72 |
| - |
73 |
| - fn write(&self, buf: &[u8], _to: Option<Endpoint>) -> Result<usize, SystemError> { |
74 |
| - if self.peer_inode.is_none() { |
75 |
| - return Err(SystemError::ENOTCONN); |
76 |
| - } |
77 |
| - |
78 |
| - let peer_inode = self.peer_inode.clone().unwrap(); |
79 |
| - let len = peer_inode.inner().write_buffer(buf)?; |
80 |
| - Ok(len) |
81 |
| - } |
82 |
| - |
83 |
| - fn connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError> { |
84 |
| - if self.peer_inode.is_some() { |
85 |
| - return Err(SystemError::EISCONN); |
86 |
| - } |
87 |
| - |
88 |
| - if let Endpoint::Inode(inode) = endpoint { |
89 |
| - self.peer_inode = inode; |
90 |
| - Ok(()) |
91 |
| - } else { |
92 |
| - Err(SystemError::EINVAL) |
93 |
| - } |
94 |
| - } |
95 |
| - |
96 |
| - fn write_buffer(&self, buf: &[u8]) -> Result<usize, SystemError> { |
97 |
| - let mut buffer = self.buffer.lock_irqsave(); |
98 |
| - |
99 |
| - let len = buf.len(); |
100 |
| - if buffer.capacity() - buffer.len() < len { |
101 |
| - return Err(SystemError::ENOBUFS); |
102 |
| - } |
103 |
| - buffer.extend_from_slice(buf); |
104 |
| - |
105 |
| - Ok(len) |
106 |
| - } |
107 |
| - |
108 |
| - fn metadata(&self) -> SocketMetadata { |
109 |
| - self.metadata.clone() |
110 |
| - } |
111 |
| - |
112 |
| - fn box_clone(&self) -> Box<dyn Socket> { |
113 |
| - Box::new(self.clone()) |
114 |
| - } |
115 |
| - |
116 |
| - fn as_any_ref(&self) -> &dyn core::any::Any { |
117 |
| - self |
118 |
| - } |
119 |
| - |
120 |
| - fn as_any_mut(&mut self) -> &mut dyn core::any::Any { |
121 |
| - self |
122 |
| - } |
123 |
| -} |
124 |
| - |
125 |
| -#[derive(Debug, Clone)] |
126 |
| -pub struct SeqpacketSocket { |
127 |
| - metadata: SocketMetadata, |
128 |
| - buffer: Arc<SpinLock<Vec<u8>>>, |
129 |
| - peer_inode: Option<Arc<Inode>>, |
130 |
| - handle: GlobalSocketHandle, |
131 |
| - posix_item: Arc<PosixSocketHandleItem>, |
132 |
| -} |
133 |
| - |
134 |
| -impl SeqpacketSocket { |
135 |
| - /// 默认的元数据缓冲区大小 |
136 |
| - pub const DEFAULT_METADATA_BUF_SIZE: usize = 1024; |
137 |
| - /// 默认的缓冲区大小 |
138 |
| - pub const DEFAULT_BUF_SIZE: usize = 64 * 1024; |
139 |
| - |
140 |
| - /// # 创建一个 Seqpacket Socket |
141 |
| - /// |
142 |
| - /// ## 参数 |
143 |
| - /// - `options`: socket选项 |
144 |
| - pub fn new(options: Options) -> Self { |
145 |
| - let buffer = Arc::new(SpinLock::new(Vec::with_capacity(Self::DEFAULT_BUF_SIZE))); |
146 |
| - |
147 |
| - let metadata = SocketMetadata::new( |
148 |
| - InetSocketType::Unix, |
149 |
| - Self::DEFAULT_BUF_SIZE, |
150 |
| - Self::DEFAULT_BUF_SIZE, |
151 |
| - Self::DEFAULT_METADATA_BUF_SIZE, |
152 |
| - options, |
153 |
| - ); |
154 |
| - |
155 |
| - let posix_item = Arc::new(PosixSocketHandleItem::new(None)); |
156 |
| - |
157 |
| - Self { |
158 |
| - metadata, |
159 |
| - buffer, |
160 |
| - peer_inode: None, |
161 |
| - handle: GlobalSocketHandle::new_kernel_handle(), |
162 |
| - posix_item, |
163 |
| - } |
164 |
| - } |
165 |
| -} |
166 |
| - |
167 |
| -impl Socket for SeqpacketSocket { |
168 |
| - fn close(&mut self) {} |
169 |
| - |
170 |
| - fn read(&self, buf: &mut [u8]) -> (Result<usize, SystemError>, Endpoint) { |
171 |
| - let mut buffer = self.buffer.lock_irqsave(); |
172 |
| - |
173 |
| - let len = core::cmp::min(buf.len(), buffer.len()); |
174 |
| - buf[..len].copy_from_slice(&buffer[..len]); |
175 |
| - |
176 |
| - let _ = buffer.split_off(len); |
177 |
| - |
178 |
| - (Ok(len), Endpoint::Inode(self.peer_inode.clone())) |
179 |
| - } |
180 |
| - |
181 |
| - fn write(&self, buf: &[u8], _to: Option<Endpoint>) -> Result<usize, SystemError> { |
182 |
| - if self.peer_inode.is_none() { |
183 |
| - return Err(SystemError::ENOTCONN); |
184 |
| - } |
185 |
| - |
186 |
| - let peer_inode = self.peer_inode.clone().unwrap(); |
187 |
| - let len = peer_inode.inner().write_buffer(buf)?; |
188 |
| - Ok(len) |
189 |
| - } |
190 |
| - |
191 |
| - fn connect(&mut self, endpoint: Endpoint) -> Result<(), SystemError> { |
192 |
| - if self.peer_inode.is_some() { |
193 |
| - return Err(SystemError::EISCONN); |
194 |
| - } |
195 |
| - |
196 |
| - if let Endpoint::Inode(inode) = endpoint { |
197 |
| - self.peer_inode = inode; |
198 |
| - Ok(()) |
199 |
| - } else { |
200 |
| - Err(SystemError::EINVAL) |
201 |
| - } |
202 |
| - } |
203 |
| - |
204 |
| - fn write_buffer(&self, buf: &[u8]) -> Result<usize, SystemError> { |
205 |
| - let mut buffer = self.buffer.lock_irqsave(); |
206 |
| - |
207 |
| - let len = buf.len(); |
208 |
| - if buffer.capacity() - buffer.len() < len { |
209 |
| - return Err(SystemError::ENOBUFS); |
210 |
| - } |
211 |
| - buffer.extend_from_slice(buf); |
212 |
| - |
213 |
| - Ok(len) |
214 |
| - } |
215 |
| - |
216 |
| - fn socket_handle(&self) -> GlobalSocketHandle { |
217 |
| - self.handle |
218 |
| - } |
219 |
| - |
220 |
| - fn metadata(&self) -> SocketMetadata { |
221 |
| - self.metadata.clone() |
222 |
| - } |
223 |
| - |
224 |
| - fn box_clone(&self) -> Box<dyn Socket> { |
225 |
| - Box::new(self.clone()) |
226 |
| - } |
227 |
| - |
228 |
| - fn as_any_ref(&self) -> &dyn core::any::Any { |
229 |
| - self |
230 |
| - } |
231 |
| - |
232 |
| - fn as_any_mut(&mut self) -> &mut dyn core::any::Any { |
233 |
| - self |
234 |
| - } |
235 |
| -} |
| 1 | +mod stream; |
0 commit comments