2013-04-17 17:55:21 -07:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-06-19 17:23:55 -07:00
|
|
|
use option::{Option, Some, None};
|
|
|
|
use result::{Ok, Err};
|
2013-08-03 02:06:24 +04:00
|
|
|
use rt::io::net::ip::SocketAddr;
|
2013-06-19 17:23:55 -07:00
|
|
|
use rt::io::{Reader, Writer};
|
|
|
|
use rt::io::{io_error, read_error, EndOfFile};
|
2013-10-16 17:05:28 -07:00
|
|
|
use rt::rtio::{RtioSocket, RtioUdpSocket, IoFactory, with_local_io};
|
2013-06-19 17:23:55 -07:00
|
|
|
|
2013-10-02 18:52:03 -07:00
|
|
|
pub struct UdpSocket {
|
2013-10-16 14:48:05 -07:00
|
|
|
priv obj: ~RtioUdpSocket
|
2013-10-02 18:52:03 -07:00
|
|
|
}
|
2013-06-19 17:23:55 -07:00
|
|
|
|
|
|
|
impl UdpSocket {
|
2013-08-03 02:06:24 +04:00
|
|
|
pub fn bind(addr: SocketAddr) -> Option<UdpSocket> {
|
2013-10-16 17:05:28 -07:00
|
|
|
do with_local_io |io| {
|
|
|
|
match io.udp_bind(addr) {
|
|
|
|
Ok(s) => Some(UdpSocket { obj: s }),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 02:06:24 +04:00
|
|
|
pub fn recvfrom(&mut self, buf: &mut [u8]) -> Option<(uint, SocketAddr)> {
|
2013-10-02 18:52:03 -07:00
|
|
|
match self.obj.recvfrom(buf) {
|
2013-06-19 17:23:55 -07:00
|
|
|
Ok((nread, src)) => Some((nread, src)),
|
|
|
|
Err(ioerr) => {
|
|
|
|
// EOF is indicated by returning None
|
|
|
|
if ioerr.kind != EndOfFile {
|
|
|
|
read_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 02:06:24 +04:00
|
|
|
pub fn sendto(&mut self, buf: &[u8], dst: SocketAddr) {
|
2013-10-02 18:52:03 -07:00
|
|
|
match self.obj.sendto(buf, dst) {
|
2013-06-19 17:23:55 -07:00
|
|
|
Ok(_) => (),
|
2013-06-25 17:05:59 -07:00
|
|
|
Err(ioerr) => io_error::cond.raise(ioerr),
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 02:06:24 +04:00
|
|
|
pub fn connect(self, other: SocketAddr) -> UdpStream {
|
2013-06-19 17:23:55 -07:00
|
|
|
UdpStream { socket: self, connectedTo: other }
|
|
|
|
}
|
2013-07-25 02:33:43 -04:00
|
|
|
|
2013-08-03 02:06:24 +04:00
|
|
|
pub fn socket_name(&mut self) -> Option<SocketAddr> {
|
2013-10-02 18:52:03 -07:00
|
|
|
match self.obj.socket_name() {
|
2013-07-25 02:33:43 -04:00
|
|
|
Ok(sn) => Some(sn),
|
|
|
|
Err(ioerr) => {
|
2013-09-27 17:02:31 -07:00
|
|
|
rtdebug!("failed to get socket name: {:?}", ioerr);
|
2013-07-25 02:33:43 -04:00
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
|
2013-06-17 12:34:58 -07:00
|
|
|
pub struct UdpStream {
|
2013-10-02 18:52:03 -07:00
|
|
|
priv socket: UdpSocket,
|
|
|
|
priv connectedTo: SocketAddr
|
2013-06-17 12:34:58 -07:00
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
|
|
|
|
impl UdpStream {
|
2013-07-17 15:16:44 -07:00
|
|
|
pub fn as_socket<T>(&mut self, f: &fn(&mut UdpSocket) -> T) -> T { f(&mut self.socket) }
|
2013-06-17 12:34:58 -07:00
|
|
|
|
2013-06-25 17:05:59 -07:00
|
|
|
pub fn disconnect(self) -> UdpSocket { self.socket }
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for UdpStream {
|
2013-06-25 14:40:36 -07:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
2013-07-17 15:16:44 -07:00
|
|
|
let peer = self.connectedTo;
|
2013-06-19 17:23:55 -07:00
|
|
|
do self.as_socket |sock| {
|
2013-06-25 11:43:40 -07:00
|
|
|
match sock.recvfrom(buf) {
|
2013-07-17 15:16:44 -07:00
|
|
|
Some((_nread, src)) if src != peer => Some(0),
|
2013-06-25 11:43:40 -07:00
|
|
|
Some((nread, _src)) => Some(nread),
|
|
|
|
None => None,
|
|
|
|
}
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
|
2013-10-21 13:08:31 -07:00
|
|
|
fn eof(&mut self) -> bool { fail!() }
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for UdpStream {
|
2013-06-19 17:23:55 -07:00
|
|
|
fn write(&mut self, buf: &[u8]) {
|
|
|
|
do self.as_socket |sock| {
|
|
|
|
sock.sendto(buf, self.connectedTo);
|
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
|
|
|
|
2013-10-21 13:08:31 -07:00
|
|
|
fn flush(&mut self) { fail!() }
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
2013-06-25 11:43:40 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use rt::test::*;
|
2013-08-03 02:06:24 +04:00
|
|
|
use rt::io::net::ip::{Ipv4Addr, SocketAddr};
|
2013-06-25 11:43:40 -07:00
|
|
|
use rt::io::*;
|
|
|
|
use option::{Some, None};
|
2013-09-04 18:09:03 -07:00
|
|
|
use rt::comm::oneshot;
|
|
|
|
use cell::Cell;
|
2013-06-25 11:43:40 -07:00
|
|
|
|
|
|
|
#[test] #[ignore]
|
|
|
|
fn bind_error() {
|
2013-09-04 18:09:03 -07:00
|
|
|
do run_in_mt_newsched_task {
|
2013-06-25 11:43:40 -07:00
|
|
|
let mut called = false;
|
|
|
|
do io_error::cond.trap(|e| {
|
|
|
|
assert!(e.kind == PermissionDenied);
|
|
|
|
called = true;
|
2013-07-31 17:59:59 -04:00
|
|
|
}).inside {
|
2013-08-03 02:06:24 +04:00
|
|
|
let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };
|
2013-06-25 11:43:40 -07:00
|
|
|
let socket = UdpSocket::bind(addr);
|
|
|
|
assert!(socket.is_none());
|
|
|
|
}
|
|
|
|
assert!(called);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 14:40:36 -07:00
|
|
|
#[test]
|
2013-07-02 16:40:57 -07:00
|
|
|
fn socket_smoke_test_ip4() {
|
2013-09-04 18:09:03 -07:00
|
|
|
do run_in_mt_newsched_task {
|
2013-06-25 11:43:40 -07:00
|
|
|
let server_ip = next_test_ip4();
|
|
|
|
let client_ip = next_test_ip4();
|
2013-09-04 18:09:03 -07:00
|
|
|
let (port, chan) = oneshot();
|
|
|
|
let port = Cell::new(port);
|
|
|
|
let chan = Cell::new(chan);
|
2013-06-25 11:43:40 -07:00
|
|
|
|
2013-07-19 14:25:05 -07:00
|
|
|
do spawntask {
|
2013-06-25 11:43:40 -07:00
|
|
|
match UdpSocket::bind(server_ip) {
|
2013-07-17 15:16:44 -07:00
|
|
|
Some(ref mut server) => {
|
2013-09-04 18:09:03 -07:00
|
|
|
chan.take().send(());
|
2013-06-25 11:43:40 -07:00
|
|
|
let mut buf = [0];
|
|
|
|
match server.recvfrom(buf) {
|
|
|
|
Some((nread, src)) => {
|
|
|
|
assert_eq!(nread, 1);
|
|
|
|
assert_eq!(buf[0], 99);
|
|
|
|
assert_eq!(src, client_ip);
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:25:05 -07:00
|
|
|
do spawntask {
|
2013-06-25 11:43:40 -07:00
|
|
|
match UdpSocket::bind(client_ip) {
|
2013-09-04 18:09:03 -07:00
|
|
|
Some(ref mut client) => {
|
|
|
|
port.take().recv();
|
|
|
|
client.sendto([99], server_ip)
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-07-02 16:40:57 -07:00
|
|
|
fn socket_smoke_test_ip6() {
|
2013-09-04 18:09:03 -07:00
|
|
|
do run_in_mt_newsched_task {
|
2013-07-02 16:40:57 -07:00
|
|
|
let server_ip = next_test_ip6();
|
|
|
|
let client_ip = next_test_ip6();
|
2013-09-04 18:09:03 -07:00
|
|
|
let (port, chan) = oneshot();
|
|
|
|
let port = Cell::new(port);
|
|
|
|
let chan = Cell::new(chan);
|
2013-07-02 16:40:57 -07:00
|
|
|
|
2013-07-19 14:25:05 -07:00
|
|
|
do spawntask {
|
2013-07-02 16:40:57 -07:00
|
|
|
match UdpSocket::bind(server_ip) {
|
2013-07-17 15:16:44 -07:00
|
|
|
Some(ref mut server) => {
|
2013-09-04 18:09:03 -07:00
|
|
|
chan.take().send(());
|
2013-07-02 16:40:57 -07:00
|
|
|
let mut buf = [0];
|
|
|
|
match server.recvfrom(buf) {
|
|
|
|
Some((nread, src)) => {
|
|
|
|
assert_eq!(nread, 1);
|
|
|
|
assert_eq!(buf[0], 99);
|
|
|
|
assert_eq!(src, client_ip);
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:25:05 -07:00
|
|
|
do spawntask {
|
2013-07-02 16:40:57 -07:00
|
|
|
match UdpSocket::bind(client_ip) {
|
2013-09-04 18:09:03 -07:00
|
|
|
Some(ref mut client) => {
|
|
|
|
port.take().recv();
|
|
|
|
client.sendto([99], server_ip)
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn stream_smoke_test_ip4() {
|
2013-09-04 18:09:03 -07:00
|
|
|
do run_in_mt_newsched_task {
|
2013-06-25 11:43:40 -07:00
|
|
|
let server_ip = next_test_ip4();
|
|
|
|
let client_ip = next_test_ip4();
|
2013-09-04 18:09:03 -07:00
|
|
|
let (port, chan) = oneshot();
|
|
|
|
let port = Cell::new(port);
|
|
|
|
let chan = Cell::new(chan);
|
2013-06-25 11:43:40 -07:00
|
|
|
|
2013-07-19 14:25:05 -07:00
|
|
|
do spawntask {
|
2013-06-25 11:43:40 -07:00
|
|
|
match UdpSocket::bind(server_ip) {
|
2013-07-02 16:40:57 -07:00
|
|
|
Some(server) => {
|
|
|
|
let server = ~server;
|
|
|
|
let mut stream = server.connect(client_ip);
|
2013-09-04 18:09:03 -07:00
|
|
|
chan.take().send(());
|
2013-07-02 16:40:57 -07:00
|
|
|
let mut buf = [0];
|
|
|
|
match stream.read(buf) {
|
|
|
|
Some(nread) => {
|
|
|
|
assert_eq!(nread, 1);
|
|
|
|
assert_eq!(buf[0], 99);
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:25:05 -07:00
|
|
|
do spawntask {
|
2013-07-02 16:40:57 -07:00
|
|
|
match UdpSocket::bind(client_ip) {
|
|
|
|
Some(client) => {
|
|
|
|
let client = ~client;
|
|
|
|
let mut stream = client.connect(server_ip);
|
2013-09-04 18:09:03 -07:00
|
|
|
port.take().recv();
|
2013-07-02 16:40:57 -07:00
|
|
|
stream.write([99]);
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-07-02 16:40:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn stream_smoke_test_ip6() {
|
2013-09-04 18:09:03 -07:00
|
|
|
do run_in_mt_newsched_task {
|
2013-07-02 16:40:57 -07:00
|
|
|
let server_ip = next_test_ip6();
|
|
|
|
let client_ip = next_test_ip6();
|
2013-09-04 18:09:03 -07:00
|
|
|
let (port, chan) = oneshot();
|
|
|
|
let port = Cell::new(port);
|
|
|
|
let chan = Cell::new(chan);
|
2013-07-02 16:40:57 -07:00
|
|
|
|
2013-07-19 14:25:05 -07:00
|
|
|
do spawntask {
|
2013-07-02 16:40:57 -07:00
|
|
|
match UdpSocket::bind(server_ip) {
|
2013-06-25 11:43:40 -07:00
|
|
|
Some(server) => {
|
|
|
|
let server = ~server;
|
|
|
|
let mut stream = server.connect(client_ip);
|
2013-09-04 18:09:03 -07:00
|
|
|
chan.take().send(());
|
2013-06-25 11:43:40 -07:00
|
|
|
let mut buf = [0];
|
|
|
|
match stream.read(buf) {
|
|
|
|
Some(nread) => {
|
|
|
|
assert_eq!(nread, 1);
|
|
|
|
assert_eq!(buf[0], 99);
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:25:05 -07:00
|
|
|
do spawntask {
|
2013-06-25 11:43:40 -07:00
|
|
|
match UdpSocket::bind(client_ip) {
|
|
|
|
Some(client) => {
|
|
|
|
let client = ~client;
|
|
|
|
let mut stream = client.connect(server_ip);
|
2013-09-04 18:09:03 -07:00
|
|
|
port.take().recv();
|
2013-06-25 11:43:40 -07:00
|
|
|
stream.write([99]);
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 05:02:53 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2013-08-03 02:06:24 +04:00
|
|
|
fn socket_name(addr: SocketAddr) {
|
2013-09-04 18:09:03 -07:00
|
|
|
do run_in_mt_newsched_task {
|
2013-07-29 13:34:08 -07:00
|
|
|
do spawntask {
|
2013-07-26 05:02:53 -04:00
|
|
|
let server = UdpSocket::bind(addr);
|
|
|
|
|
|
|
|
assert!(server.is_some());
|
|
|
|
let mut server = server.unwrap();
|
|
|
|
|
|
|
|
// Make sure socket_name gives
|
|
|
|
// us the socket we binded to.
|
|
|
|
let so_name = server.socket_name();
|
|
|
|
assert!(so_name.is_some());
|
|
|
|
assert_eq!(addr, so_name.unwrap());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn socket_name_ip4() {
|
|
|
|
socket_name(next_test_ip4());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn socket_name_ip6() {
|
|
|
|
socket_name(next_test_ip6());
|
|
|
|
}
|
2013-06-25 11:43:40 -07:00
|
|
|
}
|