@@ -26,7 +26,7 @@ use time::Duration;
2626// sockaddr and misc bindings
2727////////////////////////////////////////////////////////////////////////////////
2828
29- fn setsockopt < T > ( sock : & Socket , opt : c_int , val : c_int ,
29+ pub fn setsockopt < T > ( sock : & Socket , opt : c_int , val : c_int ,
3030 payload : T ) -> io:: Result < ( ) > {
3131 unsafe {
3232 let payload = & payload as * const T as * const c_void ;
@@ -36,7 +36,7 @@ fn setsockopt<T>(sock: &Socket, opt: c_int, val: c_int,
3636 }
3737}
3838
39- fn getsockopt < T : Copy > ( sock : & Socket , opt : c_int ,
39+ pub fn getsockopt < T : Copy > ( sock : & Socket , opt : c_int ,
4040 val : c_int ) -> io:: Result < T > {
4141 unsafe {
4242 let mut slot: T = mem:: zeroed ( ) ;
@@ -163,92 +163,6 @@ pub fn lookup_addr(addr: &IpAddr) -> io::Result<String> {
163163 }
164164}
165165
166- ////////////////////////////////////////////////////////////////////////////////
167- // Timeouts
168- ////////////////////////////////////////////////////////////////////////////////
169-
170- #[ cfg( target_os = "windows" ) ]
171- fn set_timeout ( socket : & Socket , dur : Option < Duration > , kind : libc:: c_int ) -> io:: Result < ( ) > {
172- let timeout = match dur {
173- Some ( dur) => {
174- if dur. secs ( ) == 0 && dur. extra_nanos ( ) == 0 {
175- return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
176- "cannot set a 0 duration timeout" ) ) ;
177- }
178-
179- let mut timeout = if dur. secs ( ) > ( libc:: DWORD :: max_value ( ) / 1000 ) as u64 {
180- libc:: DWORD :: max_value ( )
181- } else {
182- ( dur. secs ( ) * 1000 ) as libc:: DWORD
183- } ;
184- timeout = timeout. saturating_add ( ( dur. extra_nanos ( ) / 1000000 ) as libc:: DWORD ) ;
185- if timeout == 0 {
186- timeout = 1 ;
187- }
188- timeout
189- }
190- None => 0
191- } ;
192- setsockopt ( socket, libc:: SOL_SOCKET , kind, timeout)
193- }
194-
195- #[ cfg( not( target_os = "windows" ) ) ]
196- fn set_timeout ( socket : & Socket , dur : Option < Duration > , kind : libc:: c_int ) -> io:: Result < ( ) > {
197- let timeout = match dur {
198- Some ( dur) => {
199- if dur. secs ( ) == 0 && dur. extra_nanos ( ) == 0 {
200- return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
201- "cannot set a 0 duration timeout" ) ) ;
202- }
203-
204- let secs = if dur. secs ( ) > libc:: time_t:: max_value ( ) as u64 {
205- libc:: time_t:: max_value ( )
206- } else {
207- dur. secs ( ) as libc:: time_t
208- } ;
209- let mut timeout = libc:: timeval {
210- tv_sec : secs,
211- tv_usec : ( dur. extra_nanos ( ) / 1000 ) as libc:: time_t ,
212- } ;
213- if timeout. tv_sec == 0 && timeout. tv_usec == 0 {
214- timeout. tv_usec = 1 ;
215- }
216- timeout
217- }
218- None => {
219- libc:: timeval {
220- tv_sec : 0 ,
221- tv_usec : 0 ,
222- }
223- }
224- } ;
225- setsockopt ( socket, libc:: SOL_SOCKET , kind, timeout)
226- }
227-
228- #[ cfg( target_os = "windows" ) ]
229- fn timeout ( socket : & Socket , kind : libc:: c_int ) -> io:: Result < Option < Duration > > {
230- let raw: libc:: DWORD = try!( getsockopt ( socket, libc:: SOL_SOCKET , kind) ) ;
231- if raw == 0 {
232- Ok ( None )
233- } else {
234- let secs = raw / 1000 ;
235- let nsec = ( raw % 1000 ) * 1000000 ;
236- Ok ( Some ( Duration :: new ( secs as u64 , nsec as u32 ) ) )
237- }
238- }
239-
240- #[ cfg( not( target_os = "windows" ) ) ]
241- fn timeout ( socket : & Socket , kind : libc:: c_int ) -> io:: Result < Option < Duration > > {
242- let raw: libc:: timeval = try!( getsockopt ( socket, libc:: SOL_SOCKET , kind) ) ;
243- if raw. tv_sec == 0 && raw. tv_usec == 0 {
244- Ok ( None )
245- } else {
246- let sec = raw. tv_sec as u64 ;
247- let nsec = ( raw. tv_usec as u32 ) * 1000 ;
248- Ok ( Some ( Duration :: new ( sec, nsec) ) )
249- }
250- }
251-
252166////////////////////////////////////////////////////////////////////////////////
253167// TCP streams
254168////////////////////////////////////////////////////////////////////////////////
@@ -307,19 +221,19 @@ impl TcpStream {
307221 }
308222
309223 pub fn set_read_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
310- set_timeout ( & self . inner , dur, libc:: SO_RCVTIMEO )
224+ self . inner . set_timeout ( dur, libc:: SO_RCVTIMEO )
311225 }
312226
313227 pub fn set_write_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
314- set_timeout ( & self . inner , dur, libc:: SO_SNDTIMEO )
228+ self . inner . set_timeout ( dur, libc:: SO_SNDTIMEO )
315229 }
316230
317231 pub fn read_timeout ( & self ) -> io:: Result < Option < Duration > > {
318- timeout ( & self . inner , libc:: SO_RCVTIMEO )
232+ self . inner . timeout ( libc:: SO_RCVTIMEO )
319233 }
320234
321235 pub fn write_timeout ( & self ) -> io:: Result < Option < Duration > > {
322- timeout ( & self . inner , libc:: SO_SNDTIMEO )
236+ self . inner . timeout ( libc:: SO_SNDTIMEO )
323237 }
324238
325239 pub fn read ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
@@ -575,19 +489,19 @@ impl UdpSocket {
575489 }
576490
577491 pub fn set_read_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
578- set_timeout ( & self . inner , dur, libc:: SO_RCVTIMEO )
492+ self . inner . set_timeout ( dur, libc:: SO_RCVTIMEO )
579493 }
580494
581495 pub fn set_write_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
582- set_timeout ( & self . inner , dur, libc:: SO_SNDTIMEO )
496+ self . inner . set_timeout ( dur, libc:: SO_SNDTIMEO )
583497 }
584498
585499 pub fn read_timeout ( & self ) -> io:: Result < Option < Duration > > {
586- timeout ( & self . inner , libc:: SO_RCVTIMEO )
500+ self . inner . timeout ( libc:: SO_RCVTIMEO )
587501 }
588502
589503 pub fn write_timeout ( & self ) -> io:: Result < Option < Duration > > {
590- timeout ( & self . inner , libc:: SO_SNDTIMEO )
504+ self . inner . timeout ( libc:: SO_SNDTIMEO )
591505 }
592506}
593507
0 commit comments