Skip to content

Commit 2a84f59

Browse files
author
pierresy
committed
Added set_timeout() method to SctpStream
1 parent 8242263 commit 2a84f59

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

examples/stream.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ fn main() {
1515
println!("{}", peer.has_nodelay().unwrap());
1616

1717
// Set socket send buffer size
18-
let oldsize = peer.get_buffer_size(SoBuffer::Send).unwrap();
19-
peer.set_buffer_size(SoBuffer::Send, 4096).unwrap();
20-
println!("Set send buffer size to {} (was : {})", peer.get_buffer_size(SoBuffer::Send).unwrap(), oldsize);
18+
let oldsize = peer.get_buffer_size(SoDirection::Send).unwrap();
19+
peer.set_buffer_size(SoDirection::Send, 4096).unwrap();
20+
println!("Set send buffer size to {} (was : {})", peer.get_buffer_size(SoDirection::Send).unwrap(), oldsize);
21+
22+
println!("Setting read timeout to 10 s");
23+
peer.set_timeout(SoDirection::Receive, 10).unwrap();
2124

2225
// Write a message using the io::Write trait
2326
peer.write_all("foo bar\n".as_bytes()).unwrap();

src/lib.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This crate provides high level SCTP networking.
22
//! Currently it only supports basic SCTP features like multi-homing
33
//! in one-to-one and one-to-many associations.
4-
//! SCTP notifications and working directly on associtaion is not supported yet
4+
//! SCTP notifications and working directly on association is not supported yet
55
//! but is in the TODO list.
66
77
extern crate sctp_sys;
@@ -20,19 +20,26 @@ use std::os::unix::io::{AsRawFd, RawFd, FromRawFd};
2020
#[cfg(target_os="windows")]
2121
use std::os::windows::io::{AsRawHandle, RawHandle, FromRawHandle};
2222

23-
/// Socket buffer type
24-
pub enum SoBuffer {
25-
/// RCV buffer
23+
/// Socket direction
24+
pub enum SoDirection {
25+
/// RCV direction
2626
Receive,
27-
/// SND buffer
27+
/// SND direction
2828
Send
2929
}
3030

31-
impl SoBuffer {
32-
fn optname(&self) -> libc::c_int {
31+
impl SoDirection {
32+
fn buffer_opt(&self) -> libc::c_int {
3333
return match *self {
34-
SoBuffer::Receive => libc::SO_RCVBUF,
35-
SoBuffer::Send => libc::SO_SNDBUF
34+
SoDirection::Receive => libc::SO_RCVBUF,
35+
SoDirection::Send => libc::SO_SNDBUF
36+
};
37+
}
38+
39+
fn timeout_opt(&self) -> libc::c_int {
40+
return match *self {
41+
SoDirection::Receive => libc::SO_RCVTIMEO,
42+
SoDirection::Send => libc::SO_SNDTIMEO
3643
};
3744
}
3845
}
@@ -108,18 +115,24 @@ impl SctpStream {
108115
return Ok(val == 1);
109116
}
110117

111-
/// Set the socket buffer size for the buffer specifid by `buf`.
112-
/// Linux system will double the provided size
113-
pub fn set_buffer_size(&self, buf: SoBuffer, size: usize) -> Result<()> {
114-
return self.0.setsockopt(libc::SOL_SOCKET, buf.optname(), &(size as libc::c_int));
118+
/// Set the socket buffer size for the direction specified by `dir`.
119+
/// Linux systems will double the provided size
120+
pub fn set_buffer_size(&self, dir: SoDirection, size: usize) -> Result<()> {
121+
return self.0.setsockopt(libc::SOL_SOCKET, dir.buffer_opt(), &(size as libc::c_int));
115122
}
116123

117-
/// Get the socket buffer size for the buffer specifid by `buf`
118-
pub fn get_buffer_size(&self, buf: SoBuffer) -> Result<(usize)> {
119-
let val: u32 = try!(self.0.getsockopt(libc::SOL_SOCKET, buf.optname()));
124+
/// Get the socket buffer size for the direction specified by `dir`
125+
pub fn get_buffer_size(&self, dir: SoDirection) -> Result<(usize)> {
126+
let val: u32 = try!(self.0.getsockopt(libc::SOL_SOCKET, dir.buffer_opt()));
120127
return Ok(val as usize);
121128
}
122129

130+
/// Set `timeout` in seconds for operation `dir` (either receive or send)
131+
pub fn set_timeout(&self, dir: SoDirection, timeout: i32) -> Result<()> {
132+
let tval = libc::timeval { tv_sec: timeout as libc::c_int, tv_usec: 0 };
133+
return self.0.setsockopt(libc::SOL_SOCKET, dir.timeout_opt(), &tval);
134+
}
135+
123136
/// Try to clone the SctpStream. On success, returns a new stream
124137
/// wrapping a new socket handler
125138
pub fn try_clone(&self) -> Result<SctpStream> {

src/sctpsock.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,6 @@ extern "system" {
4747
#[cfg(target_os="windows")]
4848
fn getsockopt(sock: SOCKET, level: libc::c_int, optname: libc::c_int, optval: *mut libc::c_char, optlen: *mut libc::c_int) -> libc::c_int;
4949
}
50-
#[cfg(target_os="linux")]
51-
pub const SO_RCVTIMEO: libc::c_int = 20;
52-
#[cfg(target_os="linux")]
53-
pub const SO_SNDTIMEO: libc::c_int = 21;
54-
55-
#[cfg(target_os="windows")]
56-
pub const SO_RCVTIMEO: libc::c_int = 0x1006;
57-
#[cfg(target_os="windows")]
58-
pub const SO_SNDTIMEO: libc::c_int = 0x1005;
5950

6051
/// SCTP bind operation
6152
#[allow(dead_code)]
@@ -267,7 +258,7 @@ impl SctpSocket {
267258
let addr_ptr: *mut libc::sockaddr = transmute(&mut addr);
268259
let sock = try!(check_socket(libc::accept(self.0, addr_ptr, &mut len)));
269260
let addr = try!(SocketAddr::from_raw_ptr(addr_ptr, len));
270-
return Ok((SctpSocket::from_raw_fd(sock), addr));
261+
return Ok((SctpSocket(sock), addr));
271262
}
272263
}
273264

@@ -421,12 +412,6 @@ impl SctpSocket {
421412
return Ok(SctpSocket(new_sock));
422413
}
423414
}
424-
425-
/// Wrap the given socket descriptor in a `SctpSocket`. This is unsafe because we can't be sure
426-
/// that the given socket is valid
427-
pub unsafe fn from_raw_fd(sock: SOCKET) -> SctpSocket {
428-
return SctpSocket(sock);
429-
}
430415
}
431416

432417
impl Read for SctpSocket {

0 commit comments

Comments
 (0)