|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 | 11 | use prelude::*;
|
12 |
| -use libc::{size_t, ssize_t, c_int, c_void}; |
| 12 | +use libc::{size_t, ssize_t, c_int, c_void, c_uint}; |
13 | 13 | use rt::uv::uvll;
|
14 | 14 | use rt::uv::uvll::*;
|
15 |
| -use rt::uv::{AllocCallback, ConnectionCallback, ReadCallback}; |
| 15 | +use rt::uv::{AllocCallback, ConnectionCallback, ReadCallback, UdpReceiveCallback, UdpSendCallback}; |
16 | 16 | use rt::uv::{Loop, Watcher, Request, UvError, Buf, NativeHandle, NullCallback,
|
17 | 17 | status_to_maybe_uv_error};
|
18 | 18 | use rt::io::net::ip::{IpAddr, Ipv4, Ipv6};
|
@@ -254,6 +254,142 @@ impl NativeHandle<*uvll::uv_tcp_t> for TcpWatcher {
|
254 | 254 | }
|
255 | 255 | }
|
256 | 256 |
|
| 257 | +pub struct UdpWatcher(*uvll::uv_udp_t); |
| 258 | +impl Watcher for UdpWatcher { } |
| 259 | + |
| 260 | +pub impl UdpWatcher { |
| 261 | + fn new(loop_: &mut Loop) -> UdpWatcher { |
| 262 | + unsafe { |
| 263 | + let handle = malloc_handle(UV_UDP); |
| 264 | + assert!(handle.is_not_null()); |
| 265 | + assert_eq!(0, uvll::udp_init(loop_.native_handle(), handle)); |
| 266 | + let mut watcher: UdpWatcher = NativeHandle::from_native_handle(handle); |
| 267 | + watcher.install_watcher_data(); |
| 268 | + return watcher; |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + fn bind(&mut self, address: IpAddr) -> Result<(), UvError> { |
| 273 | + match address { |
| 274 | + Ipv4(*) => { |
| 275 | + do ip4_as_uv_ip4(address) |addr| { |
| 276 | + let result = unsafe { |
| 277 | + uvll::udp_bind(self.native_handle(), addr, 0u32) |
| 278 | + }; |
| 279 | + if result == 0 { |
| 280 | + Ok(()) |
| 281 | + } else { |
| 282 | + Err(last_uv_error(self)) |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + _ => fail!() // TODO ipv6 |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + fn recv_start(&mut self, alloc: AllocCallback, cb: UdpReceiveCallback) { |
| 291 | + { |
| 292 | + let data = self.get_watcher_data(); |
| 293 | + data.alloc_cb = Some(alloc); |
| 294 | + data.udp_recv_cb = Some(cb); |
| 295 | + } |
| 296 | + |
| 297 | + let handle = self.native_handle(); |
| 298 | + unsafe { uvll::read_start(handle, alloc_cb, recv_cb); } |
| 299 | + |
| 300 | + extern fn alloc_cb(handle: *uvll::uv_udp_t, suggested_size: size_t) -> Buf { |
| 301 | + let mut udp_watcher: UdpWatcher = NativeHandle::from_native_handle(handle); |
| 302 | + let data = udp_watcher.get_watcher_data(); |
| 303 | + let alloc_cb = data.alloc_cb.get_ref(); |
| 304 | + return (*alloc_cb)(suggested_size as uint); |
| 305 | + } |
| 306 | + |
| 307 | + /* TODO the socket address should actually be a pointer to either a sockaddr_in or sockaddr_in6. |
| 308 | + In libuv, the udp_recv callback takes a struct *sockaddr */ |
| 309 | + extern fn recv_cb(handle: *uvll::uv_udp_t, nread: ssize_t, buf: Buf, |
| 310 | + address: *uvll::sockaddr_in, flags: c_uint) { |
| 311 | + rtdebug!("buf addr: %x", buf.base as uint); |
| 312 | + rtdebug!("buf len: %d", buf.len as int); |
| 313 | + let mut udp_watcher: UdpWatcher = NativeHandle::from_native_handle(handle); |
| 314 | + let data = udp_watcher.get_watcher_data(); |
| 315 | + let cb = data.udp_recv_cb.get_ref(); |
| 316 | + let status = status_to_maybe_uv_error(handle, nread as c_int); |
| 317 | + unsafe { (*cb)(udp_watcher, nread as int, buf, *address, flags as uint, status) }; |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + fn recv_stop(&mut self) { |
| 322 | + let handle = self.native_handle(); |
| 323 | + unsafe { uvll::udp_recv_stop(handle); } |
| 324 | + } |
| 325 | + |
| 326 | + fn send(&mut self, buf: Buf, address: IpAddr, cb: UdpSendCallback) { |
| 327 | + { |
| 328 | + let data = self.get_watcher_data(); |
| 329 | + assert!(data.udp_send_cb.is_none()); |
| 330 | + data.udp_send_cb = Some(cb); |
| 331 | + } |
| 332 | + |
| 333 | + let req = UdpSendRequest::new(); |
| 334 | + let bufs = [buf]; |
| 335 | + match address { |
| 336 | + Ipv4(*) => { |
| 337 | + do ip4_as_uv_ip4(address) |addr| { |
| 338 | + unsafe { |
| 339 | + assert!(0 == uvll::udp_send(req.native_handle(), |
| 340 | + self.native_handle(), |
| 341 | + bufs, addr, send_cb)); |
| 342 | + } |
| 343 | + } |
| 344 | + } |
| 345 | + _ => fail!() // TODO ipv6 |
| 346 | + } |
| 347 | + |
| 348 | + extern fn send_cb(req: *uvll::uv_udp_send_t, status: c_int) { |
| 349 | + let send_request: UdpSendRequest = NativeHandle::from_native_handle(req); |
| 350 | + let mut udp_watcher = send_request.handle(); |
| 351 | + send_request.delete(); |
| 352 | + let cb = { |
| 353 | + let data = udp_watcher.get_watcher_data(); |
| 354 | + let cb = data.udp_send_cb.swap_unwrap(); |
| 355 | + cb |
| 356 | + }; |
| 357 | + let status = status_to_maybe_uv_error(udp_watcher.native_handle(), status); |
| 358 | + cb(udp_watcher, status); |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | + fn close(self, cb: NullCallback) { |
| 363 | + { |
| 364 | + let mut this = self; |
| 365 | + let data = this.get_watcher_data(); |
| 366 | + assert!(data.close_cb.is_none()); |
| 367 | + data.close_cb = Some(cb); |
| 368 | + } |
| 369 | + |
| 370 | + unsafe { uvll::close(self.native_handle(), close_cb); } |
| 371 | + |
| 372 | + extern fn close_cb(handle: *uvll::uv_udp_t) { |
| 373 | + let mut udp_watcher: UdpWatcher = NativeHandle::from_native_handle(handle); |
| 374 | + { |
| 375 | + let data = udp_watcher.get_watcher_data(); |
| 376 | + data.close_cb.swap_unwrap()(); |
| 377 | + } |
| 378 | + udp_watcher.drop_watcher_data(); |
| 379 | + unsafe { free_handle(handle as *c_void) } |
| 380 | + } |
| 381 | + } |
| 382 | +} |
| 383 | + |
| 384 | +impl NativeHandle<*uvll::uv_udp_t> for UdpWatcher { |
| 385 | + fn from_native_handle(handle: *uvll::uv_udp_t) -> UdpWatcher { |
| 386 | + UdpWatcher(handle) |
| 387 | + } |
| 388 | + fn native_handle(&self) -> *uvll::uv_udp_t { |
| 389 | + match self { &UdpWatcher(ptr) => ptr } |
| 390 | + } |
| 391 | +} |
| 392 | + |
257 | 393 | // uv_connect_t is a subclass of uv_req_t
|
258 | 394 | struct ConnectRequest(*uvll::uv_connect_t);
|
259 | 395 | impl Request for ConnectRequest { }
|
@@ -327,6 +463,40 @@ impl NativeHandle<*uvll::uv_write_t> for WriteRequest {
|
327 | 463 | }
|
328 | 464 | }
|
329 | 465 |
|
| 466 | +pub struct UdpSendRequest(*uvll::uv_udp_send_t); |
| 467 | + |
| 468 | +impl Request for UdpSendRequest { } |
| 469 | + |
| 470 | +pub impl UdpSendRequest { |
| 471 | + fn new() -> UdpSendRequest { |
| 472 | + let send_handle = unsafe { |
| 473 | + malloc_req(UV_UDP_SEND) |
| 474 | + }; |
| 475 | + assert!(send_handle.is_not_null()); |
| 476 | + let send_handle = send_handle as *uvll::uv_udp_send_t; |
| 477 | + UdpSendRequest(send_handle) |
| 478 | + } |
| 479 | + |
| 480 | + fn handle(&self) -> UdpWatcher { |
| 481 | + unsafe { |
| 482 | + let udp_handle = uvll::get_udp_handle_from_send_req(self.native_handle()); |
| 483 | + NativeHandle::from_native_handle(udp_handle) |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + fn delete(self) { |
| 488 | + unsafe { free_req(self.native_handle() as *c_void) } |
| 489 | + } |
| 490 | +} |
| 491 | + |
| 492 | +impl NativeHandle<*uvll::uv_udp_send_t> for UdpSendRequest { |
| 493 | + fn from_native_handle(handle: *uvll::uv_udp_send_t) -> UdpSendRequest { |
| 494 | + UdpSendRequest(handle) |
| 495 | + } |
| 496 | + fn native_handle(&self) -> *uvll::uv_udp_send_t { |
| 497 | + match self { &UdpSendRequest(ptr) => ptr } |
| 498 | + } |
| 499 | +} |
330 | 500 |
|
331 | 501 | #[cfg(test)]
|
332 | 502 | mod test {
|
|
0 commit comments