Skip to content
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

Add IO#readable?, BasicSocket#readable? and StringIO#readable?. #10516

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
35 changes: 35 additions & 0 deletions ext/socket/basicsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,39 @@ bsock_do_not_rev_lookup_set(VALUE self, VALUE val)
return val;
}

static VALUE
bsock_readable_p(VALUE self)
{
VALUE result = rb_call_super(0, NULL);

if (RTEST(result)) {
rb_io_t *fptr;
RB_IO_POINTER(self, fptr);

char buffer[1];
#ifdef MSG_DONTWAIT
int result = recv(fptr->fd, buffer, 1, MSG_PEEK | MSG_DONTWAIT);
#else
rb_io_set_nonblock(fptr);
int result = recv(fptr->fd, buffer, 1, MSG_PEEK);
#endif

if (result > 0) {
return Qtrue;
}

if (result == 0) {
return Qfalse;
}

if (result == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
return Qtrue;
}
} else {
return Qfalse;
}
}

void
rsock_init_basicsocket(void)
{
Expand Down Expand Up @@ -768,6 +801,8 @@ rsock_init_basicsocket(void)
rb_define_method(rb_cBasicSocket, "do_not_reverse_lookup", bsock_do_not_reverse_lookup, 0);
rb_define_method(rb_cBasicSocket, "do_not_reverse_lookup=", bsock_do_not_reverse_lookup_set, 1);

rb_define_method(rb_cBasicSocket, "readable?", bsock_readable_p, 0);

/* for ext/socket/lib/socket.rb use only: */
rb_define_private_method(rb_cBasicSocket,
"__recv_nonblock", bsock_recv_nonblock, 4);
Expand Down
9 changes: 9 additions & 0 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,14 @@ strio_eof(VALUE self)
return Qtrue;
}

static VALUE
strio_readable_p(VALUE self)
{
if (strio_to_read(self)) return Qtrue;
return Qfalse;

Comment on lines +644 to +646
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return RBOOL(strio_to_read(self));

}

/* :nodoc: */
static VALUE
strio_copy(VALUE copy, VALUE orig)
Expand Down Expand Up @@ -1936,6 +1944,7 @@ Init_stringio(void)
rb_define_method(StringIO, "closed_write?", strio_closed_write, 0);
rb_define_method(StringIO, "eof", strio_eof, 0);
rb_define_method(StringIO, "eof?", strio_eof, 0);
rb_define_method(StringIO, "readable?", strio_readable_p, 0);
/* call-seq: strio.fcntl */
rb_define_method(StringIO, "fcntl", strio_fcntl, -1);
/* call-seq: strio.flush -> strio */
Expand Down
15 changes: 15 additions & 0 deletions io.c
Original file line number Diff line number Diff line change
Expand Up @@ -2623,6 +2623,20 @@ io_fillbuf(rb_io_t *fptr)
return 0;
}

VALUE
rb_io_readable_p(VALUE io)
{
rb_io_t *fptr;
RB_IO_POINTER(io, fptr);

rb_io_check_char_readable(fptr);

if (READ_CHAR_PENDING(fptr)) return Qtrue;
if (READ_DATA_PENDING(fptr)) return Qtrue;

return RBOOL(fptr->fd >= 0);
}

/*
* call-seq:
* eof -> true or false
Expand Down Expand Up @@ -15707,6 +15721,7 @@ Init_IO(void)
rb_define_method(rb_cIO, "rewind", rb_io_rewind, 0);
rb_define_method(rb_cIO, "pos", rb_io_tell, 0);
rb_define_method(rb_cIO, "pos=", rb_io_set_pos, 1);
rb_define_method(rb_cIO, "readable?", rb_io_readable_p, 0);
rb_define_method(rb_cIO, "eof", rb_io_eof, 0);
rb_define_method(rb_cIO, "eof?", rb_io_eof, 0);

Expand Down