1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
use crate::{FromInner, Inner, IntoInner, NREAD};
use uv::{
uv_accept, uv_is_readable, uv_is_writable, uv_listen, uv_read_start, uv_read_stop, uv_shutdown,
uv_stream_get_write_queue_size, uv_stream_set_blocking, uv_stream_t, uv_try_write, uv_write,
uv_write2,
};
callbacks! {
pub AllocCB(handle: crate::Handle, suggested_size: usize) -> Option<crate::Buf>;
pub ConnectionCB(stream: StreamHandle, status: crate::Result<u32>);
pub ReadCB(stream: StreamHandle, nread: crate::Result<usize>, buf: crate::ReadonlyBuf);
}
pub(crate) struct StreamDataFields<'a> {
pub(crate) alloc_cb: AllocCB<'a>,
connection_cb: ConnectionCB<'a>,
read_cb: ReadCB<'a>,
pub(crate) addl: super::AddlStreamData<'a>,
}
pub(crate) extern "C" fn uv_alloc_cb(
handle: *mut uv::uv_handle_t,
suggested_size: u64,
buf: *mut uv::uv_buf_t,
) {
let dataptr = StreamHandle::get_data(uv_handle!(handle));
if !dataptr.is_null() {
unsafe {
let mut new_buf = (*dataptr)
.alloc_cb
.call(handle.into_inner(), suggested_size as _);
match new_buf.as_mut() {
Some(new_buf) => {
buf.copy_from_nonoverlapping(new_buf.inner(), 1);
new_buf.destroy_container();
}
None => {
(*buf).base = std::ptr::null_mut();
(*buf).len = 0;
}
}
}
}
}
extern "C" fn uv_connection_cb(stream: *mut uv_stream_t, status: std::os::raw::c_int) {
let dataptr = StreamHandle::get_data(stream);
if !dataptr.is_null() {
unsafe {
let status = if status < 0 {
Err(crate::Error::from_inner(status as uv::uv_errno_t))
} else {
Ok(status as _)
};
(*dataptr).connection_cb.call(stream.into_inner(), status);
}
}
}
extern "C" fn uv_read_cb(stream: *mut uv_stream_t, nread: NREAD, buf: *const uv::uv_buf_t) {
let dataptr = StreamHandle::get_data(stream);
if !dataptr.is_null() {
unsafe {
let nread = if nread < 0 {
Err(crate::Error::from_inner(nread as uv::uv_errno_t))
} else {
Ok(nread as usize)
};
(*dataptr)
.read_cb
.call(stream.into_inner(), nread as _, buf.into_inner());
}
}
}
#[derive(Clone, Copy)]
pub struct StreamHandle {
handle: *mut uv_stream_t,
}
impl StreamHandle {
pub(crate) fn initialize_data(stream: *mut uv_stream_t, addl: super::AddlStreamData) {
let data = super::super::StreamData(StreamDataFields {
alloc_cb: ().into(),
connection_cb: ().into(),
read_cb: ().into(),
addl,
});
crate::Handle::initialize_data(uv_handle!(stream), data);
}
pub(crate) fn get_data<'a>(stream: *mut uv_stream_t) -> *mut StreamDataFields<'a> {
if let super::super::StreamData(ref mut d) =
unsafe { &mut (*crate::Handle::get_data(uv_handle!(stream))).addl }
{
return d;
}
std::ptr::null_mut()
}
}
pub trait ToStream {
fn to_stream(&self) -> StreamHandle;
}
impl FromInner<*mut uv_stream_t> for StreamHandle {
fn from_inner(handle: *mut uv_stream_t) -> StreamHandle {
StreamHandle { handle }
}
}
impl Inner<*mut uv_stream_t> for StreamHandle {
fn inner(&self) -> *mut uv_stream_t {
self.handle
}
}
impl Inner<*const uv_stream_t> for StreamHandle {
fn inner(&self) -> *const uv_stream_t {
self.handle
}
}
impl Inner<*mut uv::uv_handle_t> for StreamHandle {
fn inner(&self) -> *mut uv::uv_handle_t {
uv_handle!(self.handle)
}
}
impl From<StreamHandle> for crate::Handle {
fn from(stream: StreamHandle) -> crate::Handle {
crate::Handle::from_inner(Inner::<*mut uv::uv_handle_t>::inner(&stream))
}
}
impl ToStream for StreamHandle {
fn to_stream(&self) -> StreamHandle {
StreamHandle {
handle: self.handle,
}
}
}
impl crate::ToHandle for StreamHandle {
fn to_handle(&self) -> crate::Handle {
crate::Handle::from_inner(Inner::<*mut uv::uv_handle_t>::inner(self))
}
}
pub trait StreamTrait: ToStream {
fn shutdown<CB: Into<crate::ShutdownCB<'static>>>(
&mut self,
cb: CB,
) -> crate::Result<crate::ShutdownReq> {
let mut req = crate::ShutdownReq::new(cb)?;
let result = crate::uvret(unsafe {
uv_shutdown(
req.inner(),
self.to_stream().inner(),
Some(crate::uv_shutdown_cb),
)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
fn listen<CB: Into<ConnectionCB<'static>>>(
&mut self,
backlog: i32,
cb: CB,
) -> crate::Result<()> {
let cb = cb.into();
let uv_cb = use_c_callback!(uv_connection_cb, cb);
let dataptr = StreamHandle::get_data(self.to_stream().inner());
if !dataptr.is_null() {
unsafe { (*dataptr).connection_cb = cb };
}
crate::uvret(unsafe { uv_listen(self.to_stream().inner(), backlog, uv_cb) })
}
fn accept(&mut self, client: &mut StreamHandle) -> crate::Result<()> {
crate::uvret(unsafe { uv_accept(self.to_stream().inner(), client.inner()) })
}
fn read_start<ACB: Into<AllocCB<'static>>, RCB: Into<ReadCB<'static>>>(
&mut self,
alloc_cb: ACB,
read_cb: RCB,
) -> crate::Result<()> {
let alloc_cb = alloc_cb.into();
let read_cb = read_cb.into();
let uv_alloc_cb = use_c_callback!(uv_alloc_cb, alloc_cb);
let uv_read_cb = use_c_callback!(uv_read_cb, read_cb);
let dataptr = StreamHandle::get_data(self.to_stream().inner());
if !dataptr.is_null() {
unsafe {
(*dataptr).alloc_cb = alloc_cb;
(*dataptr).read_cb = read_cb;
}
}
crate::uvret(unsafe { uv_read_start(self.to_stream().inner(), uv_alloc_cb, uv_read_cb) })
}
fn read_stop(&mut self) -> crate::Result<()> {
crate::uvret(unsafe { uv_read_stop(self.to_stream().inner()) })
}
fn write<CB: Into<crate::WriteCB<'static>>>(
&mut self,
bufs: &[impl crate::BufTrait],
cb: CB,
) -> crate::Result<crate::WriteReq> {
let mut req = crate::WriteReq::new(bufs, cb)?;
let result = crate::uvret(unsafe {
uv_write(
req.inner(),
self.to_stream().inner(),
req.bufs_ptr,
bufs.len() as _,
Some(crate::uv_write_cb),
)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
fn write2<CB: Into<crate::WriteCB<'static>>>(
&mut self,
send_handle: &StreamHandle,
bufs: &[impl crate::BufTrait],
cb: CB,
) -> crate::Result<crate::WriteReq> {
let mut req = crate::WriteReq::new(bufs, cb)?;
let result = crate::uvret(unsafe {
uv_write2(
req.inner(),
self.to_stream().inner(),
req.bufs_ptr,
bufs.len() as _,
send_handle.inner(),
Some(crate::uv_write_cb),
)
});
if result.is_err() {
req.destroy();
}
result.map(|_| req)
}
fn try_write(&mut self, bufs: &[impl crate::BufTrait]) -> crate::Result<i32> {
let (bufs_ptr, bufs_len, bufs_capacity) = bufs.into_inner();
let result = unsafe { uv_try_write(self.to_stream().inner(), bufs_ptr, bufs_len as _) };
unsafe { std::mem::drop(Vec::from_raw_parts(bufs_ptr, bufs_len, bufs_capacity)) };
crate::uvret(result).map(|_| result as _)
}
fn is_readable(&self) -> bool {
unsafe { uv_is_readable(self.to_stream().inner()) != 0 }
}
fn is_writable(&self) -> bool {
unsafe { uv_is_writable(self.to_stream().inner()) != 0 }
}
fn set_blocking(&mut self, blocking: bool) -> crate::Result<()> {
crate::uvret(unsafe {
uv_stream_set_blocking(self.to_stream().inner(), if blocking { 1 } else { 0 })
})
}
fn get_write_queue_size(&self) -> usize {
unsafe { uv_stream_get_write_queue_size(self.to_stream().inner()) as _ }
}
}
impl StreamTrait for StreamHandle {}
impl crate::HandleTrait for StreamHandle {}