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-06-17 12:34:58 -07:00
|
|
|
use rt::io::net::ip::IpAddr;
|
2013-06-19 17:23:55 -07:00
|
|
|
use rt::io::{Reader, Writer};
|
|
|
|
use rt::io::{io_error, read_error, EndOfFile};
|
|
|
|
use rt::rtio::{RtioUdpSocketObject, RtioUdpSocket, IoFactory, IoFactoryObject};
|
|
|
|
use rt::local::Local;
|
|
|
|
|
|
|
|
pub struct UdpSocket {
|
|
|
|
rtsocket: ~RtioUdpSocketObject
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UdpSocket {
|
|
|
|
fn new(s: ~RtioUdpSocketObject) -> UdpSocket {
|
|
|
|
UdpSocket { rtsocket: s }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bind(addr: IpAddr) -> Option<UdpSocket> {
|
|
|
|
let socket = unsafe {
|
|
|
|
let io = Local::unsafe_borrow::<IoFactoryObject>();
|
|
|
|
(*io).udp_bind(addr)
|
|
|
|
};
|
|
|
|
match socket {
|
|
|
|
Ok(s) => { Some(UdpSocket { rtsocket: s }) }
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn recvfrom(&self, buf: &mut [u8]) -> Option<(uint, IpAddr)> {
|
|
|
|
match (*self.rtsocket).recvfrom(buf) {
|
|
|
|
Ok((nread, src)) => Some((nread, src)),
|
|
|
|
Err(ioerr) => {
|
|
|
|
// EOF is indicated by returning None
|
|
|
|
if ioerr.kind != EndOfFile {
|
|
|
|
read_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sendto(&self, buf: &[u8], dst: IpAddr) {
|
|
|
|
match (*self.rtsocket).sendto(buf, dst) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX convert ~self to self eventually
|
|
|
|
pub fn connect(~self, other: IpAddr) -> UdpStream {
|
|
|
|
UdpStream { socket: self, connectedTo: other }
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
|
2013-06-17 12:34:58 -07:00
|
|
|
pub struct UdpStream {
|
2013-06-19 17:23:55 -07:00
|
|
|
socket: ~UdpSocket,
|
|
|
|
connectedTo: IpAddr
|
2013-06-17 12:34:58 -07:00
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
|
|
|
|
impl UdpStream {
|
2013-06-19 17:23:55 -07:00
|
|
|
pub fn as_socket<T>(&self, f: &fn(&UdpSocket) -> T) -> T {
|
|
|
|
f(self.socket)
|
2013-06-17 12:34:58 -07:00
|
|
|
}
|
|
|
|
|
2013-06-19 17:23:55 -07:00
|
|
|
pub fn disconnect(self) -> ~UdpSocket {
|
|
|
|
let UdpStream { socket: s, _ } = self;
|
|
|
|
s
|
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-06-19 17:23:55 -07:00
|
|
|
do self.as_socket |sock| {
|
2013-06-25 11:43:40 -07:00
|
|
|
match sock.recvfrom(buf) {
|
|
|
|
Some((_nread, src)) if src != self.connectedTo => Some(0),
|
|
|
|
Some((nread, _src)) => Some(nread),
|
|
|
|
None => None,
|
|
|
|
}
|
2013-06-19 17:23:55 -07:00
|
|
|
}
|
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
|
|
|
|
fn eof(&mut self) -> bool { fail!() }
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-19 17:23:55 -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::*;
|
|
|
|
use rt::io::net::ip::Ipv4;
|
|
|
|
use rt::io::*;
|
|
|
|
use option::{Some, None};
|
|
|
|
|
|
|
|
#[test] #[ignore]
|
|
|
|
fn bind_error() {
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let mut called = false;
|
|
|
|
do io_error::cond.trap(|e| {
|
|
|
|
assert!(e.kind == PermissionDenied);
|
|
|
|
called = true;
|
|
|
|
}).in {
|
|
|
|
let addr = Ipv4(0, 0, 0, 0, 1);
|
|
|
|
let socket = UdpSocket::bind(addr);
|
|
|
|
assert!(socket.is_none());
|
|
|
|
}
|
|
|
|
assert!(called);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 14:40:36 -07:00
|
|
|
#[test]
|
2013-06-25 11:43:40 -07:00
|
|
|
fn socket_smoke_test() {
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let server_ip = next_test_ip4();
|
|
|
|
let client_ip = next_test_ip4();
|
|
|
|
|
|
|
|
do spawntask_immediately {
|
|
|
|
match UdpSocket::bind(server_ip) {
|
|
|
|
Some(server) => {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
None => fail!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => fail!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do spawntask_immediately {
|
|
|
|
match UdpSocket::bind(client_ip) {
|
|
|
|
Some(client) => client.sendto([99], server_ip),
|
|
|
|
None => fail!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn stream_smoke_test() {
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let server_ip = next_test_ip4();
|
|
|
|
let client_ip = next_test_ip4();
|
|
|
|
|
|
|
|
do spawntask_immediately {
|
|
|
|
match UdpSocket::bind(server_ip) {
|
|
|
|
Some(server) => {
|
|
|
|
let server = ~server;
|
|
|
|
let mut stream = server.connect(client_ip);
|
|
|
|
let mut buf = [0];
|
|
|
|
match stream.read(buf) {
|
|
|
|
Some(nread) => {
|
|
|
|
assert_eq!(nread, 1);
|
|
|
|
assert_eq!(buf[0], 99);
|
|
|
|
}
|
|
|
|
None => fail!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => fail!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do spawntask_immediately {
|
|
|
|
match UdpSocket::bind(client_ip) {
|
|
|
|
Some(client) => {
|
|
|
|
let client = ~client;
|
|
|
|
let mut stream = client.connect(server_ip);
|
|
|
|
stream.write([99]);
|
|
|
|
}
|
|
|
|
None => fail!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|