Ruby
2.1.3p242(2014-09-19revision47630)
Main Page
Modules
Data Structures
Files
File List
Globals
ext
io
wait
wait.c
Go to the documentation of this file.
1
/**********************************************************************
2
3
io/wait.c -
4
5
$Author: zzak $
6
created at: Tue Aug 28 09:08:06 JST 2001
7
8
All the files in this distribution are covered under the Ruby's
9
license (see the file COPYING).
10
11
**********************************************************************/
12
13
#include "
ruby.h
"
14
#include "
ruby/io.h
"
15
16
#include <sys/types.h>
17
#if defined(HAVE_UNISTD_H) && (defined(__sun))
18
#include <
unistd.h
>
19
#endif
20
#if defined(HAVE_SYS_IOCTL_H)
21
#include <sys/ioctl.h>
22
#endif
23
#if defined(FIONREAD_HEADER)
24
#include FIONREAD_HEADER
25
#endif
26
27
#ifdef HAVE_RB_W32_IOCTLSOCKET
28
#define ioctl ioctlsocket
29
#define ioctl_arg u_long
30
#define ioctl_arg2num(i) ULONG2NUM(i)
31
#else
32
#define ioctl_arg int
33
#define ioctl_arg2num(i) INT2NUM(i)
34
#endif
35
36
#ifdef HAVE_RB_W32_IS_SOCKET
37
#define FIONREAD_POSSIBLE_P(fd) rb_w32_is_socket(fd)
38
#else
39
#define FIONREAD_POSSIBLE_P(fd) ((void)(fd),Qtrue)
40
#endif
41
42
static
VALUE
io_ready_p
_
((
VALUE
io));
43
static
VALUE
io_wait_readable
_
((
int
argc
,
VALUE
*
argv
,
VALUE
io));
44
static
VALUE
io_wait_writable
_
((
int
argc
,
VALUE
*
argv
,
VALUE
io));
45
void
Init_wait
_
((
void
));
46
47
/*
48
* call-seq:
49
* io.nread -> int
50
*
51
* Returns number of bytes that can be read without blocking.
52
* Returns zero if no information available.
53
*/
54
55
static
VALUE
56
io_nread
(
VALUE
io)
57
{
58
rb_io_t
*fptr;
59
int
len;
60
ioctl_arg
n;
61
62
GetOpenFile
(io, fptr);
63
rb_io_check_readable
(fptr);
64
len =
rb_io_read_pending
(fptr);
65
if
(len > 0)
return
len;
66
if
(!
FIONREAD_POSSIBLE_P
(fptr->
fd
))
return
INT2FIX
(0);
67
if
(
ioctl
(fptr->
fd
, FIONREAD, &n))
return
INT2FIX
(0);
68
if
(n > 0)
return
ioctl_arg2num
(n);
69
return
INT2FIX
(0);
70
}
71
72
/*
73
* call-seq:
74
* io.ready? -> true, false or nil
75
*
76
* Returns true if input available without blocking, or false.
77
* Returns nil if no information available.
78
*/
79
80
static
VALUE
81
io_ready_p
(
VALUE
io)
82
{
83
rb_io_t
*fptr;
84
ioctl_arg
n;
85
86
GetOpenFile
(io, fptr);
87
rb_io_check_readable
(fptr);
88
if
(
rb_io_read_pending
(fptr))
return
Qtrue
;
89
if
(!
FIONREAD_POSSIBLE_P
(fptr->
fd
))
return
Qnil
;
90
if
(
ioctl
(fptr->
fd
, FIONREAD, &n))
return
Qnil
;
91
if
(n > 0)
return
Qtrue
;
92
return
Qfalse
;
93
}
94
95
/*
96
* call-seq:
97
* io.wait -> IO, true, false or nil
98
* io.wait(timeout) -> IO, true, false or nil
99
* io.wait_readable -> IO, true, false or nil
100
* io.wait_readable(timeout) -> IO, true, false or nil
101
*
102
* Waits until input is available or times out and returns self or nil when
103
* EOF is reached.
104
*/
105
106
static
VALUE
107
io_wait_readable
(
int
argc
,
VALUE
*
argv
,
VALUE
io)
108
{
109
rb_io_t
*fptr;
110
int
i;
111
ioctl_arg
n;
112
VALUE
timeout;
113
struct
timeval
timerec;
114
struct
timeval
*tv;
115
116
GetOpenFile
(io, fptr);
117
rb_io_check_readable
(fptr);
118
rb_scan_args
(argc, argv,
"01"
, &timeout);
119
if
(
NIL_P
(timeout)) {
120
tv =
NULL
;
121
}
122
else
{
123
timerec =
rb_time_interval
(timeout);
124
tv = &timerec;
125
}
126
127
if
(
rb_io_read_pending
(fptr))
return
Qtrue
;
128
if
(!
FIONREAD_POSSIBLE_P
(fptr->
fd
))
return
Qfalse
;
129
i =
rb_wait_for_single_fd
(fptr->
fd
,
RB_WAITFD_IN
, tv);
130
if
(i < 0)
131
rb_sys_fail
(0);
132
rb_io_check_closed
(fptr);
133
if
(
ioctl
(fptr->
fd
, FIONREAD, &n))
rb_sys_fail
(0);
134
if
(n > 0)
return
io;
135
return
Qnil
;
136
}
137
138
/*
139
* call-seq:
140
* io.wait_writable -> IO
141
* io.wait_writable(timeout) -> IO or nil
142
*
143
* Waits until IO writable is available or times out and returns self or
144
* nil when EOF is reached.
145
*/
146
static
VALUE
147
io_wait_writable
(
int
argc
,
VALUE
*
argv
,
VALUE
io)
148
{
149
rb_io_t
*fptr;
150
int
i;
151
VALUE
timeout;
152
struct
timeval
timerec;
153
struct
timeval
*tv;
154
155
GetOpenFile
(io, fptr);
156
rb_io_check_writable
(fptr);
157
rb_scan_args
(argc, argv,
"01"
, &timeout);
158
if
(
NIL_P
(timeout)) {
159
tv =
NULL
;
160
}
161
else
{
162
timerec =
rb_time_interval
(timeout);
163
tv = &timerec;
164
}
165
166
i =
rb_wait_for_single_fd
(fptr->
fd
,
RB_WAITFD_OUT
, tv);
167
if
(i < 0)
168
rb_sys_fail
(0);
169
rb_io_check_closed
(fptr);
170
if
(i &
RB_WAITFD_OUT
)
171
return
io;
172
return
Qnil
;
173
}
174
175
/*
176
* IO wait methods
177
*/
178
179
void
180
Init_wait
()
181
{
182
rb_define_method
(
rb_cIO
,
"nread"
,
io_nread
, 0);
183
rb_define_method
(
rb_cIO
,
"ready?"
,
io_ready_p
, 0);
184
rb_define_method
(
rb_cIO
,
"wait"
,
io_wait_readable
, -1);
185
rb_define_method
(
rb_cIO
,
"wait_readable"
,
io_wait_readable
, -1);
186
rb_define_method
(
rb_cIO
,
"wait_writable"
,
io_wait_writable
, -1);
187
}
188
Generated on Mon Sep 22 2014 11:05:22 for Ruby by
1.8.2