Skip to main content

compio_driver/sys/op/socket/
poll.rs

1use Interest::*;
2use rustix::net::sockopt::socket_error;
3
4use crate::{PollOpCode as OpCode, op::*, sys::driver::*};
5
6unsafe impl OpCode for CreateSocket {
7    type Control = ();
8
9    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
10        Ok(Decision::Blocking)
11    }
12
13    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
14        Poll::Ready(self.call())
15    }
16}
17
18unsafe impl<S: AsFd> OpCode for Bind<S> {
19    type Control = ();
20
21    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
22        Ok(Decision::Blocking)
23    }
24
25    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
26        Poll::Ready(self.call())
27    }
28}
29
30unsafe impl<S: AsFd> OpCode for Listen<S> {
31    type Control = ();
32
33    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
34        Ok(Decision::Blocking)
35    }
36
37    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
38        Poll::Ready(self.call())
39    }
40}
41
42unsafe impl<S: AsFd> OpCode for ShutdownSocket<S> {
43    type Control = ();
44
45    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
46        Ok(Decision::Blocking)
47    }
48
49    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
50        Poll::Ready(self.call())
51    }
52}
53
54unsafe impl OpCode for CloseSocket {
55    type Control = ();
56
57    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
58        Ok(Decision::Blocking)
59    }
60
61    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
62        Poll::Ready(self.call())
63    }
64}
65
66unsafe impl<S: AsFd> OpCode for Accept<S> {
67    type Control = ();
68
69    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
70        decide(self.fd.as_fd().as_raw_fd(), Readable, || self.call())
71    }
72
73    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
74        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
75    }
76
77    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
78        poll_io(|| self.call())
79    }
80}
81
82unsafe impl<S: AsFd> OpCode for Connect<S> {
83    type Control = ();
84
85    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
86        decide(self.fd.as_fd().as_raw_fd(), Writable, || self.call())
87    }
88
89    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
90        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
91    }
92
93    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
94        socket_error(&self.fd)??;
95        Poll::Ready(Ok(0))
96    }
97}
98
99unsafe impl<T: IoBufMut, S: AsFd> OpCode for Recv<T, S> {
100    type Control = ();
101
102    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
103        decide(self.fd.as_fd().as_raw_fd(), Readable, || self.call())
104    }
105
106    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
107        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
108    }
109
110    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
111        poll_io(|| self.call())
112    }
113}
114
115unsafe impl<T: IoBuf, S: AsFd> OpCode for Send<T, S> {
116    type Control = ();
117
118    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
119        decide(self.fd.as_fd().as_raw_fd(), Writable, || self.call())
120    }
121
122    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
123        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
124    }
125
126    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
127        poll_io(|| self.call())
128    }
129}
130
131impl<S: AsFd> RecvFromHeader<S> {
132    pub fn fd(&self) -> RawFd {
133        self.fd.as_fd().as_raw_fd()
134    }
135}
136
137unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for RecvVectored<T, S> {
138    type Control = RecvVectoredControl;
139
140    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
141        self.init_control(ctrl)
142    }
143
144    fn pre_submit(&mut self, control: &mut Self::Control) -> io::Result<Decision> {
145        decide(self.fd.as_fd().as_raw_fd(), Readable, || self.call(control))
146    }
147
148    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
149        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
150    }
151
152    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
153        poll_io(|| self.call(control))
154    }
155}
156
157unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for SendVectored<T, S> {
158    type Control = SendVectoredControl;
159
160    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
161        self.init_control(ctrl)
162    }
163
164    fn pre_submit(&mut self, control: &mut Self::Control) -> io::Result<Decision> {
165        decide(self.fd.as_fd().as_raw_fd(), Writable, || self.call(control))
166    }
167
168    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
169        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
170    }
171
172    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
173        poll_io(|| self.call(control))
174    }
175}
176
177unsafe impl<T: IoBufMut, S: AsFd> OpCode for RecvFrom<T, S> {
178    type Control = ();
179
180    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
181        decide(self.header.fd(), Readable, || self.call())
182    }
183
184    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
185        Some(OpType::fd(self.header.fd.as_fd().as_raw_fd()))
186    }
187
188    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
189        poll_io(|| self.call())
190    }
191}
192
193unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for RecvFromVectored<T, S> {
194    type Control = RecvMsgControl;
195
196    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
197        ctrl.slices = self.buffer.sys_slices_mut().into();
198        ctrl.msg.msg_name = &raw mut self.header.addr as _;
199        ctrl.msg.msg_namelen = self.header.addr.size_of() as _;
200        ctrl.msg.msg_iov = ctrl.slices.as_mut_ptr() as _;
201        ctrl.msg.msg_iovlen = ctrl.slices.len() as _;
202    }
203
204    fn pre_submit(&mut self, control: &mut Self::Control) -> io::Result<Decision> {
205        decide(self.header.fd(), Readable, || self.call(control))
206    }
207
208    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
209        Some(OpType::fd(self.header.fd.as_fd().as_raw_fd()))
210    }
211
212    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
213        poll_io(|| self.call(control))
214    }
215}
216
217unsafe impl<T: IoBuf, S: AsFd> OpCode for SendTo<T, S> {
218    type Control = ();
219
220    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
221        decide(self.header.fd.as_fd().as_raw_fd(), Writable, || self.call())
222    }
223
224    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
225        Some(OpType::fd(self.header.fd.as_fd().as_raw_fd()))
226    }
227
228    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
229        poll_io(|| self.call())
230    }
231}
232
233unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for SendToVectored<T, S> {
234    type Control = SendMsgControl;
235
236    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
237        ctrl.slices = self.buffer.sys_slices().into();
238        ctrl.msg.msg_name = self.header.addr.as_ptr() as _;
239        ctrl.msg.msg_namelen = self.header.addr.len() as _;
240        ctrl.msg.msg_iov = ctrl.slices.as_ptr() as _;
241        ctrl.msg.msg_iovlen = ctrl.slices.len() as _;
242    }
243
244    fn pre_submit(&mut self, control: &mut Self::Control) -> io::Result<Decision> {
245        decide(self.header.fd.as_fd().as_raw_fd(), Writable, || {
246            self.call(control)
247        })
248    }
249
250    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
251        Some(OpType::fd(self.header.fd.as_fd().as_raw_fd()))
252    }
253
254    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
255        poll_io(|| self.call(control))
256    }
257}
258
259unsafe impl<T: IoVectoredBufMut, C: IoBufMut, S: AsFd> OpCode for RecvMsg<T, C, S> {
260    type Control = RecvMsgControl;
261
262    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
263        self.init_control(ctrl)
264    }
265
266    fn pre_submit(&mut self, control: &mut Self::Control) -> io::Result<Decision> {
267        decide(self.header.fd(), Readable, || self.call(control))
268    }
269
270    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
271        Some(OpType::fd(self.header.fd.as_fd().as_raw_fd()))
272    }
273
274    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
275        poll_io(|| self.call(control))
276    }
277
278    unsafe fn set_result(
279        &mut self,
280        control: &mut Self::Control,
281        _: &io::Result<usize>,
282        _: &crate::Extra,
283    ) {
284        self.update_control(control);
285    }
286}
287
288unsafe impl<T: IoVectoredBuf, C: IoBuf, S: AsFd> OpCode for SendMsg<T, C, S> {
289    type Control = SendMsgControl;
290
291    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
292        self.init_control(ctrl)
293    }
294
295    fn pre_submit(&mut self, control: &mut Self::Control) -> io::Result<Decision> {
296        decide(self.fd.as_fd().as_raw_fd(), Writable, || self.call(control))
297    }
298
299    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
300        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
301    }
302
303    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
304        poll_io(|| self.call(control))
305    }
306}