Skip to content

UDP and TCP methods should take &mut self to reflect that libuv may change the underlying handle. #7908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/libstd/rt/io/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl UdpSocket {
}
}

pub fn recvfrom(&self, buf: &mut [u8]) -> Option<(uint, IpAddr)> {
pub fn recvfrom(&mut self, buf: &mut [u8]) -> Option<(uint, IpAddr)> {
match (**self).recvfrom(buf) {
Ok((nread, src)) => Some((nread, src)),
Err(ioerr) => {
Expand All @@ -43,7 +43,7 @@ impl UdpSocket {
}
}

pub fn sendto(&self, buf: &[u8], dst: IpAddr) {
pub fn sendto(&mut self, buf: &[u8], dst: IpAddr) {
match (**self).sendto(buf, dst) {
Ok(_) => (),
Err(ioerr) => io_error::cond.raise(ioerr),
Expand All @@ -61,16 +61,17 @@ pub struct UdpStream {
}

impl UdpStream {
pub fn as_socket<T>(&self, f: &fn(&UdpSocket) -> T) -> T { f(&self.socket) }
pub fn as_socket<T>(&mut self, f: &fn(&mut UdpSocket) -> T) -> T { f(&mut self.socket) }

pub fn disconnect(self) -> UdpSocket { self.socket }
}

impl Reader for UdpStream {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
let peer = self.connectedTo;
do self.as_socket |sock| {
match sock.recvfrom(buf) {
Some((_nread, src)) if src != self.connectedTo => Some(0),
Some((_nread, src)) if src != peer => Some(0),
Some((nread, _src)) => Some(nread),
None => None,
}
Expand Down Expand Up @@ -122,7 +123,7 @@ mod test {

do spawntask_immediately {
match UdpSocket::bind(server_ip) {
Some(server) => {
Some(ref mut server) => {
let mut buf = [0];
match server.recvfrom(buf) {
Some((nread, src)) => {
Expand All @@ -139,7 +140,7 @@ mod test {

do spawntask_immediately {
match UdpSocket::bind(client_ip) {
Some(client) => client.sendto([99], server_ip),
Some(ref mut client) => client.sendto([99], server_ip),
None => fail!()
}
}
Expand All @@ -154,7 +155,7 @@ mod test {

do spawntask_immediately {
match UdpSocket::bind(server_ip) {
Some(server) => {
Some(ref mut server) => {
let mut buf = [0];
match server.recvfrom(buf) {
Some((nread, src)) => {
Expand All @@ -171,7 +172,7 @@ mod test {

do spawntask_immediately {
match UdpSocket::bind(client_ip) {
Some(client) => client.sendto([99], server_ip),
Some(ref mut client) => client.sendto([99], server_ip),
None => fail!()
}
}
Expand Down
40 changes: 20 additions & 20 deletions src/libstd/rt/rtio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,37 @@ pub trait IoFactory {

pub trait RtioTcpListener : RtioSocket {
fn accept(&mut self) -> Result<~RtioTcpStreamObject, IoError>;
fn accept_simultaneously(&self);
fn dont_accept_simultaneously(&self);
fn accept_simultaneously(&mut self);
fn dont_accept_simultaneously(&mut self);
}

pub trait RtioTcpStream : RtioSocket {
fn read(&self, buf: &mut [u8]) -> Result<uint, IoError>;
fn write(&self, buf: &[u8]) -> Result<(), IoError>;
fn peer_name(&self) -> IpAddr;
fn control_congestion(&self);
fn nodelay(&self);
fn keepalive(&self, delay_in_seconds: uint);
fn letdie(&self);
fn read(&mut self, buf: &mut [u8]) -> Result<uint, IoError>;
fn write(&mut self, buf: &[u8]) -> Result<(), IoError>;
fn peer_name(&mut self) -> IpAddr;
fn control_congestion(&mut self);
fn nodelay(&mut self);
fn keepalive(&mut self, delay_in_seconds: uint);
fn letdie(&mut self);
}

pub trait RtioSocket {
fn socket_name(&self) -> IpAddr;
fn socket_name(&mut self) -> IpAddr;
}

pub trait RtioUdpSocket : RtioSocket {
fn recvfrom(&self, buf: &mut [u8]) -> Result<(uint, IpAddr), IoError>;
fn sendto(&self, buf: &[u8], dst: IpAddr) -> Result<(), IoError>;
fn recvfrom(&mut self, buf: &mut [u8]) -> Result<(uint, IpAddr), IoError>;
fn sendto(&mut self, buf: &[u8], dst: IpAddr) -> Result<(), IoError>;

fn join_multicast(&self, multi: IpAddr);
fn leave_multicast(&self, multi: IpAddr);
fn join_multicast(&mut self, multi: IpAddr);
fn leave_multicast(&mut self, multi: IpAddr);

fn loop_multicast_locally(&self);
fn dont_loop_multicast_locally(&self);
fn loop_multicast_locally(&mut self);
fn dont_loop_multicast_locally(&mut self);

fn multicast_time_to_live(&self, ttl: int);
fn time_to_live(&self, ttl: int);
fn multicast_time_to_live(&mut self, ttl: int);
fn time_to_live(&mut self, ttl: int);

fn hear_broadcasts(&self);
fn ignore_broadcasts(&self);
fn hear_broadcasts(&mut self);
fn ignore_broadcasts(&mut self);
}
30 changes: 14 additions & 16 deletions src/libstd/rt/uv/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl UdpWatcher {
}
}

pub fn bind(&self, address: IpAddr) -> Result<(), UvError> {
pub fn bind(&mut self, address: IpAddr) -> Result<(), UvError> {
do ip_as_uv_ip(address) |addr| {
let result = unsafe {
match addr {
Expand All @@ -374,10 +374,9 @@ impl UdpWatcher {
}
}

pub fn recv_start(&self, alloc: AllocCallback, cb: UdpReceiveCallback) {
pub fn recv_start(&mut self, alloc: AllocCallback, cb: UdpReceiveCallback) {
{
let mut this = *self;
let data = this.get_watcher_data();
let data = self.get_watcher_data();
data.alloc_cb = Some(alloc);
data.udp_recv_cb = Some(cb);
}
Expand Down Expand Up @@ -409,14 +408,13 @@ impl UdpWatcher {
}
}

pub fn recv_stop(&self) {
pub fn recv_stop(&mut self) {
unsafe { uvll::udp_recv_stop(self.native_handle()); }
}

pub fn send(&self, buf: Buf, address: IpAddr, cb: UdpSendCallback) {
pub fn send(&mut self, buf: Buf, address: IpAddr, cb: UdpSendCallback) {
{
let mut this = *self;
let data = this.get_watcher_data();
let data = self.get_watcher_data();
assert!(data.udp_send_cb.is_none());
data.udp_send_cb = Some(cb);
}
Expand Down Expand Up @@ -620,7 +618,7 @@ mod test {
fn udp_bind_close_ip4() {
do run_in_bare_thread() {
let mut loop_ = Loop::new();
let udp_watcher = { UdpWatcher::new(&mut loop_) };
let mut udp_watcher = { UdpWatcher::new(&mut loop_) };
let addr = next_test_ip4();
udp_watcher.bind(addr);
udp_watcher.close(||());
Expand All @@ -633,7 +631,7 @@ mod test {
fn udp_bind_close_ip6() {
do run_in_bare_thread() {
let mut loop_ = Loop::new();
let udp_watcher = { UdpWatcher::new(&mut loop_) };
let mut udp_watcher = { UdpWatcher::new(&mut loop_) };
let addr = next_test_ip6();
udp_watcher.bind(addr);
udp_watcher.close(||());
Expand Down Expand Up @@ -798,15 +796,15 @@ mod test {
let server_addr = next_test_ip4();
let client_addr = next_test_ip4();

let server = UdpWatcher::new(&loop_);
let mut server = UdpWatcher::new(&loop_);
assert!(server.bind(server_addr).is_ok());

rtdebug!("starting read");
let alloc: AllocCallback = |size| {
vec_to_uv_buf(vec::from_elem(size, 0u8))
};

do server.recv_start(alloc) |server, nread, buf, src, flags, status| {
do server.recv_start(alloc) |mut server, nread, buf, src, flags, status| {
server.recv_stop();
rtdebug!("i'm reading!");
assert!(status.is_none());
Expand All @@ -830,7 +828,7 @@ mod test {

do Thread::start {
let mut loop_ = Loop::new();
let client = UdpWatcher::new(&loop_);
let mut client = UdpWatcher::new(&loop_);
assert!(client.bind(client_addr).is_ok());
let msg = ~[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let buf = slice_to_uv_buf(msg);
Expand All @@ -857,15 +855,15 @@ mod test {
let server_addr = next_test_ip6();
let client_addr = next_test_ip6();

let server = UdpWatcher::new(&loop_);
let mut server = UdpWatcher::new(&loop_);
assert!(server.bind(server_addr).is_ok());

rtdebug!("starting read");
let alloc: AllocCallback = |size| {
vec_to_uv_buf(vec::from_elem(size, 0u8))
};

do server.recv_start(alloc) |server, nread, buf, src, flags, status| {
do server.recv_start(alloc) |mut server, nread, buf, src, flags, status| {
server.recv_stop();
rtdebug!("i'm reading!");
assert!(status.is_none());
Expand All @@ -889,7 +887,7 @@ mod test {

do Thread::start {
let mut loop_ = Loop::new();
let client = UdpWatcher::new(&loop_);
let mut client = UdpWatcher::new(&loop_);
assert!(client.bind(client_addr).is_ok());
let msg = ~[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let buf = slice_to_uv_buf(msg);
Expand Down
Loading