Skip to content

Commit 022a01d

Browse files
committed
std: Add experimental networking methods
The underlying I/O objects implement a good deal of various options here and there for tuning network sockets and how they perform. Most of this is a relic of "whatever libuv provides", but these options are genuinely useful. It is unclear at this time whether these options should be well supported or not, or whether they have correct names or not. For now, I believe it's better to expose the functionality than to not, but all new methods are added with an #[experimental] annotation.
1 parent a28a701 commit 022a01d

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/libstd/io/net/tcp.rs

+23
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,29 @@ impl TcpStream {
8585
pub fn socket_name(&mut self) -> IoResult<SocketAddr> {
8686
self.obj.socket_name()
8787
}
88+
89+
/// Sets the nodelay flag on this connection to the boolean specified
90+
#[experimental]
91+
pub fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> {
92+
if nodelay {
93+
self.obj.nodelay()
94+
} else {
95+
self.obj.control_congestion()
96+
}
97+
}
98+
99+
/// Sets the keepalive timeout to the timeout specified.
100+
///
101+
/// If the value specified is `None`, then the keepalive flag is cleared on
102+
/// this connection. Otherwise, the keepalive timeout will be set to the
103+
/// specified time, in seconds.
104+
#[experimental]
105+
pub fn set_keepalive(&mut self, delay_in_seconds: Option<uint>) -> IoResult<()> {
106+
match delay_in_seconds {
107+
Some(i) => self.obj.keepalive(i),
108+
None => self.obj.letdie(),
109+
}
110+
}
88111
}
89112

90113
impl Clone for TcpStream {

src/libstd/io/net/udp.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! datagram protocol.
1717
1818
use clone::Clone;
19-
use io::net::ip::SocketAddr;
19+
use io::net::ip::{SocketAddr, IpAddr};
2020
use io::{Reader, Writer, IoResult};
2121
use kinds::Send;
2222
use result::{Ok, Err};
@@ -95,6 +95,52 @@ impl UdpSocket {
9595
pub fn socket_name(&mut self) -> IoResult<SocketAddr> {
9696
self.obj.socket_name()
9797
}
98+
99+
/// Joins a multicast IP address (becomes a member of it)
100+
#[experimental]
101+
pub fn join_multicast(&mut self, multi: IpAddr) -> IoResult<()> {
102+
self.obj.join_multicast(multi)
103+
}
104+
105+
/// Leaves a multicast IP address (drops membership from it)
106+
#[experimental]
107+
pub fn leave_multicast(&mut self, multi: IpAddr) -> IoResult<()> {
108+
self.obj.leave_multicast(multi)
109+
}
110+
111+
/// Set the multicast loop flag to the specified value
112+
///
113+
/// This lets multicast packets loop back to local sockets (if enabled)
114+
#[experimental]
115+
pub fn set_multicast_loop(&mut self, on: bool) -> IoResult<()> {
116+
if on {
117+
self.obj.loop_multicast_locally()
118+
} else {
119+
self.obj.dont_loop_multicast_locally()
120+
}
121+
}
122+
123+
/// Sets the multicast TTL
124+
#[experimental]
125+
pub fn set_multicast_ttl(&mut self, ttl: int) -> IoResult<()> {
126+
self.obj.multicast_time_to_live(ttl)
127+
}
128+
129+
/// Sets this socket's TTL
130+
#[experimental]
131+
pub fn set_ttl(&mut self, ttl: int) -> IoResult<()> {
132+
self.obj.time_to_live(ttl)
133+
}
134+
135+
/// Sets the broadcast flag on or off
136+
#[experimental]
137+
pub fn set_broadast(&mut self, broadcast: bool) -> IoResult<()> {
138+
if broadcast {
139+
self.obj.hear_broadcasts()
140+
} else {
141+
self.obj.ignore_broadcasts()
142+
}
143+
}
98144
}
99145

100146
impl Clone for UdpSocket {

0 commit comments

Comments
 (0)